forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlsHandlerTestBase.cs
380 lines (318 loc) · 11.6 KB
/
ControlsHandlerTestBase.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
376
377
378
379
380
using System;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Devices;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using Microsoft.Maui.TestUtils.DeviceTests.Runners;
using Xunit;
#if ANDROID || IOS || MACCATALYST
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
#endif
namespace Microsoft.Maui.DeviceTests
{
public partial class ControlsHandlerTestBase : HandlerTestBase, IDisposable
{
// In order to run any page level tests android needs to add itself to the decor view inside a new fragment
// that way all the lifecycle events related to being attached to the window will fire
// adding/removing that many fragments in parallel to the decor view was causing the tests to be unreliable
// That being said...
// There's definitely a chance that the code written to manage this process could be improved
public const string RunInNewWindowCollection = "Serialize test because it has to add itself to the main window";
protected override MauiAppBuilder ConfigureBuilder(MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.TryAddSingleton<IApplication>((_) => new ApplicationStub());
return mauiAppBuilder.ConfigureTestBuilder();
}
protected void SetupShellHandlers(IMauiHandlersCollection handlers)
{
handlers.TryAddHandler(typeof(Controls.Shell), typeof(ShellHandler));
handlers.TryAddHandler<Layout, LayoutHandler>();
handlers.TryAddHandler<Image, ImageHandler>();
handlers.TryAddHandler<Label, LabelHandler>();
handlers.TryAddHandler<Page, PageHandler>();
handlers.TryAddHandler(typeof(Toolbar), typeof(ToolbarHandler));
handlers.TryAddHandler(typeof(MenuBar), typeof(MenuBarHandler));
handlers.TryAddHandler(typeof(MenuBarItem), typeof(MenuBarItemHandler));
handlers.TryAddHandler(typeof(MenuFlyoutItem), typeof(MenuFlyoutItemHandler));
handlers.TryAddHandler(typeof(MenuFlyoutSubItem), typeof(MenuFlyoutSubItemHandler));
handlers.TryAddHandler<ScrollView, ScrollViewHandler>();
#if WINDOWS
handlers.TryAddHandler(typeof(ShellItem), typeof(ShellItemHandler));
handlers.TryAddHandler(typeof(ShellSection), typeof(ShellSectionHandler));
handlers.TryAddHandler(typeof(ShellContent), typeof(ShellContentHandler));
#endif
}
protected THandler CreateHandler<THandler>(IElement view)
where THandler : IElementHandler, new()
{
return CreateHandler<THandler>(view, MauiContext);
}
protected async Task<THandler> CreateHandlerAsync<THandler>(IElement view)
where THandler : IElementHandler, new() =>
await InvokeOnMainThreadAsync(() => CreateHandler<THandler>(view));
protected Task<TValue> GetValueAsync<TValue, THandler>(IElement view, Func<THandler, TValue> func)
where THandler : IElementHandler, new()
{
return InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<THandler>(view);
return func(handler);
});
}
protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Action<THandler> action)
where THandler : class, IElementHandler
{
return CreateHandlerAndAddToWindow<THandler>(view, handler =>
{
action(handler);
return Task.CompletedTask;
});
}
static SemaphoreSlim _takeOverMainContentSempahore = new SemaphoreSlim(1);
protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Func<THandler, Task> action, IMauiContext mauiContext = null)
where THandler : class, IElementHandler
{
mauiContext ??= MauiContext;
return InvokeOnMainThreadAsync(async () =>
{
IWindow window = null;
var application = mauiContext.Services.GetService<IApplication>();
if (view is IWindow w)
{
window = w;
}
else if (view is Page page)
{
window = new Controls.Window(page);
}
else
{
window = new Controls.Window(new ContentPage() { Content = (View)view });
}
if (application is ApplicationStub appStub)
{
appStub.SetWindow((Window)window);
// Trigger the work flow of creating a window
_ = application.CreateWindow(null);
}
try
{
await _takeOverMainContentSempahore.WaitAsync();
await SetupWindowForTests<THandler>(window, async () =>
{
IView content = window.Content;
if (content is IPageContainer<Page> pc)
{
content = pc.CurrentPage;
if (content == null)
{
// This is mainly a timing issue with Shell.
// Basically the `CurrentPage` on Shell isn't initialized until it's
// actually navigated to because it's a DataTemplate.
// The CurrentPage doesn't come into existence until the platform requests it.
// The initial `Navigated` events on Shell all fire a bit too early as well.
// Ideally I'd just use that instead of having to add a delay.
await Task.Delay(100);
content = pc.CurrentPage;
}
_ = content ?? throw new InvalidOperationException("Current Page Not Initialized");
}
await OnLoadedAsync(content as VisualElement);
#if WINDOWS
await Task.Delay(10);
#endif
if (typeof(THandler).IsAssignableFrom(window.Handler.GetType()))
await action((THandler)window.Handler);
else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType()))
await action((THandler)window.Content.Handler);
else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType()))
await action((THandler)cp.Content.Handler);
else
throw new Exception($"I can't work with {typeof(THandler)}");
}, mauiContext);
}
finally
{
_takeOverMainContentSempahore.Release();
}
});
}
async protected Task ValidatePropertyInitValue<TValue, THandler>(
IView view,
Func<TValue> GetValue,
Func<THandler, TValue> GetPlatformValue,
TValue expectedValue)
where THandler : IElementHandler, new()
{
var values = await GetValueAsync(view, (THandler handler) =>
{
return new
{
ViewValue = GetValue(),
PlatformViewValue = GetPlatformValue(handler)
};
});
Assert.Equal(expectedValue, values.ViewValue);
Assert.Equal(expectedValue, values.PlatformViewValue);
}
async protected Task ValidatePropertyUpdatesValue<TValue, THandler>(
IView view,
string property,
Func<THandler, TValue> GetPlatformValue,
TValue expectedSetValue,
TValue expectedUnsetValue)
where THandler : IElementHandler, new()
{
var propInfo = view.GetType().GetProperty(property);
// set initial values
propInfo.SetValue(view, expectedSetValue);
var (handler, viewVal, nativeVal) = await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<THandler>(view);
return (handler, (TValue)propInfo.GetValue(view), GetPlatformValue(handler));
});
Assert.Equal(expectedSetValue, viewVal);
Assert.Equal(expectedSetValue, nativeVal);
await ValidatePropertyUpdatesAfterInitValue(handler, property, GetPlatformValue, expectedSetValue, expectedUnsetValue);
}
async protected Task ValidatePropertyUpdatesAfterInitValue<TValue, THandler>(
THandler handler,
string property,
Func<THandler, TValue> GetPlatformValue,
TValue expectedSetValue,
TValue expectedUnsetValue)
where THandler : IElementHandler
{
var view = handler.VirtualView;
var propInfo = handler.VirtualView.GetType().GetProperty(property);
// confirm can update
var (viewVal, nativeVal) = await InvokeOnMainThreadAsync(() =>
{
propInfo.SetValue(view, expectedUnsetValue);
handler.UpdateValue(property);
return ((TValue)propInfo.GetValue(view), GetPlatformValue(handler));
});
Assert.Equal(expectedUnsetValue, viewVal);
Assert.Equal(expectedUnsetValue, nativeVal);
// confirm can revert
(viewVal, nativeVal) = await InvokeOnMainThreadAsync(() =>
{
propInfo.SetValue(view, expectedSetValue);
handler.UpdateValue(property);
return ((TValue)propInfo.GetValue(view), GetPlatformValue(handler));
});
Assert.Equal(expectedSetValue, viewVal);
Assert.Equal(expectedSetValue, nativeVal);
}
protected void OnLoaded(VisualElement frameworkElement, Action action)
{
if (frameworkElement.IsLoaded)
{
action();
return;
}
EventHandler loaded = null;
loaded = (_, __) =>
{
if (loaded != null)
frameworkElement.Loaded -= loaded;
action();
};
frameworkElement.Loaded += loaded;
}
protected void OnUnloaded(VisualElement frameworkElement, Action action)
{
if (!frameworkElement.IsLoaded)
{
action();
return;
}
EventHandler unloaded = null;
unloaded = (_, __) =>
{
if (unloaded != null)
frameworkElement.Unloaded -= unloaded;
action();
};
frameworkElement.Unloaded += unloaded;
}
protected Task OnUnloadedAsync(VisualElement frameworkElement, TimeSpan? timeOut = null)
{
timeOut = timeOut ?? TimeSpan.FromSeconds(2);
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
OnUnloaded(frameworkElement, () => taskCompletionSource.SetResult(true));
return taskCompletionSource.Task.WaitAsync(timeOut.Value);
}
protected Task OnLoadedAsync(VisualElement frameworkElement, TimeSpan? timeOut = null)
{
timeOut = timeOut ?? TimeSpan.FromSeconds(2);
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
OnLoaded(frameworkElement, () => taskCompletionSource.SetResult(true));
return taskCompletionSource.Task.WaitAsync(timeOut.Value);
}
protected async Task OnNavigatedToAsync(Page page, TimeSpan? timeOut = null)
{
await OnLoadedAsync(page, timeOut);
if (page.HasNavigatedTo)
return;
timeOut = timeOut ?? TimeSpan.FromSeconds(2);
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
page.NavigatedTo += NavigatedTo;
await taskCompletionSource.Task.WaitAsync(timeOut.Value);
void NavigatedTo(object sender, NavigatedToEventArgs e)
{
taskCompletionSource.SetResult(true);
page.NavigatedTo -= NavigatedTo;
}
}
protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSpan? timeOut = null)
{
if (frameworkElement.Frame.Height > 0 &&
frameworkElement.Frame.Width > 0)
{
return;
}
timeOut = timeOut ?? TimeSpan.FromSeconds(2);
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
frameworkElement.BatchCommitted += OnBatchCommitted;
await taskCompletionSource.Task.WaitAsync(timeOut.Value);
// Wait for the layout to propagate to the platform
await AssertionExtensions.Wait(
() => !frameworkElement.GetBoundingBox().Size.Equals(Size.Zero)
);
void OnBatchCommitted(object sender, Controls.Internals.EventArg<VisualElement> e)
{
if (frameworkElement.Frame.Height <= 0 ||
frameworkElement.Frame.Width <= 0)
{
return;
}
frameworkElement.BatchCommitted -= OnBatchCommitted;
taskCompletionSource.SetResult(true);
}
}
protected IToolbar GetToolbar(IElementHandler handler)
{
return (handler.VirtualView as IWindowController)
.Window
.GetVisualTreeDescendants()
.OfType<IToolbarElement>()
.SingleOrDefault(x => x.Toolbar != null)
?.Toolbar;
}
}
}