DialogTests.cs 61 KB

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