Skip to content

Commit 39925b9

Browse files
Switch to using VisualUITestBase for general TokenizingTextBox tests, use proper async tests
Also splits out Clear test into two separate tests.
1 parent d039aa8 commit 39925b9

File tree

1 file changed

+103
-54
lines changed

1 file changed

+103
-54
lines changed

UnitTests/UnitTests.UWP/UI/Controls/Test_TokenizingTextBox_General.cs

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Toolkit.Uwp;
56
using Microsoft.Toolkit.Uwp.UI;
67
using Microsoft.Toolkit.Uwp.UI.Controls;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
89
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
10+
using System.Threading.Tasks;
911
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
1013
using Windows.UI.Xaml.Markup;
1114

1215
namespace UnitTests.UWP.UI.Controls
1316
{
1417
[TestClass]
15-
public class Test_TokenizingTextBox_General
18+
public class Test_TokenizingTextBox_General : VisualUITestBase
1619
{
1720
[TestCategory("Test_TokenizingTextBox_General")]
18-
[UITestMethod]
19-
public void Test_Clear()
21+
[TestMethod]
22+
public async Task Test_ClearTokens()
2023
{
21-
var treeRoot = XamlReader.Load(
24+
await App.DispatcherQueue.EnqueueAsync(async () =>
25+
{
26+
var treeRoot = XamlReader.Load(
2227
@"<Page
2328
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
2429
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
@@ -29,49 +34,90 @@ public void Test_Clear()
2934
3035
</Page>") as FrameworkElement;
3136

32-
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
37+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
3338

34-
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
39+
await SetTestContentAsync(treeRoot);
3540

36-
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
41+
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
3742

38-
Assert.AreEqual(tokenBox.Items.Count, 1, "Token default items failed");
43+
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
44+
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed");
3945

40-
tokenBox.AddTokenItem("TokenItem1");
41-
tokenBox.AddTokenItem("TokenItem2");
42-
tokenBox.AddTokenItem("TokenItem3");
43-
tokenBox.AddTokenItem("TokenItem4");
46+
// Add 4 items
47+
tokenBox.AddTokenItem("TokenItem1");
48+
tokenBox.AddTokenItem("TokenItem2");
49+
tokenBox.AddTokenItem("TokenItem3");
50+
tokenBox.AddTokenItem("TokenItem4");
4451

45-
Assert.AreEqual(tokenBox.Items.Count, 5, "Token Add count failed");
52+
Assert.AreEqual(5, tokenBox.Items.Count, "Token Add count failed"); // 5th item is the textbox
4653

47-
// now test clear
48-
Assert.IsTrue(tokenBox.ClearAsync().Wait(200), "Failed to wait for Clear() to finish");
54+
var count = 0;
4955

50-
Assert.AreEqual(tokenBox.Items.Count, 1, "Clear Failed to clear");
56+
tokenBox.TokenItemRemoving += (sender, args) => { count++; };
5157

52-
// test cancelled clear
53-
tokenBox.AddTokenItem("TokenItem1");
54-
tokenBox.AddTokenItem("TokenItem2");
55-
tokenBox.AddTokenItem("TokenItem3");
56-
tokenBox.AddTokenItem("TokenItem4");
58+
// now test clear
59+
await tokenBox.ClearAsync();
5760

58-
Assert.AreEqual(tokenBox.Items.Count, 5, "Token Add count failed");
61+
Assert.AreEqual(1, tokenBox.Items.Count, "Clear Failed to clear"); // Still expect textbox to remain
62+
Assert.AreEqual(4, count, "Did not receive 4 removal events.");
63+
});
64+
}
5965

60-
tokenBox.TokenItemRemoving += (sender, args) => { args.Cancel = true; };
66+
[TestCategory("Test_TokenizingTextBox_General")]
67+
[TestMethod]
68+
public async Task Test_ClearTokenCancel()
69+
{
70+
await App.DispatcherQueue.EnqueueAsync(async () =>
71+
{
72+
var treeRoot = XamlReader.Load(
73+
@"<Page
74+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
75+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
76+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
6177
62-
Assert.IsTrue(tokenBox.ClearAsync().Wait(200), "Failed to wait for Clear() to finish");
78+
<controls:TokenizingTextBox x:Name=""tokenboxname"">
79+
</controls:TokenizingTextBox>
80+
81+
</Page>") as FrameworkElement;
6382

64-
Assert.AreEqual(tokenBox.Items.Count, 5, "Cancelled Clear Failed ");
83+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
84+
85+
await SetTestContentAsync(treeRoot);
86+
87+
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
88+
89+
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
90+
Assert.AreEqual(1, tokenBox.Items.Count, "Token default items failed");
91+
92+
// test cancelled clear
93+
tokenBox.AddTokenItem("TokenItem1");
94+
tokenBox.AddTokenItem("TokenItem2");
95+
tokenBox.AddTokenItem("TokenItem3");
96+
tokenBox.AddTokenItem("TokenItem4");
97+
98+
Assert.AreEqual(5, tokenBox.Items.Count, "Token Add count failed");
99+
100+
tokenBox.TokenItemRemoving += (sender, args) => { args.Cancel = true; };
101+
102+
await tokenBox.ClearAsync();
103+
104+
// Should have the same number of items left
105+
Assert.AreEqual(5, tokenBox.Items.Count, "Cancelled Clear Failed ");
106+
107+
// TODO: We should have test for individual removal as well.
108+
});
65109
}
66110

67111
[TestCategory("Test_TokenizingTextBox_General")]
68-
[UITestMethod]
69-
public void Test_MaximumTokens()
112+
[TestMethod]
113+
public async Task Test_MaximumTokens()
70114
{
71-
var maxTokens = 2;
115+
await App.DispatcherQueue.EnqueueAsync(async () =>
116+
{
117+
var maxTokens = 2;
72118

73-
var treeRoot = XamlReader.Load(
74-
$@"<Page
119+
var treeRoot = XamlReader.Load(
120+
$@"<Page
75121
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
76122
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
77123
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
@@ -81,39 +127,42 @@ public void Test_MaximumTokens()
81127
82128
</Page>") as FrameworkElement;
83129

84-
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
130+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
131+
132+
await SetTestContentAsync(treeRoot);
85133

86-
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
134+
var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
87135

88-
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
136+
Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
89137

90-
// Items includes the text fields as well, so we can expect at least one item to exist initially, the input box.
91-
// Use the starting count as an offset.
92-
var startingItemsCount = tokenBox.Items.Count;
138+
// Items includes the text fields as well, so we can expect at least one item to exist initially, the input box.
139+
// Use the starting count as an offset.
140+
var startingItemsCount = tokenBox.Items.Count;
93141

94-
// Add two items.
95-
tokenBox.AddTokenItem("TokenItem1");
96-
tokenBox.AddTokenItem("TokenItem2");
142+
// Add two items.
143+
tokenBox.AddTokenItem("TokenItem1");
144+
tokenBox.AddTokenItem("TokenItem2");
97145

98-
// Make sure we have the appropriate amount of items and that they are in the appropriate order.
99-
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add failed");
100-
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
101-
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
146+
// Make sure we have the appropriate amount of items and that they are in the appropriate order.
147+
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add failed");
148+
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
149+
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
102150

103-
// Attempt to add an additional item, beyond the maximum.
104-
tokenBox.AddTokenItem("TokenItem3");
151+
// Attempt to add an additional item, beyond the maximum.
152+
tokenBox.AddTokenItem("TokenItem3");
105153

106-
// Check that the number of items did not change, because the maximum number of items are already present.
107-
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add succeeded, where it should have failed.");
108-
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
109-
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
154+
// Check that the number of items did not change, because the maximum number of items are already present.
155+
Assert.AreEqual(startingItemsCount + maxTokens, tokenBox.Items.Count, "Token Add succeeded, where it should have failed.");
156+
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
157+
Assert.AreEqual("TokenItem2", tokenBox.Items[1]);
110158

111-
// Reduce the maximum number of tokens.
112-
tokenBox.MaximumTokens = 1;
159+
// Reduce the maximum number of tokens.
160+
tokenBox.MaximumTokens = 1;
113161

114-
// The last token should be removed to account for the reduced maximum.
115-
Assert.AreEqual(startingItemsCount + 1, tokenBox.Items.Count);
116-
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
162+
// The last token should be removed to account for the reduced maximum.
163+
Assert.AreEqual(startingItemsCount + 1, tokenBox.Items.Count);
164+
Assert.AreEqual("TokenItem1", tokenBox.Items[0]);
165+
});
117166
}
118167
}
119168
}

0 commit comments

Comments
 (0)