DialogTests.cs 58 KB

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