DialogTests.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 System.Text;
  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 = $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.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 (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 += (s, a) => {
  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. // #3127: Before
  159. // Assert.Equal (new Size (17, 8), d.Frame.Size);
  160. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  161. //╔══════════════════╗
  162. //║ ║
  163. //║ ┌───────────────┐
  164. //║ │ │
  165. //║ │ │
  166. //║ │ │
  167. //║ │ │
  168. //║ │ │
  169. //║ │ │
  170. //╚══└───────────────┘", output);
  171. // #3127: After: Because Toplevel is now Width/Height = Dim.Filll
  172. Assert.Equal (new Size (15, 6), d.Frame.Size);
  173. TestHelpers.AssertDriverContentsWithFrameAre (@"
  174. ╔══════════════════╗
  175. ║ ║
  176. ║ ┌─────────────┐ ║
  177. ║ │ │ ║
  178. ║ │ │ ║
  179. ║ │ │ ║
  180. ║ │ │ ║
  181. ║ └─────────────┘ ║
  182. ║ ║
  183. ╚══════════════════╝", output);
  184. } else if (iterations > 0) {
  185. Application.RequestStop ();
  186. }
  187. };
  188. Application.Begin (Application.Top);
  189. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  190. Application.Run ();
  191. }
  192. [Fact]
  193. [AutoInitShutdown]
  194. public void ButtonAlignment_One ()
  195. {
  196. var d = (FakeDriver)Application.Driver;
  197. RunState runstate = null;
  198. var title = "1234";
  199. // E.g "|[ ok ]|"
  200. var btnText = "ok";
  201. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  202. var width = buttonRow.Length;
  203. d.SetBufferSize (width, 1);
  204. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  205. // Center
  206. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  207. Application.End (runstate);
  208. // Justify
  209. buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  210. Assert.Equal (width, buttonRow.Length);
  211. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  212. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  213. Application.End (runstate);
  214. // Right
  215. buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  216. Assert.Equal (width, buttonRow.Length);
  217. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  218. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  219. Application.End (runstate);
  220. // Left
  221. buttonRow = $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  222. Assert.Equal (width, buttonRow.Length);
  223. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  224. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  225. Application.End (runstate);
  226. // Wider
  227. buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  228. width = buttonRow.Length;
  229. d.SetBufferSize (width, 1);
  230. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  231. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  232. Application.End (runstate);
  233. // Justify
  234. buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  235. Assert.Equal (width, buttonRow.Length);
  236. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
  237. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  238. Application.End (runstate);
  239. // Right
  240. buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  241. Assert.Equal (width, buttonRow.Length);
  242. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
  243. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  244. Application.End (runstate);
  245. // Left
  246. buttonRow = $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  247. Assert.Equal (width, buttonRow.Length);
  248. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
  249. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  250. Application.End (runstate);
  251. }
  252. [Fact]
  253. [AutoInitShutdown]
  254. public void ButtonAlignment_Two ()
  255. {
  256. RunState runstate = null;
  257. var d = (FakeDriver)Application.Driver;
  258. var title = "1234";
  259. // E.g "|[ yes ][ no ]|"
  260. var btn1Text = "yes";
  261. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  262. var btn2Text = "no";
  263. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  264. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  265. var width = buttonRow.Length;
  266. d.SetBufferSize (buttonRow.Length, 3);
  267. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text));
  268. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  269. Application.End (runstate);
  270. // Justify
  271. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  272. Assert.Equal (width, buttonRow.Length);
  273. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text));
  274. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  275. Application.End (runstate);
  276. // Right
  277. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  278. Assert.Equal (width, buttonRow.Length);
  279. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
  280. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  281. Application.End (runstate);
  282. // Left
  283. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  284. Assert.Equal (width, buttonRow.Length);
  285. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
  286. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  287. Application.End (runstate);
  288. }
  289. [Fact]
  290. [AutoInitShutdown]
  291. public void ButtonAlignment_Two_Hidden ()
  292. {
  293. RunState runstate = null;
  294. bool firstIteration = false;
  295. var d = (FakeDriver)Application.Driver;
  296. var title = "1234";
  297. // E.g "|[ yes ][ no ]|"
  298. var btn1Text = "yes";
  299. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  300. var btn2Text = "no";
  301. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  302. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  303. var width = buttonRow.Length;
  304. d.SetBufferSize (buttonRow.Length, 3);
  305. Dialog dlg = null;
  306. Button button1, button2;
  307. // Default (Center)
  308. button1 = new Button (btn1Text);
  309. button2 = new Button (btn2Text);
  310. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
  311. button1.Visible = false;
  312. Application.RunIteration (ref runstate, ref firstIteration);
  313. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  314. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  315. Application.End (runstate);
  316. // Justify
  317. Assert.Equal (width, buttonRow.Length);
  318. button1 = new Button (btn1Text);
  319. button2 = new Button (btn2Text);
  320. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
  321. button1.Visible = false;
  322. Application.RunIteration (ref runstate, ref firstIteration);
  323. buttonRow = $@"{CM.Glyphs.VLine} {btn2}{CM.Glyphs.VLine}";
  324. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  325. Application.End (runstate);
  326. // Right
  327. Assert.Equal (width, buttonRow.Length);
  328. button1 = new Button (btn1Text);
  329. button2 = new Button (btn2Text);
  330. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
  331. button1.Visible = false;
  332. Application.RunIteration (ref runstate, ref firstIteration);
  333. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  334. Application.End (runstate);
  335. // Left
  336. Assert.Equal (width, buttonRow.Length);
  337. button1 = new Button (btn1Text);
  338. button2 = new Button (btn2Text);
  339. (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
  340. button1.Visible = false;
  341. Application.RunIteration (ref runstate, ref firstIteration);
  342. buttonRow = $@"{CM.Glyphs.VLine} {btn2} {CM.Glyphs.VLine}";
  343. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  344. Application.End (runstate);
  345. }
  346. [Fact]
  347. [AutoInitShutdown]
  348. public void ButtonAlignment_Three ()
  349. {
  350. RunState runstate = null;
  351. var d = (FakeDriver)Application.Driver;
  352. var title = "1234";
  353. // E.g "|[ yes ][ no ][ maybe ]|"
  354. var btn1Text = "yes";
  355. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  356. var btn2Text = "no";
  357. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  358. var btn3Text = "maybe";
  359. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  360. var buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  361. var width = buttonRow.Length;
  362. d.SetBufferSize (buttonRow.Length, 3);
  363. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  364. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  365. Application.End (runstate);
  366. // Justify
  367. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  368. Assert.Equal (width, buttonRow.Length);
  369. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  370. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  371. Application.End (runstate);
  372. // Right
  373. buttonRow = $@"{CM.Glyphs.VLine} {btn1} {btn2} {btn3}{CM.Glyphs.VLine}";
  374. Assert.Equal (width, buttonRow.Length);
  375. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  376. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  377. Application.End (runstate);
  378. // Left
  379. buttonRow = $@"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {CM.Glyphs.VLine}";
  380. Assert.Equal (width, buttonRow.Length);
  381. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
  382. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  383. Application.End (runstate);
  384. }
  385. [Fact]
  386. [AutoInitShutdown]
  387. public void ButtonAlignment_Four ()
  388. {
  389. RunState runstate = null;
  390. var d = (FakeDriver)Application.Driver;
  391. var title = "1234";
  392. // E.g "|[ yes ][ no ][ maybe ]|"
  393. var btn1Text = "yes";
  394. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  395. var btn2Text = "no";
  396. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  397. var btn3Text = "maybe";
  398. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  399. var btn4Text = "never";
  400. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  401. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  402. var width = buttonRow.Length;
  403. d.SetBufferSize (buttonRow.Length, 3);
  404. // Default - Center
  405. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  406. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  407. Application.End (runstate);
  408. // Justify
  409. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  410. Assert.Equal (width, buttonRow.Length);
  411. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  412. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  413. Application.End (runstate);
  414. // Right
  415. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  416. Assert.Equal (width, buttonRow.Length);
  417. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  418. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  419. Application.End (runstate);
  420. // Left
  421. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  422. Assert.Equal (width, buttonRow.Length);
  423. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  424. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  425. Application.End (runstate);
  426. }
  427. [Fact]
  428. [AutoInitShutdown]
  429. public void ButtonAlignment_Four_On_Too_Small_Width ()
  430. {
  431. RunState runstate = null;
  432. var d = (FakeDriver)Application.Driver;
  433. var title = "1234";
  434. // E.g "|[ yes ][ no ][ maybe ][ never ]|"
  435. var btn1Text = "yes";
  436. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  437. var btn2Text = "no";
  438. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  439. var btn3Text = "maybe";
  440. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  441. var btn4Text = "never";
  442. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  443. var buttonRow = string.Empty;
  444. var width = 30;
  445. d.SetBufferSize (width, 1);
  446. // Default - Center
  447. buttonRow = $"{CM.Glyphs.VLine}es {CM.Glyphs.RightBracket} {btn2} {btn3} {CM.Glyphs.LeftBracket} neve{CM.Glyphs.VLine}";
  448. (runstate, var dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  449. Assert.Equal (new Size (width, 1), dlg.Frame.Size);
  450. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  451. Application.End (runstate);
  452. // Justify
  453. buttonRow = $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} yes {CM.Glyphs.LeftBracket} no {CM.Glyphs.LeftBracket} maybe {CM.Glyphs.LeftBracket} never {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}";
  454. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  455. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  456. // Right
  457. buttonRow = $"{CM.Glyphs.VLine}{CM.Glyphs.RightBracket} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  458. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  459. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  460. // Left
  461. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {CM.Glyphs.LeftBracket} n{CM.Glyphs.VLine}";
  462. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  463. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
  464. }
  465. [Fact]
  466. [AutoInitShutdown]
  467. public void ButtonAlignment_Four_Wider ()
  468. {
  469. RunState runstate = null;
  470. var d = (FakeDriver)Application.Driver;
  471. var title = "1234";
  472. // E.g "|[ yes ][ no ][ maybe ]|"
  473. var btn1Text = "yes";
  474. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  475. var btn2Text = "no";
  476. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  477. var btn3Text = "你你你你你"; // This is a wide char
  478. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  479. // Requires a Nerd Font
  480. var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
  481. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  482. // Note extra spaces to make dialog even wider
  483. // 123456 123456
  484. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  485. var width = buttonRow.GetColumns ();
  486. d.SetBufferSize (width, 3);
  487. // Default - Center
  488. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  489. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  490. Application.End (runstate);
  491. // Justify
  492. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  493. Assert.Equal (width, buttonRow.GetColumns ());
  494. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  495. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  496. Application.End (runstate);
  497. // Right
  498. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  499. Assert.Equal (width, buttonRow.GetColumns ());
  500. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  501. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  502. Application.End (runstate);
  503. // Left
  504. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  505. Assert.Equal (width, buttonRow.GetColumns ());
  506. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  507. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  508. Application.End (runstate);
  509. }
  510. [Fact]
  511. [AutoInitShutdown]
  512. public void ButtonAlignment_Four_WideOdd ()
  513. {
  514. RunState runstate = null;
  515. var d = (FakeDriver)Application.Driver;
  516. var title = "1234";
  517. // E.g "|[ yes ][ no ][ maybe ]|"
  518. var btn1Text = "really long button 1";
  519. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  520. var btn2Text = "really long button 2";
  521. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  522. var btn3Text = "really long button 3";
  523. var btn3 = $"{CM.Glyphs.LeftBracket} {btn3Text} {CM.Glyphs.RightBracket}";
  524. var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
  525. var btn4 = $"{CM.Glyphs.LeftBracket} {btn4Text} {CM.Glyphs.RightBracket}";
  526. // Note extra spaces to make dialog even wider
  527. // 123456 1234567
  528. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  529. var width = buttonRow.Length;
  530. d.SetBufferSize (buttonRow.Length, 1);
  531. // Default - Center
  532. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  533. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  534. Application.End (runstate);
  535. // Justify
  536. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  537. Assert.Equal (width, buttonRow.Length);
  538. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  539. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  540. Application.End (runstate);
  541. // Right
  542. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {btn3} {btn4}{CM.Glyphs.VLine}";
  543. Assert.Equal (width, buttonRow.Length);
  544. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  545. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  546. Application.End (runstate);
  547. // Left
  548. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {btn3} {btn4} {CM.Glyphs.VLine}";
  549. Assert.Equal (width, buttonRow.Length);
  550. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
  551. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  552. Application.End (runstate);
  553. }
  554. [Fact]
  555. [AutoInitShutdown]
  556. public void Zero_Buttons_Works ()
  557. {
  558. RunState runstate = null;
  559. var d = (FakeDriver)Application.Driver;
  560. var title = "1234";
  561. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.VLine}";
  562. var width = buttonRow.Length;
  563. d.SetBufferSize (buttonRow.Length, 3);
  564. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
  565. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  566. Application.End (runstate);
  567. }
  568. [Fact]
  569. [AutoInitShutdown]
  570. public void One_Button_Works ()
  571. {
  572. RunState runstate = null;
  573. var d = (FakeDriver)Application.Driver;
  574. var title = "";
  575. var btnText = "ok";
  576. var buttonRow = $"{CM.Glyphs.VLine} {CM.Glyphs.LeftBracket} {btnText} {CM.Glyphs.RightBracket} {CM.Glyphs.VLine}";
  577. var width = buttonRow.Length;
  578. d.SetBufferSize (buttonRow.Length, 10);
  579. (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
  580. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  581. Application.End (runstate);
  582. }
  583. [Fact]
  584. [AutoInitShutdown]
  585. public void Add_Button_Works ()
  586. {
  587. RunState runstate = null;
  588. var d = (FakeDriver)Application.Driver;
  589. var title = "1234";
  590. var btn1Text = "yes";
  591. var btn1 = $"{CM.Glyphs.LeftBracket} {btn1Text} {CM.Glyphs.RightBracket}";
  592. var btn2Text = "no";
  593. var btn2 = $"{CM.Glyphs.LeftBracket} {btn2Text} {CM.Glyphs.RightBracket}";
  594. // We test with one button first, but do this to get the width right for 2
  595. var width = $@"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}".Length;
  596. d.SetBufferSize (width, 1);
  597. // Default (center)
  598. var dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Center };
  599. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  600. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  601. runstate = Application.Begin (dlg);
  602. var buttonRow = $"{CM.Glyphs.VLine} {btn1} {CM.Glyphs.VLine}";
  603. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  604. // Now add a second button
  605. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2} {CM.Glyphs.VLine}";
  606. dlg.AddButton (new Button (btn2Text));
  607. bool first = false;
  608. Application.RunIteration (ref runstate, ref first);
  609. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  610. Application.End (runstate);
  611. // Justify
  612. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Justify };
  613. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  614. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  615. runstate = Application.Begin (dlg);
  616. buttonRow = $"{CM.Glyphs.VLine} {btn1}{CM.Glyphs.VLine}";
  617. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  618. // Now add a second button
  619. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2}{CM.Glyphs.VLine}";
  620. dlg.AddButton (new Button (btn2Text));
  621. first = false;
  622. Application.RunIteration (ref runstate, ref first);
  623. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  624. Application.End (runstate);
  625. // Right
  626. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Right };
  627. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  628. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  629. runstate = Application.Begin (dlg);
  630. buttonRow = $"{CM.Glyphs.VLine}{new string (' ', width - btn1.Length - 2)}{btn1}{CM.Glyphs.VLine}";
  631. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  632. // Now add a second button
  633. buttonRow = $"{CM.Glyphs.VLine} {btn1} {btn2}{CM.Glyphs.VLine}";
  634. dlg.AddButton (new Button (btn2Text));
  635. first = false;
  636. Application.RunIteration (ref runstate, ref first);
  637. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  638. Application.End (runstate);
  639. // Left
  640. dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Left };
  641. // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
  642. dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
  643. runstate = Application.Begin (dlg);
  644. buttonRow = $"{CM.Glyphs.VLine}{btn1}{new string (' ', width - btn1.Length - 2)}{CM.Glyphs.VLine}";
  645. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  646. // Now add a second button
  647. buttonRow = $"{CM.Glyphs.VLine}{btn1} {btn2} {CM.Glyphs.VLine}";
  648. dlg.AddButton (new Button (btn2Text));
  649. first = false;
  650. Application.RunIteration (ref runstate, ref first);
  651. TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
  652. Application.End (runstate);
  653. }
  654. [Fact]
  655. [AutoInitShutdown]
  656. public void FileDialog_FileSystemWatcher ()
  657. {
  658. for (int i = 0; i < 8; i++) {
  659. var fd = new FileDialog ();
  660. fd.Ready += (s, e) => Application.RequestStop ();
  661. Application.Run (fd);
  662. }
  663. }
  664. [Fact, AutoInitShutdown]
  665. public void Dialog_Opened_From_Another_Dialog ()
  666. {
  667. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  668. var btn1 = new Button ("press me 1");
  669. Button btn2 = null;
  670. Button btn3 = null;
  671. string expected = null;
  672. btn1.Clicked += (s, e) => {
  673. btn2 = new Button ("Show Sub");
  674. btn3 = new Button ("Close");
  675. btn3.Clicked += (s, e) => Application.RequestStop ();
  676. btn2.Clicked += (s, e) => {
  677. // Don't test MessageBox in Dialog unit tests!
  678. var subBtn = new Button ("Ok") { IsDefault = true };
  679. var subDlg = new Dialog (subBtn) { Text = "ya", Width = 20, Height = 5 };
  680. subBtn.Clicked += (s, e) => Application.RequestStop (subDlg);
  681. Application.Run (subDlg);
  682. };
  683. var dlg = new Dialog (btn2, btn3);
  684. Application.Run (dlg);
  685. };
  686. var btn = $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Ok {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
  687. var iterations = -1;
  688. Application.Iteration += (s, a) => {
  689. iterations++;
  690. if (iterations == 0) {
  691. Assert.True (btn1.NewKeyDownEvent (new (KeyCode.Space)));
  692. } else if (iterations == 1) {
  693. expected = @$"
  694. ┌───────────────────────┐
  695. │ │
  696. │ │
  697. │ │
  698. │ │
  699. │ │
  700. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  701. └───────────────────────┘";
  702. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  703. Assert.True (btn2.NewKeyDownEvent (new (KeyCode.Space)));
  704. } else if (iterations == 2) {
  705. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  706. ┌───────────────────────┐
  707. │ ┌──────────────────┐ │
  708. │ │ya │ │
  709. │ │ │ │
  710. │ │ {btn} │ │
  711. │ └──────────────────┘ │
  712. │{CM.Glyphs.LeftBracket} Show Sub {CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Close {CM.Glyphs.RightBracket} │
  713. └───────────────────────┘", output);
  714. Assert.True (Application.Current.NewKeyDownEvent (new (KeyCode.Enter)));
  715. } else if (iterations == 3) {
  716. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  717. Assert.True (btn3.NewKeyDownEvent (new (KeyCode.Space)));
  718. } else if (iterations == 4) {
  719. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  720. Application.RequestStop ();
  721. }
  722. };
  723. Application.Run ();
  724. Application.Shutdown ();
  725. Assert.Equal (4, iterations);
  726. }
  727. [Fact, AutoInitShutdown]
  728. public void Dialog_In_Window_With_Size_One_Button_Aligns ()
  729. {
  730. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  731. var win = new Window ();
  732. int iterations = 0;
  733. Application.Iteration += (s, a) => {
  734. if (++iterations > 2) {
  735. Application.RequestStop ();
  736. }
  737. };
  738. var btn = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  739. win.Loaded += (s, a) => {
  740. var dlg = new Dialog (new Button ("Ok")) { Width = 18, Height = 3 };
  741. dlg.Loaded += (s, a) => {
  742. Application.Refresh ();
  743. var expected = @$"
  744. ┌──────────────────┐
  745. │┌────────────────┐│
  746. ││ {btn} ││
  747. │└────────────────┘│
  748. └──────────────────┘";
  749. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  750. };
  751. Application.Run (dlg);
  752. };
  753. Application.Run (win);
  754. }
  755. // [Theory, AutoInitShutdown]
  756. // [InlineData (5)]
  757. // //[InlineData (6)]
  758. // //[InlineData (7)]
  759. // //[InlineData (8)]
  760. // //[InlineData (9)]
  761. // public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height)
  762. // {
  763. // ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  764. // var win = new Window ();
  765. // Application.Iteration += (s, a) => {
  766. // var dlg = new Dialog ("Test", new Button ("Ok"));
  767. // dlg.LayoutComplete += (s, a) => {
  768. // Application.Refresh ();
  769. // // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)??
  770. // var expected = @"
  771. //┌┌┤Test├─────────┐─┐
  772. //││ │ │
  773. //││ [ Ok ] │ │
  774. //│└───────────────┘ │
  775. //└──────────────────┘";
  776. // _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  777. // dlg.RequestStop ();
  778. // win.RequestStop ();
  779. // };
  780. // Application.Run (dlg);
  781. // };
  782. // Application.Run (win);
  783. // Application.Shutdown ();
  784. // }
  785. // TODO: This is not really a Dialog test, but a ViewLayout test (Width = Dim.Fill (1) - Dim.Function (Btn_Width))
  786. // TODO: Move (and simplify)
  787. [Fact, AutoInitShutdown]
  788. public void Dialog_In_Window_With_TextField_And_Button_AnchorEnd ()
  789. {
  790. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  791. var win = new Window ();
  792. int iterations = 0;
  793. Application.Iteration += (s, a) => {
  794. if (++iterations > 2) {
  795. Application.RequestStop ();
  796. }
  797. };
  798. var b = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  799. win.Loaded += (s, a) => {
  800. var dlg = new Dialog () { Width = 18, Height = 3 };
  801. Assert.Equal (16, dlg.Bounds.Width);
  802. Button btn = null;
  803. btn = new Button ("Ok") {
  804. X = Pos.AnchorEnd () - Pos.Function (Btn_Width)
  805. };
  806. btn.SetRelativeLayout (dlg.Bounds);
  807. Assert.Equal (6, btn.Bounds.Width);
  808. Assert.Equal (10, btn.Frame.X); // dlg.Bounds.Width (16) - btn.Frame.Width (6) = 10
  809. Assert.Equal (0, btn.Frame.Y);
  810. Assert.Equal (6, btn.Frame.Width);
  811. Assert.Equal (1, btn.Frame.Height);
  812. int Btn_Width ()
  813. {
  814. return (btn?.Bounds.Width) ?? 0;
  815. }
  816. var tf = new TextField ("01234567890123456789") {
  817. // Dim.Fill (1) fills remaining space minus 1
  818. // Dim.Function (Btn_Width) is 6
  819. Width = Dim.Fill (1) - Dim.Function (Btn_Width)
  820. };
  821. tf.SetRelativeLayout (dlg.Bounds);
  822. Assert.Equal (9, tf.Bounds.Width); // dlg.Bounds.Width (16) - Dim.Fill (1) - Dim.Function (6) = 9
  823. Assert.Equal (0, tf.Frame.X);
  824. Assert.Equal (0, tf.Frame.Y);
  825. Assert.Equal (9, tf.Frame.Width);
  826. Assert.Equal (1, tf.Frame.Height);
  827. dlg.Loaded += (s, a) => {
  828. Application.Refresh ();
  829. Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
  830. Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
  831. var expected = @$"
  832. ┌──────────────────┐
  833. │┌────────────────┐│
  834. ││23456789 {b}││
  835. │└────────────────┘│
  836. └──────────────────┘";
  837. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  838. dlg.SetNeedsLayout ();
  839. dlg.LayoutSubviews ();
  840. Application.Refresh ();
  841. Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
  842. Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
  843. expected = @$"
  844. ┌──────────────────┐
  845. │┌────────────────┐│
  846. ││23456789 {b}││
  847. │└────────────────┘│
  848. └──────────────────┘";
  849. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  850. };
  851. dlg.Add (btn, tf);
  852. Application.Run (dlg);
  853. };
  854. Application.Run (win);
  855. }
  856. }
  857. }