ScreeenTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using Xunit.Abstractions;
  2. namespace ApplicationTests.Screen;
  3. /// <summary>
  4. /// Parallelizable tests for IApplication.ScreenChanged event and Screen property.
  5. /// Tests using the modern instance-based IApplication API.
  6. /// </summary>
  7. public class ScreenTests (ITestOutputHelper output)
  8. {
  9. private readonly ITestOutputHelper _output = output;
  10. #region ScreenChanged Event Tests
  11. [Fact]
  12. public void Screen_Size_Changes ()
  13. {
  14. IApplication app = Application.Create ();
  15. app.Init ("fake");
  16. IDriver? driver = app.Driver;
  17. app.Driver!.SetScreenSize (80, 25);
  18. Assert.Equal (new (0, 0, 80, 25), driver!.Screen);
  19. Assert.Equal (new (0, 0, 80, 25), app.Screen);
  20. // TODO: Should not be possible to manually change these at whim!
  21. driver.Cols = 100;
  22. driver.Rows = 30;
  23. app.Driver!.SetScreenSize (100, 30);
  24. Assert.Equal (new (0, 0, 100, 30), driver.Screen);
  25. app.Screen = new (0, 0, driver.Cols, driver.Rows);
  26. Assert.Equal (new (0, 0, 100, 30), driver.Screen);
  27. app.Dispose ();
  28. }
  29. [Fact]
  30. public void ScreenChanged_Event_Fires_When_Driver_Size_Changes ()
  31. {
  32. // Arrange
  33. using IApplication app = Application.Create ();
  34. app.Init ("fake");
  35. var eventFired = false;
  36. Rectangle? newScreen = null;
  37. EventHandler<EventArgs<Rectangle>> handler = (sender, args) =>
  38. {
  39. eventFired = true;
  40. newScreen = args.Value;
  41. };
  42. app.ScreenChanged += handler;
  43. try
  44. {
  45. // Act
  46. app.Driver!.SetScreenSize (100, 40);
  47. // Assert
  48. Assert.True (eventFired);
  49. Assert.NotNull (newScreen);
  50. Assert.Equal (new (0, 0, 100, 40), newScreen.Value);
  51. }
  52. finally
  53. {
  54. app.ScreenChanged -= handler;
  55. }
  56. }
  57. [Fact]
  58. public void ScreenChanged_Event_Updates_Application_Screen_Property ()
  59. {
  60. // Arrange
  61. using IApplication app = Application.Create ();
  62. app.Init ("fake");
  63. Rectangle initialScreen = app.Screen;
  64. Assert.Equal (new (0, 0, 80, 25), initialScreen);
  65. // Act
  66. app.Driver!.SetScreenSize (120, 50);
  67. // Assert
  68. Assert.Equal (new (0, 0, 120, 50), app.Screen);
  69. }
  70. [Fact]
  71. public void ScreenChanged_Event_Sender_Is_IApplication ()
  72. {
  73. // Arrange
  74. using IApplication app = Application.Create ();
  75. app.Init ("fake");
  76. object? eventSender = null;
  77. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { eventSender = sender; };
  78. app.ScreenChanged += handler;
  79. try
  80. {
  81. // Act
  82. app.Driver!.SetScreenSize (100, 30);
  83. // Assert
  84. Assert.NotNull (eventSender);
  85. Assert.IsAssignableFrom<IApplication> (eventSender);
  86. }
  87. finally
  88. {
  89. app.ScreenChanged -= handler;
  90. }
  91. }
  92. [Fact]
  93. public void ScreenChanged_Event_Provides_Correct_Rectangle_In_EventArgs ()
  94. {
  95. // Arrange
  96. using IApplication app = Application.Create ();
  97. app.Init ("fake");
  98. Rectangle? capturedRectangle = null;
  99. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { capturedRectangle = args.Value; };
  100. app.ScreenChanged += handler;
  101. try
  102. {
  103. // Act
  104. app.Driver!.SetScreenSize (200, 60);
  105. // Assert
  106. Assert.NotNull (capturedRectangle);
  107. Assert.Equal (0, capturedRectangle.Value.X);
  108. Assert.Equal (0, capturedRectangle.Value.Y);
  109. Assert.Equal (200, capturedRectangle.Value.Width);
  110. Assert.Equal (60, capturedRectangle.Value.Height);
  111. }
  112. finally
  113. {
  114. app.ScreenChanged -= handler;
  115. }
  116. }
  117. [Fact]
  118. public void ScreenChanged_Event_Fires_Multiple_Times_For_Multiple_Resizes ()
  119. {
  120. // Arrange
  121. using IApplication app = Application.Create ();
  122. app.Init ("fake");
  123. var eventCount = 0;
  124. List<Size> sizes = new ();
  125. EventHandler<EventArgs<Rectangle>> handler = (sender, args) =>
  126. {
  127. eventCount++;
  128. sizes.Add (args.Value.Size);
  129. };
  130. app.ScreenChanged += handler;
  131. try
  132. {
  133. // Act
  134. app.Driver!.SetScreenSize (100, 30);
  135. app.Driver!.SetScreenSize (120, 40);
  136. app.Driver!.SetScreenSize (80, 25);
  137. // Assert
  138. Assert.Equal (3, eventCount);
  139. Assert.Equal (3, sizes.Count);
  140. Assert.Equal (new (100, 30), sizes [0]);
  141. Assert.Equal (new (120, 40), sizes [1]);
  142. Assert.Equal (new (80, 25), sizes [2]);
  143. }
  144. finally
  145. {
  146. app.ScreenChanged -= handler;
  147. }
  148. }
  149. [Fact]
  150. public void ScreenChanged_Event_Does_Not_Fire_When_No_Resize_Occurs ()
  151. {
  152. // Arrange
  153. using IApplication app = Application.Create ();
  154. app.Init ("fake");
  155. var eventFired = false;
  156. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { eventFired = true; };
  157. app.ScreenChanged += handler;
  158. try
  159. {
  160. // Act - Don't resize, just access Screen property
  161. Rectangle screen = app.Screen;
  162. // Assert
  163. Assert.False (eventFired);
  164. Assert.Equal (new (0, 0, 80, 25), screen);
  165. }
  166. finally
  167. {
  168. app.ScreenChanged -= handler;
  169. }
  170. }
  171. [Fact]
  172. public void ScreenChanged_Event_Can_Be_Unsubscribed ()
  173. {
  174. // Arrange
  175. using IApplication app = Application.Create ();
  176. app.Init ("fake");
  177. var eventCount = 0;
  178. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { eventCount++; };
  179. app.ScreenChanged += handler;
  180. // Act - First resize should fire
  181. app.Driver!.SetScreenSize (100, 30);
  182. Assert.Equal (1, eventCount);
  183. // Unsubscribe
  184. app.ScreenChanged -= handler;
  185. // Second resize should not fire
  186. app.Driver!.SetScreenSize (120, 40);
  187. // Assert
  188. Assert.Equal (1, eventCount);
  189. }
  190. [Fact]
  191. public void ScreenChanged_Event_Sets_Runnables_To_NeedsLayout ()
  192. {
  193. // Arrange
  194. using IApplication app = Application.Create ();
  195. app.Init ("fake");
  196. using var runnable = new Runnable ();
  197. SessionToken? token = app.Begin (runnable);
  198. Assert.NotNull (app.TopRunnableView);
  199. app.LayoutAndDraw ();
  200. // Clear the NeedsLayout flag
  201. Assert.False (app.TopRunnableView.NeedsLayout);
  202. try
  203. {
  204. // Act
  205. app.Driver!.SetScreenSize (100, 30);
  206. // Assert
  207. Assert.True (app.TopRunnableView.NeedsLayout);
  208. }
  209. finally
  210. {
  211. // Cleanup
  212. if (token is { })
  213. {
  214. app.End (token);
  215. }
  216. }
  217. }
  218. [Fact]
  219. public void ScreenChanged_Event_Handles_Multiple_Runnables_In_Session_Stack ()
  220. {
  221. // Arrange
  222. using IApplication app = Application.Create ();
  223. app.Init ("fake");
  224. using var runnable1 = new Runnable ();
  225. SessionToken? token1 = app.Begin (runnable1);
  226. app.LayoutAndDraw ();
  227. using var runnable2 = new Runnable ();
  228. SessionToken? token2 = app.Begin (runnable2);
  229. app.LayoutAndDraw ();
  230. // Both should not need layout after drawing
  231. Assert.False (runnable1.NeedsLayout);
  232. Assert.False (runnable2.NeedsLayout);
  233. try
  234. {
  235. // Act - Resize should mark both as needing layout
  236. app.Driver!.SetScreenSize (100, 30);
  237. // Assert
  238. Assert.True (runnable1.NeedsLayout);
  239. Assert.True (runnable2.NeedsLayout);
  240. }
  241. finally
  242. {
  243. // Cleanup
  244. if (token2 is { })
  245. {
  246. app.End (token2);
  247. }
  248. if (token1 is { })
  249. {
  250. app.End (token1);
  251. }
  252. }
  253. }
  254. [Fact]
  255. public void ScreenChanged_Event_With_No_Active_Runnables_Does_Not_Throw ()
  256. {
  257. // Arrange
  258. using IApplication app = Application.Create ();
  259. app.Init ("fake");
  260. var eventFired = false;
  261. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { eventFired = true; };
  262. app.ScreenChanged += handler;
  263. try
  264. {
  265. // Act - Resize with no runnables
  266. Exception? exception = Record.Exception (() => app.Driver!.SetScreenSize (100, 30));
  267. // Assert
  268. Assert.Null (exception);
  269. Assert.True (eventFired);
  270. }
  271. finally
  272. {
  273. app.ScreenChanged -= handler;
  274. }
  275. }
  276. #endregion ScreenChanged Event Tests
  277. #region Screen Property Tests
  278. [Fact]
  279. public void Screen_Property_Returns_Driver_Screen_When_Not_Set ()
  280. {
  281. // Arrange
  282. using IApplication app = Application.Create ();
  283. app.Init ("fake");
  284. // Act
  285. Rectangle screen = app.Screen;
  286. // Assert
  287. Assert.Equal (app.Driver!.Screen, screen);
  288. Assert.Equal (new (0, 0, 80, 25), screen);
  289. }
  290. [Fact]
  291. public void Screen_Property_Returns_Default_Size_When_Driver_Not_Initialized ()
  292. {
  293. // Arrange
  294. using IApplication app = Application.Create ();
  295. // Act - Don't call Init
  296. Rectangle screen = app.Screen;
  297. // Assert - Should return default size
  298. Assert.Equal (new (0, 0, 2048, 2048), screen);
  299. }
  300. [Fact]
  301. public void Screen_Property_Throws_When_Setting_Non_Zero_Origin ()
  302. {
  303. // Arrange
  304. using IApplication app = Application.Create ();
  305. app.Init ("fake");
  306. // Act & Assert
  307. var exception = Assert.Throws<NotImplementedException> (() =>
  308. app.Screen = new (10, 10, 80, 25));
  309. Assert.Contains ("Screen locations other than 0, 0", exception.Message);
  310. }
  311. [Fact]
  312. public void Screen_Property_Allows_Setting_With_Zero_Origin ()
  313. {
  314. // Arrange
  315. using IApplication app = Application.Create ();
  316. app.Init ("fake");
  317. // Act
  318. Exception? exception = Record.Exception (() =>
  319. app.Screen = new (0, 0, 100, 50));
  320. // Assert
  321. Assert.Null (exception);
  322. Assert.Equal (new (0, 0, 100, 50), app.Screen);
  323. }
  324. [Fact]
  325. public void Screen_Property_Setting_Raises_ScreenChanged_Event ()
  326. {
  327. // Arrange
  328. using IApplication app = Application.Create ();
  329. app.Init ("fake");
  330. var eventFired = false;
  331. EventHandler<EventArgs<Rectangle>> handler = (sender, args) => { eventFired = true; };
  332. app.ScreenChanged += handler;
  333. try
  334. {
  335. // Act - Manually set Screen property
  336. app.Screen = new (0, 0, 100, 50);
  337. Assert.True (eventFired);
  338. Assert.Equal (new (0, 0, 100, 50), app.Screen);
  339. }
  340. finally
  341. {
  342. app.ScreenChanged -= handler;
  343. }
  344. }
  345. [Fact]
  346. public void Screen_Property_Thread_Safe_Access ()
  347. {
  348. // Arrange
  349. using IApplication app = Application.Create ();
  350. app.Init ("fake");
  351. List<Exception> exceptions = new ();
  352. List<Task> tasks = new ();
  353. // Act - Access Screen property from multiple threads
  354. for (var i = 0; i < 10; i++)
  355. {
  356. tasks.Add (
  357. Task.Run (() =>
  358. {
  359. try
  360. {
  361. Rectangle screen = app.Screen;
  362. Assert.NotEqual (Rectangle.Empty, screen);
  363. }
  364. catch (Exception ex)
  365. {
  366. lock (exceptions)
  367. {
  368. exceptions.Add (ex);
  369. }
  370. }
  371. }));
  372. }
  373. #pragma warning disable xUnit1031
  374. Task.WaitAll (tasks.ToArray ());
  375. #pragma warning restore xUnit1031
  376. // Assert - No exceptions should occur
  377. Assert.Empty (exceptions);
  378. }
  379. #endregion Screen Property Tests
  380. }