GridSplitterTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.Equal(GridResizeDirection.Rows, splitter.GetEffectiveResizeDirection());
  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.Equal(GridResizeDirection.Columns, splitter.GetEffectiveResizeDirection());
  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.Equal(GridResizeDirection.Columns, splitter.GetEffectiveResizeDirection());
  75. }
  76. [Fact]
  77. public void In_First_Position_Doesnt_Throw_Exception()
  78. {
  79. GridSplitter splitter;
  80. var grid = new Grid
  81. {
  82. ColumnDefinitions = new ColumnDefinitions("Auto,*,*"),
  83. RowDefinitions = new RowDefinitions("*,*"),
  84. Children =
  85. {
  86. (splitter = new GridSplitter { [Grid.ColumnProperty] = 0 }),
  87. new Border { [Grid.ColumnProperty] = 1 },
  88. new Border { [Grid.ColumnProperty] = 2 },
  89. }
  90. };
  91. var root = new TestRoot { Child = grid };
  92. root.Measure(new Size(100, 300));
  93. root.Arrange(new Rect(0, 0, 100, 300));
  94. splitter.RaiseEvent(
  95. new VectorEventArgs { RoutedEvent = Thumb.DragStartedEvent });
  96. splitter.RaiseEvent(new VectorEventArgs
  97. {
  98. RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(100, 1000)
  99. });
  100. }
  101. [Theory]
  102. [InlineData(false)]
  103. [InlineData(true)]
  104. public void Horizontal_Stays_Within_Constraints(bool showsPreview)
  105. {
  106. var control1 = new Border { [Grid.RowProperty] = 0 };
  107. var splitter = new GridSplitter { [Grid.RowProperty] = 1, ShowsPreview = showsPreview};
  108. var control2 = new Border { [Grid.RowProperty] = 2 };
  109. var rowDefinitions = new RowDefinitions
  110. {
  111. new RowDefinition(1, GridUnitType.Star) { MinHeight = 70, MaxHeight = 110 },
  112. new RowDefinition(GridLength.Auto),
  113. new RowDefinition(1, GridUnitType.Star) { MinHeight = 10, MaxHeight = 140 },
  114. };
  115. var grid = new Grid { RowDefinitions = rowDefinitions, Children = { control1, splitter, control2 } };
  116. var root = new TestRoot
  117. {
  118. Child = new VisualLayerManager
  119. {
  120. Child = grid
  121. }
  122. };
  123. root.Measure(new Size(100, 200));
  124. root.Arrange(new Rect(0, 0, 100, 200));
  125. splitter.RaiseEvent(
  126. new VectorEventArgs { RoutedEvent = Thumb.DragStartedEvent });
  127. splitter.RaiseEvent(new VectorEventArgs
  128. {
  129. RoutedEvent = Thumb.DragDeltaEvent,
  130. Vector = new Vector(0, -100)
  131. });
  132. if (showsPreview)
  133. {
  134. Assert.Equal(rowDefinitions[0].Height, new GridLength(1, GridUnitType.Star));
  135. Assert.Equal(rowDefinitions[2].Height, new GridLength(1, GridUnitType.Star));
  136. }
  137. else
  138. {
  139. Assert.Equal(rowDefinitions[0].Height, new GridLength(70, GridUnitType.Star));
  140. Assert.Equal(rowDefinitions[2].Height, new GridLength(130, GridUnitType.Star));
  141. }
  142. splitter.RaiseEvent(new VectorEventArgs
  143. {
  144. RoutedEvent = Thumb.DragDeltaEvent,
  145. Vector = new Vector(0, 100)
  146. });
  147. if (showsPreview)
  148. {
  149. Assert.Equal(rowDefinitions[0].Height, new GridLength(1, GridUnitType.Star));
  150. Assert.Equal(rowDefinitions[2].Height, new GridLength(1, GridUnitType.Star));
  151. }
  152. else
  153. {
  154. Assert.Equal(rowDefinitions[0].Height, new GridLength(110, GridUnitType.Star));
  155. Assert.Equal(rowDefinitions[2].Height, new GridLength(90, GridUnitType.Star));
  156. }
  157. splitter.RaiseEvent(new VectorEventArgs
  158. {
  159. RoutedEvent = Thumb.DragCompletedEvent
  160. });
  161. Assert.Equal(rowDefinitions[0].Height, new GridLength(110, GridUnitType.Star));
  162. Assert.Equal(rowDefinitions[2].Height, new GridLength(90, GridUnitType.Star));
  163. }
  164. [Theory]
  165. [InlineData(false)]
  166. [InlineData(true)]
  167. public void Vertical_Stays_Within_Constraints(bool showsPreview)
  168. {
  169. var control1 = new Border { [Grid.ColumnProperty] = 0 };
  170. var splitter = new GridSplitter { [Grid.ColumnProperty] = 1, ShowsPreview = showsPreview};
  171. var control2 = new Border { [Grid.ColumnProperty] = 2 };
  172. var columnDefinitions = new ColumnDefinitions
  173. {
  174. new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 10, MaxWidth = 190 },
  175. new ColumnDefinition(GridLength.Auto),
  176. new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 80, MaxWidth = 120 },
  177. };
  178. var grid = new Grid { ColumnDefinitions = columnDefinitions, Children = { control1, splitter, control2 } };
  179. var root = new TestRoot
  180. {
  181. Child = new VisualLayerManager
  182. {
  183. Child = grid
  184. }
  185. };
  186. root.Measure(new Size(200, 100));
  187. root.Arrange(new Rect(0, 0, 200, 100));
  188. splitter.RaiseEvent(
  189. new VectorEventArgs { RoutedEvent = Thumb.DragStartedEvent });
  190. splitter.RaiseEvent(new VectorEventArgs
  191. {
  192. RoutedEvent = Thumb.DragDeltaEvent,
  193. Vector = new Vector(-100, 0)
  194. });
  195. if (showsPreview)
  196. {
  197. Assert.Equal(columnDefinitions[0].Width, new GridLength(1, GridUnitType.Star));
  198. Assert.Equal(columnDefinitions[2].Width, new GridLength(1, GridUnitType.Star));
  199. }
  200. else
  201. {
  202. Assert.Equal(columnDefinitions[0].Width, new GridLength(80, GridUnitType.Star));
  203. Assert.Equal(columnDefinitions[2].Width, new GridLength(120, GridUnitType.Star));
  204. }
  205. splitter.RaiseEvent(new VectorEventArgs
  206. {
  207. RoutedEvent = Thumb.DragDeltaEvent,
  208. Vector = new Vector(100, 0)
  209. });
  210. if (showsPreview)
  211. {
  212. Assert.Equal(columnDefinitions[0].Width, new GridLength(1, GridUnitType.Star));
  213. Assert.Equal(columnDefinitions[2].Width, new GridLength(1, GridUnitType.Star));
  214. }
  215. else
  216. {
  217. Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star));
  218. Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star));
  219. }
  220. splitter.RaiseEvent(new VectorEventArgs
  221. {
  222. RoutedEvent = Thumb.DragCompletedEvent
  223. });
  224. Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star));
  225. Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star));
  226. }
  227. [Theory]
  228. [InlineData(Key.Up, 90, 110)]
  229. [InlineData(Key.Down, 110, 90)]
  230. public void Vertical_Keyboard_Input_Can_Move_Splitter(Key key, double expectedHeightFirst, double expectedHeightSecond)
  231. {
  232. var control1 = new Border { [Grid.RowProperty] = 0 };
  233. var splitter = new GridSplitter { [Grid.RowProperty] = 1, KeyboardIncrement = 10d };
  234. var control2 = new Border { [Grid.RowProperty] = 2 };
  235. var rowDefinitions = new RowDefinitions
  236. {
  237. new RowDefinition(1, GridUnitType.Star),
  238. new RowDefinition(GridLength.Auto),
  239. new RowDefinition(1, GridUnitType.Star)
  240. };
  241. var grid = new Grid { RowDefinitions = rowDefinitions, Children = { control1, splitter, control2 } };
  242. var root = new TestRoot
  243. {
  244. Child = grid
  245. };
  246. root.Measure(new Size(200, 200));
  247. root.Arrange(new Rect(0, 0, 200, 200));
  248. splitter.RaiseEvent(new KeyEventArgs
  249. {
  250. RoutedEvent = InputElement.KeyDownEvent,
  251. Key = key
  252. });
  253. Assert.Equal(rowDefinitions[0].Height, new GridLength(expectedHeightFirst, GridUnitType.Star));
  254. Assert.Equal(rowDefinitions[2].Height, new GridLength(expectedHeightSecond, GridUnitType.Star));
  255. }
  256. [Theory]
  257. [InlineData(Key.Left, 90, 110)]
  258. [InlineData(Key.Right, 110, 90)]
  259. public void Horizontal_Keyboard_Input_Can_Move_Splitter(Key key, double expectedWidthFirst, double expectedWidthSecond)
  260. {
  261. var control1 = new Border { [Grid.ColumnProperty] = 0 };
  262. var splitter = new GridSplitter { [Grid.ColumnProperty] = 1, KeyboardIncrement = 10d };
  263. var control2 = new Border { [Grid.ColumnProperty] = 2 };
  264. var columnDefinitions = new ColumnDefinitions
  265. {
  266. new ColumnDefinition(1, GridUnitType.Star),
  267. new ColumnDefinition(GridLength.Auto),
  268. new ColumnDefinition(1, GridUnitType.Star)
  269. };
  270. var grid = new Grid { ColumnDefinitions = columnDefinitions, Children = { control1, splitter, control2 } };
  271. var root = new TestRoot
  272. {
  273. Child = grid
  274. };
  275. root.Measure(new Size(200, 200));
  276. root.Arrange(new Rect(0, 0, 200, 200));
  277. splitter.RaiseEvent(new KeyEventArgs
  278. {
  279. RoutedEvent = InputElement.KeyDownEvent,
  280. Key = key
  281. });
  282. Assert.Equal(columnDefinitions[0].Width, new GridLength(expectedWidthFirst, GridUnitType.Star));
  283. Assert.Equal(columnDefinitions[2].Width, new GridLength(expectedWidthSecond, GridUnitType.Star));
  284. }
  285. [Fact]
  286. public void Pressing_Escape_Key_Cancels_Resizing()
  287. {
  288. var control1 = new Border { [Grid.ColumnProperty] = 0 };
  289. var splitter = new GridSplitter { [Grid.ColumnProperty] = 1, KeyboardIncrement = 10d };
  290. var control2 = new Border { [Grid.ColumnProperty] = 2 };
  291. var columnDefinitions = new ColumnDefinitions
  292. {
  293. new ColumnDefinition(1, GridUnitType.Star),
  294. new ColumnDefinition(GridLength.Auto),
  295. new ColumnDefinition(1, GridUnitType.Star)
  296. };
  297. var grid = new Grid { ColumnDefinitions = columnDefinitions, Children = { control1, splitter, control2 } };
  298. var root = new TestRoot
  299. {
  300. Child = grid
  301. };
  302. root.Measure(new Size(200, 200));
  303. root.Arrange(new Rect(0, 0, 200, 200));
  304. splitter.RaiseEvent(
  305. new VectorEventArgs { RoutedEvent = Thumb.DragStartedEvent });
  306. splitter.RaiseEvent(new VectorEventArgs
  307. {
  308. RoutedEvent = Thumb.DragDeltaEvent,
  309. Vector = new Vector(-100, 0)
  310. });
  311. Assert.Equal(columnDefinitions[0].Width, new GridLength(0, GridUnitType.Star));
  312. Assert.Equal(columnDefinitions[2].Width, new GridLength(200, GridUnitType.Star));
  313. splitter.RaiseEvent(new KeyEventArgs
  314. {
  315. RoutedEvent = InputElement.KeyDownEvent,
  316. Key = Key.Escape
  317. });
  318. Assert.Equal(columnDefinitions[0].Width, new GridLength(1, GridUnitType.Star));
  319. Assert.Equal(columnDefinitions[2].Width, new GridLength(1, GridUnitType.Star));
  320. }
  321. }
  322. }