GridSplitterTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Avalonia.Controls.Primitives;
  2. using Avalonia.Input;
  3. using Avalonia.Platform;
  4. using Avalonia.UnitTests;
  5. using Moq;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class GridSplitterTests
  10. {
  11. public GridSplitterTests()
  12. {
  13. var cursorFactoryImpl = new Mock<IStandardCursorFactory>();
  14. AvaloniaLocator.CurrentMutable.Bind<IStandardCursorFactory>().ToConstant(cursorFactoryImpl.Object);
  15. }
  16. [Fact]
  17. public void Detects_Horizontal_Orientation()
  18. {
  19. var grid = new Grid()
  20. {
  21. RowDefinitions = new RowDefinitions("*,Auto,*"),
  22. ColumnDefinitions = new ColumnDefinitions("*,*"),
  23. Children =
  24. {
  25. new Border { [Grid.RowProperty] = 0 },
  26. new GridSplitter { [Grid.RowProperty] = 1, Name = "splitter" },
  27. new Border { [Grid.RowProperty] = 2 }
  28. }
  29. };
  30. var root = new TestRoot { Child = grid };
  31. root.Measure(new Size(100, 300));
  32. root.Arrange(new Rect(0, 0, 100, 300));
  33. Assert.Contains(grid.FindControl<GridSplitter>("splitter").Classes, ":horizontal".Equals);
  34. }
  35. [Fact]
  36. public void Detects_Vertical_Orientation()
  37. {
  38. var grid = new Grid()
  39. {
  40. ColumnDefinitions = new ColumnDefinitions("*,Auto,*"),
  41. RowDefinitions = new RowDefinitions("*,*"),
  42. Children =
  43. {
  44. new Border { [Grid.ColumnProperty] = 0 },
  45. new GridSplitter { [Grid.ColumnProperty] = 1, Name = "splitter" },
  46. new Border { [Grid.ColumnProperty] = 2 },
  47. }
  48. };
  49. var root = new TestRoot { Child = grid };
  50. root.Measure(new Size(100, 300));
  51. root.Arrange(new Rect(0, 0, 100, 300));
  52. Assert.Contains(grid.FindControl<GridSplitter>("splitter").Classes, ":vertical".Equals);
  53. }
  54. [Fact]
  55. public void Detects_With_Both_Auto()
  56. {
  57. var grid = new Grid()
  58. {
  59. ColumnDefinitions = new ColumnDefinitions("Auto,Auto,Auto"),
  60. RowDefinitions = new RowDefinitions("Auto,Auto"),
  61. Children =
  62. {
  63. new Border { [Grid.ColumnProperty] = 0 },
  64. new GridSplitter { [Grid.ColumnProperty] = 1, Name = "splitter" },
  65. new Border { [Grid.ColumnProperty] = 2 },
  66. }
  67. };
  68. var root = new TestRoot { Child = grid };
  69. root.Measure(new Size(100, 300));
  70. root.Arrange(new Rect(0, 0, 100, 300));
  71. Assert.Contains(grid.FindControl<GridSplitter>("splitter").Classes, ":vertical".Equals);
  72. }
  73. [Fact]
  74. public void Horizontal_Stays_Within_Constraints()
  75. {
  76. var control1 = new Border { [Grid.RowProperty] = 0 };
  77. var splitter = new GridSplitter
  78. {
  79. [Grid.RowProperty] = 1,
  80. };
  81. var control2 = new Border { [Grid.RowProperty] = 2 };
  82. var rowDefinitions = new RowDefinitions()
  83. {
  84. new RowDefinition(1, GridUnitType.Star) { MinHeight = 70, MaxHeight = 110 },
  85. new RowDefinition(GridLength.Auto),
  86. new RowDefinition(1, GridUnitType.Star) { MinHeight = 10, MaxHeight = 140 },
  87. };
  88. var grid = new Grid()
  89. {
  90. RowDefinitions = rowDefinitions,
  91. Children =
  92. {
  93. control1, splitter, control2
  94. }
  95. };
  96. var root = new TestRoot { Child = grid };
  97. root.Measure(new Size(100, 200));
  98. root.Arrange(new Rect(0, 0, 100, 200));
  99. splitter.RaiseEvent(new VectorEventArgs
  100. {
  101. RoutedEvent = Thumb.DragDeltaEvent,
  102. Vector = new Vector(0, -100)
  103. });
  104. Assert.Equal(rowDefinitions[0].Height, new GridLength(70, GridUnitType.Star));
  105. Assert.Equal(rowDefinitions[2].Height, new GridLength(130, GridUnitType.Star));
  106. splitter.RaiseEvent(new VectorEventArgs
  107. {
  108. RoutedEvent = Thumb.DragDeltaEvent,
  109. Vector = new Vector(0, 100)
  110. });
  111. Assert.Equal(rowDefinitions[0].Height, new GridLength(110, GridUnitType.Star));
  112. Assert.Equal(rowDefinitions[2].Height, new GridLength(90, GridUnitType.Star));
  113. }
  114. [Fact]
  115. public void In_First_Position_Doesnt_Throw_Exception()
  116. {
  117. var grid = new Grid()
  118. {
  119. ColumnDefinitions = new ColumnDefinitions("Auto,*,*"),
  120. RowDefinitions = new RowDefinitions("*,*"),
  121. Children =
  122. {
  123. new GridSplitter { [Grid.ColumnProperty] = 0, Name = "splitter" },
  124. new Border { [Grid.ColumnProperty] = 1 },
  125. new Border { [Grid.ColumnProperty] = 2 },
  126. }
  127. };
  128. var root = new TestRoot { Child = grid };
  129. root.Measure(new Size(100, 300));
  130. root.Arrange(new Rect(0, 0, 100, 300));
  131. var splitter = grid.FindControl<GridSplitter>("splitter");
  132. splitter.RaiseEvent(new VectorEventArgs
  133. {
  134. RoutedEvent = Thumb.DragDeltaEvent,
  135. Vector = new Vector(100, 1000)
  136. });
  137. }
  138. [Fact]
  139. public void Vertical_Stays_Within_Constraints()
  140. {
  141. var control1 = new Border { [Grid.ColumnProperty] = 0 };
  142. var splitter = new GridSplitter
  143. {
  144. [Grid.ColumnProperty] = 1,
  145. };
  146. var control2 = new Border { [Grid.ColumnProperty] = 2 };
  147. var columnDefinitions = new ColumnDefinitions()
  148. {
  149. new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 10, MaxWidth = 190 },
  150. new ColumnDefinition(GridLength.Auto),
  151. new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 80, MaxWidth = 120 },
  152. };
  153. var grid = new Grid()
  154. {
  155. ColumnDefinitions = columnDefinitions,
  156. Children =
  157. {
  158. control1, splitter, control2
  159. }
  160. };
  161. var root = new TestRoot { Child = grid };
  162. root.Measure(new Size(200, 100));
  163. root.Arrange(new Rect(0, 0, 200, 100));
  164. splitter.RaiseEvent(new VectorEventArgs
  165. {
  166. RoutedEvent = Thumb.DragDeltaEvent,
  167. Vector = new Vector(-100, 0)
  168. });
  169. Assert.Equal(columnDefinitions[0].Width, new GridLength(80, GridUnitType.Star));
  170. Assert.Equal(columnDefinitions[2].Width, new GridLength(120, GridUnitType.Star));
  171. splitter.RaiseEvent(new VectorEventArgs
  172. {
  173. RoutedEvent = Thumb.DragDeltaEvent,
  174. Vector = new Vector(100, 0)
  175. });
  176. Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star));
  177. Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star));
  178. }
  179. }
  180. }