-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
NavigationPageTests.cs
352 lines (307 loc) · 10.2 KB
/
NavigationPageTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.NavigationPage)]
[Collection(ControlsHandlerTestBase.RunInNewWindowCollection)]
public partial class NavigationPageTests : ControlsHandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
#if IOS || MACCATALYST
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationRenderer));
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedRenderer));
#else
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedViewHandler));
#endif
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Window, WindowHandlerStub>();
handlers.AddHandler<Frame, FrameRenderer>();
handlers.AddHandler<Label, LabelHandler>();
handlers.AddHandler<Button, ButtonHandler>();
handlers.AddHandler<CollectionView, CollectionViewHandler>();
});
});
}
[Fact]
public async Task PoppingNavigationPageDoesntCrash()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()) { Title = "App Page" };
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await navPage.PushAsync(new NavigationPage(new ContentPage() { Content = new Frame(), Title = "Detail" }));
await navPage.PopAsync();
});
}
[Fact]
public async Task InitialPageFiresNavigatedEvent()
{
SetupBuilder();
var page = new ContentPage();
var navPage = new NavigationPage(page) { Title = "App Page" };
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await OnNavigatedToAsync(page);
Assert.True(page.HasNavigatedTo);
});
}
[Fact]
public async Task PushedPageFiresNavigatedEventOnInitialLoad()
{
SetupBuilder();
bool pageFiredNavigated = false;
var page = new ContentPage();
page.NavigatedTo += (_, _) => pageFiredNavigated = true;
var page2 = new ContentPage();
var navPage = new NavigationPage(page) { Title = "App Page" };
await navPage.PushAsync(page2);
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await OnNavigatedToAsync(page2);
Assert.True(page2.HasNavigatedTo);
});
Assert.False(pageFiredNavigated);
}
#if !IOS && !MACCATALYST
[Fact(DisplayName = "Back Button Visibility Changes with push/pop")]
public async Task BackButtonVisibilityChangesWithPushPop()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage());
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
Assert.False(IsBackButtonVisible(handler));
await navPage.PushAsync(new ContentPage());
Assert.True(IsBackButtonVisible(handler));
await navPage.PopAsync();
Assert.False(IsBackButtonVisible(handler));
});
}
[Fact(DisplayName = "Set Has Back Button")]
public async Task SetHasBackButton()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage());
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await navPage.PushAsync(new ContentPage());
NavigationPage.SetHasBackButton(navPage.CurrentPage, false);
Assert.False(IsBackButtonVisible(handler));
NavigationPage.SetHasBackButton(navPage.CurrentPage, true);
Assert.True(IsBackButtonVisible(handler));
});
}
#endif
[Fact(DisplayName = "Set Has Navigation Bar")]
public async Task SetHasNavigationBar()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage() { Title = "Nav Bar" });
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
Assert.True(await AssertionExtensions.Wait(() => IsNavigationBarVisible(handler)));
NavigationPage.SetHasNavigationBar(navPage.CurrentPage, false);
Assert.True(await AssertionExtensions.Wait(() => !IsNavigationBarVisible(handler)));
NavigationPage.SetHasNavigationBar(navPage.CurrentPage, true);
Assert.True(await AssertionExtensions.Wait(() => IsNavigationBarVisible(handler)));
});
}
[Fact(DisplayName = "NavigationBar Removes When MainPage Set To ContentPage")]
public async Task NavigationBarRemovesWhenMainPageSetToContentPage()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage());
var window = new Window(navPage);
await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async (handler) =>
{
var contentPage = new ContentPage();
window.Page = contentPage;
await OnLoadedAsync(contentPage);
Assert.True(await AssertionExtensions.Wait(() => !IsNavigationBarVisible(handler)));
});
}
#if !IOS && !MACCATALYST
[Fact(DisplayName = "Toolbar Items Map Correctly")]
public async Task ToolbarItemsMapCorrectly()
{
SetupBuilder();
var toolbarItem = new ToolbarItem() { Text = "Toolbar Item 1" };
var navPage = new NavigationPage(new ContentPage()
{
ToolbarItems =
{
toolbarItem
}
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
{
ToolbarItemsMatch(handler, toolbarItem);
return Task.CompletedTask;
});
}
#endif
[Fact(DisplayName = "Toolbar Title")]
public async Task ToolbarTitle()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
{
string title = GetToolbarTitle(handler);
Assert.Equal("Page Title", title);
return Task.CompletedTask;
});
}
[Fact(DisplayName = "Insert Page Before Root Page and then PopToRoot")]
public async Task InsertPageBeforeRootPageAndThenPopToRoot()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
navPage.Navigation.InsertPageBefore(new ContentPage(), navPage.RootPage);
await navPage.PopToRootAsync(false);
});
// Just verifying that nothing crashes
}
#if !IOS && !MACCATALYST
[Fact(DisplayName = "Insert Page Before RootPage ShowsBackButton")]
public async Task InsertPageBeforeRootPageShowsBackButton()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
navPage.Navigation.InsertPageBefore(new ContentPage(), navPage.RootPage);
// InsertPageBefore is actually async in the background so we have to insert a pause
// here to allow android to settle before testing
await Task.Delay(100);
Assert.True(IsBackButtonVisible(navPage.Handler));
});
}
#endif
[Fact(DisplayName = "Remove Root Page Hides Back Button")]
public async Task RemoveRootPageHidesBackButton()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await navPage.Navigation.PushAsync(new ContentPage());
navPage.Navigation.RemovePage(navPage.RootPage);
// RemovePage is actually async in the background so we have to insert a pause
// here to allow android to settle before testing
await Task.Delay(100);
Assert.False(IsBackButtonVisible(navPage.Handler));
});
}
[Fact(DisplayName = "Pushing a Tabbed Page Doesn't Throw Exception")]
public async Task PushingATabbedPageDoesntThrowException()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
var tabbedPage1 = CreateTabbedPage("1");
var tabbedPage2 = CreateTabbedPage("2");
await navPage.PushAsync(tabbedPage1);
tabbedPage1.SelectedItem = tabbedPage1.Children[1];
await OnLoadedAsync(tabbedPage1.Children[1]);
await navPage.PopAsync();
await navPage.PushAsync(tabbedPage2);
TabbedPage CreateTabbedPage(string title) => new TabbedPage()
{
Title = title,
Children =
{
new ContentPage(),
new ContentPage()
}
};
});
}
[Fact(DisplayName = "NavigationPage Does Not Leak")]
public async Task DoesNotLeak()
{
SetupBuilder();
WeakReference pageReference = null;
var navPage = new NavigationPage(new ContentPage { Title = "Page 1" });
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
var page = new ContentPage
{
Title = "Page 2",
Content = new VerticalStackLayout
{
new Label(),
new Button(),
new CollectionView(),
}
};
pageReference = new WeakReference(page);
await navPage.Navigation.PushAsync(page);
await navPage.Navigation.PopAsync();
});
// As we add more controls to this test, more GCs will be required
for (int i = 0; i < 16; i++)
{
if (!pageReference.IsAlive)
break;
await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
}
Assert.NotNull(pageReference);
Assert.False(pageReference.IsAlive, "Page should not be alive!");
}
[Fact(DisplayName = "Can Reuse Pages")]
public async Task CanReusePages()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage { Title = "Page 1" });
var reusedPage = new ContentPage
{
Content = new Label()
};
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
await navPage.Navigation.PushAsync(reusedPage);
await navPage.Navigation.PopAsync();
await navPage.Navigation.PushAsync(reusedPage);
await OnLoadedAsync(reusedPage.Content);
});
}
}
}