1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System . Collections . Generic ;
6
+ using System . Collections . ObjectModel ;
7
+ using System . Linq ;
8
+ using System . Threading . Tasks ;
9
+ using Windows . UI . Xaml . Automation ;
10
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
11
+ using Windows . UI . Xaml . Automation . Peers ;
12
+ using Microsoft . Toolkit . Uwp ;
13
+ using Microsoft . Toolkit . Uwp . UI . Automation . Peers ;
14
+ using Microsoft . Toolkit . Uwp . UI . Controls ;
15
+
16
+ namespace UnitTests . UWP . UI . Controls
17
+ {
18
+ [ TestClass ]
19
+ [ TestCategory ( "Test_TokenizingTextBox" ) ]
20
+ public class Test_TokenizingTextBox_AutomationPeer : VisualUITestBase
21
+ {
22
+ [ TestMethod ]
23
+ public async Task ShouldConfigureTokenizingTextBoxAutomationPeerAsync ( )
24
+ {
25
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
26
+ {
27
+ const string expectedAutomationName = "MyAutomationName" ;
28
+ const string expectedName = "MyName" ;
29
+ const string expectedValue = "Wor" ;
30
+
31
+ var items = new ObservableCollection < TokenizingTextBoxTestItem > { new ( ) { Title = "Hello" } , new ( ) { Title = "World" } } ;
32
+
33
+ var tokenizingTextBox = new TokenizingTextBox { ItemsSource = items } ;
34
+
35
+ await SetTestContentAsync ( tokenizingTextBox ) ;
36
+
37
+ var tokenizingTextBoxAutomationPeer =
38
+ FrameworkElementAutomationPeer . CreatePeerForElement ( tokenizingTextBox ) as TokenizingTextBoxAutomationPeer ;
39
+
40
+ Assert . IsNotNull ( tokenizingTextBoxAutomationPeer , "Verify that the AutomationPeer is TokenizingTextBoxAutomationPeer." ) ;
41
+
42
+ // Asserts the automation peer name based on the Automation Property Name value.
43
+ tokenizingTextBox . SetValue ( AutomationProperties . NameProperty , expectedAutomationName ) ;
44
+ Assert . IsTrue ( tokenizingTextBoxAutomationPeer . GetName ( ) . Contains ( expectedAutomationName ) , "Verify that the UIA name contains the given AutomationProperties.Name of the TokenizingTextBox." ) ;
45
+
46
+ // Asserts the automation peer name based on the element Name property.
47
+ tokenizingTextBox . Name = expectedName ;
48
+ Assert . IsTrue ( tokenizingTextBoxAutomationPeer . GetName ( ) . Contains ( expectedName ) , "Verify that the UIA name contains the given Name of the TokenizingTextBox." ) ;
49
+
50
+ tokenizingTextBoxAutomationPeer . SetValue ( expectedValue ) ;
51
+ Assert . IsTrue ( tokenizingTextBoxAutomationPeer . Value . Equals ( expectedValue ) , "Verify that the Value contains the given Text of the TokenizingTextBox." ) ;
52
+ } ) ;
53
+ }
54
+
55
+ [ TestMethod ]
56
+ public async Task ShouldReturnTokensForTokenizingTextBoxAutomationPeerAsync ( )
57
+ {
58
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
59
+ {
60
+ var items = new ObservableCollection < TokenizingTextBoxTestItem >
61
+ {
62
+ new ( ) { Title = "Hello" } , new ( ) { Title = "World" }
63
+ } ;
64
+
65
+ var tokenizingTextBox = new TokenizingTextBox { ItemsSource = items } ;
66
+
67
+ await SetTestContentAsync ( tokenizingTextBox ) ;
68
+
69
+ tokenizingTextBox
70
+ . SelectAllTokensAndText ( ) ; // Will be 3 items due to the `AndText` that will select an empty text item.
71
+
72
+ var tokenizingTextBoxAutomationPeer =
73
+ FrameworkElementAutomationPeer . CreatePeerForElement ( tokenizingTextBox ) as
74
+ TokenizingTextBoxAutomationPeer ;
75
+
76
+ Assert . IsNotNull (
77
+ tokenizingTextBoxAutomationPeer ,
78
+ "Verify that the AutomationPeer is TokenizingTextBoxAutomationPeer." ) ;
79
+
80
+ var selectedItems = tokenizingTextBoxAutomationPeer
81
+ . GetChildren ( )
82
+ . Cast < ListViewItemAutomationPeer > ( )
83
+ . Select ( peer => peer . Owner as TokenizingTextBoxItem )
84
+ . Select ( item => item ? . Content as TokenizingTextBoxTestItem )
85
+ . ToList ( ) ;
86
+
87
+ Assert . AreEqual ( 3 , selectedItems . Count ) ;
88
+ Assert . AreEqual ( items [ 0 ] , selectedItems [ 0 ] ) ;
89
+ Assert . AreEqual ( items [ 1 ] , selectedItems [ 1 ] ) ;
90
+ Assert . IsNull ( selectedItems [ 2 ] ) ; // The 3rd item is the empty text item.
91
+ } ) ;
92
+ }
93
+
94
+ [ TestMethod ]
95
+ public async Task ShouldThrowElementNotEnabledExceptionIfValueSetWhenDisabled ( )
96
+ {
97
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
98
+ {
99
+ const string expectedValue = "Wor" ;
100
+
101
+ var tokenizingTextBox = new TokenizingTextBox { IsEnabled = false } ;
102
+
103
+ await SetTestContentAsync ( tokenizingTextBox ) ;
104
+
105
+ var tokenizingTextBoxAutomationPeer =
106
+ FrameworkElementAutomationPeer . CreatePeerForElement ( tokenizingTextBox ) as TokenizingTextBoxAutomationPeer ;
107
+
108
+ Assert . ThrowsException < ElementNotEnabledException > ( ( ) =>
109
+ {
110
+ tokenizingTextBoxAutomationPeer . SetValue ( expectedValue ) ;
111
+ } ) ;
112
+ } ) ;
113
+ }
114
+
115
+ public class TokenizingTextBoxTestItem
116
+ {
117
+ public string Title { get ; set ; }
118
+
119
+ public override string ToString ( )
120
+ {
121
+ return Title ;
122
+ }
123
+ }
124
+ }
125
+ }
0 commit comments