DialogTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  9. using Xunit.Abstractions;
  10. using NStack;
  11. using static Terminal.Gui.Application;
  12. namespace Terminal.Gui.DialogTests {
  13. public class DialogTests {
  14. readonly ITestOutputHelper output;
  15. public DialogTests (ITestOutputHelper output)
  16. {
  17. this.output = output;
  18. }
  19. //[Fact]
  20. //[AutoInitShutdown]
  21. //public void Default_Has_Border ()
  22. //{
  23. // var d = (FakeDriver)Application.Driver;
  24. // d.SetBufferSize (20, 5);
  25. // Application.RunState runstate = null;
  26. // var title = "Title";
  27. // var btnText = "ok";
  28. // var buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  29. // var width = buttonRow.Length;
  30. // var topRow = $"┌┤{title} {new string (d.HLine.ToString () [0], width - title.Length - 2)}├┐";
  31. // var bottomRow = $"└{new string (d.HLine.ToString () [0], width - 2)}┘";
  32. // var dlg = new Dialog (title, new Button (btnText));
  33. // Application.Begin (dlg);
  34. // TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  35. // Application.End (runstate);
  36. //}
  37. private (Application.RunState, Dialog) RunButtonTestDialog (string title, int width, Dialog.ButtonAlignments align, params Button [] btns)
  38. {
  39. var dlg = new Dialog (title, width, 1, btns) {
  40. ButtonAlignment = align,
  41. };
  42. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  43. dlg.BorderFrame.Thickness = new Thickness (1, 0, 1, 0);
  44. return (Application.Begin (dlg), dlg);
  45. }
  46. [Fact]
  47. [AutoInitShutdown]
  48. public void ButtonAlignment_One ()
  49. {
  50. var d = (FakeDriver)Application.Driver;
  51. Application.RunState runstate = null;
  52. var title = "1234";
  53. // E.g "|[ ok ]|"
  54. var btnText = "ok";
  55. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  56. var width = buttonRow.Length;
  57. d.SetBufferSize (width, 1);
  58. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  59. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  60. Application.End (runstate);
  61. // Justify
  62. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  63. Assert.Equal (width, buttonRow.Length);
  64. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  65. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  66. Application.End (runstate);
  67. // Right
  68. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  69. Assert.Equal (width, buttonRow.Length);
  70. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  71. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  72. Application.End (runstate);
  73. // Left
  74. buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  75. Assert.Equal (width, buttonRow.Length);
  76. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  77. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  78. Application.End (runstate);
  79. // Wider
  80. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  81. width = buttonRow.Length;
  82. d.SetBufferSize (width, 1);
  83. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  84. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  85. Application.End (runstate);
  86. // Justify
  87. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  88. Assert.Equal (width, buttonRow.Length);
  89. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  90. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  91. Application.End (runstate);
  92. // Right
  93. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  94. Assert.Equal (width, buttonRow.Length);
  95. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  96. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  97. Application.End (runstate);
  98. // Left
  99. buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  100. Assert.Equal (width, buttonRow.Length);
  101. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  102. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  103. Application.End (runstate);
  104. }
  105. [Fact]
  106. [AutoInitShutdown]
  107. public void ButtonAlignment_Two ()
  108. {
  109. Application.RunState runstate = null;
  110. var d = (FakeDriver)Application.Driver;
  111. var title = "1234";
  112. // E.g "|[ yes ][ no ]|"
  113. var btn1Text = "yes";
  114. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  115. var btn2Text = "no";
  116. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  117. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  118. var width = buttonRow.Length;
  119. d.SetBufferSize (buttonRow.Length, 3);
  120. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text));
  121. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  122. Application.End (runstate);
  123. // Justify
  124. buttonRow = $@"{d.VLine}{btn1} {btn2}{d.VLine}";
  125. Assert.Equal (width, buttonRow.Length);
  126. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text));
  127. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  128. Application.End (runstate);
  129. // Right
  130. buttonRow = $@"{d.VLine} {btn1} {btn2}{d.VLine}";
  131. Assert.Equal (width, buttonRow.Length);
  132. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
  133. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  134. Application.End (runstate);
  135. // Left
  136. buttonRow = $@"{d.VLine}{btn1} {btn2} {d.VLine}";
  137. Assert.Equal (width, buttonRow.Length);
  138. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
  139. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  140. Application.End (runstate);
  141. }
  142. [Fact]
  143. [AutoInitShutdown]
  144. public void ButtonAlignment_Two_Hidden ()
  145. {
  146. Application.RunState runstate = null;
  147. bool firstIteration = false;
  148. var d = (FakeDriver)Application.Driver;
  149. var title = "1234";
  150. // E.g "|[ yes ][ no ]|"
  151. var btn1Text = "yes";
  152. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  153. var btn2Text = "no";
  154. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  155. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  156. var width = buttonRow.Length;
  157. d.SetBufferSize (buttonRow.Length, 3);
  158. Dialog dlg = null;
  159. Button button1, button2;
  160. // Default (Center)
  161. button1 = new Button (btn1Text);
  162. button2 = new Button (btn2Text);
  163. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
  164. button1.Visible = false;
  165. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  166. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  167. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  168. Application.End (runstate);
  169. // Justify
  170. Assert.Equal (width, buttonRow.Length);
  171. button1 = new Button (btn1Text);
  172. button2 = new Button (btn2Text);
  173. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
  174. button1.Visible = false;
  175. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  176. buttonRow = $@"{d.VLine} {btn2}{d.VLine}";
  177. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  178. Application.End (runstate);
  179. // Right
  180. Assert.Equal (width, buttonRow.Length);
  181. button1 = new Button (btn1Text);
  182. button2 = new Button (btn2Text);
  183. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
  184. button1.Visible = false;
  185. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  186. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  187. Application.End (runstate);
  188. // Left
  189. Assert.Equal (width, buttonRow.Length);
  190. button1 = new Button (btn1Text);
  191. button2 = new Button (btn2Text);
  192. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
  193. button1.Visible = false;
  194. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  195. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  196. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  197. Application.End (runstate);
  198. }
  199. [Fact]
  200. [AutoInitShutdown]
  201. public void ButtonAlignment_Three ()
  202. {
  203. Application.RunState runstate = null;
  204. var d = (FakeDriver)Application.Driver;
  205. var title = "1234";
  206. // E.g "|[ yes ][ no ][ maybe ]|"
  207. var btn1Text = "yes";
  208. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  209. var btn2Text = "no";
  210. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  211. var btn3Text = "maybe";
  212. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  213. var buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3} {d.VLine}";
  214. var width = buttonRow.Length;
  215. d.SetBufferSize (buttonRow.Length, 3);
  216. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  217. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  218. Application.End (runstate);
  219. // Justify
  220. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3}{d.VLine}";
  221. Assert.Equal (width, buttonRow.Length);
  222. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  223. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  224. Application.End (runstate);
  225. // Right
  226. buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3}{d.VLine}";
  227. Assert.Equal (width, buttonRow.Length);
  228. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  229. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  230. Application.End (runstate);
  231. // Left
  232. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3} {d.VLine}";
  233. Assert.Equal (width, buttonRow.Length);
  234. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  235. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  236. Application.End (runstate);
  237. }
  238. [Fact]
  239. [AutoInitShutdown]
  240. public void ButtonAlignment_Four ()
  241. {
  242. Application.RunState runstate = null;
  243. var d = (FakeDriver)Application.Driver;
  244. var title = "1234";
  245. // E.g "|[ yes ][ no ][ maybe ]|"
  246. var btn1Text = "yes";
  247. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  248. var btn2Text = "no";
  249. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  250. var btn3Text = "maybe";
  251. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  252. var btn4Text = "never";
  253. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  254. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  255. var width = buttonRow.Length;
  256. d.SetBufferSize (buttonRow.Length, 3);
  257. // Default - Center
  258. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  259. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  260. Application.End (runstate);
  261. // Justify
  262. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  263. Assert.Equal (width, buttonRow.Length);
  264. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  265. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  266. Application.End (runstate);
  267. // Right
  268. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  269. Assert.Equal (width, buttonRow.Length);
  270. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  271. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  272. Application.End (runstate);
  273. // Left
  274. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  275. Assert.Equal (width, buttonRow.Length);
  276. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  277. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  278. Application.End (runstate);
  279. }
  280. [Fact]
  281. [AutoInitShutdown]
  282. public void ButtonAlignment_Four_On_Smaller_Width ()
  283. {
  284. Application.RunState runstate = null;
  285. var d = (FakeDriver)Application.Driver;
  286. var title = "1234";
  287. // E.g "|[ yes ][ no ][ maybe ][ never ]|"
  288. var btn1Text = "yes";
  289. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  290. var btn2Text = "no";
  291. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  292. var btn3Text = "maybe";
  293. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  294. var btn4Text = "never";
  295. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  296. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  297. var width = buttonRow.Length;
  298. d.SetBufferSize (30, 1);
  299. // Default - Center
  300. buttonRow = $"yes ] {btn2} {btn3} [ never";
  301. Assert.NotEqual (width, buttonRow.Length);
  302. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  303. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  304. Application.End (runstate);
  305. // Justify
  306. buttonRow = $"es ] {btn2} {btn3} [ neve";
  307. Assert.NotEqual (width, buttonRow.Length);
  308. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  309. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  310. // Right
  311. buttonRow = $" yes ] {btn2} {btn3} [ neve";
  312. Assert.NotEqual (width, buttonRow.Length);
  313. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  314. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  315. // Left
  316. buttonRow = $"es ] {btn2} {btn3} [ never";
  317. Assert.NotEqual (width, buttonRow.Length);
  318. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  319. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  320. }
  321. [Fact]
  322. [AutoInitShutdown]
  323. public void ButtonAlignment_Four_Wider ()
  324. {
  325. Application.RunState runstate = null;
  326. var d = (FakeDriver)Application.Driver;
  327. var title = "1234";
  328. // E.g "|[ yes ][ no ][ maybe ]|"
  329. var btn1Text = "yes";
  330. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  331. var btn2Text = "no";
  332. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  333. var btn3Text = "你你你你你"; // This is a wide char
  334. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  335. // Requires a Nerd Font
  336. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  337. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  338. // Note extra spaces to make dialog even wider
  339. // 123456 123456
  340. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  341. var width = ustring.Make (buttonRow).ConsoleWidth;
  342. d.SetBufferSize (width, 3);
  343. // Default - Center
  344. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  345. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  346. Application.End (runstate);
  347. // Justify
  348. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  349. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  350. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  351. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  352. Application.End (runstate);
  353. // Right
  354. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  355. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  356. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  357. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  358. Application.End (runstate);
  359. // Left
  360. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  361. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  362. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  363. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  364. Application.End (runstate);
  365. }
  366. [Fact]
  367. [AutoInitShutdown]
  368. public void ButtonAlignment_Four_WideOdd ()
  369. {
  370. Application.RunState runstate = null;
  371. var d = (FakeDriver)Application.Driver;
  372. var title = "1234";
  373. // E.g "|[ yes ][ no ][ maybe ]|"
  374. var btn1Text = "really long button 1";
  375. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  376. var btn2Text = "really long button 2";
  377. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  378. var btn3Text = "really long button 3";
  379. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  380. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  381. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  382. // Note extra spaces to make dialog even wider
  383. // 123456 1234567
  384. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  385. var width = buttonRow.Length;
  386. d.SetBufferSize (buttonRow.Length, 1);
  387. // Default - Center
  388. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  389. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  390. Application.End (runstate);
  391. // Justify
  392. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  393. Assert.Equal (width, buttonRow.Length);
  394. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  395. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  396. Application.End (runstate);
  397. // Right
  398. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  399. Assert.Equal (width, buttonRow.Length);
  400. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  401. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  402. Application.End (runstate);
  403. // Left
  404. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  405. Assert.Equal (width, buttonRow.Length);
  406. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  407. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  408. Application.End (runstate);
  409. }
  410. [Fact]
  411. [AutoInitShutdown]
  412. public void Zero_Buttons_Works ()
  413. {
  414. Application.RunState runstate = null;
  415. var d = (FakeDriver)Application.Driver;
  416. var title = "1234";
  417. var buttonRow = $"{d.VLine} {d.VLine}";
  418. var width = buttonRow.Length;
  419. d.SetBufferSize (buttonRow.Length, 3);
  420. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  421. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  422. Application.End (runstate);
  423. }
  424. [Fact]
  425. [AutoInitShutdown]
  426. public void One_Button_Works ()
  427. {
  428. Application.RunState runstate = null;
  429. var d = (FakeDriver)Application.Driver;
  430. var title = "1234";
  431. var btnText = "ok";
  432. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  433. var width = buttonRow.Length;
  434. d.SetBufferSize (buttonRow.Length, 3);
  435. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  436. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  437. Application.End (runstate);
  438. }
  439. [Fact]
  440. [AutoInitShutdown]
  441. public void Add_Button_Works ()
  442. {
  443. Application.RunState runstate = null;
  444. var d = (FakeDriver)Application.Driver;
  445. var title = "1234";
  446. var btn1Text = "yes";
  447. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  448. var btn2Text = "no";
  449. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  450. // We test with one button first, but do this to get the width right for 2
  451. var width = $@"{d.VLine} {btn1} {btn2} {d.VLine}".Length;
  452. d.SetBufferSize (width, 1);
  453. // Default (center)
  454. var dlg = new Dialog (title, width, 1, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Center };
  455. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  456. dlg.BorderFrame.Thickness = new Thickness (1, 0, 1, 0);
  457. runstate = Application.Begin (dlg);
  458. var buttonRow = $"{d.VLine} {btn1} {d.VLine}";
  459. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  460. // Now add a second button
  461. buttonRow = $"{d.VLine} {btn1} {btn2} {d.VLine}";
  462. dlg.AddButton (new Button (btn2Text));
  463. bool first = false;
  464. Application.RunMainLoopIteration (ref runstate, true, ref first);
  465. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  466. Application.End (runstate);
  467. // Justify
  468. dlg = new Dialog (title, width, 1, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Justify };
  469. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  470. dlg.BorderFrame.Thickness = new Thickness (1, 0, 1, 0);
  471. runstate = Application.Begin (dlg);
  472. buttonRow = $"{d.VLine} {btn1}{d.VLine}";
  473. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  474. // Now add a second button
  475. buttonRow = $"{d.VLine}{btn1} {btn2}{d.VLine}";
  476. dlg.AddButton (new Button (btn2Text));
  477. first = false;
  478. Application.RunMainLoopIteration (ref runstate, true, ref first);
  479. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  480. Application.End (runstate);
  481. // Right
  482. dlg = new Dialog (title, width, 1, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Right };
  483. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  484. dlg.BorderFrame.Thickness = new Thickness (1, 0, 1, 0);
  485. runstate = Application.Begin (dlg);
  486. buttonRow = $"{d.VLine}{new string (' ', width - btn1.Length - 2)}{btn1}{d.VLine}";
  487. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  488. // Now add a second button
  489. buttonRow = $"{d.VLine} {btn1} {btn2}{d.VLine}";
  490. dlg.AddButton (new Button (btn2Text));
  491. first = false;
  492. Application.RunMainLoopIteration (ref runstate, true, ref first);
  493. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  494. Application.End (runstate);
  495. // Left
  496. dlg = new Dialog (title, width, 1, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Left };
  497. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  498. dlg.BorderFrame.Thickness = new Thickness (1, 0, 1, 0);
  499. runstate = Application.Begin (dlg);
  500. buttonRow = $"{d.VLine}{btn1}{new string (' ', width - btn1.Length - 2)}{d.VLine}";
  501. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  502. // Now add a second button
  503. buttonRow = $"{d.VLine}{btn1} {btn2} {d.VLine}";
  504. dlg.AddButton (new Button (btn2Text));
  505. first = false;
  506. Application.RunMainLoopIteration (ref runstate, true, ref first);
  507. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  508. Application.End (runstate);
  509. }
  510. [Fact]
  511. [AutoInitShutdown]
  512. public void FileDialog_FileSystemWatcher ()
  513. {
  514. for (int i = 0; i < 8; i++) {
  515. var fd = new FileDialog ();
  516. fd.Ready += (s, e) => Application.RequestStop ();
  517. Application.Run (fd);
  518. }
  519. }
  520. [Fact, AutoInitShutdown]
  521. public void Dialog_Opened_From_Another_Dialog ()
  522. {
  523. var btn1 = new Button ("press me 1");
  524. Button btn2 = null;
  525. Button btn3 = null;
  526. string expected = null;
  527. btn1.Clicked += (s, e) => {
  528. btn2 = new Button ("Show Sub");
  529. btn3 = new Button ("Close");
  530. btn3.Clicked += (s, e) => Application.RequestStop ();
  531. btn2.Clicked += (s, e) => { MessageBox.Query ("hey", "ya", "ok"); };
  532. var dlg = new Dialog ("Hey", btn2, btn3);
  533. Application.Run (dlg);
  534. };
  535. var iterations = -1;
  536. Application.Iteration += () => {
  537. iterations++;
  538. if (iterations == 0) {
  539. Assert.True (btn1.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  540. } else if (iterations == 1) {
  541. expected = @"
  542. ┌┤Hey├─────────────────────────────────────────────────────────────┐
  543. │ │
  544. │ │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. │ │
  555. │ │
  556. │ │
  557. │ │
  558. │ │
  559. │ │
  560. │ │
  561. │ [ Show Sub ] [ Close ] │
  562. └──────────────────────────────────────────────────────────────────┘";
  563. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  564. Assert.True (btn2.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  565. } else if (iterations == 2) {
  566. TestHelpers.AssertDriverContentsWithFrameAre (@"
  567. ┌┤Hey├─────────────────────────────────────────────────────────────┐
  568. │ │
  569. │ │
  570. │ │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ ┌┤hey├─────────────────────────────────────────┐ │
  576. │ │ ya │ │
  577. │ │ │ │
  578. │ │ [◦ ok ◦] │ │
  579. │ └──────────────────────────────────────────────┘ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. │ │
  586. │ [ Show Sub ] [ Close ] │
  587. └──────────────────────────────────────────────────────────────────┘", output);
  588. Assert.True (Application.Current.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  589. } else if (iterations == 3) {
  590. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  591. Assert.True (btn3.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  592. } else if (iterations == 4) {
  593. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  594. Application.RequestStop ();
  595. }
  596. };
  597. Application.Run ();
  598. Application.Shutdown ();
  599. Assert.Equal (4, iterations);
  600. }
  601. [Fact, AutoInitShutdown]
  602. public void Dialog_In_Window_With_Size_One_Button_Aligns ()
  603. {
  604. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  605. var win = new Window ();
  606. int iterations = 0;
  607. Application.Iteration += () => {
  608. if (++iterations > 2) {
  609. Application.RequestStop ();
  610. }
  611. };
  612. win.Loaded += (s, a) => {
  613. var dlg = new Dialog ("Test", 18, 3, new Button ("Ok"));
  614. dlg.Loaded += (s, a) => {
  615. Application.Refresh ();
  616. var expected = @"
  617. ┌──────────────────┐
  618. │┌┤Test├──────────┐│
  619. ││ [ Ok ] ││
  620. │└────────────────┘│
  621. └──────────────────┘";
  622. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  623. };
  624. Application.Run (dlg);
  625. };
  626. Application.Run (win);
  627. }
  628. // [Theory, AutoInitShutdown]
  629. // [InlineData (5)]
  630. // //[InlineData (6)]
  631. // //[InlineData (7)]
  632. // //[InlineData (8)]
  633. // //[InlineData (9)]
  634. // public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height)
  635. // {
  636. // ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  637. // var win = new Window ();
  638. // Application.Iteration += () => {
  639. // var dlg = new Dialog ("Test", new Button ("Ok"));
  640. // dlg.LayoutComplete += (s, a) => {
  641. // Application.Refresh ();
  642. // // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)??
  643. // var expected = @"
  644. //┌┌┤Test├─────────┐─┐
  645. //││ │ │
  646. //││ [ Ok ] │ │
  647. //│└───────────────┘ │
  648. //└──────────────────┘";
  649. // _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  650. // dlg.RequestStop ();
  651. // win.RequestStop ();
  652. // };
  653. // Application.Run (dlg);
  654. // };
  655. // Application.Run (win);
  656. // Application.Shutdown ();
  657. // }
  658. }
  659. }