DialogTests.cs 57 KB

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