forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridTests.cs
2518 lines (2037 loc) · 79.5 KB
/
GridTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class GridTests : BaseTestFixture
{
[SetUp]
public override void Setup()
{
base.Setup();
Device.PlatformServices = new MockPlatformServices();
}
[TearDown]
public override void TearDown()
{
base.TearDown();
Device.PlatformServices = null;
}
[Test]
public void ThrowsOnNullAdd()
{
var layout = new Grid();
Assert.Throws<ArgumentNullException>(() => layout.Children.Add(null));
}
[Test]
public void ThrowsOnNullRemove()
{
var layout = new Grid();
Assert.Throws<ArgumentNullException>(() => layout.Children.Remove(null));
}
[Test]
public void StarColumnsHaveEqualWidths()
{
var grid = new Grid
{
VerticalOptions = LayoutOptions.Start,
ColumnSpacing = 12
};
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(5, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var label = new ColumnTestLabel
{
VerticalOptions = LayoutOptions.Start,
LineBreakMode = LineBreakMode.WordWrap,
Text = "There's a 104 days of summer vacation 'til school comes along just to end it. So the annual problem for our generation is finding a good way to spend it."
};
grid.Children.Add(label, 1, 0);
var gridWidth = 411;
grid.Measure(gridWidth, 1000);
var column0Width = grid.ColumnDefinitions[0].ActualWidth;
var column1Width = grid.ColumnDefinitions[1].ActualWidth;
Assert.That(column0Width, Is.EqualTo(column1Width));
Assert.That(column0Width, Is.LessThan(gridWidth));
}
[Test]
public void StarRowsHaveEqualHeights()
{
var grid = new Grid
{
VerticalOptions = LayoutOptions.Start,
RowSpacing = 12
};
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(5, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
var label = new RowTestLabel
{
VerticalOptions = LayoutOptions.Start,
LineBreakMode = LineBreakMode.WordWrap,
Text = "There's a 104 days of summer vacation 'til school comes along just to end it. So the annual problem for our generation is finding a good way to spend it."
};
grid.Children.Add(label, 1, 0);
var gridHeight = 411;
grid.Measure(1000, gridHeight);
var column0Height = grid.RowDefinitions[0].ActualHeight;
var column1Height = grid.RowDefinitions[1].ActualHeight;
Assert.That(column0Height, Is.EqualTo(column1Height));
Assert.That(column0Height, Is.LessThan(gridHeight));
}
[Test]
public void StarRowsDoNotOverlapWithStackLayoutOnTop()
{
SetupStarRowOverlapTest(rowAIsOnTop: false, out VisualElement rowAControl,
out VisualElement rowBControl, out Label lastLabel);
var bottomOfRowB = rowBControl.Y + rowBControl.Height;
var bottomOfLastLabelInRowB = rowBControl.Y + lastLabel.Y + lastLabel.Height;
var topOfRowA = rowAControl.Y;
Assert.That(bottomOfRowB, Is.EqualTo(bottomOfLastLabelInRowB));
Assert.That(topOfRowA, Is.EqualTo(bottomOfRowB),
"B is on top of A, so the top of A should be the bottom of B");
}
[Test]
public void StarRowsDoNotOverlapWithStackLayoutOnBottom()
{
SetupStarRowOverlapTest(rowAIsOnTop: true, out VisualElement rowAControl,
out VisualElement rowBControl, out Label lastLabel);
var topOfRowB = rowBControl.Y;
var bottomOfRowB = rowBControl.Y + rowBControl.Height;
var bottomOfLastLabelInRowB = rowBControl.Y + lastLabel.Y + lastLabel.Height;
var bottomOfRowA = rowAControl.Y + rowAControl.Height;
Assert.That(bottomOfRowB, Is.EqualTo(bottomOfLastLabelInRowB));
Assert.That(topOfRowB, Is.EqualTo(bottomOfRowA),
"A is on top of B, so the top of B should be the bottom of A");
}
[Test]
public void StarColumnsDoNotOverlapWithStackLayoutAtStart()
{
SetupStarColumnOverlapTest(colAIsAtStart: false, out VisualElement colAControl,
out VisualElement colBControl, out Label lastLabel);
var endOfColB = colBControl.X + colBControl.Width;
var endOfLastLabelInColB = colBControl.X + lastLabel.X + lastLabel.Width;
var startOfColA = colAControl.X;
Assert.That(endOfColB, Is.EqualTo(endOfLastLabelInColB));
Assert.That(startOfColA, Is.EqualTo(endOfColB),
"B is before A, so the start of A should be the end of B");
}
[Test]
public void StarColumnsDoNotOverlapWithStackLayoutAtEnd()
{
SetupStarColumnOverlapTest(colAIsAtStart: true, out VisualElement colAControl,
out VisualElement colBControl, out Label lastLabel);
var startOfColB = colBControl.X;
var endOfColB = colBControl.X + colBControl.Width;
var endOfLastLabelInColB = colBControl.X + lastLabel.X + lastLabel.Width;
var endOfColA = colAControl.X + colAControl.Width;
Assert.That(endOfColB, Is.EqualTo(endOfLastLabelInColB));
Assert.That(endOfColA, Is.EqualTo(startOfColB),
"A is before B, so the end of A should be the start of B");
}
void SetupStarRowOverlapTest(bool rowAIsOnTop, out VisualElement rowAControl,
out VisualElement rowBControl, out Label lastLabel)
{
var grid = new Grid
{
RowSpacing = 0,
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition() { Height = GridLength.Star },
new RowDefinition() { Height = GridLength.Star }
}
};
var labelSize = new Size(100, 20);
Label label;
rowAControl = label = new FixedSizeLabel(labelSize) { Text = "Hello" };
StackLayout stackLayout;
rowBControl = stackLayout = new StackLayout() { Spacing = 0, IsPlatformEnabled = true };
for (int n = 0; n < 14; n++)
{
var labelInStack = new FixedSizeLabel(labelSize) { Text = "Hello" };
stackLayout.Children.Add(labelInStack);
}
lastLabel = new FixedSizeLabel(labelSize) { Text = "Hello" };
stackLayout.Children.Add(lastLabel);
grid.Children.Add(stackLayout);
grid.Children.Add(label);
if (rowAIsOnTop)
{
Grid.SetRow(rowAControl, 0);
Grid.SetRow(rowBControl, 1);
}
else
{
Grid.SetRow(rowBControl, 0);
Grid.SetRow(rowAControl, 1);
}
var sizeRequest = grid.Measure(300, double.PositiveInfinity);
grid.Layout(new Rectangle(0, 0, sizeRequest.Request.Width, sizeRequest.Request.Height));
}
void SetupStarColumnOverlapTest(bool colAIsAtStart, out VisualElement colAControl,
out VisualElement colBControl, out Label lastLabel)
{
var grid = new Grid
{
ColumnSpacing = 0,
ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition() { Width = GridLength.Star },
new ColumnDefinition() { Width = GridLength.Star }
}
};
var labelSize = new Size(20, 100);
Label label;
colAControl = label = new FixedSizeLabel(labelSize) { Text = "Hello" };
StackLayout stackLayout;
colBControl = stackLayout = new StackLayout() { Spacing = 0, Orientation = StackOrientation.Horizontal, IsPlatformEnabled = true };
for (int n = 0; n < 14; n++)
{
var labelInStack = new FixedSizeLabel(labelSize) { Text = "Hello" };
stackLayout.Children.Add(labelInStack);
}
lastLabel = new FixedSizeLabel(labelSize) { Text = "Hello" };
stackLayout.Children.Add(lastLabel);
grid.Children.Add(stackLayout);
grid.Children.Add(label);
if (colAIsAtStart)
{
Grid.SetColumn(colAControl, 0);
Grid.SetColumn(colBControl, 1);
}
else
{
Grid.SetColumn(colBControl, 0);
Grid.SetColumn(colAControl, 1);
}
var sizeRequest = grid.Measure(double.PositiveInfinity, 300);
grid.Layout(new Rectangle(0, 0, sizeRequest.Request.Width, sizeRequest.Request.Height));
}
[Test(Description = "Columns with a Star width less than one should not cause the Grid to contract below the target width; see https://github.com/xamarin/Xamarin.Forms/issues/11742")]
public void StarWidthsLessThanOneShouldNotContractGrid()
{
var grid = new Grid
{
VerticalOptions = LayoutOptions.Start,
ColumnSpacing = 12
};
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0.8, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var label0 = new ColumnTestLabel
{
VerticalOptions = LayoutOptions.Start,
LineBreakMode = LineBreakMode.WordWrap,
Text = "This should wrap a bit"
};
var label1 = new ColumnTestLabel
{
VerticalOptions = LayoutOptions.Start,
LineBreakMode = LineBreakMode.WordWrap,
Text = "This ought to fit in the space just fine."
};
grid.Children.Add(label0, 0, 0);
grid.Children.Add(label1, 1, 0);
var gridWidth = 411;
grid.Measure(gridWidth, 1000);
var column0Width = grid.ColumnDefinitions[0].ActualWidth;
var column1Width = grid.ColumnDefinitions[1].ActualWidth;
Assert.That(column0Width, Is.LessThan(column1Width));
// Having a first column which is a fraction of a Star width should not cause the grid
// to contract below the target width
var totalColumnSpacing = (grid.ColumnDefinitions.Count - 1) * grid.ColumnSpacing;
Assert.That(column0Width + column1Width + totalColumnSpacing, Is.GreaterThanOrEqualTo(gridWidth));
}
[Test]
public void ColumnsLessThanOneStarShouldBeTallerThanOneStarColumns()
{
var gridWidth = 400;
var grid1 = new Grid() { ColumnSpacing = 0 };
grid1.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid1.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid1.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var label1 = new ColumnTestLabel
{
Text = "label1"
};
grid1.Children.Add(label1, 0, 0);
grid1.Measure(gridWidth, double.PositiveInfinity);
var grid1Height = grid1.RowDefinitions[0].ActualHeight;
var grid2 = new Grid() { ColumnSpacing = 0 };
// Because the column with the label in it is narrower in this grid (0.5* vs 1*), the label will have
// grow taller to fit the text. So we expect this grid to grow vertically to accommodate it.
grid2.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0.5, GridUnitType.Star) });
grid2.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid2.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var label2 = new ColumnTestLabel
{
Text = "label2"
};
grid2.Children.Add(label2, 0, 0);
grid2.Measure(gridWidth, double.PositiveInfinity);
var grid2Height = grid2.RowDefinitions[0].ActualHeight;
Assert.That(grid2Height, Is.GreaterThan(grid1Height));
}
[Test]
public void ContentHeightSumShouldMatchGridHeightWithAutoRows()
{
var widthConstraint = 400;
var grid1 = new Grid() { ColumnSpacing = 0, Padding = 0, RowSpacing = 0 };
grid1.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid1.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid1.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid1.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var label1 = new ColumnTestLabel { Text = "label1" };
var label2 = new ColumnTestLabel { Text = "label2" };
grid1.Children.Add(label1, 0, 0);
grid1.Children.Add(label2, 0, 1);
var grid1Size = grid1.Measure(widthConstraint, double.PositiveInfinity, MeasureFlags.IncludeMargins);
grid1.Layout(new Rectangle(0, 0, grid1Size.Request.Width, grid1Size.Request.Height));
var grid1Height = grid1.Height;
var expectedHeight = label1.Height + label2.Height + grid1.RowSpacing;
Assert.That(grid1Height, Is.EqualTo(expectedHeight));
}
[Test]
public void UnconstrainedStarRowWithMultipleStarColumnsAllowsTextToGrow()
{
var outerGrid = new Grid() { ColumnSpacing = 0, Padding = 0, RowSpacing = 0, IsPlatformEnabled = true };
outerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
outerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
outerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
var sl = new StackLayout { Padding = 0, IsPlatformEnabled = true };
var label1 = new HeightBasedOnTextLengthLabel
{
Text = "The actual text here doesn't matter, just the length. The length determines the height."
};
var label2 = new ColumnTestLabel { FontSize = 13, LineBreakMode = LineBreakMode.NoWrap, Text = "Description" };
sl.Children.Add(label1);
sl.Children.Add(label2);
var bv = new BoxView { WidthRequest = 50, BackgroundColor = Color.Blue, IsPlatformEnabled = true };
outerGrid.Children.Add(sl);
outerGrid.Children.Add(bv);
Grid.SetColumn(bv, 1);
var width = 400;
var expectedColumnWidth = width / 2;
// Measure and layout the grid
var firstMeasure = outerGrid.Measure(width, double.PositiveInfinity, MeasureFlags.IncludeMargins).Request;
outerGrid.Layout(new Rectangle(0, 0, firstMeasure.Width, firstMeasure.Height));
// Verify that the actual height of the label is what we would expect (within a tolerance)
Assert.That(label1.Height, Is.EqualTo(label1.DesiredHeight(expectedColumnWidth)).Within(2));
var label1OriginalHeight = label1.Height;
// Increase the text
label1.Text += label1.Text;
// And measure/layout again
var secondMeasure = outerGrid.Measure(width, double.PositiveInfinity, MeasureFlags.IncludeMargins).Request;
outerGrid.Layout(new Rectangle(0, 0, secondMeasure.Width, secondMeasure.Height));
// Verify that the actual height of the label is what we would expect (within a tolerance)
Assert.That(label1.Height, Is.EqualTo(label1.DesiredHeight(expectedColumnWidth)).Within(2));
// And that the new height is taller than the old one (since there's more text, and the column width did not change)
Assert.That(label1.Height, Is.GreaterThan(label1OriginalHeight));
}
[Test]
[TestCase(0.1), TestCase(0.2), TestCase(0.3), TestCase(0.4), TestCase(0.5)]
[TestCase(0.6), TestCase(0.7), TestCase(0.8), TestCase(0.9)]
public void AbsoluteColumnShouldNotBloatStarredColumns(double firstColumnWidth)
{
// This is a re-creation of the layout from Issue 12292
// The problem is that a huge label in a star column between a partial star column and
// an absolute column causes the container to get the wrong width during an early measure pass.
// The big label gets the wrong dimensions for measurement, and returns a height that won't actually
// work in the final layout.
var outerGrid = new Grid { ColumnSpacing = 0, Padding = 0, RowSpacing = 0, IsPlatformEnabled = true };
outerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
outerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(firstColumnWidth, GridUnitType.Star) }); // 0.3, but 0.2 works fine
outerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
// This last column is the trouble spot; MeasureAndContractStarredColumns needs to account for this width
// when measuring and distributing the starred column space. Otherwise it reports the wrong width and every
// measure after that is wrong.
outerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 36 });
var innerGrid = new Grid() { ColumnSpacing = 0, Padding = 0, RowSpacing = 0, IsPlatformEnabled = true };
outerGrid.Children.Add(innerGrid);
Grid.SetColumn(innerGrid, 1);
innerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
innerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var hugeLabel = new _12292TestLabel() { };
var tinyLabel = new ColumnTestLabel { Text = "label1" };
innerGrid.Children.Add(hugeLabel);
innerGrid.Children.Add(tinyLabel);
Grid.SetRow(tinyLabel, 1);
var scrollView = new ScrollView() { IsPlatformEnabled = true };
scrollView.Content = outerGrid;
var layoutSize = scrollView.Measure(411, 603, MeasureFlags.IncludeMargins);
// The containing ScrollView should measure a width of about 411; the absolute column at the end of the grid
// shouldn't expand the ScrollView's measure to 447-ish. It's this expansion of the ScrollView that causes
// all subsequent parts of layout to go pear-shaped.
Assert.That(layoutSize.Request.Width, Is.EqualTo(411).Within(2));
}
[Test]
public void ContractionAppliedEquallyOnMultiStarColumns()
{
var grid = new Grid { ColumnSpacing = 0 };
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var smaller = new Size(100, 10);
var larger = new Size(200, 10);
var min = new Size(0, 10);
var label0 = new FixedSizeLabel(min, smaller)
{
Text = "label0"
};
var label1 = new FixedSizeLabel(min, larger)
{
Text = "label1"
};
grid.Children.Add(label0, 0, 0);
grid.Children.Add(label1, 1, 0);
// requested total width is 300, so this will force a contraction to 200
grid.Measure(200, 100);
var column0Width = grid.ColumnDefinitions[0].ActualWidth;
var column1Width = grid.ColumnDefinitions[1].ActualWidth;
Assert.That(column0Width, Is.EqualTo(column1Width / 2));
}
[Test]
public void AllStarColumnsCanOnlyContractToTheLargestMinimum()
{
var grid = new Grid { ColumnSpacing = 0 };
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var leftColumn = new Size(100, 10);
var rightColumn = new Size(100, 10);
var largerMin = new Size(75, 10);
var smallerMin = new Size(50, 10);
var leftLabel = new FixedSizeLabel(largerMin, leftColumn)
{
Text = "label0"
};
var rightLabel = new FixedSizeLabel(smallerMin, rightColumn)
{
Text = "label1"
};
grid.Children.Add(leftLabel, 0, 0);
grid.Children.Add(rightLabel, 1, 0);
// requested total width is 200, so this will force an attemped contraction to 100
grid.Measure(100, 100);
var column0Width = grid.ColumnDefinitions[0].ActualWidth;
var column1Width = grid.ColumnDefinitions[1].ActualWidth;
Assert.That(column0Width, Is.EqualTo(column1Width));
}
[Test]
public void ContractionAppliedEquallyOnMultiStarRows()
{
var grid = new Grid { ColumnSpacing = 0, RowSpacing = 0 };
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(2, GridUnitType.Star) });
var smaller = new Size(100, 100);
var larger = new Size(100, 200);
var min = new Size(100, 0);
var label0 = new FixedSizeLabel(min, smaller)
{
Text = "label0"
};
var label1 = new FixedSizeLabel(min, larger)
{
Text = "label1"
};
grid.Children.Add(label0, 0, 0);
grid.Children.Add(label1, 1, 0);
// requested total height is 300, so this will force a contraction to 200
grid.Measure(200, 200);
var column0Height = grid.RowDefinitions[0].ActualHeight;
var column1Height = grid.RowDefinitions[1].ActualHeight;
Assert.That(column0Height, Is.EqualTo(column1Height / 2));
}
[Test]
public void Issue13127()
{
var scrollView = new ScrollView() { IsPlatformEnabled = true };
var outerGrid = new Grid() { RowSpacing = 0, IsPlatformEnabled = true };
var outerStackLayout = new StackLayout() { Spacing = 0, IsPlatformEnabled = true };
var innerGrid = new Grid() { RowSpacing = 0, IsPlatformEnabled = true };
innerGrid.RowDefinitions = new RowDefinitionCollection() {
new RowDefinition(){ Height = new GridLength(6, GridUnitType.Star)},
new RowDefinition(){ Height = new GridLength(4, GridUnitType.Star)},
};
// Set up the background view, only covers the first row
var background = new BoxView() { IsPlatformEnabled = true };
Grid.SetRowSpan(background, 1);
// Create the foreground, which spans both rows
var foreground = new StackLayout() { Spacing = 0, IsPlatformEnabled = true };
var view1 = new FixedSizeLabel(new Size(200, 50)) { IsPlatformEnabled = true };
var view2 = new FixedSizeLabel(new Size(200, 100)) { IsPlatformEnabled = true };
foreground.Children.Add(view1);
foreground.Children.Add(view2);
Grid.SetRowSpan(foreground, 2);
innerGrid.Children.Add(background);
innerGrid.Children.Add(foreground);
outerStackLayout.Children.Add(innerGrid);
outerGrid.Children.Add(outerStackLayout);
scrollView.Content = outerGrid;
var sizeRequest = scrollView.Measure(500, 1000);
scrollView.Layout(new Rectangle(0, 0, sizeRequest.Request.Width, 1000));
Assert.That(innerGrid.Height, Is.EqualTo(foreground.Height));
Assert.That(background.Height, Is.EqualTo(foreground.Height * 0.6).Within(0.01));
Assert.That(background.Height, Is.EqualTo(165));
}
abstract class TestLabel : Label
{
protected TestLabel()
{
IsPlatformEnabled = true;
}
}
class FixedSizeLabel : TestLabel
{
readonly Size _minimumSize;
readonly Size _requestedSize;
public FixedSizeLabel(Size minimumSize, Size requestedSize)
{
_minimumSize = minimumSize;
_requestedSize = requestedSize;
}
public FixedSizeLabel(Size size) : this(size, size) { }
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
return new SizeRequest(_requestedSize, _minimumSize);
}
}
class _12292TestLabel : Label
{
// We need a label that simulates a fairly specific layout/measure pattern to reproduce
// the circumstances of issue 12292.
int _counter;
public _12292TestLabel()
{
IsPlatformEnabled = true;
Text = "dfghjkl;SCAsdnlv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh dvjhdbcviaijdlvnkhubv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh oebwepuvjlvsdiljhssdlncaCSN SNCAascsbdn sciwohwfwef wbodlaoj bcwhuofw9qph nxaxhsavcgsdcvewp ibewfwfhpo sbcshclcsdc aasusos 9 p;fqpnwuvaycxaslucn;we;oivwemopv mre]bn ;nvw modcefin e['vmdkv wqs vwlj vqur;/ b;bnoerbor blk evoneifb;4rbkk-eq'o ge vlfbmokfen wov mdkqncvw;bnzdFCGHSIAJDOKFBLKVSCBAXVCGFAYGUIOK;LBMDF, NZBCHGFSYUGAEUHRPK;LBMFNVBCFYEWYGUIOPBK; M,MNBCDTFYYU9GIPL;LMVNCX KOEKFULIDJOPKWLFSBVHGIROIQWDMC, ;QLKHFEUHFIJOKPDS;LMNDFVGUHFIDJXHFJOKPEOEJGHRIFJEWODK;LMNBVJHGIOJFEPKWD;LMNDBF VBIWOPKWFKBNRGJOFKPELDWKNVDSHFIOEFIEPKLMDWNDVSFBDIHOFEPDKWL;MNVFBF C,P POIUYFUYGIHOJ;LMFE WGREBFX CUOUIGYUFCHJDKJLFK;EGRHMNBHIOKVEJKVNERNVOEIV OWIEFNIENEKEDLC,WP,EFF dfghjkl;SCAsdnlv dvjhdbcviaijdlvnkhubv oebwepuvjlvsdiljh ssdlncaCSN SNCAascsbdn sciwohwfwef wbodlaoj bcwhuofw9qph nxaxhsavcgsdcvewp ibewfwfhpo sbcshclcsdc aasusos 9 p;fqpnwuvaycxaslucn;we;oivwemopv mre]bn ;nvw modcefin e['vmdkv wqs vwlj vqur;/ b;bnoerbor blk evoneifb;4rbkk-eq'o ge vlfbmokfen wov mdkqncvw;bnzdFCGHSIAJDOKFBLKVSCBAXVCGFAYGUIOK;LBMDF, NZBCHGFSYUGAEUHRPK;LBMFNVBCFYEWYGUIOPBK; M,MNBCDTFYYU9GIPL;LMVNCX KOEKFULIDJOPKWLFSBVHGIROIQWDMC, ;QLKHFEUHFIJOKPDS;LMNDFVGUHFIDJXHFJOKPEOEJGHRIFJEWODK;LMNBVJHGIOJFEPKWD;LMNDBF VBIWOPKWFKBNRGJOFKPELDWKNVDSHFIOEFIEPKLMDWNDVSFBDIHOFEPDKWL;MNVFBF C,P POIUYFUYGIHOJ;LMFE WGREBFX CUOUIGYUFCHJDKJLFK;EGRHMNBHIOKVEJKVNERNVOEIV OWIEFNIENEKEDLC,WP,1234567890234567890";
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
double minWidth = 10.2857142857143;
_counter += 1;
switch (_counter % 6)
{
case 1:
return new SizeRequest(
new Size(375.619047619048, 673.904761904762),
new Size(minWidth, 673.904761904762));
case 2:
return new SizeRequest(
new Size(411.428571428571, 575.619047619048),
new Size(minWidth, 575.619047619048));
case 3:
return new SizeRequest(
new Size(336.380952380952, 755.809523809524),
new Size(minWidth, 755.809523809524));
case 4:
return new SizeRequest(
new Size(375.619047619048, 673.904761904762),
new Size(minWidth, 673.904761904762));
case 5:
return new SizeRequest(
new Size(313.142857142857, 772.190476190476),
new Size(minWidth, 772.190476190476));
case 0:
return new SizeRequest(
new Size(313.142857142857, 772.190476190476),
new Size(minWidth, 772.190476190476));
}
throw new Exception("This shouldn't happen, unless we make measure/layout more or less efficient and " +
"OnMeasure isn't called 6 times during this test.");
}
}
class HeightBasedOnTextLengthLabel : TestLabel
{
public double DesiredHeight(double widthConstraint)
{
return (Text.Length / widthConstraint) * 200;
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var minimumSize = new Size(20, 20);
var height = DesiredHeight(widthConstraint);
return new SizeRequest(new Size(widthConstraint, height), minimumSize);
}
}
class ColumnTestLabel : TestLabel
{
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var minimumSize = new Size(20, 20);
var height = 10000 / widthConstraint;
return new SizeRequest(new Size(widthConstraint, height), minimumSize);
}
}
class RowTestLabel : TestLabel
{
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var minimumSize = new Size(20, 20);
if (double.IsInfinity(heightConstraint))
{
heightConstraint = 1000;
}
var width = 10000 / heightConstraint;
return new SizeRequest(new Size(width, heightConstraint), minimumSize);
}
}
[TestFixture]
public class AddDimension : GridTests
{
[Datapoints]
public static IEnumerable<string> Operations = new[]
{
"HHH",
"HHV",
"HVH",
"HVV",
"VHH",
"VHV",
"VVH",
"VVV",
"RCRHVHVHVHVHV",
"HHHV",
"VVVH",
"RV",
"RH",
"CV",
"CH",
"RVRRV",
"CHCCH",
};
Grid _grid;
int _id = 0;
int _rowDef = 0;
int _colDef = 0;
int _totalWidth = 0;
int _totalHeight = 0;
public void AddHoizontal()
{
// new block gets new id
var id = _id++;
// adding column only increases height if no rows exist
if (_totalHeight == 0)
_totalHeight = 1;
// adding column always increased width by 1
_totalWidth++;
// column spans rows 0 to the last row
var row = 0;
var height = _totalHeight;
// column is always added at the end with a width of 1
var column = _totalWidth - 1;
var width = 1;
_grid.Children.AddHorizontal(
new Label()
{
Text = $"{id}: {column}x{row} {width}x{height}"
}
);
}
public void AddVertical()
{
// new block gets new id
var id = _id++;
// adding row only increases width if no columns exist
if (_totalWidth == 0)
_totalWidth = 1;
// adding row always increased height by 1
_totalHeight++;
// row spans columns 0 to the last column
var column = 0;
var width = _totalWidth;
// row is always added at the end with a height of 1
var row = _totalHeight - 1;
var height = 1;
_grid.Children.AddVertical(
new Label()
{
Text = $"{id}: {column}x{row} {width}x{height}"
}
);
}
public void AddRowDef()
{
_rowDef++;
_totalHeight = Math.Max(_rowDef, _totalHeight);
_grid.RowDefinitions.Add(new RowDefinition());
}
public void AddColumnDef()
{
_colDef++;
_totalWidth = Math.Max(_colDef, _totalWidth);
_grid.ColumnDefinitions.Add(new ColumnDefinition());
}
[TearDown]
public override void TearDown()
{
_grid = null;
_id = 0;
_rowDef = 0;
_colDef = 0;
_totalWidth = 0;
_totalHeight = 0;
}
[Theory]
public void AddDimensionTheory(string operations)
{
_grid = new Grid();
foreach (var op in operations)
{
if (op == 'H')
AddHoizontal();
if (op == 'V')
AddVertical();
if (op == 'R')
AddRowDef();
if (op == 'C')
AddColumnDef();
_grid.Layout(new Rectangle(0, 0, 912, 912));
}
Console.WriteLine($"Operations: {string.Join(string.Empty, operations)}");
var id = 0;
foreach (var view in _grid.Children.Cast<Label>().OrderBy(o => o.Text))
{
var expected = $"{id++}: " +
$"{Grid.GetColumn(view)}x{Grid.GetRow(view)} " +
$"{Grid.GetColumnSpan(view)}x{Grid.GetRowSpan(view)}";
var actual = view.Text;
Console.WriteLine($" {expected} == {actual}");
Assert.That(expected == actual);
}
}
}
[Test]
public void TestBasicVerticalLayout()
{
var layout = new Grid();
var label1 = new Label { IsPlatformEnabled = true };
var label2 = new Label { IsPlatformEnabled = true };
var label3 = new Label { IsPlatformEnabled = true };
layout.Children.AddVertical(new View[] {
label1,
label2,
label3
});
layout.Layout(new Rectangle(0, 0, 912, 912));
Assert.AreEqual(912, layout.Width);
Assert.AreEqual(912, layout.Height);
Assert.AreEqual(new Rectangle(0, 0, 912, 300), label1.Bounds);
Assert.AreEqual(new Rectangle(0, 306, 912, 300), label2.Bounds);
Assert.AreEqual(new Rectangle(0, 612, 912, 300), label3.Bounds);
}
[Test]
public void TestBasicHorizontalLayout()
{
var layout = new Grid();
var label1 = new Label { IsPlatformEnabled = true };
var label2 = new Label { IsPlatformEnabled = true };
var label3 = new Label { IsPlatformEnabled = true };
layout.Children.AddHorizontal(new View[] {
label1,
label2,
label3
});
layout.Layout(new Rectangle(0, 0, 912, 912));
Assert.AreEqual(912, layout.Width);
Assert.AreEqual(912, layout.Height);
Assert.AreEqual(new Rectangle(0, 0, 300, 912), label1.Bounds);
Assert.AreEqual(new Rectangle(306, 0, 300, 912), label2.Bounds);
Assert.AreEqual(new Rectangle(612, 0, 300, 912), label3.Bounds);
}
[Test]
public void TestVerticalExpandStart()
{
var layout = new Grid();
var label1 = new Label { IsPlatformEnabled = true };
var label2 = new Label { IsPlatformEnabled = true };
layout.RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = new GridLength (1, GridUnitType.Star) },
new RowDefinition { Height = GridLength.Auto},
};
layout.Children.Add(label1, 0, 0);
layout.Children.Add(label2, 0, 1);
layout.Layout(new Rectangle(0, 0, 1000, 1000));
Assert.AreEqual(1000, layout.Width);
Assert.AreEqual(1000, layout.Height);
Assert.AreEqual(new Rectangle(0, 0, 1000, 1000 - 20 - layout.RowSpacing), label1.Bounds);
Assert.AreEqual(new Rectangle(0, 1000 - 20, 1000, 20), label2.Bounds);
}
[Test]
public void TestHorizontalExpandStart()
{
var layout = new Grid();
var label1 = new Label { IsPlatformEnabled = true };
var label2 = new Label { IsPlatformEnabled = true };
layout.ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
new ColumnDefinition { Width = GridLength.Auto },
};
layout.Children.Add(label1, 0, 0);
layout.Children.Add(label2, 1, 0);