/
HandlerTestBase.cs
82 lines (67 loc) · 1.91 KB
/
HandlerTestBase.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
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests
{
public partial class HandlerTestBase : TestBase, IDisposable
{
private MauiApp _mauiApp;
private IMauiContext _mauiContext;
private bool _isCreated;
public void EnsureHandlerCreated(Action<MauiAppBuilder> additionalCreationActions = null)
{
if (_isCreated)
{
return;
}
_isCreated = true;
var appBuilder = MauiApp
.CreateBuilder();
additionalCreationActions?.Invoke(appBuilder);
_mauiApp = appBuilder.Build();
_mauiContext = new ContextStub(_mauiApp.Services);
}
public void Dispose()
{
((IDisposable)_mauiApp)?.Dispose();
_mauiApp = null;
_mauiContext = null;
}
protected IMauiContext MauiContext
{
get
{
EnsureHandlerCreated();
return _mauiContext;
}
}
protected THandler CreateHandler<THandler>(IView view)
where THandler : IViewHandler
{
var handler = Activator.CreateInstance<THandler>();
handler.SetMauiContext(MauiContext);
handler.SetVirtualView(view);
view.Handler = handler;
var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity);
view.Arrange(new Rect(0, 0, size.Width, size.Height));
handler.PlatformArrange(view.Frame);
return handler;
}
protected async Task<THandler> CreateHandlerAsync<THandler>(IView view) where THandler : IViewHandler =>
await InvokeOnMainThreadAsync(() => CreateHandler<THandler>(view));
protected Task<TValue> GetValueAsync<TValue, THandler>(IView view, Func<THandler, TValue> func)
where THandler : IViewHandler
{
return InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<THandler>(view);
return func(handler);
});
}
}
}