/
HandlerTestBaseOfT.Tests.cs
375 lines (334 loc) · 9.69 KB
/
HandlerTestBaseOfT.Tests.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Media;
using Xunit;
using Xunit.Sdk;
namespace Microsoft.Maui.DeviceTests
{
public abstract partial class HandlerTestBase<THandler, TStub>
{
[Fact]
public async Task DisconnectHandlerDoesntCrash()
{
var handler = await CreateHandlerAsync(new TStub()) as IPlatformViewHandler;
await InvokeOnMainThreadAsync(() =>
{
handler.DisconnectHandler();
});
}
[Fact(DisplayName = "Automation Id is set correctly")]
public async Task SetAutomationId()
{
var view = new TStub
{
AutomationId = "TestId"
};
var id = await GetValueAsync(view, handler => GetAutomationId(handler));
Assert.Equal(view.AutomationId, id);
}
[Theory(DisplayName = "FlowDirection is set correctly")]
[InlineData(FlowDirection.LeftToRight)]
[InlineData(FlowDirection.RightToLeft)]
public async Task SetFlowDirection(FlowDirection flowDirection)
{
var view = new TStub
{
FlowDirection = flowDirection
};
var id = await GetValueAsync(view, handler => GetFlowDirection(handler));
Assert.Equal(flowDirection, id);
}
[Theory(DisplayName = "Opacity is set correctly")]
[InlineData(0)]
[InlineData(0.25)]
[InlineData(0.5)]
[InlineData(0.75)]
[InlineData(1)]
public async Task SetOpacity(double opacity)
{
var view = new TStub
{
Opacity = opacity
};
var id = await GetValueAsync(view, handler => GetOpacity(handler));
Assert.Equal(view.Opacity, id);
}
[Theory(DisplayName = "Visibility is set correctly")]
[InlineData(Visibility.Collapsed)]
[InlineData(Visibility.Hidden)]
public virtual async Task SetVisibility(Visibility visibility)
{
var view = new TStub
{
Visibility = visibility
};
var id = await GetValueAsync(view, handler => GetVisibility(handler));
Assert.Equal(view.Visibility, id);
}
[Fact(DisplayName = "Setting Semantic Description makes element accessible"
#if IOS
, Skip = "This is failing on iOS: https://github.com/dotnet/maui/issues/19122"
#endif)]
public async virtual Task SettingSemanticDescriptionMakesElementAccessible()
{
var view = new TStub();
MockAccessibilityExpectations(view);
view.Semantics.Description = "Test";
var important = await GetValueAsync(view, handler => view.IsAccessibilityElement());
Assert.True(important);
}
[Fact(DisplayName = "Setting Semantic Hint makes element accessible"
#if IOS
, Skip = "This is failing on iOS: https://github.com/dotnet/maui/issues/19122"
#endif)]
public async virtual Task SettingSemanticHintMakesElementAccessible()
{
var view = new TStub();
MockAccessibilityExpectations(view);
view.Semantics.Hint = "Test";
var important = await GetValueAsync(view, handler => view.IsAccessibilityElement());
Assert.True(important);
}
[Fact(DisplayName = "Semantic Description is set correctly"
#if ANDROID
, Skip = "This value can't be validated through automated tests"
#endif
)]
public async Task SetSemanticDescription()
{
var view = new TStub();
view.Semantics.Description = "Test";
var id = await GetValueAsync(view, handler => GetSemanticDescription(handler));
Assert.Equal(view.Semantics.Description, id);
}
[Fact(DisplayName = "Semantic Hint is set correctly"
#if ANDROID
, Skip = "This value can't be validated through automated tests"
#endif
)]
public async Task SetSemanticHint()
{
var view = new TStub();
view.Semantics.Hint = "Test";
var id = await GetValueAsync(view, handler => GetSemanticHint(handler));
Assert.Equal(view.Semantics.Hint, id);
}
[Fact(DisplayName = "Semantic Heading is set correctly"
#if IOS
, Skip = "This is failing on iOS: https://github.com/dotnet/maui/issues/19122"
#endif
)]
public async Task SetSemanticHeading()
{
var view = new TStub();
view.Semantics.HeadingLevel = SemanticHeadingLevel.Level1;
var id = await GetValueAsync(view, handler => GetSemanticHeading(handler));
Assert.Equal(view.Semantics.HeadingLevel, id);
}
[Fact(DisplayName = "Null Semantics Doesnt throw exception")]
public async Task NullSemanticsClass()
{
var view = new TStub
{
Semantics = null,
AutomationId = "CreationFailed"
};
var id = await GetValueAsync(view, handler => GetAutomationId(handler));
Assert.Equal(view.AutomationId, id);
}
[Fact(DisplayName = "Clip Initializes ContainerView Correctly")]
public async virtual Task ContainerViewInitializesCorrectly()
{
var view = new TStub
{
Height = 100,
Width = 100,
Background = new SolidPaintStub(Colors.Red),
Clip = new EllipseGeometryStub(new Graphics.Point(50, 50), 50, 50)
};
await CreateHandlerAsync(view);
await view.AssertHasContainer(true);
}
[Fact(DisplayName = "ContainerView Remains If Shadow Mapper Runs Again")]
public virtual async Task ContainerViewRemainsIfShadowMapperRunsAgain()
{
var view = new TStub
{
Height = 100,
Width = 100,
Background = new SolidPaintStub(Colors.Red),
Clip = new EllipseGeometryStub(new Graphics.Point(50, 50), 50, 50)
};
var handler = await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler(view);
handler.UpdateValue(nameof(IView.Shadow));
return handler;
});
await view.AssertHasContainer(true);
}
[Fact(DisplayName = "ContainerView Adds And Removes"
#if WINDOWS
, Skip = "Failing on Windows"
#endif
)]
public virtual async Task ContainerViewAddsAndRemoves()
{
var view = new TStub
{
Height = 100,
Width = 100
};
await InvokeOnMainThreadAsync(async () =>
{
var handler = CreateHandler(view);
// This is a view that always has a container
// so there's nothing to test here
if (handler.HasContainer)
return;
await AttachAndRun(view,
async (handler) =>
{
await view.AssertHasContainer(false);
view.Clip = new EllipseGeometryStub(new Graphics.Point(50, 50), 50, 50);
handler.UpdateValue(nameof(IView.Clip));
await Task.Delay(10);
await view.AssertHasContainer(true);
view.Clip = null;
handler.UpdateValue(nameof(IView.Clip));
await Task.Delay(10);
await view.AssertHasContainer(false);
});
return;
});
}
[Theory(DisplayName = "Native View Bounds are not empty"
#if WINDOWS
, Skip = "https://github.com/dotnet/maui/issues/9054"
#endif
)]
[InlineData(1)]
[InlineData(100)]
[InlineData(1000)]
public async Task ReturnsNonEmptyPlatformViewBounds(int size)
{
var view = new TStub()
{
Height = size,
Width = size,
};
var platformViewBounds = await GetValueAsync(view, handler => GetPlatformViewBounds(handler));
Assert.NotEqual(platformViewBounds, new Graphics.Rect());
}
[Theory(DisplayName = "Native View Bounding Box is not empty"
#if WINDOWS
, Skip = "https://github.com/dotnet/maui/issues/9054"
#endif
)]
[InlineData(1)]
[InlineData(100)]
[InlineData(1000)]
public virtual async Task ReturnsNonEmptyNativeBoundingBox(int size)
{
var view = new TStub()
{
Height = size,
Width = size,
};
var nativeBoundingBox = await GetValueAsync(view, handler => GetBoundingBox(handler));
Assert.NotEqual(nativeBoundingBox, Graphics.Rect.Zero);
// Currently there's an issue with label/progress where they don't set the frame size to
// the explicit Width and Height values set
// https://github.com/dotnet/maui/issues/7935
if (view is ILabel)
{
// TODO:
}
else if (view is IBorderView)
{
// TODO:
}
#if IOS || MACCATALYST
else if (view is ISearchBar)
{
//Search bar currently only measures to one size
//https://github.com/dotnet/maui/issues/11136
}
#endif
#if ANDROID
else if (size == 1 && view is ISwitch)
{
// https://github.com/dotnet/maui/issues/11020
}
#endif
else if (view is IProgress)
{
AssertWithinTolerance(size, nativeBoundingBox.Size.Width);
}
else
{
var expectedSize = new Size(size, size);
AssertWithinTolerance(expectedSize, nativeBoundingBox.Size);
}
}
[Theory(DisplayName = "PlatformView Transforms are not empty")]
[InlineData(1)]
[InlineData(100)]
[InlineData(1000)]
public async Task ReturnsNonEmptyPlatformViewTransforms(int size)
{
var view = new TStub()
{
Height = size,
Width = size,
Scale = .5,
Rotation = size,
};
var platformViewTransform = await GetValueAsync(view, handler => GetViewTransform(handler));
Assert.NotEqual(platformViewTransform, new System.Numerics.Matrix4x4());
}
[Theory(DisplayName = "View Renders To Image"
#if !ANDROID
, Skip = "iOS and Windows can't render elements to images from test runner. It's missing the required root windows."
#endif
)]
[InlineData(ScreenshotFormat.Jpeg)]
[InlineData(ScreenshotFormat.Png)]
public async Task RendersAsImage(ScreenshotFormat type)
{
var view = new TStub()
{
Height = 100,
Width = 100,
};
var result = await GetValueAsync(view, handler => handler.VirtualView.CaptureAsync());
Assert.NotNull(result);
using var stream = await result.OpenReadAsync(type);
Assert.True(stream.Length > 0);
}
[Fact]
public void HandlersHaveAllExpectedContructors()
{
bool hasBothMappers = false;
var constructors = typeof(THandler).GetConstructors();
foreach (var ctor in constructors)
{
var args = ctor.GetParameters();
if (args.Length == 2)
{
if (args[0].ParameterType.IsAssignableTo(typeof(IPropertyMapper)) &&
args[1].ParameterType.IsAssignableTo(typeof(CommandMapper)))
{
hasBothMappers = true;
}
}
}
Assert.True(hasBothMappers, "Missing constructor with IPropertyMapper and ICommandMapper");
}
}
}