GridTests.cs 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class GridTests : ScopedTestBase
  10. {
  11. private readonly ITestOutputHelper output;
  12. public GridTests(ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. private static Grid CreateGrid(params (string name, GridLength width)[] columns)
  17. {
  18. return CreateGrid(columns.Select(c =>
  19. (c.name, c.width, ColumnDefinition.MinWidthProperty.GetDefaultValue(typeof(ColumnDefinition)))).ToArray());
  20. }
  21. private static Grid CreateGrid(params (string name, GridLength width, double minWidth)[] columns)
  22. {
  23. return CreateGrid(columns.Select(c =>
  24. (c.name, c.width, c.minWidth, ColumnDefinition.MaxWidthProperty.GetDefaultValue(typeof(ColumnDefinition)))).ToArray());
  25. }
  26. private static Grid CreateGrid(params (string name, GridLength width, double minWidth, double maxWidth)[] columns)
  27. {
  28. var grid = new Grid();
  29. foreach (var k in columns.Select(c => new ColumnDefinition
  30. {
  31. SharedSizeGroup = c.name,
  32. Width = c.width,
  33. MinWidth = c.minWidth,
  34. MaxWidth = c.maxWidth
  35. }))
  36. grid.ColumnDefinitions.Add(k);
  37. return grid;
  38. }
  39. private Control AddSizer(Grid grid, int column, double size = 30)
  40. {
  41. var ctrl = new Control { MinWidth = size, MinHeight = size };
  42. ctrl.SetValue(Grid.ColumnProperty, column);
  43. grid.Children.Add(ctrl);
  44. output.WriteLine($"[AddSizer] Column: {column} MinWidth: {size} MinHeight: {size}");
  45. return ctrl;
  46. }
  47. private void PrintColumnDefinitions(Grid grid)
  48. {
  49. output.WriteLine($"[Grid] ActualWidth: {grid.Bounds.Width} ActualHeight: {grid.Bounds.Width}");
  50. output.WriteLine($"[ColumnDefinitions]");
  51. for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
  52. {
  53. var cd = grid.ColumnDefinitions[i];
  54. output.WriteLine($"[{i}] ActualWidth: {cd.ActualWidth} SharedSizeGroup: {cd.SharedSizeGroup}");
  55. }
  56. }
  57. [Fact]
  58. public void Calculates_Colspan_Correctly()
  59. {
  60. var target = new Grid
  61. {
  62. ColumnDefinitions = new ColumnDefinitions
  63. {
  64. new ColumnDefinition(GridLength.Auto),
  65. new ColumnDefinition(new GridLength(4, GridUnitType.Pixel)),
  66. new ColumnDefinition(GridLength.Auto),
  67. },
  68. RowDefinitions = new RowDefinitions
  69. {
  70. new RowDefinition(GridLength.Auto),
  71. new RowDefinition(GridLength.Auto),
  72. },
  73. Children =
  74. {
  75. new Border
  76. {
  77. Width = 100,
  78. Height = 25,
  79. [Grid.ColumnSpanProperty] = 3,
  80. },
  81. new Border
  82. {
  83. Width = 150,
  84. Height = 25,
  85. [Grid.RowProperty] = 1,
  86. },
  87. new Border
  88. {
  89. Width = 50,
  90. Height = 25,
  91. [Grid.RowProperty] = 1,
  92. [Grid.ColumnProperty] = 2,
  93. }
  94. },
  95. };
  96. target.Measure(Size.Infinity);
  97. // Issue #25 only appears after a second measure
  98. target.InvalidateMeasure();
  99. target.Measure(Size.Infinity);
  100. target.Arrange(new Rect(target.DesiredSize));
  101. Assert.Equal(new Size(204, 50), target.Bounds.Size);
  102. Assert.Equal(150d, target.ColumnDefinitions[0].ActualWidth);
  103. Assert.Equal(4d, target.ColumnDefinitions[1].ActualWidth);
  104. Assert.Equal(50d, target.ColumnDefinitions[2].ActualWidth);
  105. Assert.Equal(new Rect(52, 0, 100, 25), target.Children[0].Bounds);
  106. Assert.Equal(new Rect(0, 25, 150, 25), target.Children[1].Bounds);
  107. Assert.Equal(new Rect(154, 25, 50, 25), target.Children[2].Bounds);
  108. }
  109. [Fact]
  110. public void Layout_EmptyColumnRow_LayoutLikeANormalPanel()
  111. {
  112. // Arrange & Action
  113. var grid = GridMock.New(arrange: new Size(600, 200));
  114. // Assert
  115. GridAssert.ChildrenWidth(grid, 600);
  116. GridAssert.ChildrenHeight(grid, 200);
  117. }
  118. [Fact]
  119. public void Layout_PixelRowColumn_BoundsCorrect()
  120. {
  121. // Arrange & Action
  122. var rowGrid = GridMock.New(new RowDefinitions("100,200,300"));
  123. var columnGrid = GridMock.New(new ColumnDefinitions("50,100,150"));
  124. // Assert
  125. GridAssert.ChildrenHeight(rowGrid, 100, 200, 300);
  126. GridAssert.ChildrenWidth(columnGrid, 50, 100, 150);
  127. }
  128. [Fact]
  129. public void Layout_StarRowColumn_BoundsCorrect()
  130. {
  131. // Arrange & Action
  132. var rowGrid = GridMock.New(new RowDefinitions("1*,2*,3*"), 600);
  133. var columnGrid = GridMock.New(new ColumnDefinitions("*,*,2*"), 600);
  134. // Assert
  135. GridAssert.ChildrenHeight(rowGrid, 100, 200, 300);
  136. GridAssert.ChildrenWidth(columnGrid, 150, 150, 300);
  137. }
  138. [Fact]
  139. public void Layout_MixPixelStarRowColumn_BoundsCorrect()
  140. {
  141. // Arrange & Action
  142. var rowGrid = GridMock.New(new RowDefinitions("1*,2*,150"), 600);
  143. var columnGrid = GridMock.New(new ColumnDefinitions("1*,2*,150"), 600);
  144. // Assert
  145. GridAssert.ChildrenHeight(rowGrid, 150, 300, 150);
  146. GridAssert.ChildrenWidth(columnGrid, 150, 300, 150);
  147. }
  148. [Fact]
  149. public void Layout_StarRowColumnWithMinLength_BoundsCorrect()
  150. {
  151. // Arrange & Action
  152. var rowGrid = GridMock.New(new RowDefinitions
  153. {
  154. new RowDefinition(1, GridUnitType.Star) { MinHeight = 200 },
  155. new RowDefinition(1, GridUnitType.Star),
  156. new RowDefinition(1, GridUnitType.Star),
  157. }, 300);
  158. var columnGrid = GridMock.New(new ColumnDefinitions
  159. {
  160. new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 200 },
  161. new ColumnDefinition(1, GridUnitType.Star),
  162. new ColumnDefinition(1, GridUnitType.Star),
  163. }, 300);
  164. // Assert
  165. GridAssert.ChildrenHeight(rowGrid, 200, 50, 50);
  166. GridAssert.ChildrenWidth(columnGrid, 200, 50, 50);
  167. }
  168. [Fact]
  169. public void Layout_StarRowColumnWithMaxLength_BoundsCorrect()
  170. {
  171. // Arrange & Action
  172. var rowGrid = GridMock.New(new RowDefinitions
  173. {
  174. new RowDefinition(1, GridUnitType.Star) { MaxHeight = 200 },
  175. new RowDefinition(1, GridUnitType.Star),
  176. new RowDefinition(1, GridUnitType.Star),
  177. }, 800);
  178. var columnGrid = GridMock.New(new ColumnDefinitions
  179. {
  180. new ColumnDefinition(1, GridUnitType.Star) { MaxWidth = 200 },
  181. new ColumnDefinition(1, GridUnitType.Star),
  182. new ColumnDefinition(1, GridUnitType.Star),
  183. }, 800);
  184. // Assert
  185. GridAssert.ChildrenHeight(rowGrid, 200, 300, 300);
  186. GridAssert.ChildrenWidth(columnGrid, 200, 300, 300);
  187. }
  188. [Fact]
  189. public void Changing_Child_Column_Invalidates_Measure()
  190. {
  191. Border child;
  192. var target = new Grid
  193. {
  194. ColumnDefinitions = new ColumnDefinitions("*,*"),
  195. Children =
  196. {
  197. (child = new Border
  198. {
  199. [Grid.ColumnProperty] = 0,
  200. }),
  201. }
  202. };
  203. target.Measure(Size.Infinity);
  204. target.Arrange(new Rect(target.DesiredSize));
  205. Assert.True(target.IsMeasureValid);
  206. Grid.SetColumn(child, 1);
  207. Assert.False(target.IsMeasureValid);
  208. }
  209. [Fact]
  210. public void Grid_GridLength_Same_Size_Pixel_0()
  211. {
  212. var grid = CreateGrid(
  213. (null, new GridLength()),
  214. (null, new GridLength()),
  215. (null, new GridLength()),
  216. (null, new GridLength()));
  217. var scope = new Grid();
  218. scope.Children.Add(grid);
  219. var root = new Grid();
  220. root.UseLayoutRounding = false;
  221. root.SetValue(Grid.IsSharedSizeScopeProperty, false);
  222. root.Children.Add(scope);
  223. grid.Measure(new Size(200, 200));
  224. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  225. PrintColumnDefinitions(grid);
  226. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == null), cd => Assert.Equal(0, cd.ActualWidth));
  227. }
  228. [Fact]
  229. public void Grid_GridLength_Same_Size_Pixel_50()
  230. {
  231. var grid = CreateGrid(
  232. (null, new GridLength(50)),
  233. (null, new GridLength(50)),
  234. (null, new GridLength(50)),
  235. (null, new GridLength(50)));
  236. var scope = new Grid();
  237. scope.Children.Add(grid);
  238. var root = new Grid();
  239. root.UseLayoutRounding = false;
  240. root.SetValue(Grid.IsSharedSizeScopeProperty, false);
  241. root.Children.Add(scope);
  242. grid.Measure(new Size(200, 200));
  243. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  244. PrintColumnDefinitions(grid);
  245. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == null), cd => Assert.Equal(50, cd.ActualWidth));
  246. }
  247. [Fact]
  248. public void Grid_GridLength_Same_Size_Auto()
  249. {
  250. var grid = CreateGrid(
  251. (null, new GridLength(0, GridUnitType.Auto)),
  252. (null, new GridLength(0, GridUnitType.Auto)),
  253. (null, new GridLength(0, GridUnitType.Auto)),
  254. (null, new GridLength(0, GridUnitType.Auto)));
  255. var scope = new Grid();
  256. scope.Children.Add(grid);
  257. var root = new Grid();
  258. root.UseLayoutRounding = false;
  259. root.SetValue(Grid.IsSharedSizeScopeProperty, false);
  260. root.Children.Add(scope);
  261. grid.Measure(new Size(200, 200));
  262. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  263. PrintColumnDefinitions(grid);
  264. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == null), cd => Assert.Equal(0, cd.ActualWidth));
  265. }
  266. [Fact]
  267. public void Grid_GridLength_Same_Size_Star()
  268. {
  269. var grid = CreateGrid(
  270. (null, new GridLength(1, GridUnitType.Star)),
  271. (null, new GridLength(1, GridUnitType.Star)),
  272. (null, new GridLength(1, GridUnitType.Star)),
  273. (null, new GridLength(1, GridUnitType.Star)));
  274. var scope = new Grid();
  275. scope.Children.Add(grid);
  276. var root = new Grid();
  277. root.UseLayoutRounding = false;
  278. root.SetValue(Grid.IsSharedSizeScopeProperty, false);
  279. root.Children.Add(scope);
  280. grid.Measure(new Size(200, 200));
  281. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  282. PrintColumnDefinitions(grid);
  283. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == null), cd => Assert.Equal(50, cd.ActualWidth));
  284. }
  285. [Fact]
  286. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0()
  287. {
  288. var grid = CreateGrid(
  289. ("A", new GridLength()),
  290. ("A", new GridLength()),
  291. ("A", new GridLength()),
  292. ("A", new GridLength()));
  293. var scope = new Grid();
  294. scope.Children.Add(grid);
  295. var root = new Grid();
  296. root.UseLayoutRounding = false;
  297. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  298. root.Children.Add(scope);
  299. grid.Measure(new Size(200, 200));
  300. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  301. PrintColumnDefinitions(grid);
  302. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  303. }
  304. [Fact]
  305. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50()
  306. {
  307. var grid = CreateGrid(
  308. ("A", new GridLength(50)),
  309. ("A", new GridLength(50)),
  310. ("A", new GridLength(50)),
  311. ("A", new GridLength(50)));
  312. var scope = new Grid();
  313. scope.Children.Add(grid);
  314. var root = new Grid();
  315. root.UseLayoutRounding = false;
  316. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  317. root.Children.Add(scope);
  318. grid.Measure(new Size(200, 200));
  319. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  320. PrintColumnDefinitions(grid);
  321. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(50, cd.ActualWidth));
  322. }
  323. [Fact]
  324. public void SharedSize_Grid_GridLength_Same_Size_Auto()
  325. {
  326. var grid = CreateGrid(
  327. ("A", new GridLength(0, GridUnitType.Auto)),
  328. ("A", new GridLength(0, GridUnitType.Auto)),
  329. ("A", new GridLength(0, GridUnitType.Auto)),
  330. ("A", new GridLength(0, GridUnitType.Auto)));
  331. var scope = new Grid();
  332. scope.Children.Add(grid);
  333. var root = new Grid();
  334. root.UseLayoutRounding = false;
  335. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  336. root.Children.Add(scope);
  337. grid.Measure(new Size(200, 200));
  338. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  339. PrintColumnDefinitions(grid);
  340. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  341. }
  342. [Fact]
  343. public void SharedSize_Grid_GridLength_Same_Size_Star()
  344. {
  345. var grid = CreateGrid(
  346. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  347. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  348. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  349. ("A", new GridLength(1, GridUnitType.Star))); // Star sizing is treated as Auto, 1 is ignored
  350. var scope = new Grid();
  351. scope.Children.Add(grid);
  352. var root = new Grid();
  353. root.UseLayoutRounding = false;
  354. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  355. root.Children.Add(scope);
  356. grid.Measure(new Size(200, 200));
  357. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  358. PrintColumnDefinitions(grid);
  359. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  360. }
  361. [Fact]
  362. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_First_Column_0()
  363. {
  364. var grid = CreateGrid(
  365. (null, new GridLength()),
  366. ("A", new GridLength()),
  367. ("A", new GridLength()),
  368. ("A", new GridLength()),
  369. ("A", new GridLength()));
  370. var scope = new Grid();
  371. scope.Children.Add(grid);
  372. var root = new Grid();
  373. root.UseLayoutRounding = false;
  374. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  375. root.Children.Add(scope);
  376. grid.Measure(new Size(200, 200));
  377. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  378. PrintColumnDefinitions(grid);
  379. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  380. }
  381. [Fact]
  382. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_First_Column_0()
  383. {
  384. var grid = CreateGrid(
  385. (null, new GridLength()),
  386. ("A", new GridLength(50)),
  387. ("A", new GridLength(50)),
  388. ("A", new GridLength(50)),
  389. ("A", new GridLength(50)));
  390. var scope = new Grid();
  391. scope.Children.Add(grid);
  392. var root = new Grid();
  393. root.UseLayoutRounding = false;
  394. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  395. root.Children.Add(scope);
  396. grid.Measure(new Size(200, 200));
  397. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  398. PrintColumnDefinitions(grid);
  399. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(50, cd.ActualWidth));
  400. }
  401. [Fact]
  402. public void SharedSize_Grid_GridLength_Same_Size_Auto_First_Column_0()
  403. {
  404. var grid = CreateGrid(
  405. (null, new GridLength()),
  406. ("A", new GridLength(0, GridUnitType.Auto)),
  407. ("A", new GridLength(0, GridUnitType.Auto)),
  408. ("A", new GridLength(0, GridUnitType.Auto)),
  409. ("A", new GridLength(0, GridUnitType.Auto)));
  410. var scope = new Grid();
  411. scope.Children.Add(grid);
  412. var root = new Grid();
  413. root.UseLayoutRounding = false;
  414. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  415. root.Children.Add(scope);
  416. grid.Measure(new Size(200, 200));
  417. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  418. PrintColumnDefinitions(grid);
  419. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  420. }
  421. [Fact]
  422. public void SharedSize_Grid_GridLength_Same_Size_Star_First_Column_0()
  423. {
  424. var grid = CreateGrid(
  425. (null, new GridLength()),
  426. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  427. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  428. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  429. ("A", new GridLength(1, GridUnitType.Star))); // Star sizing is treated as Auto, 1 is ignored
  430. var scope = new Grid();
  431. scope.Children.Add(grid);
  432. var root = new Grid();
  433. root.UseLayoutRounding = false;
  434. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  435. root.Children.Add(scope);
  436. grid.Measure(new Size(200, 200));
  437. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  438. PrintColumnDefinitions(grid);
  439. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  440. }
  441. [Fact]
  442. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_Last_Column_0()
  443. {
  444. var grid = CreateGrid(
  445. ("A", new GridLength()),
  446. ("A", new GridLength()),
  447. ("A", new GridLength()),
  448. ("A", new GridLength()),
  449. (null, new GridLength()));
  450. var scope = new Grid();
  451. scope.Children.Add(grid);
  452. var root = new Grid();
  453. root.UseLayoutRounding = false;
  454. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  455. root.Children.Add(scope);
  456. grid.Measure(new Size(200, 200));
  457. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  458. PrintColumnDefinitions(grid);
  459. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  460. }
  461. [Fact]
  462. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_Last_Column_0()
  463. {
  464. var grid = CreateGrid(
  465. ("A", new GridLength(50)),
  466. ("A", new GridLength(50)),
  467. ("A", new GridLength(50)),
  468. ("A", new GridLength(50)),
  469. (null, new GridLength()));
  470. var scope = new Grid();
  471. scope.Children.Add(grid);
  472. var root = new Grid();
  473. root.UseLayoutRounding = false;
  474. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  475. root.Children.Add(scope);
  476. grid.Measure(new Size(200, 200));
  477. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  478. PrintColumnDefinitions(grid);
  479. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(50, cd.ActualWidth));
  480. }
  481. [Fact]
  482. public void SharedSize_Grid_GridLength_Same_Size_Auto_Last_Column_0()
  483. {
  484. var grid = CreateGrid(
  485. ("A", new GridLength(0, GridUnitType.Auto)),
  486. ("A", new GridLength(0, GridUnitType.Auto)),
  487. ("A", new GridLength(0, GridUnitType.Auto)),
  488. ("A", new GridLength(0, GridUnitType.Auto)),
  489. (null, new GridLength()));
  490. var scope = new Grid();
  491. scope.Children.Add(grid);
  492. var root = new Grid();
  493. root.UseLayoutRounding = false;
  494. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  495. root.Children.Add(scope);
  496. grid.Measure(new Size(200, 200));
  497. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  498. PrintColumnDefinitions(grid);
  499. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  500. }
  501. [Fact]
  502. public void SharedSize_Grid_GridLength_Same_Size_Star_Last_Column_0()
  503. {
  504. var grid = CreateGrid(
  505. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  506. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  507. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  508. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  509. (null, new GridLength()));
  510. var scope = new Grid();
  511. scope.Children.Add(grid);
  512. var root = new Grid();
  513. root.UseLayoutRounding = false;
  514. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  515. root.Children.Add(scope);
  516. grid.Measure(new Size(200, 200));
  517. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  518. PrintColumnDefinitions(grid);
  519. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  520. }
  521. [Fact]
  522. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_First_And_Last_Column_0()
  523. {
  524. var grid = CreateGrid(
  525. (null, new GridLength()),
  526. ("A", new GridLength()),
  527. ("A", new GridLength()),
  528. ("A", new GridLength()),
  529. ("A", new GridLength()),
  530. (null, new GridLength()));
  531. var scope = new Grid();
  532. scope.Children.Add(grid);
  533. var root = new Grid();
  534. root.UseLayoutRounding = false;
  535. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  536. root.Children.Add(scope);
  537. grid.Measure(new Size(200, 200));
  538. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  539. PrintColumnDefinitions(grid);
  540. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  541. }
  542. [Fact]
  543. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_First_And_Last_Column_0()
  544. {
  545. var grid = CreateGrid(
  546. (null, new GridLength()),
  547. ("A", new GridLength(50)),
  548. ("A", new GridLength(50)),
  549. ("A", new GridLength(50)),
  550. ("A", new GridLength(50)),
  551. (null, new GridLength()));
  552. var scope = new Grid();
  553. scope.Children.Add(grid);
  554. var root = new Grid();
  555. root.UseLayoutRounding = false;
  556. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  557. root.Children.Add(scope);
  558. grid.Measure(new Size(200, 200));
  559. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  560. PrintColumnDefinitions(grid);
  561. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(50, cd.ActualWidth));
  562. }
  563. [Fact]
  564. public void SharedSize_Grid_GridLength_Same_Size_Auto_First_And_Last_Column_0()
  565. {
  566. var grid = CreateGrid(
  567. (null, new GridLength()),
  568. ("A", new GridLength(0, GridUnitType.Auto)),
  569. ("A", new GridLength(0, GridUnitType.Auto)),
  570. ("A", new GridLength(0, GridUnitType.Auto)),
  571. ("A", new GridLength(0, GridUnitType.Auto)),
  572. (null, new GridLength()));
  573. var scope = new Grid();
  574. scope.Children.Add(grid);
  575. var root = new Grid();
  576. root.UseLayoutRounding = false;
  577. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  578. root.Children.Add(scope);
  579. grid.Measure(new Size(200, 200));
  580. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  581. PrintColumnDefinitions(grid);
  582. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  583. }
  584. [Fact]
  585. public void SharedSize_Grid_GridLength_Same_Size_Star_First_And_Last_Column_0()
  586. {
  587. var grid = CreateGrid(
  588. (null, new GridLength()),
  589. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  590. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  591. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  592. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  593. (null, new GridLength()));
  594. var scope = new Grid();
  595. scope.Children.Add(grid);
  596. var root = new Grid();
  597. root.UseLayoutRounding = false;
  598. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  599. root.Children.Add(scope);
  600. grid.Measure(new Size(200, 200));
  601. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  602. PrintColumnDefinitions(grid);
  603. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  604. }
  605. [Fact]
  606. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_Two_Groups()
  607. {
  608. var grid = CreateGrid(
  609. ("A", new GridLength()),
  610. ("B", new GridLength()),
  611. ("B", new GridLength()),
  612. ("A", new GridLength()));
  613. var scope = new Grid();
  614. scope.Children.Add(grid);
  615. var root = new Grid();
  616. root.UseLayoutRounding = false;
  617. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  618. root.Children.Add(scope);
  619. grid.Measure(new Size(200, 200));
  620. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  621. PrintColumnDefinitions(grid);
  622. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  623. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  624. }
  625. [Fact]
  626. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_Two_Groups()
  627. {
  628. var grid = CreateGrid(
  629. ("A", new GridLength(25)),
  630. ("B", new GridLength(75)),
  631. ("B", new GridLength(75)),
  632. ("A", new GridLength(25)));
  633. var scope = new Grid();
  634. scope.Children.Add(grid);
  635. var root = new Grid();
  636. root.UseLayoutRounding = false;
  637. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  638. root.Children.Add(scope);
  639. grid.Measure(new Size(200, 200));
  640. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  641. PrintColumnDefinitions(grid);
  642. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  643. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  644. }
  645. [Fact]
  646. public void SharedSize_Grid_GridLength_Same_Size_Auto_Two_Groups()
  647. {
  648. var grid = CreateGrid(
  649. ("A", new GridLength(0, GridUnitType.Auto)),
  650. ("B", new GridLength(0, GridUnitType.Auto)),
  651. ("B", new GridLength(0, GridUnitType.Auto)),
  652. ("A", new GridLength(0, GridUnitType.Auto)));
  653. var scope = new Grid();
  654. scope.Children.Add(grid);
  655. var root = new Grid();
  656. root.UseLayoutRounding = false;
  657. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  658. root.Children.Add(scope);
  659. grid.Measure(new Size(200, 200));
  660. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  661. PrintColumnDefinitions(grid);
  662. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  663. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  664. }
  665. [Fact]
  666. public void SharedSize_Grid_GridLength_Same_Size_Star_Two_Groups()
  667. {
  668. var grid = CreateGrid(
  669. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  670. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  671. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  672. ("A", new GridLength(1, GridUnitType.Star))); // Star sizing is treated as Auto, 1 is ignored
  673. var scope = new Grid();
  674. scope.Children.Add(grid);
  675. var root = new Grid();
  676. root.UseLayoutRounding = false;
  677. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  678. root.Children.Add(scope);
  679. grid.Measure(new Size(200, 200));
  680. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  681. PrintColumnDefinitions(grid);
  682. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  683. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  684. }
  685. [Fact]
  686. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_First_Column_0_Two_Groups()
  687. {
  688. var grid = CreateGrid(
  689. (null, new GridLength()),
  690. ("A", new GridLength()),
  691. ("B", new GridLength()),
  692. ("B", new GridLength()),
  693. ("A", new GridLength()));
  694. var scope = new Grid();
  695. scope.Children.Add(grid);
  696. var root = new Grid();
  697. root.UseLayoutRounding = false;
  698. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  699. root.Children.Add(scope);
  700. grid.Measure(new Size(200, 200));
  701. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  702. PrintColumnDefinitions(grid);
  703. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  704. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  705. }
  706. [Fact]
  707. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_First_Column_0_Two_Groups()
  708. {
  709. var grid = CreateGrid(
  710. (null, new GridLength()),
  711. ("A", new GridLength(25)),
  712. ("B", new GridLength(75)),
  713. ("B", new GridLength(75)),
  714. ("A", new GridLength(25)));
  715. var scope = new Grid();
  716. scope.Children.Add(grid);
  717. var root = new Grid();
  718. root.UseLayoutRounding = false;
  719. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  720. root.Children.Add(scope);
  721. grid.Measure(new Size(200, 200));
  722. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  723. PrintColumnDefinitions(grid);
  724. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  725. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  726. }
  727. [Fact]
  728. public void SharedSize_Grid_GridLength_Same_Size_Auto_First_Column_0_Two_Groups()
  729. {
  730. var grid = CreateGrid(
  731. (null, new GridLength()),
  732. ("A", new GridLength(0, GridUnitType.Auto)),
  733. ("B", new GridLength(0, GridUnitType.Auto)),
  734. ("B", new GridLength(0, GridUnitType.Auto)),
  735. ("A", new GridLength(0, GridUnitType.Auto)));
  736. var scope = new Grid();
  737. scope.Children.Add(grid);
  738. var root = new Grid();
  739. root.UseLayoutRounding = false;
  740. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  741. root.Children.Add(scope);
  742. grid.Measure(new Size(200, 200));
  743. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  744. PrintColumnDefinitions(grid);
  745. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  746. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  747. }
  748. [Fact]
  749. public void SharedSize_Grid_GridLength_Same_Size_Star_First_Column_0_Two_Groups()
  750. {
  751. var grid = CreateGrid(
  752. (null, new GridLength()),
  753. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  754. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  755. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  756. ("A", new GridLength(1, GridUnitType.Star))); // Star sizing is treated as Auto, 1 is ignored
  757. var scope = new Grid();
  758. scope.Children.Add(grid);
  759. var root = new Grid();
  760. root.UseLayoutRounding = false;
  761. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  762. root.Children.Add(scope);
  763. grid.Measure(new Size(200, 200));
  764. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  765. PrintColumnDefinitions(grid);
  766. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  767. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  768. }
  769. [Fact]
  770. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_Last_Column_0_Two_Groups()
  771. {
  772. var grid = CreateGrid(
  773. ("A", new GridLength()),
  774. ("B", new GridLength()),
  775. ("B", new GridLength()),
  776. ("A", new GridLength()),
  777. (null, new GridLength()));
  778. var scope = new Grid();
  779. scope.Children.Add(grid);
  780. var root = new Grid();
  781. root.UseLayoutRounding = false;
  782. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  783. root.Children.Add(scope);
  784. grid.Measure(new Size(200, 200));
  785. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  786. PrintColumnDefinitions(grid);
  787. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  788. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  789. }
  790. [Fact]
  791. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_Last_Column_0_Two_Groups()
  792. {
  793. var grid = CreateGrid(
  794. ("A", new GridLength(25)),
  795. ("B", new GridLength(75)),
  796. ("B", new GridLength(75)),
  797. ("A", new GridLength(25)),
  798. (null, new GridLength()));
  799. var scope = new Grid();
  800. scope.Children.Add(grid);
  801. var root = new Grid();
  802. root.UseLayoutRounding = false;
  803. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  804. root.Children.Add(scope);
  805. grid.Measure(new Size(200, 200));
  806. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  807. PrintColumnDefinitions(grid);
  808. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  809. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  810. }
  811. [Fact]
  812. public void SharedSize_Grid_GridLength_Same_Size_Auto_Last_Column_0_Two_Groups()
  813. {
  814. var grid = CreateGrid(
  815. ("A", new GridLength(0, GridUnitType.Auto)),
  816. ("B", new GridLength(0, GridUnitType.Auto)),
  817. ("B", new GridLength(0, GridUnitType.Auto)),
  818. ("A", new GridLength(0, GridUnitType.Auto)),
  819. (null, new GridLength()));
  820. var scope = new Grid();
  821. scope.Children.Add(grid);
  822. var root = new Grid();
  823. root.UseLayoutRounding = false;
  824. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  825. root.Children.Add(scope);
  826. grid.Measure(new Size(200, 200));
  827. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  828. PrintColumnDefinitions(grid);
  829. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  830. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  831. }
  832. [Fact]
  833. public void SharedSize_Grid_GridLength_Same_Size_Star_Last_Column_0_Two_Groups()
  834. {
  835. var grid = CreateGrid(
  836. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  837. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  838. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  839. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  840. (null, new GridLength()));
  841. var scope = new Grid();
  842. scope.Children.Add(grid);
  843. var root = new Grid();
  844. root.UseLayoutRounding = false;
  845. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  846. root.Children.Add(scope);
  847. grid.Measure(new Size(200, 200));
  848. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  849. PrintColumnDefinitions(grid);
  850. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  851. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  852. }
  853. [Fact]
  854. public void SharedSize_Grid_GridLength_Same_Size_Pixel_0_First_And_Last_Column_0_Two_Groups()
  855. {
  856. var grid = CreateGrid(
  857. (null, new GridLength()),
  858. ("A", new GridLength()),
  859. ("B", new GridLength()),
  860. ("B", new GridLength()),
  861. ("A", new GridLength()),
  862. (null, new GridLength()));
  863. var scope = new Grid();
  864. scope.Children.Add(grid);
  865. var root = new Grid();
  866. root.UseLayoutRounding = false;
  867. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  868. root.Children.Add(scope);
  869. grid.Measure(new Size(200, 200));
  870. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  871. PrintColumnDefinitions(grid);
  872. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  873. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  874. }
  875. [Fact]
  876. public void SharedSize_Grid_GridLength_Same_Size_Pixel_50_First_And_Last_Column_0_Two_Groups()
  877. {
  878. var grid = CreateGrid(
  879. (null, new GridLength()),
  880. ("A", new GridLength(25)),
  881. ("B", new GridLength(75)),
  882. ("B", new GridLength(75)),
  883. ("A", new GridLength(25)),
  884. (null, new GridLength()));
  885. var scope = new Grid();
  886. scope.Children.Add(grid);
  887. var root = new Grid();
  888. root.UseLayoutRounding = false;
  889. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  890. root.Children.Add(scope);
  891. grid.Measure(new Size(200, 200));
  892. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  893. PrintColumnDefinitions(grid);
  894. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  895. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  896. }
  897. [Fact]
  898. public void SharedSize_Grid_GridLength_Same_Size_Auto_First_And_Last_Column_0_Two_Groups()
  899. {
  900. var grid = CreateGrid(
  901. (null, new GridLength()),
  902. ("A", new GridLength(0, GridUnitType.Auto)),
  903. ("B", new GridLength(0, GridUnitType.Auto)),
  904. ("B", new GridLength(0, GridUnitType.Auto)),
  905. ("A", new GridLength(0, GridUnitType.Auto)),
  906. (null, new GridLength()));
  907. var scope = new Grid();
  908. scope.Children.Add(grid);
  909. var root = new Grid();
  910. root.UseLayoutRounding = false;
  911. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  912. root.Children.Add(scope);
  913. grid.Measure(new Size(200, 200));
  914. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  915. PrintColumnDefinitions(grid);
  916. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  917. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  918. }
  919. [Fact]
  920. public void SharedSize_Grid_GridLength_Same_Size_Star_First_And_Last_Column_0_Two_Groups()
  921. {
  922. var grid = CreateGrid(
  923. (null, new GridLength()),
  924. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  925. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  926. ("B", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  927. ("A", new GridLength(1, GridUnitType.Star)), // Star sizing is treated as Auto, 1 is ignored
  928. (null, new GridLength()));
  929. var scope = new Grid();
  930. scope.Children.Add(grid);
  931. var root = new Grid();
  932. root.UseLayoutRounding = false;
  933. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  934. root.Children.Add(scope);
  935. grid.Measure(new Size(200, 200));
  936. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  937. PrintColumnDefinitions(grid);
  938. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  939. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(0, cd.ActualWidth));
  940. }
  941. [Fact]
  942. public void Size_Propagation_Is_Constrained_To_Innermost_Scope()
  943. {
  944. var grids = new[] { CreateGrid(("A", new GridLength())), CreateGrid(("A", new GridLength(30)), (null, new GridLength())) };
  945. var innerScope = new Grid();
  946. foreach (var grid in grids)
  947. innerScope.Children.Add(grid);
  948. innerScope.SetValue(Grid.IsSharedSizeScopeProperty, true);
  949. var outerGrid = CreateGrid(("A", new GridLength(0)));
  950. var outerScope = new Grid();
  951. outerScope.Children.Add(outerGrid);
  952. outerScope.Children.Add(innerScope);
  953. var root = new Grid();
  954. root.UseLayoutRounding = false;
  955. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  956. root.Children.Add(outerScope);
  957. root.Measure(new Size(50, 50));
  958. root.Arrange(new Rect(new Point(), new Point(50, 50)));
  959. Assert.Equal(0, outerGrid.ColumnDefinitions[0].ActualWidth);
  960. }
  961. [Fact]
  962. public void Size_Group_Changes_Are_Tracked()
  963. {
  964. var grids = new[] {
  965. CreateGrid((null, new GridLength(0, GridUnitType.Auto)), (null, new GridLength())),
  966. CreateGrid(("A", new GridLength(30)), (null, new GridLength())) };
  967. var scope = new Grid();
  968. foreach (var xgrids in grids)
  969. scope.Children.Add(xgrids);
  970. var root = new Grid();
  971. root.UseLayoutRounding = false;
  972. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  973. root.Children.Add(scope);
  974. root.Measure(new Size(50, 50));
  975. root.Arrange(new Rect(new Point(), new Point(50, 50)));
  976. PrintColumnDefinitions(grids[0]);
  977. Assert.Equal(0, grids[0].ColumnDefinitions[0].ActualWidth);
  978. grids[0].ColumnDefinitions[0].SharedSizeGroup = "A";
  979. root.Measure(new Size(51, 51));
  980. root.Arrange(new Rect(new Point(), new Point(51, 51)));
  981. PrintColumnDefinitions(grids[0]);
  982. Assert.Equal(30, grids[0].ColumnDefinitions[0].ActualWidth);
  983. grids[0].ColumnDefinitions[0].SharedSizeGroup = null;
  984. root.Measure(new Size(52, 52));
  985. root.Arrange(new Rect(new Point(), new Point(52, 52)));
  986. PrintColumnDefinitions(grids[0]);
  987. Assert.Equal(0, grids[0].ColumnDefinitions[0].ActualWidth);
  988. }
  989. [Fact]
  990. public void Size_Group_Definition_Resizes_Are_Tracked()
  991. {
  992. var grids = new[] {
  993. CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())),
  994. CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())) };
  995. var scope = new Grid();
  996. foreach (var xgrids in grids)
  997. scope.Children.Add(xgrids);
  998. var rootGrid = new Grid();
  999. rootGrid.UseLayoutRounding = false;
  1000. rootGrid.SetValue(Grid.IsSharedSizeScopeProperty, true);
  1001. rootGrid.Children.Add(scope);
  1002. var root = new TestRoot(rootGrid)
  1003. {
  1004. Width = 50,
  1005. Height = 50,
  1006. };
  1007. root.LayoutManager.ExecuteInitialLayoutPass();
  1008. PrintColumnDefinitions(grids[0]);
  1009. Assert.Equal(5, grids[0].ColumnDefinitions[0].ActualWidth);
  1010. Assert.Equal(5, grids[1].ColumnDefinitions[0].ActualWidth);
  1011. grids[0].ColumnDefinitions[0].Width = new GridLength(10, GridUnitType.Pixel);
  1012. foreach (Grid grid in grids)
  1013. {
  1014. grid.Measure(new Size(50, 50));
  1015. grid.Arrange(new Rect(new Point(), new Point(50, 50)));
  1016. }
  1017. PrintColumnDefinitions(grids[0]);
  1018. Assert.Equal(10, grids[0].ColumnDefinitions[0].ActualWidth);
  1019. Assert.Equal(10, grids[1].ColumnDefinitions[0].ActualWidth);
  1020. }
  1021. [Fact]
  1022. public void Collection_Changes_Are_Tracked()
  1023. {
  1024. var grid = CreateGrid(
  1025. ("A", new GridLength(20)),
  1026. ("A", new GridLength(30)),
  1027. ("A", new GridLength(40)),
  1028. (null, new GridLength()));
  1029. var scope = new Grid();
  1030. scope.Children.Add(grid);
  1031. var root = new Grid();
  1032. root.UseLayoutRounding = false;
  1033. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  1034. root.Children.Add(scope);
  1035. grid.Measure(new Size(200, 200));
  1036. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1037. PrintColumnDefinitions(grid);
  1038. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(40, cd.ActualWidth));
  1039. grid.ColumnDefinitions.RemoveAt(2);
  1040. // NOTE: THIS IS BROKEN IN WPF
  1041. // grid.Measure(new Size(200, 200));
  1042. // grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1043. // PrintColumnDefinitions(grid);
  1044. // Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(30, cd.ActualWidth));
  1045. grid.ColumnDefinitions.Insert(1, new ColumnDefinition { Width = new GridLength(30), SharedSizeGroup = "A" });
  1046. grid.Measure(new Size(200, 200));
  1047. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1048. PrintColumnDefinitions(grid);
  1049. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(30, cd.ActualWidth));
  1050. grid.ColumnDefinitions[1] = new ColumnDefinition { Width = new GridLength(10), SharedSizeGroup = "A" };
  1051. grid.Measure(new Size(200, 200));
  1052. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1053. PrintColumnDefinitions(grid);
  1054. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(30, cd.ActualWidth));
  1055. grid.ColumnDefinitions[1] = new ColumnDefinition { Width = new GridLength(50), SharedSizeGroup = "A" };
  1056. // NOTE: THIS IS BROKEN IN WPF
  1057. // grid.Measure(new Size(200, 200));
  1058. // grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1059. // PrintColumnDefinitions(grid);
  1060. // Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  1061. }
  1062. [Fact]
  1063. public void Size_Priorities_Are_Maintained()
  1064. {
  1065. var sizers = new List<Control>();
  1066. var grid = CreateGrid(
  1067. ("A", new GridLength(20)),
  1068. ("A", new GridLength(20, GridUnitType.Auto)),
  1069. ("A", new GridLength(1, GridUnitType.Star)),
  1070. ("A", new GridLength(1, GridUnitType.Star)),
  1071. (null, new GridLength()));
  1072. for (int i = 0; i < 3; i++)
  1073. sizers.Add(AddSizer(grid, i, 6 + i * 6));
  1074. var scope = new Grid();
  1075. scope.Children.Add(grid);
  1076. var root = new Grid();
  1077. root.UseLayoutRounding = false;
  1078. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  1079. root.Children.Add(scope);
  1080. grid.Measure(new Size(100, 100));
  1081. grid.Arrange(new Rect(new Point(), new Point(100, 100)));
  1082. PrintColumnDefinitions(grid);
  1083. // all in group are equal to the first fixed column
  1084. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(20, cd.ActualWidth));
  1085. grid.ColumnDefinitions[0].SharedSizeGroup = null;
  1086. grid.Measure(new Size(100, 100));
  1087. grid.Arrange(new Rect(new Point(), new Point(100, 100)));
  1088. PrintColumnDefinitions(grid);
  1089. // NOTE: THIS IS BROKEN IN WPF
  1090. // Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(6 + 2 * 6, cd.ActualWidth));
  1091. // grid.ColumnDefinitions[1].SharedSizeGroup = null;
  1092. // grid.Measure(new Size(100, 100));
  1093. // grid.Arrange(new Rect(new Point(), new Point(100, 100)));
  1094. // PrintColumnDefinitions(grid);
  1095. // NOTE: THIS IS BROKEN IN WPF
  1096. // all in group are equal to width (MinWidth) of the sizer in the second column
  1097. // Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(6 + 1 * 6, cd.ActualWidth));
  1098. // NOTE: THIS IS BROKEN IN WPF
  1099. // grid.ColumnDefinitions[2].SharedSizeGroup = null;
  1100. // NOTE: THIS IS BROKEN IN WPF
  1101. // grid.Measure(new Size(double.PositiveInfinity, 100));
  1102. // grid.Arrange(new Rect(new Point(), new Point(100, 100)));
  1103. // PrintColumnDefinitions(grid);
  1104. // with no constraint star columns default to the MinWidth of the sizer in the column
  1105. // Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(0, cd.ActualWidth));
  1106. }
  1107. [Fact]
  1108. public void ColumnDefinitions_Collection_Is_ReadOnly()
  1109. {
  1110. var grid = CreateGrid(
  1111. ("A", new GridLength(50)),
  1112. ("A", new GridLength(50)),
  1113. ("A", new GridLength(50)),
  1114. ("A", new GridLength(50)));
  1115. var scope = new Grid();
  1116. scope.Children.Add(grid);
  1117. var root = new Grid();
  1118. root.UseLayoutRounding = false;
  1119. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  1120. root.Children.Add(scope);
  1121. grid.Measure(new Size(200, 200));
  1122. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1123. PrintColumnDefinitions(grid);
  1124. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(50, cd.ActualWidth));
  1125. grid.ColumnDefinitions[0] = new ColumnDefinition { Width = new GridLength(25), SharedSizeGroup = "A" };
  1126. grid.ColumnDefinitions[1] = new ColumnDefinition { Width = new GridLength(75), SharedSizeGroup = "B" };
  1127. grid.ColumnDefinitions[2] = new ColumnDefinition { Width = new GridLength(75), SharedSizeGroup = "B" };
  1128. grid.ColumnDefinitions[3] = new ColumnDefinition { Width = new GridLength(25), SharedSizeGroup = "A" };
  1129. grid.Measure(new Size(200, 200));
  1130. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1131. PrintColumnDefinitions(grid);
  1132. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  1133. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  1134. }
  1135. [Fact]
  1136. public void ColumnDefinitions_Collection_Reset_SharedSizeGroup()
  1137. {
  1138. var grid = CreateGrid(
  1139. ("A", new GridLength(25)),
  1140. ("B", new GridLength(75)),
  1141. ("B", new GridLength(75)),
  1142. ("A", new GridLength(25)));
  1143. var scope = new Grid();
  1144. scope.Children.Add(grid);
  1145. var root = new Grid();
  1146. root.UseLayoutRounding = false;
  1147. root.SetValue(Grid.IsSharedSizeScopeProperty, true);
  1148. root.Children.Add(scope);
  1149. grid.Measure(new Size(200, 200));
  1150. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1151. PrintColumnDefinitions(grid);
  1152. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "A"), cd => Assert.Equal(25, cd.ActualWidth));
  1153. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == "B"), cd => Assert.Equal(75, cd.ActualWidth));
  1154. grid.ColumnDefinitions[0].SharedSizeGroup = null;
  1155. grid.ColumnDefinitions[0].Width = new GridLength(50);
  1156. grid.ColumnDefinitions[1].SharedSizeGroup = null;
  1157. grid.ColumnDefinitions[1].Width = new GridLength(50);
  1158. grid.ColumnDefinitions[2].SharedSizeGroup = null;
  1159. grid.ColumnDefinitions[2].Width = new GridLength(50);
  1160. grid.ColumnDefinitions[3].SharedSizeGroup = null;
  1161. grid.ColumnDefinitions[3].Width = new GridLength(50);
  1162. grid.Measure(new Size(200, 200));
  1163. grid.Arrange(new Rect(new Point(), new Point(200, 200)));
  1164. PrintColumnDefinitions(grid);
  1165. Assert.All(grid.ColumnDefinitions.Where(cd => cd.SharedSizeGroup == null), cd => Assert.Equal(50, cd.ActualWidth));
  1166. }
  1167. [Fact]
  1168. public void Correct_Grid_Bounds_When_Child_Control_Has_DesiredSize_Larger_Than_Available_Space()
  1169. {
  1170. // Issue #2746
  1171. var grid = new Grid
  1172. {
  1173. RowDefinitions = RowDefinitions.Parse("Auto"),
  1174. Children =
  1175. {
  1176. new TestControl
  1177. {
  1178. MeasureSize = new Size(150, 150),
  1179. }
  1180. }
  1181. };
  1182. var parent = new Decorator { Child = grid };
  1183. parent.Measure(new Size(100, 100));
  1184. parent.Arrange(new Rect(grid.DesiredSize));
  1185. Assert.Equal(new Size(100, 100), grid.Bounds.Size);
  1186. }
  1187. [Theory]
  1188. [InlineData(true)]
  1189. [InlineData(false)]
  1190. public void Changing_Column_Width_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1191. {
  1192. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1193. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1194. {
  1195. if (setUsingAvaloniaProperty)
  1196. grid.ColumnDefinitions[0][ColumnDefinition.WidthProperty] = new GridLength(5);
  1197. else
  1198. grid.ColumnDefinitions[0].Width = new GridLength(5);
  1199. });
  1200. }
  1201. [Theory]
  1202. [InlineData(true)]
  1203. [InlineData(false)]
  1204. public void Changing_Column_MinWidth_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1205. {
  1206. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1207. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1208. {
  1209. if (setUsingAvaloniaProperty)
  1210. grid.ColumnDefinitions[0][ColumnDefinition.MinWidthProperty] = 5;
  1211. else
  1212. grid.ColumnDefinitions[0].MinWidth = 5;
  1213. });
  1214. }
  1215. [Theory]
  1216. [InlineData(true)]
  1217. [InlineData(false)]
  1218. public void Changing_Column_MaxWidth_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1219. {
  1220. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1221. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1222. {
  1223. if (setUsingAvaloniaProperty)
  1224. grid.ColumnDefinitions[0][ColumnDefinition.MaxWidthProperty] = 5;
  1225. else
  1226. grid.ColumnDefinitions[0].MaxWidth = 5;
  1227. });
  1228. }
  1229. [Theory]
  1230. [InlineData(true)]
  1231. [InlineData(false)]
  1232. public void Changing_Row_Height_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1233. {
  1234. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1235. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1236. {
  1237. if (setUsingAvaloniaProperty)
  1238. grid.RowDefinitions[0][RowDefinition.HeightProperty] = new GridLength(5);
  1239. else
  1240. grid.RowDefinitions[0].Height = new GridLength(5);
  1241. });
  1242. }
  1243. [Theory]
  1244. [InlineData(true)]
  1245. [InlineData(false)]
  1246. public void Changing_Row_MinHeight_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1247. {
  1248. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1249. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1250. {
  1251. if (setUsingAvaloniaProperty)
  1252. grid.RowDefinitions[0][RowDefinition.MinHeightProperty] = 5;
  1253. else
  1254. grid.RowDefinitions[0].MinHeight = 5;
  1255. });
  1256. }
  1257. [Theory]
  1258. [InlineData(true)]
  1259. [InlineData(false)]
  1260. public void Changing_Row_MaxHeight_Should_Invalidate_Grid(bool setUsingAvaloniaProperty)
  1261. {
  1262. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1263. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1264. {
  1265. if (setUsingAvaloniaProperty)
  1266. grid.RowDefinitions[0][RowDefinition.MaxHeightProperty] = 5;
  1267. else
  1268. grid.RowDefinitions[0].MaxHeight = 5;
  1269. });
  1270. }
  1271. [Fact]
  1272. public void Adding_Column_Should_Invalidate_Grid()
  1273. {
  1274. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1275. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1276. {
  1277. grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(5)));
  1278. });
  1279. }
  1280. [Fact]
  1281. public void Adding_Row_Should_Invalidate_Grid()
  1282. {
  1283. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1284. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1285. {
  1286. grid.RowDefinitions.Add(new RowDefinition(new GridLength(5)));
  1287. });
  1288. }
  1289. [Fact]
  1290. public void Replacing_Columns_Should_Invalidate_Grid()
  1291. {
  1292. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1293. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1294. {
  1295. grid.ColumnDefinitions = ColumnDefinitions.Parse("2*,1*");
  1296. });
  1297. }
  1298. [Fact]
  1299. public void Replacing_Rows_Should_Invalidate_Grid()
  1300. {
  1301. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1302. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1303. {
  1304. grid.RowDefinitions = RowDefinitions.Parse("2*,1*");
  1305. });
  1306. }
  1307. [Fact]
  1308. public void Removing_Column_Should_Invalidate_Grid()
  1309. {
  1310. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("1*,1*") };
  1311. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1312. {
  1313. grid.ColumnDefinitions.RemoveAt(0);
  1314. });
  1315. }
  1316. [Fact]
  1317. public void Removing_Row_Should_Invalidate_Grid()
  1318. {
  1319. var grid = new Grid { RowDefinitions = RowDefinitions.Parse("1*,1*") };
  1320. Change_Property_And_Verify_Measure_Requested(grid, () =>
  1321. {
  1322. grid.RowDefinitions.RemoveAt(0);
  1323. });
  1324. }
  1325. [Fact]
  1326. public void Removing_Child_Should_Invalidate_Grid_And_Be_Operational()
  1327. {
  1328. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("*,Auto") };
  1329. grid.Children.Add(new Decorator() { [Grid.ColumnProperty] = 0 });
  1330. grid.Children.Add(new Decorator() { Width = 10, Height = 10, [Grid.ColumnProperty] = 1 });
  1331. var size = new Size(100, 100);
  1332. grid.Measure(size);
  1333. grid.Arrange(new Rect(size));
  1334. Assert.True(grid.IsMeasureValid);
  1335. Assert.True(grid.IsArrangeValid);
  1336. Assert.Equal(90, grid.Children[0].Bounds.Width);
  1337. Assert.Equal(10, grid.Children[1].Bounds.Width);
  1338. grid.Children.RemoveAt(1);
  1339. Assert.False(grid.IsMeasureValid);
  1340. Assert.False(grid.IsArrangeValid);
  1341. grid.Measure(size);
  1342. grid.Arrange(new Rect(size));
  1343. Assert.True(grid.IsMeasureValid);
  1344. Assert.True(grid.IsArrangeValid);
  1345. Assert.Equal(100, grid.Children[0].Bounds.Width);
  1346. }
  1347. [Fact]
  1348. public void Adding_Child_Should_Invalidate_Grid_And_Be_Operational()
  1349. {
  1350. var grid = new Grid { ColumnDefinitions = ColumnDefinitions.Parse("*,Auto") };
  1351. grid.Children.Add(new Decorator() { [Grid.ColumnProperty] = 0 });
  1352. var size = new Size(100, 100);
  1353. grid.Measure(size);
  1354. grid.Arrange(new Rect(size));
  1355. Assert.True(grid.IsMeasureValid);
  1356. Assert.True(grid.IsArrangeValid);
  1357. Assert.Equal(100, grid.Children[0].Bounds.Width);
  1358. grid.Children.Add(new Decorator() { Width = 10, Height = 10, [Grid.ColumnProperty] = 1 });
  1359. Assert.False(grid.IsMeasureValid);
  1360. Assert.False(grid.IsArrangeValid);
  1361. grid.Measure(size);
  1362. grid.Arrange(new Rect(size));
  1363. Assert.True(grid.IsMeasureValid);
  1364. Assert.True(grid.IsArrangeValid);
  1365. Assert.Equal(90, grid.Children[0].Bounds.Width);
  1366. Assert.Equal(10, grid.Children[1].Bounds.Width);
  1367. }
  1368. private static void Change_Property_And_Verify_Measure_Requested(Grid grid, Action change)
  1369. {
  1370. grid.Measure(new Size(100, 100));
  1371. grid.Arrange(new Rect(grid.DesiredSize));
  1372. Assert.True(grid.IsMeasureValid);
  1373. Assert.True(grid.IsArrangeValid);
  1374. change();
  1375. Assert.False(grid.IsMeasureValid);
  1376. Assert.False(grid.IsArrangeValid);
  1377. }
  1378. [Fact]
  1379. public void Should_Grid_Controls_With_Spacing()
  1380. {
  1381. var target = new Grid
  1382. {
  1383. RowSpacing = 10,
  1384. ColumnSpacing = 10,
  1385. RowDefinitions = RowDefinitions.Parse("100,100"),
  1386. ColumnDefinitions = ColumnDefinitions.Parse("100,100"),
  1387. Children =
  1388. {
  1389. new Border(),
  1390. new Border { [Grid.ColumnProperty] = 1 },
  1391. new Border { [Grid.RowProperty] = 1 },
  1392. new Border { [Grid.RowProperty] = 1, [Grid.ColumnProperty] = 1 }
  1393. }
  1394. };
  1395. target.Measure(Size.Infinity);
  1396. target.Arrange(new Rect(target.DesiredSize));
  1397. Assert.Equal(new Rect(0, 0, 210, 210), target.Bounds);
  1398. Assert.Equal(new Rect(0, 0, 100, 100), target.Children[0].Bounds);
  1399. Assert.Equal(new Rect(110, 0, 100, 100), target.Children[1].Bounds);
  1400. Assert.Equal(new Rect(0, 110, 100, 100), target.Children[2].Bounds);
  1401. Assert.Equal(new Rect(110, 110, 100, 100), target.Children[3].Bounds);
  1402. }
  1403. [Fact]
  1404. public void Should_Grid_Controls_With_Spacing_Complicated()
  1405. {
  1406. var target = new Grid
  1407. {
  1408. Width = 200,
  1409. Height = 200,
  1410. RowSpacing = 10,
  1411. ColumnSpacing = 10,
  1412. RowDefinitions = RowDefinitions.Parse("50,*,2*,Auto"),
  1413. ColumnDefinitions = ColumnDefinitions.Parse("50,*,2*,Auto"),
  1414. Children =
  1415. {
  1416. new Border(),
  1417. new Border { [Grid.RowProperty] = 1, [Grid.ColumnProperty] = 1 },
  1418. new Border { [Grid.RowProperty] = 2, [Grid.ColumnProperty] = 2 },
  1419. new Border { [Grid.RowProperty] = 3, [Grid.ColumnProperty] = 3, Width = 30, Height = 30 },
  1420. },
  1421. };
  1422. target.Measure(Size.Infinity);
  1423. target.Arrange(new Rect(target.DesiredSize));
  1424. Assert.Equal(new Rect(0, 0, 200, 200), target.Bounds);
  1425. Assert.Equal(new Rect(0, 0, 50, 50), target.Children[0].Bounds);
  1426. Assert.Equal(new Rect(60, 60, 30, 30), target.Children[1].Bounds);
  1427. Assert.Equal(new Rect(100, 100, 60, 60), target.Children[2].Bounds);
  1428. Assert.Equal(new Rect(170, 170, 30, 30), target.Children[3].Bounds);
  1429. }
  1430. [Fact]
  1431. public void Should_Grid_Controls_With_Spacing_Overflow()
  1432. {
  1433. var target = new Grid
  1434. {
  1435. Width = 100,
  1436. Height = 100,
  1437. ColumnSpacing = 20,
  1438. RowSpacing = 20,
  1439. ColumnDefinitions = ColumnDefinitions.Parse("30,*,*,Auto"),
  1440. RowDefinitions = RowDefinitions.Parse("30,*,*,Auto"),
  1441. Children =
  1442. {
  1443. new Border(),
  1444. new Border { [Grid.RowProperty] = 1, [Grid.ColumnProperty] = 1 },
  1445. new Border { [Grid.RowProperty] = 2, [Grid.ColumnProperty] = 2 },
  1446. new Border { [Grid.RowProperty] = 3, [Grid.ColumnProperty] = 3, Width = 30, Height = 30 },
  1447. },
  1448. };
  1449. target.Measure(Size.Infinity);
  1450. target.Arrange(new Rect(target.DesiredSize));
  1451. Assert.Equal(new Rect(0, 0, 100, 100), target.Bounds);
  1452. Assert.Equal(new Rect(0, 0, 30, 30), target.Children[0].Bounds);
  1453. Assert.Equal(new Rect(50, 50, 0, 0), target.Children[1].Bounds);
  1454. Assert.Equal(new Rect(70, 70, 0, 0), target.Children[2].Bounds);
  1455. Assert.Equal(new Rect(90, 90, 30, 30), target.Children[3].Bounds);
  1456. }
  1457. [Fact]
  1458. public void Should_Grid_Controls_With_Spacing_Overflow2()
  1459. {
  1460. var target = new Grid
  1461. {
  1462. Height = 100,
  1463. ColumnSpacing = 20,
  1464. ColumnDefinitions = ColumnDefinitions.Parse("*,Auto"),
  1465. Children =
  1466. {
  1467. new Border { Width = 60 },
  1468. new Border { [Grid.ColumnProperty] = 1, Width = 60 }
  1469. },
  1470. };
  1471. target.Measure(new Size(100, 100));
  1472. target.Arrange(new Rect(target.DesiredSize));
  1473. Assert.Equal(new Rect(0, 0, 100, 100), target.Bounds);
  1474. Assert.Equal(new Rect(-20, 0, 60, 100), target.Children[0].Bounds);
  1475. Assert.Equal(new Rect(40, 0, 60, 100), target.Children[1].Bounds);
  1476. }
  1477. [Fact]
  1478. public void Grid_Controls_With_Spacing_With_Span()
  1479. {
  1480. var target = new Grid
  1481. {
  1482. ColumnSpacing = 20,
  1483. RowDefinitions = RowDefinitions.Parse("Auto"),
  1484. ColumnDefinitions = ColumnDefinitions.Parse("20,20"),
  1485. Children =
  1486. {
  1487. new Border
  1488. {
  1489. Height = 100,
  1490. [Grid.ColumnSpanProperty] = 2
  1491. }
  1492. },
  1493. };
  1494. target.Measure(new Size(100, 100));
  1495. target.Arrange(new Rect(target.DesiredSize));
  1496. Assert.Equal(new Rect(0, 0, 60, 100), target.Bounds);
  1497. Assert.Equal(new Rect(0, 0, 60, 100), target.Children[0].Bounds);
  1498. }
  1499. [Fact]
  1500. public void Grid_Controls_With_Spacing_With_Span_And_SharedSize()
  1501. {
  1502. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  1503. var grid1 = new Grid()
  1504. {
  1505. [Grid.RowProperty] = 0,
  1506. RowDefinitions = RowDefinitions.Parse("Auto,*,Auto,Auto"),
  1507. ColumnDefinitions =
  1508. [
  1509. new ColumnDefinition(GridLength.Auto),
  1510. new ColumnDefinition(GridLength.Star),
  1511. new ColumnDefinition(GridLength.Auto),
  1512. new ColumnDefinition(GridLength.Auto)
  1513. {
  1514. SharedSizeGroup = "C3"
  1515. }
  1516. ],
  1517. RowSpacing = 10,
  1518. ColumnSpacing = 10,
  1519. Children =
  1520. {
  1521. new ScrollViewer()
  1522. {
  1523. [Grid.RowProperty] = 0,
  1524. [Grid.ColumnProperty] = 0,
  1525. [Grid.RowSpanProperty] = 3,
  1526. [Grid.ColumnSpanProperty] = 3,
  1527. Content = new TextBlock()
  1528. {
  1529. Text = @"0: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1530. 1: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1531. 2: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1532. 3: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1533. 4: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1534. 5: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1535. 6: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1536. 7: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1537. 8: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890
  1538. 9: 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890"
  1539. }
  1540. },
  1541. new Button()
  1542. {
  1543. [Grid.RowProperty] = 3,
  1544. [Grid.ColumnProperty] = 0,
  1545. Width = 100,
  1546. Height = 40
  1547. },
  1548. new Button()
  1549. {
  1550. [Grid.RowProperty] = 3,
  1551. [Grid.ColumnProperty] = 2,
  1552. Width = 100,
  1553. Height = 40
  1554. },
  1555. new Button()
  1556. {
  1557. [Grid.RowProperty] = 0,
  1558. [Grid.ColumnProperty] = 3,
  1559. Width = 100,
  1560. Height = 40
  1561. },
  1562. new Button()
  1563. {
  1564. [Grid.RowProperty] = 2,
  1565. [Grid.ColumnProperty] = 3,
  1566. Width = 100,
  1567. Height = 40
  1568. }
  1569. }
  1570. };
  1571. var grid2 = new Grid()
  1572. {
  1573. [Grid.RowProperty] = 1,
  1574. ColumnDefinitions =
  1575. [
  1576. new ColumnDefinition(GridLength.Star),
  1577. new ColumnDefinition(GridLength.Auto)
  1578. {
  1579. SharedSizeGroup = "C3"
  1580. }
  1581. ],
  1582. Children =
  1583. {
  1584. new TextBlock()
  1585. {
  1586. [Grid.ColumnProperty] = 1,
  1587. Height = 20,
  1588. Text="1234567890"
  1589. }
  1590. }
  1591. };
  1592. var root = new Grid()
  1593. {
  1594. [Grid.IsSharedSizeScopeProperty] = true,
  1595. RowDefinitions = RowDefinitions.Parse("*,Auto"),
  1596. RowSpacing = 10,
  1597. Margin = new Thickness(10)
  1598. };
  1599. root.Children.Add(grid1);
  1600. root.Children.Add(grid2);
  1601. root.Measure(new Size(550, 240));
  1602. root.Arrange(new Rect(new Point(), new Point(550, 240)));
  1603. Assert.Equal(new Rect(0, 0, 420, 140), grid1.Children[0].Bounds);
  1604. Assert.Equal(grid1.Children[4].Bounds.Left, grid2.Children[0].Bounds.Left);
  1605. Assert.Equal(grid1.Children[4].Bounds.Width, grid2.Children[0].Bounds.Width);
  1606. }
  1607. [Fact]
  1608. public void Grid_With_ColumnSpacing_And_ColumnDefinitions_Unset()
  1609. {
  1610. var target = new Grid
  1611. {
  1612. Height = 300,
  1613. Width = 100,
  1614. ColumnSpacing = 10,
  1615. RowDefinitions = RowDefinitions.Parse("Auto,*"),//Set RowDefinitions to avoid
  1616. Children =
  1617. {
  1618. new Border
  1619. {
  1620. [Grid.RowProperty] = 0,
  1621. Height = 80,
  1622. Margin = new Thickness(10),
  1623. },
  1624. new Border
  1625. {
  1626. [Grid.RowProperty] = 1,
  1627. Margin = new Thickness(20),
  1628. },
  1629. },
  1630. };
  1631. target.Measure(new Size(100, 300));
  1632. target.Arrange(new Rect(target.DesiredSize));
  1633. Assert.Equal(new Rect(10, 10, 80, 80), target.Children[0].Bounds);
  1634. Assert.Equal(new Rect(20, 120, 60, 160),target.Children[1].Bounds);
  1635. }
  1636. private class TestControl : Control
  1637. {
  1638. public Size MeasureSize { get; set; }
  1639. protected override Size MeasureOverride(Size availableSize) => MeasureSize;
  1640. }
  1641. }
  1642. }