DialogTests.cs 60 KB

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