-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathFocusHandlerTests.cs
111 lines (97 loc) · 3.07 KB
/
FocusHandlerTests.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Hosting;
using Xunit;
namespace Microsoft.Maui.DeviceTests
{
public abstract partial class FocusHandlerTests<THandler, TStub, TLayoutStub> : HandlerTestBasement<THandler, TStub>
where THandler : class, IViewHandler, new()
where TStub : IStubBase, new()
where TLayoutStub : IStubBase, ILayout, new()
{
[Fact]
public async Task FocusAndIsFocusedIsWorking()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handler =>
{
handler.AddHandler<TLayoutStub, LayoutHandler>();
handler.AddHandler<TStub, THandler>();
});
});
// create layout with 2 elements
TStub inputControl1;
TStub inputControl2;
var layout = new TLayoutStub
{
(inputControl1 = new TStub { Width = 100, Height = 50 }),
(inputControl2 = new TStub { Width = 100, Height = 50 })
};
layout.Width = 100;
layout.Height = 150;
await AttachAndRun<LayoutHandler>(layout, async (contentViewHandler) =>
{
var platform1 = inputControl1.ToPlatform();
var platform2 = inputControl2.ToPlatform();
// focus the first control
var result1 = inputControl1.Handler.InvokeWithResult(nameof(IView.Focus), new FocusRequest());
Assert.True(result1);
// assert
await inputControl1.WaitForFocused();
Assert.True(inputControl1.IsFocused);
Assert.False(inputControl2.IsFocused);
// focus the second control
var result2 = inputControl2.Handler.InvokeWithResult(nameof(IView.Focus), new FocusRequest());
Assert.True(result2);
// assert
await inputControl2.WaitForFocused();
Assert.False(inputControl1.IsFocused);
Assert.True(inputControl2.IsFocused);
});
}
// TODO: Android is not unfocusing
#if IOS || MACCATALYST || WINDOWS
[Fact]
public async Task UnfocusAndIsFocusedIsWorking()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handler =>
{
handler.AddHandler<TLayoutStub, LayoutHandler>();
handler.AddHandler<TStub, THandler>();
});
});
// create layout with 2 elements
TStub inputControl1;
TStub inputControl2;
var layout = new TLayoutStub
{
(inputControl1 = new TStub { Width = 100, Height = 50 }),
(inputControl2 = new TStub { Width = 100, Height = 50 })
};
layout.Width = 100;
layout.Height = 150;
await AttachAndRun<LayoutHandler>(layout, async (contentViewHandler) =>
{
var platform1 = inputControl1.ToPlatform();
var platform2 = inputControl2.ToPlatform();
// focus the first control
var result1 = inputControl1.Handler.InvokeWithResult(nameof(IView.Focus), new FocusRequest());
Assert.True(result1);
// assert
await inputControl1.WaitForFocused();
Assert.True(inputControl1.IsFocused);
Assert.False(inputControl2.IsFocused);
// UNfocus the first control (revert the focus)
inputControl1.Handler.Invoke(nameof(IView.Unfocus));
// assert
await inputControl1.WaitForUnFocused();
Assert.False(inputControl1.IsFocused);
Assert.False(inputControl2.IsFocused);
});
}
#endif
}
}