SplitViewTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using Avalonia.Animation;
  2. using Avalonia.Input;
  3. using Avalonia.UnitTests;
  4. using Moq;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests
  7. {
  8. public class SplitViewTests : ScopedTestBase
  9. {
  10. [Fact]
  11. public void SplitView_PaneOpening_Should_Fire_Before_PaneOpened()
  12. {
  13. var splitView = new SplitView();
  14. bool handledOpening = false;
  15. splitView.PaneOpening += (x, e) =>
  16. {
  17. handledOpening = true;
  18. };
  19. splitView.PaneOpened += (x, e) =>
  20. {
  21. Assert.True(handledOpening);
  22. };
  23. splitView.IsPaneOpen = true;
  24. }
  25. [Fact]
  26. public void SplitView_PaneClosing_Should_Fire_Before_PaneClosed()
  27. {
  28. var splitView = new SplitView();
  29. splitView.IsPaneOpen = true;
  30. bool handledClosing = false;
  31. splitView.PaneClosing += (x, e) =>
  32. {
  33. handledClosing = true;
  34. };
  35. splitView.PaneClosed += (x, e) =>
  36. {
  37. Assert.True(handledClosing);
  38. };
  39. splitView.IsPaneOpen = false;
  40. }
  41. [Fact]
  42. public void SplitView_Cancel_Close_Should_Prevent_Pane_From_Closing()
  43. {
  44. var splitView = new SplitView();
  45. splitView.IsPaneOpen = true;
  46. splitView.PaneClosing += (x, e) =>
  47. {
  48. e.Cancel = true;
  49. };
  50. splitView.IsPaneOpen = false;
  51. Assert.True(splitView.IsPaneOpen);
  52. }
  53. [Fact]
  54. public void SplitView_TemplateSettings_Are_Correct_For_Display_Modes()
  55. {
  56. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  57. .With(globalClock: new MockGlobalClock()));
  58. var wnd = new Window
  59. {
  60. Width = 1280,
  61. Height = 720
  62. };
  63. var splitView = new SplitView();
  64. wnd.Content = splitView;
  65. wnd.Show();
  66. var zeroGridLength = new GridLength(0);
  67. var compactLength = splitView.CompactPaneLength;
  68. var compactGridLength = new GridLength(compactLength);
  69. // Overlay is default DisplayMode
  70. Assert.Equal(0, splitView.TemplateSettings.ClosedPaneWidth);
  71. Assert.Equal(zeroGridLength, splitView.TemplateSettings.PaneColumnGridLength);
  72. splitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
  73. Assert.Equal(compactLength, splitView.TemplateSettings.ClosedPaneWidth);
  74. Assert.Equal(compactGridLength, splitView.TemplateSettings.PaneColumnGridLength);
  75. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  76. Assert.Equal(0, splitView.TemplateSettings.ClosedPaneWidth);
  77. Assert.Equal(GridLength.Auto, splitView.TemplateSettings.PaneColumnGridLength);
  78. splitView.DisplayMode = SplitViewDisplayMode.CompactInline;
  79. Assert.Equal(compactLength, splitView.TemplateSettings.ClosedPaneWidth);
  80. Assert.Equal(GridLength.Auto, splitView.TemplateSettings.PaneColumnGridLength);
  81. }
  82. [Fact]
  83. public void SplitView_TemplateSettings_Update_With_CompactPaneLength()
  84. {
  85. var splitView = new SplitView();
  86. // CompactInline:
  87. // - ClosedPaneWidth = CompactPaneLength
  88. // - PaneColumnGridLength = Auto
  89. splitView.DisplayMode = SplitViewDisplayMode.CompactInline;
  90. var compactLength = splitView.CompactPaneLength;
  91. Assert.Equal(GridLength.Auto, splitView.TemplateSettings.PaneColumnGridLength);
  92. Assert.Equal(compactLength, splitView.TemplateSettings.ClosedPaneWidth);
  93. splitView.CompactPaneLength = 100;
  94. Assert.Equal(GridLength.Auto, splitView.TemplateSettings.PaneColumnGridLength);
  95. Assert.Equal(100, splitView.TemplateSettings.ClosedPaneWidth);
  96. // CompactOverlay:
  97. // - ClosedPaneWidth = CompactPaneLength
  98. // - PaneColumnGridLength = GridLength { CompactPaneLength, Pixel }
  99. splitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
  100. splitView.CompactPaneLength = 50;
  101. Assert.Equal(new GridLength(50), splitView.TemplateSettings.PaneColumnGridLength);
  102. Assert.Equal(50, splitView.TemplateSettings.ClosedPaneWidth);
  103. // Value shouldn't change for these - changing the display mode will update
  104. // the template settings with the right value
  105. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  106. splitView.CompactPaneLength = 1;
  107. Assert.Equal(GridLength.Auto, splitView.TemplateSettings.PaneColumnGridLength);
  108. Assert.Equal(0, splitView.TemplateSettings.ClosedPaneWidth);
  109. splitView.DisplayMode = SplitViewDisplayMode.Overlay;
  110. splitView.CompactPaneLength = 2;
  111. Assert.Equal(new GridLength(0), splitView.TemplateSettings.PaneColumnGridLength);
  112. Assert.Equal(0, splitView.TemplateSettings.ClosedPaneWidth);
  113. }
  114. [Fact]
  115. public void SplitView_Pointer_Closes_Pane_In_Overlay_Mode()
  116. {
  117. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  118. .With(globalClock: new MockGlobalClock()));
  119. var wnd = new Window
  120. {
  121. Width = 1280,
  122. Height = 720
  123. };
  124. var splitView = new SplitView();
  125. wnd.Content = splitView;
  126. wnd.Show();
  127. splitView.IsPaneOpen = true;
  128. splitView.RaiseEvent(new PointerReleasedEventArgs(splitView,
  129. null, wnd, new Point(1270, 30), 0,
  130. new PointerPointProperties(),
  131. KeyModifiers.None,
  132. MouseButton.Left));
  133. Assert.False(splitView.IsPaneOpen);
  134. // Inline shouldn't close the pane
  135. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  136. splitView.IsPaneOpen = true;
  137. splitView.RaiseEvent(new PointerReleasedEventArgs(splitView,
  138. null, wnd, new Point(1270, 30), 0,
  139. new PointerPointProperties(),
  140. KeyModifiers.None,
  141. MouseButton.Left));
  142. Assert.True(splitView.IsPaneOpen);
  143. }
  144. [Fact]
  145. public void SplitView_Pointer_Should_Not_Close_Pane_If_Over_Pane()
  146. {
  147. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  148. .With(globalClock: new MockGlobalClock()));
  149. var wnd = new Window
  150. {
  151. Width = 1280,
  152. Height = 720
  153. };
  154. var clickBorder = new Border
  155. {
  156. Width = 100,
  157. Height = 100,
  158. HorizontalAlignment = Layout.HorizontalAlignment.Left,
  159. VerticalAlignment = Layout.VerticalAlignment.Top
  160. };
  161. var splitView = new SplitView
  162. {
  163. Pane = clickBorder
  164. };
  165. wnd.Content = splitView;
  166. wnd.Show();
  167. splitView.IsPaneOpen = true;
  168. clickBorder.RaiseEvent(new PointerReleasedEventArgs(splitView,
  169. null, wnd, new Point(5, 5), 0,
  170. new PointerPointProperties(),
  171. KeyModifiers.None,
  172. MouseButton.Left));
  173. Assert.True(splitView.IsPaneOpen);
  174. }
  175. [Fact]
  176. public void SplitView_Escape_Key_Closes_Light_Dismissable_Pane()
  177. {
  178. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  179. .With(globalClock: new MockGlobalClock()));
  180. var wnd = new Window
  181. {
  182. Width = 1280,
  183. Height = 720
  184. };
  185. var button = new Button();
  186. var splitView = new SplitView
  187. {
  188. Pane = button
  189. };
  190. wnd.Content = splitView;
  191. wnd.Show();
  192. splitView.IsPaneOpen = true;
  193. button.RaiseEvent(new KeyEventArgs
  194. {
  195. Key = Key.Escape,
  196. RoutedEvent = InputElement.KeyDownEvent
  197. });
  198. Assert.False(splitView.IsPaneOpen);
  199. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  200. splitView.IsPaneOpen = true;
  201. button.RaiseEvent(new KeyEventArgs
  202. {
  203. Key = Key.Escape,
  204. RoutedEvent = InputElement.KeyDownEvent
  205. });
  206. Assert.True(splitView.IsPaneOpen);
  207. }
  208. [Fact]
  209. public void Top_Level_Back_Requested_Closes_Light_Dismissable_Pane()
  210. {
  211. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  212. .With(globalClock: new MockGlobalClock()));
  213. var wnd = new Window
  214. {
  215. Width = 1280,
  216. Height = 720
  217. };
  218. var splitView = new SplitView();
  219. wnd.Content = splitView;
  220. wnd.Show();
  221. splitView.IsPaneOpen = true;
  222. wnd.RaiseEvent(new Interactivity.RoutedEventArgs(TopLevel.BackRequestedEvent));
  223. Assert.False(splitView.IsPaneOpen);
  224. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  225. splitView.IsPaneOpen = true;
  226. wnd.RaiseEvent(new Interactivity.RoutedEventArgs(TopLevel.BackRequestedEvent));
  227. Assert.True(splitView.IsPaneOpen);
  228. }
  229. [Fact]
  230. public void With_Default_IsPaneOpen_Value_Should_Have_Closed_Pseudo_Class_Set()
  231. {
  232. // Testing this control Pseudo Classes requires placing the SplitView on a window
  233. // prior to asserting them, because some of the pseudo classes are set either when
  234. // the template is applied or the control is attached to the visual tree
  235. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  236. .With(globalClock: new MockGlobalClock()));
  237. var wnd = new Window
  238. {
  239. Width = 1280,
  240. Height = 720
  241. };
  242. var splitView = new SplitView();
  243. wnd.Content = splitView;
  244. wnd.Show();
  245. Assert.Contains(splitView.Classes, ":closed".Equals);
  246. }
  247. [Fact]
  248. public void SplitView_Shouldnt_Close_Panel_When_IsPaneOpen_True_Then_Display_Mode_Changed()
  249. {
  250. using var app = UnitTestApplication.Start(TestServices.StyledWindow
  251. .With(globalClock: new MockGlobalClock()));
  252. var wnd = new Window
  253. {
  254. Width = 1280,
  255. Height = 720
  256. };
  257. var splitView = new SplitView();
  258. splitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
  259. wnd.Content = splitView;
  260. wnd.Show();
  261. splitView.IsPaneOpen = true;
  262. splitView.RaiseEvent(new PointerReleasedEventArgs(splitView,
  263. null, wnd, new Point(1270, 30), 0,
  264. new PointerPointProperties(),
  265. KeyModifiers.None,
  266. MouseButton.Left));
  267. Assert.False(splitView.IsPaneOpen);
  268. // Inline shouldn't close the pane
  269. splitView.IsPaneOpen = true;
  270. // Change the display mode once the pane is already open.
  271. splitView.DisplayMode = SplitViewDisplayMode.Inline;
  272. splitView.RaiseEvent(new PointerReleasedEventArgs(splitView,
  273. null, wnd, new Point(1270, 30), 0,
  274. new PointerPointProperties(),
  275. KeyModifiers.None,
  276. MouseButton.Left));
  277. Assert.True(splitView.IsPaneOpen);
  278. }
  279. }
  280. }