LayoutTests.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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_Does_Not_Throws_Missing_Add ()
  32. {
  33. var root = new View ();
  34. var sub1 = new View ();
  35. var sub2 = new View ();
  36. sub1.Add (sub2);
  37. root.Add (sub1);
  38. sub2.Width = Dim.Width (root);
  39. var exception = Record.Exception (root.LayoutSubviews);
  40. Assert.Null (exception);
  41. }
  42. [Fact]
  43. public void TopologicalSort_Recursive_Ref ()
  44. {
  45. var root = new View ();
  46. var sub1 = new View ();
  47. root.Add (sub1);
  48. var sub2 = new View ();
  49. root.Add (sub2);
  50. sub2.Width = Dim.Width (sub2);
  51. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  52. }
  53. [Fact, AutoInitShutdown]
  54. public void SetWidth_CanSetWidth_ForceValidatePosDim ()
  55. {
  56. var top = new View () {
  57. X = 0,
  58. Y = 0,
  59. Width = 80,
  60. };
  61. var v = new View () {
  62. Width = Dim.Fill (),
  63. ForceValidatePosDim = true
  64. };
  65. top.Add (v);
  66. Assert.False (v.SetWidth (70, out int rWidth));
  67. Assert.Equal (70, rWidth);
  68. v.Width = Dim.Fill (1);
  69. Assert.False (v.SetWidth (70, out rWidth));
  70. Assert.Equal (69, rWidth);
  71. v.Width = null;
  72. Assert.True (v.SetWidth (70, out rWidth));
  73. Assert.Equal (70, rWidth);
  74. Assert.False (v.IsInitialized);
  75. Application.Top.Add (top);
  76. Application.Begin (Application.Top);
  77. Assert.True (v.IsInitialized);
  78. v.Width = Dim.Fill (1);
  79. Assert.Throws<ArgumentException> (() => v.Width = 75);
  80. v.LayoutStyle = LayoutStyle.Absolute;
  81. v.Width = 75;
  82. Assert.True (v.SetWidth (60, out rWidth));
  83. Assert.Equal (60, rWidth);
  84. }
  85. [Fact, AutoInitShutdown]
  86. public void SetHeight_CanSetHeight_ForceValidatePosDim ()
  87. {
  88. var top = new View () {
  89. X = 0,
  90. Y = 0,
  91. Height = 20
  92. };
  93. var v = new View () {
  94. Height = Dim.Fill (),
  95. ForceValidatePosDim = true
  96. };
  97. top.Add (v);
  98. Assert.False (v.SetHeight (10, out int rHeight));
  99. Assert.Equal (10, rHeight);
  100. v.Height = Dim.Fill (1);
  101. Assert.False (v.SetHeight (10, out rHeight));
  102. Assert.Equal (9, rHeight);
  103. v.Height = null;
  104. Assert.True (v.SetHeight (10, out rHeight));
  105. Assert.Equal (10, rHeight);
  106. Assert.False (v.IsInitialized);
  107. Application.Top.Add (top);
  108. Application.Begin (Application.Top);
  109. Assert.True (v.IsInitialized);
  110. v.Height = Dim.Fill (1);
  111. Assert.Throws<ArgumentException> (() => v.Height = 15);
  112. v.LayoutStyle = LayoutStyle.Absolute;
  113. v.Height = 15;
  114. Assert.True (v.SetHeight (5, out rHeight));
  115. Assert.Equal (5, rHeight);
  116. }
  117. [Fact]
  118. public void GetCurrentWidth_CanSetWidth ()
  119. {
  120. var top = new View () {
  121. X = 0,
  122. Y = 0,
  123. Width = 80,
  124. };
  125. var v = new View () {
  126. Width = Dim.Fill ()
  127. };
  128. top.Add (v);
  129. Assert.False (v.AutoSize);
  130. Assert.True (v.GetCurrentWidth (out int cWidth));
  131. Assert.Equal (80, cWidth);
  132. v.Width = Dim.Fill (1);
  133. Assert.True (v.GetCurrentWidth (out cWidth));
  134. Assert.Equal (79, cWidth);
  135. v.AutoSize = true;
  136. Assert.True (v.GetCurrentWidth (out cWidth));
  137. Assert.Equal (79, cWidth);
  138. }
  139. [Fact]
  140. public void GetCurrentHeight_CanSetHeight ()
  141. {
  142. var top = new View () {
  143. X = 0,
  144. Y = 0,
  145. Height = 20
  146. };
  147. var v = new View () {
  148. Height = Dim.Fill ()
  149. };
  150. top.Add (v);
  151. Assert.False (v.AutoSize);
  152. Assert.True (v.GetCurrentHeight (out int cHeight));
  153. Assert.Equal (20, cHeight);
  154. v.Height = Dim.Fill (1);
  155. Assert.True (v.GetCurrentHeight (out cHeight));
  156. Assert.Equal (19, cHeight);
  157. v.AutoSize = true;
  158. Assert.True (v.GetCurrentHeight (out cHeight));
  159. Assert.Equal (19, cHeight);
  160. }
  161. [Fact]
  162. public void AutoSize_False_If_Text_Emmpty ()
  163. {
  164. var view1 = new View ();
  165. var view2 = new View ("");
  166. var view3 = new View () { Text = "" };
  167. Assert.False (view1.AutoSize);
  168. Assert.False (view2.AutoSize);
  169. Assert.False (view3.AutoSize);
  170. }
  171. [Fact]
  172. public void AutoSize_False_If_Text_Is_Not_Emmpty ()
  173. {
  174. var view1 = new View ();
  175. view1.Text = "Hello World";
  176. var view2 = new View ("Hello World");
  177. var view3 = new View () { Text = "Hello World" };
  178. Assert.False (view1.AutoSize);
  179. Assert.False (view2.AutoSize);
  180. Assert.False (view3.AutoSize);
  181. }
  182. [Fact]
  183. public void AutoSize_True_Label_If_Text_Emmpty ()
  184. {
  185. var label1 = new Label ();
  186. var label2 = new Label ("");
  187. var label3 = new Label () { Text = "" };
  188. Assert.True (label1.AutoSize);
  189. Assert.True (label2.AutoSize);
  190. Assert.True (label3.AutoSize);
  191. }
  192. [Fact]
  193. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  194. {
  195. var label1 = new Label ();
  196. label1.Text = "Hello World";
  197. var label2 = new Label ("Hello World");
  198. var label3 = new Label () { Text = "Hello World" };
  199. Assert.True (label1.AutoSize);
  200. Assert.True (label2.AutoSize);
  201. Assert.True (label3.AutoSize);
  202. }
  203. [Fact]
  204. public void AutoSize_False_ResizeView_Is_Always_False ()
  205. {
  206. var label = new Label () { AutoSize = false };
  207. label.Text = "New text";
  208. Assert.False (label.AutoSize);
  209. Assert.Equal ("{X=0,Y=0,Width=0,Height=1}", label.Bounds.ToString ());
  210. }
  211. [Fact]
  212. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  213. {
  214. var label = new Label ();
  215. label.Text = "New text";
  216. Assert.True (label.AutoSize);
  217. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  218. }
  219. [Fact, AutoInitShutdown]
  220. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  221. {
  222. var win = new Window (new Rect (0, 0, 30, 80), "");
  223. var label = new Label () { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  224. win.Add (label);
  225. Application.Top.Add (win);
  226. // Text is empty so height=0
  227. Assert.False (label.AutoSize);
  228. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  229. label.Text = "New text\nNew line";
  230. Application.Top.LayoutSubviews ();
  231. Assert.False (label.AutoSize);
  232. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  233. Assert.False (label.IsInitialized);
  234. Application.Begin (Application.Top);
  235. Assert.True (label.IsInitialized);
  236. Assert.False (label.AutoSize);
  237. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  238. }
  239. [Fact, AutoInitShutdown]
  240. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  241. {
  242. var win = new Window (new Rect (0, 0, 30, 80), "");
  243. var label = new Label () { Width = Dim.Fill () };
  244. win.Add (label);
  245. Application.Top.Add (win);
  246. Assert.True (label.IsAdded);
  247. // Text is empty so height=0
  248. Assert.True (label.AutoSize);
  249. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  250. label.Text = "First line\nSecond line";
  251. Application.Top.LayoutSubviews ();
  252. Assert.True (label.AutoSize);
  253. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  254. Assert.False (label.IsInitialized);
  255. Application.Begin (Application.Top);
  256. Assert.True (label.AutoSize);
  257. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  258. Assert.True (label.IsInitialized);
  259. label.AutoSize = false;
  260. Application.Refresh ();
  261. Assert.False (label.AutoSize);
  262. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  263. }
  264. [Fact, AutoInitShutdown]
  265. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  266. {
  267. var win = new Window (new Rect (0, 0, 30, 80), "");
  268. var label = new Label () { Width = Dim.Fill () };
  269. win.Add (label);
  270. Application.Top.Add (win);
  271. // Text is empty so height=0
  272. Assert.True (label.AutoSize);
  273. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  274. Application.Begin (Application.Top);
  275. Assert.True (label.AutoSize);
  276. Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
  277. label.Text = "First line\nSecond line";
  278. Application.Refresh ();
  279. // Here the AutoSize ensuring the right size
  280. Assert.True (label.AutoSize);
  281. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  282. label.AutoSize = false;
  283. Application.Refresh ();
  284. // Here the SetMinWidthHeight ensuring the minimum height
  285. Assert.False (label.AutoSize);
  286. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  287. label.Text = "First changed line\nSecond changed line\nNew line";
  288. Application.Refresh ();
  289. Assert.False (label.AutoSize);
  290. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  291. label.AutoSize = true;
  292. Application.Refresh ();
  293. Assert.True (label.AutoSize);
  294. Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
  295. }
  296. [Fact, AutoInitShutdown]
  297. public void AutoSize_True_Setting_With_Height_Horizontal ()
  298. {
  299. var label = new Label ("Hello") { Width = 10, Height = 2 };
  300. var viewX = new View ("X") { X = Pos.Right (label) };
  301. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  302. Application.Top.Add (label, viewX, viewY);
  303. Application.Begin (Application.Top);
  304. Assert.True (label.AutoSize);
  305. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  306. var expected = @"
  307. Hello X
  308. Y
  309. ";
  310. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  311. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  312. label.AutoSize = false;
  313. Application.Refresh ();
  314. Assert.False (label.AutoSize);
  315. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  316. expected = @"
  317. Hello X
  318. Y
  319. ";
  320. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  321. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  322. }
  323. [Fact, AutoInitShutdown]
  324. public void AutoSize_True_Setting_With_Height_Vertical ()
  325. {
  326. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  327. var viewX = new View ("X") { X = Pos.Right (label) };
  328. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  329. Application.Top.Add (label, viewX, viewY);
  330. Application.Begin (Application.Top);
  331. Assert.True (label.AutoSize);
  332. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  333. var expected = @"
  334. H X
  335. e
  336. l
  337. l
  338. o
  339. Y
  340. ";
  341. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  342. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  343. label.AutoSize = false;
  344. Application.Refresh ();
  345. Assert.False (label.AutoSize);
  346. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  347. expected = @"
  348. H X
  349. e
  350. l
  351. l
  352. o
  353. Y
  354. ";
  355. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  356. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  357. }
  358. [Fact]
  359. [AutoInitShutdown]
  360. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  361. {
  362. var lbl = new Label ("123");
  363. Application.Top.Add (lbl);
  364. Application.Begin (Application.Top);
  365. Assert.True (lbl.AutoSize);
  366. Assert.Equal ("123 ", GetContents ());
  367. lbl.Text = "12";
  368. lbl.SuperView.Redraw (lbl.SuperView.NeedDisplay);
  369. Assert.Equal ("12 ", GetContents ());
  370. string GetContents ()
  371. {
  372. var text = "";
  373. for (int i = 0; i < 4; i++) {
  374. text += (char)Application.Driver.Contents [0, i, 0];
  375. }
  376. return text;
  377. }
  378. }
  379. [Fact, AutoInitShutdown]
  380. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  381. {
  382. var text = $"First line{Environment.NewLine}Second line";
  383. var horizontalView = new View () {
  384. Width = 20,
  385. Text = text
  386. };
  387. var verticalView = new View () {
  388. Y = 3,
  389. Height = 20,
  390. Text = text,
  391. TextDirection = TextDirection.TopBottom_LeftRight
  392. };
  393. var win = new Window () {
  394. Width = Dim.Fill (),
  395. Height = Dim.Fill (),
  396. Text = "Window"
  397. };
  398. win.Add (horizontalView, verticalView);
  399. Application.Top.Add (win);
  400. Application.Begin (Application.Top);
  401. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  402. Assert.False (horizontalView.AutoSize);
  403. Assert.False (verticalView.AutoSize);
  404. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  405. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  406. var expected = @"
  407. ┌──────────────────────────────┐
  408. │First line Second li │
  409. │ │
  410. │ │
  411. │F │
  412. │i │
  413. │r │
  414. │s │
  415. │t │
  416. │ │
  417. │l │
  418. │i │
  419. │n │
  420. │e │
  421. │ │
  422. │S │
  423. │e │
  424. │c │
  425. │o │
  426. │n │
  427. │d │
  428. │ │
  429. │l │
  430. │i │
  431. │ │
  432. │ │
  433. │ │
  434. │ │
  435. │ │
  436. │ │
  437. │ │
  438. └──────────────────────────────┘
  439. ";
  440. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  441. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  442. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  443. Application.Top.Redraw (Application.Top.Bounds);
  444. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  445. expected = @"
  446. ┌──────────────────────────────┐
  447. │First line Second li │
  448. │ │
  449. │ │
  450. │最 │
  451. │初 │
  452. │の │
  453. │行 │
  454. │ │
  455. │二 │
  456. │行 │
  457. │目 │
  458. │ │
  459. │ │
  460. │ │
  461. │ │
  462. │ │
  463. │ │
  464. │ │
  465. │ │
  466. │ │
  467. │ │
  468. │ │
  469. │ │
  470. │ │
  471. │ │
  472. │ │
  473. │ │
  474. │ │
  475. │ │
  476. │ │
  477. └──────────────────────────────┘
  478. ";
  479. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  480. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  481. }
  482. [Fact, AutoInitShutdown]
  483. public void TextDirection_Toggle ()
  484. {
  485. var view = new View ();
  486. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  487. win.Add (view);
  488. Application.Top.Add (win);
  489. Application.Begin (Application.Top);
  490. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  491. Assert.False (view.AutoSize);
  492. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  493. Assert.Equal (Rect.Empty, view.Frame);
  494. Assert.Equal ("Absolute(0)", view.X.ToString ());
  495. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  496. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  497. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  498. var expected = @"
  499. ┌────────────────────┐
  500. │ │
  501. │ │
  502. │ │
  503. │ │
  504. │ │
  505. │ │
  506. │ │
  507. │ │
  508. │ │
  509. │ │
  510. │ │
  511. │ │
  512. │ │
  513. │ │
  514. │ │
  515. │ │
  516. │ │
  517. │ │
  518. │ │
  519. │ │
  520. └────────────────────┘
  521. ";
  522. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  523. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  524. view.Text = "Hello World";
  525. view.Width = 11;
  526. Application.Refresh ();
  527. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  528. Assert.Equal ("Absolute(0)", view.X.ToString ());
  529. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  530. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  531. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  532. expected = @"
  533. ┌────────────────────┐
  534. │Hello World │
  535. │ │
  536. │ │
  537. │ │
  538. │ │
  539. │ │
  540. │ │
  541. │ │
  542. │ │
  543. │ │
  544. │ │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. └────────────────────┘
  555. ";
  556. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  557. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  558. view.AutoSize = true;
  559. view.Text = "Hello Worlds";
  560. Application.Refresh ();
  561. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  562. Assert.Equal ("Absolute(0)", view.X.ToString ());
  563. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  564. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  565. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  566. expected = @"
  567. ┌────────────────────┐
  568. │Hello Worlds │
  569. │ │
  570. │ │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. │ │
  586. │ │
  587. │ │
  588. └────────────────────┘
  589. ";
  590. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  591. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  592. view.TextDirection = TextDirection.TopBottom_LeftRight;
  593. Application.Refresh ();
  594. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  595. Assert.Equal ("Absolute(0)", view.X.ToString ());
  596. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  597. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  598. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  599. expected = @"
  600. ┌────────────────────┐
  601. │H │
  602. │e │
  603. │l │
  604. │l │
  605. │o │
  606. │ │
  607. │W │
  608. │o │
  609. │r │
  610. │l │
  611. │d │
  612. │s │
  613. │ │
  614. │ │
  615. │ │
  616. │ │
  617. │ │
  618. │ │
  619. │ │
  620. │ │
  621. └────────────────────┘
  622. ";
  623. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  624. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  625. view.AutoSize = false;
  626. view.Height = 1;
  627. Application.Refresh ();
  628. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  629. Assert.Equal ("Absolute(0)", view.X.ToString ());
  630. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  631. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  632. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  633. expected = @"
  634. ┌────────────────────┐
  635. │HelloWorlds │
  636. │ │
  637. │ │
  638. │ │
  639. │ │
  640. │ │
  641. │ │
  642. │ │
  643. │ │
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ │
  649. │ │
  650. │ │
  651. │ │
  652. │ │
  653. │ │
  654. │ │
  655. └────────────────────┘
  656. ";
  657. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  658. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  659. view.PreserveTrailingSpaces = true;
  660. Application.Refresh ();
  661. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  662. Assert.Equal ("Absolute(0)", view.X.ToString ());
  663. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  664. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  665. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  666. expected = @"
  667. ┌────────────────────┐
  668. │Hello World │
  669. │ │
  670. │ │
  671. │ │
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. │ │
  684. │ │
  685. │ │
  686. │ │
  687. │ │
  688. └────────────────────┘
  689. ";
  690. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  691. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  692. view.PreserveTrailingSpaces = false;
  693. var f = view.Frame;
  694. view.Width = f.Height;
  695. view.Height = f.Width;
  696. view.TextDirection = TextDirection.TopBottom_LeftRight;
  697. Application.Refresh ();
  698. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  699. Assert.Equal ("Absolute(0)", view.X.ToString ());
  700. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  701. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  702. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  703. expected = @"
  704. ┌────────────────────┐
  705. │H │
  706. │e │
  707. │l │
  708. │l │
  709. │o │
  710. │ │
  711. │W │
  712. │o │
  713. │r │
  714. │l │
  715. │d │
  716. │ │
  717. │ │
  718. │ │
  719. │ │
  720. │ │
  721. │ │
  722. │ │
  723. │ │
  724. │ │
  725. └────────────────────┘
  726. ";
  727. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  728. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  729. view.AutoSize = true;
  730. Application.Refresh ();
  731. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  732. Assert.Equal ("Absolute(0)", view.X.ToString ());
  733. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  734. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  735. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  736. expected = @"
  737. ┌────────────────────┐
  738. │H │
  739. │e │
  740. │l │
  741. │l │
  742. │o │
  743. │ │
  744. │W │
  745. │o │
  746. │r │
  747. │l │
  748. │d │
  749. │s │
  750. │ │
  751. │ │
  752. │ │
  753. │ │
  754. │ │
  755. │ │
  756. │ │
  757. │ │
  758. └────────────────────┘
  759. ";
  760. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  761. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  762. }
  763. [Fact, AutoInitShutdown]
  764. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  765. {
  766. var text = $"Fi_nish 終";
  767. var horizontalView = new View () {
  768. AutoSize = true,
  769. HotKeySpecifier = '_',
  770. Text = text
  771. };
  772. var verticalView = new View () {
  773. Y = 3,
  774. AutoSize = true,
  775. HotKeySpecifier = '_',
  776. Text = text,
  777. TextDirection = TextDirection.TopBottom_LeftRight
  778. };
  779. var win = new Window () {
  780. Width = Dim.Fill (),
  781. Height = Dim.Fill (),
  782. Text = "Window"
  783. };
  784. win.Add (horizontalView, verticalView);
  785. Application.Top.Add (win);
  786. Application.Begin (Application.Top);
  787. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  788. Assert.True (horizontalView.AutoSize);
  789. Assert.True (verticalView.AutoSize);
  790. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  791. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  792. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  793. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  794. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  795. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  796. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  797. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  798. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  799. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  800. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  801. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  802. var expected = @"
  803. ┌────────────────────┐
  804. │Finish 終 │
  805. │ │
  806. │ │
  807. │F │
  808. │i │
  809. │n │
  810. │i │
  811. │s │
  812. │h │
  813. │ │
  814. │終 │
  815. │ │
  816. │ │
  817. │ │
  818. │ │
  819. │ │
  820. │ │
  821. │ │
  822. │ │
  823. │ │
  824. └────────────────────┘
  825. ";
  826. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  827. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  828. verticalView.Text = $"最初_の行二行目";
  829. Application.Top.Redraw (Application.Top.Bounds);
  830. Assert.True (horizontalView.AutoSize);
  831. Assert.True (verticalView.AutoSize);
  832. // height was initialized with 8 and is kept as minimum
  833. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  834. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  835. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  836. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  837. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  838. expected = @"
  839. ┌────────────────────┐
  840. │Finish 終 │
  841. │ │
  842. │ │
  843. │最 │
  844. │初 │
  845. │の │
  846. │行 │
  847. │二 │
  848. │行 │
  849. │目 │
  850. │ │
  851. │ │
  852. │ │
  853. │ │
  854. │ │
  855. │ │
  856. │ │
  857. │ │
  858. │ │
  859. │ │
  860. └────────────────────┘
  861. ";
  862. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  863. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  864. }
  865. [Fact, AutoInitShutdown]
  866. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  867. {
  868. var btn = new Button () {
  869. X = Pos.Center (),
  870. Y = Pos.Center (),
  871. Text = "Say He_llo 你"
  872. };
  873. var win = new Window () {
  874. Width = Dim.Fill (),
  875. Height = Dim.Fill (),
  876. Title = "Test Demo 你"
  877. };
  878. win.Add (btn);
  879. Application.Top.Add (win);
  880. Assert.True (btn.AutoSize);
  881. Application.Begin (Application.Top);
  882. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  883. var expected = @"
  884. ┌ Test Demo 你 ──────────────┐
  885. │ │
  886. │ [ Say Hello 你 ] │
  887. │ │
  888. └────────────────────────────┘
  889. ";
  890. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  891. Assert.True (btn.AutoSize);
  892. btn.Text = "Say He_llo 你 changed";
  893. Assert.True (btn.AutoSize);
  894. Application.Refresh ();
  895. expected = @"
  896. ┌ Test Demo 你 ──────────────┐
  897. │ │
  898. │ [ Say Hello 你 changed ] │
  899. │ │
  900. └────────────────────────────┘
  901. ";
  902. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  903. }
  904. [Fact, AutoInitShutdown]
  905. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  906. {
  907. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  908. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  909. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  910. var view4 = new View () {
  911. Text = "Say Hello view4 你",
  912. AutoSize = false,
  913. Width = 10,
  914. Height = 5,
  915. TextDirection = TextDirection.TopBottom_LeftRight
  916. };
  917. var view5 = new View () {
  918. Text = "Say Hello view5 你",
  919. Width = 10,
  920. Height = 5,
  921. AutoSize = false,
  922. TextDirection = TextDirection.TopBottom_LeftRight
  923. };
  924. var view6 = new View () {
  925. AutoSize = false,
  926. Width = 10,
  927. Height = 5,
  928. TextDirection = TextDirection.TopBottom_LeftRight,
  929. Text = "Say Hello view6 你",
  930. };
  931. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  932. Assert.False (view1.IsInitialized);
  933. Assert.False (view2.IsInitialized);
  934. Assert.False (view3.IsInitialized);
  935. Assert.False (view4.IsInitialized);
  936. Assert.False (view5.IsInitialized);
  937. Assert.False (view1.AutoSize);
  938. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  939. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  940. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  941. Assert.False (view2.AutoSize);
  942. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  943. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  944. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  945. Assert.False (view3.AutoSize);
  946. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  947. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  948. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  949. Assert.False (view4.AutoSize);
  950. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  951. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  952. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  953. Assert.False (view5.AutoSize);
  954. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  955. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  956. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  957. Assert.False (view6.AutoSize);
  958. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  959. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  960. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  961. Application.Begin (Application.Top);
  962. Assert.True (view1.IsInitialized);
  963. Assert.True (view2.IsInitialized);
  964. Assert.True (view3.IsInitialized);
  965. Assert.True (view4.IsInitialized);
  966. Assert.True (view5.IsInitialized);
  967. Assert.False (view1.AutoSize);
  968. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  969. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  970. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  971. Assert.False (view2.AutoSize);
  972. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  973. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  974. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  975. Assert.False (view3.AutoSize);
  976. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  977. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  978. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  979. Assert.False (view4.AutoSize);
  980. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  981. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  982. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  983. Assert.False (view5.AutoSize);
  984. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  985. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  986. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  987. Assert.False (view6.AutoSize);
  988. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  989. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  990. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  991. }
  992. [Fact, AutoInitShutdown]
  993. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  994. {
  995. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  996. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  997. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  998. var view4 = new View () {
  999. Text = "Say Hello view4 你",
  1000. AutoSize = true,
  1001. Width = 10,
  1002. Height = 5,
  1003. TextDirection = TextDirection.TopBottom_LeftRight
  1004. };
  1005. var view5 = new View () {
  1006. Text = "Say Hello view5 你",
  1007. Width = 10,
  1008. Height = 5,
  1009. AutoSize = true,
  1010. TextDirection = TextDirection.TopBottom_LeftRight
  1011. };
  1012. var view6 = new View () {
  1013. AutoSize = true,
  1014. Width = 10,
  1015. Height = 5,
  1016. TextDirection = TextDirection.TopBottom_LeftRight,
  1017. Text = "Say Hello view6 你",
  1018. };
  1019. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1020. Assert.False (view1.IsInitialized);
  1021. Assert.False (view2.IsInitialized);
  1022. Assert.False (view3.IsInitialized);
  1023. Assert.False (view4.IsInitialized);
  1024. Assert.False (view5.IsInitialized);
  1025. Assert.True (view1.AutoSize);
  1026. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1027. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1028. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1029. Assert.True (view2.AutoSize);
  1030. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1031. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1032. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1033. Assert.True (view3.AutoSize);
  1034. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1035. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1036. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1037. Assert.True (view4.AutoSize);
  1038. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1039. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1040. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1041. Assert.True (view5.AutoSize);
  1042. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1043. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1044. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1045. Assert.True (view6.AutoSize);
  1046. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1047. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1048. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1049. Application.Begin (Application.Top);
  1050. Assert.True (view1.IsInitialized);
  1051. Assert.True (view2.IsInitialized);
  1052. Assert.True (view3.IsInitialized);
  1053. Assert.True (view4.IsInitialized);
  1054. Assert.True (view5.IsInitialized);
  1055. Assert.True (view1.AutoSize);
  1056. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1057. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1058. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1059. Assert.True (view2.AutoSize);
  1060. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1061. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1062. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1063. Assert.True (view3.AutoSize);
  1064. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1065. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1066. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1067. Assert.True (view4.AutoSize);
  1068. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1069. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1070. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1071. Assert.True (view5.AutoSize);
  1072. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1073. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1074. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1075. Assert.True (view6.AutoSize);
  1076. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1077. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1078. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1079. }
  1080. [Fact, AutoInitShutdown]
  1081. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1082. {
  1083. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1084. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1085. Text = "Say Hello view2 你",
  1086. AutoSize = true,
  1087. TextDirection = TextDirection.TopBottom_LeftRight
  1088. };
  1089. Application.Top.Add (view1, view2);
  1090. var rs = Application.Begin (Application.Top);
  1091. Assert.True (view1.AutoSize);
  1092. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1093. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1094. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1095. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1096. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1097. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1098. Assert.True (view2.AutoSize);
  1099. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1100. Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1101. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1102. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1103. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1104. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1105. view1.Frame = new Rect (0, 0, 25, 4);
  1106. bool firstIteration = false;
  1107. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1108. Assert.True (view1.AutoSize);
  1109. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1110. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1111. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1112. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1113. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1114. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1115. view2.Frame = new Rect (0, 0, 1, 25);
  1116. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1117. Assert.True (view2.AutoSize);
  1118. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1119. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1120. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1121. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1122. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1123. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1124. }
  1125. [Fact, AutoInitShutdown]
  1126. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1127. {
  1128. var top = Application.Top;
  1129. var view1 = new View ();
  1130. Assert.False (view1.IsAdded);
  1131. Assert.Null (view1.X);
  1132. Assert.Null (view1.Y);
  1133. Assert.Null (view1.Width);
  1134. Assert.Null (view1.Height);
  1135. top.Add (view1);
  1136. Assert.True (view1.IsAdded);
  1137. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1138. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1139. Assert.Equal ("Absolute(0)", view1.Width.ToString ());
  1140. Assert.Equal ("Absolute(0)", view1.Height.ToString ());
  1141. var view2 = new View () {
  1142. X = Pos.Center (),
  1143. Y = Pos.Center (),
  1144. Width = Dim.Fill (),
  1145. Height = Dim.Fill ()
  1146. };
  1147. Assert.False (view2.IsAdded);
  1148. Assert.Equal ("Center", view2.X.ToString ());
  1149. Assert.Equal ("Center", view2.Y.ToString ());
  1150. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1151. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1152. top.Add (view2);
  1153. Assert.True (view2.IsAdded);
  1154. Assert.Equal ("Center", view2.X.ToString ());
  1155. Assert.Equal ("Center", view2.Y.ToString ());
  1156. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1157. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1158. }
  1159. [Fact]
  1160. public void SetRelativeLayout_PosCombine_Center_Plus_Absolute ()
  1161. {
  1162. var superView = new View () {
  1163. AutoSize = false,
  1164. Width = 10,
  1165. Height = 10
  1166. };
  1167. var testView = new View () {
  1168. AutoSize = false,
  1169. X = Pos.Center (),
  1170. Y = Pos.Center (),
  1171. Width = 1,
  1172. Height = 1
  1173. };
  1174. superView.Add (testView);
  1175. testView.SetRelativeLayout (superView.Frame);
  1176. Assert.Equal (4, testView.Frame.X);
  1177. Assert.Equal (4, testView.Frame.Y);
  1178. testView = new View () {
  1179. AutoSize = false,
  1180. X = Pos.Center () + 1,
  1181. Y = Pos.Center () + 1,
  1182. Width = 1,
  1183. Height = 1
  1184. };
  1185. superView.Add (testView);
  1186. testView.SetRelativeLayout (superView.Frame);
  1187. Assert.Equal (5, testView.Frame.X);
  1188. Assert.Equal (5, testView.Frame.Y);
  1189. testView = new View () {
  1190. AutoSize = false,
  1191. X = 1 + Pos.Center (),
  1192. Y = 1 + Pos.Center (),
  1193. Width = 1,
  1194. Height = 1
  1195. };
  1196. superView.Add (testView);
  1197. testView.SetRelativeLayout (superView.Frame);
  1198. Assert.Equal (5, testView.Frame.X);
  1199. Assert.Equal (5, testView.Frame.Y);
  1200. testView = new View () {
  1201. AutoSize = false,
  1202. X = 1 + Pos.Percent (50),
  1203. Y = Pos.Percent (50) + 1,
  1204. Width = 1,
  1205. Height = 1
  1206. };
  1207. superView.Add (testView);
  1208. testView.SetRelativeLayout (superView.Frame);
  1209. Assert.Equal (6, testView.Frame.X);
  1210. Assert.Equal (6, testView.Frame.Y);
  1211. testView = new View () {
  1212. AutoSize = false,
  1213. X = Pos.Percent (10) + Pos.Percent (40),
  1214. Y = Pos.Percent (10) + Pos.Percent (40),
  1215. Width = 1,
  1216. Height = 1
  1217. };
  1218. superView.Add (testView);
  1219. testView.SetRelativeLayout (superView.Frame);
  1220. Assert.Equal (5, testView.Frame.X);
  1221. Assert.Equal (5, testView.Frame.Y);
  1222. testView = new View () {
  1223. AutoSize = false,
  1224. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  1225. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  1226. Width = 1,
  1227. Height = 1
  1228. };
  1229. superView.Add (testView);
  1230. testView.SetRelativeLayout (superView.Frame);
  1231. Assert.Equal (5, testView.Frame.X);
  1232. Assert.Equal (5, testView.Frame.Y);
  1233. testView = new View () {
  1234. AutoSize = false,
  1235. X = Pos.Left (testView),
  1236. Y = Pos.Left (testView),
  1237. Width = 1,
  1238. Height = 1
  1239. };
  1240. superView.Add (testView);
  1241. testView.SetRelativeLayout (superView.Frame);
  1242. Assert.Equal (5, testView.Frame.X);
  1243. Assert.Equal (5, testView.Frame.Y);
  1244. testView = new View () {
  1245. AutoSize = false,
  1246. X = 1 + Pos.Left (testView),
  1247. Y = Pos.Top (testView) + 1,
  1248. Width = 1,
  1249. Height = 1
  1250. };
  1251. superView.Add (testView);
  1252. testView.SetRelativeLayout (superView.Frame);
  1253. Assert.Equal (6, testView.Frame.X);
  1254. Assert.Equal (6, testView.Frame.Y);
  1255. }
  1256. }
  1257. }