DialogTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. namespace Terminal.Gui.Views {
  12. public class DialogTests {
  13. readonly ITestOutputHelper output;
  14. public DialogTests (ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. private (Application.RunState, Dialog) RunButtonTestDialog (string title, int width, Dialog.ButtonAlignments align, params Button [] btns)
  19. {
  20. var dlg = new Dialog (title, width, 3, btns) { ButtonAlignment = align };
  21. return (Application.Begin (dlg), dlg);
  22. }
  23. [Fact]
  24. [AutoInitShutdown]
  25. public void ButtonAlignment_One ()
  26. {
  27. var d = ((FakeDriver)Application.Driver);
  28. Application.RunState runstate = null;
  29. var title = "1234";
  30. // E.g "|[ ok ]|"
  31. var btnText = "ok";
  32. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  33. var width = buttonRow.Length;
  34. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  35. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  36. d.SetBufferSize (width, 3);
  37. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  38. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  39. Application.End (runstate);
  40. // Justify
  41. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  42. Assert.Equal (width, buttonRow.Length);
  43. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  44. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  45. Application.End (runstate);
  46. // Right
  47. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  48. Assert.Equal (width, buttonRow.Length);
  49. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  50. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  51. Application.End (runstate);
  52. // Left
  53. buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  54. Assert.Equal (width, buttonRow.Length);
  55. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  56. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  57. Application.End (runstate);
  58. }
  59. [Fact]
  60. [AutoInitShutdown]
  61. public void ButtonAlignment_Two ()
  62. {
  63. Application.RunState runstate = null;
  64. var d = ((FakeDriver)Application.Driver);
  65. var title = "1234";
  66. // E.g "|[ yes ][ no ]|"
  67. var btn1Text = "yes";
  68. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  69. var btn2Text = "no";
  70. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  71. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  72. var width = buttonRow.Length;
  73. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  74. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  75. d.SetBufferSize (buttonRow.Length, 3);
  76. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text));
  77. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  78. Application.End (runstate);
  79. // Justify
  80. buttonRow = $@"{d.VLine}{btn1} {btn2}{d.VLine}";
  81. Assert.Equal (width, buttonRow.Length);
  82. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text));
  83. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  84. Application.End (runstate);
  85. // Right
  86. buttonRow = $@"{d.VLine} {btn1} {btn2}{d.VLine}";
  87. Assert.Equal (width, buttonRow.Length);
  88. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
  89. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  90. Application.End (runstate);
  91. // Left
  92. buttonRow = $@"{d.VLine}{btn1} {btn2} {d.VLine}";
  93. Assert.Equal (width, buttonRow.Length);
  94. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
  95. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  96. Application.End (runstate);
  97. }
  98. [Fact]
  99. [AutoInitShutdown]
  100. public void ButtonAlignment_Two_Hidden ()
  101. {
  102. Application.RunState runstate = null;
  103. bool firstIteration = false;
  104. var d = ((FakeDriver)Application.Driver);
  105. var title = "1234";
  106. // E.g "|[ yes ][ no ]|"
  107. var btn1Text = "yes";
  108. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  109. var btn2Text = "no";
  110. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  111. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  112. var width = buttonRow.Length;
  113. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  114. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  115. d.SetBufferSize (buttonRow.Length, 3);
  116. Dialog dlg = null;
  117. Button button1, button2;
  118. // Default (Center)
  119. button1 = new Button (btn1Text);
  120. button2 = new Button (btn2Text);
  121. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
  122. button1.Visible = false;
  123. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  124. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  125. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  126. Application.End (runstate);
  127. // Justify
  128. Assert.Equal (width, buttonRow.Length);
  129. button1 = new Button (btn1Text);
  130. button2 = new Button (btn2Text);
  131. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
  132. button1.Visible = false;
  133. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  134. buttonRow = $@"{d.VLine} {btn2}{d.VLine}";
  135. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  136. Application.End (runstate);
  137. // Right
  138. Assert.Equal (width, buttonRow.Length);
  139. button1 = new Button (btn1Text);
  140. button2 = new Button (btn2Text);
  141. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
  142. button1.Visible = false;
  143. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  144. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  145. Application.End (runstate);
  146. // Left
  147. Assert.Equal (width, buttonRow.Length);
  148. button1 = new Button (btn1Text);
  149. button2 = new Button (btn2Text);
  150. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
  151. button1.Visible = false;
  152. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  153. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  154. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  155. Application.End (runstate);
  156. }
  157. [Fact]
  158. [AutoInitShutdown]
  159. public void ButtonAlignment_Three ()
  160. {
  161. Application.RunState runstate = null;
  162. var d = ((FakeDriver)Application.Driver);
  163. var title = "1234";
  164. // E.g "|[ yes ][ no ][ maybe ]|"
  165. var btn1Text = "yes";
  166. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  167. var btn2Text = "no";
  168. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  169. var btn3Text = "maybe";
  170. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  171. var buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3} {d.VLine}";
  172. var width = buttonRow.Length;
  173. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  174. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  175. d.SetBufferSize (buttonRow.Length, 3);
  176. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  177. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  178. Application.End (runstate);
  179. // Justify
  180. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3}{d.VLine}";
  181. Assert.Equal (width, buttonRow.Length);
  182. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  183. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  184. Application.End (runstate);
  185. // Right
  186. buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3}{d.VLine}";
  187. Assert.Equal (width, buttonRow.Length);
  188. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  189. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  190. Application.End (runstate);
  191. // Left
  192. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3} {d.VLine}";
  193. Assert.Equal (width, buttonRow.Length);
  194. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  195. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  196. Application.End (runstate);
  197. }
  198. [Fact]
  199. [AutoInitShutdown]
  200. public void ButtonAlignment_Four ()
  201. {
  202. Application.RunState runstate = null;
  203. var d = ((FakeDriver)Application.Driver);
  204. var title = "1234";
  205. // E.g "|[ yes ][ no ][ maybe ]|"
  206. var btn1Text = "yes";
  207. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  208. var btn2Text = "no";
  209. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  210. var btn3Text = "maybe";
  211. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  212. var btn4Text = "never";
  213. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  214. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  215. var width = buttonRow.Length;
  216. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  217. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  218. d.SetBufferSize (buttonRow.Length, 3);
  219. // Default - Center
  220. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  221. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  222. Application.End (runstate);
  223. // Justify
  224. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  225. Assert.Equal (width, buttonRow.Length);
  226. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  227. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  228. Application.End (runstate);
  229. // Right
  230. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  231. Assert.Equal (width, buttonRow.Length);
  232. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  233. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  234. Application.End (runstate);
  235. // Left
  236. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  237. Assert.Equal (width, buttonRow.Length);
  238. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  239. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  240. Application.End (runstate);
  241. }
  242. [Fact]
  243. [AutoInitShutdown]
  244. public void ButtonAlignment_Four_On_Smaller_Width ()
  245. {
  246. Application.RunState runstate = null;
  247. var d = ((FakeDriver)Application.Driver);
  248. var title = "1234";
  249. // E.g "|[ yes ][ no ][ maybe ][ never ]|"
  250. var btn1Text = "yes";
  251. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  252. var btn2Text = "no";
  253. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  254. var btn3Text = "maybe";
  255. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  256. var btn4Text = "never";
  257. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  258. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  259. var width = buttonRow.Length;
  260. var topRow = "34 ───────────────────────────";
  261. var bottomRow = "──────────────────────────────";
  262. d.SetBufferSize (30, 3);
  263. // Default - Center
  264. buttonRow = $"yes ] {btn2} {btn3} [ never";
  265. Assert.NotEqual (width, buttonRow.Length);
  266. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  267. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  268. Application.End (runstate);
  269. // Justify
  270. buttonRow = $"es ] {btn2} {btn3} [ neve";
  271. Assert.NotEqual (width, buttonRow.Length);
  272. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  273. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  274. Application.End (runstate);
  275. // Right
  276. buttonRow = $" yes ] {btn2} {btn3} [ neve";
  277. Assert.NotEqual (width, buttonRow.Length);
  278. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  279. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  280. Application.End (runstate);
  281. // Left
  282. buttonRow = $"es ] {btn2} {btn3} [ never ";
  283. Assert.NotEqual (width, buttonRow.Length);
  284. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  285. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  286. Application.End (runstate);
  287. }
  288. [Fact]
  289. [AutoInitShutdown]
  290. public void ButtonAlignment_Four_Wider ()
  291. {
  292. Application.RunState runstate = null;
  293. var d = ((FakeDriver)Application.Driver);
  294. var title = "1234";
  295. // E.g "|[ yes ][ no ][ maybe ]|"
  296. var btn1Text = "yes";
  297. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  298. var btn2Text = "no";
  299. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  300. var btn3Text = "你你你你你"; // This is a wide char
  301. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  302. // Requires a Nerd Font
  303. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  304. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  305. // Note extra spaces to make dialog even wider
  306. // 12345 123456
  307. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  308. var width = ustring.Make (buttonRow).ConsoleWidth;
  309. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  310. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  311. d.SetBufferSize (width, 3);
  312. // Default - Center
  313. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  314. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  315. Application.End (runstate);
  316. // Justify
  317. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  318. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  319. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  320. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  321. Application.End (runstate);
  322. // Right
  323. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  324. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  325. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  326. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  327. Application.End (runstate);
  328. // Left
  329. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  330. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  331. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  332. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  333. Application.End (runstate);
  334. }
  335. [Fact]
  336. [AutoInitShutdown]
  337. public void ButtonAlignment_Four_WideOdd ()
  338. {
  339. Application.RunState runstate = null;
  340. var d = ((FakeDriver)Application.Driver);
  341. var title = "1234";
  342. // E.g "|[ yes ][ no ][ maybe ]|"
  343. var btn1Text = "really long button 1";
  344. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  345. var btn2Text = "really long button 2";
  346. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  347. var btn3Text = "really long button 3";
  348. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  349. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  350. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  351. // Note extra spaces to make dialog even wider
  352. // 12345 123456
  353. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  354. var width = buttonRow.Length;
  355. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  356. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  357. d.SetBufferSize (buttonRow.Length, 3);
  358. // Default - Center
  359. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  360. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  361. Application.End (runstate);
  362. // Justify
  363. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  364. Assert.Equal (width, buttonRow.Length);
  365. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  366. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  367. Application.End (runstate);
  368. // Right
  369. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  370. Assert.Equal (width, buttonRow.Length);
  371. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  372. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  373. Application.End (runstate);
  374. // Left
  375. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  376. Assert.Equal (width, buttonRow.Length);
  377. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  378. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  379. Application.End (runstate);
  380. }
  381. [Fact]
  382. [AutoInitShutdown]
  383. public void Zero_Buttons_Works ()
  384. {
  385. Application.RunState runstate = null;
  386. var d = ((FakeDriver)Application.Driver);
  387. var title = "1234";
  388. var buttonRow = $"{d.VLine} {d.VLine}";
  389. var width = buttonRow.Length;
  390. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  391. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  392. d.SetBufferSize (buttonRow.Length, 3);
  393. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  394. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  395. Application.End (runstate);
  396. }
  397. [Fact]
  398. [AutoInitShutdown]
  399. public void One_Button_Works ()
  400. {
  401. Application.RunState runstate = null;
  402. var d = ((FakeDriver)Application.Driver);
  403. var title = "1234";
  404. var btnText = "ok";
  405. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  406. var width = buttonRow.Length;
  407. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  408. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  409. d.SetBufferSize (buttonRow.Length, 3);
  410. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  411. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  412. Application.End (runstate);
  413. }
  414. [Fact]
  415. [AutoInitShutdown]
  416. public void Add_Button_Works ()
  417. {
  418. Application.RunState runstate = null;
  419. var d = ((FakeDriver)Application.Driver);
  420. var title = "1234";
  421. var btn1Text = "yes";
  422. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  423. var btn2Text = "no";
  424. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  425. // We test with one button first, but do this to get the width right for 2
  426. var width = $@"{d.VLine} {btn1} {btn2} {d.VLine}".Length;
  427. d.SetBufferSize (width, 3);
  428. var topRow = $"{d.ULCorner} {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}{d.URCorner}";
  429. var bottomRow = $"{d.LLCorner}{new String (d.HLine.ToString () [0], width - 2)}{d.LRCorner}";
  430. // Default (center)
  431. var dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Center };
  432. runstate = Application.Begin (dlg);
  433. var buttonRow = $"{d.VLine} {btn1} {d.VLine}";
  434. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  435. // Now add a second button
  436. buttonRow = $"{d.VLine} {btn1} {btn2} {d.VLine}";
  437. dlg.AddButton (new Button (btn2Text));
  438. bool first = false;
  439. Application.RunMainLoopIteration (ref runstate, true, ref first);
  440. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  441. Application.End (runstate);
  442. // Justify
  443. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Justify };
  444. runstate = Application.Begin (dlg);
  445. buttonRow = $"{d.VLine} {btn1}{d.VLine}";
  446. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  447. // Now add a second button
  448. buttonRow = $"{d.VLine}{btn1} {btn2}{d.VLine}";
  449. dlg.AddButton (new Button (btn2Text));
  450. first = false;
  451. Application.RunMainLoopIteration (ref runstate, true, ref first);
  452. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  453. Application.End (runstate);
  454. // Right
  455. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Right };
  456. runstate = Application.Begin (dlg);
  457. buttonRow = $"{d.VLine}{new String (' ', width - btn1.Length - 2)}{btn1}{d.VLine}";
  458. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  459. // Now add a second button
  460. buttonRow = $"{d.VLine} {btn1} {btn2}{d.VLine}";
  461. dlg.AddButton (new Button (btn2Text));
  462. first = false;
  463. Application.RunMainLoopIteration (ref runstate, true, ref first);
  464. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  465. Application.End (runstate);
  466. // Left
  467. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Left };
  468. runstate = Application.Begin (dlg);
  469. buttonRow = $"{d.VLine}{btn1}{new String (' ', width - btn1.Length - 2)}{d.VLine}";
  470. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  471. // Now add a second button
  472. buttonRow = $"{d.VLine}{btn1} {btn2} {d.VLine}";
  473. dlg.AddButton (new Button (btn2Text));
  474. first = false;
  475. Application.RunMainLoopIteration (ref runstate, true, ref first);
  476. TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  477. Application.End (runstate);
  478. }
  479. [Fact]
  480. [AutoInitShutdown]
  481. public void FileDialog_FileSystemWatcher ()
  482. {
  483. for (int i = 0; i < 8; i++) {
  484. var fd = new FileDialog ();
  485. fd.Ready += () => Application.RequestStop ();
  486. Application.Run (fd);
  487. }
  488. }
  489. }
  490. }