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 . Linq ;
6
+ using System . Threading . Tasks ;
7
+ using Microsoft . Toolkit . Uwp ;
8
+ using Microsoft . Toolkit . Uwp . UI ;
9
+ using Microsoft . Toolkit . Uwp . UI . Controls ;
10
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
11
+ using Microsoft . VisualStudio . TestTools . UnitTesting . AppContainer ;
12
+ using Windows . Foundation ;
13
+ using Windows . UI . Xaml ;
14
+ using Windows . UI . Xaml . Controls ;
15
+ using Windows . UI . Xaml . Markup ;
16
+
17
+ namespace UnitTests . UI . Controls
18
+ {
19
+ [ TestClass ]
20
+ public class Test_WrapPanel_Visibility : VisualUITestBase
21
+ {
22
+ [ TestCategory ( "WrapPanel" ) ]
23
+ [ TestMethod ]
24
+ public async Task Test_WrapPanel_Visibility_Horizontal_First ( )
25
+ {
26
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
27
+ {
28
+ var treeRoot = XamlReader . Load ( @"<Page
29
+ xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
30
+ xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
31
+ xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
32
+ <controls:WrapPanel x:Name=""WrapPanel"">
33
+ <Border Width=""150"" Height=""50"" Visibility=""Collapsed""/>
34
+ <Border Width=""100"" Height=""50""/>
35
+ <Border Width=""125"" Height=""50""/>
36
+ <Border Width=""50"" Height=""50""/>
37
+ </controls:WrapPanel>
38
+ </Page>" ) as FrameworkElement ;
39
+
40
+ var expected = new ( int u , int v , int w , int h ) [ ]
41
+ {
42
+ ( 0 , 0 , 0 , 0 ) , // Collapsed
43
+ ( 0 , 0 , 100 , 50 ) ,
44
+ ( 100 , 0 , 125 , 50 ) ,
45
+ ( 225 , 0 , 50 , 50 ) ,
46
+ } ;
47
+
48
+ Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
49
+
50
+ // Initialize Visual Tree
51
+ await SetTestContentAsync ( treeRoot ) ;
52
+
53
+ var panel = treeRoot . FindChild ( "WrapPanel" ) as WrapPanel ;
54
+
55
+ Assert . IsNotNull ( panel , "Could not find WrapPanel in tree." ) ;
56
+
57
+ // Force Layout calculations
58
+ panel . UpdateLayout ( ) ;
59
+
60
+ var children = panel . Children . Select ( item => item as FrameworkElement ) . ToArray ( ) ;
61
+
62
+ Assert . AreEqual ( 4 , panel . Children . Count ) ;
63
+
64
+ // Check all children are in expected places.
65
+ for ( int i = 0 ; i < children . Count ( ) ; i ++ )
66
+ {
67
+ var transform = treeRoot . CoordinatesTo ( children [ i ] ) ;
68
+ Assert . AreEqual ( expected [ i ] . u , transform . X , $ "Child { i } not in expected X location.") ;
69
+ Assert . AreEqual ( expected [ i ] . v , transform . Y , $ "Child { i } not in expected Y location.") ;
70
+
71
+ Assert . AreEqual ( expected [ i ] . w , children [ i ] . ActualWidth , $ "Child { i } not of expected width.") ;
72
+ Assert . AreEqual ( expected [ i ] . h , children [ i ] . ActualHeight , $ "Child { i } not of expected height.") ;
73
+ }
74
+ } ) ;
75
+ }
76
+
77
+ //// TODO: Add matching tests with spacing inbetween.
78
+
79
+ [ TestCategory ( "WrapPanel" ) ]
80
+ [ TestMethod ]
81
+ public async Task Test_WrapPanel_Visibility_Horizontal_Middle ( )
82
+ {
83
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
84
+ {
85
+ var treeRoot = XamlReader . Load ( @"<Page
86
+ xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
87
+ xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
88
+ xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
89
+ <controls:WrapPanel x:Name=""WrapPanel"">
90
+ <Border Width=""150"" Height=""50""/>
91
+ <Border Width=""100"" Height=""50"" Visibility=""Collapsed""/>
92
+ <Border Width=""125"" Height=""50""/>
93
+ <Border Width=""50"" Height=""50""/>
94
+ </controls:WrapPanel>
95
+ </Page>" ) as FrameworkElement ;
96
+
97
+ var expected = new ( int u , int v , int w , int h ) [ ]
98
+ {
99
+ ( 0 , 0 , 150 , 50 ) ,
100
+ ( 150 , 0 , 0 , 0 ) , // Collapsed
101
+ ( 150 , 0 , 125 , 50 ) ,
102
+ ( 275 , 0 , 50 , 50 ) ,
103
+ } ;
104
+
105
+ Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
106
+
107
+ // Initialize Visual Tree
108
+ await SetTestContentAsync ( treeRoot ) ;
109
+
110
+ var panel = treeRoot . FindChild ( "WrapPanel" ) as WrapPanel ;
111
+
112
+ Assert . IsNotNull ( panel , "Could not find WrapPanel in tree." ) ;
113
+
114
+ // Force Layout calculations
115
+ panel . UpdateLayout ( ) ;
116
+
117
+ var children = panel . Children . Select ( item => item as FrameworkElement ) . ToArray ( ) ;
118
+
119
+ Assert . AreEqual ( 4 , panel . Children . Count ) ;
120
+
121
+ // Check all children are in expected places.
122
+ for ( int i = 0 ; i < children . Count ( ) ; i ++ )
123
+ {
124
+ var transform = treeRoot . CoordinatesTo ( children [ i ] ) ;
125
+ Assert . AreEqual ( expected [ i ] . u , transform . X , $ "Child { i } not in expected X location.") ;
126
+ Assert . AreEqual ( expected [ i ] . v , transform . Y , $ "Child { i } not in expected Y location.") ;
127
+
128
+ Assert . AreEqual ( expected [ i ] . w , children [ i ] . ActualWidth , $ "Child { i } not of expected width.") ;
129
+ Assert . AreEqual ( expected [ i ] . h , children [ i ] . ActualHeight , $ "Child { i } not of expected height.") ;
130
+ }
131
+ } ) ;
132
+ }
133
+
134
+ [ TestCategory ( "WrapPanel" ) ]
135
+ [ TestMethod ]
136
+ public async Task Test_WrapPanel_Visibility_Horizontal_Last ( )
137
+ {
138
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
139
+ {
140
+ var treeRoot = XamlReader . Load ( @"<Page
141
+ xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
142
+ xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
143
+ xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
144
+ <controls:WrapPanel x:Name=""WrapPanel"">
145
+ <Border Width=""150"" Height=""50""/>
146
+ <Border Width=""100"" Height=""50""/>
147
+ <Border Width=""125"" Height=""50""/>
148
+ <Border Width=""50"" Height=""50"" Visibility=""Collapsed""/>
149
+ </controls:WrapPanel>
150
+ </Page>" ) as FrameworkElement ;
151
+
152
+ var expected = new ( int u , int v , int w , int h ) [ ]
153
+ {
154
+ ( 0 , 0 , 150 , 50 ) ,
155
+ ( 150 , 0 , 100 , 50 ) ,
156
+ ( 250 , 0 , 125 , 50 ) ,
157
+ ( 375 , 0 , 0 , 0 ) , // Collapsed
158
+ } ;
159
+
160
+ Assert . IsNotNull ( treeRoot , "Could not load XAML tree." ) ;
161
+
162
+ // Initialize Visual Tree
163
+ await SetTestContentAsync ( treeRoot ) ;
164
+
165
+ var panel = treeRoot . FindChild ( "WrapPanel" ) as WrapPanel ;
166
+
167
+ Assert . IsNotNull ( panel , "Could not find WrapPanel in tree." ) ;
168
+
169
+ // Force Layout calculations
170
+ panel . UpdateLayout ( ) ;
171
+
172
+ var children = panel . Children . Select ( item => item as FrameworkElement ) . ToArray ( ) ;
173
+
174
+ Assert . AreEqual ( 4 , panel . Children . Count ) ;
175
+
176
+ // Check all children are in expected places.
177
+ for ( int i = 0 ; i < children . Count ( ) ; i ++ )
178
+ {
179
+ var transform = treeRoot . CoordinatesTo ( children [ i ] ) ;
180
+ Assert . AreEqual ( expected [ i ] . u , transform . X , $ "Child { i } not in expected X location.") ;
181
+ Assert . AreEqual ( expected [ i ] . v , transform . Y , $ "Child { i } not in expected Y location.") ;
182
+
183
+ Assert . AreEqual ( expected [ i ] . w , children [ i ] . ActualWidth , $ "Child { i } not of expected width.") ;
184
+ Assert . AreEqual ( expected [ i ] . h , children [ i ] . ActualHeight , $ "Child { i } not of expected height.") ;
185
+ }
186
+ } ) ;
187
+ }
188
+
189
+ //// TODO: Add tests for wrapping lines based on size.
190
+ //// TODO: Add a test with StretchChild behavior as well after...
191
+ }
192
+ }
0 commit comments