GridSplitterTests.cs 8.5 KB

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