BasicFluentAssertionTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using TerminalGuiFluentTesting;
  2. using Xunit.Abstractions;
  3. namespace IntegrationTests.FluentTests;
  4. public class BasicFluentAssertionTests
  5. {
  6. private readonly TextWriter _out;
  7. public BasicFluentAssertionTests (ITestOutputHelper outputHelper)
  8. {
  9. _out = new TestOutputWriter (outputHelper);
  10. }
  11. [Theory]
  12. [ClassData (typeof (V2TestDrivers))]
  13. public void GuiTestContext_NewInstance_Runs (V2TestDriver d)
  14. {
  15. using GuiTestContext context = With.A<Window> (40, 10, d);
  16. Assert.True (Application.Top!.Running);
  17. context.WriteOutLogs (_out);
  18. context.Stop ();
  19. }
  20. [Theory]
  21. [ClassData (typeof (V2TestDrivers))]
  22. public void GuiTestContext_QuitKey_Stops (V2TestDriver d)
  23. {
  24. using GuiTestContext context = With.A<Window> (40, 10, d);
  25. Assert.True (Application.Top!.Running);
  26. Toplevel top = Application.Top;
  27. context.RaiseKeyDownEvent (Application.QuitKey);
  28. Assert.False (top!.Running);
  29. Application.Top?.Dispose ();
  30. Application.Shutdown ();
  31. context.WriteOutLogs (_out);
  32. context.Stop ();
  33. }
  34. [Theory]
  35. [ClassData (typeof (V2TestDrivers))]
  36. public void GuiTestContext_StartsAndStopsWithoutError (V2TestDriver d)
  37. {
  38. using GuiTestContext context = With.A<Window> (40, 10, d);
  39. // No actual assertions are needed — if no exceptions are thrown, it's working
  40. context.Stop ();
  41. }
  42. [Theory]
  43. [ClassData (typeof (V2TestDrivers))]
  44. public void GuiTestContext_ForgotToStop (V2TestDriver d)
  45. {
  46. using GuiTestContext context = With.A<Window> (40, 10, d);
  47. }
  48. [Theory]
  49. [ClassData (typeof (V2TestDrivers))]
  50. public void TestWindowsResize (V2TestDriver d)
  51. {
  52. var lbl = new Label
  53. {
  54. Width = Dim.Fill ()
  55. };
  56. using GuiTestContext c = With.A<Window> (40, 10, d)
  57. .Add (lbl)
  58. .Then (() => Assert.Equal (38, lbl.Frame.Width)) // Window has 2 border
  59. .ResizeConsole (20, 20)
  60. .Then (() => Assert.Equal (18, lbl.Frame.Width))
  61. .WriteOutLogs (_out)
  62. .Stop ();
  63. }
  64. [Theory]
  65. [ClassData (typeof (V2TestDrivers))]
  66. public void ContextMenu_CrashesOnRight (V2TestDriver d)
  67. {
  68. var clicked = false;
  69. MenuItemv2 [] menuItems = [new ("_New File", string.Empty, () => { clicked = true; })];
  70. using GuiTestContext c = With.A<Window> (40, 10, d)
  71. .WithContextMenu (new PopoverMenu (menuItems))
  72. .ScreenShot ("Before open menu", _out)
  73. // Click in main area inside border
  74. .RightClick (1, 1)
  75. .Then (
  76. () =>
  77. {
  78. // Test depends on menu having a border
  79. IPopover? popover = Application.Popover!.GetActivePopover ();
  80. Assert.NotNull (popover);
  81. var popoverMenu = popover as PopoverMenu;
  82. popoverMenu!.Root!.BorderStyle = LineStyle.Single;
  83. })
  84. .WaitIteration ()
  85. .ScreenShot ("After open menu", _out)
  86. .LeftClick (2, 2)
  87. .Stop ()
  88. .WriteOutLogs (_out);
  89. Assert.True (clicked);
  90. }
  91. [Theory]
  92. [ClassData (typeof (V2TestDrivers))]
  93. public void ContextMenu_OpenSubmenu (V2TestDriver d)
  94. {
  95. var clicked = false;
  96. MenuItemv2 [] menuItems = [
  97. new ("One", "", null),
  98. new ("Two", "", null),
  99. new ("Three", "", null),
  100. new ("Four", "", new (
  101. [
  102. new ("SubMenu1", "", null),
  103. new ("SubMenu2", "", ()=>clicked=true),
  104. new ("SubMenu3", "", null),
  105. new ("SubMenu4", "", null),
  106. new ("SubMenu5", "", null),
  107. new ("SubMenu6", "", null),
  108. new ("SubMenu7", "", null)
  109. ])),
  110. new ("Five", "", null),
  111. new ("Six", "", null)
  112. ];
  113. using GuiTestContext c = With.A<Window> (40, 10, d)
  114. .WithContextMenu (new PopoverMenu (menuItems))
  115. .ScreenShot ("Before open menu", _out)
  116. // Click in main area inside border
  117. .RightClick (1, 1)
  118. .ScreenShot ("After open menu", _out)
  119. .Down ()
  120. .Down ()
  121. .Down ()
  122. .Right ()
  123. .ScreenShot ("After open submenu", _out)
  124. .Down ()
  125. .Enter ()
  126. .ScreenShot ("Menu should be closed after selecting", _out)
  127. .Stop ()
  128. .WriteOutLogs (_out);
  129. Assert.True (clicked);
  130. }
  131. [Theory]
  132. [ClassData (typeof (V2TestDrivers))]
  133. public void Toplevel_TabGroup_Forward_Backward (V2TestDriver d)
  134. {
  135. var v1 = new View { Id = "v1", CanFocus = true };
  136. var v2 = new View { Id = "v2", CanFocus = true };
  137. var v3 = new View { Id = "v3", CanFocus = true };
  138. var v4 = new View { Id = "v4", CanFocus = true };
  139. var v5 = new View { Id = "v5", CanFocus = true };
  140. var v6 = new View { Id = "v6", CanFocus = true };
  141. using GuiTestContext c = With.A<Window> (50, 20, d)
  142. .Then (
  143. () =>
  144. {
  145. var w1 = new Window { Id = "w1" };
  146. w1.Add (v1, v2);
  147. var w2 = new Window { Id = "w2" };
  148. w2.Add (v3, v4);
  149. var w3 = new Window { Id = "w3" };
  150. w3.Add (v5, v6);
  151. Toplevel top = Application.Top!;
  152. Application.Top!.Add (w1, w2, w3);
  153. })
  154. .WaitIteration ()
  155. .Then (() => Assert.True (v5.HasFocus))
  156. .RaiseKeyDownEvent (Key.F6)
  157. .Then (() => Assert.True (v1.HasFocus))
  158. .RaiseKeyDownEvent (Key.F6)
  159. .Then (() => Assert.True (v3.HasFocus))
  160. .RaiseKeyDownEvent (Key.F6.WithShift)
  161. .Then (() => Assert.True (v1.HasFocus))
  162. .RaiseKeyDownEvent (Key.F6.WithShift)
  163. .Then (() => Assert.True (v5.HasFocus))
  164. .RaiseKeyDownEvent (Key.F6.WithShift)
  165. .Then (() => Assert.True (v3.HasFocus))
  166. .RaiseKeyDownEvent (Key.F6)
  167. .Then (() => Assert.True (v5.HasFocus))
  168. .RaiseKeyDownEvent (Key.F6)
  169. .Then (() => Assert.True (v1.HasFocus))
  170. .RaiseKeyDownEvent (Key.F6)
  171. .Then (() => Assert.True (v3.HasFocus))
  172. .RaiseKeyDownEvent (Key.F6.WithShift)
  173. .Then (() => Assert.True (v1.HasFocus))
  174. .RaiseKeyDownEvent (Key.F6.WithShift)
  175. .Then (() => Assert.True (v5.HasFocus))
  176. .RaiseKeyDownEvent (Key.F6.WithShift)
  177. .Then (() => Assert.True (v3.HasFocus))
  178. .RaiseKeyDownEvent (Key.Tab)
  179. .Then (() => Assert.True (v4.HasFocus))
  180. .RaiseKeyDownEvent (Key.F6)
  181. .Then (() => Assert.True (v5.HasFocus))
  182. .RaiseKeyDownEvent (Key.F6)
  183. .Then (() => Assert.True (v1.HasFocus))
  184. .RaiseKeyDownEvent (Key.F6.WithShift)
  185. .Then (() => Assert.True (v5.HasFocus))
  186. .RaiseKeyDownEvent (Key.Tab)
  187. .Then (() => Assert.True (v6.HasFocus))
  188. .RaiseKeyDownEvent (Key.F6.WithShift)
  189. .Then (() => Assert.True (v4.HasFocus))
  190. .RaiseKeyDownEvent (Key.F6)
  191. .Then (() => Assert.True (v6.HasFocus))
  192. .WriteOutLogs (_out)
  193. .Stop ();
  194. Assert.False (v1.HasFocus);
  195. Assert.False (v2.HasFocus);
  196. Assert.False (v3.HasFocus);
  197. Assert.False (v4.HasFocus);
  198. Assert.False (v5.HasFocus);
  199. Assert.False (v6.HasFocus);
  200. }
  201. }