/
HandlerTestBase.Android.cs
233 lines (188 loc) · 6.97 KB
/
HandlerTestBase.Android.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
using System;
using System.Threading.Tasks;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using AndroidX.AppCompat.App;
using AndroidX.AppCompat.Graphics.Drawable;
using AndroidX.AppCompat.Widget;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Fragment.App;
using Google.Android.Material.AppBar;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Platform;
using ALayoutInflater = Android.Views.LayoutInflater;
using AView = Android.Views.View;
using AViewGroup = Android.Views.ViewGroup;
using ImportantForAccessibility = Android.Views.ImportantForAccessibility;
using Xunit;
namespace Microsoft.Maui.DeviceTests
{
public partial class HandlerTestBase
{
protected bool GetIsAccessibilityElement(IViewHandler viewHandler) =>
GetSemanticPlatformElement(viewHandler).ImportantForAccessibility == ImportantForAccessibility.Yes;
protected bool GetExcludedWithChildren(IViewHandler viewHandler) =>
GetSemanticPlatformElement(viewHandler).ImportantForAccessibility == ImportantForAccessibility.NoHideDescendants;
public AView GetSemanticPlatformElement(IViewHandler viewHandler)
{
if (viewHandler.PlatformView is AndroidX.AppCompat.Widget.SearchView sv)
return sv.FindViewById(Resource.Id.search_button)!;
return (AView)viewHandler.PlatformView;
}
static Drawable _decorDrawable;
Task SetupWindowForTests<THandler>(IWindow window, Func<Task> runTests)
where THandler : class, IElementHandler
{
return InvokeOnMainThreadAsync(async () =>
{
AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
_decorDrawable ??= rootView.Background;
var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);
var fragmentManager = MauiContext.GetFragmentManager();
var viewFragment = new WindowTestFragment(MauiContext, window);
try
{
linearLayoutCompat.Id = AView.GenerateViewId();
rootView.AddView(linearLayoutCompat);
fragmentManager
.BeginTransaction()
.Add(linearLayoutCompat.Id, viewFragment)
.Commit();
await viewFragment.FinishedLoading;
await runTests.Invoke();
}
finally
{
if (window.Handler != null)
{
window.Handler.DisconnectHandler();
}
fragmentManager
.BeginTransaction()
.Remove(viewFragment)
.Commit();
rootView.RemoveView(linearLayoutCompat);
await linearLayoutCompat.OnUnloadedAsync();
if (viewFragment.View != null)
await viewFragment.View.OnUnloadedAsync();
await viewFragment.FinishedDestroying;
// This is mainly to remove changes to the decor view that shell imposes
if (_decorDrawable != rootView.Background)
rootView.Background = _decorDrawable;
// Unset the Support Action bar if the calling code has set the support action bar
if (MauiContext.Context.GetActivity() is AppCompatActivity aca)
{
aca.SetSupportActionBar(null);
}
}
});
}
public bool ToolbarItemsMatch(
IElementHandler handler,
params ToolbarItem[] toolbarItems)
{
var toolbar = GetPlatformToolbar(handler);
var menu = toolbar.Menu;
Assert.Equal(toolbarItems.Length, menu.Size());
for (var i = 0; i < toolbarItems.Length; i++)
{
ToolbarItem toolbarItem = toolbarItems[i];
var primaryCommand = menu.GetItem(i);
Assert.Equal(toolbarItem.Text, $"{primaryCommand.TitleFormatted}");
}
return true;
}
protected AView GetTitleView(IElementHandler handler)
{
var toolbar = GetPlatformToolbar(handler);
var container = toolbar?.GetFirstChildOfType<Controls.Toolbar.Container>();
if (container != null && container.ChildCount > 0)
return container.GetChildAt(0);
return null;
}
protected MaterialToolbar GetPlatformToolbar(IElementHandler handler)
{
if (handler is Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer sr)
{
var shell = handler.VirtualView as Shell;
var currentPage = shell.CurrentPage;
var pagePlatformView = currentPage.Handler.PlatformView as AView;
var parentContainer = pagePlatformView.GetParentOfType<CoordinatorLayout>();
var toolbar = parentContainer.GetFirstChildOfType<MaterialToolbar>();
return toolbar;
}
else
{
return GetPlatformToolbar(handler.MauiContext);
}
}
protected MaterialToolbar GetPlatformToolbar(IMauiContext mauiContext)
{
var navManager = mauiContext.GetNavigationRootManager();
var appbarLayout =
navManager?.RootView?.FindViewById<AViewGroup>(Resource.Id.navigationlayout_appbar);
var toolBar = appbarLayout?.GetFirstChildOfType<MaterialToolbar>();
toolBar = toolBar ?? navManager.ToolbarElement?.Toolbar?.Handler?.PlatformView as
MaterialToolbar;
if (toolBar == null)
{
appbarLayout =
(navManager?.RootView as AViewGroup)?.GetFirstChildOfType<AppBarLayout>();
toolBar = appbarLayout?.GetFirstChildOfType<MaterialToolbar>();
}
return toolBar;
}
protected bool IsBackButtonVisible(IElementHandler handler)
{
if (GetPlatformToolbar(handler)?.NavigationIcon is DrawerArrowDrawable dad)
return dad.Progress == 1;
return false;
}
class WindowTestFragment : Fragment
{
TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
TaskCompletionSource<bool> _finishedDestroying = new TaskCompletionSource<bool>();
readonly IMauiContext _mauiContext;
readonly IWindow _window;
public IMauiContext ScopedMauiContext { get; set; }
public Task FinishedLoading => _taskCompletionSource.Task;
public Task FinishedDestroying => _taskCompletionSource.Task;
public FakeActivityRootView FakeActivityRootView { get; set; }
public WindowTestFragment(IMauiContext mauiContext, IWindow window)
{
_mauiContext = mauiContext;
_window = window;
}
public override AView OnCreateView(ALayoutInflater inflater, AViewGroup container, Bundle savedInstanceState)
{
ScopedMauiContext = _mauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager, registerNewNavigationRoot: true);
_ = _window.ToHandler(ScopedMauiContext);
var rootView = ScopedMauiContext.GetNavigationRootManager().RootView;
var decorView = RequireActivity().Window.DecorView;
rootView.LayoutParameters = new LinearLayoutCompat.LayoutParams(decorView.MeasuredWidth, decorView.MeasuredHeight);
FakeActivityRootView = new FakeActivityRootView(ScopedMauiContext.Context);
FakeActivityRootView.LayoutParameters = new LinearLayoutCompat.LayoutParams(decorView.MeasuredWidth, decorView.MeasuredHeight);
FakeActivityRootView.AddView(rootView);
return FakeActivityRootView;
}
public override void OnResume()
{
base.OnResume();
_taskCompletionSource.SetResult(true);
}
public override void OnDestroy()
{
base.OnDestroy();
_finishedDestroying.SetResult(true);
}
}
public class FakeActivityRootView : LinearLayoutCompat
{
public FakeActivityRootView(Context context) : base(context)
{
Id = AView.GenerateViewId();
}
}
}
}