LayoutTests.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. Assert.Equal (79, v.Frame.Width);
  166. }
  167. [Fact]
  168. public void GetCurrentHeight_TrySetHeight ()
  169. {
  170. var top = new View () {
  171. X = 0,
  172. Y = 0,
  173. Height = 20
  174. };
  175. var v = new View () {
  176. Height = Dim.Fill ()
  177. };
  178. top.Add (v);
  179. top.LayoutSubviews ();
  180. Assert.False (v.AutoSize);
  181. Assert.True (v.TrySetHeight (0, out _));
  182. Assert.Equal (20, v.Frame.Height);
  183. v.Height = Dim.Fill (1);
  184. top.LayoutSubviews ();
  185. Assert.True (v.TrySetHeight (0, out _));
  186. Assert.Equal (19, v.Frame.Height);
  187. v.AutoSize = true;
  188. top.LayoutSubviews ();
  189. Assert.True (v.TrySetHeight (0, out _));
  190. Assert.Equal (19, v.Frame.Height);
  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. Text = text
  442. };
  443. var verticalView = new View () {
  444. Y = 3,
  445. Height = 20,
  446. Text = text,
  447. TextDirection = TextDirection.TopBottom_LeftRight
  448. };
  449. var win = new Window () {
  450. Width = Dim.Fill (),
  451. Height = Dim.Fill (),
  452. Text = "Window"
  453. };
  454. win.Add (horizontalView, verticalView);
  455. Application.Top.Add (win);
  456. Application.Begin (Application.Top);
  457. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  458. Assert.False (horizontalView.AutoSize);
  459. Assert.False (verticalView.AutoSize);
  460. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  461. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  462. var expected = @"
  463. ┌──────────────────────────────┐
  464. │First line Second li │
  465. │ │
  466. │ │
  467. │F │
  468. │i │
  469. │r │
  470. │s │
  471. │t │
  472. │ │
  473. │l │
  474. │i │
  475. │n │
  476. │e │
  477. │ │
  478. │S │
  479. │e │
  480. │c │
  481. │o │
  482. │n │
  483. │d │
  484. │ │
  485. │l │
  486. │i │
  487. │ │
  488. │ │
  489. │ │
  490. │ │
  491. │ │
  492. │ │
  493. │ │
  494. └──────────────────────────────┘
  495. ";
  496. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  497. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  498. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  499. Application.Top.Redraw (Application.Top.Bounds);
  500. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  501. expected = @"
  502. ┌──────────────────────────────┐
  503. │First line Second li │
  504. │ │
  505. │ │
  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. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  536. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  537. }
  538. [Fact, AutoInitShutdown]
  539. public void TextDirection_Toggle ()
  540. {
  541. var view = new View ();
  542. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  543. win.Add (view);
  544. Application.Top.Add (win);
  545. Application.Begin (Application.Top);
  546. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  547. Assert.False (view.AutoSize);
  548. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  549. Assert.Equal (Rect.Empty, view.Frame);
  550. Assert.Equal ("Absolute(0)", view.X.ToString ());
  551. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  552. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  553. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  554. var expected = @"
  555. ┌────────────────────┐
  556. │ │
  557. │ │
  558. │ │
  559. │ │
  560. │ │
  561. │ │
  562. │ │
  563. │ │
  564. │ │
  565. │ │
  566. │ │
  567. │ │
  568. │ │
  569. │ │
  570. │ │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ │
  576. └────────────────────┘
  577. ";
  578. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  579. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  580. view.Text = "Hello World";
  581. view.Width = 11;
  582. Application.Refresh ();
  583. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  584. Assert.Equal ("Absolute(0)", view.X.ToString ());
  585. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  586. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  587. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  588. expected = @"
  589. ┌────────────────────┐
  590. │Hello World │
  591. │ │
  592. │ │
  593. │ │
  594. │ │
  595. │ │
  596. │ │
  597. │ │
  598. │ │
  599. │ │
  600. │ │
  601. │ │
  602. │ │
  603. │ │
  604. │ │
  605. │ │
  606. │ │
  607. │ │
  608. │ │
  609. │ │
  610. └────────────────────┘
  611. ";
  612. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  613. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  614. view.AutoSize = true;
  615. view.Text = "Hello Worlds";
  616. Application.Refresh ();
  617. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  618. Assert.Equal ("Absolute(0)", view.X.ToString ());
  619. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  620. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  621. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  622. expected = @"
  623. ┌────────────────────┐
  624. │Hello Worlds │
  625. │ │
  626. │ │
  627. │ │
  628. │ │
  629. │ │
  630. │ │
  631. │ │
  632. │ │
  633. │ │
  634. │ │
  635. │ │
  636. │ │
  637. │ │
  638. │ │
  639. │ │
  640. │ │
  641. │ │
  642. │ │
  643. │ │
  644. └────────────────────┘
  645. ";
  646. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  647. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  648. view.TextDirection = TextDirection.TopBottom_LeftRight;
  649. Application.Refresh ();
  650. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  651. Assert.Equal ("Absolute(0)", view.X.ToString ());
  652. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  653. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  654. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  655. expected = @"
  656. ┌────────────────────┐
  657. │H │
  658. │e │
  659. │l │
  660. │l │
  661. │o │
  662. │ │
  663. │W │
  664. │o │
  665. │r │
  666. │l │
  667. │d │
  668. │s │
  669. │ │
  670. │ │
  671. │ │
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. └────────────────────┘
  678. ";
  679. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  680. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  681. view.AutoSize = false;
  682. view.Height = 1;
  683. Application.Refresh ();
  684. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  685. Assert.Equal ("Absolute(0)", view.X.ToString ());
  686. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  687. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  688. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  689. expected = @"
  690. ┌────────────────────┐
  691. │HelloWorlds │
  692. │ │
  693. │ │
  694. │ │
  695. │ │
  696. │ │
  697. │ │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. │ │
  705. │ │
  706. │ │
  707. │ │
  708. │ │
  709. │ │
  710. │ │
  711. └────────────────────┘
  712. ";
  713. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  714. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  715. view.PreserveTrailingSpaces = true;
  716. Application.Refresh ();
  717. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  718. Assert.Equal ("Absolute(0)", view.X.ToString ());
  719. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  720. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  721. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  722. expected = @"
  723. ┌────────────────────┐
  724. │Hello World │
  725. │ │
  726. │ │
  727. │ │
  728. │ │
  729. │ │
  730. │ │
  731. │ │
  732. │ │
  733. │ │
  734. │ │
  735. │ │
  736. │ │
  737. │ │
  738. │ │
  739. │ │
  740. │ │
  741. │ │
  742. │ │
  743. │ │
  744. └────────────────────┘
  745. ";
  746. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  747. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  748. view.PreserveTrailingSpaces = false;
  749. var f = view.Frame;
  750. view.Width = f.Height;
  751. view.Height = f.Width;
  752. view.TextDirection = TextDirection.TopBottom_LeftRight;
  753. Application.Refresh ();
  754. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  755. Assert.Equal ("Absolute(0)", view.X.ToString ());
  756. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  757. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  758. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  759. expected = @"
  760. ┌────────────────────┐
  761. │H │
  762. │e │
  763. │l │
  764. │l │
  765. │o │
  766. │ │
  767. │W │
  768. │o │
  769. │r │
  770. │l │
  771. │d │
  772. │ │
  773. │ │
  774. │ │
  775. │ │
  776. │ │
  777. │ │
  778. │ │
  779. │ │
  780. │ │
  781. └────────────────────┘
  782. ";
  783. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  784. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  785. view.AutoSize = true;
  786. Application.Refresh ();
  787. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  788. Assert.Equal ("Absolute(0)", view.X.ToString ());
  789. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  790. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  791. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  792. expected = @"
  793. ┌────────────────────┐
  794. │H │
  795. │e │
  796. │l │
  797. │l │
  798. │o │
  799. │ │
  800. │W │
  801. │o │
  802. │r │
  803. │l │
  804. │d │
  805. │s │
  806. │ │
  807. │ │
  808. │ │
  809. │ │
  810. │ │
  811. │ │
  812. │ │
  813. │ │
  814. └────────────────────┘
  815. ";
  816. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  817. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  818. }
  819. [Fact, AutoInitShutdown]
  820. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  821. {
  822. var text = $"Fi_nish 終";
  823. var horizontalView = new View () {
  824. AutoSize = true,
  825. HotKeySpecifier = '_',
  826. Text = text
  827. };
  828. var verticalView = new View () {
  829. Y = 3,
  830. AutoSize = true,
  831. HotKeySpecifier = '_',
  832. Text = text,
  833. TextDirection = TextDirection.TopBottom_LeftRight
  834. };
  835. var win = new Window () {
  836. Width = Dim.Fill (),
  837. Height = Dim.Fill (),
  838. Text = "Window"
  839. };
  840. win.Add (horizontalView, verticalView);
  841. Application.Top.Add (win);
  842. Application.Begin (Application.Top);
  843. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  844. Assert.True (horizontalView.AutoSize);
  845. Assert.True (verticalView.AutoSize);
  846. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  847. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  848. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  849. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  850. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  851. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  852. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  853. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  854. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  855. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  856. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  857. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  858. var expected = @"
  859. ┌────────────────────┐
  860. │Finish 終 │
  861. │ │
  862. │ │
  863. │F │
  864. │i │
  865. │n │
  866. │i │
  867. │s │
  868. │h │
  869. │ │
  870. │終 │
  871. │ │
  872. │ │
  873. │ │
  874. │ │
  875. │ │
  876. │ │
  877. │ │
  878. │ │
  879. │ │
  880. └────────────────────┘
  881. ";
  882. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  883. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  884. verticalView.Text = $"最初_の行二行目";
  885. Application.Top.Redraw (Application.Top.Bounds);
  886. Assert.True (horizontalView.AutoSize);
  887. Assert.True (verticalView.AutoSize);
  888. // height was initialized with 8 and is kept as minimum
  889. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  890. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  891. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  892. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  893. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  894. expected = @"
  895. ┌────────────────────┐
  896. │Finish 終 │
  897. │ │
  898. │ │
  899. │最 │
  900. │初 │
  901. │の │
  902. │行 │
  903. │二 │
  904. │行 │
  905. │目 │
  906. │ │
  907. │ │
  908. │ │
  909. │ │
  910. │ │
  911. │ │
  912. │ │
  913. │ │
  914. │ │
  915. │ │
  916. └────────────────────┘
  917. ";
  918. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  919. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  920. }
  921. [Fact, AutoInitShutdown]
  922. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  923. {
  924. var btn = new Button () {
  925. X = Pos.Center (),
  926. Y = Pos.Center (),
  927. Text = "Say He_llo 你"
  928. };
  929. var win = new Window () {
  930. Width = Dim.Fill (),
  931. Height = Dim.Fill (),
  932. Title = "Test Demo 你"
  933. };
  934. win.Add (btn);
  935. Application.Top.Add (win);
  936. Assert.True (btn.AutoSize);
  937. Application.Begin (Application.Top);
  938. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  939. var expected = @"
  940. ┌┤Test Demo 你├──────────────┐
  941. │ │
  942. │ [ Say Hello 你 ] │
  943. │ │
  944. └────────────────────────────┘
  945. ";
  946. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  947. Assert.True (btn.AutoSize);
  948. btn.Text = "Say He_llo 你 changed";
  949. Assert.True (btn.AutoSize);
  950. Application.Refresh ();
  951. expected = @"
  952. ┌┤Test Demo 你├──────────────┐
  953. │ │
  954. │ [ Say Hello 你 changed ] │
  955. │ │
  956. └────────────────────────────┘
  957. ";
  958. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  959. }
  960. [Fact, AutoInitShutdown]
  961. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  962. {
  963. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  964. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  965. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  966. var view4 = new View () {
  967. Text = "Say Hello view4 你",
  968. AutoSize = false,
  969. Width = 10,
  970. Height = 5,
  971. TextDirection = TextDirection.TopBottom_LeftRight
  972. };
  973. var view5 = new View () {
  974. Text = "Say Hello view5 你",
  975. Width = 10,
  976. Height = 5,
  977. AutoSize = false,
  978. TextDirection = TextDirection.TopBottom_LeftRight
  979. };
  980. var view6 = new View () {
  981. AutoSize = false,
  982. Width = 10,
  983. Height = 5,
  984. TextDirection = TextDirection.TopBottom_LeftRight,
  985. Text = "Say Hello view6 你",
  986. };
  987. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  988. Assert.False (view1.IsInitialized);
  989. Assert.False (view2.IsInitialized);
  990. Assert.False (view3.IsInitialized);
  991. Assert.False (view4.IsInitialized);
  992. Assert.False (view5.IsInitialized);
  993. Assert.False (view1.AutoSize);
  994. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  995. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  996. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  997. Assert.False (view2.AutoSize);
  998. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  999. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1000. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1001. Assert.False (view3.AutoSize);
  1002. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1003. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1004. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1005. Assert.False (view4.AutoSize);
  1006. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1007. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1008. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1009. Assert.False (view5.AutoSize);
  1010. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1011. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1012. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1013. Assert.False (view6.AutoSize);
  1014. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1015. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1016. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1017. Application.Begin (Application.Top);
  1018. Assert.True (view1.IsInitialized);
  1019. Assert.True (view2.IsInitialized);
  1020. Assert.True (view3.IsInitialized);
  1021. Assert.True (view4.IsInitialized);
  1022. Assert.True (view5.IsInitialized);
  1023. Assert.False (view1.AutoSize);
  1024. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1025. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1026. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1027. Assert.False (view2.AutoSize);
  1028. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1029. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1030. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1031. Assert.False (view3.AutoSize);
  1032. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1033. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1034. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1035. Assert.False (view4.AutoSize);
  1036. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1037. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1038. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1039. Assert.False (view5.AutoSize);
  1040. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1041. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1042. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1043. Assert.False (view6.AutoSize);
  1044. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1045. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1046. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1047. }
  1048. [Fact, AutoInitShutdown]
  1049. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  1050. {
  1051. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  1052. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  1053. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1054. var view4 = new View () {
  1055. Text = "Say Hello view4 你",
  1056. AutoSize = true,
  1057. Width = 10,
  1058. Height = 5,
  1059. TextDirection = TextDirection.TopBottom_LeftRight
  1060. };
  1061. var view5 = new View () {
  1062. Text = "Say Hello view5 你",
  1063. Width = 10,
  1064. Height = 5,
  1065. AutoSize = true,
  1066. TextDirection = TextDirection.TopBottom_LeftRight
  1067. };
  1068. var view6 = new View () {
  1069. AutoSize = true,
  1070. Width = 10,
  1071. Height = 5,
  1072. TextDirection = TextDirection.TopBottom_LeftRight,
  1073. Text = "Say Hello view6 你",
  1074. };
  1075. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1076. Assert.False (view1.IsInitialized);
  1077. Assert.False (view2.IsInitialized);
  1078. Assert.False (view3.IsInitialized);
  1079. Assert.False (view4.IsInitialized);
  1080. Assert.False (view5.IsInitialized);
  1081. Assert.True (view1.AutoSize);
  1082. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1083. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1084. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1085. Assert.True (view2.AutoSize);
  1086. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1087. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1088. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1089. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1090. //Assert.True (view3.AutoSize);
  1091. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1092. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1093. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1094. //Assert.True (view4.AutoSize);
  1095. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1096. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1097. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1098. //Assert.True (view5.AutoSize);
  1099. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1100. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1101. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1102. //Assert.True (view6.AutoSize);
  1103. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1104. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1105. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1106. Application.Begin (Application.Top);
  1107. Assert.True (view1.IsInitialized);
  1108. Assert.True (view2.IsInitialized);
  1109. Assert.True (view3.IsInitialized);
  1110. Assert.True (view4.IsInitialized);
  1111. Assert.True (view5.IsInitialized);
  1112. Assert.True (view1.AutoSize);
  1113. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1114. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1115. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1116. Assert.True (view2.AutoSize);
  1117. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1118. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1119. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1120. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1121. //Assert.True (view3.AutoSize);
  1122. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1123. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1124. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1125. //Assert.True (view4.AutoSize);
  1126. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1127. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1128. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1129. //Assert.True (view5.AutoSize);
  1130. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1131. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1132. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1133. //Assert.True (view6.AutoSize);
  1134. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1135. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1136. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1137. }
  1138. [Fact, AutoInitShutdown]
  1139. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1140. {
  1141. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1142. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1143. Text = "Say Hello view2 你",
  1144. AutoSize = true,
  1145. TextDirection = TextDirection.TopBottom_LeftRight
  1146. };
  1147. Application.Top.Add (view1, view2);
  1148. var rs = Application.Begin (Application.Top);
  1149. Assert.True (view1.AutoSize);
  1150. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1151. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1152. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1153. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1154. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1155. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1156. Assert.True (view2.AutoSize);
  1157. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1158. Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1159. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1160. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1161. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1162. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1163. view1.Frame = new Rect (0, 0, 25, 4);
  1164. bool firstIteration = false;
  1165. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1166. Assert.True (view1.AutoSize);
  1167. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1168. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1169. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1170. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1171. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1172. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1173. view2.Frame = new Rect (0, 0, 1, 25);
  1174. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1175. Assert.True (view2.AutoSize);
  1176. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1177. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1178. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1179. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1180. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1181. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1182. }
  1183. [Fact, AutoInitShutdown]
  1184. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1185. {
  1186. var top = Application.Top;
  1187. var view1 = new View ();
  1188. Assert.False (view1.IsAdded);
  1189. Assert.Null (view1.X);
  1190. Assert.Null (view1.Y);
  1191. Assert.Null (view1.Width);
  1192. Assert.Null (view1.Height);
  1193. top.Add (view1);
  1194. Assert.True (view1.IsAdded);
  1195. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1196. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1197. Assert.Equal ("Absolute(0)", view1.Width.ToString ());
  1198. Assert.Equal ("Absolute(0)", view1.Height.ToString ());
  1199. var view2 = new View () {
  1200. X = Pos.Center (),
  1201. Y = Pos.Center (),
  1202. Width = Dim.Fill (),
  1203. Height = Dim.Fill ()
  1204. };
  1205. Assert.False (view2.IsAdded);
  1206. Assert.Equal ("Center", view2.X.ToString ());
  1207. Assert.Equal ("Center", view2.Y.ToString ());
  1208. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1209. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1210. top.Add (view2);
  1211. Assert.True (view2.IsAdded);
  1212. Assert.Equal ("Center", view2.X.ToString ());
  1213. Assert.Equal ("Center", view2.Y.ToString ());
  1214. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1215. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1216. }
  1217. [Fact]
  1218. public void SetRelativeLayout_PosCombine_Center_Plus_Absolute ()
  1219. {
  1220. var superView = new View () {
  1221. AutoSize = false,
  1222. Width = 10,
  1223. Height = 10
  1224. };
  1225. var testView = new View () {
  1226. AutoSize = false,
  1227. X = Pos.Center (),
  1228. Y = Pos.Center (),
  1229. Width = 1,
  1230. Height = 1
  1231. };
  1232. superView.Add (testView);
  1233. testView.SetRelativeLayout (superView.Frame);
  1234. Assert.Equal (4, testView.Frame.X);
  1235. Assert.Equal (4, testView.Frame.Y);
  1236. testView = new View () {
  1237. AutoSize = false,
  1238. X = Pos.Center () + 1,
  1239. Y = Pos.Center () + 1,
  1240. Width = 1,
  1241. Height = 1
  1242. };
  1243. superView.Add (testView);
  1244. testView.SetRelativeLayout (superView.Frame);
  1245. Assert.Equal (5, testView.Frame.X);
  1246. Assert.Equal (5, testView.Frame.Y);
  1247. testView = new View () {
  1248. AutoSize = false,
  1249. X = 1 + Pos.Center (),
  1250. Y = 1 + Pos.Center (),
  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.Percent (50),
  1261. Y = Pos.Percent (50) + 1,
  1262. Width = 1,
  1263. Height = 1
  1264. };
  1265. superView.Add (testView);
  1266. testView.SetRelativeLayout (superView.Frame);
  1267. Assert.Equal (6, testView.Frame.X);
  1268. Assert.Equal (6, testView.Frame.Y);
  1269. testView = new View () {
  1270. AutoSize = false,
  1271. X = Pos.Percent (10) + Pos.Percent (40),
  1272. Y = Pos.Percent (10) + Pos.Percent (40),
  1273. Width = 1,
  1274. Height = 1
  1275. };
  1276. superView.Add (testView);
  1277. testView.SetRelativeLayout (superView.Frame);
  1278. Assert.Equal (5, testView.Frame.X);
  1279. Assert.Equal (5, testView.Frame.Y);
  1280. testView = new View () {
  1281. AutoSize = false,
  1282. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  1283. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  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 = Pos.Left(testView),
  1294. Y = Pos.Left (testView),
  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 = 1 + Pos.Left (testView),
  1305. Y = Pos.Top (testView) + 1,
  1306. Width = 1,
  1307. Height = 1
  1308. };
  1309. superView.Add (testView);
  1310. testView.SetRelativeLayout (superView.Frame);
  1311. Assert.Equal (6, testView.Frame.X);
  1312. Assert.Equal (6, testView.Frame.Y);
  1313. }
  1314. }
  1315. }