DialogTests.cs 60 KB

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