LayoutTests.cs 47 KB

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