-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SwitchUnitTests.cs
156 lines (126 loc) · 4.5 KB
/
SwitchUnitTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class SwitchUnitTests : BaseTestFixture
{
const string CommonStatesName = "CommonStates";
const string DisabledStateName = "Disabled";
const string FocusedStateName = "Focused";
const string NormalStateName = "Normal";
const string OnStateName = "On";
const string OffStateName = "Off";
static VisualStateGroupList CreateTestStateGroups()
{
var stateGroups = new VisualStateGroupList();
var visualStateGroup = new VisualStateGroup { Name = CommonStatesName };
var disabledState = new VisualState { Name = DisabledStateName };
var focusedState = new VisualState { Name = FocusedStateName };
var normalState = new VisualState { Name = NormalStateName };
var onState = new VisualState { Name = OnStateName };
var offState = new VisualState { Name = OffStateName };
visualStateGroup.States.Add(disabledState);
visualStateGroup.States.Add(focusedState);
visualStateGroup.States.Add(normalState);
visualStateGroup.States.Add(onState);
visualStateGroup.States.Add(offState);
stateGroups.Add(visualStateGroup);
return stateGroups;
}
static VisualStateGroupList CreateTestStateGroupsWithoutOnOffStates()
{
var stateGroups = new VisualStateGroupList();
var visualStateGroup = new VisualStateGroup { Name = CommonStatesName };
var disabledState = new VisualState { Name = DisabledStateName };
var focusedState = new VisualState { Name = FocusedStateName };
var normalState = new VisualState { Name = NormalStateName };
visualStateGroup.States.Add(disabledState);
visualStateGroup.States.Add(focusedState);
visualStateGroup.States.Add(normalState);
stateGroups.Add(visualStateGroup);
return stateGroups;
}
static VisualStateGroupList CreateTestStateGroupsWithoutNormalState()
{
var stateGroups = new VisualStateGroupList();
var visualStateGroup = new VisualStateGroup { Name = CommonStatesName };
var disabledState = new VisualState { Name = DisabledStateName };
visualStateGroup.States.Add(disabledState);
stateGroups.Add(visualStateGroup);
return stateGroups;
}
[Test]
public void TestConstructor ()
{
Switch sw = new Switch ();
Assert.IsFalse (sw.IsToggled);
}
[Test]
public void TestOnEvent ()
{
Switch sw = new Switch ();
bool fired = false;
sw.Toggled += (sender, e) => fired = true;
sw.IsToggled = true;
Assert.IsTrue (fired);
}
[Test]
public void TestOnEventNotDoubleFired ()
{
var sw = new Switch ();
bool fired = false;
sw.IsToggled = true;
sw.Toggled += (sender, args) => fired = true;
sw.IsToggled = true;
Assert.IsFalse (fired);
}
[Test]
public void VisualStateIsDisabledIfSwitchIsDisabled()
{
var switch1 = new Switch();
switch1.IsEnabled = false;
VisualStateManager.SetVisualStateGroups(switch1, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(switch1);
Assert.That(groups1[0].CurrentState.Name, Is.EqualTo(DisabledStateName));
}
[Test]
public void VisualStateIsOnIfAvailableAndSwitchIsEnabledAndOn()
{
var switch1 = new Switch();
switch1.IsEnabled = true;
switch1.IsToggled = true;
VisualStateManager.SetVisualStateGroups(switch1, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(switch1);
Assert.That(groups1[0].CurrentState.Name, Is.EqualTo(OnStateName));
}
[Test]
public void VisualStateIsOffIfAvailableAndSwitchIsEnabledAndOff()
{
var switch1 = new Switch();
switch1.IsEnabled = true;
switch1.IsToggled = false;
VisualStateManager.SetVisualStateGroups(switch1, CreateTestStateGroups());
var groups1 = VisualStateManager.GetVisualStateGroups(switch1);
Assert.That(groups1[0].CurrentState.Name, Is.EqualTo(OffStateName));
}
[Test]
public void InitialStateIsNormalIfAvailableButOnOffNotAvailable()
{
var switch1 = new Switch();
VisualStateManager.SetVisualStateGroups(switch1, CreateTestStateGroupsWithoutOnOffStates());
var groups1 = VisualStateManager.GetVisualStateGroups(switch1);
Assert.That(groups1[0].CurrentState.Name, Is.EqualTo(NormalStateName));
}
[Test]
public void InitialStateIsNullIfNormalOnOffNotAvailable()
{
var switch1 = new Switch();
VisualStateManager.SetVisualStateGroups(switch1, CreateTestStateGroupsWithoutNormalState());
var groups1 = VisualStateManager.GetVisualStateGroups(switch1);
Assert.Null(groups1[0].CurrentState);
}
}
}