LayoutTests.cs 54 KB

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