ApplicationV2Tests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. using System.Runtime.CompilerServices;
  4. using Microsoft.Extensions.Logging;
  5. using Moq;
  6. namespace UnitTests.ConsoleDrivers.V2;
  7. public class ApplicationV2Tests
  8. {
  9. private ApplicationV2 NewApplicationV2 ()
  10. {
  11. var netInput = new Mock<INetInput> ();
  12. SetupRunInputMockMethodToBlock (netInput);
  13. var winInput = new Mock<IWindowsInput> ();
  14. SetupRunInputMockMethodToBlock (winInput);
  15. return new (
  16. () => netInput.Object,
  17. Mock.Of<IConsoleOutput>,
  18. () => winInput.Object,
  19. Mock.Of<IConsoleOutput>);
  20. }
  21. [Fact]
  22. public void Init_CreatesKeybindings ()
  23. {
  24. var v2 = NewApplicationV2 ();
  25. Application.KeyBindings.Clear ();
  26. Assert.Empty (Application.KeyBindings.GetBindings ());
  27. v2.Init ();
  28. Assert.NotEmpty (Application.KeyBindings.GetBindings ());
  29. v2.Shutdown ();
  30. }
  31. [Fact]
  32. public void Init_DriverIsFacade ()
  33. {
  34. var v2 = NewApplicationV2 ();
  35. Assert.Null (Application.Driver);
  36. v2.Init ();
  37. Assert.NotNull (Application.Driver);
  38. var type = Application.Driver.GetType ();
  39. Assert.True (type.IsGenericType);
  40. Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
  41. v2.Shutdown ();
  42. Assert.Null (Application.Driver);
  43. }
  44. [Fact]
  45. public void Init_ExplicitlyRequestWin ()
  46. {
  47. Assert.Null (Application.Driver);
  48. var netInput = new Mock<INetInput> (MockBehavior.Strict);
  49. var netOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
  50. var winInput = new Mock<IWindowsInput> (MockBehavior.Strict);
  51. var winOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
  52. winInput.Setup (i => i.Initialize (It.IsAny<ConcurrentQueue<WindowsConsole.InputRecord>> ()))
  53. .Verifiable (Times.Once);
  54. SetupRunInputMockMethodToBlock (winInput);
  55. winInput.Setup (i => i.Dispose ())
  56. .Verifiable (Times.Once);
  57. winOutput.Setup (i => i.Dispose ())
  58. .Verifiable (Times.Once);
  59. var v2 = new ApplicationV2 (
  60. () => netInput.Object,
  61. () => netOutput.Object,
  62. () => winInput.Object,
  63. () => winOutput.Object);
  64. Assert.Null (Application.Driver);
  65. v2.Init (null, "v2win");
  66. Assert.NotNull (Application.Driver);
  67. var type = Application.Driver.GetType ();
  68. Assert.True (type.IsGenericType);
  69. Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
  70. v2.Shutdown ();
  71. Assert.Null (Application.Driver);
  72. winInput.VerifyAll ();
  73. }
  74. [Fact]
  75. public void Init_ExplicitlyRequestNet ()
  76. {
  77. var netInput = new Mock<INetInput> (MockBehavior.Strict);
  78. var netOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
  79. var winInput = new Mock<IWindowsInput> (MockBehavior.Strict);
  80. var winOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
  81. netInput.Setup (i => i.Initialize (It.IsAny<ConcurrentQueue<ConsoleKeyInfo>> ()))
  82. .Verifiable (Times.Once);
  83. SetupRunInputMockMethodToBlock (netInput);
  84. netInput.Setup (i => i.Dispose ())
  85. .Verifiable (Times.Once);
  86. netOutput.Setup (i => i.Dispose ())
  87. .Verifiable (Times.Once);
  88. var v2 = new ApplicationV2 (
  89. () => netInput.Object,
  90. () => netOutput.Object,
  91. () => winInput.Object,
  92. () => winOutput.Object);
  93. Assert.Null (Application.Driver);
  94. v2.Init (null, "v2net");
  95. Assert.NotNull (Application.Driver);
  96. var type = Application.Driver.GetType ();
  97. Assert.True (type.IsGenericType);
  98. Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
  99. v2.Shutdown ();
  100. Assert.Null (Application.Driver);
  101. netInput.VerifyAll ();
  102. }
  103. private void SetupRunInputMockMethodToBlock (Mock<IWindowsInput> winInput)
  104. {
  105. winInput.Setup (r => r.Run (It.IsAny<CancellationToken> ()))
  106. .Callback<CancellationToken> (token =>
  107. {
  108. // Simulate an infinite loop that checks for cancellation
  109. while (!token.IsCancellationRequested)
  110. {
  111. // Perform the action that should repeat in the loop
  112. // This could be some mock behavior or just an empty loop depending on the context
  113. }
  114. })
  115. .Verifiable (Times.Once);
  116. }
  117. private void SetupRunInputMockMethodToBlock (Mock<INetInput> netInput)
  118. {
  119. netInput.Setup (r => r.Run (It.IsAny<CancellationToken> ()))
  120. .Callback<CancellationToken> (token =>
  121. {
  122. // Simulate an infinite loop that checks for cancellation
  123. while (!token.IsCancellationRequested)
  124. {
  125. // Perform the action that should repeat in the loop
  126. // This could be some mock behavior or just an empty loop depending on the context
  127. }
  128. })
  129. .Verifiable (Times.Once);
  130. }
  131. [Fact]
  132. public void NoInitThrowOnRun ()
  133. {
  134. Assert.Null (Application.Driver);
  135. var app = NewApplicationV2 ();
  136. var ex = Assert.Throws<NotInitializedException> (() => app.Run (new Window ()));
  137. Assert.Equal ("Run cannot be accessed before Initialization", ex.Message);
  138. app.Shutdown();
  139. }
  140. [Fact]
  141. public void InitRunShutdown_Top_Set_To_Null_After_Shutdown ()
  142. {
  143. var orig = ApplicationImpl.Instance;
  144. var v2 = NewApplicationV2 ();
  145. ApplicationImpl.ChangeInstance (v2);
  146. v2.Init ();
  147. var timeoutToken = v2.AddTimeout (TimeSpan.FromMilliseconds (150),
  148. () =>
  149. {
  150. if (Application.Top != null)
  151. {
  152. Application.RequestStop ();
  153. return true;
  154. }
  155. return true;
  156. }
  157. );
  158. Assert.Null (Application.Top);
  159. // Blocks until the timeout call is hit
  160. v2.Run (new Window ());
  161. Assert.True (v2.RemoveTimeout (timeoutToken));
  162. Assert.NotNull (Application.Top);
  163. Application.Top?.Dispose ();
  164. v2.Shutdown ();
  165. Assert.Null (Application.Top);
  166. ApplicationImpl.ChangeInstance (orig);
  167. }
  168. [Fact]
  169. public void InitRunShutdown_Running_Set_To_False ()
  170. {
  171. var orig = ApplicationImpl.Instance;
  172. var v2 = NewApplicationV2 ();
  173. ApplicationImpl.ChangeInstance (v2);
  174. v2.Init ();
  175. Toplevel top = new Window ()
  176. {
  177. Title = "InitRunShutdown_Running_Set_To_False"
  178. };
  179. var timeoutToken = v2.AddTimeout (TimeSpan.FromMilliseconds (150),
  180. () =>
  181. {
  182. Assert.True (top!.Running);
  183. if (Application.Top != null)
  184. {
  185. Application.RequestStop ();
  186. return true;
  187. }
  188. return true;
  189. }
  190. );
  191. Assert.False (top!.Running);
  192. // Blocks until the timeout call is hit
  193. v2.Run (top);
  194. Assert.True (v2.RemoveTimeout (timeoutToken));
  195. Assert.False (top!.Running);
  196. // BUGBUG: Shutdown sets Top to null, not End.
  197. //Assert.Null (Application.Top);
  198. Application.Top?.Dispose ();
  199. v2.Shutdown ();
  200. ApplicationImpl.ChangeInstance (orig);
  201. }
  202. [Fact]
  203. public void InitRunShutdown_End_Is_Called ()
  204. {
  205. var orig = ApplicationImpl.Instance;
  206. var v2 = NewApplicationV2 ();
  207. ApplicationImpl.ChangeInstance (v2);
  208. Assert.Null (Application.Top);
  209. Assert.Null (Application.Driver);
  210. v2.Init ();
  211. Toplevel top = new Window ();
  212. // BUGBUG: Both Closed and Unloaded are called from End; what's the difference?
  213. int closedCount = 0;
  214. top.Closed
  215. += (_, a) =>
  216. {
  217. closedCount++;
  218. };
  219. int unloadedCount = 0;
  220. top.Unloaded
  221. += (_, a) =>
  222. {
  223. unloadedCount++;
  224. };
  225. var timeoutToken = v2.AddTimeout (TimeSpan.FromMilliseconds (150),
  226. () =>
  227. {
  228. Assert.True (top!.Running);
  229. if (Application.Top != null)
  230. {
  231. Application.RequestStop ();
  232. return true;
  233. }
  234. return true;
  235. }
  236. );
  237. Assert.Equal (0, closedCount);
  238. Assert.Equal (0, unloadedCount);
  239. // Blocks until the timeout call is hit
  240. v2.Run (top);
  241. Assert.Equal (1, closedCount);
  242. Assert.Equal (1, unloadedCount);
  243. Assert.True (v2.RemoveTimeout (timeoutToken));
  244. Application.Top?.Dispose ();
  245. v2.Shutdown ();
  246. Assert.Equal (1, closedCount);
  247. Assert.Equal (1, unloadedCount);
  248. ApplicationImpl.ChangeInstance (orig);
  249. }
  250. [Fact]
  251. public void InitRunShutdown_QuitKey_Quits ()
  252. {
  253. var orig = ApplicationImpl.Instance;
  254. var v2 = NewApplicationV2 ();
  255. ApplicationImpl.ChangeInstance (v2);
  256. v2.Init ();
  257. Toplevel top = new Window ()
  258. {
  259. Title = "InitRunShutdown_QuitKey_Quits"
  260. };
  261. var timeoutToken = v2.AddTimeout (TimeSpan.FromMilliseconds (150),
  262. () =>
  263. {
  264. Assert.True (top!.Running);
  265. if (Application.Top != null)
  266. {
  267. Application.RaiseKeyDownEvent (Application.QuitKey);
  268. return true;
  269. }
  270. return true;
  271. }
  272. );
  273. Assert.False (top!.Running);
  274. // Blocks until the timeout call is hit
  275. v2.Run (top);
  276. Assert.True (v2.RemoveTimeout (timeoutToken));
  277. Assert.False (top!.Running);
  278. Assert.NotNull (Application.Top);
  279. top.Dispose ();
  280. v2.Shutdown ();
  281. Assert.Null (Application.Top);
  282. ApplicationImpl.ChangeInstance (orig);
  283. }
  284. [Fact]
  285. public void InitRunShutdown_Generic_IdleForExit ()
  286. {
  287. var orig = ApplicationImpl.Instance;
  288. var v2 = NewApplicationV2 ();
  289. ApplicationImpl.ChangeInstance (v2);
  290. v2.Init ();
  291. v2.AddIdle (IdleExit);
  292. Assert.Null (Application.Top);
  293. // Blocks until the timeout call is hit
  294. v2.Run<Window> ();
  295. Assert.NotNull (Application.Top);
  296. Application.Top?.Dispose ();
  297. v2.Shutdown ();
  298. Assert.Null (Application.Top);
  299. ApplicationImpl.ChangeInstance (orig);
  300. }
  301. [Fact]
  302. public void Shutdown_Closing_Closed_Raised ()
  303. {
  304. var orig = ApplicationImpl.Instance;
  305. var v2 = NewApplicationV2 ();
  306. ApplicationImpl.ChangeInstance (v2);
  307. v2.Init ();
  308. int closing = 0;
  309. int closed = 0;
  310. var t = new Toplevel ();
  311. t.Closing
  312. += (_, a) =>
  313. {
  314. // Cancel the first time
  315. if (closing == 0)
  316. {
  317. a.Cancel = true;
  318. }
  319. closing++;
  320. Assert.Same (t, a.RequestingTop);
  321. };
  322. t.Closed
  323. += (_, a) =>
  324. {
  325. closed++;
  326. Assert.Same (t, a.Toplevel);
  327. };
  328. v2.AddIdle (IdleExit);
  329. // Blocks until the timeout call is hit
  330. v2.Run (t);
  331. Application.Top?.Dispose ();
  332. v2.Shutdown ();
  333. ApplicationImpl.ChangeInstance (orig);
  334. Assert.Equal (2, closing);
  335. Assert.Equal (1, closed);
  336. }
  337. private bool IdleExit ()
  338. {
  339. if (Application.Top != null)
  340. {
  341. Application.RequestStop ();
  342. return true;
  343. }
  344. return true;
  345. }
  346. [Fact]
  347. public void Shutdown_Called_Repeatedly_DoNotDuplicateDisposeOutput ()
  348. {
  349. var netInput = new Mock<INetInput> ();
  350. SetupRunInputMockMethodToBlock (netInput);
  351. Mock<IConsoleOutput>? outputMock = null;
  352. var v2 = new ApplicationV2 (
  353. () => netInput.Object,
  354. () => (outputMock = new Mock<IConsoleOutput> ()).Object,
  355. Mock.Of<IWindowsInput>,
  356. Mock.Of<IConsoleOutput>);
  357. v2.Init (null, "v2net");
  358. v2.Shutdown ();
  359. v2.Shutdown ();
  360. outputMock!.Verify (o => o.Dispose (), Times.Once);
  361. }
  362. [Fact]
  363. public void Init_Called_Repeatedly_WarnsAndIgnores ()
  364. {
  365. var v2 = NewApplicationV2 ();
  366. Assert.Null (Application.Driver);
  367. v2.Init ();
  368. Assert.NotNull (Application.Driver);
  369. var mockLogger = new Mock<ILogger> ();
  370. var beforeLogger = Logging.Logger;
  371. Logging.Logger = mockLogger.Object;
  372. v2.Init ();
  373. v2.Init ();
  374. mockLogger.Verify (
  375. l => l.Log (LogLevel.Error,
  376. It.IsAny<EventId> (),
  377. It.Is<It.IsAnyType> ((v, t) => v.ToString () == "Init called multiple times without shutdown, ignoring."),
  378. It.IsAny<Exception> (),
  379. It.IsAny<Func<It.IsAnyType, Exception, string>> ()!)
  380. , Times.Exactly (2));
  381. v2.Shutdown ();
  382. // Restore the original null logger to be polite to other tests
  383. Logging.Logger = beforeLogger;
  384. }
  385. // QUESTION: What does this test really test? It's poorly named.
  386. [Fact]
  387. public void Open_CallsContinueWithOnUIThread ()
  388. {
  389. var orig = ApplicationImpl.Instance;
  390. var v2 = NewApplicationV2 ();
  391. ApplicationImpl.ChangeInstance (v2);
  392. v2.Init ();
  393. var b = new Button ();
  394. bool result = false;
  395. b.Accepting +=
  396. (_, _) =>
  397. {
  398. Task.Run (() =>
  399. {
  400. Task.Delay (300).Wait ();
  401. }).ContinueWith (
  402. (t, _) =>
  403. {
  404. // no longer loading
  405. Application.Invoke (() =>
  406. {
  407. result = true;
  408. Application.RequestStop ();
  409. });
  410. },
  411. TaskScheduler.FromCurrentSynchronizationContext ());
  412. };
  413. v2.AddTimeout (TimeSpan.FromMilliseconds (150),
  414. () =>
  415. {
  416. // Run asynchronous logic inside Task.Run
  417. if (Application.Top != null)
  418. {
  419. b.NewKeyDownEvent (Key.Enter);
  420. b.NewKeyUpEvent (Key.Enter);
  421. return false;
  422. }
  423. return true;
  424. });
  425. Assert.Null (Application.Top);
  426. var w = new Window ()
  427. {
  428. Title = "Open_CallsContinueWithOnUIThread"
  429. };
  430. w.Add (b);
  431. // Blocks until the timeout call is hit
  432. v2.Run (w);
  433. Assert.NotNull (Application.Top);
  434. Application.Top?.Dispose ();
  435. v2.Shutdown ();
  436. Assert.Null (Application.Top);
  437. ApplicationImpl.ChangeInstance (orig);
  438. Assert.True (result);
  439. }
  440. }