LayoutTests.cs 44 KB

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