LayoutTests.cs 46 KB

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