ApplicationTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.Core {
  9. public class ApplicationTests {
  10. public ApplicationTests ()
  11. {
  12. #if DEBUG_IDISPOSABLE
  13. Responder.Instances.Clear ();
  14. #endif
  15. }
  16. [Fact]
  17. public void Init_Shutdown_Cleans_Up ()
  18. {
  19. // Verify initial state is per spec
  20. Pre_Init_State ();
  21. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  22. // Verify post-Init state is correct
  23. Post_Init_State ();
  24. // MockDriver is always 80x25
  25. Assert.Equal (80, Application.Driver.Cols);
  26. Assert.Equal (25, Application.Driver.Rows);
  27. Application.Shutdown ();
  28. // Verify state is back to initial
  29. Pre_Init_State ();
  30. }
  31. void Pre_Init_State ()
  32. {
  33. Assert.Null (Application.Driver);
  34. Assert.Null (Application.Top);
  35. Assert.Null (Application.Current);
  36. Assert.Throws<ArgumentNullException> (() => Application.HeightAsBuffer == true);
  37. Assert.False (Application.AlwaysSetPosition);
  38. Assert.Null (Application.MainLoop);
  39. Assert.Null (Application.Iteration);
  40. Assert.Null (Application.RootMouseEvent);
  41. Assert.Null (Application.Resized);
  42. }
  43. void Post_Init_State ()
  44. {
  45. Assert.NotNull (Application.Driver);
  46. Assert.NotNull (Application.Top);
  47. Assert.NotNull (Application.Current);
  48. Assert.False (Application.HeightAsBuffer);
  49. Assert.False (Application.AlwaysSetPosition);
  50. Assert.NotNull (Application.MainLoop);
  51. Assert.Null (Application.Iteration);
  52. Assert.Null (Application.RootMouseEvent);
  53. Assert.Null (Application.Resized);
  54. }
  55. [Fact]
  56. public void RunState_Dispose_Cleans_Up ()
  57. {
  58. var rs = new Application.RunState (null);
  59. Assert.NotNull (rs);
  60. // Should not throw because Toplevel was null
  61. rs.Dispose ();
  62. var top = new Toplevel ();
  63. rs = new Application.RunState (top);
  64. Assert.NotNull (rs);
  65. // Should throw because there's no stack
  66. Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
  67. }
  68. void Init ()
  69. {
  70. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  71. Assert.NotNull (Application.Driver);
  72. Assert.NotNull (Application.MainLoop);
  73. }
  74. void Shutdown ()
  75. {
  76. Application.Shutdown ();
  77. }
  78. [Fact]
  79. public void Begin_End_Cleana_Up ()
  80. {
  81. // Setup Mock driver
  82. Init ();
  83. // Test null Toplevel
  84. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  85. var top = new Toplevel ();
  86. var rs = Application.Begin (top);
  87. Assert.NotNull (rs);
  88. Assert.Equal (top, Application.Current);
  89. Application.End (rs);
  90. Assert.Null (Application.Current);
  91. Assert.NotNull (Application.Top);
  92. Assert.NotNull (Application.MainLoop);
  93. Assert.NotNull (Application.Driver);
  94. Shutdown ();
  95. Assert.Null (Application.Top);
  96. Assert.Null (Application.MainLoop);
  97. Assert.Null (Application.Driver);
  98. }
  99. [Fact]
  100. public void RequestStop_Stops ()
  101. {
  102. // Setup Mock driver
  103. Init ();
  104. var top = new Toplevel ();
  105. var rs = Application.Begin (top);
  106. Assert.NotNull (rs);
  107. Assert.Equal (top, Application.Current);
  108. Application.Iteration = () => {
  109. Application.RequestStop ();
  110. };
  111. Application.Run (top);
  112. Application.Shutdown ();
  113. Assert.Null (Application.Current);
  114. Assert.Null (Application.Top);
  115. Assert.Null (Application.MainLoop);
  116. Assert.Null (Application.Driver);
  117. }
  118. [Fact]
  119. public void RunningFalse_Stops ()
  120. {
  121. // Setup Mock driver
  122. Init ();
  123. var top = new Toplevel ();
  124. var rs = Application.Begin (top);
  125. Assert.NotNull (rs);
  126. Assert.Equal (top, Application.Current);
  127. Application.Iteration = () => {
  128. top.Running = false;
  129. };
  130. Application.Run (top);
  131. Application.Shutdown ();
  132. Assert.Null (Application.Current);
  133. Assert.Null (Application.Top);
  134. Assert.Null (Application.MainLoop);
  135. Assert.Null (Application.Driver);
  136. }
  137. [Fact]
  138. public void KeyUp_Event ()
  139. {
  140. // Setup Mock driver
  141. Init ();
  142. // Setup some fake keypresses (This)
  143. var input = "Tests";
  144. // Put a control-q in at the end
  145. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  146. foreach (var c in input.Reverse ()) {
  147. if (char.IsLetter (c)) {
  148. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  149. } else {
  150. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  151. }
  152. }
  153. int stackSize = Console.MockKeyPresses.Count;
  154. int iterations = 0;
  155. Application.Iteration = () => {
  156. iterations++;
  157. // Stop if we run out of control...
  158. if (iterations > 10) {
  159. Application.RequestStop ();
  160. }
  161. };
  162. int keyUps = 0;
  163. var output = string.Empty;
  164. Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
  165. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  166. output += (char)args.KeyEvent.KeyValue;
  167. }
  168. keyUps++;
  169. };
  170. Application.Run (Application.Top);
  171. // Input string should match output
  172. Assert.Equal (input, output);
  173. // # of key up events should match stack size
  174. //Assert.Equal (stackSize, keyUps);
  175. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  176. // it must be literal (Linux only).
  177. Assert.Equal (6, keyUps);
  178. // # of key up events should match # of iterations
  179. Assert.Equal (stackSize, iterations);
  180. Application.Shutdown ();
  181. Assert.Null (Application.Current);
  182. Assert.Null (Application.Top);
  183. Assert.Null (Application.MainLoop);
  184. Assert.Null (Application.Driver);
  185. }
  186. [Fact]
  187. public void Loaded_Ready_Unlodaded_Events ()
  188. {
  189. Init ();
  190. var top = Application.Top;
  191. var count = 0;
  192. top.Loaded += () => count++;
  193. top.Ready += () => count++;
  194. top.Unloaded += () => count++;
  195. Application.Iteration = () => Application.RequestStop ();
  196. Application.Run ();
  197. Application.Shutdown ();
  198. Assert.Equal (3, count);
  199. }
  200. [Fact]
  201. public void Shutdown_Allows_Async ()
  202. {
  203. static async Task TaskWithAsyncContinuation ()
  204. {
  205. await Task.Yield ();
  206. await Task.Yield ();
  207. }
  208. Init ();
  209. Application.Shutdown ();
  210. var task = TaskWithAsyncContinuation ();
  211. Thread.Sleep (20);
  212. Assert.True (task.IsCompletedSuccessfully);
  213. }
  214. [Fact]
  215. public void Shutdown_Resets_SyncContext ()
  216. {
  217. Init ();
  218. Application.Shutdown ();
  219. Assert.Null (SynchronizationContext.Current);
  220. }
  221. [Fact]
  222. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  223. {
  224. Init ();
  225. var top = Application.Top;
  226. var w1 = new Window ();
  227. var v1 = new TextField ();
  228. var v2 = new TextView ();
  229. w1.Add (v1, v2);
  230. var w2 = new Window ();
  231. var v3 = new CheckBox ();
  232. var v4 = new Button ();
  233. w2.Add (v3, v4);
  234. top.Add (w1, w2);
  235. Application.Iteration += () => {
  236. Assert.True (v1.HasFocus);
  237. // Using default keys.
  238. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  239. new KeyModifiers () { Ctrl = true }));
  240. Assert.True (v2.HasFocus);
  241. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  242. new KeyModifiers () { Ctrl = true }));
  243. Assert.True (v3.HasFocus);
  244. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  245. new KeyModifiers () { Ctrl = true }));
  246. Assert.True (v4.HasFocus);
  247. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  248. new KeyModifiers () { Ctrl = true }));
  249. Assert.True (v1.HasFocus);
  250. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  251. new KeyModifiers () { Shift = true, Ctrl = true }));
  252. Assert.True (v4.HasFocus);
  253. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  254. new KeyModifiers () { Shift = true, Ctrl = true }));
  255. Assert.True (v3.HasFocus);
  256. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  257. new KeyModifiers () { Shift = true, Ctrl = true }));
  258. Assert.True (v2.HasFocus);
  259. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  260. new KeyModifiers () { Shift = true, Ctrl = true }));
  261. Assert.True (v1.HasFocus);
  262. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  263. new KeyModifiers () { Ctrl = true }));
  264. Assert.True (v2.HasFocus);
  265. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  266. new KeyModifiers () { Ctrl = true }));
  267. Assert.True (v3.HasFocus);
  268. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  269. new KeyModifiers () { Ctrl = true }));
  270. Assert.True (v4.HasFocus);
  271. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  272. new KeyModifiers () { Ctrl = true }));
  273. Assert.True (v1.HasFocus);
  274. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  275. new KeyModifiers () { Ctrl = true }));
  276. Assert.True (v4.HasFocus);
  277. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  278. new KeyModifiers () { Ctrl = true }));
  279. Assert.True (v3.HasFocus);
  280. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  281. new KeyModifiers () { Ctrl = true }));
  282. Assert.True (v2.HasFocus);
  283. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  284. new KeyModifiers () { Ctrl = true }));
  285. Assert.True (v1.HasFocus);
  286. // Using another's alternate keys.
  287. Application.AlternateForwardKey = Key.F7;
  288. Application.AlternateBackwardKey = Key.F6;
  289. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  290. Assert.True (v2.HasFocus);
  291. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  292. Assert.True (v3.HasFocus);
  293. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  294. Assert.True (v4.HasFocus);
  295. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  296. Assert.True (v1.HasFocus);
  297. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  298. Assert.True (v4.HasFocus);
  299. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  300. Assert.True (v3.HasFocus);
  301. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  302. Assert.True (v2.HasFocus);
  303. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  304. Assert.True (v1.HasFocus);
  305. Application.RequestStop ();
  306. };
  307. Application.Run (top);
  308. }
  309. }
  310. }