-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Test_TokenizingTextBox_General.cs
372 lines (270 loc) · 15.2 KB
/
Test_TokenizingTextBox_General.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Toolkit.Uwp;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
namespace UnitTests.UWP.UI.Controls
{
[TestClass]
public class Test_TokenizingTextBox_General : VisualUITestBase
{
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_ClearTokens()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"">
</controls:TokenizingTextBox>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed");
// Add 4 items
tokenBox.AddTokenItem("TokenItem1");
tokenBox.AddTokenItem("TokenItem2");
tokenBox.AddTokenItem("TokenItem3");
tokenBox.AddTokenItem("TokenItem4");
Assert.AreEqual(5, tokenBox.Items.Count, "Token Add count failed"); // 5th item is the textbox
var count = 0;
tokenBox.TokenItemRemoving += (sender, args) => { count++; };
// now test clear
await tokenBox.ClearAsync();
Assert.AreEqual(1, tokenBox.Items.Count, "Clear Failed to clear"); // Still expect textbox to remain
Assert.AreEqual(4, count, "Did not receive 4 removal events.");
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_ClearTokenCancel()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"">
</controls:TokenizingTextBox>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed");
// test cancelled clear
tokenBox.AddTokenItem("TokenItem1");
tokenBox.AddTokenItem("TokenItem2");
tokenBox.AddTokenItem("TokenItem3");
tokenBox.AddTokenItem("TokenItem4");
Assert.AreEqual(5, tokenBox.Items.Count, "Token Add count failed");
tokenBox.TokenItemRemoving += (sender, args) => { args.Cancel = true; };
await tokenBox.ClearAsync();
// Should have the same number of items left
Assert.AreEqual(5, tokenBox.Items.Count, "Cancelled Clear Failed ");
// TODO: We should have test for individual removal as well.
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_MaximumTokens()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var maxTokens = 2;
var treeRoot = XamlReader.Load(
$@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"" MaximumTokens=""{maxTokens}"">
</controls:TokenizingTextBox>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
// Items includes the text fields as well, so we can expect at least one item to exist initially, the input box.
// Use the starting count as an offset.
var startingItemsCount = tokenBox.Items.Count;
// Add two items.
tokenBox.AddTokenItem("TokenItem1");
tokenBox.AddTokenItem("TokenItem2");
// Make sure we have the appropriate amount of items and that they are in the appropriate order.
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add failed");
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
// Attempt to add an additional item, beyond the maximum.
tokenBox.AddTokenItem("TokenItem3");
// Check that the number of items did not change, because the maximum number of items are already present.
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add succeeded, where it should have failed.");
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
// Reduce the maximum number of tokens.
tokenBox.MaximumTokens = 1;
// The last token should be removed to account for the reduced maximum.
Assert.AreEqual(startingItemsCount + 1, tokenBox.Items.Count);
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_SetInitialText()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"" Text=""Some Text""/>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed"); // AutoSuggestBox
// Test initial value of property
Assert.AreEqual("Some Text", tokenBox.Text, "Token text not equal to starting value.");
// Reach into AutoSuggestBox's text to check it was set properly
var autoSuggestBox = tokenBox.FindDescendant<AutoSuggestBox>();
Assert.IsNotNull(autoSuggestBox, "Could not find inner autosuggestbox");
Assert.AreEqual("Some Text", autoSuggestBox.Text, "Inner text not set based on initial value of TokenizingTextBox");
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_ChangeText()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname""/>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed"); // AutoSuggestBox
// Test initial value of property
Assert.AreEqual(string.Empty, tokenBox.Text, "Text should start as empty.");
// Reach into AutoSuggestBox's text to check it was set properly
var autoSuggestBox = tokenBox.FindDescendant<AutoSuggestBox>();
Assert.IsNotNull(autoSuggestBox, "Could not find inner autosuggestbox");
Assert.AreEqual(string.Empty, autoSuggestBox.Text, "Inner text not set based on initial value of TokenizingTextBox");
// Change Text
tokenBox.Text = "New Text";
// Wait for update
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
Assert.AreEqual("New Text", tokenBox.Text, "Text should be changed now.");
Assert.AreEqual("New Text", autoSuggestBox.Text, "Inner text not set based on value of TokenizingTextBox");
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_ClearText()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"" Text=""Some Text""/>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed"); // AutoSuggestBox
// TODO: When in Labs, we should inject text via keyboard here vs. setting an initial value (more independent of SetInitialText test).
// Test initial value of property
Assert.AreEqual("Some Text", tokenBox.Text, "Token text not equal to starting value.");
// Reach into AutoSuggestBox's text to check it was set properly
var autoSuggestBox = tokenBox.FindDescendant<AutoSuggestBox>();
Assert.IsNotNull(autoSuggestBox, "Could not find inner autosuggestbox");
Assert.AreEqual("Some Text", autoSuggestBox.Text, "Inner text not set based on initial value of TokenizingTextBox");
await tokenBox.ClearAsync();
Assert.AreEqual(string.Empty, autoSuggestBox.Text, "Inner text was not cleared.");
Assert.AreEqual(string.Empty, tokenBox.Text, "TokenizingTextBox text was not cleared.");
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_SetInitialTextWithDelimiter()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"" TokenDelimiter="","" Text=""Token 1, Token 2, Token 3""/>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Tokens not created"); // AutoSuggestBox
Assert.AreEqual("Token 1, Token 2, Token 3", tokenBox.Text, "Token text not equal to starting value.");
await Task.Delay(500); // TODO: Wait for a loaded event?
Assert.AreEqual(1 + 2, tokenBox.Items.Count, "Tokens not created");
// Test initial value of property
Assert.AreEqual("Token 3", tokenBox.Text, "Token text should be last value now.");
Assert.AreEqual("Token 1", tokenBox.Items[0]);
Assert.AreEqual("Token 2", tokenBox.Items[1]);
});
}
[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_SetInitialTextWithDelimiterAll()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:TokenizingTextBox x:Name=""tokenboxname"" TokenDelimiter="","" Text=""Token 1, Token 2, Token 3, ""/>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await SetTestContentAsync(treeRoot);
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.AreEqual(1, tokenBox.Items.Count, "Tokens not created"); // AutoSuggestBox
Assert.AreEqual("Token 1, Token 2, Token 3, ", tokenBox.Text, "Token text not equal to starting value.");
await Task.Delay(500); // TODO: Wait for a loaded event?
Assert.AreEqual(1 + 3, tokenBox.Items.Count, "Tokens not created");
// Test initial value of property
Assert.AreEqual(string.Empty, tokenBox.Text, "Token text should be blank now.");
Assert.AreEqual("Token 1", tokenBox.Items[0]);
Assert.AreEqual("Token 2", tokenBox.Items[1]);
Assert.AreEqual("Token 3", tokenBox.Items[2]);
});
}
}
}