DialogTests.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Application;
  3. namespace Terminal.Gui.DialogTests;
  4. public class DialogTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public DialogTests (ITestOutputHelper output) { _output = output; }
  8. [Fact (Skip = "Dim.Auto WIP")]
  9. [AutoInitShutdown]
  10. public void Add_Button_Works ()
  11. {
  12. RunState runstate = null;
  13. var d = (FakeDriver)Driver;
  14. var title = "1234";
  15. var btn1Text = "yes";
  16. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  17. var btn2Text = "no";
  18. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  19. // We test with one button first, but do this to get the width right for 2
  20. int width = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}".Length;
  21. d.SetBufferSize (width, 1);
  22. // Default (center)
  23. var dlg = new Dialog
  24. {
  25. Title = title,
  26. Width = width,
  27. Height = 1,
  28. ButtonAlignment = Alignment.Center,
  29. Buttons = [new Button { Text = btn1Text }]
  30. };
  31. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  32. dlg.Border.Thickness = new (1, 0, 1, 0);
  33. runstate = Begin (dlg);
  34. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {CM.Glyphs.VLine}";
  35. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  36. // Now add a second button
  37. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  38. dlg.AddButton (new () { Text = btn2Text });
  39. var first = false;
  40. RunIteration (ref runstate, ref first);
  41. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  42. End (runstate);
  43. dlg.Dispose ();
  44. // Justify
  45. dlg = new ()
  46. {
  47. Title = title,
  48. Width = width,
  49. Height = 1,
  50. ButtonAlignment = Alignment.Fill,
  51. Buttons = [new Button { Text = btn1Text }]
  52. };
  53. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  54. dlg.Border.Thickness = new (1, 0, 1, 0);
  55. runstate = Begin (dlg);
  56. buttonRow = $"{CM.Glyphs.VLine}{btn1} {CM.Glyphs.VLine}";
  57. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  58. // Now add a second button
  59. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  60. dlg.AddButton (new () { Text = btn2Text });
  61. first = false;
  62. RunIteration (ref runstate, ref first);
  63. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  64. End (runstate);
  65. dlg.Dispose ();
  66. // Right
  67. dlg = new ()
  68. {
  69. Title = title,
  70. Width = width,
  71. Height = 1,
  72. ButtonAlignment = Alignment.End,
  73. Buttons = [new Button { Text = btn1Text }]
  74. };
  75. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  76. dlg.Border.Thickness = new (1, 0, 1, 0);
  77. runstate = Begin (dlg);
  78. buttonRow = $"{CM.Glyphs.VLine}{new (' ', width - btn1.Length - 2)}{btn1}{CM.Glyphs.VLine}";
  79. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  80. // Now add a second button
  81. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  82. dlg.AddButton (new () { Text = btn2Text });
  83. first = false;
  84. RunIteration (ref runstate, ref first);
  85. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  86. End (runstate);
  87. dlg.Dispose ();
  88. // Left
  89. dlg = new ()
  90. {
  91. Title = title,
  92. Width = width,
  93. Height = 1,
  94. ButtonAlignment = Alignment.Start,
  95. Buttons = [new Button { Text = btn1Text }]
  96. };
  97. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  98. dlg.Border.Thickness = new (1, 0, 1, 0);
  99. runstate = Begin (dlg);
  100. buttonRow = $"{CM.Glyphs.VLine}{btn1}{new (' ', width - btn1.Length - 2)}{CM.Glyphs.VLine}";
  101. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  102. // Now add a second button
  103. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  104. dlg.AddButton (new () { Text = btn2Text });
  105. first = false;
  106. RunIteration (ref runstate, ref first);
  107. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  108. End (runstate);
  109. dlg.Dispose ();
  110. }
  111. [Fact]
  112. [AutoInitShutdown]
  113. public void ButtonAlignment_Four ()
  114. {
  115. RunState runstate = null;
  116. var d = (FakeDriver)Driver;
  117. var title = "1234";
  118. // E.g "|[ yes ][ no ][ maybe ]|"
  119. var btn1Text = "yes";
  120. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  121. var btn2Text = "no";
  122. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  123. var btn3Text = "maybe";
  124. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  125. var btn4Text = "never";
  126. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  127. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  128. int width = buttonRow.Length;
  129. d.SetBufferSize (buttonRow.Length, 3);
  130. // Default - Center
  131. (runstate, Dialog dlg) = RunButtonTestDialog (
  132. title,
  133. width,
  134. Alignment.Center,
  135. new Button { Text = btn1Text },
  136. new Button { Text = btn2Text },
  137. new Button { Text = btn3Text },
  138. new Button { Text = btn4Text }
  139. );
  140. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  141. End (runstate);
  142. dlg.Dispose ();
  143. // Justify
  144. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  145. Assert.Equal (width, buttonRow.Length);
  146. (runstate, dlg) = RunButtonTestDialog (
  147. title,
  148. width,
  149. Alignment.Fill,
  150. new Button { Text = btn1Text },
  151. new Button { Text = btn2Text },
  152. new Button { Text = btn3Text },
  153. new Button { Text = btn4Text }
  154. );
  155. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  156. End (runstate);
  157. dlg.Dispose ();
  158. // Right
  159. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  160. Assert.Equal (width, buttonRow.Length);
  161. (runstate, dlg) = RunButtonTestDialog (
  162. title,
  163. width,
  164. Alignment.End,
  165. new Button { Text = btn1Text },
  166. new Button { Text = btn2Text },
  167. new Button { Text = btn3Text },
  168. new Button { Text = btn4Text }
  169. );
  170. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  171. End (runstate);
  172. dlg.Dispose ();
  173. // Left
  174. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  175. Assert.Equal (width, buttonRow.Length);
  176. (runstate, dlg) = RunButtonTestDialog (
  177. title,
  178. width,
  179. Alignment.Start,
  180. new Button { Text = btn1Text },
  181. new Button { Text = btn2Text },
  182. new Button { Text = btn3Text },
  183. new Button { Text = btn4Text }
  184. );
  185. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  186. End (runstate);
  187. dlg.Dispose ();
  188. }
  189. [Fact]
  190. [AutoInitShutdown]
  191. public void ButtonAlignment_Four_On_Too_Small_Width ()
  192. {
  193. RunState runstate = null;
  194. var d = (FakeDriver)Driver;
  195. var title = "1234";
  196. // E.g "|[ yes ][ no ][ maybe ][ never ]|"
  197. var btn1Text = "yes";
  198. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  199. var btn2Text = "no";
  200. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  201. var btn3Text = "maybe";
  202. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  203. var btn4Text = "never";
  204. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  205. var buttonRow = string.Empty;
  206. var width = 30;
  207. d.SetBufferSize (width, 1);
  208. // Default - Center
  209. buttonRow =
  210. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} yes {CM.Glyphs.RightBracket}{btn2}{btn3}{CM.Glyphs.LeftBracket} neve{CM.Glyphs.VLine}";
  211. (runstate, Dialog dlg) = RunButtonTestDialog (
  212. title,
  213. width,
  214. Alignment.Center,
  215. new Button { Text = btn1Text },
  216. new Button { Text = btn2Text },
  217. new Button { Text = btn3Text },
  218. new Button { Text = btn4Text }
  219. );
  220. Assert.Equal (new (width, 1), dlg.Frame.Size);
  221. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  222. End (runstate);
  223. dlg.Dispose ();
  224. // Justify
  225. buttonRow =
  226. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} yes {CM.Glyphs.LeftBracket} no {CM.Glyphs.LeftBracket} maybe {CM.Glyphs.LeftBracket} never {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  227. (runstate, dlg) = RunButtonTestDialog (
  228. title,
  229. width,
  230. Alignment.Fill,
  231. new Button { Text = btn1Text },
  232. new Button { Text = btn2Text },
  233. new Button { Text = btn3Text },
  234. new Button { Text = btn4Text }
  235. );
  236. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  237. End (runstate);
  238. dlg.Dispose ();
  239. // Right
  240. buttonRow = $"{CM.Glyphs.VLine}es {CM.Glyphs.RightBracket}{btn2}{btn3}{btn4}{CM.Glyphs.VLine}";
  241. Assert.Equal (width, buttonRow.Length);
  242. (runstate, dlg) = RunButtonTestDialog (
  243. title,
  244. width,
  245. Alignment.End,
  246. new Button { Text = btn1Text },
  247. new Button { Text = btn2Text },
  248. new Button { Text = btn3Text },
  249. new Button { Text = btn4Text }
  250. );
  251. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  252. End (runstate);
  253. dlg.Dispose ();
  254. // Left
  255. buttonRow = $"{CM.Glyphs.VLine}{btn1}{btn2}{btn3}{CM.Glyphs.LeftBracket} neve{CM.Glyphs.VLine}";
  256. (runstate, dlg) = RunButtonTestDialog (
  257. title,
  258. width,
  259. Alignment.Start,
  260. new Button { Text = btn1Text },
  261. new Button { Text = btn2Text },
  262. new Button { Text = btn3Text },
  263. new Button { Text = btn4Text }
  264. );
  265. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  266. End (runstate);
  267. dlg.Dispose ();
  268. }
  269. [Fact]
  270. [AutoInitShutdown]
  271. public void ButtonAlignment_Four_WideOdd ()
  272. {
  273. RunState runstate = null;
  274. var d = (FakeDriver)Driver;
  275. var title = "1234";
  276. // E.g "|[ yes ][ no ][ maybe ]|"
  277. var btn1Text = "really long button 1";
  278. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  279. var btn2Text = "really long button 2";
  280. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  281. var btn3Text = "really long button 3";
  282. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  283. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  284. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  285. // Note extra spaces to make dialog even wider
  286. // 123456 1234567
  287. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  288. int width = buttonRow.Length;
  289. d.SetBufferSize (buttonRow.Length, 1);
  290. // Default - Center
  291. (runstate, Dialog dlg) = RunButtonTestDialog (
  292. title,
  293. width,
  294. Alignment.Center,
  295. new Button { Text = btn1Text },
  296. new Button { Text = btn2Text },
  297. new Button { Text = btn3Text },
  298. new Button { Text = btn4Text }
  299. );
  300. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  301. End (runstate);
  302. dlg.Dispose ();
  303. // Justify
  304. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  305. Assert.Equal (width, buttonRow.Length);
  306. (runstate, dlg) = RunButtonTestDialog (
  307. title,
  308. width,
  309. Alignment.Fill,
  310. new Button { Text = btn1Text },
  311. new Button { Text = btn2Text },
  312. new Button { Text = btn3Text },
  313. new Button { Text = btn4Text }
  314. );
  315. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  316. End (runstate);
  317. dlg.Dispose ();
  318. // Right
  319. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  320. Assert.Equal (width, buttonRow.Length);
  321. (runstate, dlg) = RunButtonTestDialog (
  322. title,
  323. width,
  324. Alignment.End,
  325. new Button { Text = btn1Text },
  326. new Button { Text = btn2Text },
  327. new Button { Text = btn3Text },
  328. new Button { Text = btn4Text }
  329. );
  330. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  331. End (runstate);
  332. dlg.Dispose ();
  333. // Left
  334. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  335. Assert.Equal (width, buttonRow.Length);
  336. (runstate, dlg) = RunButtonTestDialog (
  337. title,
  338. width,
  339. Alignment.Start,
  340. new Button { Text = btn1Text },
  341. new Button { Text = btn2Text },
  342. new Button { Text = btn3Text },
  343. new Button { Text = btn4Text }
  344. );
  345. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  346. End (runstate);
  347. dlg.Dispose ();
  348. }
  349. [Fact]
  350. [AutoInitShutdown]
  351. public void ButtonAlignment_Four_Wider ()
  352. {
  353. RunState runstate = null;
  354. var d = (FakeDriver)Driver;
  355. var title = "1234";
  356. // E.g "|[ yes ][ no ][ maybe ]|"
  357. var btn1Text = "yes";
  358. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  359. var btn2Text = "no";
  360. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  361. var btn3Text = "你你你你你"; // This is a wide char
  362. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  363. // Requires a Nerd Font
  364. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  365. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  366. // Note extra spaces to make dialog even wider
  367. // 123456 123456
  368. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  369. int width = buttonRow.GetColumns ();
  370. d.SetBufferSize (width, 3);
  371. // Default - Center
  372. (runstate, Dialog dlg) = RunButtonTestDialog (
  373. title,
  374. width,
  375. Alignment.Center,
  376. new Button { Text = btn1Text },
  377. new Button { Text = btn2Text },
  378. new Button { Text = btn3Text },
  379. new Button { Text = btn4Text }
  380. );
  381. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  382. End (runstate);
  383. dlg.Dispose ();
  384. // Justify
  385. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  386. Assert.Equal (width, buttonRow.GetColumns ());
  387. (runstate, dlg) = RunButtonTestDialog (
  388. title,
  389. width,
  390. Alignment.Fill,
  391. new Button { Text = btn1Text },
  392. new Button { Text = btn2Text },
  393. new Button { Text = btn3Text },
  394. new Button { Text = btn4Text }
  395. );
  396. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  397. End (runstate);
  398. dlg.Dispose ();
  399. // Right
  400. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  401. Assert.Equal (width, buttonRow.GetColumns ());
  402. (runstate, dlg) = RunButtonTestDialog (
  403. title,
  404. width,
  405. Alignment.End,
  406. new Button { Text = btn1Text },
  407. new Button { Text = btn2Text },
  408. new Button { Text = btn3Text },
  409. new Button { Text = btn4Text }
  410. );
  411. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  412. End (runstate);
  413. dlg.Dispose ();
  414. // Left
  415. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  416. Assert.Equal (width, buttonRow.GetColumns ());
  417. (runstate, dlg) = RunButtonTestDialog (
  418. title,
  419. width,
  420. Alignment.Start,
  421. new Button { Text = btn1Text },
  422. new Button { Text = btn2Text },
  423. new Button { Text = btn3Text },
  424. new Button { Text = btn4Text }
  425. );
  426. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  427. End (runstate);
  428. dlg.Dispose ();
  429. }
  430. [Fact]
  431. [AutoInitShutdown]
  432. public void ButtonAlignment_One ()
  433. {
  434. var d = (FakeDriver)Driver;
  435. RunState runstate = null;
  436. var title = "1234";
  437. // E.g "|[ ok ]|"
  438. var btnText = "ok";
  439. var buttonRow =
  440. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  441. int width = buttonRow.Length;
  442. d.SetBufferSize (width, 1);
  443. (runstate, Dialog dlg) = RunButtonTestDialog (
  444. title,
  445. width,
  446. Alignment.Center,
  447. new Button { Text = btnText }
  448. );
  449. // Center
  450. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  451. End (runstate);
  452. dlg.Dispose ();
  453. // Justify
  454. buttonRow =
  455. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  456. Assert.Equal (width, buttonRow.Length);
  457. (runstate, dlg) = RunButtonTestDialog (
  458. title,
  459. width,
  460. Alignment.Fill,
  461. new Button { Text = btnText }
  462. );
  463. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  464. End (runstate);
  465. dlg.Dispose ();
  466. // Right
  467. buttonRow =
  468. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  469. Assert.Equal (width, buttonRow.Length);
  470. (runstate, dlg) = RunButtonTestDialog (
  471. title,
  472. width,
  473. Alignment.End,
  474. new Button { Text = btnText }
  475. );
  476. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  477. End (runstate);
  478. dlg.Dispose ();
  479. // Left
  480. buttonRow =
  481. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  482. Assert.Equal (width, buttonRow.Length);
  483. (runstate, dlg) = RunButtonTestDialog (
  484. title,
  485. width,
  486. Alignment.Start,
  487. new Button { Text = btnText }
  488. );
  489. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  490. End (runstate);
  491. dlg.Dispose ();
  492. // Wider
  493. buttonRow =
  494. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  495. width = buttonRow.Length;
  496. d.SetBufferSize (width, 1);
  497. (runstate, dlg) = RunButtonTestDialog (
  498. title,
  499. width,
  500. Alignment.Center,
  501. new Button { Text = btnText }
  502. );
  503. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  504. End (runstate);
  505. dlg.Dispose ();
  506. // Justify
  507. buttonRow =
  508. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  509. Assert.Equal (width, buttonRow.Length);
  510. (runstate, dlg) = RunButtonTestDialog (
  511. title,
  512. width,
  513. Alignment.Fill,
  514. new Button { Text = btnText }
  515. );
  516. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  517. End (runstate);
  518. dlg.Dispose ();
  519. // Right
  520. buttonRow =
  521. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  522. Assert.Equal (width, buttonRow.Length);
  523. (runstate, dlg) = RunButtonTestDialog (
  524. title,
  525. width,
  526. Alignment.End,
  527. new Button { Text = btnText }
  528. );
  529. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  530. End (runstate);
  531. dlg.Dispose ();
  532. // Left
  533. buttonRow =
  534. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  535. Assert.Equal (width, buttonRow.Length);
  536. (runstate, dlg) = RunButtonTestDialog (
  537. title,
  538. width,
  539. Alignment.Start,
  540. new Button { Text = btnText }
  541. );
  542. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  543. End (runstate);
  544. dlg.Dispose ();
  545. }
  546. [Fact]
  547. [AutoInitShutdown]
  548. public void ButtonAlignment_Three ()
  549. {
  550. RunState runstate = null;
  551. var d = (FakeDriver)Driver;
  552. var title = "1234";
  553. // E.g "|[ yes ][ no ][ maybe ]|"
  554. var btn1Text = "yes";
  555. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  556. var btn2Text = "no";
  557. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  558. var btn3Text = "maybe";
  559. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  560. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  561. int width = buttonRow.Length;
  562. d.SetBufferSize (buttonRow.Length, 3);
  563. (runstate, Dialog dlg) = RunButtonTestDialog (
  564. title,
  565. width,
  566. Alignment.Center,
  567. new Button { Text = btn1Text },
  568. new Button { Text = btn2Text },
  569. new Button { Text = btn3Text }
  570. );
  571. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  572. End (runstate);
  573. dlg.Dispose ();
  574. // Justify
  575. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  576. Assert.Equal (width, buttonRow.Length);
  577. (runstate, dlg) = RunButtonTestDialog (
  578. title,
  579. width,
  580. Alignment.Fill,
  581. new Button { Text = btn1Text },
  582. new Button { Text = btn2Text },
  583. new Button { Text = btn3Text }
  584. );
  585. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  586. End (runstate);
  587. dlg.Dispose ();
  588. // Right
  589. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  590. Assert.Equal (width, buttonRow.Length);
  591. (runstate, dlg) = RunButtonTestDialog (
  592. title,
  593. width,
  594. Alignment.End,
  595. new Button { Text = btn1Text },
  596. new Button { Text = btn2Text },
  597. new Button { Text = btn3Text }
  598. );
  599. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  600. End (runstate);
  601. dlg.Dispose ();
  602. // Left
  603. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  604. Assert.Equal (width, buttonRow.Length);
  605. (runstate, dlg) = RunButtonTestDialog (
  606. title,
  607. width,
  608. Alignment.Start,
  609. new Button { Text = btn1Text },
  610. new Button { Text = btn2Text },
  611. new Button { Text = btn3Text }
  612. );
  613. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  614. End (runstate);
  615. dlg.Dispose ();
  616. }
  617. [Fact]
  618. [AutoInitShutdown]
  619. public void ButtonAlignment_Two ()
  620. {
  621. RunState runstate = null;
  622. var d = (FakeDriver)Driver;
  623. var title = "1234";
  624. // E.g "|[ yes ][ no ]|"
  625. var btn1Text = "yes";
  626. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  627. var btn2Text = "no";
  628. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  629. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  630. int width = buttonRow.Length;
  631. d.SetBufferSize (buttonRow.Length, 3);
  632. (runstate, Dialog dlg) = RunButtonTestDialog (
  633. title,
  634. width,
  635. Alignment.Center,
  636. new Button { Text = btn1Text },
  637. new Button { Text = btn2Text }
  638. );
  639. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  640. End (runstate);
  641. dlg.Dispose ();
  642. // Justify
  643. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  644. Assert.Equal (width, buttonRow.Length);
  645. (runstate, dlg) = RunButtonTestDialog (
  646. title,
  647. width,
  648. Alignment.Fill,
  649. new Button { Text = btn1Text },
  650. new Button { Text = btn2Text }
  651. );
  652. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  653. End (runstate);
  654. dlg.Dispose ();
  655. // Right
  656. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  657. Assert.Equal (width, buttonRow.Length);
  658. (runstate, dlg) = RunButtonTestDialog (
  659. title,
  660. width,
  661. Alignment.End,
  662. new Button { Text = btn1Text },
  663. new Button { Text = btn2Text }
  664. );
  665. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  666. End (runstate);
  667. dlg.Dispose ();
  668. // Left
  669. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  670. Assert.Equal (width, buttonRow.Length);
  671. (runstate, dlg) = RunButtonTestDialog (
  672. title,
  673. width,
  674. Alignment.Start,
  675. new Button { Text = btn1Text },
  676. new Button { Text = btn2Text }
  677. );
  678. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  679. End (runstate);
  680. dlg.Dispose ();
  681. }
  682. [Fact]
  683. [AutoInitShutdown]
  684. public void ButtonAlignment_Two_Hidden ()
  685. {
  686. RunState runstate = null;
  687. var firstIteration = false;
  688. var d = (FakeDriver)Driver;
  689. var title = "1234";
  690. // E.g "|[ yes ][ no ]|"
  691. var btn1Text = "yes";
  692. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  693. var btn2Text = "no";
  694. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  695. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  696. int width = buttonRow.Length;
  697. d.SetBufferSize (buttonRow.Length, 3);
  698. Dialog dlg = null;
  699. Button button1, button2;
  700. // Default (Center)
  701. button1 = new Button { Text = btn1Text };
  702. button2 = new Button { Text = btn2Text };
  703. (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Center, button1, button2);
  704. button1.Visible = false;
  705. RunIteration (ref runstate, ref firstIteration);
  706. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  707. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  708. End (runstate);
  709. dlg.Dispose ();
  710. // Justify
  711. Assert.Equal (width, buttonRow.Length);
  712. button1 = new Button { Text = btn1Text };
  713. button2 = new Button { Text = btn2Text };
  714. (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Fill, button1, button2);
  715. button1.Visible = false;
  716. RunIteration (ref runstate, ref firstIteration);
  717. buttonRow = $@"{CM.Glyphs.VLine} {btn2}{CM.Glyphs.VLine}";
  718. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  719. End (runstate);
  720. dlg.Dispose ();
  721. // Right
  722. Assert.Equal (width, buttonRow.Length);
  723. button1 = new Button { Text = btn1Text };
  724. button2 = new Button { Text = btn2Text };
  725. (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.End, button1, button2);
  726. button1.Visible = false;
  727. RunIteration (ref runstate, ref firstIteration);
  728. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  729. End (runstate);
  730. dlg.Dispose ();
  731. // Left
  732. Assert.Equal (width, buttonRow.Length);
  733. button1 = new Button { Text = btn1Text };
  734. button2 = new Button { Text = btn2Text };
  735. (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Start, button1, button2);
  736. button1.Visible = false;
  737. RunIteration (ref runstate, ref firstIteration);
  738. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  739. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  740. End (runstate);
  741. dlg.Dispose ();
  742. }
  743. [Fact (Skip = "Dim.Auto WIP")]
  744. [AutoInitShutdown]
  745. public void Dialog_In_Window_With_Size_One_Button_Aligns ()
  746. {
  747. ((FakeDriver)Driver).SetBufferSize (20, 5);
  748. var win = new Window ();
  749. var iterations = 0;
  750. Iteration += (s, a) =>
  751. {
  752. if (++iterations > 2)
  753. {
  754. RequestStop ();
  755. }
  756. };
  757. var btn = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  758. win.Loaded += (s, a) =>
  759. {
  760. Dialog.DefaultButtonAlignment = Alignment.Center;
  761. var dlg = new Dialog { Width = 18, Height = 3, Buttons = [new () { Text = "Ok" }] };
  762. dlg.Loaded += (s, a) =>
  763. {
  764. Refresh ();
  765. var expected = @$"
  766. ┌──────────────────┐
  767. │┌────────────────┐│
  768. ││ {btn} ││
  769. │└────────────────┘│
  770. └──────────────────┘";
  771. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  772. };
  773. Run (dlg);
  774. };
  775. Run (win);
  776. }
  777. [Theory (Skip = "Dim.Auto WIP")]
  778. [AutoInitShutdown]
  779. [InlineData (
  780. 5,
  781. @"
  782. ┌┌───────────────┐─┐
  783. ││ │ │
  784. ││ ⟦ Ok ⟧ │ │
  785. │└───────────────┘ │
  786. └──────────────────┘"
  787. )]
  788. [InlineData (
  789. 6,
  790. @"
  791. ┌┌───────────────┐─┐
  792. ││ │ │
  793. ││ │ │
  794. ││ ⟦ Ok ⟧ │ │
  795. │└───────────────┘ │
  796. └──────────────────┘"
  797. )]
  798. [InlineData (
  799. 7,
  800. @"
  801. ┌──────────────────┐
  802. │┌───────────────┐ │
  803. ││ │ │
  804. ││ │ │
  805. ││ ⟦ Ok ⟧ │ │
  806. │└───────────────┘ │
  807. └──────────────────┘"
  808. )]
  809. [InlineData (
  810. 8,
  811. @"
  812. ┌──────────────────┐
  813. │┌───────────────┐ │
  814. ││ │ │
  815. ││ │ │
  816. ││ │ │
  817. ││ ⟦ Ok ⟧ │ │
  818. │└───────────────┘ │
  819. └──────────────────┘"
  820. )]
  821. [InlineData (
  822. 9,
  823. @"
  824. ┌──────────────────┐
  825. │┌───────────────┐ │
  826. ││ │ │
  827. ││ │ │
  828. ││ │ │
  829. ││ │ │
  830. ││ ⟦ Ok ⟧ │ │
  831. │└───────────────┘ │
  832. └──────────────────┘"
  833. )]
  834. public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height, string expected)
  835. {
  836. ((FakeDriver)Driver).SetBufferSize (20, height);
  837. var win = new Window ();
  838. int iterations = -1;
  839. Dialog.DefaultButtonAlignment = Alignment.Center;
  840. Iteration += (s, a) =>
  841. {
  842. iterations++;
  843. if (iterations == 0)
  844. {
  845. var dlg = new Dialog
  846. {
  847. Buttons = [new () { Text = "Ok" }],
  848. Width = Dim.Percent (85),
  849. Height = Dim.Percent (85)
  850. };
  851. Run (dlg);
  852. }
  853. else if (iterations == 1)
  854. {
  855. // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)?? No
  856. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  857. }
  858. else
  859. {
  860. RequestStop ();
  861. }
  862. };
  863. Run (win);
  864. }
  865. [Fact]
  866. [AutoInitShutdown]
  867. public void Dialog_Opened_From_Another_Dialog ()
  868. {
  869. ((FakeDriver)Driver).SetBufferSize (30, 10);
  870. Dialog.DefaultButtonAlignment = Alignment.Center;
  871. var btn1 = new Button { Text = "press me 1" };
  872. Button btn2 = null;
  873. Button btn3 = null;
  874. string expected = null;
  875. btn1.Accept += (s, e) =>
  876. {
  877. btn2 = new () { Text = "Show Sub" };
  878. btn3 = new () { Text = "Close" };
  879. btn3.Accept += (s, e) => RequestStop ();
  880. btn2.Accept += (s, e) =>
  881. {
  882. // Don't test MessageBox in Dialog unit tests!
  883. var subBtn = new Button { Text = "Ok", IsDefault = true };
  884. var subDlg = new Dialog { Text = "ya", Width = 20, Height = 5, Buttons = [subBtn] };
  885. subBtn.Accept += (s, e) => RequestStop (subDlg);
  886. Run (subDlg);
  887. };
  888. var dlg = new Dialog
  889. {
  890. Buttons = [btn2, btn3],
  891. Width = Dim.Percent (85),
  892. Height = Dim.Percent (85)
  893. };
  894. Run (dlg);
  895. dlg.Dispose ();
  896. };
  897. var btn =
  898. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
  899. int iterations = -1;
  900. Iteration += (s, a) =>
  901. {
  902. iterations++;
  903. if (iterations == 0)
  904. {
  905. Assert.True (btn1.NewKeyDownEvent (Key.Space));
  906. }
  907. else if (iterations == 1)
  908. {
  909. expected = @$"
  910. ┌───────────────────────┐
  911. │ │
  912. │ │
  913. │ │
  914. │ │
  915. │ │
  916. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  917. └───────────────────────┘";
  918. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  919. Assert.True (btn2.NewKeyDownEvent (Key.Space));
  920. }
  921. else if (iterations == 2)
  922. {
  923. TestHelpers.AssertDriverContentsWithFrameAre (
  924. @$"
  925. ┌───────────────────────┐
  926. │ ┌──────────────────┐ │
  927. │ │ya │ │
  928. │ │ │ │
  929. │ │ {btn} │ │
  930. │ └──────────────────┘ │
  931. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  932. └───────────────────────┘",
  933. _output
  934. );
  935. Assert.True (Current.NewKeyDownEvent (Key.Enter));
  936. }
  937. else if (iterations == 3)
  938. {
  939. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  940. Assert.True (btn3.NewKeyDownEvent (Key.Space));
  941. }
  942. else if (iterations == 4)
  943. {
  944. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  945. RequestStop ();
  946. }
  947. };
  948. Run ().Dispose ();
  949. Shutdown ();
  950. Assert.Equal (4, iterations);
  951. }
  952. [Fact]
  953. [AutoInitShutdown]
  954. public void FileDialog_FileSystemWatcher ()
  955. {
  956. for (var i = 0; i < 8; i++)
  957. {
  958. var fd = new FileDialog ();
  959. fd.Ready += (s, e) => RequestStop ();
  960. Run (fd);
  961. fd.Dispose ();
  962. }
  963. }
  964. [Fact]
  965. [AutoInitShutdown]
  966. public void Location_Default ()
  967. {
  968. var d = new Dialog ()
  969. {
  970. Width = Dim.Percent (85),
  971. Height = Dim.Percent (85)
  972. };
  973. Begin (d);
  974. ((FakeDriver)Driver).SetBufferSize (100, 100);
  975. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  976. var expected = 7;
  977. Assert.Equal (new (expected, expected), d.Frame.Location);
  978. }
  979. [Fact]
  980. [AutoInitShutdown]
  981. public void Location_Not_Default ()
  982. {
  983. var d = new Dialog { X = 1, Y = 1 };
  984. Begin (d);
  985. ((FakeDriver)Driver).SetBufferSize (100, 100);
  986. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  987. var expected = 1;
  988. Assert.Equal (new (expected, expected), d.Frame.Location);
  989. }
  990. [Fact]
  991. [AutoInitShutdown]
  992. public void Location_When_Application_Top_Not_Default ()
  993. {
  994. var expected = 5;
  995. var d = new Dialog { X = expected, Y = expected, Height = 5, Width = 5 };
  996. Begin (d);
  997. ((FakeDriver)Driver).SetBufferSize (20, 10);
  998. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  999. Assert.Equal (new (expected, expected), d.Frame.Location);
  1000. TestHelpers.AssertDriverContentsWithFrameAre (
  1001. @"
  1002. ┌───┐
  1003. │ │
  1004. │ │
  1005. │ │
  1006. └───┘",
  1007. _output
  1008. );
  1009. }
  1010. [Fact]
  1011. [AutoInitShutdown]
  1012. public void Location_When_Not_Application_Top_Not_Default ()
  1013. {
  1014. var top = new Toplevel ();
  1015. top.BorderStyle = LineStyle.Double;
  1016. int iterations = -1;
  1017. Iteration += (s, a) =>
  1018. {
  1019. iterations++;
  1020. if (iterations == 0)
  1021. {
  1022. var d = new Dialog { X = 5, Y = 5, Height = 3, Width = 5 };
  1023. RunState rs = Begin (d);
  1024. Assert.Equal (new (5, 5), d.Frame.Location);
  1025. TestHelpers.AssertDriverContentsWithFrameAre (
  1026. @"
  1027. ╔══════════════════╗
  1028. ║ ║
  1029. ║ ║
  1030. ║ ║
  1031. ║ ║
  1032. ║ ┌───┐ ║
  1033. ║ │ │ ║
  1034. ║ └───┘ ║
  1035. ║ ║
  1036. ╚══════════════════╝",
  1037. _output
  1038. );
  1039. End (rs);
  1040. d.Dispose ();
  1041. d = new ()
  1042. {
  1043. X = 5, Y = 5,
  1044. Width = Dim.Percent (85),
  1045. Height = Dim.Percent (85)
  1046. };
  1047. rs = Begin (d);
  1048. // This is because of PostionTopLevels and EnsureVisibleBounds
  1049. Assert.Equal (new (3, 2), d.Frame.Location);
  1050. // #3127: Before
  1051. // Assert.Equal (new (17, 8), d.Frame.Size);
  1052. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  1053. //╔══════════════════╗
  1054. //║ ║
  1055. //║ ┌───────────────┐
  1056. //║ │ │
  1057. //║ │ │
  1058. //║ │ │
  1059. //║ │ │
  1060. //║ │ │
  1061. //║ │ │
  1062. //╚══└───────────────┘", _output);
  1063. // #3127: After: Because Toplevel is now Width/Height = Dim.Filll
  1064. Assert.Equal (new (15, 6), d.Frame.Size);
  1065. TestHelpers.AssertDriverContentsWithFrameAre (
  1066. @"
  1067. ╔══════════════════╗
  1068. ║ ║
  1069. ║ ┌─────────────┐ ║
  1070. ║ │ │ ║
  1071. ║ │ │ ║
  1072. ║ │ │ ║
  1073. ║ │ │ ║
  1074. ║ └─────────────┘ ║
  1075. ║ ║
  1076. ╚══════════════════╝",
  1077. _output
  1078. );
  1079. End (rs);
  1080. d.Dispose ();
  1081. }
  1082. else if (iterations > 0)
  1083. {
  1084. RequestStop ();
  1085. }
  1086. };
  1087. ((FakeDriver)Driver).SetBufferSize (20, 10);
  1088. Run (top);
  1089. }
  1090. [Fact]
  1091. [AutoInitShutdown]
  1092. public void One_Button_Works ()
  1093. {
  1094. RunState runstate = null;
  1095. var d = (FakeDriver)Driver;
  1096. var title = "";
  1097. var btnText = "ok";
  1098. var buttonRow =
  1099. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  1100. int width = buttonRow.Length;
  1101. d.SetBufferSize (buttonRow.Length, 10);
  1102. (runstate, Dialog _) = RunButtonTestDialog (
  1103. title,
  1104. width,
  1105. Alignment.Center,
  1106. new Button { Text = btnText }
  1107. );
  1108. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1109. End (runstate);
  1110. }
  1111. [Fact]
  1112. [AutoInitShutdown]
  1113. public void Size_Default ()
  1114. {
  1115. var d = new Dialog ()
  1116. {
  1117. Width = Dim.Percent (85),
  1118. Height = Dim.Percent (85)
  1119. };
  1120. Begin (d);
  1121. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1122. // Default size is Percent(85)
  1123. Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
  1124. d.Dispose ();
  1125. }
  1126. [Fact]
  1127. [AutoInitShutdown]
  1128. public void Size_Not_Default ()
  1129. {
  1130. var d = new Dialog { Width = 50, Height = 50 };
  1131. Begin (d);
  1132. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1133. // Default size is Percent(85)
  1134. Assert.Equal (new (50, 50), d.Frame.Size);
  1135. d.Dispose ();
  1136. }
  1137. [Fact]
  1138. [SetupFakeDriver]
  1139. public void Zero_Buttons_Works ()
  1140. {
  1141. RunState runstate = null;
  1142. var d = (FakeDriver)Driver;
  1143. var title = "1234";
  1144. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.VLine}";
  1145. int width = buttonRow.Length;
  1146. d.SetBufferSize (buttonRow.Length, 3);
  1147. (runstate, Dialog dlg) = RunButtonTestDialog (title, width, Alignment.Center, null);
  1148. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1149. End (runstate);
  1150. dlg.Dispose ();
  1151. }
  1152. private (RunState, Dialog) RunButtonTestDialog (
  1153. string title,
  1154. int width,
  1155. Alignment align,
  1156. params Button [] btns
  1157. )
  1158. {
  1159. var dlg = new Dialog
  1160. {
  1161. Title = title,
  1162. X = 0,
  1163. Y = 0,
  1164. Width = width,
  1165. Height = 1,
  1166. ButtonAlignment = align,
  1167. Buttons = btns
  1168. };
  1169. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  1170. dlg.Border.Thickness = new (1, 0, 1, 0);
  1171. return (Begin (dlg), dlg);
  1172. }
  1173. [Fact]
  1174. [AutoInitShutdown]
  1175. public void Run_Does_Not_Dispose_Dialog ()
  1176. {
  1177. var top = new Toplevel ();
  1178. Dialog dlg = new ();
  1179. dlg.Ready += Dlg_Ready;
  1180. Run (dlg);
  1181. #if DEBUG_IDISPOSABLE
  1182. Assert.False (dlg.WasDisposed);
  1183. Assert.False (Top.WasDisposed);
  1184. Assert.NotEqual (top, Top);
  1185. Assert.Equal (dlg, Top);
  1186. #endif
  1187. // dlg wasn't disposed yet and it's possible to access to his properties
  1188. Assert.False (dlg.Canceled);
  1189. Assert.NotNull (dlg);
  1190. dlg.Canceled = true;
  1191. Assert.True (dlg.Canceled);
  1192. dlg.Dispose ();
  1193. top.Dispose ();
  1194. #if DEBUG_IDISPOSABLE
  1195. Assert.True (dlg.WasDisposed);
  1196. Assert.True (Top.WasDisposed);
  1197. Assert.NotNull (Top);
  1198. #endif
  1199. Shutdown ();
  1200. Assert.Null (Top);
  1201. return;
  1202. void Dlg_Ready (object sender, EventArgs e) { RequestStop (); }
  1203. }
  1204. [Fact]
  1205. [AutoInitShutdown]
  1206. public void Can_Access_Cancel_Property_After_Run ()
  1207. {
  1208. Dialog dlg = new ();
  1209. dlg.Ready += Dlg_Ready;
  1210. Run (dlg);
  1211. #if DEBUG_IDISPOSABLE
  1212. Assert.False (dlg.WasDisposed);
  1213. Assert.False (Top.WasDisposed);
  1214. Assert.Equal (dlg, Top);
  1215. #endif
  1216. Assert.True (dlg.Canceled);
  1217. // Run it again is possible because it isn't disposed yet
  1218. Run (dlg);
  1219. // Run another view without dispose the prior will throw an assertion
  1220. #if DEBUG_IDISPOSABLE
  1221. Dialog dlg2 = new ();
  1222. dlg2.Ready += Dlg_Ready;
  1223. Exception exception = Record.Exception (() => Run (dlg2));
  1224. Assert.NotNull (exception);
  1225. dlg.Dispose ();
  1226. // Now it's possible to tun dlg2 without throw
  1227. Run (dlg2);
  1228. Assert.True (dlg.WasDisposed);
  1229. Assert.False (Top.WasDisposed);
  1230. Assert.Equal (dlg2, Top);
  1231. Assert.False (dlg2.WasDisposed);
  1232. dlg2.Dispose ();
  1233. // Now an assertion will throw accessing the Canceled property
  1234. exception = Record.Exception (() => Assert.True (dlg.Canceled));
  1235. Assert.NotNull (exception);
  1236. Assert.True (Top.WasDisposed);
  1237. Shutdown ();
  1238. Assert.True (dlg2.WasDisposed);
  1239. Assert.Null (Top);
  1240. #endif
  1241. return;
  1242. void Dlg_Ready (object sender, EventArgs e)
  1243. {
  1244. ((Dialog)sender).Canceled = true;
  1245. RequestStop ();
  1246. }
  1247. }
  1248. }