LayoutTests.cs 45 KB

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