MessageBoxTests.cs 22 KB

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