MessageBoxTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. #nullable enable
  2. using System.Text;
  3. using UICatalog;
  4. using UnitTests;
  5. using Xunit.Abstractions;
  6. namespace ViewsTests;
  7. public class MessageBoxTests (ITestOutputHelper output)
  8. {
  9. [Fact]
  10. public void KeyBindings_Enter_Causes_Focused_Button_Click_No_Accept ()
  11. {
  12. IApplication app = Application.Create ();
  13. app.Init ("fake");
  14. try
  15. {
  16. int? result = null;
  17. var iteration = 0;
  18. var btnAcceptCount = 0;
  19. app.Iteration += OnApplicationOnIteration;
  20. app.Run<Runnable<bool>> ();
  21. app.Iteration -= OnApplicationOnIteration;
  22. Assert.Equal (1, result);
  23. Assert.Equal (1, btnAcceptCount);
  24. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  25. {
  26. iteration++;
  27. switch (iteration)
  28. {
  29. case 1:
  30. result = MessageBox.Query (app, string.Empty, string.Empty, 0, false, "btn0", "btn1");
  31. app.RequestStop ();
  32. break;
  33. case 2:
  34. // Tab to btn2
  35. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  36. var btn = app.Navigation!.GetFocused () as Button;
  37. btn!.Accepting += (sender, e) => { btnAcceptCount++; };
  38. // Click
  39. app.Keyboard.RaiseKeyDownEvent (Key.Enter);
  40. break;
  41. default:
  42. Assert.Fail ();
  43. break;
  44. }
  45. }
  46. }
  47. finally
  48. {
  49. app.Dispose ();
  50. }
  51. }
  52. [Fact]
  53. public void KeyBindings_Esc_Closes ()
  54. {
  55. IApplication app = Application.Create ();
  56. app.Init ("fake");
  57. try
  58. {
  59. int? result = 999;
  60. var iteration = 0;
  61. app.Iteration += OnApplicationOnIteration;
  62. app.Run<Runnable<bool>> ();
  63. app.Iteration -= OnApplicationOnIteration;
  64. Assert.Null (result);
  65. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  66. {
  67. iteration++;
  68. switch (iteration)
  69. {
  70. case 1:
  71. result = MessageBox.Query (app, string.Empty, string.Empty, 0, false, "btn0", "btn1");
  72. app.RequestStop ();
  73. break;
  74. case 2:
  75. app.Keyboard.RaiseKeyDownEvent (Key.Esc);
  76. break;
  77. default:
  78. Assert.Fail ();
  79. break;
  80. }
  81. }
  82. }
  83. finally
  84. {
  85. app.Dispose ();
  86. }
  87. }
  88. [Fact]
  89. public void KeyBindings_Space_Causes_Focused_Button_Click_No_Accept ()
  90. {
  91. IApplication app = Application.Create ();
  92. app.Init ("fake");
  93. try
  94. {
  95. int? result = null;
  96. var iteration = 0;
  97. var btnAcceptCount = 0;
  98. app.Iteration += OnApplicationOnIteration;
  99. app.Run<Runnable<bool>> ();
  100. app.Iteration -= OnApplicationOnIteration;
  101. Assert.Equal (1, result);
  102. Assert.Equal (1, btnAcceptCount);
  103. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  104. {
  105. iteration++;
  106. switch (iteration)
  107. {
  108. case 1:
  109. result = MessageBox.Query (app, string.Empty, string.Empty, 0, false, "btn0", "btn1");
  110. app.RequestStop ();
  111. break;
  112. case 2:
  113. // Tab to btn2
  114. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  115. var btn = app.Navigation!.GetFocused () as Button;
  116. btn!.Accepting += (sender, e) => { btnAcceptCount++; };
  117. app.Keyboard.RaiseKeyDownEvent (Key.Space);
  118. break;
  119. default:
  120. Assert.Fail ();
  121. break;
  122. }
  123. }
  124. }
  125. finally
  126. {
  127. app.Dispose ();
  128. }
  129. }
  130. [Theory]
  131. [InlineData (@"", false, false, 6, 6, 2, 2)]
  132. [InlineData (@"", false, true, 3, 6, 9, 3)]
  133. [InlineData (@"01234\n-----\n01234", false, false, 1, 6, 13, 3)]
  134. [InlineData (@"01234\n-----\n01234", true, false, 1, 5, 13, 4)]
  135. [InlineData (@"0123456789", false, false, 1, 6, 12, 3)]
  136. [InlineData (@"0123456789", false, true, 1, 5, 12, 4)]
  137. [InlineData (@"01234567890123456789", false, true, 1, 5, 13, 4)]
  138. [InlineData (@"01234567890123456789", true, true, 1, 5, 13, 5)]
  139. [InlineData (@"01234567890123456789\n01234567890123456789", false, true, 1, 5, 13, 4)]
  140. [InlineData (@"01234567890123456789\n01234567890123456789", true, true, 1, 4, 13, 7)]
  141. public void Location_And_Size_Correct (string message, bool wrapMessage, bool hasButton, int expectedX, int expectedY, int expectedW, int expectedH)
  142. {
  143. IApplication app = Application.Create ();
  144. app.Init ("fake");
  145. try
  146. {
  147. int iterations = -1;
  148. app.Driver!.SetScreenSize (15, 15); // 15 x 15 gives us enough room for a button with one char (9x1)
  149. Dialog.DefaultShadow = ShadowStyle.None;
  150. Button.DefaultShadow = ShadowStyle.None;
  151. var mbFrame = Rectangle.Empty;
  152. app.Iteration += OnApplicationOnIteration;
  153. app.Run<Runnable<bool>> ();
  154. app.Iteration -= OnApplicationOnIteration;
  155. Assert.Equal (new (expectedX, expectedY, expectedW, expectedH), mbFrame);
  156. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  157. {
  158. iterations++;
  159. if (iterations == 0)
  160. {
  161. MessageBox.Query (app, string.Empty, message, 0, wrapMessage, hasButton ? ["0"] : []);
  162. app.RequestStop ();
  163. }
  164. else if (iterations == 1)
  165. {
  166. mbFrame = app.TopRunnableView!.Frame;
  167. app.RequestStop ();
  168. }
  169. }
  170. }
  171. finally
  172. {
  173. app.Dispose ();
  174. }
  175. }
  176. [Fact]
  177. public void Message_With_Spaces_WrapMessage_False ()
  178. {
  179. IApplication app = Application.Create ();
  180. app.Init ("fake");
  181. try
  182. {
  183. int iterations = -1;
  184. var top = new Runnable ();
  185. top.BorderStyle = LineStyle.None;
  186. app.Driver!.SetScreenSize (20, 10);
  187. var btn =
  188. $"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} btn {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}";
  189. // Override CM
  190. MessageBox.DefaultButtonAlignment = Alignment.End;
  191. MessageBox.DefaultBorderStyle = LineStyle.Double;
  192. Dialog.DefaultShadow = ShadowStyle.None;
  193. Button.DefaultShadow = ShadowStyle.None;
  194. app.Iteration += OnApplicationOnIteration;
  195. try
  196. {
  197. app.Run (top);
  198. }
  199. finally
  200. {
  201. app.Iteration -= OnApplicationOnIteration;
  202. top.Dispose ();
  203. }
  204. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  205. {
  206. iterations++;
  207. if (iterations == 0)
  208. {
  209. var sb = new StringBuilder ();
  210. for (var i = 0; i < 17; i++)
  211. {
  212. sb.Append ("ff ");
  213. }
  214. MessageBox.Query (app, string.Empty, sb.ToString (), 0, false, "btn");
  215. app.RequestStop ();
  216. }
  217. else if (iterations == 2)
  218. {
  219. DriverAssert.AssertDriverContentsWithFrameAre (
  220. @"
  221. ╔════════════════╗
  222. ║ ff ff ff ff ff ║
  223. ║ ⟦► btn ◄⟧║
  224. ╚════════════════╝",
  225. output,
  226. app.Driver);
  227. app.RequestStop ();
  228. // Really long text
  229. MessageBox.Query (app, string.Empty, new ('f', 500), 0, false, "btn");
  230. }
  231. else if (iterations == 4)
  232. {
  233. DriverAssert.AssertDriverContentsWithFrameAre (
  234. @"
  235. ╔════════════════╗
  236. ║ffffffffffffffff║
  237. ║ ⟦► btn ◄⟧║
  238. ╚════════════════╝",
  239. output,
  240. app.Driver);
  241. app.RequestStop ();
  242. }
  243. }
  244. }
  245. finally
  246. {
  247. app.Dispose ();
  248. }
  249. }
  250. [Fact]
  251. public void Message_With_Spaces_WrapMessage_True ()
  252. {
  253. IApplication app = Application.Create ();
  254. app.Init ("fake");
  255. try
  256. {
  257. int iterations = -1;
  258. var top = new Runnable ();
  259. top.BorderStyle = LineStyle.None;
  260. app.Driver!.SetScreenSize (20, 10);
  261. var btn =
  262. $"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} btn {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}";
  263. // Override CM
  264. MessageBox.DefaultButtonAlignment = Alignment.End;
  265. MessageBox.DefaultBorderStyle = LineStyle.Double;
  266. Dialog.DefaultShadow = ShadowStyle.None;
  267. Button.DefaultShadow = ShadowStyle.None;
  268. app.Iteration += OnApplicationOnIteration;
  269. try
  270. {
  271. app.Run (top);
  272. }
  273. finally
  274. {
  275. app.Iteration -= OnApplicationOnIteration;
  276. top.Dispose ();
  277. }
  278. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  279. {
  280. iterations++;
  281. if (iterations == 0)
  282. {
  283. var sb = new StringBuilder ();
  284. for (var i = 0; i < 17; i++)
  285. {
  286. sb.Append ("ff ");
  287. }
  288. MessageBox.Query (app, string.Empty, sb.ToString (), 0, true, "btn");
  289. app.RequestStop ();
  290. }
  291. else if (iterations == 2)
  292. {
  293. DriverAssert.AssertDriverContentsWithFrameAre (
  294. @"
  295. ╔══════════════╗
  296. ║ff ff ff ff ff║
  297. ║ff ff ff ff ff║
  298. ║ff ff ff ff ff║
  299. ║ ff ff ║
  300. ║ ⟦► btn ◄⟧║
  301. ╚══════════════╝",
  302. output,
  303. app.Driver);
  304. app.RequestStop ();
  305. // Really long text
  306. MessageBox.Query (app, string.Empty, new ('f', 500), 0, true, "btn");
  307. }
  308. else if (iterations == 4)
  309. {
  310. DriverAssert.AssertDriverContentsWithFrameAre (
  311. @"
  312. ╔════════════════╗
  313. ║ffffffffffffffff║
  314. ║ffffffffffffffff║
  315. ║ffffffffffffffff║
  316. ║ffffffffffffffff║
  317. ║ffffffffffffffff║
  318. ║ffffffffffffffff║
  319. ║fffffff⟦► btn ◄⟧║
  320. ╚════════════════╝",
  321. output,
  322. app.Driver);
  323. app.RequestStop ();
  324. }
  325. }
  326. }
  327. finally
  328. {
  329. app.Dispose ();
  330. }
  331. }
  332. [Theory (Skip = "Bogus test: Never does anything")]
  333. [InlineData (0, 0, "1")]
  334. [InlineData (1, 1, "1")]
  335. [InlineData (7, 5, "1")]
  336. [InlineData (50, 50, "1")]
  337. [InlineData (0, 0, "message")]
  338. [InlineData (1, 1, "message")]
  339. [InlineData (7, 5, "message")]
  340. [InlineData (50, 50, "message")]
  341. public void Size_Not_Default_Message (int height, int width, string message)
  342. {
  343. IApplication app = Application.Create ();
  344. app.Init ("fake");
  345. try
  346. {
  347. int iterations = -1;
  348. app.Driver!.SetScreenSize (100, 100);
  349. app.Iteration += (s, a) =>
  350. {
  351. iterations++;
  352. if (iterations == 0)
  353. {
  354. MessageBox.Query (app, height, width, string.Empty, message);
  355. app.RequestStop ();
  356. }
  357. else if (iterations == 1)
  358. {
  359. Assert.IsType<Dialog> (app.TopRunnableView);
  360. Assert.Equal (new (height, width), app.TopRunnableView.Frame.Size);
  361. app.RequestStop ();
  362. }
  363. };
  364. }
  365. finally
  366. {
  367. app.Dispose ();
  368. }
  369. }
  370. [Theory (Skip = "Bogus test: Never does anything")]
  371. [InlineData (0, 0, "1")]
  372. [InlineData (1, 1, "1")]
  373. [InlineData (7, 5, "1")]
  374. [InlineData (50, 50, "1")]
  375. [InlineData (0, 0, "message")]
  376. [InlineData (1, 1, "message")]
  377. [InlineData (7, 5, "message")]
  378. [InlineData (50, 50, "message")]
  379. public void Size_Not_Default_Message_Button (int height, int width, string message)
  380. {
  381. IApplication app = Application.Create ();
  382. app.Init ("fake");
  383. try
  384. {
  385. int iterations = -1;
  386. app.Iteration += (s, a) =>
  387. {
  388. iterations++;
  389. if (iterations == 0)
  390. {
  391. MessageBox.Query (app, height, width, string.Empty, message, "_Ok");
  392. app.RequestStop ();
  393. }
  394. else if (iterations == 1)
  395. {
  396. Assert.IsType<Dialog> (app.TopRunnableView);
  397. Assert.Equal (new (height, width), app.TopRunnableView.Frame.Size);
  398. app.RequestStop ();
  399. }
  400. };
  401. }
  402. finally
  403. {
  404. app.Dispose ();
  405. }
  406. }
  407. [Theory (Skip = "Bogus test: Never does anything")]
  408. [InlineData (0, 0)]
  409. [InlineData (1, 1)]
  410. [InlineData (7, 5)]
  411. [InlineData (50, 50)]
  412. public void Size_Not_Default_No_Message (int height, int width)
  413. {
  414. IApplication app = Application.Create ();
  415. app.Init ("fake");
  416. try
  417. {
  418. int iterations = -1;
  419. app.Iteration += (s, a) =>
  420. {
  421. iterations++;
  422. if (iterations == 0)
  423. {
  424. MessageBox.Query (app, height, width, string.Empty, string.Empty);
  425. app.RequestStop ();
  426. }
  427. else if (iterations == 1)
  428. {
  429. Assert.IsType<Dialog> (app.TopRunnableView);
  430. Assert.Equal (new (height, width), app.TopRunnableView.Frame.Size);
  431. app.RequestStop ();
  432. }
  433. };
  434. }
  435. finally
  436. {
  437. app.Dispose ();
  438. }
  439. }
  440. [Fact]
  441. public void UICatalog_AboutBox ()
  442. {
  443. IApplication app = Application.Create ();
  444. app.Init ("fake");
  445. try
  446. {
  447. int iterations = -1;
  448. app.Driver!.SetScreenSize (70, 15);
  449. // Override CM
  450. MessageBox.DefaultButtonAlignment = Alignment.End;
  451. MessageBox.DefaultBorderStyle = LineStyle.Double;
  452. Dialog.DefaultShadow = ShadowStyle.None;
  453. Button.DefaultShadow = ShadowStyle.None;
  454. app.Iteration += OnApplicationOnIteration;
  455. var top = new Runnable ();
  456. top.BorderStyle = LineStyle.Single;
  457. try
  458. {
  459. app.Run (top);
  460. }
  461. finally
  462. {
  463. app.Iteration -= OnApplicationOnIteration;
  464. top.Dispose ();
  465. }
  466. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  467. {
  468. iterations++;
  469. if (iterations == 0)
  470. {
  471. MessageBox.Query (
  472. app,
  473. "",
  474. UICatalogRunnable.GetAboutBoxMessage (),
  475. wrapMessage: false,
  476. buttons: "_Ok");
  477. app.RequestStop ();
  478. }
  479. else if (iterations == 2)
  480. {
  481. var expectedText = """
  482. ┌────────────────────────────────────────────────────────────────────┐
  483. │ ╔═══════════════════════════════════════════════════════════╗ │
  484. │ ║UI Catalog: A comprehensive sample library and test app for║ │
  485. │ ║ ║ │
  486. │ ║ _______ _ _ _____ _ ║ │
  487. │ ║|__ __| (_) | | / ____| (_) ║ │
  488. │ ║ | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ║ │
  489. │ ║ | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ║ │
  490. │ ║ | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ║ │
  491. │ ║ |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ║ │
  492. │ ║ ║ │
  493. │ ║ v2 - Pre-Alpha ║ │
  494. │ ║ ⟦► Ok ◄⟧║ │
  495. │ ╚═══════════════════════════════════════════════════════════╝ │
  496. └────────────────────────────────────────────────────────────────────┘
  497. """;
  498. DriverAssert.AssertDriverContentsAre (expectedText, output, app.Driver);
  499. app.RequestStop ();
  500. }
  501. }
  502. }
  503. finally
  504. {
  505. app.Dispose ();
  506. }
  507. }
  508. [Theory]
  509. [MemberData (nameof (AcceptingKeys))]
  510. public void Button_IsDefault_True_Return_His_Index_On_Accepting (Key key)
  511. {
  512. IApplication app = Application.Create ();
  513. app.Init ("fake");
  514. try
  515. {
  516. app.Iteration += OnApplicationOnIteration;
  517. int? res = MessageBox.Query (app, "hey", "IsDefault", "Yes", "No");
  518. app.Iteration -= OnApplicationOnIteration;
  519. Assert.Equal (0, res);
  520. void OnApplicationOnIteration (object? o, EventArgs<IApplication?> iterationEventArgs) { Assert.True (app.Keyboard.RaiseKeyDownEvent (key)); }
  521. }
  522. finally
  523. {
  524. app.Dispose ();
  525. }
  526. }
  527. public static IEnumerable<object []> AcceptingKeys ()
  528. {
  529. yield return [Key.Enter];
  530. yield return [Key.Space];
  531. }
  532. }