DialogTests.cs 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 = Dialog.ButtonAlignments.Center,
  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 Thickness (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 Button { 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 Dialog
  46. {
  47. Title = title,
  48. Width = width,
  49. Height = 1,
  50. ButtonAlignment = Dialog.ButtonAlignments.Justify,
  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 Thickness (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 Button { 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 Dialog
  68. {
  69. Title = title,
  70. Width = width,
  71. Height = 1,
  72. ButtonAlignment = Dialog.ButtonAlignments.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 Thickness (1, 0, 1, 0);
  77. runstate = Begin (dlg);
  78. buttonRow = $"{CM.Glyphs.VLine}{new string (' ', 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 Button { 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 Dialog
  90. {
  91. Title = title,
  92. Width = width,
  93. Height = 1,
  94. ButtonAlignment = Dialog.ButtonAlignments.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 Thickness (1, 0, 1, 0);
  99. runstate = Begin (dlg);
  100. buttonRow = $"{CM.Glyphs.VLine}{btn1}{new string (' ', 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 Button { 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. Dialog.ButtonAlignments.Center,
  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. Dialog.ButtonAlignments.Justify,
  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. Dialog.ButtonAlignments.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. Dialog.ButtonAlignments.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. Dialog.ButtonAlignments.Center,
  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. $"{
  227. CM.Glyphs.VLine
  228. }{
  229. CM.Glyphs.LeftBracket
  230. } yes {
  231. CM.Glyphs.LeftBracket
  232. } no {
  233. CM.Glyphs.LeftBracket
  234. } maybe {
  235. CM.Glyphs.LeftBracket
  236. } never {
  237. CM.Glyphs.RightBracket
  238. }{
  239. CM.Glyphs.VLine
  240. }";
  241. (runstate, dlg) = RunButtonTestDialog (
  242. title,
  243. width,
  244. Dialog.ButtonAlignments.Justify,
  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. // Right
  254. buttonRow = $"{CM.Glyphs.VLine}{CM.Glyphs.RightBracket} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  255. (runstate, dlg) = RunButtonTestDialog (
  256. title,
  257. width,
  258. Dialog.ButtonAlignments.Right,
  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. // Left
  268. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {CM.Glyphs.LeftBracket} n{CM.Glyphs.VLine}";
  269. (runstate, dlg) = RunButtonTestDialog (
  270. title,
  271. width,
  272. Dialog.ButtonAlignments.Left,
  273. new Button { Text = btn1Text },
  274. new Button { Text = btn2Text },
  275. new Button { Text = btn3Text },
  276. new Button { Text = btn4Text }
  277. );
  278. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  279. End (runstate);
  280. dlg.Dispose ();
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void ButtonAlignment_Four_WideOdd ()
  285. {
  286. RunState runstate = null;
  287. var d = (FakeDriver)Driver;
  288. var title = "1234";
  289. // E.g "|[ yes ][ no ][ maybe ]|"
  290. var btn1Text = "really long button 1";
  291. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  292. var btn2Text = "really long button 2";
  293. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  294. var btn3Text = "really long button 3";
  295. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  296. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  297. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  298. // Note extra spaces to make dialog even wider
  299. // 123456 1234567
  300. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  301. int width = buttonRow.Length;
  302. d.SetBufferSize (buttonRow.Length, 1);
  303. // Default - Center
  304. (runstate, Dialog dlg) = RunButtonTestDialog (
  305. title,
  306. width,
  307. Dialog.ButtonAlignments.Center,
  308. new Button { Text = btn1Text },
  309. new Button { Text = btn2Text },
  310. new Button { Text = btn3Text },
  311. new Button { Text = btn4Text }
  312. );
  313. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  314. End (runstate);
  315. dlg.Dispose ();
  316. // Justify
  317. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  318. Assert.Equal (width, buttonRow.Length);
  319. (runstate, dlg) = RunButtonTestDialog (
  320. title,
  321. width,
  322. Dialog.ButtonAlignments.Justify,
  323. new Button { Text = btn1Text },
  324. new Button { Text = btn2Text },
  325. new Button { Text = btn3Text },
  326. new Button { Text = btn4Text }
  327. );
  328. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  329. End (runstate);
  330. dlg.Dispose ();
  331. // Right
  332. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  333. Assert.Equal (width, buttonRow.Length);
  334. (runstate, dlg) = RunButtonTestDialog (
  335. title,
  336. width,
  337. Dialog.ButtonAlignments.Right,
  338. new Button { Text = btn1Text },
  339. new Button { Text = btn2Text },
  340. new Button { Text = btn3Text },
  341. new Button { Text = btn4Text }
  342. );
  343. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  344. End (runstate);
  345. dlg.Dispose ();
  346. // Left
  347. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  348. Assert.Equal (width, buttonRow.Length);
  349. (runstate, dlg) = RunButtonTestDialog (
  350. title,
  351. width,
  352. Dialog.ButtonAlignments.Left,
  353. new Button { Text = btn1Text },
  354. new Button { Text = btn2Text },
  355. new Button { Text = btn3Text },
  356. new Button { Text = btn4Text }
  357. );
  358. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  359. End (runstate);
  360. dlg.Dispose ();
  361. }
  362. [Fact]
  363. [AutoInitShutdown]
  364. public void ButtonAlignment_Four_Wider ()
  365. {
  366. RunState runstate = null;
  367. var d = (FakeDriver)Driver;
  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. Dialog.ButtonAlignments.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. Dialog.ButtonAlignments.Justify,
  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. Dialog.ButtonAlignments.Right,
  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. Dialog.ButtonAlignments.Left,
  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. var title = "1234";
  450. // E.g "|[ ok ]|"
  451. var btnText = "ok";
  452. var buttonRow =
  453. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  454. int width = buttonRow.Length;
  455. d.SetBufferSize (width, 1);
  456. (runstate, Dialog dlg) = RunButtonTestDialog (
  457. title,
  458. width,
  459. Dialog.ButtonAlignments.Center,
  460. new Button { Text = btnText }
  461. );
  462. // Center
  463. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  464. End (runstate);
  465. dlg.Dispose ();
  466. // Justify
  467. buttonRow =
  468. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  469. Assert.Equal (width, buttonRow.Length);
  470. (runstate, dlg) = RunButtonTestDialog (
  471. title,
  472. width,
  473. Dialog.ButtonAlignments.Justify,
  474. new Button { Text = btnText }
  475. );
  476. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  477. End (runstate);
  478. dlg.Dispose ();
  479. // Right
  480. buttonRow =
  481. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  482. Assert.Equal (width, buttonRow.Length);
  483. (runstate, dlg) = RunButtonTestDialog (
  484. title,
  485. width,
  486. Dialog.ButtonAlignments.Right,
  487. new Button { Text = btnText }
  488. );
  489. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  490. End (runstate);
  491. dlg.Dispose ();
  492. // Left
  493. buttonRow =
  494. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  495. Assert.Equal (width, buttonRow.Length);
  496. (runstate, dlg) = RunButtonTestDialog (
  497. title,
  498. width,
  499. Dialog.ButtonAlignments.Left,
  500. new Button { Text = btnText }
  501. );
  502. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  503. End (runstate);
  504. dlg.Dispose ();
  505. // Wider
  506. buttonRow =
  507. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  508. width = buttonRow.Length;
  509. d.SetBufferSize (width, 1);
  510. (runstate, dlg) = RunButtonTestDialog (
  511. title,
  512. width,
  513. Dialog.ButtonAlignments.Center,
  514. new Button { Text = btnText }
  515. );
  516. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  517. End (runstate);
  518. dlg.Dispose ();
  519. // Justify
  520. buttonRow =
  521. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  522. Assert.Equal (width, buttonRow.Length);
  523. (runstate, dlg) = RunButtonTestDialog (
  524. title,
  525. width,
  526. Dialog.ButtonAlignments.Justify,
  527. new Button { Text = btnText }
  528. );
  529. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  530. End (runstate);
  531. dlg.Dispose ();
  532. // Right
  533. buttonRow =
  534. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  535. Assert.Equal (width, buttonRow.Length);
  536. (runstate, dlg) = RunButtonTestDialog (
  537. title,
  538. width,
  539. Dialog.ButtonAlignments.Right,
  540. new Button { Text = btnText }
  541. );
  542. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  543. End (runstate);
  544. dlg.Dispose ();
  545. // Left
  546. buttonRow =
  547. $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  548. Assert.Equal (width, buttonRow.Length);
  549. (runstate, dlg) = RunButtonTestDialog (
  550. title,
  551. width,
  552. Dialog.ButtonAlignments.Left,
  553. new Button { Text = btnText }
  554. );
  555. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  556. End (runstate);
  557. dlg.Dispose ();
  558. }
  559. [Fact]
  560. [AutoInitShutdown]
  561. public void ButtonAlignment_Three ()
  562. {
  563. RunState runstate = null;
  564. var d = (FakeDriver)Driver;
  565. var title = "1234";
  566. // E.g "|[ yes ][ no ][ maybe ]|"
  567. var btn1Text = "yes";
  568. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  569. var btn2Text = "no";
  570. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  571. var btn3Text = "maybe";
  572. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  573. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  574. int width = buttonRow.Length;
  575. d.SetBufferSize (buttonRow.Length, 3);
  576. (runstate, Dialog dlg) = RunButtonTestDialog (
  577. title,
  578. width,
  579. Dialog.ButtonAlignments.Center,
  580. new Button { Text = btn1Text },
  581. new Button { Text = btn2Text },
  582. new Button { Text = btn3Text }
  583. );
  584. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  585. End (runstate);
  586. dlg.Dispose ();
  587. // Justify
  588. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  589. Assert.Equal (width, buttonRow.Length);
  590. (runstate, dlg) = RunButtonTestDialog (
  591. title,
  592. width,
  593. Dialog.ButtonAlignments.Justify,
  594. new Button { Text = btn1Text },
  595. new Button { Text = btn2Text },
  596. new Button { Text = btn3Text }
  597. );
  598. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  599. End (runstate);
  600. dlg.Dispose ();
  601. // Right
  602. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  603. Assert.Equal (width, buttonRow.Length);
  604. (runstate, dlg) = RunButtonTestDialog (
  605. title,
  606. width,
  607. Dialog.ButtonAlignments.Right,
  608. new Button { Text = btn1Text },
  609. new Button { Text = btn2Text },
  610. new Button { Text = btn3Text }
  611. );
  612. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  613. End (runstate);
  614. dlg.Dispose ();
  615. // Left
  616. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  617. Assert.Equal (width, buttonRow.Length);
  618. (runstate, dlg) = RunButtonTestDialog (
  619. title,
  620. width,
  621. Dialog.ButtonAlignments.Left,
  622. new Button { Text = btn1Text },
  623. new Button { Text = btn2Text },
  624. new Button { Text = btn3Text }
  625. );
  626. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  627. End (runstate);
  628. dlg.Dispose ();
  629. }
  630. [Fact]
  631. [AutoInitShutdown]
  632. public void ButtonAlignment_Two ()
  633. {
  634. RunState runstate = null;
  635. var d = (FakeDriver)Driver;
  636. var title = "1234";
  637. // E.g "|[ yes ][ no ]|"
  638. var btn1Text = "yes";
  639. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  640. var btn2Text = "no";
  641. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  642. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  643. int width = buttonRow.Length;
  644. d.SetBufferSize (buttonRow.Length, 3);
  645. (runstate, Dialog dlg) = RunButtonTestDialog (
  646. title,
  647. width,
  648. Dialog.ButtonAlignments.Center,
  649. new Button { Text = btn1Text },
  650. new Button { Text = btn2Text }
  651. );
  652. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  653. End (runstate);
  654. dlg.Dispose ();
  655. // Justify
  656. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  657. Assert.Equal (width, buttonRow.Length);
  658. (runstate, dlg) = RunButtonTestDialog (
  659. title,
  660. width,
  661. Dialog.ButtonAlignments.Justify,
  662. new Button { Text = btn1Text },
  663. new Button { Text = btn2Text }
  664. );
  665. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  666. End (runstate);
  667. dlg.Dispose ();
  668. // Right
  669. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  670. Assert.Equal (width, buttonRow.Length);
  671. (runstate, dlg) = RunButtonTestDialog (
  672. title,
  673. width,
  674. Dialog.ButtonAlignments.Right,
  675. new Button { Text = btn1Text },
  676. new Button { Text = btn2Text }
  677. );
  678. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  679. End (runstate);
  680. dlg.Dispose ();
  681. // Left
  682. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  683. Assert.Equal (width, buttonRow.Length);
  684. (runstate, dlg) = RunButtonTestDialog (
  685. title,
  686. width,
  687. Dialog.ButtonAlignments.Left,
  688. new Button { Text = btn1Text },
  689. new Button { Text = btn2Text }
  690. );
  691. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  692. End (runstate);
  693. dlg.Dispose ();
  694. }
  695. [Fact]
  696. [AutoInitShutdown]
  697. public void ButtonAlignment_Two_Hidden ()
  698. {
  699. RunState runstate = null;
  700. var firstIteration = false;
  701. var d = (FakeDriver)Driver;
  702. var title = "1234";
  703. // E.g "|[ yes ][ no ]|"
  704. var btn1Text = "yes";
  705. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  706. var btn2Text = "no";
  707. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  708. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  709. int width = buttonRow.Length;
  710. d.SetBufferSize (buttonRow.Length, 3);
  711. Dialog dlg = null;
  712. Button button1, button2;
  713. // Default (Center)
  714. button1 = new Button { Text = btn1Text };
  715. button2 = new Button { Text = btn2Text };
  716. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
  717. button1.Visible = false;
  718. RunIteration (ref runstate, ref firstIteration);
  719. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  720. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  721. End (runstate);
  722. dlg.Dispose ();
  723. // Justify
  724. Assert.Equal (width, buttonRow.Length);
  725. button1 = new Button { Text = btn1Text };
  726. button2 = new Button { Text = btn2Text };
  727. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
  728. button1.Visible = false;
  729. RunIteration (ref runstate, ref firstIteration);
  730. buttonRow = $@"{CM.Glyphs.VLine} {btn2}{CM.Glyphs.VLine}";
  731. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  732. End (runstate);
  733. dlg.Dispose ();
  734. // Right
  735. Assert.Equal (width, buttonRow.Length);
  736. button1 = new Button { Text = btn1Text };
  737. button2 = new Button { Text = btn2Text };
  738. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
  739. button1.Visible = false;
  740. RunIteration (ref runstate, ref firstIteration);
  741. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  742. End (runstate);
  743. dlg.Dispose ();
  744. // Left
  745. Assert.Equal (width, buttonRow.Length);
  746. button1 = new Button { Text = btn1Text };
  747. button2 = new Button { Text = btn2Text };
  748. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
  749. button1.Visible = false;
  750. RunIteration (ref runstate, ref firstIteration);
  751. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  752. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  753. End (runstate);
  754. dlg.Dispose ();
  755. }
  756. [Fact]
  757. [AutoInitShutdown]
  758. public void Dialog_In_Window_With_Size_One_Button_Aligns ()
  759. {
  760. ((FakeDriver)Driver).SetBufferSize (20, 5);
  761. var win = new Window ();
  762. var iterations = 0;
  763. Iteration += (s, a) =>
  764. {
  765. if (++iterations > 2)
  766. {
  767. RequestStop ();
  768. }
  769. };
  770. var btn = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  771. win.Loaded += (s, a) =>
  772. {
  773. var dlg = new Dialog { Width = 18, Height = 3, Buttons = [new Button { Text = "Ok" }] };
  774. dlg.Loaded += (s, a) =>
  775. {
  776. Refresh ();
  777. var expected = @$"
  778. ┌──────────────────┐
  779. │┌────────────────┐│
  780. ││ {
  781. btn
  782. } ││
  783. │└────────────────┘│
  784. └──────────────────┘";
  785. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  786. };
  787. Run (dlg);
  788. };
  789. Run (win);
  790. }
  791. [Theory]
  792. [AutoInitShutdown]
  793. [InlineData (
  794. 5,
  795. @"
  796. ┌┌───────────────┐─┐
  797. ││ │ │
  798. ││ ⟦ Ok ⟧ │ │
  799. │└───────────────┘ │
  800. └──────────────────┘"
  801. )]
  802. [InlineData (
  803. 6,
  804. @"
  805. ┌┌───────────────┐─┐
  806. ││ │ │
  807. ││ │ │
  808. ││ ⟦ Ok ⟧ │ │
  809. │└───────────────┘ │
  810. └──────────────────┘"
  811. )]
  812. [InlineData (
  813. 7,
  814. @"
  815. ┌──────────────────┐
  816. │┌───────────────┐ │
  817. ││ │ │
  818. ││ │ │
  819. ││ ⟦ Ok ⟧ │ │
  820. │└───────────────┘ │
  821. └──────────────────┘"
  822. )]
  823. [InlineData (
  824. 8,
  825. @"
  826. ┌──────────────────┐
  827. │┌───────────────┐ │
  828. ││ │ │
  829. ││ │ │
  830. ││ │ │
  831. ││ ⟦ Ok ⟧ │ │
  832. │└───────────────┘ │
  833. └──────────────────┘"
  834. )]
  835. [InlineData (
  836. 9,
  837. @"
  838. ┌──────────────────┐
  839. │┌───────────────┐ │
  840. ││ │ │
  841. ││ │ │
  842. ││ │ │
  843. ││ │ │
  844. ││ ⟦ Ok ⟧ │ │
  845. │└───────────────┘ │
  846. └──────────────────┘"
  847. )]
  848. public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height, string expected)
  849. {
  850. ((FakeDriver)Driver).SetBufferSize (20, height);
  851. var win = new Window ();
  852. int iterations = -1;
  853. Iteration += (s, a) =>
  854. {
  855. iterations++;
  856. if (iterations == 0)
  857. {
  858. var dlg = new Dialog { Buttons = [new Button { Text = "Ok" }] };
  859. Run (dlg);
  860. }
  861. else if (iterations == 1)
  862. {
  863. // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)?? No
  864. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  865. }
  866. else
  867. {
  868. RequestStop ();
  869. }
  870. };
  871. Run (win);
  872. }
  873. [Fact]
  874. [AutoInitShutdown]
  875. public void Dialog_Opened_From_Another_Dialog ()
  876. {
  877. ((FakeDriver)Driver).SetBufferSize (30, 10);
  878. var btn1 = new Button { Text = "press me 1" };
  879. Button btn2 = null;
  880. Button btn3 = null;
  881. string expected = null;
  882. btn1.Accept += (s, e) =>
  883. {
  884. btn2 = new Button { Text = "Show Sub" };
  885. btn3 = new Button { Text = "Close" };
  886. btn3.Accept += (s, e) => RequestStop ();
  887. btn2.Accept += (s, e) =>
  888. {
  889. // Don't test MessageBox in Dialog unit tests!
  890. var subBtn = new Button { Text = "Ok", IsDefault = true };
  891. var subDlg = new Dialog { Text = "ya", Width = 20, Height = 5, Buttons = [subBtn] };
  892. subBtn.Accept += (s, e) => RequestStop (subDlg);
  893. Run (subDlg);
  894. };
  895. var dlg = new Dialog { Buttons = [btn2, btn3] };
  896. Run (dlg);
  897. };
  898. var btn =
  899. $"{
  900. CM.Glyphs.LeftBracket
  901. }{
  902. CM.Glyphs.LeftDefaultIndicator
  903. } Ok {
  904. CM.Glyphs.RightDefaultIndicator
  905. }{
  906. CM.Glyphs.RightBracket
  907. }";
  908. int iterations = -1;
  909. Iteration += (s, a) =>
  910. {
  911. iterations++;
  912. if (iterations == 0)
  913. {
  914. Assert.True (btn1.NewKeyDownEvent (Key.Space));
  915. }
  916. else if (iterations == 1)
  917. {
  918. expected = @$"
  919. ┌───────────────────────┐
  920. │ │
  921. │ │
  922. │ │
  923. │ │
  924. │ │
  925. │{
  926. CM.Glyphs.LeftBracket
  927. } Show Sub {
  928. CM.Glyphs.RightBracket
  929. } {
  930. CM.Glyphs.LeftBracket
  931. } Close {
  932. CM.Glyphs.RightBracket
  933. } │
  934. └───────────────────────┘";
  935. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  936. Assert.True (btn2.NewKeyDownEvent (Key.Space));
  937. }
  938. else if (iterations == 2)
  939. {
  940. TestHelpers.AssertDriverContentsWithFrameAre (
  941. @$"
  942. ┌───────────────────────┐
  943. │ ┌──────────────────┐ │
  944. │ │ya │ │
  945. │ │ │ │
  946. │ │ {
  947. btn
  948. } │ │
  949. │ └──────────────────┘ │
  950. │{
  951. CM.Glyphs.LeftBracket
  952. } Show Sub {
  953. CM.Glyphs.RightBracket
  954. } {
  955. CM.Glyphs.LeftBracket
  956. } Close {
  957. CM.Glyphs.RightBracket
  958. } │
  959. └───────────────────────┘",
  960. _output
  961. );
  962. Assert.True (Current.NewKeyDownEvent (Key.Enter));
  963. }
  964. else if (iterations == 3)
  965. {
  966. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  967. Assert.True (btn3.NewKeyDownEvent (Key.Space));
  968. }
  969. else if (iterations == 4)
  970. {
  971. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  972. RequestStop ();
  973. }
  974. };
  975. Run ();
  976. Top.Dispose ();
  977. Shutdown ();
  978. Assert.Equal (4, iterations);
  979. }
  980. [Fact]
  981. [AutoInitShutdown]
  982. public void FileDialog_FileSystemWatcher ()
  983. {
  984. for (var i = 0; i < 8; i++)
  985. {
  986. var fd = new FileDialog ();
  987. fd.Ready += (s, e) => RequestStop ();
  988. Run (fd);
  989. fd.Dispose ();
  990. }
  991. }
  992. [Fact]
  993. [AutoInitShutdown]
  994. public void Location_Default ()
  995. {
  996. var d = new Dialog ();
  997. Begin (d);
  998. ((FakeDriver)Driver).SetBufferSize (100, 100);
  999. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1000. var expected = 7;
  1001. Assert.Equal (new Point (expected, expected), (Point)d.Frame.Location);
  1002. }
  1003. [Fact]
  1004. [AutoInitShutdown]
  1005. public void Location_Not_Default ()
  1006. {
  1007. var d = new Dialog { X = 1, Y = 1 };
  1008. Begin (d);
  1009. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1010. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1011. var expected = 1;
  1012. Assert.Equal (new Point (expected, expected), (Point)d.Frame.Location);
  1013. }
  1014. [Fact]
  1015. [AutoInitShutdown]
  1016. public void Location_When_Application_Top_Not_Default ()
  1017. {
  1018. var expected = 5;
  1019. var d = new Dialog { X = expected, Y = expected, Height = 5, Width = 5 };
  1020. Begin (d);
  1021. ((FakeDriver)Driver).SetBufferSize (20, 10);
  1022. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  1023. Assert.Equal (new Point (expected, expected), (Point)d.Frame.Location);
  1024. TestHelpers.AssertDriverContentsWithFrameAre (
  1025. @"
  1026. ┌───┐
  1027. │ │
  1028. │ │
  1029. │ │
  1030. └───┘",
  1031. _output
  1032. );
  1033. }
  1034. [Fact]
  1035. [AutoInitShutdown]
  1036. public void Location_When_Not_Application_Top_Not_Default ()
  1037. {
  1038. var top = new Toplevel ();
  1039. top.BorderStyle = LineStyle.Double;
  1040. int iterations = -1;
  1041. Iteration += (s, a) =>
  1042. {
  1043. iterations++;
  1044. if (iterations == 0)
  1045. {
  1046. var d = new Dialog { X = 5, Y = 5, Height = 3, Width = 5 };
  1047. Begin (d);
  1048. Assert.Equal (new Point (5, 5), (Point)d.Frame.Location);
  1049. TestHelpers.AssertDriverContentsWithFrameAre (
  1050. @"
  1051. ╔══════════════════╗
  1052. ║ ║
  1053. ║ ║
  1054. ║ ║
  1055. ║ ║
  1056. ║ ┌───┐ ║
  1057. ║ │ │ ║
  1058. ║ └───┘ ║
  1059. ║ ║
  1060. ╚══════════════════╝",
  1061. _output
  1062. );
  1063. d.Dispose ();
  1064. d = new Dialog { X = 5, Y = 5 };
  1065. Begin (d);
  1066. // This is because of PostionTopLevels and EnsureVisibleBounds
  1067. Assert.Equal (new (3, 2), d.Frame.Location);
  1068. // #3127: Before
  1069. // Assert.Equal (new (17, 8), d.Frame.Size);
  1070. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  1071. //╔══════════════════╗
  1072. //║ ║
  1073. //║ ┌───────────────┐
  1074. //║ │ │
  1075. //║ │ │
  1076. //║ │ │
  1077. //║ │ │
  1078. //║ │ │
  1079. //║ │ │
  1080. //╚══└───────────────┘", _output);
  1081. // #3127: After: Because Toplevel is now Width/Height = Dim.Filll
  1082. Assert.Equal (new (15, 6), d.Frame.Size);
  1083. TestHelpers.AssertDriverContentsWithFrameAre (
  1084. @"
  1085. ╔══════════════════╗
  1086. ║ ║
  1087. ║ ┌─────────────┐ ║
  1088. ║ │ │ ║
  1089. ║ │ │ ║
  1090. ║ │ │ ║
  1091. ║ │ │ ║
  1092. ║ └─────────────┘ ║
  1093. ║ ║
  1094. ╚══════════════════╝",
  1095. _output
  1096. );
  1097. d.Dispose ();
  1098. }
  1099. else if (iterations > 0)
  1100. {
  1101. RequestStop ();
  1102. }
  1103. };
  1104. ((FakeDriver)Driver).SetBufferSize (20, 10);
  1105. Run (top);
  1106. }
  1107. [Fact]
  1108. [AutoInitShutdown]
  1109. public void One_Button_Works ()
  1110. {
  1111. RunState runstate = null;
  1112. var d = (FakeDriver)Driver;
  1113. var title = "";
  1114. var btnText = "ok";
  1115. var buttonRow =
  1116. $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  1117. int width = buttonRow.Length;
  1118. d.SetBufferSize (buttonRow.Length, 10);
  1119. (runstate, Dialog _) = RunButtonTestDialog (
  1120. title,
  1121. width,
  1122. Dialog.ButtonAlignments.Center,
  1123. new Button { Text = btnText }
  1124. );
  1125. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1126. End (runstate);
  1127. }
  1128. [Fact]
  1129. [AutoInitShutdown]
  1130. public void Size_Default ()
  1131. {
  1132. var d = new Dialog ();
  1133. Begin (d);
  1134. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1135. // Default size is Percent(85)
  1136. Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
  1137. }
  1138. [Fact]
  1139. [AutoInitShutdown]
  1140. public void Size_Not_Default ()
  1141. {
  1142. var d = new Dialog { Width = 50, Height = 50 };
  1143. Begin (d);
  1144. ((FakeDriver)Driver).SetBufferSize (100, 100);
  1145. // Default size is Percent(85)
  1146. Assert.Equal (new (50, 50), d.Frame.Size);
  1147. }
  1148. [Fact]
  1149. [AutoInitShutdown]
  1150. public void Zero_Buttons_Works ()
  1151. {
  1152. RunState runstate = null;
  1153. var d = (FakeDriver)Driver;
  1154. var title = "1234";
  1155. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.VLine}";
  1156. int width = buttonRow.Length;
  1157. d.SetBufferSize (buttonRow.Length, 3);
  1158. (runstate, Dialog _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  1159. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output);
  1160. End (runstate);
  1161. }
  1162. private (RunState, Dialog) RunButtonTestDialog (
  1163. string title,
  1164. int width,
  1165. Dialog.ButtonAlignments align,
  1166. params Button [] btns
  1167. )
  1168. {
  1169. var dlg = new Dialog
  1170. {
  1171. Title = title,
  1172. X = 0,
  1173. Y = 0,
  1174. Width = width,
  1175. Height = 1,
  1176. ButtonAlignment = align,
  1177. Buttons = btns
  1178. };
  1179. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  1180. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  1181. return (Begin (dlg), dlg);
  1182. }
  1183. [Fact]
  1184. [AutoInitShutdown]
  1185. public void Run_Does_Not_Dispose_Dialog ()
  1186. {
  1187. var top = new Toplevel ();
  1188. Dialog dlg = new ();
  1189. dlg.Ready += Dlg_Ready;
  1190. Run (dlg);
  1191. #if DEBUG_IDISPOSABLE
  1192. Assert.False (dlg.WasDisposed);
  1193. Assert.False (Top.WasDisposed);
  1194. Assert.NotEqual (top, Top);
  1195. Assert.Equal (dlg, Top);
  1196. #endif
  1197. // dlg wasn't disposed yet and it's possible to access to his properties
  1198. Assert.False (dlg.Canceled);
  1199. Assert.NotNull (dlg);
  1200. dlg.Canceled = true;
  1201. Assert.True (dlg.Canceled);
  1202. dlg.Dispose ();
  1203. top.Dispose ();
  1204. #if DEBUG_IDISPOSABLE
  1205. Assert.True (dlg.WasDisposed);
  1206. Assert.True (Top.WasDisposed);
  1207. Assert.NotNull (Top);
  1208. #endif
  1209. Shutdown();
  1210. Assert.Null (Top);
  1211. return;
  1212. void Dlg_Ready (object sender, EventArgs e)
  1213. {
  1214. RequestStop ();
  1215. }
  1216. }
  1217. [Fact]
  1218. [AutoInitShutdown]
  1219. public void Can_Access_Cancel_Property_After_Run ()
  1220. {
  1221. Dialog dlg = new ();
  1222. dlg.Ready += Dlg_Ready;
  1223. Run (dlg);
  1224. #if DEBUG_IDISPOSABLE
  1225. Assert.False (dlg.WasDisposed);
  1226. Assert.False (Top.WasDisposed);
  1227. Assert.Equal (dlg, Top);
  1228. #endif
  1229. Assert.True (dlg.Canceled);
  1230. // Run it again is possible because it isn't disposed yet
  1231. Run (dlg);
  1232. // Run another view without dispose the prior will throw an assertion
  1233. #if DEBUG_IDISPOSABLE
  1234. Dialog dlg2 = new ();
  1235. dlg2.Ready += Dlg_Ready;
  1236. var exception = Record.Exception (() => Run (dlg2));
  1237. Assert.NotNull (exception);
  1238. dlg.Dispose();
  1239. // Now it's possible to tun dlg2 without throw
  1240. Run (dlg2);
  1241. Assert.True (dlg.WasDisposed);
  1242. Assert.False (Top.WasDisposed);
  1243. Assert.Equal (dlg2, Top);
  1244. Assert.False (dlg2.WasDisposed);
  1245. dlg2.Dispose ();
  1246. // Now an assertion will throw accessing the Canceled property
  1247. exception = Record.Exception (() => Assert.True (dlg.Canceled));
  1248. Assert.NotNull (exception);
  1249. Assert.True (Top.WasDisposed);
  1250. Shutdown ();
  1251. Assert.True (dlg2.WasDisposed);
  1252. Assert.Null (Top);
  1253. #endif
  1254. return;
  1255. void Dlg_Ready (object sender, EventArgs e)
  1256. {
  1257. ((Dialog)sender).Canceled = true;
  1258. RequestStop ();
  1259. }
  1260. }
  1261. }