This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathShellFlyoutItemTemplateTests.cs
331 lines (264 loc) · 9.66 KB
/
ShellFlyoutItemTemplateTests.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
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class ShellFlyoutItemTemplateTests : ShellTestBase
{
[Test]
public void FlyoutItemDefaultStylesApplied()
{
Shell shell = new Shell();
var shellItem = CreateShellItem();
shell.Items.Add(shellItem);
var element = GetFlyoutItemDataTemplateElement<Element>(shell, shellItem);
var label = element.LogicalChildren.OfType<Label>().First();
Assert.AreEqual(TextAlignment.Center, label.VerticalTextAlignment);
}
[Test]
public void FlyoutItemLabelStyleCustom()
{
var classStyle = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Start }
},
Class = "fooClass",
};
Shell shell = new Shell();
shell.Resources = new ResourceDictionary { classStyle };
var shellItem = CreateShellItem();
shellItem.StyleClass = new[] { "fooClass" };
shell.Items.Add(shellItem);
var element = GetFlyoutItemDataTemplateElement<Element>(shell, shellItem);
var label = element.LogicalChildren.OfType<Label>().First();
Assert.AreEqual(TextAlignment.Start, label.VerticalTextAlignment);
}
[Test]
public void MenuItemLabelStyleCustom()
{
var classStyle = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Start }
},
Class = "fooClass",
};
Shell shell = new Shell();
shell.Resources = new ResourceDictionary { classStyle };
var shellItem = CreateShellItem();
var menuItem = new MenuItem();
var shellMenuItem = new MenuShellItem(menuItem);
menuItem.StyleClass = new[] { "fooClass" };
shell.Items.Add(shellItem);
shell.Items.Add(shellMenuItem);
var label = GetFlyoutItemDataTemplateElement<Label>(shell, shellMenuItem);
Assert.AreEqual(TextAlignment.Start, label.VerticalTextAlignment);
}
[Test]
public void FlyoutItemLabelStyleDefault()
{
var classStyle = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Start }
},
Class = FlyoutItem.LabelStyle,
};
Shell shell = new Shell();
shell.Resources = new ResourceDictionary { classStyle };
var shellItem = CreateShellItem();
shell.Items.Add(shellItem);
var label = GetFlyoutItemDataTemplateElement<Label>(shell, shellItem);
Assert.AreEqual(TextAlignment.Start, label.VerticalTextAlignment);
}
[Test]
public void FlyoutItemDefaultTemplates()
{
Shell shell = new Shell();
IShellController sc = (IShellController)shell;
shell.MenuItemTemplate = new DataTemplate(() => new Label() { Text = "MenuItemTemplate" });
shell.ItemTemplate = new DataTemplate(() => new Label() { Text = "ItemTemplate" });
var shellItem = CreateShellItem();
var menuItem = new MenuShellItem(new MenuItem());
shell.Items.Add(shellItem);
shell.Items.Add(menuItem);
DataTemplate triggerDefault = shell.ItemTemplate;
triggerDefault = shell.MenuItemTemplate;
Assert.AreEqual("ItemTemplate", GetFlyoutItemDataTemplateElement<Label>(shell, shellItem).Text);
Assert.AreEqual("MenuItemTemplate", GetFlyoutItemDataTemplateElement<Label>(shell, menuItem).Text);
Assert.AreEqual("MenuItemTemplate", GetFlyoutItemDataTemplateElement<Label>(shell, menuItem.MenuItem).Text);
}
[Test]
public void FlyoutItemLabelVisualStateManager()
{
var groups = new VisualStateGroupList();
var commonGroup = new VisualStateGroup();
commonGroup.Name = "CommonStates";
groups.Add(commonGroup);
var normalState = new VisualState();
normalState.Name = "Normal";
var selectedState = new VisualState();
selectedState.Name = "Selected";
normalState.Setters.Add(new Setter
{
Property = Label.BackgroundColorProperty,
Value = Color.Red,
TargetName = "FlyoutItemLabel"
});
selectedState.Setters.Add(new Setter
{
Property = Label.BackgroundColorProperty,
Value = Color.Green,
TargetName = "FlyoutItemLabel"
});
commonGroup.States.Add(normalState);
commonGroup.States.Add(selectedState);
var classStyle = new Style(typeof(Grid))
{
Setters = {
new Setter
{
Property = VisualStateManager.VisualStateGroupsProperty,
Value = groups
}
},
Class = FlyoutItem.LayoutStyle,
};
Shell shell = new Shell();
shell.Resources = new ResourceDictionary { classStyle };
var shellItem = CreateShellItem();
shell.Items.Add(shellItem);
var grid = GetFlyoutItemDataTemplateElement<Grid>(shell, shellItem);
var label = grid.LogicalChildren.OfType<Label>().First();
Assert.AreEqual(Color.Red, label.BackgroundColor);
Assert.IsTrue(VisualStateManager.GoToState(grid, "Selected"));
Assert.AreEqual(Color.Green, label.BackgroundColor);
}
[Test]
public void BindingContextFlyoutItems()
{
var flyoutItemVM = new TestShellViewModel() { Text = "Dog" };
Shell shell = new Shell();
shell.BindingContext = flyoutItemVM;
var item1 = CreateShellItem<FlyoutItem>();
item1.SetBinding(FlyoutItem.TitleProperty, "Text", mode: BindingMode.TwoWay);
shell.Items.Add(item1);
MenuItem menuItem = new MenuItem();
menuItem.SetBinding(MenuItem.TextProperty, "Text", mode: BindingMode.TwoWay);
shell.Items.Add(menuItem);
var flyoutItemLabel = GetFlyoutItemDataTemplateElement<Label>(shell, shell.Items[0]);
var menuItemLabel = GetFlyoutItemDataTemplateElement<Label>(shell, shell.Items[1]);
Assert.AreEqual("Dog", flyoutItemLabel.Text);
Assert.AreEqual("Dog", menuItemLabel.Text);
flyoutItemVM.Text = "Cat";
Assert.AreEqual("Cat", flyoutItemLabel.Text);
Assert.AreEqual("Cat", menuItemLabel.Text);
}
[Test]
public void BindingContextSetsCorrectlyWhenUsingAsMultipleItemAndImplicitlyGeneratedShellSections()
{
Shell shell = new Shell();
FlyoutItem item = new FlyoutItem() { FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems };
ShellContent shellContent1 = new ShellContent();
ShellContent shellContent2 = new ShellContent();
item.Items.Add(shellContent1);
item.Items.Add(shellContent2);
shell.Items.Add(item);
var vm = new TestShellViewModel();
vm.SubViewModel = new TestShellViewModel() { Text = "Item1" };
vm.SubViewModel2 = new TestShellViewModel() { Text = "Item2" };
shell.BindingContext = vm;
shellContent1.SetBinding(BindableObject.BindingContextProperty, "SubViewModel");
shellContent2.SetBinding(BindableObject.BindingContextProperty, "SubViewModel2");
shell.ItemTemplate = new DataTemplate(() =>
{
Label label = new Label();
label.SetBinding(Label.TextProperty, "BindingContext.Text");
return label;
});
var flyoutItems = (shell as IShellController).GenerateFlyoutGrouping();
var label1 = GetFlyoutItemDataTemplateElement<Label>(shell, flyoutItems[0][0]);
var label2 = GetFlyoutItemDataTemplateElement<Label>(shell, flyoutItems[0][1]);
Assert.AreEqual(label1.BindingContext, shellContent1);
Assert.AreEqual(label2.BindingContext, shellContent2);
Assert.AreEqual("Item1", label1.Text);
Assert.AreEqual("Item2", label2.Text);
}
T GetFlyoutItemDataTemplateElement<T>(Shell shell, BindableObject bo)
where T : class
{
var content = (shell as IShellController).GetFlyoutItemDataTemplate(bo).CreateContent();
if (content is BindableObject bindableContent)
{
if (bo is MenuItem mi)
bindableContent.BindingContext = mi.Parent;
else
bindableContent.BindingContext = bo;
}
if (content is Element e)
{
e.Parent = shell;
}
else
{
e = null;
}
if (content is T t)
return t;
if (e == null)
return default(T);
return e.LogicalChildren.OfType<T>().First();
}
//[Test]
//public void FlyoutItemLabelStyleCanBeChangedAfterRendered()
//{
// var classStyle = new Style(typeof(Label))
// {
// Setters = {
// new Setter { Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Start }
// },
// Class = "fooClass",
// };
// Shell shell = new Shell();
// shell.Resources = new ResourceDictionary { classStyle };
// var shellItem = CreateShellItem();
// shell.Items.Add(shellItem);
// var flyoutItemTemplate = (shell as IShellController).GetFlyoutItemDataTemplate(shellItem);
// var thing = (Element)flyoutItemTemplate.CreateContent();
// thing.Parent = shell;
// var label = thing.LogicalChildren.OfType<Label>().First();
// Assert.AreEqual(TextAlignment.Center, label.VerticalTextAlignment);
// shellItem.StyleClass = new[] { "fooClass" };
// Assert.AreEqual(TextAlignment.Start, label.VerticalTextAlignment);
//}
//[Test]
//public void MenuItemLabelStyleCanBeChangedAfterRendered()
//{
// var classStyle = new Style(typeof(Label))
// {
// Setters = {
// new Setter { Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Start }
// },
// Class = "fooClass",
// };
// Shell shell = new Shell();
// shell.Resources = new ResourceDictionary { classStyle };
// var shellItem = CreateShellItem();
// var menuItem = new MenuItem();
// var shellMenuItem = new MenuShellItem(menuItem);
// shell.Items.Add(shellItem);
// shell.Items.Add(shellMenuItem);
// var flyoutItemTemplate = (shell as IShellController).GetFlyoutItemDataTemplate(shellMenuItem);
// var thing = (Element)flyoutItemTemplate.CreateContent();
// thing.Parent = shell;
// var label = thing.LogicalChildren.OfType<Label>().First();
// Assert.AreEqual(TextAlignment.Center, label.VerticalTextAlignment);
// menuItem.StyleClass = new[] { "fooClass" };
// Assert.AreEqual(TextAlignment.Start, label.VerticalTextAlignment);
//}
}
}