MessageBoxTests.cs 22 KB

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