@@ -8,7 +8,7 @@ public class UniformGridTests
8
8
[ Fact ]
9
9
public void Grid_Columns_Equals_Rows_For_Auto_Columns_And_Rows ( )
10
10
{
11
- var target = new UniformGrid ( )
11
+ var target = new UniformGrid
12
12
{
13
13
Children =
14
14
{
@@ -21,14 +21,15 @@ public void Grid_Columns_Equals_Rows_For_Auto_Columns_And_Rows()
21
21
target . Measure ( Size . Infinity ) ;
22
22
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
23
23
24
- // 2 * 2 grid
25
- Assert . Equal ( new Size ( 2 * 80 , 2 * 90 ) , target . Bounds . Size ) ;
24
+ // 2 * 2 grid => each cell: 80 x 90
25
+ // Final size => (2 * 80) x (2 * 90) = 160 x 180
26
+ Assert . Equal ( new Size ( 160 , 180 ) , target . Bounds . Size ) ;
26
27
}
27
28
28
29
[ Fact ]
29
30
public void Grid_Expands_Vertically_For_Columns_With_Auto_Rows ( )
30
31
{
31
- var target = new UniformGrid ( )
32
+ var target = new UniformGrid
32
33
{
33
34
Columns = 2 ,
34
35
Children =
@@ -44,14 +45,15 @@ public void Grid_Expands_Vertically_For_Columns_With_Auto_Rows()
44
45
target . Measure ( Size . Infinity ) ;
45
46
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
46
47
47
- // 2 * 3 grid
48
- Assert . Equal ( new Size ( 2 * 80 , 3 * 90 ) , target . Bounds . Size ) ;
48
+ // 2 * 3 grid => each cell: 80 x 90
49
+ // Final size => (2 * 80) x (3 * 90) = 160 x 270
50
+ Assert . Equal ( new Size ( 160 , 270 ) , target . Bounds . Size ) ;
49
51
}
50
52
51
53
[ Fact ]
52
54
public void Grid_Extends_For_Columns_And_First_Column_With_Auto_Rows ( )
53
55
{
54
- var target = new UniformGrid ( )
56
+ var target = new UniformGrid
55
57
{
56
58
Columns = 3 ,
57
59
FirstColumn = 2 ,
@@ -68,14 +70,15 @@ public void Grid_Extends_For_Columns_And_First_Column_With_Auto_Rows()
68
70
target . Measure ( Size . Infinity ) ;
69
71
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
70
72
71
- // 3 * 3 grid
72
- Assert . Equal ( new Size ( 3 * 80 , 3 * 90 ) , target . Bounds . Size ) ;
73
+ // 3 * 3 grid => each cell: 80 x 90
74
+ // Final size => (3 * 80) x (3 * 90) = 240 x 270
75
+ Assert . Equal ( new Size ( 240 , 270 ) , target . Bounds . Size ) ;
73
76
}
74
77
75
78
[ Fact ]
76
79
public void Grid_Expands_Horizontally_For_Rows_With_Auto_Columns ( )
77
80
{
78
- var target = new UniformGrid ( )
81
+ var target = new UniformGrid
79
82
{
80
83
Rows = 2 ,
81
84
Children =
@@ -91,14 +94,15 @@ public void Grid_Expands_Horizontally_For_Rows_With_Auto_Columns()
91
94
target . Measure ( Size . Infinity ) ;
92
95
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
93
96
94
- // 3 * 2 grid
95
- Assert . Equal ( new Size ( 3 * 80 , 2 * 90 ) , target . Bounds . Size ) ;
97
+ // 3 * 2 grid => each cell: 80 x 90
98
+ // Final size => (3 * 80) x (2 * 90) = 240 x 180
99
+ Assert . Equal ( new Size ( 240 , 180 ) , target . Bounds . Size ) ;
96
100
}
97
101
98
102
[ Fact ]
99
103
public void Grid_Size_Is_Limited_By_Rows_And_Columns ( )
100
104
{
101
- var target = new UniformGrid ( )
105
+ var target = new UniformGrid
102
106
{
103
107
Columns = 2 ,
104
108
Rows = 2 ,
@@ -115,14 +119,15 @@ public void Grid_Size_Is_Limited_By_Rows_And_Columns()
115
119
target . Measure ( Size . Infinity ) ;
116
120
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
117
121
118
- // 2 * 2 grid
119
- Assert . Equal ( new Size ( 2 * 80 , 2 * 90 ) , target . Bounds . Size ) ;
122
+ // 2 * 2 grid => each cell: 80 x 90
123
+ // Final size => (2 * 80) x (2 * 90) = 160 x 180
124
+ Assert . Equal ( new Size ( 160 , 180 ) , target . Bounds . Size ) ;
120
125
}
121
126
122
127
[ Fact ]
123
128
public void Not_Visible_Children_Are_Ignored ( )
124
129
{
125
- var target = new UniformGrid ( )
130
+ var target = new UniformGrid
126
131
{
127
132
Children =
128
133
{
@@ -137,8 +142,167 @@ public void Not_Visible_Children_Are_Ignored()
137
142
target . Measure ( Size . Infinity ) ;
138
143
target . Arrange ( new Rect ( target . DesiredSize ) ) ;
139
144
140
- // 2 * 2 grid
141
- Assert . Equal ( new Size ( 2 * 50 , 2 * 70 ) , target . Bounds . Size ) ;
145
+ // Visible children: 4
146
+ // Auto => 2 x 2 grid => each cell: 50 x 70
147
+ // Final size => (2 * 50) x (2 * 70) = 100 x 140
148
+ Assert . Equal ( new Size ( 100 , 140 ) , target . Bounds . Size ) ;
149
+ }
150
+
151
+ //
152
+ // New tests to cover RowSpacing and ColumnSpacing
153
+ //
154
+
155
+ [ Fact ]
156
+ public void Grid_Respects_ColumnSpacing_For_Auto_Columns_And_Rows ( )
157
+ {
158
+ // We have 3 visible children and no fixed Rows/Columns => 2x2 grid
159
+ // Largest child is 80 x 90. ColumnSpacing = 10, RowSpacing = 0
160
+ var target = new UniformGrid
161
+ {
162
+ ColumnSpacing = 10 ,
163
+ Children =
164
+ {
165
+ new Border { Width = 50 , Height = 70 } ,
166
+ new Border { Width = 30 , Height = 50 } ,
167
+ new Border { Width = 80 , Height = 90 }
168
+ }
169
+ } ;
170
+
171
+ target . Measure ( Size . Infinity ) ;
172
+ target . Arrange ( new Rect ( target . DesiredSize ) ) ;
173
+
174
+ // Without spacing => width = 2*80 = 160, height = 2*90 = 180
175
+ // With columnSpacing=10 => total width = 2*80 + 1*10 = 170
176
+ // RowSpacing=0 => total height = 180
177
+ Assert . Equal ( new Size ( 170 , 180 ) , target . Bounds . Size ) ;
178
+ }
179
+
180
+ [ Fact ]
181
+ public void Grid_Respects_RowSpacing_For_Auto_Columns_And_Rows ( )
182
+ {
183
+ // 3 visible children => 2x2 grid again
184
+ // Largest child is 80 x 90. RowSpacing = 15, ColumnSpacing = 0
185
+ var target = new UniformGrid
186
+ {
187
+ RowSpacing = 15 ,
188
+ Children =
189
+ {
190
+ new Border { Width = 50 , Height = 70 } ,
191
+ new Border { Width = 30 , Height = 50 } ,
192
+ new Border { Width = 80 , Height = 90 }
193
+ }
194
+ } ;
195
+
196
+ target . Measure ( Size . Infinity ) ;
197
+ target . Arrange ( new Rect ( target . DesiredSize ) ) ;
198
+
199
+ // Without spacing => width = 160, height = 180
200
+ // With rowSpacing=15 => total height = 2*90 + 1*15 = 195
201
+ // ColumnSpacing=0 => total width = 160
202
+ Assert . Equal ( new Size ( 160 , 195 ) , target . Bounds . Size ) ;
203
+ }
204
+
205
+ [ Fact ]
206
+ public void Grid_Respects_Both_Row_And_Column_Spacing_For_Fixed_Grid ( )
207
+ {
208
+ // 4 visible children => 2 rows x 2 columns, each child is 50x70 or 80x90
209
+ // We'll fix the Grid to 2x2 so the largest child dictates the cell size: 80x90
210
+ // RowSpacing=10, ColumnSpacing=5
211
+ var target = new UniformGrid
212
+ {
213
+ Rows = 2 ,
214
+ Columns = 2 ,
215
+ RowSpacing = 10 ,
216
+ ColumnSpacing = 5 ,
217
+ Children =
218
+ {
219
+ new Border { Width = 50 , Height = 70 } ,
220
+ new Border { Width = 30 , Height = 50 } ,
221
+ new Border { Width = 80 , Height = 90 } ,
222
+ new Border { Width = 20 , Height = 30 } ,
223
+ }
224
+ } ;
225
+
226
+ target . Measure ( Size . Infinity ) ;
227
+ target . Arrange ( new Rect ( target . DesiredSize ) ) ;
228
+
229
+ // Each cell = 80 x 90
230
+ // Final width = (2 * 80) + (1 * 5) = 160 + 5 = 165
231
+ // Final height = (2 * 90) + (1 * 10) = 180 + 10 = 190
232
+ Assert . Equal ( new Size ( 165 , 190 ) , target . Bounds . Size ) ;
233
+ }
234
+
235
+ [ Fact ]
236
+ public void Grid_Respects_Spacing_When_Invisible_Child_Exists ( )
237
+ {
238
+ // 3 *visible* children => auto => 2x2 grid
239
+ // Largest child is 80 x 90.
240
+ // Add spacing so we can confirm it doesn't add extra columns/rows for invisible child.
241
+ var target = new UniformGrid
242
+ {
243
+ RowSpacing = 5 ,
244
+ ColumnSpacing = 5 ,
245
+ Children =
246
+ {
247
+ new Border { Width = 50 , Height = 70 } ,
248
+ new Border { Width = 80 , Height = 90 , IsVisible = false } ,
249
+ new Border { Width = 30 , Height = 50 } ,
250
+ new Border { Width = 40 , Height = 60 }
251
+ }
252
+ } ;
253
+
254
+ // Visible children: 3 => auto => sqrt(3) => 2x2
255
+ // Largest visible child is 50x70 or 30x50 or 40x60 => the biggest is 50x70
256
+ // Actually, let's ensure we have a child bigger than that:
257
+ // (So let's modify the 40x60 to something bigger than 50x70, e.g. 80x90 for clarity)
258
+ // We'll do that in the collection above if needed, but let's keep as is for example.
259
+
260
+ target . Measure ( Size . Infinity ) ;
261
+ target . Arrange ( new Rect ( target . DesiredSize ) ) ;
262
+
263
+ // The largest visible child is 50x70. So each cell is 50x70.
264
+ // For a 2x2 grid with 3 visible children:
265
+ // - total width = (2 * 50) + (1 * 5) = 100 + 5 = 105
266
+ // - total height = (2 * 70) + (1 * 5) = 140 + 5 = 145
267
+ Assert . Equal ( new Size ( 105 , 145 ) , target . Bounds . Size ) ;
268
+ }
269
+
270
+ /// <summary>
271
+ /// Exposes MeasureOverride for testing inherited classes
272
+ /// </summary>
273
+ public class UniformGridExposeMeasureOverride : UniformGrid
274
+ {
275
+ public new Size MeasureOverride ( Size availableSize )
276
+ {
277
+ return base . MeasureOverride ( availableSize ) ;
278
+ }
279
+ }
280
+
281
+ [ Fact ]
282
+ public void Measure_WithRowsAndColumnsZeroAndNonZeroSpacing_ProducesZeroDesiredSize ( )
283
+ {
284
+ // MeasureOverride() is called by Layoutable.MeasureCore() and it ensures that
285
+ // the desired size is never negative. but in case of inherited classes MeasureOverride() may return negative values.
286
+ var target = new UniformGridExposeMeasureOverride
287
+ {
288
+ Rows = 0 ,
289
+ Columns = 0 ,
290
+ RowSpacing = 10 ,
291
+ ColumnSpacing = 20
292
+ } ;
293
+
294
+ var availableSize = new Size ( 100 , 100 ) ;
295
+
296
+ var desiredSize = target . MeasureOverride ( availableSize ) ;
297
+
298
+ // Fail case:
299
+ // Because _rows and _columns are 0, the calculation becomes:
300
+ // totalWidth = maxWidth * 0 + ColumnSpacing * (0 - 1) = -ColumnSpacing
301
+ // totalHeight = maxHeight * 0 + RowSpacing * (0 - 1) = -RowSpacing
302
+ // Expected: (0, 0)
303
+ Assert . Equal ( 0 , desiredSize . Width ) ;
304
+ Assert . Equal ( 0 , desiredSize . Height ) ;
305
+
142
306
}
143
307
}
144
308
}
0 commit comments