DialogTests.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  41. // Now add a second button
  42. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  43. dlg.AddButton (new () { Text = btn2Text });
  44. var first = false;
  45. RunIteration (ref runstate, ref first);
  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. buttonRow = $"{CM.Glyphs.VLine}{btn1} {CM.Glyphs.VLine}";
  62. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  63. // Now add a second button
  64. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  65. dlg.AddButton (new () { Text = btn2Text });
  66. first = false;
  67. RunIteration (ref runstate, ref first);
  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. buttonRow = $"{CM.Glyphs.VLine}{new (' ', width - btn1.Length - 2)}{btn1}{CM.Glyphs.VLine}";
  84. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  85. // Now add a second button
  86. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  87. dlg.AddButton (new () { Text = btn2Text });
  88. first = false;
  89. RunIteration (ref runstate, ref first);
  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. buttonRow = $"{CM.Glyphs.VLine}{btn1}{new (' ', width - btn1.Length - 2)}{CM.Glyphs.VLine}";
  106. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  107. // Now add a second button
  108. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  109. dlg.AddButton (new () { Text = btn2Text });
  110. first = false;
  111. RunIteration (ref runstate, ref first);
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (
  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) = RunButtonTestDialog (title, width, Alignment.Center, button1, button2);
  725. button1.Visible = false;
  726. RunIteration (ref runstate, ref 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) = RunButtonTestDialog (title, width, Alignment.Fill, button1, button2);
  736. button1.Visible = false;
  737. RunIteration (ref runstate, ref firstIteration);
  738. buttonRow = $@"{CM.Glyphs.VLine} {btn2}{CM.Glyphs.VLine}";
  739. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  740. End (runstate);
  741. dlg.Dispose ();
  742. // Right
  743. Assert.Equal (width, buttonRow.Length);
  744. button1 = new Button { Text = btn1Text };
  745. button2 = new Button { Text = btn2Text };
  746. (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.End, button1, button2);
  747. button1.Visible = false;
  748. RunIteration (ref runstate, ref 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) = RunButtonTestDialog (title, width, Alignment.Start, button1, button2);
  757. button1.Visible = false;
  758. RunIteration (ref runstate, ref 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. Refresh ();
  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. // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)?? No
  889. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  890. }
  891. else
  892. {
  893. RequestStop ();
  894. }
  895. };
  896. Run (win);
  897. win.Dispose ();
  898. }
  899. [Fact]
  900. [AutoInitShutdown]
  901. public void Dialog_Opened_From_Another_Dialog ()
  902. {
  903. ((FakeDriver)Driver).SetBufferSize (30, 10);
  904. // Override CM
  905. Dialog.DefaultButtonAlignment = Alignment.Center;
  906. Dialog.DefaultBorderStyle = LineStyle.Single;
  907. Dialog.DefaultShadow = ShadowStyle.None;
  908. Button.DefaultShadow = ShadowStyle.None;
  909. var btn1 = new Button { Text = "press me 1" };
  910. Button btn2 = null;
  911. Button btn3 = null;
  912. string expected = null;
  913. btn1.Accepting += (s, e) =>
  914. {
  915. btn2 = new () { Text = "Show Sub" };
  916. btn3 = new () { Text = "Close" };
  917. btn3.Accepting += (s, e) => RequestStop ();
  918. btn2.Accepting += (s, e) =>
  919. {
  920. // Don't test MessageBox in Dialog unit tests!
  921. var subBtn = new Button { Text = "Ok", IsDefault = true };
  922. var subDlg = new Dialog { Text = "ya", Width = 20, Height = 5, Buttons = [subBtn] };
  923. subBtn.Accepting += (s, e) => RequestStop (subDlg);
  924. Run (subDlg);
  925. };
  926. var dlg = new Dialog
  927. {
  928. Buttons = [btn2, btn3],
  929. Width = Dim.Percent (85),
  930. Height = Dim.Percent (85)
  931. };
  932. Run (dlg);
  933. dlg.Dispose ();
  934. };
  935. var btn =
  936. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
  937. int iterations = -1;
  938. Iteration += (s, a) =>
  939. {
  940. iterations++;
  941. if (iterations == 0)
  942. {
  943. Assert.False (btn1.NewKeyDownEvent (Key.Space));
  944. }
  945. else if (iterations == 1)
  946. {
  947. expected = @$"
  948. ┌───────────────────────┐
  949. │ │
  950. │ │
  951. │ │
  952. │ │
  953. │ │
  954. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  955. └───────────────────────┘";
  956. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  957. Assert.False (btn2.NewKeyDownEvent (Key.Space));
  958. }
  959. else if (iterations == 2)
  960. {
  961. TestHelpers.AssertDriverContentsWithFrameAre (
  962. @$"
  963. ┌───────────────────────┐
  964. │ ┌──────────────────┐ │
  965. │ │ya │ │
  966. │ │ │ │
  967. │ │ {btn} │ │
  968. │ └──────────────────┘ │
  969. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  970. └───────────────────────┘",
  971. _output
  972. );
  973. Assert.False (Top!.NewKeyDownEvent (Key.Enter));
  974. }
  975. else if (iterations == 3)
  976. {
  977. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  978. Assert.False (btn3.NewKeyDownEvent (Key.Space));
  979. }
  980. else if (iterations == 4)
  981. {
  982. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  983. RequestStop ();
  984. }
  985. };
  986. Run ().Dispose ();
  987. Shutdown ();
  988. Assert.Equal (4, iterations);
  989. }
  990. [Fact]
  991. [AutoInitShutdown]
  992. public void FileDialog_FileSystemWatcher ()
  993. {
  994. for (var i = 0; i < 8; i++)
  995. {
  996. var fd = new FileDialog ();
  997. fd.Ready += (s, e) => RequestStop ();
  998. Run (fd);
  999. fd.Dispose ();
  1000. }
  1001. }
  1002. [Fact]
  1003. [AutoInitShutdown]
  1004. public void Location_Default ()
  1005. {
  1006. var d = new Dialog ()
  1007. {
  1008. Width = Dim.Percent (85),
  1009. Height = Dim.Percent (85)
  1010. };
  1011. Begin (d);
  1012. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1013. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1014. var expected = 7;
  1015. Assert.Equal (new (expected, expected), d.Frame.Location);
  1016. d.Dispose ();
  1017. }
  1018. [Fact]
  1019. [AutoInitShutdown]
  1020. public void Location_Not_Default ()
  1021. {
  1022. var d = new Dialog { X = 1, Y = 1 };
  1023. Begin (d);
  1024. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1025. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1026. var expected = 1;
  1027. Assert.Equal (new (expected, expected), d.Frame.Location);
  1028. d.Dispose ();
  1029. }
  1030. [Fact]
  1031. [AutoInitShutdown]
  1032. public void Location_When_Application_Top_Not_Default ()
  1033. {
  1034. // Override CM
  1035. Window.DefaultBorderStyle = LineStyle.Single;
  1036. Dialog.DefaultButtonAlignment = Alignment.Center;
  1037. Dialog.DefaultBorderStyle = LineStyle.Single;
  1038. Dialog.DefaultShadow = ShadowStyle.None;
  1039. Button.DefaultShadow = ShadowStyle.None;
  1040. var expected = 5;
  1041. var d = new Dialog { X = expected, Y = expected, Height = 5, Width = 5 };
  1042. Begin (d);
  1043. ((FakeDriver)Driver).SetBufferSize (20, 10);
  1044. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1045. Assert.Equal (new (expected, expected), d.Frame.Location);
  1046. TestHelpers.AssertDriverContentsWithFrameAre (
  1047. @"
  1048. ┌───┐
  1049. │ │
  1050. │ │
  1051. │ │
  1052. └───┘",
  1053. _output
  1054. );
  1055. d.Dispose ();
  1056. }
  1057. // [Fact]
  1058. // [AutoInitShutdown]
  1059. // public void Location_When_Not_Application_Top_Not_Default ()
  1060. // {
  1061. // var top = new Toplevel ();
  1062. // top.BorderStyle = LineStyle.Double;
  1063. // int iterations = -1;
  1064. // // Override CM
  1065. // Window.DefaultBorderStyle = LineStyle.Single;
  1066. // Dialog.DefaultButtonAlignment = Alignment.Center;
  1067. // Dialog.DefaultBorderStyle = LineStyle.Single;
  1068. // Dialog.DefaultShadow = ShadowStyle.None;
  1069. // Iteration += (s, a) =>
  1070. // {
  1071. // iterations++;
  1072. // if (iterations == 0)
  1073. // {
  1074. // var d = new Dialog { X = 5, Y = 5, Height = 3, Width = 5 };
  1075. // RunState rs = Begin (d);
  1076. // Assert.Equal (new (5, 5), d.Frame.Location);
  1077. // TestHelpers.AssertDriverContentsWithFrameAre (
  1078. // @"
  1079. //╔══════════════════╗
  1080. //║ ║
  1081. //║ ║
  1082. //║ ║
  1083. //║ ║
  1084. //║ ┌───┐ ║
  1085. //║ │ │ ║
  1086. //║ └───┘ ║
  1087. //║ ║
  1088. //╚══════════════════╝",
  1089. // _output
  1090. // );
  1091. // End (rs);
  1092. // d.Dispose ();
  1093. // d = new ()
  1094. // {
  1095. // X = 5, Y = 5,
  1096. // Width = Dim.Percent (85),
  1097. // Height = Dim.Percent (85)
  1098. // };
  1099. // rs = Begin (d);
  1100. // TestHelpers.AssertDriverContentsWithFrameAre (
  1101. // @"
  1102. //╔══════════════════╗
  1103. //║ ║
  1104. //║ ║
  1105. //║ ║
  1106. //║ ║
  1107. //║ ┌──────────────
  1108. //║ │
  1109. //║ │
  1110. //║ │
  1111. //╚════│ ",
  1112. // _output
  1113. // );
  1114. // End (rs);
  1115. // d.Dispose ();
  1116. // }
  1117. // else if (iterations > 0)
  1118. // {
  1119. // RequestStop ();
  1120. // }
  1121. // };
  1122. // ((FakeDriver)Driver).SetBufferSize (20, 10);
  1123. // Run (top);
  1124. // top.Dispose ();
  1125. // }
  1126. [Fact]
  1127. [AutoInitShutdown]
  1128. public void One_Button_Works ()
  1129. {
  1130. RunState runstate = null;
  1131. var d = (FakeDriver)Driver;
  1132. Button.DefaultShadow = ShadowStyle.None;
  1133. var title = "";
  1134. var btnText = "ok";
  1135. var buttonRow =
  1136. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  1137. int width = buttonRow.Length;
  1138. d.SetBufferSize (buttonRow.Length, 10);
  1139. (runstate, Dialog dlg) = RunButtonTestDialog (
  1140. title,
  1141. width,
  1142. Alignment.Center,
  1143. new Button { Text = btnText }
  1144. );
  1145. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1146. End (runstate);
  1147. dlg.Dispose ();
  1148. }
  1149. [Fact]
  1150. [AutoInitShutdown]
  1151. public void Size_Default ()
  1152. {
  1153. var d = new Dialog ()
  1154. {
  1155. Width = Dim.Percent (85),
  1156. Height = Dim.Percent (85)
  1157. };
  1158. Begin (d);
  1159. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1160. // Default size is Percent(85)
  1161. Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
  1162. d.Dispose ();
  1163. }
  1164. [Fact]
  1165. [AutoInitShutdown]
  1166. public void Size_Not_Default ()
  1167. {
  1168. Dialog.DefaultShadow = ShadowStyle.None;
  1169. Button.DefaultShadow = ShadowStyle.None;
  1170. var d = new Dialog { Width = 50, Height = 50 };
  1171. Begin (d);
  1172. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1173. // Default size is Percent(85)
  1174. Assert.Equal (new (50, 50), d.Frame.Size);
  1175. d.Dispose ();
  1176. }
  1177. [Fact]
  1178. [SetupFakeDriver]
  1179. public void Zero_Buttons_Works ()
  1180. {
  1181. RunState runstate = null;
  1182. var d = (FakeDriver)Driver;
  1183. var title = "1234";
  1184. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.VLine}";
  1185. int width = buttonRow.Length;
  1186. d.SetBufferSize (buttonRow.Length, 3);
  1187. (runstate, Dialog dlg) = RunButtonTestDialog (title, width, Alignment.Center, null);
  1188. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1189. End (runstate);
  1190. dlg.Dispose ();
  1191. }
  1192. private (RunState, Dialog) RunButtonTestDialog (
  1193. string title,
  1194. int width,
  1195. Alignment align,
  1196. params Button [] btns
  1197. )
  1198. {
  1199. // Override CM
  1200. Dialog.DefaultButtonAlignment = Alignment.Center;
  1201. Dialog.DefaultBorderStyle = LineStyle.Single;
  1202. Dialog.DefaultShadow = ShadowStyle.None;
  1203. Button.DefaultShadow = ShadowStyle.None;
  1204. var dlg = new Dialog
  1205. {
  1206. Title = title,
  1207. X = 0,
  1208. Y = 0,
  1209. Width = width,
  1210. Height = 1,
  1211. ButtonAlignment = align,
  1212. Buttons = btns
  1213. };
  1214. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  1215. dlg.Border.Thickness = new (1, 0, 1, 0);
  1216. return (Begin (dlg), dlg);
  1217. }
  1218. [Fact]
  1219. [AutoInitShutdown]
  1220. public void Run_Does_Not_Dispose_Dialog ()
  1221. {
  1222. var top = new Toplevel ();
  1223. Dialog dlg = new ();
  1224. dlg.Ready += Dlg_Ready;
  1225. Run (dlg);
  1226. #if DEBUG_IDISPOSABLE
  1227. Assert.False (dlg.WasDisposed);
  1228. Assert.False (Top.WasDisposed);
  1229. Assert.NotEqual (top, Top);
  1230. Assert.Equal (dlg, Top);
  1231. #endif
  1232. // dlg wasn't disposed yet and it's possible to access to his properties
  1233. Assert.False (dlg.Canceled);
  1234. Assert.NotNull (dlg);
  1235. dlg.Canceled = true;
  1236. Assert.True (dlg.Canceled);
  1237. dlg.Dispose ();
  1238. top.Dispose ();
  1239. #if DEBUG_IDISPOSABLE
  1240. Assert.True (dlg.WasDisposed);
  1241. Assert.True (Top.WasDisposed);
  1242. Assert.NotNull (Top);
  1243. #endif
  1244. Shutdown ();
  1245. Assert.Null (Top);
  1246. return;
  1247. void Dlg_Ready (object sender, EventArgs e) { RequestStop (); }
  1248. }
  1249. [Fact]
  1250. [AutoInitShutdown]
  1251. public void Can_Access_Cancel_Property_After_Run ()
  1252. {
  1253. Dialog dlg = new ();
  1254. dlg.Ready += Dlg_Ready;
  1255. Run (dlg);
  1256. #if DEBUG_IDISPOSABLE
  1257. Assert.False (dlg.WasDisposed);
  1258. Assert.False (Top.WasDisposed);
  1259. Assert.Equal (dlg, Top);
  1260. #endif
  1261. Assert.True (dlg.Canceled);
  1262. // Run it again is possible because it isn't disposed yet
  1263. Run (dlg);
  1264. // Run another view without dispose the prior will throw an assertion
  1265. #if DEBUG_IDISPOSABLE
  1266. Dialog dlg2 = new ();
  1267. dlg2.Ready += Dlg_Ready;
  1268. Exception exception = Record.Exception (() => Run (dlg2));
  1269. Assert.NotNull (exception);
  1270. dlg.Dispose ();
  1271. // Now it's possible to tun dlg2 without throw
  1272. Run (dlg2);
  1273. Assert.True (dlg.WasDisposed);
  1274. Assert.False (Top.WasDisposed);
  1275. Assert.Equal (dlg2, Top);
  1276. Assert.False (dlg2.WasDisposed);
  1277. dlg2.Dispose ();
  1278. // Now an assertion will throw accessing the Canceled property
  1279. exception = Record.Exception (() => Assert.True (dlg.Canceled));
  1280. Assert.NotNull (exception);
  1281. Assert.True (Top.WasDisposed);
  1282. Shutdown ();
  1283. Assert.True (dlg2.WasDisposed);
  1284. Assert.Null (Top);
  1285. #endif
  1286. return;
  1287. void Dlg_Ready (object sender, EventArgs e)
  1288. {
  1289. ((Dialog)sender).Canceled = true;
  1290. RequestStop ();
  1291. }
  1292. }
  1293. }