ApplicationPopoverTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using static System.Net.Mime.MediaTypeNames;
  2. namespace Terminal.Gui.ApplicationTests;
  3. public class ApplicationPopoverTests
  4. {
  5. [Fact]
  6. public void Popover_ApplicationInit_Inits ()
  7. {
  8. // Arrange
  9. Assert.Null (Application.Popover);
  10. Application.Init (new FakeDriver ());
  11. // Act
  12. Assert.NotNull (Application.Popover);
  13. Application.ResetState (true);
  14. }
  15. [Fact]
  16. public void Popover_ApplicationShutdown_CleansUp ()
  17. {
  18. // Arrange
  19. Assert.Null (Application.Popover);
  20. Application.Init (new FakeDriver ());
  21. // Act
  22. Assert.NotNull (Application.Popover);
  23. Application.Shutdown ();
  24. // Test
  25. Assert.Null (Application.Popover);
  26. }
  27. [Fact]
  28. public void Popover_NotCleanedUp_On_End ()
  29. {
  30. // Arrange
  31. Assert.Null (Application.Popover);
  32. Application.Init (new FakeDriver ());
  33. Assert.NotNull (Application.Popover);
  34. Application.Iteration += (s, a) => Application.RequestStop ();
  35. var top = new Toplevel ();
  36. RunState rs = Application.Begin (top);
  37. // Act
  38. Application.End (rs);
  39. // Test
  40. Assert.NotNull (Application.Popover);
  41. top.Dispose ();
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. public void Popover_Active_Hidden_On_End ()
  46. {
  47. // Arrange
  48. Assert.Null (Application.Popover);
  49. Application.Init (new FakeDriver ());
  50. Application.Iteration += (s, a) => Application.RequestStop ();
  51. var top = new Toplevel ();
  52. RunState rs = Application.Begin (top);
  53. IPopoverTestClass popover = new ();
  54. Application.Popover?.ShowPopover (popover);
  55. Assert.True (popover.Visible);
  56. // Act
  57. Application.End (rs);
  58. top.Dispose ();
  59. // Test
  60. Assert.False (popover.Visible);
  61. Assert.NotNull (Application.Popover);
  62. popover.Dispose ();
  63. Application.Shutdown ();
  64. }
  65. public class IPopoverTestClass : View, IPopover
  66. {
  67. public List<Key> HandledKeys { get; } = new List<Key> ();
  68. public int NewCommandInvokeCount { get; private set; }
  69. public IPopoverTestClass ()
  70. {
  71. CanFocus = true;
  72. AddCommand (Command.New, NewCommandHandler);
  73. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  74. bool? NewCommandHandler (ICommandContext ctx)
  75. {
  76. NewCommandInvokeCount++;
  77. return false;
  78. }
  79. }
  80. protected override bool OnKeyDown (Key key)
  81. {
  82. HandledKeys.Add (key);
  83. return false;
  84. }
  85. }
  86. //[Fact]
  87. //public void Popover_SetToNull ()
  88. //{
  89. // // Arrange
  90. // var popover = new View ();
  91. // Application.Popover = popover;
  92. // // Act
  93. // Application.Popover = null;
  94. // // Assert
  95. // Assert.Null (Application.Popover);
  96. // Application.ResetState (ignoreDisposed: true);
  97. //}
  98. //[Fact]
  99. //public void Popover_VisibleChangedEvent ()
  100. //{
  101. // // Arrange
  102. // var popover = new View ()
  103. // {
  104. // Visible = false
  105. // };
  106. // Application.Popover = popover;
  107. // bool eventTriggered = false;
  108. // popover.VisibleChanged += (sender, e) => eventTriggered = true;
  109. // // Act
  110. // popover.Visible = true;
  111. // // Assert
  112. // Assert.True (eventTriggered);
  113. // Application.ResetState (ignoreDisposed: true);
  114. //}
  115. //[Fact]
  116. //public void Popover_InitializesCorrectly ()
  117. //{
  118. // // Arrange
  119. // var popover = new View ();
  120. // // Act
  121. // Application.Popover = popover;
  122. // // Assert
  123. // Assert.True (popover.IsInitialized);
  124. // Application.ResetState (ignoreDisposed: true);
  125. //}
  126. //[Fact]
  127. //public void Popover_SetsColorScheme ()
  128. //{
  129. // // Arrange
  130. // var popover = new View ();
  131. // var topColorScheme = new ColorScheme ();
  132. // Application.Top = new Toplevel { ColorScheme = topColorScheme };
  133. // // Act
  134. // Application.Popover = popover;
  135. // // Assert
  136. // Assert.Equal (topColorScheme, popover.ColorScheme);
  137. // Application.ResetState (ignoreDisposed: true);
  138. //}
  139. //[Fact]
  140. //public void Popover_VisibleChangedToTrue_SetsFocus ()
  141. //{
  142. // // Arrange
  143. // var popover = new View ()
  144. // {
  145. // Visible = false,
  146. // CanFocus = true
  147. // };
  148. // Application.Popover = popover;
  149. // // Act
  150. // popover.Visible = true;
  151. // // Assert
  152. // Assert.True (popover.Visible);
  153. // Assert.True (popover.HasFocus);
  154. // Application.ResetState (ignoreDisposed: true);
  155. //}
  156. //[Theory]
  157. //[InlineData(-1, -1)]
  158. //[InlineData (0, 0)]
  159. //[InlineData (2048, 2048)]
  160. //[InlineData (2049, 2049)]
  161. //public void Popover_VisibleChangedToTrue_Locates_In_Visible_Position (int x, int y)
  162. //{
  163. // // Arrange
  164. // var popover = new View ()
  165. // {
  166. // X = x,
  167. // Y = y,
  168. // Visible = false,
  169. // CanFocus = true,
  170. // Width = 1,
  171. // Height = 1
  172. // };
  173. // Application.Popover = popover;
  174. // // Act
  175. // popover.Visible = true;
  176. // Application.LayoutAndDraw();
  177. // // Assert
  178. // Assert.True (Application.Screen.Contains (popover.Frame));
  179. // Application.ResetState (ignoreDisposed: true);
  180. //}
  181. //[Fact]
  182. //public void Popover_VisibleChangedToFalse_Hides_And_Removes_Focus ()
  183. //{
  184. // // Arrange
  185. // var popover = new View ()
  186. // {
  187. // Visible = false,
  188. // CanFocus = true
  189. // };
  190. // Application.Popover = popover;
  191. // popover.Visible = true;
  192. // // Act
  193. // popover.Visible = false;
  194. // // Assert
  195. // Assert.False (popover.Visible);
  196. // Assert.False (popover.HasFocus);
  197. // Application.ResetState (ignoreDisposed: true);
  198. //}
  199. //[Fact]
  200. //public void Popover_Quit_Command_Hides ()
  201. //{
  202. // // Arrange
  203. // var popover = new View ()
  204. // {
  205. // Visible = false,
  206. // CanFocus = true
  207. // };
  208. // Application.Popover = popover;
  209. // popover.Visible = true;
  210. // Assert.True (popover.Visible);
  211. // Assert.True (popover.HasFocus);
  212. // // Act
  213. // Application.RaiseKeyDownEvent (Application.QuitKey);
  214. // // Assert
  215. // Assert.False (popover.Visible);
  216. // Assert.False (popover.HasFocus);
  217. // Application.ResetState (ignoreDisposed: true);
  218. //}
  219. //[Fact]
  220. //public void Popover_MouseClick_Outside_Hides_Passes_Event_On ()
  221. //{
  222. // // Arrange
  223. // Application.Top = new Toplevel ()
  224. // {
  225. // Id = "top",
  226. // Height = 10,
  227. // Width = 10,
  228. // };
  229. // View otherView = new ()
  230. // {
  231. // X = 1,
  232. // Y = 1,
  233. // Height = 1,
  234. // Width = 1,
  235. // Id = "otherView",
  236. // };
  237. // bool otherViewPressed = false;
  238. // otherView.MouseEvent += (sender, e) =>
  239. // {
  240. // otherViewPressed = e.Flags.HasFlag(MouseFlags.Button1Pressed);
  241. // };
  242. // Application.Top.Add (otherView);
  243. // var popover = new View ()
  244. // {
  245. // Id = "popover",
  246. // X = 5,
  247. // Y = 5,
  248. // Width = 1,
  249. // Height = 1,
  250. // Visible = false,
  251. // CanFocus = true
  252. // };
  253. // Application.Popover = popover;
  254. // popover.Visible = true;
  255. // Assert.True (popover.Visible);
  256. // Assert.True (popover.HasFocus);
  257. // // Act
  258. // // Click on popover
  259. // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (5, 5) });
  260. // Assert.True (popover.Visible);
  261. // // Click outside popover (on button)
  262. // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (1, 1) });
  263. // // Assert
  264. // Assert.True (otherViewPressed);
  265. // Assert.False (popover.Visible);
  266. // Application.Top.Dispose ();
  267. // Application.ResetState (ignoreDisposed: true);
  268. //}
  269. //[Theory]
  270. //[InlineData (0, 0, false)]
  271. //[InlineData (5, 5, true)]
  272. //[InlineData (10, 10, false)]
  273. //[InlineData (5, 10, false)]
  274. //[InlineData (9, 9, false)]
  275. //public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible)
  276. //{
  277. // // Arrange
  278. // Application.Top = new Toplevel ()
  279. // {
  280. // Id = "top",
  281. // Height = 10,
  282. // Width = 10,
  283. // };
  284. // var popover = new View ()
  285. // {
  286. // Id = "popover",
  287. // X = 5,
  288. // Y = 5,
  289. // Width = 1,
  290. // Height = 1,
  291. // Visible = false,
  292. // CanFocus = true
  293. // };
  294. // Application.Popover = popover;
  295. // popover.Visible = true;
  296. // Assert.True (popover.Visible);
  297. // Assert.True (popover.HasFocus);
  298. // // Act
  299. // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (mouseX, mouseY) });
  300. // // Assert
  301. // Assert.Equal (expectedVisible, popover.Visible);
  302. // Application.Top.Dispose ();
  303. // Application.ResetState (ignoreDisposed: true);
  304. //}
  305. //[Fact]
  306. //public void Popover_SetAndGet_ReturnsCorrectValue ()
  307. //{
  308. // // Arrange
  309. // var view = new View ();
  310. // // Act
  311. // Application.Popover = view;
  312. // // Assert
  313. // Assert.Equal (view, Application.Popover);
  314. // Application.ResetState (ignoreDisposed: true);
  315. //}
  316. //[Fact]
  317. //public void Popover_SetToNull_HidesPreviousPopover ()
  318. //{
  319. // // Arrange
  320. // var view = new View { Visible = true };
  321. // Application.Popover = view;
  322. // // Act
  323. // Application.Popover = null;
  324. // // Assert
  325. // Assert.False (view.Visible);
  326. // Assert.Null (Application.Popover);
  327. // Application.ResetState (ignoreDisposed: true);
  328. //}
  329. //[Fact]
  330. //public void Popover_SetNewPopover_HidesPreviousPopover ()
  331. //{
  332. // // Arrange
  333. // var oldView = new View { Visible = true };
  334. // var newView = new View ();
  335. // Application.Popover = oldView;
  336. // // Act
  337. // Application.Popover = newView;
  338. // // Assert
  339. // Assert.False (oldView.Visible);
  340. // Assert.Equal (newView, Application.Popover);
  341. // Application.ResetState (ignoreDisposed: true);
  342. //}
  343. //[Fact]
  344. //public void Popover_SetNewPopover_InitializesAndSetsProperties ()
  345. //{
  346. // // Arrange
  347. // var view = new View ();
  348. // // Act
  349. // Application.Popover = view;
  350. // // Assert
  351. // Assert.True (view.IsInitialized);
  352. // Assert.True (view.Arrangement.HasFlag (ViewArrangement.Overlapped));
  353. // Assert.Equal (Application.Top?.ColorScheme, view.ColorScheme);
  354. // Application.ResetState (ignoreDisposed: true);
  355. //}
  356. }