BasicFluentAssertionTests.cs 9.7 KB

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