-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
TimePickerUnitTests.cs
251 lines (230 loc) · 9.47 KB
/
TimePickerUnitTests.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Xunit;
namespace MaterialDesignThemes.Wpf.Tests
{
public class TimePickerUnitTests
{
private readonly TimePicker _timePicker;
public TimePickerUnitTests()
{
_timePicker = new TimePicker();
_timePicker.ApplyDefaultStyle();
}
[StaTheory]
[MemberData(nameof(GetDisplaysExpectedTextData))]
public void DisplaysExpectedText(CultureInfo culture, DatePickerFormat format, bool is24Hour, bool withSeconds,
DateTime? selectedTime, string expectedText)
{
_timePicker.Language = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
_timePicker.SelectedTimeFormat = format;
_timePicker.Is24Hours = is24Hour;
_timePicker.WithSeconds = withSeconds;
_timePicker.SelectedTime = selectedTime;
string currentTestString = $"{culture.ThreeLetterISOLanguageName} {(is24Hour ? "24 Hour" : "12 Hour")} {format} format {(withSeconds ? "with seconds" : "")}";
Assert.True(expectedText == _timePicker.Text, $"Expected '{expectedText}' but was '{_timePicker.Text}' - {currentTestString}");
}
[StaTheory]
[MemberData(nameof(GetParseLocalizedTimeStringData))]
public void CanParseLocalizedTimeString(CultureInfo culture, DatePickerFormat format, bool is24Hour, bool withSeconds,
string timeString, DateTime? expectedTime)
{
_timePicker.Language = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
_timePicker.SelectedTimeFormat = format;
_timePicker.Is24Hours = is24Hour;
_timePicker.WithSeconds = withSeconds;
var textBox = _timePicker.FindVisualChild<TextBox>(TimePicker.TextBoxPartName);
textBox.Text = timeString;
textBox.RaiseEvent(new RoutedEventArgs(UIElement.LostFocusEvent));
string currentTestString = $"{culture.ThreeLetterISOLanguageName} {(is24Hour ? "24 Hour" : "12 Hour")} {format} format {(withSeconds ? "with seconds" : "")}";
Assert.True(expectedTime == _timePicker.SelectedTime, $"Expected '{expectedTime}' but was '{_timePicker.SelectedTime}' - {currentTestString}");
}
public static IEnumerable<object[]> GetParseLocalizedTimeStringData()
{
//for now just using the same set of data to make sure we can go both directions.
foreach (object[] data in GetDisplaysExpectedTextData())
{
var culture = (CultureInfo) data[0];
bool is24Hour = (bool) data[2];
var withSeconds = (bool) data[3];
var date = (DateTime) data[4];
var timeString = (string) data[5];
//Convert the date to Today
date = DateTime.Today.AddHours(date.Hour).AddMinutes(date.Minute).AddSeconds(withSeconds ? date.Second : 0);
if (!is24Hour && date.Hour > 12 &&
(string.IsNullOrEmpty(culture.DateTimeFormat.AMDesignator) ||
string.IsNullOrEmpty(culture.DateTimeFormat.PMDesignator)))
{
//Because there is no AM/PM designator, 12 hour times will be treated as AM
date = date.AddHours(-12);
}
//Invert the order of the parameters.
data[5] = date;
data[4] = timeString;
yield return data;
}
}
public static IEnumerable<object[]> GetDisplaysExpectedTextData()
{
//AM intentionally picks values with only a single digit to verify the DatePickerFormat is applied
var am = new DateTime(2000, 1, 1, 3, 5, 9);
//PM intentionally picks two digit values greater than 12 to ensure the 24 hour format is applied
var pm = new DateTime(2000, 1, 1, 16, 30, 25);
//Invariant culture
foreach (var data in GetDisplaysExpectedTextDataForCulture(CultureInfo.InvariantCulture, am,
"3:05 AM", "3:05:09 AM", //12 hour short
"03:05 AM", "03:05:09 AM", //12 hour long
"3:05", "3:05:09", //24 hour short
"03:05", "03:05:09")) //24 hour long
{
yield return data;
}
foreach (var data in GetDisplaysExpectedTextDataForCulture(CultureInfo.InvariantCulture, pm,
"4:30 PM", "4:30:25 PM", //12 hour short
"04:30 PM", "04:30:25 PM", //12 hour long
"16:30", "16:30:25", //24 hour short
"16:30", "16:30:25")) //24 hour long
{
yield return data;
}
//US English
var usEnglish = CultureInfo.GetCultureInfo("en-US");
foreach (var data in GetDisplaysExpectedTextDataForCulture(usEnglish, am,
"3:05 AM", "3:05:09 AM", //12 hour short
"03:05 AM", "03:05:09 AM", //12 hour long
"3:05", "3:05:09", //24 hour short
"03:05", "03:05:09")) //24 hour long
{
yield return data;
}
foreach (var data in GetDisplaysExpectedTextDataForCulture(usEnglish, pm,
"4:30 PM", "4:30:25 PM", //12 hour short
"04:30 PM", "04:30:25 PM", //12 hour long
"16:30", "16:30:25", //24 hour short
"16:30", "16:30:25")) //24 hour long
{
yield return data;
}
//Spain Spanish
var spainSpanish = CultureInfo.GetCultureInfo("es-ES");
foreach (var data in GetDisplaysExpectedTextDataForCulture(spainSpanish, am,
"3:05", "3:05:09", //12 hour short
"03:05", "03:05:09", //12 hour long
"3:05", "3:05:09", //24 hour short
"03:05", "03:05:09")) //24 hour long
{
yield return data;
}
foreach (var data in GetDisplaysExpectedTextDataForCulture(spainSpanish, pm,
"4:30", "4:30:25", //12 hour short
"04:30", "04:30:25", //12 hour long
"16:30", "16:30:25", //24 hour short
"16:30", "16:30:25")) //24 hour long
{
yield return data;
}
//Iran Farsi fa-IR
var iranFarsi = CultureInfo.GetCultureInfo("fa-IR");
foreach (var data in GetDisplaysExpectedTextDataForCulture(iranFarsi, am,
"3:05 ق.ظ", "3:05:09 ق.ظ", //12 hour short
"03:05 ق.ظ", "03:05:09 ق.ظ", //12 hour long
"3:05", "3:05:09", //24 hour short
"03:05", "03:05:09")) //24 hour long
{
yield return data;
}
foreach (var data in GetDisplaysExpectedTextDataForCulture(iranFarsi, pm,
"4:30 ب.ظ", "4:30:25 ب.ظ", //12 hour short
"04:30 ب.ظ", "04:30:25 ب.ظ", //12 hour long
"16:30", "16:30:25", //24 hour short
"16:30", "16:30:25")) //24 hour long
{
yield return data;
}
}
private static IEnumerable<object[]> GetDisplaysExpectedTextDataForCulture(CultureInfo culture,
DateTime dateTime,
string short12Hour, string short12HourWithSeconds,
string long12Hour, string long12HourWithSeconds,
string short24Hour, string short24HourWithSeconds,
string long24Hour, string long24HourWithSeconds)
{
yield return new object[]
{
culture,
DatePickerFormat.Short,
false,
false,
dateTime,
short12Hour
};
yield return new object[]
{
culture,
DatePickerFormat.Short,
false,
true,
dateTime,
short12HourWithSeconds
};
yield return new object[]
{
culture,
DatePickerFormat.Long,
false,
false,
dateTime,
long12Hour
};
yield return new object[]
{
culture,
DatePickerFormat.Long,
false,
true,
dateTime,
long12HourWithSeconds
};
yield return new object[]
{
culture,
DatePickerFormat.Short,
true,
false,
dateTime,
short24Hour
};
yield return new object[]
{
culture,
DatePickerFormat.Short,
true,
true,
dateTime,
short24HourWithSeconds
};
yield return new object[]
{
culture,
DatePickerFormat.Long,
true,
false,
dateTime,
long24Hour
};
yield return new object[]
{
culture,
DatePickerFormat.Long,
true,
true,
dateTime,
long24HourWithSeconds
};
}
}
}