MessageBoxTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. ║ ff ff ff ff ff ║
  154. ║ ⟦► btn ◄⟧║
  155. ╚════════════════╝",
  156. _output
  157. );
  158. Application.RequestStop ();
  159. // Really long text
  160. MessageBox.Query (string.Empty, new string ('f', 500), 0, false, "btn");
  161. }
  162. else if (iterations == 2)
  163. {
  164. Application.Refresh ();
  165. TestHelpers.AssertDriverContentsWithFrameAre (
  166. @"
  167. ╔════════════════╗
  168. ║ffffffffffffffff║
  169. ║ ⟦► btn ◄⟧║
  170. ╚════════════════╝",
  171. _output
  172. );
  173. Application.RequestStop ();
  174. }
  175. };
  176. Application.Run (top);
  177. }
  178. [Fact]
  179. [AutoInitShutdown]
  180. public void Message_With_Spaces_WrapMessage_True ()
  181. {
  182. int iterations = -1;
  183. var top = new Toplevel ();
  184. top.BorderStyle = LineStyle.None;
  185. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  186. var btn =
  187. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} btn {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
  188. Application.Iteration += (s, a) =>
  189. {
  190. iterations++;
  191. if (iterations == 0)
  192. {
  193. var sb = new StringBuilder ();
  194. for (var i = 0; i < 17; i++)
  195. {
  196. sb.Append ("ff ");
  197. }
  198. MessageBox.Query (string.Empty, sb.ToString (), 0, true, "btn");
  199. Application.RequestStop ();
  200. }
  201. else if (iterations == 1)
  202. {
  203. Application.Refresh ();
  204. TestHelpers.AssertDriverContentsWithFrameAre (@"
  205. ╔══════════════╗
  206. ║ff ff ff ff ff║
  207. ║ff ff ff ff ff║
  208. ║ff ff ff ff ff║
  209. ║ ff ff ║
  210. ║ ⟦► btn ◄⟧║
  211. ╚══════════════╝",
  212. _output
  213. );
  214. Application.RequestStop ();
  215. // Really long text
  216. MessageBox.Query (string.Empty, new string ('f', 500), 0, true, "btn");
  217. }
  218. else if (iterations == 2)
  219. {
  220. Application.Refresh ();
  221. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  222. ╔════════════════╗
  223. ║ffffffffffffffff║
  224. ║ffffffffffffffff║
  225. ║ffffffffffffffff║
  226. ║ffffffffffffffff║
  227. ║ffffffffffffffff║
  228. ║ffffffffffffffff║
  229. ║fffffff⟦► btn ◄⟧║
  230. ╚════════════════╝",
  231. _output
  232. );
  233. Application.RequestStop ();
  234. }
  235. };
  236. Application.Run (top);
  237. top.Dispose ();
  238. }
  239. [Theory]
  240. [InlineData (0, 0, "1")]
  241. [InlineData (1, 1, "1")]
  242. [InlineData (7, 5, "1")]
  243. [InlineData (50, 50, "1")]
  244. [InlineData (0, 0, "message")]
  245. [InlineData (1, 1, "message")]
  246. [InlineData (7, 5, "message")]
  247. [InlineData (50, 50, "message")]
  248. [AutoInitShutdown]
  249. public void Size_Not_Default_Message (int height, int width, string message)
  250. {
  251. int iterations = -1;
  252. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  253. Application.Iteration += (s, a) =>
  254. {
  255. iterations++;
  256. if (iterations == 0)
  257. {
  258. MessageBox.Query (height, width, string.Empty, message, null);
  259. Application.RequestStop ();
  260. }
  261. else if (iterations == 1)
  262. {
  263. Application.Refresh ();
  264. Assert.IsType<Dialog> (Application.Current);
  265. Assert.Equal (new (height, width), Application.Current.Frame.Size);
  266. Application.RequestStop ();
  267. }
  268. };
  269. }
  270. [Theory]
  271. [InlineData (0, 0, "1")]
  272. [InlineData (1, 1, "1")]
  273. [InlineData (7, 5, "1")]
  274. [InlineData (50, 50, "1")]
  275. [InlineData (0, 0, "message")]
  276. [InlineData (1, 1, "message")]
  277. [InlineData (7, 5, "message")]
  278. [InlineData (50, 50, "message")]
  279. [AutoInitShutdown]
  280. public void Size_Not_Default_Message_Button (int height, int width, string message)
  281. {
  282. int iterations = -1;
  283. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  284. Application.Iteration += (s, a) =>
  285. {
  286. iterations++;
  287. if (iterations == 0)
  288. {
  289. MessageBox.Query (height, width, string.Empty, message, "_Ok");
  290. Application.RequestStop ();
  291. }
  292. else if (iterations == 1)
  293. {
  294. Application.Refresh ();
  295. Assert.IsType<Dialog> (Application.Current);
  296. Assert.Equal (new (height, width), Application.Current.Frame.Size);
  297. Application.RequestStop ();
  298. }
  299. };
  300. }
  301. [Theory]
  302. [InlineData (0, 0)]
  303. [InlineData (1, 1)]
  304. [InlineData (7, 5)]
  305. [InlineData (50, 50)]
  306. [AutoInitShutdown]
  307. public void Size_Not_Default_No_Message (int height, int width)
  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, string.Empty, null);
  317. Application.RequestStop ();
  318. }
  319. else if (iterations == 1)
  320. {
  321. Application.Refresh ();
  322. Assert.IsType<Dialog> (Application.Current);
  323. Assert.Equal (new (height, width), Application.Current.Frame.Size);
  324. Application.RequestStop ();
  325. }
  326. };
  327. }
  328. // TODO: Reimplement once messagebox ues Dim.Auto
  329. // [Fact]
  330. // [AutoInitShutdown]
  331. // public void Size_Tiny_Fixed_Size ()
  332. // {
  333. // int iterations = -1;
  334. // Application.Iteration += (s, a) =>
  335. // {
  336. // iterations++;
  337. // if (iterations == 0)
  338. // {
  339. // MessageBox.Query (7, 5, string.Empty, "Message", "_Ok");
  340. // Application.RequestStop ();
  341. // }
  342. // else if (iterations == 1)
  343. // {
  344. // Application.Refresh ();
  345. // Assert.Equal (new (7, 5), Application.Current.Frame.Size);
  346. // TestHelpers.AssertDriverContentsWithFrameAre (
  347. // @$"
  348. // ┌─────┐
  349. // │Messa│
  350. // │ ge │
  351. // │ Ok {
  352. // CM.Glyphs.RightDefaultIndicator
  353. // }│
  354. // └─────┘
  355. //",
  356. // _output
  357. // );
  358. // Application.RequestStop ();
  359. // }
  360. // };
  361. // Application.Run ().Dispose ();
  362. // }
  363. }