/
TabbedPageTests.Windows.cs
245 lines (208 loc) · 7.83 KB
/
TabbedPageTests.Windows.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
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using WFrameworkElement = Microsoft.UI.Xaml.FrameworkElement;
using WSolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.DeviceTests.Stubs;
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.TabbedPage)]
public partial class TabbedPageTests : HandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedViewHandler));
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
});
});
}
[Fact(DisplayName = "Toolbar Visible When Pushing To TabbedPage")]
public async Task ToolbarVisibleWhenPushingToTabbedPage()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()) { Title = "App Page" };
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
// When the current active page is a TabbedPage then
// we put to toolbar inside the PaneFooter so it's
// to the right of the tabs
await navPage.PushAsync(CreateBasicTabbedPage());
var navView = GetMauiNavigationView(handler.MauiContext);
var header = (WFrameworkElement)navView.PaneFooter;
Assert.NotNull(header);
Assert.True(header.ActualHeight > 0);
Assert.True(header.ActualWidth > 0);
await navPage.PopAsync();
header = (WFrameworkElement)navView.Header;
Assert.NotNull(header);
Assert.True(header.ActualHeight > 0);
Assert.True(header.ActualWidth > 0);
await navPage.PushAsync(CreateBasicTabbedPage());
header = (WFrameworkElement)navView.PaneFooter;
Assert.NotNull(header);
Assert.True(header.ActualHeight > 0);
Assert.True(header.ActualWidth > 0);
});
}
[Fact(DisplayName = "TabbedPage Disconnects")]
public async Task TabbedViewHandlerDisconnects()
{
SetupBuilder();
var tabbedPage = CreateBasicTabbedPage();
await CreateHandlerAndAddToWindow<TabbedViewHandler>(tabbedPage, (handler) =>
{
// Validate that no exceptions are thrown
((IElementHandler)handler).DisconnectHandler();
return Task.CompletedTask;
});
}
[Fact(DisplayName = "BarBackground Color")]
public async Task BarBackgroundColor()
{
SetupBuilder();
var tabbedPage = CreateBasicTabbedPage();
tabbedPage.BarBackground = SolidColorBrush.Purple;
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(tabbedPage), (handler) =>
{
var navView = GetMauiNavigationView(tabbedPage.Handler.MauiContext);
var platformBrush = (WSolidColorBrush)((Paint)tabbedPage.BarBackground).ToPlatform();
Assert.Equal(platformBrush.Color, ((WSolidColorBrush)navView.TopNavArea.Background).Color);
return Task.CompletedTask;
});
}
[Fact(DisplayName = "Swapping Root Window Content for New Tabbed Page")]
public async Task SwapWindowContentForNewTabbedPage()
{
SetupBuilder();
var window = new Window()
{
Page = CreateBasicTabbedPage()
};
await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async windowHandler =>
{
window.Page.Handler.DisconnectHandler();
// Swap out main page
window.Page = CreateBasicTabbedPage();
// wait for new handler to finish loading
await ((IPlatformViewHandler)window.Page.Handler).PlatformView.OnLoadedAsync();
var navView = GetMauiNavigationView(window.Page.Handler.MauiContext);
// make sure root view is displaying as top tabs
Assert.Equal(UI.Xaml.Controls.NavigationViewPaneDisplayMode.Top, navView.PaneDisplayMode);
});
}
[Fact(DisplayName = "Bar Text Color")]
public async Task BarTextColor()
{
SetupBuilder();
var tabbedPage = CreateBasicTabbedPage();
tabbedPage.BarTextColor = Colors.Red;
await CreateHandlerAndAddToWindow<TabbedViewHandler>(tabbedPage, handler =>
{
var navView = GetMauiNavigationView(handler.MauiContext);
var navItem = GetNavigationViewItems(navView).ToList()[0];
Assert.Equal(Colors.Red, ((WSolidColorBrush)navItem.Foreground).ToColor());
tabbedPage.BarTextColor = Colors.Blue;
Assert.Equal(Colors.Blue, ((WSolidColorBrush)navItem.Foreground).ToColor());
return Task.CompletedTask;
});
}
[Fact(DisplayName = "Tab Title")]
public async Task TabTitle()
{
SetupBuilder();
await CreateHandlerAndAddToWindow<TabbedViewHandler>(CreateBasicTabbedPage(), handler =>
{
var navView = GetMauiNavigationView(handler.MauiContext);
var navItem = GetNavigationViewItems(navView).ToList()[0];
Assert.Equal("Page 1", navItem.Content);
(handler.VirtualView as TabbedPage).Children[0].Title = "New Page Name";
Assert.Equal("New Page Name", navItem.Content);
return Task.CompletedTask;
});
}
[Fact(DisplayName = "Selected/Unselected Color")]
public async Task SelectedAndUnselectedTabColor()
{
SetupBuilder();
var tabbedPage = CreateBasicTabbedPage();
tabbedPage.Children.Add(new ContentPage() { Title = "Page 2" });
tabbedPage.SelectedTabColor = Colors.Red;
tabbedPage.UnselectedTabColor = Colors.Purple;
await CreateHandlerAndAddToWindow<TabbedViewHandler>(tabbedPage, handler =>
{
var navView = GetMauiNavigationView(handler.MauiContext);
var navItem1 = GetNavigationViewItems(navView).ToList()[0];
var navItem2 = GetNavigationViewItems(navView).ToList()[1];
Assert.Equal(Colors.Red, ((WSolidColorBrush)navItem1.Background).ToColor());
Assert.Equal(Colors.Purple, ((WSolidColorBrush)navItem2.Background).ToColor());
tabbedPage.CurrentPage = tabbedPage.Children[1];
Assert.Equal(Colors.Purple, ((WSolidColorBrush)navItem1.Background).ToColor());
Assert.Equal(Colors.Red, ((WSolidColorBrush)navItem2.Background).ToColor());
return Task.CompletedTask;
});
}
[Fact(DisplayName = "Adding and Removing Pages Propagates Correctly")]
public async Task AddingAndRemovingPagesPropagatesCorrectly()
{
SetupBuilder();
await CreateHandlerAndAddToWindow<TabbedViewHandler>(CreateBasicTabbedPage(), async handler =>
{
var navView = GetMauiNavigationView(handler.MauiContext);
var items = GetNavigationViewItems(navView).ToList();
Assert.Single(items);
(handler.VirtualView as TabbedPage).Children.Add(new ContentPage());
// Wait for the navitem to propagate
await Task.Delay(100);
items = GetNavigationViewItems(navView).ToList();
Assert.Equal(2, items.Count);
(handler.VirtualView as TabbedPage).Children.RemoveAt(1);
// Wait for the navitem to propagate
await Task.Delay(100);
items = GetNavigationViewItems(navView).ToList();
Assert.Single(items);
});
}
[Fact(DisplayName = "Selected Item Changed Propagates to CurrentPage")]
public async Task SelectedItemChangedPropagatesToCurrentPage()
{
SetupBuilder();
var tabbedPage = CreateBasicTabbedPage();
tabbedPage.Children.Add(new ContentPage());
await CreateHandlerAndAddToWindow<TabbedViewHandler>(tabbedPage, handler =>
{
var navView = GetMauiNavigationView(handler.MauiContext);
var secondItem = (navView.MenuItemsSource as IEnumerable<NavigationViewItemViewModel>).Skip(1).FirstOrDefault();
navView.SelectedItem = secondItem;
Assert.Equal(tabbedPage.CurrentPage, tabbedPage.Children[1]);
return Task.CompletedTask;
});
}
TabbedPage CreateBasicTabbedPage()
{
return new TabbedPage()
{
Title = "Tabbed Page",
Children =
{
new ContentPage() { Title = "Page 1" }
}
};
}
}
}