/
PlatformBehaviorTests.cs
84 lines (74 loc) · 2.04 KB
/
PlatformBehaviorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Xunit;
#if IOS
using PlatformView = UIKit.UIView;
#elif MACOS
using PlatformView = AppKit.NSView;
#elif ANDROID
using PlatformView = Android.Views.View;
#elif WINDOWS
using PlatformView = Microsoft.UI.Xaml.FrameworkElement;
#elif NET6_0 || (NETSTANDARD || !PLATFORM)
using PlatformView = System.Object;
#endif
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Behavior)]
#if ANDROID || IOS
[Collection(HandlerTestBase.RunInNewWindowCollection)]
#endif
public partial class PlatformBehaviorTests : HandlerTestBase
{
[Fact]
public async Task BasicLoadUnloadingWorks()
{
var behavior = new PlatformBehaviorTest();
var editor = new Editor();
editor.Behaviors.Add(behavior);
IView layout = new VerticalStackLayout()
{
Children =
{
editor
}
};
Assert.False(behavior.IsAttached);
Assert.Equal(0, behavior.AttachedCount);
Assert.Equal(0, behavior.DetachedCount);
await CreateHandlerAndAddToWindow<LayoutHandler>(layout, (_) =>
{
Assert.True(behavior.IsAttached);
Assert.Equal(1, behavior.AttachedCount);
Assert.Equal(0, behavior.DetachedCount);
return Task.CompletedTask;
});
Assert.False(behavior.IsAttached);
Assert.Equal(1, behavior.AttachedCount);
Assert.Equal(1, behavior.DetachedCount);
}
class PlatformBehaviorTest : PlatformBehavior<Editor>
{
public int AttachedCount { get; private set; }
public int DetachedCount { get; private set; }
public bool IsAttached { get; private set; } = false;
protected override void OnAttachedTo(Editor bindable, PlatformView platformView)
{
base.OnAttachedTo(bindable, platformView);
IsAttached = true;
AttachedCount++;
}
protected override void OnDetachedFrom(Editor bindable, PlatformView platformView)
{
base.OnDetachedFrom(bindable, platformView);
IsAttached = false;
DetachedCount++;
}
}
}
}