MessageBoxTests.cs 20 KB

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