2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using Microsoft . Toolkit . Uwp ;
5
6
using Microsoft . Toolkit . Uwp . UI ;
6
7
using Microsoft . Toolkit . Uwp . UI . Controls ;
7
8
using Microsoft . VisualStudio . TestTools . UnitTesting ;
8
9
using Microsoft . VisualStudio . TestTools . UnitTesting . AppContainer ;
10
+ using System . Threading . Tasks ;
9
11
using Windows . UI . Xaml ;
12
+ using Windows . UI . Xaml . Controls ;
10
13
using Windows . UI . Xaml . Markup ;
11
14
12
15
namespace UnitTests . UWP . UI . Controls
13
16
{
14
17
[ TestClass ]
15
- public class Test_TokenizingTextBox_General
18
+ public class Test_TokenizingTextBox_General : VisualUITestBase
16
19
{
17
20
[ TestCategory ( "Test_TokenizingTextBox_General" ) ]
18
- [ UITestMethod ]
19
- public void Test_Clear ( )
21
+ [ TestMethod ]
22
+ public async Task Test_ClearTokens ( )
20
23
{
21
- var treeRoot = XamlReader . Load (
24
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
25
+ {
26
+ var treeRoot = XamlReader . Load (
22
27
@"<Page
23
28
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
24
29
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
@@ -29,49 +34,90 @@ public void Test_Clear()
29
34
30
35
</Page>" ) as FrameworkElement ;
31
36
32
- Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
37
+ Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
33
38
34
- var tokenBox = treeRoot . FindChild ( "tokenboxname" ) as TokenizingTextBox ;
39
+ await SetTestContentAsync ( treeRoot ) ;
35
40
36
- Assert . IsNotNull ( tokenBox , "Could not find TokenizingTextBox in tree." ) ;
41
+ var tokenBox = treeRoot . FindChild ( "tokenboxname" ) as TokenizingTextBox ;
37
42
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" ) ;
39
45
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" ) ;
44
51
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
46
53
47
- // now test clear
48
- Assert . IsTrue ( tokenBox . ClearAsync ( ) . Wait ( 200 ) , "Failed to wait for Clear() to finish" ) ;
54
+ var count = 0 ;
49
55
50
- Assert . AreEqual ( tokenBox . Items . Count , 1 , "Clear Failed to clear" ) ;
56
+ tokenBox . TokenItemRemoving += ( sender , args ) => { count ++ ; } ;
51
57
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 ( ) ;
57
60
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
+ }
59
65
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"">
61
77
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 ;
63
82
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
+ } ) ;
65
109
}
66
110
67
111
[ TestCategory ( "Test_TokenizingTextBox_General" ) ]
68
- [ UITestMethod ]
69
- public void Test_MaximumTokens ( )
112
+ [ TestMethod ]
113
+ public async Task Test_MaximumTokens ( )
70
114
{
71
- var maxTokens = 2 ;
115
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
116
+ {
117
+ var maxTokens = 2 ;
72
118
73
- var treeRoot = XamlReader . Load (
74
- $@ "<Page
119
+ var treeRoot = XamlReader . Load (
120
+ $@ "<Page
75
121
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
76
122
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
77
123
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
@@ -81,39 +127,42 @@ public void Test_MaximumTokens()
81
127
82
128
</Page>" ) as FrameworkElement ;
83
129
84
- Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
130
+ Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
131
+
132
+ await SetTestContentAsync ( treeRoot ) ;
85
133
86
- var tokenBox = treeRoot . FindChild ( "tokenboxname" ) as TokenizingTextBox ;
134
+ var tokenBox = treeRoot . FindChild ( "tokenboxname" ) as TokenizingTextBox ;
87
135
88
- Assert . IsNotNull ( tokenBox , "Could not find TokenizingTextBox in tree." ) ;
136
+ Assert . IsNotNull ( tokenBox , "Could not find TokenizingTextBox in tree." ) ;
89
137
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 ;
93
141
94
- // Add two items.
95
- tokenBox . AddTokenItem ( "TokenItem1" ) ;
96
- tokenBox . AddTokenItem ( "TokenItem2" ) ;
142
+ // Add two items.
143
+ tokenBox . AddTokenItem ( "TokenItem1" ) ;
144
+ tokenBox . AddTokenItem ( "TokenItem2" ) ;
97
145
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 ] ) ;
102
150
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" ) ;
105
153
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 ] ) ;
110
158
111
- // Reduce the maximum number of tokens.
112
- tokenBox . MaximumTokens = 1 ;
159
+ // Reduce the maximum number of tokens.
160
+ tokenBox . MaximumTokens = 1 ;
113
161
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
+ } ) ;
117
166
}
118
167
}
119
168
}
0 commit comments