ApplicationImplTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. using System.Collections.Concurrent;
  2. using Moq;
  3. namespace ApplicationTests;
  4. public class ApplicationImplTests
  5. {
  6. [Fact]
  7. public void Internal_Properties_Correct ()
  8. {
  9. IApplication app = Application.Create ();
  10. app.Init ("fake");
  11. Assert.True (app.Initialized);
  12. Assert.Null (app.TopRunnableView);
  13. SessionToken? rs = app.Begin (new Runnable<bool> ());
  14. Assert.Equal (app.TopRunnable, rs!.Runnable);
  15. Assert.Null (app.Mouse.MouseGrabView); // public
  16. app.Dispose ();
  17. }
  18. #region DisposeTests
  19. [Fact]
  20. public async Task Dispose_Allows_Async ()
  21. {
  22. var isCompletedSuccessfully = false;
  23. async Task TaskWithAsyncContinuation ()
  24. {
  25. await Task.Yield ();
  26. await Task.Yield ();
  27. isCompletedSuccessfully = true;
  28. }
  29. IApplication app = Application.Create ();
  30. app.Dispose ();
  31. Assert.False (isCompletedSuccessfully);
  32. await TaskWithAsyncContinuation ();
  33. Thread.Sleep (100);
  34. Assert.True (isCompletedSuccessfully);
  35. }
  36. [Fact]
  37. public void Dispose_Resets_SyncContext ()
  38. {
  39. IApplication app = Application.Create ();
  40. app.Dispose ();
  41. Assert.Null (SynchronizationContext.Current);
  42. }
  43. [Fact]
  44. public void Dispose_Alone_Does_Nothing ()
  45. {
  46. IApplication app = Application.Create ();
  47. app.Dispose ();
  48. }
  49. #endregion
  50. /// <summary>
  51. /// Crates a new ApplicationImpl instance for testing. The input, output, and size monitor components are mocked.
  52. /// </summary>
  53. private IApplication NewMockedApplicationImpl ()
  54. {
  55. Mock<INetInput> netInput = new ();
  56. SetupRunInputMockMethodToBlock (netInput);
  57. Mock<IComponentFactory<ConsoleKeyInfo>> m = new ();
  58. m.Setup (f => f.CreateInput ()).Returns (netInput.Object);
  59. m.Setup (f => f.CreateInputProcessor (It.IsAny<ConcurrentQueue<ConsoleKeyInfo>> ())).Returns (Mock.Of<IInputProcessor> ());
  60. Mock<IOutput> consoleOutput = new ();
  61. var size = new Size (80, 25);
  62. consoleOutput.Setup (o => o.SetSize (It.IsAny<int> (), It.IsAny<int> ()))
  63. .Callback<int, int> ((w, h) => size = new (w, h));
  64. consoleOutput.Setup (o => o.GetSize ()).Returns (() => size);
  65. m.Setup (f => f.CreateOutput ()).Returns (consoleOutput.Object);
  66. m.Setup (f => f.CreateSizeMonitor (It.IsAny<IOutput> (), It.IsAny<IOutputBuffer> ())).Returns (Mock.Of<ISizeMonitor> ());
  67. return new ApplicationImpl (m.Object);
  68. }
  69. private void SetupRunInputMockMethodToBlock (Mock<INetInput> netInput)
  70. {
  71. netInput.Setup (r => r.Run (It.IsAny<CancellationToken> ()))
  72. .Callback<CancellationToken> (token =>
  73. {
  74. // Simulate an infinite loop that checks for cancellation
  75. while (!token.IsCancellationRequested)
  76. {
  77. // Perform the action that should repeat in the loop
  78. // This could be some mock behavior or just an empty loop depending on the context
  79. }
  80. })
  81. .Verifiable (Times.Once);
  82. }
  83. [Fact]
  84. public void NoInitThrowOnRun ()
  85. {
  86. IApplication app = NewMockedApplicationImpl ();
  87. var ex = Assert.Throws<NotInitializedException> (() => app.Run (new Window ()));
  88. app.Dispose ();
  89. }
  90. [Fact]
  91. public void InitRunShutdown_Top_Set_To_Null_After_Shutdown ()
  92. {
  93. IApplication app = NewMockedApplicationImpl ();
  94. app.Init ("fake");
  95. object? timeoutToken = app.AddTimeout (
  96. TimeSpan.FromMilliseconds (150),
  97. () =>
  98. {
  99. if (app.TopRunnableView is { })
  100. {
  101. app.RequestStop ();
  102. return false;
  103. }
  104. return false;
  105. }
  106. );
  107. Assert.Null (app.TopRunnableView);
  108. // Blocks until the timeout call is hit
  109. app.Run (new Window ());
  110. // We returned false above, so we should not have to remove the timeout
  111. Assert.False (app.RemoveTimeout (timeoutToken!));
  112. Assert.Null (app.TopRunnableView);
  113. app.Dispose ();
  114. Assert.Null (app.TopRunnableView);
  115. }
  116. [Fact]
  117. public void InitRunShutdown_Running_Set_To_False ()
  118. {
  119. IApplication app = NewMockedApplicationImpl ()!;
  120. app.Init ("fake");
  121. IRunnable top = new Window
  122. {
  123. Title = "InitRunShutdown_Running_Set_To_False"
  124. };
  125. object? timeoutToken = app.AddTimeout (
  126. TimeSpan.FromMilliseconds (150),
  127. () =>
  128. {
  129. Assert.True (top!.IsRunning);
  130. if (app.TopRunnableView != null)
  131. {
  132. app.RequestStop ();
  133. return false;
  134. }
  135. return false;
  136. }
  137. );
  138. Assert.False (top.IsRunning);
  139. // Blocks until the timeout call is hit
  140. app.Run (top);
  141. // We returned false above, so we should not have to remove the timeout
  142. Assert.False (app.RemoveTimeout (timeoutToken!));
  143. Assert.False (top.IsRunning);
  144. // BUGBUG: Shutdown sets Top to null, not End.
  145. //Assert.Null (Application.TopRunnable);
  146. app.TopRunnableView?.Dispose ();
  147. app.Dispose ();
  148. }
  149. [Fact]
  150. public void InitRunShutdown_StopAfterFirstIteration_Stops ()
  151. {
  152. IApplication app = NewMockedApplicationImpl ()!;
  153. Assert.Null (app.TopRunnableView);
  154. Assert.Null (app.Driver);
  155. app.Init ("fake");
  156. IRunnable top = new Window ();
  157. var isIsModalChanged = 0;
  158. top.IsModalChanged
  159. += (_, a) => { isIsModalChanged++; };
  160. var isRunningChangedCount = 0;
  161. top.IsRunningChanged
  162. += (_, a) => { isRunningChangedCount++; };
  163. object? timeoutToken = app.AddTimeout (
  164. TimeSpan.FromMilliseconds (150),
  165. () =>
  166. {
  167. //Assert.Fail (@"Didn't stop after first iteration.");
  168. return false;
  169. }
  170. );
  171. Assert.Equal (0, isIsModalChanged);
  172. Assert.Equal (0, isRunningChangedCount);
  173. app.StopAfterFirstIteration = true;
  174. app.Run (top);
  175. Assert.Equal (2, isIsModalChanged);
  176. Assert.Equal (2, isRunningChangedCount);
  177. app.TopRunnableView?.Dispose ();
  178. app.Dispose ();
  179. Assert.Equal (2, isIsModalChanged);
  180. Assert.Equal (2, isRunningChangedCount);
  181. }
  182. [Fact]
  183. public void InitRunShutdown_End_Is_Called ()
  184. {
  185. IApplication app = NewMockedApplicationImpl ()!;
  186. Assert.Null (app.TopRunnableView);
  187. Assert.Null (app.Driver);
  188. app.Init ("fake");
  189. IRunnable top = new Window ();
  190. var isIsModalChanged = 0;
  191. top.IsModalChanged
  192. += (_, a) => { isIsModalChanged++; };
  193. var isRunningChangedCount = 0;
  194. top.IsRunningChanged
  195. += (_, a) => { isRunningChangedCount++; };
  196. object? timeoutToken = app.AddTimeout (
  197. TimeSpan.FromMilliseconds (150),
  198. () =>
  199. {
  200. Assert.True (top!.IsRunning);
  201. if (app.TopRunnableView != null)
  202. {
  203. app.RequestStop ();
  204. return false;
  205. }
  206. return false;
  207. }
  208. );
  209. Assert.Equal (0, isIsModalChanged);
  210. Assert.Equal (0, isRunningChangedCount);
  211. // Blocks until the timeout call is hit
  212. app.Run (top);
  213. Assert.Equal (2, isIsModalChanged);
  214. Assert.Equal (2, isRunningChangedCount);
  215. // We returned false above, so we should not have to remove the timeout
  216. Assert.False (app.RemoveTimeout (timeoutToken!));
  217. app.TopRunnableView?.Dispose ();
  218. app.Dispose ();
  219. Assert.Equal (2, isIsModalChanged);
  220. Assert.Equal (2, isRunningChangedCount);
  221. }
  222. [Fact]
  223. public void InitRunShutdown_QuitKey_Quits ()
  224. {
  225. IApplication app = NewMockedApplicationImpl ()!;
  226. app.Init ("fake");
  227. IRunnable top = new Window
  228. {
  229. Title = "InitRunShutdown_QuitKey_Quits"
  230. };
  231. object? timeoutToken = app.AddTimeout (
  232. TimeSpan.FromMilliseconds (150),
  233. () =>
  234. {
  235. Assert.True (top!.IsRunning);
  236. if (app.TopRunnableView != null)
  237. {
  238. app.Keyboard.RaiseKeyDownEvent (app.Keyboard.QuitKey);
  239. }
  240. return false;
  241. }
  242. );
  243. Assert.False (top!.IsRunning);
  244. // Blocks until the timeout call is hit
  245. app.Run (top);
  246. // We returned false above, so we should not have to remove the timeout
  247. Assert.False (app.RemoveTimeout (timeoutToken!));
  248. Assert.False (top!.IsRunning);
  249. Assert.Null (app.TopRunnableView);
  250. ((top as Window)!).Dispose ();
  251. app.Dispose ();
  252. Assert.Null (app.TopRunnableView);
  253. }
  254. [Fact]
  255. public void InitRunShutdown_Generic_IdleForExit ()
  256. {
  257. IApplication app = NewMockedApplicationImpl ()!;
  258. app.Init ("fake");
  259. app.AddTimeout (TimeSpan.Zero, () => IdleExit (app));
  260. Assert.Null (app.TopRunnableView);
  261. // Blocks until the timeout call is hit
  262. app.Run<Window> ();
  263. Assert.Null (app.TopRunnableView);
  264. app.Dispose ();
  265. Assert.Null (app.TopRunnableView);
  266. }
  267. [Fact]
  268. public void Run_IsRunningChanging_And_IsRunningChanged_Raised ()
  269. {
  270. IApplication app = NewMockedApplicationImpl ()!;
  271. app.Init ("fake");
  272. var isRunningChanging = 0;
  273. var isRunningChanged = 0;
  274. Runnable<bool> t = new ();
  275. t.IsRunningChanging
  276. += (_, a) => { isRunningChanging++; };
  277. t.IsRunningChanged
  278. += (_, a) => { isRunningChanged++; };
  279. app.AddTimeout (TimeSpan.Zero, () => IdleExit (app));
  280. // Blocks until the timeout call is hit
  281. app.Run (t);
  282. Assert.Equal (2, isRunningChanging);
  283. Assert.Equal (2, isRunningChanged);
  284. }
  285. [Fact]
  286. public void Run_IsRunningChanging_Cancel_IsRunningChanged_Not_Raised ()
  287. {
  288. IApplication app = NewMockedApplicationImpl ()!;
  289. app.Init ("fake");
  290. var isRunningChanging = 0;
  291. var isRunningChanged = 0;
  292. Runnable<bool> t = new ();
  293. t.IsRunningChanging
  294. += (_, a) =>
  295. {
  296. // Cancel the first time
  297. if (isRunningChanging == 0)
  298. {
  299. a.Cancel = true;
  300. }
  301. isRunningChanging++;
  302. };
  303. t.IsRunningChanged
  304. += (_, a) => { isRunningChanged++; };
  305. app.AddTimeout (TimeSpan.Zero, () => IdleExit (app));
  306. // Blocks until the timeout call is hit
  307. app.Run (t);
  308. Assert.Equal (1, isRunningChanging);
  309. Assert.Equal (0, isRunningChanged);
  310. }
  311. private bool IdleExit (IApplication app)
  312. {
  313. if (app.TopRunnableView != null)
  314. {
  315. app.RequestStop ();
  316. }
  317. // Return false so the timer does not repeat
  318. return false;
  319. }
  320. [Fact]
  321. public void Open_Calls_ContinueWith_On_UIThread ()
  322. {
  323. IApplication app = NewMockedApplicationImpl ()!;
  324. app.Init ("fake");
  325. var b = new Button ();
  326. var result = false;
  327. b.Accepting +=
  328. (_, _) =>
  329. {
  330. Task.Run (() => { Task.Delay (300).Wait (); })
  331. .ContinueWith (
  332. (t, _) =>
  333. {
  334. // no longer loading
  335. app.Invoke (() =>
  336. {
  337. result = true;
  338. app.RequestStop ();
  339. });
  340. },
  341. TaskScheduler.FromCurrentSynchronizationContext ());
  342. };
  343. app.AddTimeout (
  344. TimeSpan.FromMilliseconds (150),
  345. () =>
  346. {
  347. // Run asynchronous logic inside Task.Run
  348. if (app.TopRunnableView != null)
  349. {
  350. b.NewKeyDownEvent (Key.Enter);
  351. b.NewKeyUpEvent (Key.Enter);
  352. }
  353. return false;
  354. });
  355. Assert.Null (app.TopRunnableView);
  356. var w = new Window
  357. {
  358. Title = "Open_CallsContinueWithOnUIThread"
  359. };
  360. w.Add (b);
  361. // Blocks until the timeout call is hit
  362. app.Run (w);
  363. w?.Dispose ();
  364. app.Dispose ();
  365. Assert.True (result);
  366. }
  367. [Fact]
  368. public void ApplicationImpl_UsesInstanceFields_NotStaticReferences ()
  369. {
  370. // This test verifies that ApplicationImpl uses instance fields instead of static Application references
  371. IApplication v2 = NewMockedApplicationImpl ()!;
  372. // Before Init, all fields should be null/default
  373. Assert.Null (v2.Driver);
  374. Assert.False (v2.Initialized);
  375. //Assert.Null (v2.Popover);
  376. //Assert.Null (v2.Navigation);
  377. Assert.Null (v2.TopRunnableView);
  378. Assert.Empty (v2.SessionStack!);
  379. // Init should populate instance fields
  380. v2.Init ("fake");
  381. // After Init, Driver, Navigation, and Popover should be populated
  382. Assert.NotNull (v2.Driver);
  383. Assert.True (v2.Initialized);
  384. Assert.NotNull (v2.Popover);
  385. Assert.NotNull (v2.Navigation);
  386. Assert.Null (v2.TopRunnableView); // Top is still null until Run
  387. // Shutdown should clean up instance fields
  388. v2.Dispose ();
  389. Assert.Null (v2.Driver);
  390. Assert.False (v2.Initialized);
  391. //Assert.Null (v2.Popover);
  392. //Assert.Null (v2.Navigation);
  393. Assert.Null (v2.TopRunnableView);
  394. Assert.Empty (v2.SessionStack!);
  395. }
  396. }