-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
VisualStateManagerTests.xaml.cs
204 lines (152 loc) · 5.76 KB
/
VisualStateManagerTests.xaml.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
using System.Linq;
using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class VisualStateManagerTests : ContentPage
{
public VisualStateManagerTests()
{
InitializeComponent();
}
public VisualStateManagerTests(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture]
public class Tests
{
[SetUp]
public void SetUp()
{
Device.PlatformServices = new MockPlatformServices ();
Application.Current = new MockApplication ();
}
[TearDown]
public void TearDown()
{
Device.PlatformServices = null;
Application.Current = null;
}
[TestCase(false)]
[TestCase(true)]
public void VisualStatesFromStyleXaml(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var entry0 = layout.Entry0;
// Verify that Entry0 has no VisualStateGroups
Assert.False(entry0.HasVisualStateGroups());
Assert.That(Color.Default, Is.EqualTo(entry0.TextColor));
Assert.That(Color.Default, Is.EqualTo(entry0.PlaceholderColor));
var entry1 = layout.Entry1;
// Verify that the correct groups are set up for Entry1
var groups = VisualStateManager.GetVisualStateGroups(entry1);
Assert.AreEqual(3, groups.Count);
Assert.That(groups[0].Name, Is.EqualTo("CommonStates"));
Assert.Contains("Normal", groups[0].States.Select(state => state.Name).ToList());
Assert.Contains("Disabled", groups[0].States.Select(state => state.Name).ToList());
Assert.AreEqual(Color.Default, entry1.TextColor);
Assert.AreEqual(Color.Default, entry1.PlaceholderColor);
// Change the state of Entry1
Assert.True(VisualStateManager.GoToState(entry1, "Disabled"));
// And verify that the changes took
Assert.AreEqual(Color.Gray, entry1.TextColor);
Assert.AreEqual(Color.LightGray, entry1.PlaceholderColor);
// Verify that Entry0 was unaffected
Assert.AreEqual(Color.Default, entry0.TextColor);
Assert.AreEqual(Color.Default, entry0.PlaceholderColor);
}
[TestCase(false)]
[TestCase(true)]
public void UnapplyVisualState(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var entry1 = layout.Entry1;
Assert.AreEqual(Color.Default, entry1.TextColor);
Assert.AreEqual(Color.Default, entry1.PlaceholderColor);
// Change the state of Entry1
var groups = VisualStateManager.GetVisualStateGroups(entry1);
Assert.True(VisualStateManager.GoToState(entry1, "Disabled"));
// And verify that the changes took
Assert.AreEqual(Color.Gray, entry1.TextColor);
Assert.AreEqual(Color.LightGray, entry1.PlaceholderColor);
// Now change it to Normal
Assert.True(VisualStateManager.GoToState(entry1, "Normal"));
// And verify that the changes reverted
Assert.AreEqual(Color.Default, entry1.TextColor);
Assert.AreEqual(Color.Default, entry1.PlaceholderColor);
}
[TestCase(false)]
[TestCase(true)]
public void VisualStateGroupsDirectlyOnElement(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var entry = layout.Entry2;
var groups = VisualStateManager.GetVisualStateGroups(entry);
Assert.NotNull(groups);
Assert.That(groups.Count, Is.EqualTo(2));
}
[TestCase(false)]
[TestCase(true)]
public void EmptyGroupDirectlyOnElement(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var entry3 = layout.Entry3;
var groups = VisualStateManager.GetVisualStateGroups(entry3);
Assert.NotNull(groups);
Assert.True(groups.Count == 1);
}
[TestCase(false)]
[TestCase(true)]
public void VisualStateGroupsFromStylesAreDistinct(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var label1 = layout.ErrorLabel1;
var label2 = layout.ErrorLabel2;
var groups1 = VisualStateManager.GetVisualStateGroups(label1);
var groups2 = VisualStateManager.GetVisualStateGroups(label2);
Assert.AreNotSame(groups1, groups2);
var currentState1 = groups1[0].CurrentState;
var currentState2 = groups2[0].CurrentState;
Assert.That(currentState1.Name, Is.EqualTo("Normal"));
Assert.That(currentState2.Name, Is.EqualTo("Normal"));
VisualStateManager.GoToState(label1, "Invalid");
Assert.That(groups1[0].CurrentState.Name, Is.EqualTo("Invalid"));
Assert.That(groups2[0].CurrentState.Name, Is.EqualTo("Normal"));
}
[TestCase(false)]
[TestCase(true)]
public void SettersAreAddedToCorrectState(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var entry = layout.Entry4;
var groups = VisualStateManager.GetVisualStateGroups(entry);
Assert.That(groups.Count, Is.EqualTo(1));
var common = groups[0];
var normal = common.States.Single(state => state.Name == "Normal");
var disabled = common.States.Single(state => state.Name == "Disabled");
Assert.That(normal.Setters.Count, Is.EqualTo(0));
Assert.That(disabled.Setters.Count, Is.EqualTo(2));
}
[TestCase(false)]
[TestCase(true)]
public void VisualElementGoesToCorrectStateWhenAvailable(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var button = layout.Button1;
Assert.That(button.BackgroundColor, Is.EqualTo(Color.Lime));
}
[TestCase(false)]
[TestCase(true)]
public void TargetedVisualElementGoesToCorrectState(bool useCompiledXaml)
{
var layout = new VisualStateManagerTests(useCompiledXaml);
var label1 = layout.TargetLabel1;
VisualStateManager.GoToState(layout, "Red");
Assert.That(label1.Text, Is.EqualTo("Red"));
VisualStateManager.GoToState(layout, "Blue");
Assert.That(label1.Text, Is.EqualTo("Blue"));
}
}
}
}