LayoutTests.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. using System;
  2. using System.Text;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests;
  8. public class LayoutTests {
  9. readonly ITestOutputHelper _output;
  10. public LayoutTests (ITestOutputHelper output) => _output = output;
  11. [Fact]
  12. public void TopologicalSort_Missing_Add ()
  13. {
  14. var root = new View ();
  15. var sub1 = new View ();
  16. root.Add (sub1);
  17. var sub2 = new View ();
  18. sub1.Width = Dim.Width (sub2);
  19. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  20. sub2.Width = Dim.Width (sub1);
  21. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  22. root.Dispose ();
  23. sub1.Dispose ();
  24. sub2.Dispose ();
  25. }
  26. [Fact]
  27. public void TopologicalSort_Recursive_Ref ()
  28. {
  29. var root = new View ();
  30. var sub1 = new View ();
  31. root.Add (sub1);
  32. var sub2 = new View ();
  33. root.Add (sub2);
  34. sub2.Width = Dim.Width (sub2);
  35. var exception = Record.Exception (root.LayoutSubviews);
  36. Assert.Null (exception);
  37. root.Dispose ();
  38. sub1.Dispose ();
  39. sub2.Dispose ();
  40. }
  41. [Fact]
  42. public void LayoutSubviews_No_SuperView ()
  43. {
  44. var root = new View ();
  45. var first = new View () { Id = "first", X = 1, Y = 2, Height = 3, Width = 4 };
  46. root.Add (first);
  47. var second = new View () { Id = "second" };
  48. root.Add (second);
  49. second.X = Pos.Right (first) + 1;
  50. root.LayoutSubviews ();
  51. Assert.Equal (6, second.Frame.X);
  52. root.Dispose ();
  53. first.Dispose ();
  54. second.Dispose ();
  55. }
  56. [Fact]
  57. public void LayoutSubviews_RootHas_SuperView ()
  58. {
  59. var top = new View ();
  60. var root = new View ();
  61. top.Add (root);
  62. var first = new View () { Id = "first", X = 1, Y = 2, Height = 3, Width = 4 };
  63. root.Add (first);
  64. var second = new View () { Id = "second" };
  65. root.Add (second);
  66. second.X = Pos.Right (first) + 1;
  67. root.LayoutSubviews ();
  68. Assert.Equal (6, second.Frame.X);
  69. root.Dispose ();
  70. top.Dispose ();
  71. first.Dispose ();
  72. second.Dispose ();
  73. }
  74. [Fact]
  75. public void LayoutSubviews_ViewThatRefsSubView_Throws ()
  76. {
  77. var root = new View ();
  78. var super = new View ();
  79. root.Add (super);
  80. var sub = new View ();
  81. super.Add (sub);
  82. super.Width = Dim.Width (sub);
  83. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  84. root.Dispose ();
  85. super.Dispose ();
  86. }
  87. [Fact] [AutoInitShutdown]
  88. public void TrySetWidth_ForceValidatePosDim ()
  89. {
  90. var top = new View () {
  91. X = 0,
  92. Y = 0,
  93. Width = 80
  94. };
  95. var v = new View () {
  96. Width = Dim.Fill (),
  97. ValidatePosDim = true
  98. };
  99. top.Add (v);
  100. Assert.False (v.TrySetWidth (70, out int rWidth));
  101. Assert.Equal (70, rWidth);
  102. v.Width = Dim.Fill (1);
  103. Assert.False (v.TrySetWidth (70, out rWidth));
  104. Assert.Equal (69, rWidth);
  105. v.Width = null;
  106. Assert.True (v.TrySetWidth (70, out rWidth));
  107. Assert.Equal (70, rWidth);
  108. Assert.False (v.IsInitialized);
  109. Application.Top.Add (top);
  110. Application.Begin (Application.Top);
  111. Assert.True (v.IsInitialized);
  112. v.Width = Dim.Fill (1);
  113. v.LayoutStyle = LayoutStyle.Computed;
  114. v.Width = 75;
  115. v.LayoutStyle = LayoutStyle.Absolute;
  116. Assert.True (v.TrySetWidth (60, out rWidth));
  117. Assert.Equal (60, rWidth);
  118. }
  119. [Fact] [AutoInitShutdown]
  120. public void TrySetHeight_ForceValidatePosDim ()
  121. {
  122. var top = new View () {
  123. X = 0,
  124. Y = 0,
  125. Height = 20
  126. };
  127. var v = new View () {
  128. Height = Dim.Fill (),
  129. ValidatePosDim = true
  130. };
  131. top.Add (v);
  132. Assert.False (v.TrySetHeight (10, out int rHeight));
  133. Assert.Equal (10, rHeight);
  134. v.Height = Dim.Fill (1);
  135. Assert.False (v.TrySetHeight (10, out rHeight));
  136. Assert.Equal (9, rHeight);
  137. v.Height = null;
  138. Assert.True (v.TrySetHeight (10, out rHeight));
  139. Assert.Equal (10, rHeight);
  140. Assert.False (v.IsInitialized);
  141. Application.Top.Add (top);
  142. Application.Begin (Application.Top);
  143. Assert.True (v.IsInitialized);
  144. v.Height = Dim.Fill (1);
  145. v.LayoutStyle = LayoutStyle.Computed;
  146. v.Height = 15;
  147. v.LayoutStyle = LayoutStyle.Absolute;
  148. v.Height = 15;
  149. Assert.True (v.TrySetHeight (5, out rHeight));
  150. Assert.Equal (5, rHeight);
  151. }
  152. [Fact] [TestRespondersDisposed]
  153. public void GetCurrentWidth_TrySetWidth ()
  154. {
  155. var top = new View () {
  156. X = 0,
  157. Y = 0,
  158. Width = 80
  159. };
  160. var v = new View () {
  161. Width = Dim.Fill ()
  162. };
  163. top.Add (v);
  164. top.BeginInit ();
  165. top.EndInit ();
  166. top.LayoutSubviews ();
  167. Assert.False (v.AutoSize);
  168. Assert.True (v.TrySetWidth (0, out _));
  169. Assert.Equal (80, v.Frame.Width);
  170. v.Width = Dim.Fill (1);
  171. top.LayoutSubviews ();
  172. Assert.True (v.TrySetWidth (0, out _));
  173. Assert.Equal (79, v.Frame.Width);
  174. v.AutoSize = true;
  175. top.LayoutSubviews ();
  176. Assert.True (v.TrySetWidth (0, out _));
  177. top.Dispose ();
  178. }
  179. [Fact]
  180. public void GetCurrentHeight_TrySetHeight ()
  181. {
  182. var top = new View () {
  183. X = 0,
  184. Y = 0,
  185. Height = 20
  186. };
  187. var v = new View () {
  188. Height = Dim.Fill ()
  189. };
  190. top.Add (v);
  191. top.BeginInit ();
  192. top.EndInit ();
  193. top.LayoutSubviews ();
  194. Assert.False (v.AutoSize);
  195. Assert.True (v.TrySetHeight (0, out _));
  196. Assert.Equal (20, v.Frame.Height);
  197. v.Height = Dim.Fill (1);
  198. top.LayoutSubviews ();
  199. Assert.True (v.TrySetHeight (0, out _));
  200. Assert.Equal (19, v.Frame.Height);
  201. v.AutoSize = true;
  202. top.LayoutSubviews ();
  203. Assert.True (v.TrySetHeight (0, out _));
  204. top.Dispose ();
  205. }
  206. [Fact]
  207. public void AutoSize_False_If_Text_Emmpty ()
  208. {
  209. var view1 = new View ();
  210. var view2 = new View ("");
  211. var view3 = new View () { Text = "" };
  212. Assert.False (view1.AutoSize);
  213. Assert.False (view2.AutoSize);
  214. Assert.False (view3.AutoSize);
  215. view1.Dispose ();
  216. view2.Dispose ();
  217. view3.Dispose ();
  218. }
  219. [Fact]
  220. public void AutoSize_False_If_Text_Is_Not_Emmpty ()
  221. {
  222. var view1 = new View ();
  223. view1.Text = "Hello World";
  224. var view2 = new View ("Hello World");
  225. var view3 = new View () { Text = "Hello World" };
  226. Assert.False (view1.AutoSize);
  227. Assert.False (view2.AutoSize);
  228. Assert.False (view3.AutoSize);
  229. view1.Dispose ();
  230. view2.Dispose ();
  231. view3.Dispose ();
  232. }
  233. [Fact]
  234. public void AutoSize_True_Label_If_Text_Emmpty ()
  235. {
  236. var label1 = new Label ();
  237. var label2 = new Label ("");
  238. var label3 = new Label () { Text = "" };
  239. Assert.True (label1.AutoSize);
  240. Assert.True (label2.AutoSize);
  241. Assert.True (label3.AutoSize);
  242. label1.Dispose ();
  243. label2.Dispose ();
  244. label3.Dispose ();
  245. }
  246. [Fact]
  247. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  248. {
  249. var label1 = new Label ();
  250. label1.Text = "Hello World";
  251. var label2 = new Label ("Hello World");
  252. var label3 = new Label () { Text = "Hello World" };
  253. Assert.True (label1.AutoSize);
  254. Assert.True (label2.AutoSize);
  255. Assert.True (label3.AutoSize);
  256. label1.Dispose ();
  257. label2.Dispose ();
  258. label3.Dispose ();
  259. }
  260. [Fact]
  261. public void AutoSize_False_ResizeView_Is_Always_False ()
  262. {
  263. var super = new View ();
  264. var label = new Label () { AutoSize = false };
  265. super.Add (label);
  266. label.Text = "New text";
  267. super.LayoutSubviews ();
  268. Assert.False (label.AutoSize);
  269. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  270. super.Dispose ();
  271. }
  272. [Fact]
  273. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  274. {
  275. var super = new View ();
  276. var label = new Label ();
  277. label.Text = "New text";
  278. // BUGBUG: v2 - label was never added to super, so it was never laid out.
  279. super.Add (label);
  280. super.LayoutSubviews ();
  281. Assert.True (label.AutoSize);
  282. Assert.Equal ("(0,0,8,1)", label.Bounds.ToString ());
  283. super.Dispose ();
  284. }
  285. [Fact] [AutoInitShutdown]
  286. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  287. {
  288. var win = new Window (new Rect (0, 0, 30, 80));
  289. var label = new Label () { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  290. win.Add (label);
  291. Application.Top.Add (win);
  292. // Text is empty but height=1 by default, see Label view
  293. Assert.False (label.AutoSize);
  294. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  295. label.Text = "New text\nNew line";
  296. Application.Top.LayoutSubviews ();
  297. Assert.False (label.AutoSize);
  298. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  299. Assert.False (label.IsInitialized);
  300. var rs = Application.Begin (Application.Top);
  301. Assert.True (label.IsInitialized);
  302. Assert.False (label.AutoSize);
  303. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  304. Application.End (rs);
  305. }
  306. [Fact] [AutoInitShutdown]
  307. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  308. {
  309. var win = new Window (new Rect (0, 0, 30, 80));
  310. var label = new Label () { Width = Dim.Fill () };
  311. win.Add (label);
  312. Application.Top.Add (win);
  313. Assert.True (label.IsAdded);
  314. // Text is empty but height=1 by default, see Label view
  315. Assert.True (label.AutoSize);
  316. // BUGBUG: LayoutSubviews has not been called, so this test is not really valid (pos/dim are indeterminate, not 0)
  317. // Not really a bug because View call OnResizeNeeded method on the SetInitialProperties method
  318. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  319. label.Text = "First line\nSecond line";
  320. Application.Top.LayoutSubviews ();
  321. Assert.True (label.AutoSize);
  322. // BUGBUG: This test is bogus: label has not been initialized. pos/dim is indeterminate!
  323. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  324. Assert.False (label.IsInitialized);
  325. var rs = Application.Begin (Application.Top);
  326. Assert.True (label.AutoSize);
  327. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  328. Assert.True (label.IsInitialized);
  329. label.AutoSize = false;
  330. Application.Refresh ();
  331. Assert.False (label.AutoSize);
  332. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  333. Application.End (rs);
  334. }
  335. [Fact] [AutoInitShutdown]
  336. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  337. {
  338. var win = new Window (new Rect (0, 0, 30, 80));
  339. var label = new Label () { Width = Dim.Fill () };
  340. win.Add (label);
  341. Application.Top.Add (win);
  342. // Text is empty but height=1 by default, see Label view
  343. Assert.True (label.AutoSize);
  344. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  345. var rs = Application.Begin (Application.Top);
  346. Assert.True (label.AutoSize);
  347. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  348. // and height 0 because wasn't set and the text is empty
  349. // BUGBUG: Because of #2450, this test is bogus: pos/dim is indeterminate!
  350. //Assert.Equal ("(0,0,28,0)", label.Bounds.ToString ());
  351. label.Text = "First line\nSecond line";
  352. Application.Refresh ();
  353. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  354. // and height 2 because wasn't set and the text has 2 lines
  355. Assert.True (label.AutoSize);
  356. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  357. label.AutoSize = false;
  358. Application.Refresh ();
  359. // Here the SetMinWidthHeight ensuring the minimum height
  360. Assert.False (label.AutoSize);
  361. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  362. label.Text = "First changed line\nSecond changed line\nNew line";
  363. Application.Refresh ();
  364. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  365. // height 1 because wasn't set and SetMinWidthHeight ensuring the minimum height
  366. Assert.False (label.AutoSize);
  367. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  368. label.AutoSize = true;
  369. Application.Refresh ();
  370. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  371. // and height 3 because wasn't set and the text has 3 lines
  372. Assert.True (label.AutoSize);
  373. // BUGBUG: v2 - AutoSize is broken - temporarily disabling test See #2432
  374. //Assert.Equal ("(0,0,28,3)", label.Bounds.ToString ());
  375. Application.End (rs);
  376. }
  377. [Fact] [AutoInitShutdown]
  378. public void AutoSize_True_Setting_With_Height_Horizontal ()
  379. {
  380. var label = new Label ("Hello") { Width = 10, Height = 2 };
  381. var viewX = new View ("X") { X = Pos.Right (label) };
  382. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  383. Application.Top.Add (label, viewX, viewY);
  384. var rs = Application.Begin (Application.Top);
  385. Assert.True (label.AutoSize);
  386. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  387. string expected = @"
  388. Hello X
  389. Y
  390. ";
  391. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  392. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  393. label.AutoSize = false;
  394. Application.Refresh ();
  395. Assert.False (label.AutoSize);
  396. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  397. expected = @"
  398. Hello X
  399. Y
  400. ";
  401. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  402. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  403. Application.End (rs);
  404. }
  405. [Fact] [AutoInitShutdown]
  406. public void AutoSize_True_Setting_With_Height_Vertical ()
  407. {
  408. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  409. var viewX = new View ("X") { X = Pos.Right (label) };
  410. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  411. Application.Top.Add (label, viewX, viewY);
  412. var rs = Application.Begin (Application.Top);
  413. Assert.True (label.AutoSize);
  414. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  415. string expected = @"
  416. H X
  417. e
  418. l
  419. l
  420. o
  421. Y
  422. ";
  423. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  424. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  425. label.AutoSize = false;
  426. Application.Refresh ();
  427. Assert.False (label.AutoSize);
  428. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  429. expected = @"
  430. H X
  431. e
  432. l
  433. l
  434. o
  435. Y
  436. ";
  437. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  438. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  439. Application.End (rs);
  440. }
  441. [Fact]
  442. [AutoInitShutdown]
  443. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  444. {
  445. var lbl = new Label ("123");
  446. Application.Top.Add (lbl);
  447. var rs = Application.Begin (Application.Top);
  448. Assert.True (lbl.AutoSize);
  449. Assert.Equal ("123 ", GetContents ());
  450. lbl.Text = "12";
  451. // Here the AutoSize ensuring the right size with width 3 (Dim.Absolute)
  452. // that was set on the OnAdded method with the text length of 3
  453. // and height 1 because wasn't set and the text has 1 line
  454. Assert.Equal (new Rect (0, 0, 3, 1), lbl.Frame);
  455. Assert.Equal (new Rect (0, 0, 3, 1), lbl._needsDisplayRect);
  456. Assert.Equal (new Rect (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  457. Assert.True (lbl.SuperView.LayoutNeeded);
  458. lbl.SuperView.Draw ();
  459. Assert.Equal ("12 ", GetContents ());
  460. string GetContents ()
  461. {
  462. string text = "";
  463. for (int i = 0; i < 4; i++) {
  464. text += Application.Driver.Contents [0, i].Rune;
  465. }
  466. return text;
  467. }
  468. Application.End (rs);
  469. }
  470. [Fact] [AutoInitShutdown]
  471. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  472. {
  473. string text = $"First line{Environment.NewLine}Second line";
  474. var horizontalView = new View () {
  475. Width = 20,
  476. Height = 1,
  477. Text = text
  478. };
  479. var verticalView = new View () {
  480. Y = 3,
  481. Height = 20,
  482. Width = 1,
  483. Text = text,
  484. TextDirection = TextDirection.TopBottom_LeftRight
  485. };
  486. var win = new Window () {
  487. Width = Dim.Fill (),
  488. Height = Dim.Fill (),
  489. Text = "Window"
  490. };
  491. win.Add (horizontalView, verticalView);
  492. Application.Top.Add (win);
  493. var rs = Application.Begin (Application.Top);
  494. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  495. Assert.False (horizontalView.AutoSize);
  496. Assert.False (verticalView.AutoSize);
  497. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  498. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  499. string expected = @"
  500. ┌──────────────────────────────┐
  501. │First line Second li │
  502. │ │
  503. │ │
  504. │F │
  505. │i │
  506. │r │
  507. │s │
  508. │t │
  509. │ │
  510. │l │
  511. │i │
  512. │n │
  513. │e │
  514. │ │
  515. │S │
  516. │e │
  517. │c │
  518. │o │
  519. │n │
  520. │d │
  521. │ │
  522. │l │
  523. │i │
  524. │ │
  525. │ │
  526. │ │
  527. │ │
  528. │ │
  529. │ │
  530. │ │
  531. └──────────────────────────────┘
  532. ";
  533. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  534. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  535. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  536. Application.Top.Draw ();
  537. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  538. expected = @"
  539. ┌──────────────────────────────┐
  540. │First line Second li │
  541. │ │
  542. │ │
  543. │最 │
  544. │初 │
  545. │の │
  546. │行 │
  547. │ │
  548. │二 │
  549. │行 │
  550. │目 │
  551. │ │
  552. │ │
  553. │ │
  554. │ │
  555. │ │
  556. │ │
  557. │ │
  558. │ │
  559. │ │
  560. │ │
  561. │ │
  562. │ │
  563. │ │
  564. │ │
  565. │ │
  566. │ │
  567. │ │
  568. │ │
  569. │ │
  570. └──────────────────────────────┘
  571. ";
  572. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  573. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  574. Application.End (rs);
  575. }
  576. [Fact] [AutoInitShutdown]
  577. public void TextDirection_Toggle ()
  578. {
  579. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  580. var view = new View ();
  581. win.Add (view);
  582. Application.Top.Add (win);
  583. var rs = Application.Begin (Application.Top);
  584. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  585. Assert.Equal (new Rect (0, 0, 22, 22), win.Frame);
  586. Assert.Equal (new Rect (0, 0, 22, 22), win.Margin.Frame);
  587. Assert.Equal (new Rect (0, 0, 22, 22), win.Border.Frame);
  588. Assert.Equal (new Rect (1, 1, 20, 20), win.Padding.Frame);
  589. Assert.False (view.AutoSize);
  590. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  591. Assert.Equal (Rect.Empty, view.Frame);
  592. Assert.Equal ("Absolute(0)", view.X.ToString ());
  593. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  594. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  595. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  596. string expected = @"
  597. ┌────────────────────┐
  598. │ │
  599. │ │
  600. │ │
  601. │ │
  602. │ │
  603. │ │
  604. │ │
  605. │ │
  606. │ │
  607. │ │
  608. │ │
  609. │ │
  610. │ │
  611. │ │
  612. │ │
  613. │ │
  614. │ │
  615. │ │
  616. │ │
  617. │ │
  618. └────────────────────┘
  619. ";
  620. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  621. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  622. view.Text = "Hello World";
  623. view.Width = 11;
  624. view.Height = 1;
  625. win.LayoutSubviews ();
  626. Application.Refresh ();
  627. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  628. Assert.Equal ("Absolute(0)", view.X.ToString ());
  629. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  630. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  631. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  632. expected = @"
  633. ┌────────────────────┐
  634. │Hello World │
  635. │ │
  636. │ │
  637. │ │
  638. │ │
  639. │ │
  640. │ │
  641. │ │
  642. │ │
  643. │ │
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ │
  649. │ │
  650. │ │
  651. │ │
  652. │ │
  653. │ │
  654. └────────────────────┘
  655. ";
  656. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  657. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  658. view.AutoSize = true;
  659. view.Text = "Hello Worlds";
  660. Application.Refresh ();
  661. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  662. Assert.Equal ("Absolute(0)", view.X.ToString ());
  663. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  664. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  665. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  666. expected = @"
  667. ┌────────────────────┐
  668. │Hello Worlds │
  669. │ │
  670. │ │
  671. │ │
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. │ │
  684. │ │
  685. │ │
  686. │ │
  687. │ │
  688. └────────────────────┘
  689. ";
  690. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  691. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  692. view.TextDirection = TextDirection.TopBottom_LeftRight;
  693. Application.Refresh ();
  694. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  695. Assert.Equal ("Absolute(0)", view.X.ToString ());
  696. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  697. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  698. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  699. expected = @"
  700. ┌────────────────────┐
  701. │H │
  702. │e │
  703. │l │
  704. │l │
  705. │o │
  706. │ │
  707. │W │
  708. │o │
  709. │r │
  710. │l │
  711. │d │
  712. │s │
  713. │ │
  714. │ │
  715. │ │
  716. │ │
  717. │ │
  718. │ │
  719. │ │
  720. │ │
  721. └────────────────────┘
  722. ";
  723. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  724. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  725. view.AutoSize = false;
  726. view.Height = 1;
  727. Application.Refresh ();
  728. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  729. Assert.Equal ("Absolute(0)", view.X.ToString ());
  730. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  731. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  732. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  733. expected = @"
  734. ┌────────────────────┐
  735. │HelloWorlds │
  736. │ │
  737. │ │
  738. │ │
  739. │ │
  740. │ │
  741. │ │
  742. │ │
  743. │ │
  744. │ │
  745. │ │
  746. │ │
  747. │ │
  748. │ │
  749. │ │
  750. │ │
  751. │ │
  752. │ │
  753. │ │
  754. │ │
  755. └────────────────────┘
  756. ";
  757. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  758. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  759. view.PreserveTrailingSpaces = true;
  760. Application.Refresh ();
  761. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  762. Assert.Equal ("Absolute(0)", view.X.ToString ());
  763. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  764. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  765. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  766. expected = @"
  767. ┌────────────────────┐
  768. │Hello World │
  769. │ │
  770. │ │
  771. │ │
  772. │ │
  773. │ │
  774. │ │
  775. │ │
  776. │ │
  777. │ │
  778. │ │
  779. │ │
  780. │ │
  781. │ │
  782. │ │
  783. │ │
  784. │ │
  785. │ │
  786. │ │
  787. │ │
  788. └────────────────────┘
  789. ";
  790. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  791. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  792. view.PreserveTrailingSpaces = false;
  793. var f = view.Frame;
  794. view.Width = f.Height;
  795. view.Height = f.Width;
  796. view.TextDirection = TextDirection.TopBottom_LeftRight;
  797. Application.Refresh ();
  798. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  799. Assert.Equal ("Absolute(0)", view.X.ToString ());
  800. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  801. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  802. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  803. expected = @"
  804. ┌────────────────────┐
  805. │H │
  806. │e │
  807. │l │
  808. │l │
  809. │o │
  810. │ │
  811. │W │
  812. │o │
  813. │r │
  814. │l │
  815. │d │
  816. │ │
  817. │ │
  818. │ │
  819. │ │
  820. │ │
  821. │ │
  822. │ │
  823. │ │
  824. │ │
  825. └────────────────────┘
  826. ";
  827. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  828. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  829. view.AutoSize = true;
  830. Application.Refresh ();
  831. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  832. Assert.Equal ("Absolute(0)", view.X.ToString ());
  833. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  834. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  835. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  836. expected = @"
  837. ┌────────────────────┐
  838. │H │
  839. │e │
  840. │l │
  841. │l │
  842. │o │
  843. │ │
  844. │W │
  845. │o │
  846. │r │
  847. │l │
  848. │d │
  849. │s │
  850. │ │
  851. │ │
  852. │ │
  853. │ │
  854. │ │
  855. │ │
  856. │ │
  857. │ │
  858. └────────────────────┘
  859. ";
  860. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  861. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  862. Application.End (rs);
  863. }
  864. [Fact] [AutoInitShutdown]
  865. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  866. {
  867. string text = $"Fi_nish 終";
  868. var horizontalView = new View () {
  869. Id = "horizontalView",
  870. AutoSize = true,
  871. HotKeySpecifier = (Rune)'_',
  872. Text = text
  873. };
  874. var verticalView = new View () {
  875. Id = "verticalView",
  876. Y = 3,
  877. AutoSize = true,
  878. HotKeySpecifier = (Rune)'_',
  879. Text = text,
  880. TextDirection = TextDirection.TopBottom_LeftRight
  881. };
  882. var win = new Window () {
  883. Id = "win",
  884. Width = Dim.Fill (),
  885. Height = Dim.Fill (),
  886. Text = "Window"
  887. };
  888. win.Add (horizontalView, verticalView);
  889. Application.Top.Add (win);
  890. var rs = Application.Begin (Application.Top);
  891. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  892. Assert.True (horizontalView.AutoSize);
  893. Assert.True (verticalView.AutoSize);
  894. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  895. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  896. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  897. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  898. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  899. // BUGBUG - v2 - With v1 AutoSize = true Width/Height should always grow or keep initial value,
  900. // but in v2, autosize will be replaced by Dim.Fit. Disabling test for now.
  901. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  902. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  903. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  904. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  905. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  906. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  907. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  908. string expected = @"
  909. ┌────────────────────┐
  910. │Finish 終 │
  911. │ │
  912. │ │
  913. │F │
  914. │i │
  915. │n │
  916. │i │
  917. │s │
  918. │h │
  919. │ │
  920. │終 │
  921. │ │
  922. │ │
  923. │ │
  924. │ │
  925. │ │
  926. │ │
  927. │ │
  928. │ │
  929. │ │
  930. └────────────────────┘
  931. ";
  932. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  933. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  934. verticalView.Text = $"最初_の行二行目";
  935. Application.Top.Draw ();
  936. Assert.True (horizontalView.AutoSize);
  937. Assert.True (verticalView.AutoSize);
  938. // height was initialized with 8 and can only grow or keep initial value
  939. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  940. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  941. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  942. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  943. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  944. expected = @"
  945. ┌────────────────────┐
  946. │Finish 終 │
  947. │ │
  948. │ │
  949. │最 │
  950. │初 │
  951. │の │
  952. │行 │
  953. │二 │
  954. │行 │
  955. │目 │
  956. │ │
  957. │ │
  958. │ │
  959. │ │
  960. │ │
  961. │ │
  962. │ │
  963. │ │
  964. │ │
  965. │ │
  966. └────────────────────┘
  967. ";
  968. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  969. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  970. Application.End (rs);
  971. }
  972. [Fact] [AutoInitShutdown]
  973. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  974. {
  975. var label = new Label () {
  976. X = Pos.Center (),
  977. Y = Pos.Center (),
  978. Text = "Say Hello 你"
  979. };
  980. var win = new Window () {
  981. Width = Dim.Fill (),
  982. Height = Dim.Fill (),
  983. Title = "Test Demo 你"
  984. };
  985. win.Add (label);
  986. Application.Top.Add (win);
  987. Assert.True (label.AutoSize);
  988. var rs = Application.Begin (Application.Top);
  989. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  990. string expected = @$"
  991. ┌┤Test Demo 你├──────────────┐
  992. │ │
  993. │ Say Hello 你 │
  994. │ │
  995. └────────────────────────────┘
  996. ";
  997. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  998. Assert.True (label.AutoSize);
  999. label.Text = "Say Hello 你 changed";
  1000. Assert.True (label.AutoSize);
  1001. Application.Refresh ();
  1002. expected = @"
  1003. ┌┤Test Demo 你├──────────────┐
  1004. │ │
  1005. │ Say Hello 你 changed │
  1006. │ │
  1007. └────────────────────────────┘
  1008. ";
  1009. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1010. Application.End (rs);
  1011. }
  1012. [Fact] [AutoInitShutdown]
  1013. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  1014. {
  1015. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  1016. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  1017. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1018. var view4 = new View () {
  1019. Text = "Say Hello view4 你",
  1020. AutoSize = false,
  1021. Width = 10,
  1022. Height = 5,
  1023. TextDirection = TextDirection.TopBottom_LeftRight
  1024. };
  1025. var view5 = new View () {
  1026. Text = "Say Hello view5 你",
  1027. Width = 10,
  1028. Height = 5,
  1029. AutoSize = false,
  1030. TextDirection = TextDirection.TopBottom_LeftRight
  1031. };
  1032. var view6 = new View () {
  1033. AutoSize = false,
  1034. Width = 10,
  1035. Height = 5,
  1036. TextDirection = TextDirection.TopBottom_LeftRight,
  1037. Text = "Say Hello view6 你"
  1038. };
  1039. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1040. Assert.False (view1.IsInitialized);
  1041. Assert.False (view2.IsInitialized);
  1042. Assert.False (view3.IsInitialized);
  1043. Assert.False (view4.IsInitialized);
  1044. Assert.False (view5.IsInitialized);
  1045. Assert.False (view1.AutoSize);
  1046. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1047. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1048. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1049. Assert.False (view2.AutoSize);
  1050. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1051. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1052. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1053. Assert.False (view3.AutoSize);
  1054. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1055. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1056. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1057. Assert.False (view4.AutoSize);
  1058. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1059. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1060. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1061. Assert.False (view5.AutoSize);
  1062. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1063. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1064. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1065. Assert.False (view6.AutoSize);
  1066. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1067. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1068. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1069. var rs = Application.Begin (Application.Top);
  1070. Assert.True (view1.IsInitialized);
  1071. Assert.True (view2.IsInitialized);
  1072. Assert.True (view3.IsInitialized);
  1073. Assert.True (view4.IsInitialized);
  1074. Assert.True (view5.IsInitialized);
  1075. Assert.False (view1.AutoSize);
  1076. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1077. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1078. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1079. Assert.False (view2.AutoSize);
  1080. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1081. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1082. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1083. Assert.False (view3.AutoSize);
  1084. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1085. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1086. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1087. Assert.False (view4.AutoSize);
  1088. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1089. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1090. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1091. Assert.False (view5.AutoSize);
  1092. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1093. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1094. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1095. Assert.False (view6.AutoSize);
  1096. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1097. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1098. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1099. Application.End (rs);
  1100. }
  1101. [Fact] [AutoInitShutdown]
  1102. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Different_Orders ()
  1103. {
  1104. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  1105. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  1106. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1107. var view4 = new View () {
  1108. Text = "Say Hello view4 你",
  1109. AutoSize = true,
  1110. Width = 10,
  1111. Height = 5,
  1112. TextDirection = TextDirection.TopBottom_LeftRight
  1113. };
  1114. var view5 = new View () {
  1115. Text = "Say Hello view5 你",
  1116. Width = 10,
  1117. Height = 5,
  1118. AutoSize = true,
  1119. TextDirection = TextDirection.TopBottom_LeftRight
  1120. };
  1121. var view6 = new View () {
  1122. AutoSize = true,
  1123. Width = 10,
  1124. Height = 5,
  1125. TextDirection = TextDirection.TopBottom_LeftRight,
  1126. Text = "Say Hello view6 你"
  1127. };
  1128. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1129. Assert.False (view1.IsInitialized);
  1130. Assert.False (view2.IsInitialized);
  1131. Assert.False (view3.IsInitialized);
  1132. Assert.False (view4.IsInitialized);
  1133. Assert.False (view5.IsInitialized);
  1134. Assert.True (view1.AutoSize);
  1135. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1136. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1137. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1138. Assert.True (view2.AutoSize);
  1139. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1140. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1141. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1142. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1143. //Assert.True (view3.AutoSize);
  1144. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1145. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1146. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1147. //Assert.True (view4.AutoSize);
  1148. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1149. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1150. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1151. //Assert.True (view5.AutoSize);
  1152. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1153. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1154. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1155. //Assert.True (view6.AutoSize);
  1156. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1157. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1158. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1159. var rs = Application.Begin (Application.Top);
  1160. Assert.True (view1.IsInitialized);
  1161. Assert.True (view2.IsInitialized);
  1162. Assert.True (view3.IsInitialized);
  1163. Assert.True (view4.IsInitialized);
  1164. Assert.True (view5.IsInitialized);
  1165. Assert.True (view1.AutoSize);
  1166. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1167. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1168. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1169. Assert.True (view2.AutoSize);
  1170. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1171. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1172. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1173. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1174. //Assert.True (view3.AutoSize);
  1175. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1176. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1177. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1178. //Assert.True (view4.AutoSize);
  1179. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1180. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1181. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1182. //Assert.True (view5.AutoSize);
  1183. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1184. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1185. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1186. //Assert.True (view6.AutoSize);
  1187. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1188. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1189. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1190. Application.End (rs);
  1191. }
  1192. [Fact] [AutoInitShutdown]
  1193. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1194. {
  1195. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1196. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1197. Text = "Say Hello view2 你",
  1198. AutoSize = true,
  1199. TextDirection = TextDirection.TopBottom_LeftRight
  1200. };
  1201. Application.Top.Add (view1, view2);
  1202. var rs = Application.Begin (Application.Top);
  1203. Assert.True (view1.AutoSize);
  1204. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1205. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1206. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1207. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1208. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1209. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1210. Assert.True (view2.AutoSize);
  1211. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1212. //Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1213. //Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1214. //Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1215. //Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1216. //Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1217. //Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1218. view1.Frame = new Rect (0, 0, 25, 4);
  1219. bool firstIteration = false;
  1220. Application.RunIteration (ref rs, ref firstIteration);
  1221. Assert.True (view1.AutoSize);
  1222. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1223. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1224. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1225. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1226. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1227. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1228. view2.Frame = new Rect (0, 0, 1, 25);
  1229. Application.RunIteration (ref rs, ref firstIteration);
  1230. Assert.True (view2.AutoSize);
  1231. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1232. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1233. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1234. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1235. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1236. //Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1237. //Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1238. Application.End (rs);
  1239. }
  1240. [Fact] [AutoInitShutdown]
  1241. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1242. {
  1243. var top = Application.Top;
  1244. var view1 = new View ();
  1245. Assert.False (view1.IsAdded);
  1246. Assert.Null (view1.X);
  1247. Assert.Null (view1.Y);
  1248. Assert.Null (view1.Width);
  1249. Assert.Null (view1.Height);
  1250. top.Add (view1);
  1251. Assert.True (view1.IsAdded);
  1252. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1253. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1254. Assert.Equal ("Absolute(0)", view1.Width.ToString ());
  1255. Assert.Equal ("Absolute(0)", view1.Height.ToString ());
  1256. var view2 = new View () {
  1257. X = Pos.Center (),
  1258. Y = Pos.Center (),
  1259. Width = Dim.Fill (),
  1260. Height = Dim.Fill ()
  1261. };
  1262. Assert.False (view2.IsAdded);
  1263. Assert.Equal ("Center", view2.X.ToString ());
  1264. Assert.Equal ("Center", view2.Y.ToString ());
  1265. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1266. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1267. top.Add (view2);
  1268. Assert.True (view2.IsAdded);
  1269. Assert.Equal ("Center", view2.X.ToString ());
  1270. Assert.Equal ("Center", view2.Y.ToString ());
  1271. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1272. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1273. }
  1274. [Theory] [AutoInitShutdown]
  1275. [InlineData (1)]
  1276. [InlineData (2)]
  1277. [InlineData (3)]
  1278. [InlineData (4)]
  1279. [InlineData (5)]
  1280. [InlineData (6)]
  1281. [InlineData (7)]
  1282. [InlineData (8)]
  1283. [InlineData (9)]
  1284. [InlineData (10)]
  1285. public void Dim_CenteredSubView_85_Percent_Height (int height)
  1286. {
  1287. var win = new Window () {
  1288. Width = Dim.Fill (),
  1289. Height = Dim.Fill ()
  1290. };
  1291. var subview = new Window () {
  1292. X = Pos.Center (),
  1293. Y = Pos.Center (),
  1294. Width = Dim.Percent (85),
  1295. Height = Dim.Percent (85)
  1296. };
  1297. win.Add (subview);
  1298. var rs = Application.Begin (win);
  1299. bool firstIteration = false;
  1300. ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  1301. Application.RunIteration (ref rs, ref firstIteration);
  1302. string expected = string.Empty;
  1303. switch (height) {
  1304. case 1:
  1305. //Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
  1306. expected = @"
  1307. ────────────────────";
  1308. break;
  1309. case 2:
  1310. //Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
  1311. expected = @"
  1312. ┌──────────────────┐
  1313. └──────────────────┘
  1314. ";
  1315. break;
  1316. case 3:
  1317. //Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
  1318. expected = @"
  1319. ┌──────────────────┐
  1320. │ │
  1321. └──────────────────┘
  1322. ";
  1323. break;
  1324. case 4:
  1325. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1326. expected = @"
  1327. ┌──────────────────┐
  1328. │ ─────────────── │
  1329. │ │
  1330. └──────────────────┘";
  1331. break;
  1332. case 5:
  1333. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1334. expected = @"
  1335. ┌──────────────────┐
  1336. │ ┌─────────────┐ │
  1337. │ └─────────────┘ │
  1338. │ │
  1339. └──────────────────┘";
  1340. break;
  1341. case 6:
  1342. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1343. expected = @"
  1344. ┌──────────────────┐
  1345. │ ┌─────────────┐ │
  1346. │ │ │ │
  1347. │ └─────────────┘ │
  1348. │ │
  1349. └──────────────────┘";
  1350. break;
  1351. case 7:
  1352. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1353. expected = @"
  1354. ┌──────────────────┐
  1355. │ ┌─────────────┐ │
  1356. │ │ │ │
  1357. │ │ │ │
  1358. │ └─────────────┘ │
  1359. │ │
  1360. └──────────────────┘";
  1361. break;
  1362. case 8:
  1363. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1364. expected = @"
  1365. ┌──────────────────┐
  1366. │ ┌─────────────┐ │
  1367. │ │ │ │
  1368. │ │ │ │
  1369. │ │ │ │
  1370. │ └─────────────┘ │
  1371. │ │
  1372. └──────────────────┘";
  1373. break;
  1374. case 9:
  1375. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1376. expected = @"
  1377. ┌──────────────────┐
  1378. │ │
  1379. │ ┌─────────────┐ │
  1380. │ │ │ │
  1381. │ │ │ │
  1382. │ │ │ │
  1383. │ └─────────────┘ │
  1384. │ │
  1385. └──────────────────┘";
  1386. break;
  1387. case 10:
  1388. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1389. expected = @"
  1390. ┌──────────────────┐
  1391. │ │
  1392. │ ┌─────────────┐ │
  1393. │ │ │ │
  1394. │ │ │ │
  1395. │ │ │ │
  1396. │ │ │ │
  1397. │ └─────────────┘ │
  1398. │ │
  1399. └──────────────────┘";
  1400. break;
  1401. }
  1402. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1403. Application.End (rs);
  1404. }
  1405. [Theory] [AutoInitShutdown]
  1406. [InlineData (1)]
  1407. [InlineData (2)]
  1408. [InlineData (3)]
  1409. [InlineData (4)]
  1410. [InlineData (5)]
  1411. [InlineData (6)]
  1412. [InlineData (7)]
  1413. [InlineData (8)]
  1414. [InlineData (9)]
  1415. [InlineData (10)]
  1416. public void Dim_CenteredSubView_85_Percent_Width (int width)
  1417. {
  1418. var win = new Window () {
  1419. Width = Dim.Fill (),
  1420. Height = Dim.Fill ()
  1421. };
  1422. var subview = new Window () {
  1423. X = Pos.Center (),
  1424. Y = Pos.Center (),
  1425. Width = Dim.Percent (85),
  1426. Height = Dim.Percent (85)
  1427. };
  1428. win.Add (subview);
  1429. var rs = Application.Begin (win);
  1430. bool firstIteration = false;
  1431. ((FakeDriver)Application.Driver).SetBufferSize (width, 7);
  1432. Application.RunIteration (ref rs, ref firstIteration);
  1433. string expected = string.Empty;
  1434. switch (width) {
  1435. case 1:
  1436. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1437. expected = @"
  1438. │";
  1439. break;
  1440. case 2:
  1441. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1442. expected = @"
  1443. ┌┐
  1444. ││
  1445. ││
  1446. ││
  1447. ││
  1448. ││
  1449. └┘";
  1450. break;
  1451. case 3:
  1452. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1453. expected = @"
  1454. ┌─┐
  1455. │ │
  1456. │ │
  1457. │ │
  1458. │ │
  1459. │ │
  1460. └─┘";
  1461. break;
  1462. case 4:
  1463. Assert.Equal (new Rect (0, 0, 1, 4), subview.Frame);
  1464. expected = @"
  1465. ┌──┐
  1466. ││ │
  1467. ││ │
  1468. ││ │
  1469. ││ │
  1470. │ │
  1471. └──┘";
  1472. break;
  1473. case 5:
  1474. Assert.Equal (new Rect (0, 0, 2, 4), subview.Frame);
  1475. expected = @"
  1476. ┌───┐
  1477. │┌┐ │
  1478. │││ │
  1479. │││ │
  1480. │└┘ │
  1481. │ │
  1482. └───┘";
  1483. break;
  1484. case 6:
  1485. Assert.Equal (new Rect (0, 0, 3, 4), subview.Frame);
  1486. expected = @"
  1487. ┌────┐
  1488. │┌─┐ │
  1489. ││ │ │
  1490. ││ │ │
  1491. │└─┘ │
  1492. │ │
  1493. └────┘";
  1494. break;
  1495. case 7:
  1496. Assert.Equal (new Rect (0, 0, 4, 4), subview.Frame);
  1497. expected = @"
  1498. ┌─────┐
  1499. │┌──┐ │
  1500. ││ │ │
  1501. ││ │ │
  1502. │└──┘ │
  1503. │ │
  1504. └─────┘";
  1505. break;
  1506. case 8:
  1507. Assert.Equal (new Rect (0, 0, 5, 4), subview.Frame);
  1508. expected = @"
  1509. ┌──────┐
  1510. │┌───┐ │
  1511. ││ │ │
  1512. ││ │ │
  1513. │└───┘ │
  1514. │ │
  1515. └──────┘";
  1516. break;
  1517. case 9:
  1518. Assert.Equal (new Rect (1, 0, 5, 4), subview.Frame);
  1519. expected = @"
  1520. ┌───────┐
  1521. │ ┌───┐ │
  1522. │ │ │ │
  1523. │ │ │ │
  1524. │ └───┘ │
  1525. │ │
  1526. └───────┘";
  1527. break;
  1528. case 10:
  1529. Assert.Equal (new Rect (1, 0, 6, 4), subview.Frame);
  1530. expected = @"
  1531. ┌────────┐
  1532. │ ┌────┐ │
  1533. │ │ │ │
  1534. │ │ │ │
  1535. │ └────┘ │
  1536. │ │
  1537. └────────┘";
  1538. break;
  1539. }
  1540. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1541. Application.End (rs);
  1542. }
  1543. [Fact] [AutoInitShutdown]
  1544. public void PosCombine_DimCombine_View_With_SubViews ()
  1545. {
  1546. bool clicked = false;
  1547. var top = Application.Top;
  1548. var win1 = new Window () { Id = "win1", Width = 20, Height = 10 };
  1549. var label = new Label ("[ ok ]");
  1550. var win2 = new Window () { Id = "win2", Y = Pos.Bottom (label) + 1, Width = 10, Height = 3 };
  1551. var view1 = new View () { Id = "view1", Width = Dim.Fill (), Height = 1, CanFocus = true };
  1552. view1.MouseClick += (sender, e) => clicked = true;
  1553. var view2 = new View () { Id = "view2", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  1554. view1.Add (view2);
  1555. win2.Add (view1);
  1556. win1.Add (label, win2);
  1557. top.Add (win1);
  1558. var rs = Application.Begin (top);
  1559. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1560. ┌──────────────────┐
  1561. │[ ok ] │
  1562. │ │
  1563. │┌────────┐ │
  1564. ││ │ │
  1565. │└────────┘ │
  1566. │ │
  1567. │ │
  1568. │ │
  1569. └──────────────────┘", _output);
  1570. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1571. Assert.Equal (new Rect (0, 0, 6, 1), label.Frame);
  1572. Assert.Equal (new Rect (0, 0, 20, 10), win1.Frame);
  1573. Assert.Equal (new Rect (0, 2, 10, 3), win2.Frame);
  1574. Assert.Equal (new Rect (0, 0, 8, 1), view1.Frame);
  1575. Assert.Equal (new Rect (0, 0, 7, 1), view2.Frame);
  1576. var foundView = View.FindDeepestView (top, 9, 4, out int rx, out int ry);
  1577. Assert.Equal (foundView, view1);
  1578. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  1579. X = 9,
  1580. Y = 4,
  1581. Flags = MouseFlags.Button1Clicked
  1582. }));
  1583. Assert.True (clicked);
  1584. Application.End (rs);
  1585. }
  1586. [Fact] [TestRespondersDisposed]
  1587. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  1588. {
  1589. Application.Init (new FakeDriver ());
  1590. var top = Application.Top;
  1591. var view = new View ("view") {
  1592. Y = -2,
  1593. Height = 10,
  1594. TextDirection = TextDirection.TopBottom_LeftRight
  1595. };
  1596. top.Add (view);
  1597. Application.Iteration += (s, a) => {
  1598. Assert.Equal (-2, view.Y);
  1599. Application.RequestStop ();
  1600. };
  1601. try {
  1602. Application.Run ();
  1603. } catch (IndexOutOfRangeException ex) {
  1604. // After the fix this exception will not be caught.
  1605. Assert.IsType<IndexOutOfRangeException> (ex);
  1606. }
  1607. // Shutdown must be called to safely clean up Application if Init has been called
  1608. Application.Shutdown ();
  1609. }
  1610. [Fact] [TestRespondersDisposed]
  1611. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  1612. {
  1613. Application.Init (new FakeDriver ());
  1614. var top = Application.Top;
  1615. var view = new View ("view") { X = -2 };
  1616. top.Add (view);
  1617. Application.Iteration += (s, a) => {
  1618. Assert.Equal (-2, view.X);
  1619. Application.RequestStop ();
  1620. };
  1621. try {
  1622. Application.Run ();
  1623. } catch (IndexOutOfRangeException ex) {
  1624. // After the fix this exception will not be caught.
  1625. Assert.IsType<IndexOutOfRangeException> (ex);
  1626. }
  1627. // Shutdown must be called to safely clean up Application if Init has been called
  1628. Application.Shutdown ();
  1629. }
  1630. }