DialogTests.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. #nullable enable
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.App.Application;
  5. namespace Terminal.Gui.DialogTests;
  6. public class DialogTests (ITestOutputHelper output)
  7. {
  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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  17. var btn2Text = "no";
  18. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  19. // We test with one button first, but do this to get the width right for 2
  20. int width = $@"{Glyphs.VLine} {btn1} {btn2} {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 () { 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 = $"{Glyphs.VLine} {btn1} {Glyphs.VLine}";
  40. RunIteration (ref runState);
  41. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  42. // Now add a second button
  43. buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {Glyphs.VLine}";
  44. dlg.AddButton (new () { Text = btn2Text });
  45. RunIteration (ref runState);
  46. DriverAssert.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 () { 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 = $"{Glyphs.VLine}{btn1} {Glyphs.VLine}";
  63. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  64. // Now add a second button
  65. buttonRow = $"{Glyphs.VLine}{btn1} {btn2}{Glyphs.VLine}";
  66. dlg.AddButton (new () { Text = btn2Text });
  67. RunIteration (ref runState);
  68. DriverAssert.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 () { 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 = $"{Glyphs.VLine}{new (' ', width - btn1.Length - 2)}{btn1}{Glyphs.VLine}";
  85. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  86. // Now add a second button
  87. buttonRow = $"{Glyphs.VLine} {btn1} {btn2}{Glyphs.VLine}";
  88. dlg.AddButton (new () { Text = btn2Text });
  89. RunIteration (ref runState);
  90. DriverAssert.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 () { 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 = $"{Glyphs.VLine}{btn1}{new (' ', width - btn1.Length - 2)}{Glyphs.VLine}";
  107. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  108. // Now add a second button
  109. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {Glyphs.VLine}";
  110. dlg.AddButton (new () { Text = btn2Text });
  111. RunIteration (ref runState);
  112. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  128. var btn2Text = "no";
  129. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  130. var btn3Text = "maybe";
  131. var btn3 = $"{Glyphs.LeftBracket} {btn3Text} {Glyphs.RightBracket}";
  132. var btn4Text = "never";
  133. var btn4 = $"{Glyphs.LeftBracket} {btn4Text} {Glyphs.RightBracket}";
  134. var buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  148. End (runState);
  149. dlg.Dispose ();
  150. // Justify
  151. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  163. End (runState);
  164. dlg.Dispose ();
  165. // Right
  166. buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  178. End (runState);
  179. dlg.Dispose ();
  180. // Left
  181. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  208. var btn2Text = "no";
  209. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  210. var btn3Text = "maybe";
  211. var btn3 = $"{Glyphs.LeftBracket} {btn3Text} {Glyphs.RightBracket}";
  212. var btn4Text = "never";
  213. var btn4 = $"{Glyphs.LeftBracket} {btn4Text} {Glyphs.RightBracket}";
  214. var buttonRow = string.Empty;
  215. var width = 30;
  216. d.SetBufferSize (width, 1);
  217. // Default - Center
  218. buttonRow =
  219. $"{Glyphs.VLine} yes {Glyphs.RightBracket}{btn2}{btn3}{Glyphs.LeftBracket} never{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  231. End (runState);
  232. dlg.Dispose ();
  233. // Justify
  234. buttonRow =
  235. $"{Glyphs.VLine}{Glyphs.LeftBracket} yes {Glyphs.LeftBracket} no {Glyphs.LeftBracket} maybe {Glyphs.LeftBracket} never {Glyphs.RightBracket}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  246. End (runState);
  247. dlg.Dispose ();
  248. // Right
  249. buttonRow = $"{Glyphs.VLine}es {Glyphs.RightBracket}{btn2}{btn3}{btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  261. End (runState);
  262. dlg.Dispose ();
  263. // Left
  264. buttonRow = $"{Glyphs.VLine}{btn1}{btn2}{btn3}{Glyphs.LeftBracket} neve{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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  290. var btn2Text = "really long button 2";
  291. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  292. var btn3Text = "really long button 3";
  293. var btn3 = $"{Glyphs.LeftBracket} {btn3Text} {Glyphs.RightBracket}";
  294. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  295. var btn4 = $"{Glyphs.LeftBracket} {btn4Text} {Glyphs.RightBracket}";
  296. // Note extra spaces to make dialog even wider
  297. // 123456 1234567
  298. var buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  312. End (runState);
  313. dlg.Dispose ();
  314. // Justify
  315. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  327. End (runState);
  328. dlg.Dispose ();
  329. // Right
  330. buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  342. End (runState);
  343. dlg.Dispose ();
  344. // Left
  345. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  372. var btn2Text = "no";
  373. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  374. var btn3Text = "你你你你你"; // This is a wide char
  375. var btn3 = $"{Glyphs.LeftBracket} {btn3Text} {Glyphs.RightBracket}";
  376. // Requires a Nerd Font
  377. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  378. var btn4 = $"{Glyphs.LeftBracket} {btn4Text} {Glyphs.RightBracket}";
  379. // Note extra spaces to make dialog even wider
  380. // 123456 123456
  381. var buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  395. End (runState);
  396. dlg.Dispose ();
  397. // Justify
  398. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  410. End (runState);
  411. dlg.Dispose ();
  412. // Right
  413. buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  425. End (runState);
  426. dlg.Dispose ();
  427. // Left
  428. buttonRow = $"{Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {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. DriverAssert.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. $"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  466. End (runState);
  467. dlg.Dispose ();
  468. // Justify
  469. buttonRow =
  470. $"{Glyphs.VLine}{Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  479. End (runState);
  480. dlg.Dispose ();
  481. // Right
  482. buttonRow =
  483. $"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  492. End (runState);
  493. dlg.Dispose ();
  494. // Left
  495. buttonRow =
  496. $"{Glyphs.VLine}{Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  505. End (runState);
  506. dlg.Dispose ();
  507. // Wider
  508. buttonRow =
  509. $"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  519. End (runState);
  520. dlg.Dispose ();
  521. // Justify
  522. buttonRow =
  523. $"{Glyphs.VLine}{Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  532. End (runState);
  533. dlg.Dispose ();
  534. // Right
  535. buttonRow =
  536. $"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  545. End (runState);
  546. dlg.Dispose ();
  547. // Left
  548. buttonRow =
  549. $"{Glyphs.VLine}{Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  573. var btn2Text = "no";
  574. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  575. var btn3Text = "maybe";
  576. var btn3 = $"{Glyphs.LeftBracket} {btn3Text} {Glyphs.RightBracket}";
  577. var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {btn3} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  589. End (runState);
  590. dlg.Dispose ();
  591. // Justify
  592. buttonRow = $@"{Glyphs.VLine}{btn1} {btn2} {btn3}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  603. End (runState);
  604. dlg.Dispose ();
  605. // Right
  606. buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {btn3}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  617. End (runState);
  618. dlg.Dispose ();
  619. // Left
  620. buttonRow = $@"{Glyphs.VLine}{btn1} {btn2} {btn3} {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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  646. var btn2Text = "no";
  647. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  648. var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  659. End (runState);
  660. dlg.Dispose ();
  661. // Justify
  662. buttonRow = $@"{Glyphs.VLine}{btn1} {btn2}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  672. End (runState);
  673. dlg.Dispose ();
  674. // Right
  675. buttonRow = $@"{Glyphs.VLine} {btn1} {btn2}{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. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  685. End (runState);
  686. dlg.Dispose ();
  687. // Left
  688. buttonRow = $@"{Glyphs.VLine}{btn1} {btn2} {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. DriverAssert.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 = $"{Glyphs.LeftBracket} {btn1Text} {Glyphs.RightBracket}";
  714. var btn2Text = "no";
  715. var btn2 = $"{Glyphs.LeftBracket} {btn2Text} {Glyphs.RightBracket}";
  716. var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {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 () { Text = btn1Text };
  723. button2 = new () { Text = btn2Text };
  724. (runState, dlg) = BeginButtonTestDialog (title, width, Alignment.Center, button1, button2);
  725. button1.Visible = false;
  726. RunIteration (ref runState, firstIteration);
  727. buttonRow = $@"{Glyphs.VLine} {btn2} {Glyphs.VLine}";
  728. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  729. End (runState);
  730. dlg.Dispose ();
  731. // Justify
  732. Assert.Equal (width, buttonRow.Length);
  733. button1 = new () { Text = btn1Text };
  734. button2 = new () { Text = btn2Text };
  735. (runState, dlg) = BeginButtonTestDialog (title, width, Alignment.Fill, button1, button2);
  736. button1.Visible = false;
  737. RunIteration (ref runState, firstIteration);
  738. buttonRow = $@"{Glyphs.VLine} {btn2}{Glyphs.VLine}";
  739. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  740. End (runState);
  741. dlg.Dispose ();
  742. // Right
  743. Assert.Equal (width, buttonRow.Length);
  744. button1 = new () { Text = btn1Text };
  745. button2 = new () { Text = btn2Text };
  746. (runState, dlg) = BeginButtonTestDialog (title, width, Alignment.End, button1, button2);
  747. button1.Visible = false;
  748. RunIteration (ref runState, firstIteration);
  749. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  750. End (runState);
  751. dlg.Dispose ();
  752. // Left
  753. Assert.Equal (width, buttonRow.Length);
  754. button1 = new () { Text = btn1Text };
  755. button2 = new () { Text = btn2Text };
  756. (runState, dlg) = BeginButtonTestDialog (title, width, Alignment.Start, button1, button2);
  757. button1.Visible = false;
  758. RunIteration (ref runState, firstIteration);
  759. buttonRow = $@"{Glyphs.VLine} {btn2} {Glyphs.VLine}";
  760. DriverAssert.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 = $"{Glyphs.LeftBracket} Ok {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. LayoutAndDraw ();
  791. var expected = @$"
  792. ┌──────────────────┐
  793. │┌────────────────┐│
  794. ││ {btn} ││
  795. │└────────────────┘│
  796. └──────────────────┘";
  797. _ = DriverAssert.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. LayoutAndDraw ();
  889. // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)?? No
  890. _ = DriverAssert.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. $"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} Ok {Glyphs.RightDefaultIndicator}{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. LayoutAndDraw ();
  948. break;
  949. case 1:
  950. Assert.False (btn1.NewKeyDownEvent (Key.Space));
  951. break;
  952. case 2:
  953. LayoutAndDraw ();
  954. expected = @$"
  955. ┌───────────────────────┐
  956. │ │
  957. │ │
  958. │ │
  959. │ │
  960. │ │
  961. │{Glyphs.LeftBracket} Show Sub {Glyphs.RightBracket} {Glyphs.LeftBracket} Close {Glyphs.RightBracket} │
  962. └───────────────────────┘";
  963. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  964. Assert.False (btn2!.NewKeyDownEvent (Key.Space));
  965. break;
  966. case 3:
  967. LayoutAndDraw ();
  968. DriverAssert.AssertDriverContentsWithFrameAre (
  969. @$"
  970. ┌───────────────────────┐
  971. │ ┌──────────────────┐ │
  972. │ │ya │ │
  973. │ │ │ │
  974. │ │ {btn} │ │
  975. │ └──────────────────┘ │
  976. │{Glyphs.LeftBracket} Show Sub {Glyphs.RightBracket} {Glyphs.LeftBracket} Close {Glyphs.RightBracket} │
  977. └───────────────────────┘",
  978. output
  979. );
  980. Assert.False (Top!.NewKeyDownEvent (Key.Enter));
  981. break;
  982. case 4:
  983. LayoutAndDraw ();
  984. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  985. Assert.False (btn3!.NewKeyDownEvent (Key.Space));
  986. break;
  987. case 5:
  988. DriverAssert.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. DriverAssert.AssertDriverContentsWithFrameAre (
  1054. @"
  1055. ┌───┐
  1056. │ │
  1057. │ │
  1058. │ │
  1059. └───┘",
  1060. output
  1061. );
  1062. d.Dispose ();
  1063. }
  1064. [Fact]
  1065. [AutoInitShutdown]
  1066. public void One_Button_Works ()
  1067. {
  1068. RunState? runState = null;
  1069. var d = (FakeDriver)Driver!;
  1070. Button.DefaultShadow = ShadowStyle.None;
  1071. var title = "";
  1072. var btnText = "ok";
  1073. var buttonRow =
  1074. $"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {Glyphs.VLine}";
  1075. int width = buttonRow.Length;
  1076. d.SetBufferSize (buttonRow.Length, 10);
  1077. (runState, Dialog dlg) = BeginButtonTestDialog (
  1078. title,
  1079. width,
  1080. Alignment.Center,
  1081. new Button { Text = btnText }
  1082. );
  1083. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  1084. End (runState);
  1085. dlg.Dispose ();
  1086. }
  1087. [Fact]
  1088. [AutoInitShutdown]
  1089. public void Size_Default ()
  1090. {
  1091. var d = new Dialog
  1092. {
  1093. Width = Dim.Percent (85),
  1094. Height = Dim.Percent (85)
  1095. };
  1096. Begin (d);
  1097. ((FakeDriver)Driver!).SetBufferSize (100, 100);
  1098. // Default size is Percent(85)
  1099. Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
  1100. d.Dispose ();
  1101. }
  1102. [Fact]
  1103. [AutoInitShutdown]
  1104. public void Size_Not_Default ()
  1105. {
  1106. Dialog.DefaultShadow = ShadowStyle.None;
  1107. Button.DefaultShadow = ShadowStyle.None;
  1108. var d = new Dialog { Width = 50, Height = 50 };
  1109. Begin (d);
  1110. ((FakeDriver)Driver!).SetBufferSize (100, 100);
  1111. // Default size is Percent(85)
  1112. Assert.Equal (new (50, 50), d.Frame.Size);
  1113. d.Dispose ();
  1114. }
  1115. [Fact]
  1116. [SetupFakeDriver]
  1117. public void Zero_Buttons_Works ()
  1118. {
  1119. RunState? runState = null;
  1120. var d = (FakeDriver)Driver!;
  1121. var title = "1234";
  1122. var buttonRow = $"{Glyphs.VLine} {Glyphs.VLine}";
  1123. int width = buttonRow.Length;
  1124. d.SetBufferSize (buttonRow.Length, 3);
  1125. (runState, Dialog dlg) = BeginButtonTestDialog (title, width, Alignment.Center, null);
  1126. DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  1127. End (runState);
  1128. dlg.Dispose ();
  1129. }
  1130. private (RunState, Dialog) BeginButtonTestDialog (
  1131. string title,
  1132. int width,
  1133. Alignment align,
  1134. params Button [] btns
  1135. )
  1136. {
  1137. // Override CM
  1138. Dialog.DefaultButtonAlignment = Alignment.Center;
  1139. Dialog.DefaultBorderStyle = LineStyle.Single;
  1140. Dialog.DefaultShadow = ShadowStyle.None;
  1141. Button.DefaultShadow = ShadowStyle.None;
  1142. var dlg = new Dialog
  1143. {
  1144. Title = title,
  1145. X = 0,
  1146. Y = 0,
  1147. Width = width,
  1148. Height = 1,
  1149. ButtonAlignment = align,
  1150. Buttons = btns
  1151. };
  1152. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  1153. dlg.Border!.Thickness = new (1, 0, 1, 0);
  1154. RunState runState = Begin (dlg);
  1155. dlg.SetNeedsDraw ();
  1156. dlg.SetNeedsLayout ();
  1157. dlg.Layout ();
  1158. dlg.Draw ();
  1159. return (runState, dlg);
  1160. }
  1161. [Fact]
  1162. [AutoInitShutdown]
  1163. public void Run_Does_Not_Dispose_Dialog ()
  1164. {
  1165. var top = new Toplevel ();
  1166. Dialog dlg = new ();
  1167. dlg.Ready += Dlg_Ready;
  1168. Run (dlg);
  1169. #if DEBUG_IDISPOSABLE
  1170. Assert.False (dlg.WasDisposed);
  1171. Assert.False (Top!.WasDisposed);
  1172. Assert.NotEqual (top, Top);
  1173. Assert.Equal (dlg, Top);
  1174. #endif
  1175. // dlg wasn't disposed yet and it's possible to access to his properties
  1176. Assert.False (dlg.Canceled);
  1177. Assert.NotNull (dlg);
  1178. dlg.Canceled = true;
  1179. Assert.True (dlg.Canceled);
  1180. dlg.Dispose ();
  1181. top.Dispose ();
  1182. #if DEBUG_IDISPOSABLE
  1183. Assert.True (dlg.WasDisposed);
  1184. Assert.True (Top.WasDisposed);
  1185. Assert.NotNull (Top);
  1186. #endif
  1187. Shutdown ();
  1188. Assert.Null (Top);
  1189. return;
  1190. void Dlg_Ready (object? sender, EventArgs e) { RequestStop (); }
  1191. }
  1192. [Fact]
  1193. [AutoInitShutdown]
  1194. public void Can_Access_Cancel_Property_After_Run ()
  1195. {
  1196. Dialog dlg = new ();
  1197. dlg.Ready += Dlg_Ready;
  1198. Run (dlg);
  1199. #if DEBUG_IDISPOSABLE
  1200. Assert.False (dlg.WasDisposed);
  1201. Assert.False (Top!.WasDisposed);
  1202. Assert.Equal (dlg, Top);
  1203. #endif
  1204. Assert.True (dlg.Canceled);
  1205. // Run it again is possible because it isn't disposed yet
  1206. Run (dlg);
  1207. // Run another view without dispose the prior will throw an assertion
  1208. #if DEBUG_IDISPOSABLE
  1209. Dialog dlg2 = new ();
  1210. dlg2.Ready += Dlg_Ready;
  1211. Exception exception = Record.Exception (() => Run (dlg2));
  1212. Assert.NotNull (exception);
  1213. dlg.Dispose ();
  1214. // Now it's possible to tun dlg2 without throw
  1215. Run (dlg2);
  1216. Assert.True (dlg.WasDisposed);
  1217. Assert.False (Top.WasDisposed);
  1218. Assert.Equal (dlg2, Top);
  1219. Assert.False (dlg2.WasDisposed);
  1220. dlg2.Dispose ();
  1221. // Now an assertion will throw accessing the Canceled property
  1222. exception = Record.Exception (() => Assert.True (dlg.Canceled))!;
  1223. Assert.NotNull (exception);
  1224. Assert.True (Top.WasDisposed);
  1225. Shutdown ();
  1226. Assert.True (dlg2.WasDisposed);
  1227. Assert.Null (Top);
  1228. #endif
  1229. return;
  1230. void Dlg_Ready (object? sender, EventArgs e)
  1231. {
  1232. ((Dialog)sender!).Canceled = true;
  1233. RequestStop ();
  1234. }
  1235. }
  1236. [Fact]
  1237. [AutoInitShutdown]
  1238. public void Modal_Captures_All_Mouse ()
  1239. {
  1240. Toplevel top = new Toplevel ()
  1241. {
  1242. Id = "top",
  1243. };
  1244. var d = new Dialog
  1245. {
  1246. Width = 10,
  1247. Height = 10,
  1248. X = 1,
  1249. Y = 1
  1250. };
  1251. ((FakeDriver)Driver!).SetBufferSize (20, 20);
  1252. int iterations = 0;
  1253. Iteration += (s, a) =>
  1254. {
  1255. if (++iterations > 2)
  1256. {
  1257. RequestStop ();
  1258. }
  1259. if (iterations == 1)
  1260. {
  1261. Application.Run (d);
  1262. d.Dispose ();
  1263. }
  1264. else if (iterations == 2)
  1265. {
  1266. // Mouse click outside of dialog
  1267. Application.RaiseMouseEvent (new MouseEventArgs () { Flags = MouseFlags.Button1Clicked, ScreenPosition = new Point (0, 0) });
  1268. }
  1269. };
  1270. top.MouseEvent += (s, e) =>
  1271. {
  1272. // This should not be called because the dialog is modal
  1273. Assert.False (true, "Mouse event should not be captured by the top level when a dialog is modal.");
  1274. };
  1275. Application.Run (top);
  1276. top.Dispose ();
  1277. Application.Shutdown ();
  1278. }
  1279. }