2
0

DialogTests.cs 58 KB

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