/
ShellTests.cs
372 lines (329 loc) · 10 KB
/
ShellTests.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
#if !IOS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using Xunit;
#if ANDROID || IOS
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
#endif
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Shell)]
[Collection(HandlerTestBase.RunInNewWindowCollection)]
public partial class ShellTests : HandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Controls.Shell), typeof(ShellHandler));
handlers.AddHandler<Layout, LayoutHandler>();
handlers.AddHandler<Image, ImageHandler>();
handlers.AddHandler<Label, LabelHandler>();
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Toolbar, ToolbarHandler>();
handlers.AddHandler<MenuBar, MenuBarHandler>();
handlers.AddHandler<MenuBarItem, MenuBarItemHandler>();
handlers.AddHandler<MenuFlyoutItem, MenuFlyoutItemHandler>();
handlers.AddHandler<MenuFlyoutSubItem, MenuFlyoutSubItemHandler>();
handlers.AddHandler<NavigationPage, NavigationViewHandler>();
#if WINDOWS
handlers.AddHandler<ShellItem, ShellItemHandler>();
handlers.AddHandler<ShellSection, ShellSectionHandler>();
handlers.AddHandler<ShellContent, ShellContentHandler>();
#endif
});
});
}
[Fact(DisplayName = "FlyoutContent Renderers When FlyoutBehavior Starts As Locked")]
public async Task FlyoutContentRenderersWhenFlyoutBehaviorStartsAsLocked()
{
SetupBuilder();
var flyoutContent = new VerticalStackLayout() { Children = { new Label() { Text = "Rendered" } } };
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() } };
shell.FlyoutContent = flyoutContent;
shell.FlyoutBehavior = FlyoutBehavior.Locked;
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await OnFrameSetToNotEmpty(flyoutContent);
Assert.NotNull(flyoutContent.Handler);
Assert.True(flyoutContent.Frame.Width > 0);
Assert.True(flyoutContent.Frame.Height > 0);
});
}
#if !IOS
[Fact(DisplayName = "Flyout Starts as Open correctly")]
public async Task FlyoutIsPresented()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() } };
shell.FlyoutIsPresented = true;
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await CheckFlyoutState(handler, true);
shell.FlyoutIsPresented = false;
await CheckFlyoutState(handler, false);
});
}
#endif
[Fact(DisplayName = "Back Button Visibility Changes with push/pop")]
public async Task BackButtonVisibilityChangesWithPushPop()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new ContentPage();
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
Assert.False(IsBackButtonVisible(handler));
await shell.Navigation.PushAsync(new ContentPage());
Assert.True(IsBackButtonVisible(handler));
await shell.Navigation.PopAsync();
Assert.False(IsBackButtonVisible(handler));
});
}
[Fact(DisplayName = "Pushing the Same Page Disconnects Previous Toolbar Items")]
public async Task PushingTheSamePageUpdatesToolbar()
{
SetupBuilder();
bool canExecute = false;
var command = new Command(() => { }, () => canExecute);
var pushedPage = new ContentPage()
{
ToolbarItems =
{
new ToolbarItem()
{
Command = command
}
}
};
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new ContentPage();
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await shell.Navigation.PushAsync(pushedPage);
await shell.Navigation.PopAsync();
canExecute = true;
await shell.Navigation.PushAsync(pushedPage);
command.ChangeCanExecute();
});
}
[Fact(DisplayName = "Set Has Back Button")]
public async Task SetHasBackButton()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new ContentPage();
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
Assert.False(IsBackButtonVisible(shell.Handler));
await shell.Navigation.PushAsync(new ContentPage());
Assert.True(IsBackButtonVisible(shell.Handler));
BackButtonBehavior behavior = new BackButtonBehavior()
{
IsVisible = false
};
Shell.SetBackButtonBehavior(shell.CurrentPage, behavior);
Assert.False(IsBackButtonVisible(shell.Handler));
behavior.IsVisible = true;
NavigationPage.SetHasBackButton(shell.CurrentPage, true);
});
}
[Fact(DisplayName = "Empty Shell")]
public async Task DetailsViewUpdates()
{
SetupBuilder();
var shell = await InvokeOnMainThreadAsync<Shell>(() =>
{
return new Shell()
{
Items = { new ContentPage() }
};
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
// TODO MAUI Fix this
await Task.Delay(100);
Assert.NotNull(shell.Handler);
});
}
[Fact(DisplayName = "TitleView Causes a Crash When Switching Tabs")]
public async Task TitleViewCausesACrashWhenSwitchingTabs()
{
SetupBuilder();
var page1 = new ContentPage();
var page2 = new ContentPage();
var page3 = new ContentPage();
var titleView1 = new VerticalStackLayout();
var titleView2 = new Label();
var shellTitleView = new Editor();
Shell.SetTitleView(page1, titleView1);
Shell.SetTitleView(page2, titleView2);
var shell = await CreateShellAsync((shell) =>
{
Shell.SetTitleView(shell, shellTitleView);
shell.Items.Add(new TabBar()
{
Items =
{
new ShellContent()
{
Route = "Item1",
Content = page1
},
new ShellContent()
{
Route = "Item2",
Content = page2
},
new ShellContent()
{
Route = "Item3",
Content = page3
},
}
});
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await OnLoadedAsync(page1);
// GotoAsync which switching tabs/flyout items currently
// doesn't resolve after navigated has finished which is why we have the
// delays
// https://github.com/dotnet/maui/issues/6193
Assert.Equal(titleView1.ToPlatform(), GetTitleView(handler));
await shell.GoToAsync("//Item2");
await Task.Delay(200);
await OnLoadedAsync(page2);
Assert.Equal(titleView2.ToPlatform(), GetTitleView(handler));
await shell.GoToAsync("//Item1");
await Task.Delay(200);
await OnLoadedAsync(page1);
Assert.Equal(titleView1.ToPlatform(), GetTitleView(handler));
await shell.GoToAsync("//Item2");
await Task.Delay(200);
await OnLoadedAsync(page2);
Assert.Equal(titleView2.ToPlatform(), GetTitleView(handler));
await shell.GoToAsync("//Item3");
await Task.Delay(200);
await OnLoadedAsync(page3);
Assert.Equal(shellTitleView.ToPlatform(), GetTitleView(handler));
});
}
[Fact(DisplayName = "Handlers not recreated when changing tabs")]
public async Task HandlersNotRecreatedWhenChangingTabs()
{
SetupBuilder();
var page1 = new ContentPage();
var page2 = new ContentPage();
var shell = await CreateShellAsync((shell) =>
{
shell.Items.Add(new TabBar()
{
Items =
{
new ShellContent()
{
Route = "Item1",
Content = page1
},
new ShellContent()
{
Route = "Item2",
Content = page2
},
}
});
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
var initialHandler = page1.Handler;
await shell.GoToAsync("//Item2");
await shell.GoToAsync("//Item1");
Assert.Equal(initialHandler, page1.Handler);
});
}
[Fact(DisplayName = "Navigation Routes Correctly After Switching Flyout Items")]
public async Task NavigatedFiresAfterSwitchingFlyoutItems()
{
SetupBuilder();
var shellContent1 = new ShellContent() { Content = new ContentPage() };
var shellContent2 = new ShellContent() { Content = new ContentPage() };
var shell = await CreateShellAsync((shell) =>
{
shell.Items.Add(shellContent1);
shell.Items.Add(shellContent2);
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
IShellController shellController = shell;
var currentItem = shell.CurrentItem;
// For now on iOS/Android we're just making sure nothing crashes
#if WINDOWS
Assert.NotNull(currentItem.Handler);
#endif
await shellController.OnFlyoutItemSelectedAsync(shellContent2);
await shell.Navigation.PushAsync(new ContentPage());
await shell.GoToAsync("..");
#if WINDOWS
Assert.NotNull(shell.Handler);
Assert.NotNull(shell.CurrentItem.Handler);
Assert.NotNull(shell.CurrentItem.CurrentItem.Handler);
Assert.Null(currentItem.Handler);
Assert.Null(currentItem.CurrentItem.Handler);
#endif
});
}
[Theory]
[ClassData(typeof(ShellBasicNavigationTestCases))]
public async Task BasicShellNavigationStructurePermutations(ShellItem[] shellItems)
{
SetupBuilder();
var shell = await InvokeOnMainThreadAsync<Shell>(() =>
{
var value = new Shell();
foreach (var item in shellItems)
value.Items.Add(item);
return value;
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
// TODO MAUI Fix this
await Task.Delay(100);
await shell.GoToAsync("//page2");
await Task.Delay(100);
});
}
protected Task<Shell> CreateShellAsync(Action<Shell> action) =>
InvokeOnMainThreadAsync(() =>
{
var value = new Shell();
action?.Invoke(value);
return value;
});
}
}
#endif