-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathVisualStateManagerTests.cs
More file actions
542 lines (429 loc) · 15.5 KB
/
VisualStateManagerTests.cs
File metadata and controls
542 lines (429 loc) · 15.5 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using Xunit;
using static Microsoft.Maui.Controls.Core.UnitTests.VisualStateTestHelpers;
namespace Microsoft.Maui.Controls.Core.UnitTests
{
public class VisualStateManagerTests : IDisposable
{
[Fact]
public void InitialStateIsNormalIfAvailable()
{
var label1 = new Label();
VisualStateManager.SetVisualStateGroups(label1, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(NormalStateName, groups1[0].CurrentState.Name);
}
[Fact]
public void InitialStateIsNullIfNormalNotAvailable()
{
var label1 = new Label();
VisualStateManager.SetVisualStateGroups(label1, CreateStateGroupsWithoutNormalState());
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Null(groups1[0].CurrentState);
}
[Fact]
public void VisualElementsStateGroupsAreDistinct()
{
var label1 = new Label();
var label2 = new Label();
VisualStateManager.SetVisualStateGroups(label1, CreateTestStateGroups());
VisualStateManager.SetVisualStateGroups(label2, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
var groups2 = VisualStateManager.GetVisualStateGroups(label2);
Assert.NotSame(groups1, groups2);
Assert.Equal(NormalStateName, groups1[0].CurrentState.Name);
Assert.Equal(NormalStateName, groups2[0].CurrentState.Name);
VisualStateManager.GoToState(label1, InvalidStateName);
Assert.Equal(InvalidStateName, groups1[0].CurrentState.Name);
Assert.Equal(NormalStateName, groups2[0].CurrentState.Name);
}
[Fact]
public void VisualStateGroupsFromSettersAreDistinct()
{
var x = new Setter();
x.Property = VisualStateManager.VisualStateGroupsProperty;
x.Value = CreateTestStateGroups();
var label1 = new Label();
var label2 = new Label();
x.Apply(label1, new SetterSpecificity());
x.Apply(label2, new SetterSpecificity());
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
var groups2 = VisualStateManager.GetVisualStateGroups(label2);
Assert.NotNull(groups1);
Assert.NotNull(groups2);
Assert.NotSame(groups1, groups2);
Assert.Equal(NormalStateName, groups1[0].CurrentState.Name);
Assert.Equal(NormalStateName, groups2[0].CurrentState.Name);
VisualStateManager.GoToState(label1, InvalidStateName);
Assert.Equal(InvalidStateName, groups1[0].CurrentState.Name);
Assert.Equal(NormalStateName, groups2[0].CurrentState.Name);
}
[Fact]
public void ElementsDoNotHaveVisualStateGroupsCollectionByDefault()
{
var label1 = new Label();
Assert.False(label1.HasVisualStateGroups());
var vsg = VisualStateManager.GetVisualStateGroups(label1);
Assert.False(label1.HasVisualStateGroups());
vsg.Add(new VisualStateGroup());
Assert.True(label1.HasVisualStateGroups());
}
[Fact]
public void StateNamesMustBeUniqueWithinGroup()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
var duplicate = new VisualState { Name = NormalStateName };
Assert.Throws<InvalidOperationException>(() => vsgs[0].States.Add(duplicate));
}
[Fact]
public void StateNamesMustBeUniqueWithinGroupList()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
// Create and add a second VisualStateGroup
var secondGroup = new VisualStateGroup { Name = "Foo" };
vsgs.Add(secondGroup);
// Create a VisualState with the same name as one in another group in this list
var duplicate = new VisualState { Name = NormalStateName };
Assert.Throws<InvalidOperationException>(() => secondGroup.States.Add(duplicate));
}
[Fact]
public void StateNamesMustBeUniqueWithinGroupListWhenAddingGroup()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
// Create and add a second VisualStateGroup
var secondGroup = new VisualStateGroup { Name = "Foo" };
// Create a VisualState with the same name as one in another group in the list
var duplicate = new VisualState { Name = NormalStateName };
secondGroup.States.Add(duplicate);
Assert.Throws<InvalidOperationException>(() => vsgs.Add(secondGroup));
}
[Fact]
public void GroupWithDuplicateNameReplacesExisting()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
var secondGroup = new VisualStateGroup { Name = CommonStatesGroupName };
secondGroup.States.Add(new VisualState { Name = NormalStateName });
// Adding a group with the same name should replace the existing one, not throw
vsgs.Add(secondGroup);
Assert.Single(vsgs);
Assert.Same(secondGroup, vsgs[0]);
}
[Fact]
public void StateNamesInGroupMayNotBeNull()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
var nullStateName = new VisualState();
Assert.Throws<InvalidOperationException>(() => vsgs[0].States.Add(nullStateName));
}
[Fact]
public void StateNamesInGroupMayNotBeEmpty()
{
IList<VisualStateGroup> vsgs = CreateTestStateGroups();
var emptyStateName = new VisualState { Name = "" };
Assert.Throws<InvalidOperationException>(() => vsgs[0].States.Add(emptyStateName));
}
[Fact]
public void VerifyVisualStateChanges()
{
var label1 = new Label();
VisualStateManager.SetVisualStateGroups(label1, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(NormalStateName, groups1[0].CurrentState.Name);
label1.IsEnabled = false;
groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(DisabledStateName, groups1[0].CurrentState.Name);
label1.SetValue(VisualElement.IsFocusedPropertyKey, true);
groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(DisabledStateName, groups1[0].CurrentState.Name);
label1.IsEnabled = true;
groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(FocusedStateName, groups1[0].CurrentState.Name);
label1.SetValue(VisualElement.IsFocusedPropertyKey, false);
groups1 = VisualStateManager.GetVisualStateGroups(label1);
Assert.Equal(NormalStateName, groups1[0].CurrentState.Name);
}
[Fact]
public void VisualElementGoesToCorrectStateWhenAvailable()
{
var label = new Label();
double targetBottomMargin = 1.5;
var group = new VisualStateGroup();
var list = new VisualStateGroupList();
var normalState = new VisualState { Name = NormalStateName };
normalState.Setters.Add(new Setter { Property = View.MarginBottomProperty, Value = targetBottomMargin });
list.Add(group);
group.States.Add(normalState);
VisualStateManager.SetVisualStateGroups(label, list);
Assert.Equal(label.Margin.Bottom, targetBottomMargin);
}
[Fact]
public void VisualElementGoesToCorrectStateWhenAvailableFromSetter()
{
double targetBottomMargin = 1.5;
var group = new VisualStateGroup();
var list = new VisualStateGroupList();
var normalState = new VisualState { Name = NormalStateName };
normalState.Setters.Add(new Setter { Property = View.MarginBottomProperty, Value = targetBottomMargin });
var x = new Setter
{
Property = VisualStateManager.VisualStateGroupsProperty,
Value = list
};
list.Add(group);
group.States.Add(normalState);
var label1 = new Label();
var label2 = new Label();
x.Apply(label1, new SetterSpecificity());
x.Apply(label2, new SetterSpecificity());
Assert.Equal(label1.Margin.Bottom, targetBottomMargin);
Assert.Equal(label2.Margin.Bottom, targetBottomMargin);
}
[Fact]
public void VisualElementGoesToCorrectStateWhenSetterHasTarget()
{
double defaultMargin = default(double);
double targetMargin = 1.5;
var label1 = new Label();
var label2 = new Label();
INameScope nameScope = new NameScope();
NameScope.SetNameScope(label1, nameScope);
nameScope.RegisterName("Label1", label1);
NameScope.SetNameScope(label2, nameScope);
nameScope.RegisterName("Label2", label2);
var list = new VisualStateGroupList
{
new VisualStateGroup
{
States =
{
new VisualState
{
Name = NormalStateName,
Setters =
{
new Setter { Property = View.MarginBottomProperty, Value = targetMargin },
new Setter { TargetName = "Label2", Property = View.MarginTopProperty, Value = targetMargin }
}
}
}
}
};
VisualStateManager.SetVisualStateGroups(label1, list);
Assert.Equal(label1.Margin.Top, defaultMargin);
Assert.Equal(label1.Margin.Bottom, targetMargin);
Assert.Equal(label1.Margin.Left, defaultMargin);
Assert.Equal(label2.Margin.Top, targetMargin);
Assert.Equal(label2.Margin.Bottom, defaultMargin);
}
[Fact]
public void CanRemoveAStateAndAddANewStateWithTheSameName()
{
var stateGroups = new VisualStateGroupList();
var visualStateGroup = new VisualStateGroup { Name = CommonStatesGroupName };
var normalState = new VisualState { Name = NormalStateName };
var invalidState = new VisualState { Name = InvalidStateName };
stateGroups.Add(visualStateGroup);
visualStateGroup.States.Add(normalState);
visualStateGroup.States.Add(invalidState);
var name = visualStateGroup.States[0].Name;
visualStateGroup.States.Remove(visualStateGroup.States[0]);
visualStateGroup.States.Add(new VisualState { Name = name });
}
[Fact]
public void CanRemoveAGroupAndAddANewGroupWithTheSameName()
{
var stateGroups = new VisualStateGroupList();
var visualStateGroup = new VisualStateGroup { Name = CommonStatesGroupName };
var secondVisualStateGroup = new VisualStateGroup { Name = "Whatevs" };
var normalState = new VisualState { Name = NormalStateName };
var invalidState = new VisualState { Name = InvalidStateName };
stateGroups.Add(visualStateGroup);
visualStateGroup.States.Add(normalState);
visualStateGroup.States.Add(invalidState);
stateGroups.Add(secondVisualStateGroup);
var name = stateGroups[0].Name;
stateGroups.Remove(stateGroups[0]);
stateGroups.Add(new VisualStateGroup { Name = name });
}
public VisualStateManagerTests()
{
AppInfo.SetCurrent(new MockAppInfo() { RequestedTheme = AppTheme.Light });
Application.Current = new Application();
}
public void Dispose()
{
Application.Current = null;
}
[Fact]
//https://github.com/dotnet/maui/issues/6251
public void AppThemeBindingInVSM()
{
var label = new Label() { BackgroundColor = Colors.Red };
var list = new VisualStateGroupList
{
new VisualStateGroup
{
States =
{
new VisualState { Name = NormalStateName},
new VisualState
{
Name = DisabledStateName,
Setters =
{
new Setter { Property = View.BackgroundColorProperty, Value = new AppThemeBinding{ Light=Colors.Purple, Dark=Colors.Purple, Default=Colors.Purple } },
}
}
}
}
};
VisualStateManager.SetVisualStateGroups(label, list);
Assert.Equal(label.BackgroundColor, Colors.Red);
VisualStateManager.GoToState(label, DisabledStateName);
Assert.Equal(label.BackgroundColor, Colors.Purple);
VisualStateManager.GoToState(label, NormalStateName);
Assert.Equal(label.BackgroundColor, Colors.Red);
}
[Fact]
//https://github.com/dotnet/maui/issues/4139
public void ChangingStyleContainingVSMShouldResetStateValue()
{
var label = new Label();
var SelectedStateName = "Selected";
var style0 = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.TextColorProperty, Value = Colors.Black},
new Setter {
Property = VisualStateManager.VisualStateGroupsProperty,
Value = new VisualStateGroupList {
new VisualStateGroup {
States = {
new VisualState { Name = NormalStateName },
new VisualState {
Name = SelectedStateName,
Setters = { new Setter { Property = Label.TextColorProperty, Value=Colors.Red } }
}
}
}
}
},
}
};
var style1 = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.TextColorProperty, Value = Colors.Black},
new Setter {
Property = VisualStateManager.VisualStateGroupsProperty,
Value = new VisualStateGroupList {
new VisualStateGroup {
States = {
new VisualState { Name = NormalStateName },
new VisualState {
Name = SelectedStateName,
Setters = { new Setter { Property = Label.TextColorProperty, Value=Colors.Cyan } }
}
}
}
}
},
}
};
label.Style = style0;
Assert.Equal(label.TextColor, Colors.Black);
VisualStateManager.GoToState(label, SelectedStateName);
Assert.Equal(label.TextColor, Colors.Red);
label.Style = style1;
Assert.Equal(label.TextColor, Colors.Black);
}
[Fact]
//https://github.com/dotnet/maui/issues/6857
public void VSMFromStyleAreUnApplied()
{
var label = new Label
{
Style = new Style(typeof(Label))
{
Setters = {
new Setter {
Property = VisualStateManager.VisualStateGroupsProperty,
Value = new VisualStateGroupList {
new VisualStateGroup {
States = {
new VisualState { Name = NormalStateName },
}
}
}
},
}
}
};
Assert.NotNull(VisualStateManager.GetVisualStateGroups(label));
Assert.Single(VisualStateManager.GetVisualStateGroups(label)); //the list applied by style has one group
label.ClearValue(Label.StyleProperty); //clear the style
Assert.Empty(VisualStateManager.GetVisualStateGroups(label)); //default style (created by defaultValueCreator) has no groups
}
[Fact]
//https://github.com/dotnet/maui/issues/6885
public void UnapplyingVSMShouldUnapplySetters()
{
var label = new Label();
var SelectedStateName = "Selected";
VisualStateManager.SetVisualStateGroups(label, new VisualStateGroupList {
new VisualStateGroup {
States = {
new VisualState {
Name = SelectedStateName,
Setters = { new Setter { Property=Label.TextColorProperty, Value=Colors.HotPink} }
}
}
}
});
VisualStateManager.GoToState(label, SelectedStateName);
Assert.Equal(label.TextColor, Colors.HotPink);
label.ClearValue(VisualStateManager.VisualStateGroupsProperty);
Assert.Empty(VisualStateManager.GetVisualStateGroups(label)); //default style (created by defaultValueCreator) has no groups
Assert.False(label.TextColor == Colors.HotPink); //setter should be unapplied
}
[Theory(Skip = "This test was created to check performance characteristics; leaving it in because it may be useful again.")]
[InlineData(1, 10)]
[InlineData(1, 10000)]
[InlineData(10, 100)]
[InlineData(10, 10000)]
public void ValidatePerformance(int groups, int states)
{
IList<VisualStateGroup> vsgs = new VisualStateGroupList();
var groupList = new List<VisualStateGroup>();
for (int n = 0; n < groups; n++)
{
groupList.Add(new VisualStateGroup { Name = n.ToString() });
}
var watch = new Stopwatch();
watch.Start();
foreach (var group in groupList)
{
vsgs.Add(group);
}
watch.Stop();
double iterations = states;
var random = new Random();
for (int n = 0; n < iterations; n++)
{
var state = new VisualState { Name = n.ToString() };
var group = groupList[random.Next(0, groups - 1)];
watch.Start();
group.States.Add(state);
watch.Stop();
}
var average = watch.ElapsedMilliseconds / iterations;
Debug.WriteLine($">>>>> VisualStateManagerTests ValidatePerformance: {watch.ElapsedMilliseconds}ms over {iterations} iterations; average of {average}ms");
}
}
}