DialogTests.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  9. using Xunit.Abstractions;
  10. using NStack;
  11. using static Terminal.Gui.Application;
  12. namespace Terminal.Gui.DialogTests {
  13. public class DialogTests {
  14. readonly ITestOutputHelper output;
  15. public DialogTests (ITestOutputHelper output)
  16. {
  17. this.output = output;
  18. }
  19. //[Fact]
  20. //[AutoInitShutdown]
  21. //public void Default_Has_Border ()
  22. //{
  23. // var d = (FakeDriver)Application.Driver;
  24. // d.SetBufferSize (20, 5);
  25. // Application.RunState runstate = null;
  26. // var title = "Title";
  27. // var btnText = "ok";
  28. // var buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  29. // var width = buttonRow.Length;
  30. // var topRow = $"┌┤{title} {new string (d.HLine.ToString () [0], width - title.Length - 2)}├┐";
  31. // var bottomRow = $"└{new string (d.HLine.ToString () [0], width - 2)}┘";
  32. // var dlg = new Dialog (title, new Button (btnText));
  33. // Application.Begin (dlg);
  34. // TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
  35. // Application.End (runstate);
  36. //}
  37. private (Application.RunState, Dialog) RunButtonTestDialog (string title, int width, Dialog.ButtonAlignments align, params Button [] btns)
  38. {
  39. var dlg = new Dialog (btns) {
  40. Title = title,
  41. X = 0,
  42. Y = 0,
  43. Width = width,
  44. Height = 1,
  45. ButtonAlignment = align,
  46. };
  47. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  48. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  49. return (Application.Begin (dlg), dlg);
  50. }
  51. [Fact]
  52. [AutoInitShutdown]
  53. public void Size_Default ()
  54. {
  55. var d = new Dialog () {
  56. };
  57. Application.Begin (d);
  58. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  59. // Default size is Percent(85)
  60. Assert.Equal (new Size ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
  61. }
  62. [Fact]
  63. [AutoInitShutdown]
  64. public void Location_Default ()
  65. {
  66. var d = new Dialog () {
  67. };
  68. Application.Begin (d);
  69. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  70. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  71. var expected = 7;
  72. Assert.Equal (new Point (expected, expected), d.Frame.Location);
  73. }
  74. [Fact]
  75. [AutoInitShutdown]
  76. public void Size_Not_Default ()
  77. {
  78. var d = new Dialog () {
  79. Width = 50,
  80. Height = 50,
  81. };
  82. Application.Begin (d);
  83. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  84. // Default size is Percent(85)
  85. Assert.Equal (new Size (50, 50), d.Frame.Size);
  86. }
  87. [Fact]
  88. [AutoInitShutdown]
  89. public void Location_Not_Default ()
  90. {
  91. var d = new Dialog () {
  92. X = 1,
  93. Y = 1,
  94. };
  95. Application.Begin (d);
  96. ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
  97. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  98. var expected = 1;
  99. Assert.Equal (new Point (expected, expected), d.Frame.Location);
  100. }
  101. [Fact]
  102. [AutoInitShutdown]
  103. public void Location_When_Application_Top_Not_Default ()
  104. {
  105. var expected = 5;
  106. var d = new Dialog () {
  107. X = expected,
  108. Y = expected,
  109. Height = 5,
  110. Width = 5
  111. };
  112. Application.Begin (d);
  113. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  114. // Default location is centered, so 100 / 2 - 85 / 2 = 7
  115. Assert.Equal (new Point (expected, expected), d.Frame.Location);
  116. TestHelpers.AssertDriverContentsWithFrameAre (@"
  117. ┌───┐
  118. │ │
  119. │ │
  120. │ │
  121. └───┘", output);
  122. }
  123. [Fact]
  124. [AutoInitShutdown]
  125. public void Location_When_Not_Application_Top_Not_Default ()
  126. {
  127. Application.Top.BorderStyle = LineStyle.Double;
  128. var iterations = -1;
  129. Application.Iteration += () => {
  130. iterations++;
  131. if (iterations == 0) {
  132. var d = new Dialog () {
  133. X = 5,
  134. Y = 5,
  135. Height = 3,
  136. Width = 5
  137. };
  138. Application.Begin (d);
  139. Assert.Equal (new Point (5, 5), d.Frame.Location);
  140. TestHelpers.AssertDriverContentsWithFrameAre (@"
  141. ╔══════════════════╗
  142. ║ ║
  143. ║ ║
  144. ║ ║
  145. ║ ║
  146. ║ ┌───┐ ║
  147. ║ │ │ ║
  148. ║ └───┘ ║
  149. ║ ║
  150. ╚══════════════════╝", output);
  151. d = new Dialog () {
  152. X = 5,
  153. Y = 5,
  154. };
  155. Application.Begin (d);
  156. // This is because of PostionTopLevels and EnsureVisibleBounds
  157. Assert.Equal (new Point (3, 2), d.Frame.Location);
  158. TestHelpers.AssertDriverContentsWithFrameAre (@"
  159. ╔══════════════════╗
  160. ║ ║
  161. ║ ┌───────────────┐
  162. ║ │ │
  163. ║ │ │
  164. ║ │ │
  165. ║ │ │
  166. ║ │ │
  167. ║ │ │
  168. ╚══└───────────────┘", output);
  169. } else if (iterations > 0) {
  170. Application.RequestStop ();
  171. }
  172. };
  173. Application.Begin (Application.Top);
  174. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  175. Application.Run ();
  176. }
  177. [Fact]
  178. [AutoInitShutdown]
  179. public void ButtonAlignment_One ()
  180. {
  181. var d = (FakeDriver)Application.Driver;
  182. Application.RunState runstate = null;
  183. var title = "1234";
  184. // E.g "|[ ok ]|"
  185. var btnText = "ok";
  186. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  187. var width = buttonRow.Length;
  188. d.SetBufferSize (width, 1);
  189. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  190. // Center
  191. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  192. Application.End (runstate);
  193. // Justify
  194. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  195. Assert.Equal (width, buttonRow.Length);
  196. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  197. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  198. Application.End (runstate);
  199. // Right
  200. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  201. Assert.Equal (width, buttonRow.Length);
  202. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  203. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  204. Application.End (runstate);
  205. // Left
  206. buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  207. Assert.Equal (width, buttonRow.Length);
  208. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  209. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  210. Application.End (runstate);
  211. // Wider
  212. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  213. width = buttonRow.Length;
  214. d.SetBufferSize (width, 1);
  215. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  216. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  217. Application.End (runstate);
  218. // Justify
  219. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  220. Assert.Equal (width, buttonRow.Length);
  221. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  222. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  223. Application.End (runstate);
  224. // Right
  225. buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
  226. Assert.Equal (width, buttonRow.Length);
  227. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  228. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  229. Application.End (runstate);
  230. // Left
  231. buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  232. Assert.Equal (width, buttonRow.Length);
  233. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  234. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  235. Application.End (runstate);
  236. }
  237. [Fact]
  238. [AutoInitShutdown]
  239. public void ButtonAlignment_Two ()
  240. {
  241. Application.RunState runstate = null;
  242. var d = (FakeDriver)Application.Driver;
  243. var title = "1234";
  244. // E.g "|[ yes ][ no ]|"
  245. var btn1Text = "yes";
  246. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  247. var btn2Text = "no";
  248. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  249. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  250. var width = buttonRow.Length;
  251. d.SetBufferSize (buttonRow.Length, 3);
  252. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text));
  253. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  254. Application.End (runstate);
  255. // Justify
  256. buttonRow = $@"{d.VLine}{btn1} {btn2}{d.VLine}";
  257. Assert.Equal (width, buttonRow.Length);
  258. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text));
  259. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  260. Application.End (runstate);
  261. // Right
  262. buttonRow = $@"{d.VLine} {btn1} {btn2}{d.VLine}";
  263. Assert.Equal (width, buttonRow.Length);
  264. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
  265. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  266. Application.End (runstate);
  267. // Left
  268. buttonRow = $@"{d.VLine}{btn1} {btn2} {d.VLine}";
  269. Assert.Equal (width, buttonRow.Length);
  270. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
  271. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  272. Application.End (runstate);
  273. }
  274. [Fact]
  275. [AutoInitShutdown]
  276. public void ButtonAlignment_Two_Hidden ()
  277. {
  278. Application.RunState runstate = null;
  279. bool firstIteration = false;
  280. var d = (FakeDriver)Application.Driver;
  281. var title = "1234";
  282. // E.g "|[ yes ][ no ]|"
  283. var btn1Text = "yes";
  284. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  285. var btn2Text = "no";
  286. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  287. var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
  288. var width = buttonRow.Length;
  289. d.SetBufferSize (buttonRow.Length, 3);
  290. Dialog dlg = null;
  291. Button button1, button2;
  292. // Default (Center)
  293. button1 = new Button (btn1Text);
  294. button2 = new Button (btn2Text);
  295. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
  296. button1.Visible = false;
  297. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  298. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  299. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  300. Application.End (runstate);
  301. // Justify
  302. Assert.Equal (width, buttonRow.Length);
  303. button1 = new Button (btn1Text);
  304. button2 = new Button (btn2Text);
  305. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
  306. button1.Visible = false;
  307. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  308. buttonRow = $@"{d.VLine} {btn2}{d.VLine}";
  309. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  310. Application.End (runstate);
  311. // Right
  312. Assert.Equal (width, buttonRow.Length);
  313. button1 = new Button (btn1Text);
  314. button2 = new Button (btn2Text);
  315. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
  316. button1.Visible = false;
  317. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  318. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  319. Application.End (runstate);
  320. // Left
  321. Assert.Equal (width, buttonRow.Length);
  322. button1 = new Button (btn1Text);
  323. button2 = new Button (btn2Text);
  324. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
  325. button1.Visible = false;
  326. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  327. buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
  328. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  329. Application.End (runstate);
  330. }
  331. [Fact]
  332. [AutoInitShutdown]
  333. public void ButtonAlignment_Three ()
  334. {
  335. Application.RunState runstate = null;
  336. var d = (FakeDriver)Application.Driver;
  337. var title = "1234";
  338. // E.g "|[ yes ][ no ][ maybe ]|"
  339. var btn1Text = "yes";
  340. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  341. var btn2Text = "no";
  342. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  343. var btn3Text = "maybe";
  344. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  345. var buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3} {d.VLine}";
  346. var width = buttonRow.Length;
  347. d.SetBufferSize (buttonRow.Length, 3);
  348. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  349. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  350. Application.End (runstate);
  351. // Justify
  352. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3}{d.VLine}";
  353. Assert.Equal (width, buttonRow.Length);
  354. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  355. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  356. Application.End (runstate);
  357. // Right
  358. buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3}{d.VLine}";
  359. Assert.Equal (width, buttonRow.Length);
  360. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  361. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  362. Application.End (runstate);
  363. // Left
  364. buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3} {d.VLine}";
  365. Assert.Equal (width, buttonRow.Length);
  366. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  367. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  368. Application.End (runstate);
  369. }
  370. [Fact]
  371. [AutoInitShutdown]
  372. public void ButtonAlignment_Four ()
  373. {
  374. Application.RunState runstate = null;
  375. var d = (FakeDriver)Application.Driver;
  376. var title = "1234";
  377. // E.g "|[ yes ][ no ][ maybe ]|"
  378. var btn1Text = "yes";
  379. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  380. var btn2Text = "no";
  381. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  382. var btn3Text = "maybe";
  383. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  384. var btn4Text = "never";
  385. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  386. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  387. var width = buttonRow.Length;
  388. d.SetBufferSize (buttonRow.Length, 3);
  389. // Default - Center
  390. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  391. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  392. Application.End (runstate);
  393. // Justify
  394. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  395. Assert.Equal (width, buttonRow.Length);
  396. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  397. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  398. Application.End (runstate);
  399. // Right
  400. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  401. Assert.Equal (width, buttonRow.Length);
  402. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  403. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  404. Application.End (runstate);
  405. // Left
  406. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  407. Assert.Equal (width, buttonRow.Length);
  408. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  409. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  410. Application.End (runstate);
  411. }
  412. [Fact]
  413. [AutoInitShutdown]
  414. public void ButtonAlignment_Four_On_Too_Small_Width ()
  415. {
  416. Application.RunState runstate = null;
  417. var d = (FakeDriver)Application.Driver;
  418. var title = "1234";
  419. // E.g "|[ yes ][ no ][ maybe ][ never ]|"
  420. var btn1Text = "yes";
  421. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  422. var btn2Text = "no";
  423. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  424. var btn3Text = "maybe";
  425. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  426. var btn4Text = "never";
  427. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  428. var buttonRow = string.Empty;
  429. var width = 30;
  430. d.SetBufferSize (width, 1);
  431. // Default - Center
  432. buttonRow = $"{d.VLine}es ] {btn2} {btn3} [ neve{d.VLine}";
  433. (runstate, var dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  434. Assert.Equal (new Size (width, 1), dlg.Frame.Size);
  435. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  436. Application.End (runstate);
  437. // Justify
  438. buttonRow = $"{d.VLine}[ yes [ no [ maybe [ never ]{d.VLine}";
  439. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  440. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  441. // Right
  442. buttonRow = $"{d.VLine}] {btn2} {btn3} {btn4}{d.VLine}";
  443. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  444. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  445. // Left
  446. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} [ n{d.VLine}";
  447. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  448. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  449. }
  450. [Fact]
  451. [AutoInitShutdown]
  452. public void ButtonAlignment_Four_Wider ()
  453. {
  454. Application.RunState runstate = null;
  455. var d = (FakeDriver)Application.Driver;
  456. var title = "1234";
  457. // E.g "|[ yes ][ no ][ maybe ]|"
  458. var btn1Text = "yes";
  459. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  460. var btn2Text = "no";
  461. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  462. var btn3Text = "你你你你你"; // This is a wide char
  463. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  464. // Requires a Nerd Font
  465. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  466. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  467. // Note extra spaces to make dialog even wider
  468. // 123456 123456
  469. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  470. var width = ustring.Make (buttonRow).ConsoleWidth;
  471. d.SetBufferSize (width, 3);
  472. // Default - Center
  473. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  474. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  475. Application.End (runstate);
  476. // Justify
  477. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  478. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  479. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  480. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  481. Application.End (runstate);
  482. // Right
  483. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  484. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  485. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  486. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  487. Application.End (runstate);
  488. // Left
  489. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  490. Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
  491. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  492. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  493. Application.End (runstate);
  494. }
  495. [Fact]
  496. [AutoInitShutdown]
  497. public void ButtonAlignment_Four_WideOdd ()
  498. {
  499. Application.RunState runstate = null;
  500. var d = (FakeDriver)Application.Driver;
  501. var title = "1234";
  502. // E.g "|[ yes ][ no ][ maybe ]|"
  503. var btn1Text = "really long button 1";
  504. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  505. var btn2Text = "really long button 2";
  506. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  507. var btn3Text = "really long button 3";
  508. var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
  509. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  510. var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
  511. // Note extra spaces to make dialog even wider
  512. // 123456 1234567
  513. var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
  514. var width = buttonRow.Length;
  515. d.SetBufferSize (buttonRow.Length, 1);
  516. // Default - Center
  517. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  518. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  519. Application.End (runstate);
  520. // Justify
  521. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
  522. Assert.Equal (width, buttonRow.Length);
  523. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  524. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  525. Application.End (runstate);
  526. // Right
  527. buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
  528. Assert.Equal (width, buttonRow.Length);
  529. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  530. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  531. Application.End (runstate);
  532. // Left
  533. buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
  534. Assert.Equal (width, buttonRow.Length);
  535. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  536. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  537. Application.End (runstate);
  538. }
  539. [Fact]
  540. [AutoInitShutdown]
  541. public void Zero_Buttons_Works ()
  542. {
  543. Application.RunState runstate = null;
  544. var d = (FakeDriver)Application.Driver;
  545. var title = "1234";
  546. var buttonRow = $"{d.VLine} {d.VLine}";
  547. var width = buttonRow.Length;
  548. d.SetBufferSize (buttonRow.Length, 3);
  549. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  550. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  551. Application.End (runstate);
  552. }
  553. [Fact]
  554. [AutoInitShutdown]
  555. public void One_Button_Works ()
  556. {
  557. Application.RunState runstate = null;
  558. var d = (FakeDriver)Application.Driver;
  559. var title = "";
  560. var btnText = "ok";
  561. var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
  562. var width = buttonRow.Length;
  563. d.SetBufferSize (buttonRow.Length, 10);
  564. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  565. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  566. Application.End (runstate);
  567. }
  568. [Fact]
  569. [AutoInitShutdown]
  570. public void Add_Button_Works ()
  571. {
  572. Application.RunState runstate = null;
  573. var d = (FakeDriver)Application.Driver;
  574. var title = "1234";
  575. var btn1Text = "yes";
  576. var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
  577. var btn2Text = "no";
  578. var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
  579. // We test with one button first, but do this to get the width right for 2
  580. var width = $@"{d.VLine} {btn1} {btn2} {d.VLine}".Length;
  581. d.SetBufferSize (width, 1);
  582. // Default (center)
  583. var dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Center };
  584. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  585. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  586. runstate = Application.Begin (dlg);
  587. var buttonRow = $"{d.VLine} {btn1} {d.VLine}";
  588. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  589. // Now add a second button
  590. buttonRow = $"{d.VLine} {btn1} {btn2} {d.VLine}";
  591. dlg.AddButton (new Button (btn2Text));
  592. bool first = false;
  593. Application.RunMainLoopIteration (ref runstate, true, ref first);
  594. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  595. Application.End (runstate);
  596. // Justify
  597. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Justify };
  598. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  599. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  600. runstate = Application.Begin (dlg);
  601. buttonRow = $"{d.VLine} {btn1}{d.VLine}";
  602. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  603. // Now add a second button
  604. buttonRow = $"{d.VLine}{btn1} {btn2}{d.VLine}";
  605. dlg.AddButton (new Button (btn2Text));
  606. first = false;
  607. Application.RunMainLoopIteration (ref runstate, true, ref first);
  608. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  609. Application.End (runstate);
  610. // Right
  611. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Right };
  612. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  613. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  614. runstate = Application.Begin (dlg);
  615. buttonRow = $"{d.VLine}{new string (' ', width - btn1.Length - 2)}{btn1}{d.VLine}";
  616. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  617. // Now add a second button
  618. buttonRow = $"{d.VLine} {btn1} {btn2}{d.VLine}";
  619. dlg.AddButton (new Button (btn2Text));
  620. first = false;
  621. Application.RunMainLoopIteration (ref runstate, true, ref first);
  622. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  623. Application.End (runstate);
  624. // Left
  625. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Left };
  626. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  627. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  628. runstate = Application.Begin (dlg);
  629. buttonRow = $"{d.VLine}{btn1}{new string (' ', width - btn1.Length - 2)}{d.VLine}";
  630. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  631. // Now add a second button
  632. buttonRow = $"{d.VLine}{btn1} {btn2} {d.VLine}";
  633. dlg.AddButton (new Button (btn2Text));
  634. first = false;
  635. Application.RunMainLoopIteration (ref runstate, true, ref first);
  636. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  637. Application.End (runstate);
  638. }
  639. [Fact]
  640. [AutoInitShutdown]
  641. public void FileDialog_FileSystemWatcher ()
  642. {
  643. for (int i = 0; i < 8; i++) {
  644. var fd = new FileDialog ();
  645. fd.Ready += (s, e) => Application.RequestStop ();
  646. Application.Run (fd);
  647. }
  648. }
  649. [Fact, AutoInitShutdown]
  650. public void Dialog_Opened_From_Another_Dialog ()
  651. {
  652. var btn1 = new Button ("press me 1");
  653. Button btn2 = null;
  654. Button btn3 = null;
  655. string expected = null;
  656. btn1.Clicked += (s, e) => {
  657. btn2 = new Button ("Show Sub");
  658. btn3 = new Button ("Close");
  659. btn3.Clicked += (s, e) => Application.RequestStop ();
  660. btn2.Clicked += (s, e) => { MessageBox.Query (string.Empty, "ya", "ok"); };
  661. var dlg = new Dialog (btn2, btn3);
  662. Application.Run (dlg);
  663. };
  664. var iterations = -1;
  665. Application.Iteration += () => {
  666. iterations++;
  667. if (iterations == 0) {
  668. Assert.True (btn1.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  669. } else if (iterations == 1) {
  670. expected = @"
  671. ┌──────────────────────────────────────────────────────────────────┐
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. │ │
  684. │ │
  685. │ │
  686. │ │
  687. │ │
  688. │ │
  689. │ │
  690. │ [ Show Sub ] [ Close ] │
  691. └──────────────────────────────────────────────────────────────────┘";
  692. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  693. Assert.True (btn2.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  694. } else if (iterations == 2) {
  695. TestHelpers.AssertDriverContentsWithFrameAre (@"
  696. ┌──────────────────────────────────────────────────────────────────┐
  697. │ │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. │ ┌──────────────────────────────────────────────┐ │
  705. │ │ ya │ │
  706. │ │ │ │
  707. │ │ [◦ ok ◦] │ │
  708. │ └──────────────────────────────────────────────┘ │
  709. │ │
  710. │ │
  711. │ │
  712. │ │
  713. │ │
  714. │ │
  715. │ [ Show Sub ] [ Close ] │
  716. └──────────────────────────────────────────────────────────────────┘", output);
  717. Assert.True (Application.Current.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  718. } else if (iterations == 3) {
  719. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  720. Assert.True (btn3.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  721. } else if (iterations == 4) {
  722. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  723. Application.RequestStop ();
  724. }
  725. };
  726. Application.Run ();
  727. Application.Shutdown ();
  728. Assert.Equal (4, iterations);
  729. }
  730. [Fact, AutoInitShutdown]
  731. public void Dialog_In_Window_With_Size_One_Button_Aligns ()
  732. {
  733. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  734. var win = new Window ();
  735. int iterations = 0;
  736. Application.Iteration += () => {
  737. if (++iterations > 2) {
  738. Application.RequestStop ();
  739. }
  740. };
  741. win.Loaded += (s, a) => {
  742. var dlg = new Dialog (new Button ("Ok")) { Width = 18, Height = 3 };
  743. dlg.Loaded += (s, a) => {
  744. Application.Refresh ();
  745. var expected = @"
  746. ┌──────────────────┐
  747. │┌────────────────┐│
  748. ││ [ Ok ] ││
  749. │└────────────────┘│
  750. └──────────────────┘";
  751. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  752. };
  753. Application.Run (dlg);
  754. };
  755. Application.Run (win);
  756. }
  757. // [Theory, AutoInitShutdown]
  758. // [InlineData (5)]
  759. // //[InlineData (6)]
  760. // //[InlineData (7)]
  761. // //[InlineData (8)]
  762. // //[InlineData (9)]
  763. // public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height)
  764. // {
  765. // ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  766. // var win = new Window ();
  767. // Application.Iteration += () => {
  768. // var dlg = new Dialog ("Test", new Button ("Ok"));
  769. // dlg.LayoutComplete += (s, a) => {
  770. // Application.Refresh ();
  771. // // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)??
  772. // var expected = @"
  773. //┌┌┤Test├─────────┐─┐
  774. //││ │ │
  775. //││ [ Ok ] │ │
  776. //│└───────────────┘ │
  777. //└──────────────────┘";
  778. // _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  779. // dlg.RequestStop ();
  780. // win.RequestStop ();
  781. // };
  782. // Application.Run (dlg);
  783. // };
  784. // Application.Run (win);
  785. // Application.Shutdown ();
  786. // }
  787. [Fact, AutoInitShutdown]
  788. public void Dialog_In_Window_With_TexxtField_And_Button_AnchorEnd ()
  789. {
  790. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  791. var win = new Window ();
  792. int iterations = 0;
  793. Application.Iteration += () => {
  794. if (++iterations > 2) {
  795. Application.RequestStop ();
  796. }
  797. };
  798. win.Loaded += (s, a) => {
  799. var dlg = new Dialog () { Width = 18, Height = 3 };
  800. Button btn = null;
  801. btn = new Button ("Ok") {
  802. X = Pos.AnchorEnd () - Pos.Function (Btn_Width)
  803. };
  804. int Btn_Width ()
  805. {
  806. return (btn?.Bounds.Width) ?? 0;
  807. }
  808. var tf = new TextField ("01234567890123456789") {
  809. Width = Dim.Fill (1) - Dim.Function (Btn_Width)
  810. };
  811. dlg.Loaded += (s, a) => {
  812. Application.Refresh ();
  813. Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
  814. Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
  815. var expected = @"
  816. ┌──────────────────┐
  817. │┌────────────────┐│
  818. ││23456789 [ Ok ]││
  819. │└────────────────┘│
  820. └──────────────────┘";
  821. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  822. dlg.SetNeedsLayout ();
  823. dlg.LayoutSubviews ();
  824. Application.Refresh ();
  825. Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
  826. Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
  827. expected = @"
  828. ┌──────────────────┐
  829. │┌────────────────┐│
  830. ││23456789 [ Ok ]││
  831. │└────────────────┘│
  832. └──────────────────┘";
  833. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  834. };
  835. dlg.Add (btn, tf);
  836. Application.Run (dlg);
  837. };
  838. Application.Run (win);
  839. }
  840. }
  841. }