DialogTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. GraphViewTests.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. //GraphViewTests.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. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  136. Application.End (runstate);
  137. //// Right
  138. //buttonRow = $@"{d.VLine} {btn1} {btn2}{d.VLine}";
  139. //Assert.Equal (width, buttonRow.Length);
  140. //(runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
  141. //GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  142. //Application.End (runstate);
  143. //// Left
  144. //buttonRow = $@"{d.VLine}{btn1} {btn2} {d.VLine}";
  145. //Assert.Equal (width, buttonRow.Length);
  146. //(runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
  147. //GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  148. //Application.End (runstate);
  149. }
  150. [Fact]
  151. [AutoInitShutdown]
  152. public void ButtonAlignment_Three ()
  153. {
  154. Application.RunState runstate = null;
  155. var d = ((FakeDriver)Application.Driver);
  156. var title = "1234";
  157. // E.g "|[ yes ][ no ][ maybe ]|"
  158. var btn1Text = "yes";
  159. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  160. var btn2Text = "no";
  161. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  162. var btn3Text = "maybe";
  163. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  164. var buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3} {d.VLine}";
  165. var width = buttonRow.Length;
  166. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  167. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  168. d.SetBufferSize (buttonRow.Length, 3);
  169. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  170. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  171. Application.End (runstate);
  172. // Justify
  173. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3}{d.VLine}";
  174. Assert.Equal (width, buttonRow.Length);
  175. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  176. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  177. Application.End (runstate);
  178. // Right
  179. buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3}{d.VLine}";
  180. Assert.Equal (width, buttonRow.Length);
  181. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  182. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  183. Application.End (runstate);
  184. // Left
  185. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3} {d.VLine}";
  186. Assert.Equal (width, buttonRow.Length);
  187. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  188. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  189. Application.End (runstate);
  190. }
  191. [Fact]
  192. [AutoInitShutdown]
  193. public void ButtonAlignment_Four ()
  194. {
  195. Application.RunState runstate = null;
  196. var d = ((FakeDriver)Application.Driver);
  197. var title = "1234";
  198. // E.g "|[ yes ][ no ][ maybe ]|"
  199. var btn1Text = "yes";
  200. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  201. var btn2Text = "no";
  202. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  203. var btn3Text = "maybe";
  204. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  205. var btn4Text = "never";
  206. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  207. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  208. var width = buttonRow.Length;
  209. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  210. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  211. d.SetBufferSize (buttonRow.Length, 3);
  212. // Default - Center
  213. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  214. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  215. Application.End (runstate);
  216. // Justify
  217. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  218. Assert.Equal (width, buttonRow.Length);
  219. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  220. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  221. Application.End (runstate);
  222. // Right
  223. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  224. Assert.Equal (width, buttonRow.Length);
  225. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  226. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  227. Application.End (runstate);
  228. // Left
  229. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  230. Assert.Equal (width, buttonRow.Length);
  231. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  232. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  233. Application.End (runstate);
  234. }
  235. [Fact]
  236. [AutoInitShutdown]
  237. public void ButtonAlignment_Four_Wider ()
  238. {
  239. Application.RunState runstate = null;
  240. var d = ((FakeDriver)Application.Driver);
  241. var title = "1234";
  242. // E.g "|[ yes ][ no ][ maybe ]|"
  243. var btn1Text = "yes";
  244. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  245. var btn2Text = "no";
  246. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  247. var btn3Text = "你你你你你"; // This is a wide char
  248. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  249. // Requires a Nerd Font
  250. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  251. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  252. // Note extra spaces to make dialog even wider
  253. // 12345 123456
  254. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  255. var width = ustring.Make (buttonRow).ConsoleWidth;
  256. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  257. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  258. d.SetBufferSize (width, 3);
  259. // Default - Center
  260. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  261. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  262. Application.End (runstate);
  263. // Justify
  264. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  265. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  266. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  267. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  268. Application.End (runstate);
  269. // Right
  270. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  271. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  272. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  273. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  274. Application.End (runstate);
  275. // Left
  276. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  277. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  278. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  279. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  280. Application.End (runstate);
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void ButtonAlignment_Four_WideOdd ()
  285. {
  286. Application.RunState runstate = null;
  287. var d = ((FakeDriver)Application.Driver);
  288. var title = "1234";
  289. // E.g "|[ yes ][ no ][ maybe ]|"
  290. var btn1Text = "really long button 1";
  291. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  292. var btn2Text = "really long button 2";
  293. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  294. var btn3Text = "really long button 3";
  295. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  296. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  297. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  298. // Note extra spaces to make dialog even wider
  299. // 12345 123456
  300. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  301. var width = buttonRow.Length;
  302. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  303. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  304. d.SetBufferSize (buttonRow.Length, 3);
  305. // Default - Center
  306. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  307. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  308. Application.End (runstate);
  309. // Justify
  310. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  311. Assert.Equal (width, buttonRow.Length);
  312. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  313. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  314. Application.End (runstate);
  315. // Right
  316. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  317. Assert.Equal (width, buttonRow.Length);
  318. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  319. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  320. Application.End (runstate);
  321. // Left
  322. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  323. Assert.Equal (width, buttonRow.Length);
  324. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  325. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  326. Application.End (runstate);
  327. }
  328. [Fact]
  329. [AutoInitShutdown]
  330. public void Zero_Buttons_Works ()
  331. {
  332. Application.RunState runstate = null;
  333. var d = ((FakeDriver)Application.Driver);
  334. var title = "1234";
  335. var buttonRow = $"{d.VLine} {d.VLine}";
  336. var width = buttonRow.Length;
  337. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  338. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  339. d.SetBufferSize (buttonRow.Length, 3);
  340. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  341. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  342. Application.End (runstate);
  343. }
  344. [Fact]
  345. [AutoInitShutdown]
  346. public void One_Button_Works ()
  347. {
  348. Application.RunState runstate = null;
  349. var d = ((FakeDriver)Application.Driver);
  350. var title = "1234";
  351. var btnText = "ok";
  352. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  353. var width = buttonRow.Length;
  354. var topRow = $"┌ {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}┐";
  355. var bottomRow = $"└{new String (d.HLine.ToString () [0], width - 2)}┘";
  356. d.SetBufferSize (buttonRow.Length, 3);
  357. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  358. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  359. Application.End (runstate);
  360. }
  361. [Fact]
  362. [AutoInitShutdown]
  363. public void Add_Button_Works ()
  364. {
  365. Application.RunState runstate = null;
  366. var d = ((FakeDriver)Application.Driver);
  367. var title = "1234";
  368. var btn1Text = "yes";
  369. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  370. var btn2Text = "no";
  371. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  372. // We test with one button first, but do this to get the width right for 2
  373. var width = $@"{d.VLine} {btn1} {btn2} {d.VLine}".Length;
  374. d.SetBufferSize (width, 3);
  375. var topRow = $"{d.ULCorner} {title} {new String (d.HLine.ToString () [0], width - title.Length - 4)}{d.URCorner}";
  376. var bottomRow = $"{d.LLCorner}{new String (d.HLine.ToString () [0], width - 2)}{d.LRCorner}";
  377. // Default (center)
  378. var dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Center };
  379. runstate = Application.Begin (dlg);
  380. var buttonRow = $"{d.VLine} {btn1} {d.VLine}";
  381. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  382. // Now add a second button
  383. buttonRow = $"{d.VLine} {btn1} {btn2} {d.VLine}";
  384. dlg.AddButton (new Button (btn2Text));
  385. bool first = false;
  386. Application.RunMainLoopIteration (ref runstate, true, ref first);
  387. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  388. Application.End (runstate);
  389. // Justify
  390. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Justify };
  391. runstate = Application.Begin (dlg);
  392. buttonRow = $"{d.VLine} {btn1}{d.VLine}";
  393. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  394. // Now add a second button
  395. buttonRow = $"{d.VLine}{btn1} {btn2}{d.VLine}";
  396. dlg.AddButton (new Button (btn2Text));
  397. first = false;
  398. Application.RunMainLoopIteration (ref runstate, true, ref first);
  399. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  400. Application.End (runstate);
  401. // Right
  402. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Right };
  403. runstate = Application.Begin (dlg);
  404. buttonRow = $"{d.VLine}{new String (' ', width - btn1.Length - 2)}{btn1}{d.VLine}";
  405. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  406. // Now add a second button
  407. buttonRow = $"{d.VLine} {btn1} {btn2}{d.VLine}";
  408. dlg.AddButton (new Button (btn2Text));
  409. first = false;
  410. Application.RunMainLoopIteration (ref runstate, true, ref first);
  411. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  412. Application.End (runstate);
  413. // Left
  414. dlg = new Dialog (title, width, 3, new Button (btn1Text)) { ButtonAlignment = Dialog.ButtonAlignments.Left };
  415. runstate = Application.Begin (dlg);
  416. buttonRow = $"{d.VLine}{btn1}{new String (' ', width - btn1.Length - 2)}{d.VLine}";
  417. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  418. // Now add a second button
  419. buttonRow = $"{d.VLine}{btn1} {btn2} {d.VLine}";
  420. dlg.AddButton (new Button (btn2Text));
  421. first = false;
  422. Application.RunMainLoopIteration (ref runstate, true, ref first);
  423. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  424. Application.End (runstate);
  425. }
  426. }
  427. }