LayoutTests.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Xml.Linq;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. //using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ViewTests {
  11. public class LayoutTests {
  12. readonly ITestOutputHelper output;
  13. public LayoutTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void TopologicalSort_Missing_Add ()
  19. {
  20. var root = new View ();
  21. var sub1 = new View ();
  22. root.Add (sub1);
  23. var sub2 = new View ();
  24. sub1.Width = Dim.Width (sub2);
  25. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  26. sub2.Width = Dim.Width (sub1);
  27. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  28. }
  29. [Fact]
  30. public void TopologicalSort_Recursive_Ref ()
  31. {
  32. var root = new View ();
  33. var sub1 = new View ();
  34. root.Add (sub1);
  35. var sub2 = new View ();
  36. root.Add (sub2);
  37. sub2.Width = Dim.Width (sub2);
  38. var exception = Record.Exception (root.LayoutSubviews);
  39. Assert.Null (exception);
  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_ViewThatRefsSubView_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.BeginInit ();
  155. top.EndInit ();
  156. top.LayoutSubviews ();
  157. Assert.False (v.AutoSize);
  158. Assert.True (v.TrySetWidth (0, out _));
  159. Assert.Equal (80, v.Frame.Width);
  160. v.Width = Dim.Fill (1);
  161. top.LayoutSubviews ();
  162. Assert.True (v.TrySetWidth (0, out _));
  163. Assert.Equal (79, v.Frame.Width);
  164. v.AutoSize = true;
  165. top.LayoutSubviews ();
  166. Assert.True (v.TrySetWidth (0, out _));
  167. }
  168. [Fact]
  169. public void GetCurrentHeight_TrySetHeight ()
  170. {
  171. var top = new View () {
  172. X = 0,
  173. Y = 0,
  174. Height = 20
  175. };
  176. var v = new View () {
  177. Height = Dim.Fill ()
  178. };
  179. top.Add (v);
  180. top.BeginInit ();
  181. top.EndInit ();
  182. top.LayoutSubviews ();
  183. Assert.False (v.AutoSize);
  184. Assert.True (v.TrySetHeight (0, out _));
  185. Assert.Equal (20, v.Frame.Height);
  186. v.Height = Dim.Fill (1);
  187. top.LayoutSubviews ();
  188. Assert.True (v.TrySetHeight (0, out _));
  189. Assert.Equal (19, v.Frame.Height);
  190. v.AutoSize = true;
  191. top.LayoutSubviews ();
  192. Assert.True (v.TrySetHeight (0, out _));
  193. }
  194. [Fact]
  195. public void AutoSize_False_If_Text_Emmpty ()
  196. {
  197. var view1 = new View ();
  198. var view2 = new View ("");
  199. var view3 = new View () { Text = "" };
  200. Assert.False (view1.AutoSize);
  201. Assert.False (view2.AutoSize);
  202. Assert.False (view3.AutoSize);
  203. }
  204. [Fact]
  205. public void AutoSize_False_If_Text_Is_Not_Emmpty ()
  206. {
  207. var view1 = new View ();
  208. view1.Text = "Hello World";
  209. var view2 = new View ("Hello World");
  210. var view3 = new View () { Text = "Hello World" };
  211. Assert.False (view1.AutoSize);
  212. Assert.False (view2.AutoSize);
  213. Assert.False (view3.AutoSize);
  214. }
  215. [Fact]
  216. public void AutoSize_True_Label_If_Text_Emmpty ()
  217. {
  218. var label1 = new Label ();
  219. var label2 = new Label ("");
  220. var label3 = new Label () { Text = "" };
  221. Assert.True (label1.AutoSize);
  222. Assert.True (label2.AutoSize);
  223. Assert.True (label3.AutoSize);
  224. }
  225. [Fact]
  226. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  227. {
  228. var label1 = new Label ();
  229. label1.Text = "Hello World";
  230. var label2 = new Label ("Hello World");
  231. var label3 = new Label () { Text = "Hello World" };
  232. Assert.True (label1.AutoSize);
  233. Assert.True (label2.AutoSize);
  234. Assert.True (label3.AutoSize);
  235. }
  236. [Fact]
  237. public void AutoSize_False_ResizeView_Is_Always_False ()
  238. {
  239. var super = new View ();
  240. var label = new Label () { AutoSize = false };
  241. super.Add (label);
  242. label.Text = "New text";
  243. super.LayoutSubviews ();
  244. Assert.False (label.AutoSize);
  245. Assert.Equal ("{X=0,Y=0,Width=0,Height=1}", label.Bounds.ToString ());
  246. }
  247. [Fact]
  248. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  249. {
  250. var super = new View ();
  251. var label = new Label ();
  252. label.Text = "New text";
  253. // BUGBUG: v2 - label was never added to super, so it was never laid out.
  254. super.Add (label);
  255. super.LayoutSubviews ();
  256. Assert.True (label.AutoSize);
  257. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  258. }
  259. [Fact, AutoInitShutdown]
  260. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  261. {
  262. var win = new Window (new Rect (0, 0, 30, 80), "");
  263. var label = new Label () { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  264. win.Add (label);
  265. Application.Top.Add (win);
  266. // Text is empty so height=0
  267. Assert.False (label.AutoSize);
  268. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  269. label.Text = "New text\nNew line";
  270. Application.Top.LayoutSubviews ();
  271. Assert.False (label.AutoSize);
  272. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  273. Assert.False (label.IsInitialized);
  274. Application.Begin (Application.Top);
  275. Assert.True (label.IsInitialized);
  276. Assert.False (label.AutoSize);
  277. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  278. }
  279. [Fact, AutoInitShutdown]
  280. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  281. {
  282. var win = new Window (new Rect (0, 0, 30, 80), "win");
  283. var label = new Label () { Width = Dim.Fill () };
  284. win.Add (label);
  285. Application.Top.Add (win);
  286. Assert.True (label.IsAdded);
  287. // Text is empty so height=0
  288. Assert.True (label.AutoSize);
  289. // BUGBUG: LayoutSubviews has not been called, so this test is not really valid (pos/dim are indeterminate, not 0)
  290. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  291. label.Text = "First line\nSecond line";
  292. Application.Top.LayoutSubviews ();
  293. Assert.True (label.AutoSize);
  294. // BUGBUG: This test is bogus: label has not been initialized. pos/dim is indeterminate!
  295. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  296. Assert.False (label.IsInitialized);
  297. Application.Begin (Application.Top);
  298. Assert.True (label.AutoSize);
  299. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  300. Assert.True (label.IsInitialized);
  301. label.AutoSize = false;
  302. Application.Refresh ();
  303. Assert.False (label.AutoSize);
  304. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  305. }
  306. [Fact, AutoInitShutdown]
  307. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  308. {
  309. var win = new Window (new Rect (0, 0, 30, 80), "");
  310. var label = new Label () { Width = Dim.Fill () };
  311. win.Add (label);
  312. Application.Top.Add (win);
  313. // Text is empty so height=0
  314. Assert.True (label.AutoSize);
  315. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  316. Application.Begin (Application.Top);
  317. Assert.True (label.AutoSize);
  318. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  319. // and height 0 because wasn't set and the text is empty
  320. // BUGBUG: Because of #2450, this test is bogus: pos/dim is indeterminate!
  321. //Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
  322. label.Text = "First line\nSecond line";
  323. Application.Refresh ();
  324. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  325. // and height 2 because wasn't set and the text has 2 lines
  326. Assert.True (label.AutoSize);
  327. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  328. label.AutoSize = false;
  329. Application.Refresh ();
  330. // Here the SetMinWidthHeight ensuring the minimum height
  331. Assert.False (label.AutoSize);
  332. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  333. label.Text = "First changed line\nSecond changed line\nNew line";
  334. Application.Refresh ();
  335. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  336. // height 1 because wasn't set and SetMinWidthHeight ensuring the minimum height
  337. Assert.False (label.AutoSize);
  338. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  339. label.AutoSize = true;
  340. Application.Refresh ();
  341. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  342. // and height 3 because wasn't set and the text has 3 lines
  343. Assert.True (label.AutoSize);
  344. // BUGBUG: v2 - AutoSize is broken - temporarily disabling test See #2432
  345. //Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
  346. }
  347. [Fact, AutoInitShutdown]
  348. public void AutoSize_True_Setting_With_Height_Horizontal ()
  349. {
  350. var label = new Label ("Hello") { Width = 10, Height = 2 };
  351. var viewX = new View ("X") { X = Pos.Right (label) };
  352. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  353. Application.Top.Add (label, viewX, viewY);
  354. Application.Begin (Application.Top);
  355. Assert.True (label.AutoSize);
  356. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  357. var expected = @"
  358. Hello X
  359. Y
  360. ";
  361. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  362. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  363. label.AutoSize = false;
  364. Application.Refresh ();
  365. Assert.False (label.AutoSize);
  366. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  367. expected = @"
  368. Hello X
  369. Y
  370. ";
  371. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  372. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  373. }
  374. [Fact, AutoInitShutdown]
  375. public void AutoSize_True_Setting_With_Height_Vertical ()
  376. {
  377. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  378. var viewX = new View ("X") { X = Pos.Right (label) };
  379. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  380. Application.Top.Add (label, viewX, viewY);
  381. Application.Begin (Application.Top);
  382. Assert.True (label.AutoSize);
  383. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  384. var expected = @"
  385. H X
  386. e
  387. l
  388. l
  389. o
  390. Y
  391. ";
  392. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  393. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  394. label.AutoSize = false;
  395. Application.Refresh ();
  396. Assert.False (label.AutoSize);
  397. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  398. expected = @"
  399. H X
  400. e
  401. l
  402. l
  403. o
  404. Y
  405. ";
  406. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  407. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  408. }
  409. [Fact]
  410. [AutoInitShutdown]
  411. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  412. {
  413. var lbl = new Label ("123");
  414. Application.Top.Add (lbl);
  415. Application.Begin (Application.Top);
  416. Assert.True (lbl.AutoSize);
  417. Assert.Equal ("123 ", GetContents ());
  418. lbl.Text = "12";
  419. // Here the AutoSize ensuring the right size with width 3 (Dim.Absolute)
  420. // that was set on the OnAdded method with the text length of 3
  421. // and height 1 because wasn't set and the text has 1 line
  422. Assert.Equal (new Rect (0, 0, 3, 1), lbl.Frame);
  423. Assert.Equal (new Rect (0, 0, 3, 1), lbl._needsDisplay);
  424. Assert.Equal (new Rect (0, 0, 0, 0), lbl.SuperView._needsDisplay);
  425. Assert.True (lbl.SuperView.LayoutNeeded);
  426. lbl.SuperView.Redraw (lbl.SuperView.Bounds);
  427. Assert.Equal ("12 ", GetContents ());
  428. string GetContents ()
  429. {
  430. var text = "";
  431. for (int i = 0; i < 4; i++) {
  432. text += (char)Application.Driver.Contents [0, i, 0];
  433. }
  434. return text;
  435. }
  436. }
  437. [Fact, AutoInitShutdown]
  438. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  439. {
  440. var text = $"First line{Environment.NewLine}Second line";
  441. var horizontalView = new View () {
  442. Width = 20,
  443. Height = 1,
  444. Text = text
  445. };
  446. var verticalView = new View () {
  447. Y = 3,
  448. Height = 20,
  449. Width = 1,
  450. Text = text,
  451. TextDirection = TextDirection.TopBottom_LeftRight
  452. };
  453. var win = new Window () {
  454. Width = Dim.Fill (),
  455. Height = Dim.Fill (),
  456. Text = "Window"
  457. };
  458. win.Add (horizontalView, verticalView);
  459. Application.Top.Add (win);
  460. Application.Begin (Application.Top);
  461. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  462. Assert.False (horizontalView.AutoSize);
  463. Assert.False (verticalView.AutoSize);
  464. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  465. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  466. var expected = @"
  467. ┌──────────────────────────────┐
  468. │First line Second li │
  469. │ │
  470. │ │
  471. │F │
  472. │i │
  473. │r │
  474. │s │
  475. │t │
  476. │ │
  477. │l │
  478. │i │
  479. │n │
  480. │e │
  481. │ │
  482. │S │
  483. │e │
  484. │c │
  485. │o │
  486. │n │
  487. │d │
  488. │ │
  489. │l │
  490. │i │
  491. │ │
  492. │ │
  493. │ │
  494. │ │
  495. │ │
  496. │ │
  497. │ │
  498. └──────────────────────────────┘
  499. ";
  500. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  501. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  502. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  503. Application.Top.Redraw (Application.Top.Bounds);
  504. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  505. expected = @"
  506. ┌──────────────────────────────┐
  507. │First line Second li │
  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. └──────────────────────────────┘
  538. ";
  539. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  540. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  541. }
  542. [Fact, AutoInitShutdown]
  543. public void TextDirection_Toggle ()
  544. {
  545. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  546. var view = new View ();
  547. win.Add (view);
  548. Application.Top.Add (win);
  549. Application.Begin (Application.Top);
  550. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  551. Assert.False (view.AutoSize);
  552. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  553. Assert.Equal (Rect.Empty, view.Frame);
  554. Assert.Equal ("Absolute(0)", view.X.ToString ());
  555. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  556. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  557. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  558. var expected = @"
  559. ┌────────────────────┐
  560. │ │
  561. │ │
  562. │ │
  563. │ │
  564. │ │
  565. │ │
  566. │ │
  567. │ │
  568. │ │
  569. │ │
  570. │ │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. └────────────────────┘
  581. ";
  582. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  583. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  584. view.Text = "Hello World";
  585. view.Width = 11;
  586. view.Height = 1;
  587. win.LayoutSubviews ();
  588. Application.Refresh ();
  589. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  590. Assert.Equal ("Absolute(0)", view.X.ToString ());
  591. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  592. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  593. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  594. expected = @"
  595. ┌────────────────────┐
  596. │Hello World │
  597. │ │
  598. │ │
  599. │ │
  600. │ │
  601. │ │
  602. │ │
  603. │ │
  604. │ │
  605. │ │
  606. │ │
  607. │ │
  608. │ │
  609. │ │
  610. │ │
  611. │ │
  612. │ │
  613. │ │
  614. │ │
  615. │ │
  616. └────────────────────┘
  617. ";
  618. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  619. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  620. view.AutoSize = true;
  621. view.Text = "Hello Worlds";
  622. Application.Refresh ();
  623. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  624. Assert.Equal ("Absolute(0)", view.X.ToString ());
  625. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  626. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  627. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  628. expected = @"
  629. ┌────────────────────┐
  630. │Hello Worlds │
  631. │ │
  632. │ │
  633. │ │
  634. │ │
  635. │ │
  636. │ │
  637. │ │
  638. │ │
  639. │ │
  640. │ │
  641. │ │
  642. │ │
  643. │ │
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ │
  649. │ │
  650. └────────────────────┘
  651. ";
  652. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  653. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  654. view.TextDirection = TextDirection.TopBottom_LeftRight;
  655. Application.Refresh ();
  656. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  657. Assert.Equal ("Absolute(0)", view.X.ToString ());
  658. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  659. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  660. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  661. expected = @"
  662. ┌────────────────────┐
  663. │H │
  664. │e │
  665. │l │
  666. │l │
  667. │o │
  668. │ │
  669. │W │
  670. │o │
  671. │r │
  672. │l │
  673. │d │
  674. │s │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. └────────────────────┘
  684. ";
  685. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  686. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  687. view.AutoSize = false;
  688. view.Height = 1;
  689. Application.Refresh ();
  690. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  691. Assert.Equal ("Absolute(0)", view.X.ToString ());
  692. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  693. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  694. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  695. expected = @"
  696. ┌────────────────────┐
  697. │HelloWorlds │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. │ │
  705. │ │
  706. │ │
  707. │ │
  708. │ │
  709. │ │
  710. │ │
  711. │ │
  712. │ │
  713. │ │
  714. │ │
  715. │ │
  716. │ │
  717. └────────────────────┘
  718. ";
  719. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  720. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  721. view.PreserveTrailingSpaces = true;
  722. Application.Refresh ();
  723. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  724. Assert.Equal ("Absolute(0)", view.X.ToString ());
  725. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  726. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  727. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  728. expected = @"
  729. ┌────────────────────┐
  730. │Hello World │
  731. │ │
  732. │ │
  733. │ │
  734. │ │
  735. │ │
  736. │ │
  737. │ │
  738. │ │
  739. │ │
  740. │ │
  741. │ │
  742. │ │
  743. │ │
  744. │ │
  745. │ │
  746. │ │
  747. │ │
  748. │ │
  749. │ │
  750. └────────────────────┘
  751. ";
  752. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  753. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  754. view.PreserveTrailingSpaces = false;
  755. var f = view.Frame;
  756. view.Width = f.Height;
  757. view.Height = f.Width;
  758. view.TextDirection = TextDirection.TopBottom_LeftRight;
  759. Application.Refresh ();
  760. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  761. Assert.Equal ("Absolute(0)", view.X.ToString ());
  762. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  763. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  764. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  765. expected = @"
  766. ┌────────────────────┐
  767. │H │
  768. │e │
  769. │l │
  770. │l │
  771. │o │
  772. │ │
  773. │W │
  774. │o │
  775. │r │
  776. │l │
  777. │d │
  778. │ │
  779. │ │
  780. │ │
  781. │ │
  782. │ │
  783. │ │
  784. │ │
  785. │ │
  786. │ │
  787. └────────────────────┘
  788. ";
  789. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  790. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  791. view.AutoSize = true;
  792. Application.Refresh ();
  793. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  794. Assert.Equal ("Absolute(0)", view.X.ToString ());
  795. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  796. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  797. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  798. expected = @"
  799. ┌────────────────────┐
  800. │H │
  801. │e │
  802. │l │
  803. │l │
  804. │o │
  805. │ │
  806. │W │
  807. │o │
  808. │r │
  809. │l │
  810. │d │
  811. │s │
  812. │ │
  813. │ │
  814. │ │
  815. │ │
  816. │ │
  817. │ │
  818. │ │
  819. │ │
  820. └────────────────────┘
  821. ";
  822. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  823. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  824. }
  825. [Fact, AutoInitShutdown]
  826. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  827. {
  828. var text = $"Fi_nish 終";
  829. var horizontalView = new View () {
  830. Id = "horizontalView",
  831. AutoSize = true,
  832. HotKeySpecifier = '_',
  833. Text = text
  834. };
  835. var verticalView = new View () {
  836. Id = "verticalView",
  837. Y = 3,
  838. AutoSize = true,
  839. HotKeySpecifier = '_',
  840. Text = text,
  841. TextDirection = TextDirection.TopBottom_LeftRight
  842. };
  843. var win = new Window () {
  844. Id = "win",
  845. Width = Dim.Fill (),
  846. Height = Dim.Fill (),
  847. Text = "Window"
  848. };
  849. win.Add (horizontalView, verticalView);
  850. Application.Top.Add (win);
  851. Application.Begin (Application.Top);
  852. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  853. Assert.True (horizontalView.AutoSize);
  854. Assert.True (verticalView.AutoSize);
  855. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  856. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  857. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  858. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  859. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  860. // BUGBUG - v2 - With v1 AutoSize = true Width/Height should always match Frame.Width/Height,
  861. // but in v2, autosize will be replaced by Dim.Fit. Disabling test for now.
  862. //Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  863. //Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  864. //Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  865. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  866. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  867. //Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  868. //Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  869. var expected = @"
  870. ┌────────────────────┐
  871. │Finish 終 │
  872. │ │
  873. │ │
  874. │F │
  875. │i │
  876. │n │
  877. │i │
  878. │s │
  879. │h │
  880. │ │
  881. │終 │
  882. │ │
  883. │ │
  884. │ │
  885. │ │
  886. │ │
  887. │ │
  888. │ │
  889. │ │
  890. │ │
  891. └────────────────────┘
  892. ";
  893. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  894. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  895. verticalView.Text = $"最初_の行二行目";
  896. Application.Top.Redraw (Application.Top.Bounds);
  897. Assert.True (horizontalView.AutoSize);
  898. Assert.True (verticalView.AutoSize);
  899. // height was initialized with 8 and is kept as minimum
  900. Assert.Equal (new Rect (0, 3, 2, 7), verticalView.Frame);
  901. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  902. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  903. //Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  904. //Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  905. expected = @"
  906. ┌────────────────────┐
  907. │Finish 終 │
  908. │ │
  909. │ │
  910. │最 │
  911. │初 │
  912. │の │
  913. │行 │
  914. │二 │
  915. │行 │
  916. │目 │
  917. │ │
  918. │ │
  919. │ │
  920. │ │
  921. │ │
  922. │ │
  923. │ │
  924. │ │
  925. │ │
  926. │ │
  927. └────────────────────┘
  928. ";
  929. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  930. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  931. }
  932. [Fact, AutoInitShutdown]
  933. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  934. {
  935. var btn = new Button () {
  936. X = Pos.Center (),
  937. Y = Pos.Center (),
  938. Text = "Say He_llo 你"
  939. };
  940. var win = new Window () {
  941. Width = Dim.Fill (),
  942. Height = Dim.Fill (),
  943. Title = "Test Demo 你"
  944. };
  945. win.Add (btn);
  946. Application.Top.Add (win);
  947. Assert.True (btn.AutoSize);
  948. Application.Begin (Application.Top);
  949. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  950. var expected = @"
  951. ┌┤Test Demo 你├──────────────┐
  952. │ │
  953. │ [ Say Hello 你 ] │
  954. │ │
  955. └────────────────────────────┘
  956. ";
  957. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  958. Assert.True (btn.AutoSize);
  959. btn.Text = "Say He_llo 你 changed";
  960. Assert.True (btn.AutoSize);
  961. Application.Refresh ();
  962. expected = @"
  963. ┌┤Test Demo 你├──────────────┐
  964. │ │
  965. │ [ Say Hello 你 changed ] │
  966. │ │
  967. └────────────────────────────┘
  968. ";
  969. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  970. }
  971. [Fact, AutoInitShutdown]
  972. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  973. {
  974. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  975. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  976. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  977. var view4 = new View () {
  978. Text = "Say Hello view4 你",
  979. AutoSize = false,
  980. Width = 10,
  981. Height = 5,
  982. TextDirection = TextDirection.TopBottom_LeftRight
  983. };
  984. var view5 = new View () {
  985. Text = "Say Hello view5 你",
  986. Width = 10,
  987. Height = 5,
  988. AutoSize = false,
  989. TextDirection = TextDirection.TopBottom_LeftRight
  990. };
  991. var view6 = new View () {
  992. AutoSize = false,
  993. Width = 10,
  994. Height = 5,
  995. TextDirection = TextDirection.TopBottom_LeftRight,
  996. Text = "Say Hello view6 你",
  997. };
  998. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  999. Assert.False (view1.IsInitialized);
  1000. Assert.False (view2.IsInitialized);
  1001. Assert.False (view3.IsInitialized);
  1002. Assert.False (view4.IsInitialized);
  1003. Assert.False (view5.IsInitialized);
  1004. Assert.False (view1.AutoSize);
  1005. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1006. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1007. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1008. Assert.False (view2.AutoSize);
  1009. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1010. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1011. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1012. Assert.False (view3.AutoSize);
  1013. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1014. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1015. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1016. Assert.False (view4.AutoSize);
  1017. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1018. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1019. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1020. Assert.False (view5.AutoSize);
  1021. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1022. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1023. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1024. Assert.False (view6.AutoSize);
  1025. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1026. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1027. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1028. Application.Begin (Application.Top);
  1029. Assert.True (view1.IsInitialized);
  1030. Assert.True (view2.IsInitialized);
  1031. Assert.True (view3.IsInitialized);
  1032. Assert.True (view4.IsInitialized);
  1033. Assert.True (view5.IsInitialized);
  1034. Assert.False (view1.AutoSize);
  1035. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1036. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1037. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1038. Assert.False (view2.AutoSize);
  1039. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1040. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1041. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1042. Assert.False (view3.AutoSize);
  1043. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1044. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1045. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1046. Assert.False (view4.AutoSize);
  1047. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1048. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1049. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1050. Assert.False (view5.AutoSize);
  1051. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1052. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1053. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1054. Assert.False (view6.AutoSize);
  1055. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1056. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1057. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1058. }
  1059. [Fact, AutoInitShutdown]
  1060. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Different_Orders ()
  1061. {
  1062. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  1063. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  1064. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1065. var view4 = new View () {
  1066. Text = "Say Hello view4 你",
  1067. AutoSize = true,
  1068. Width = 10,
  1069. Height = 5,
  1070. TextDirection = TextDirection.TopBottom_LeftRight
  1071. };
  1072. var view5 = new View () {
  1073. Text = "Say Hello view5 你",
  1074. Width = 10,
  1075. Height = 5,
  1076. AutoSize = true,
  1077. TextDirection = TextDirection.TopBottom_LeftRight
  1078. };
  1079. var view6 = new View () {
  1080. AutoSize = true,
  1081. Width = 10,
  1082. Height = 5,
  1083. TextDirection = TextDirection.TopBottom_LeftRight,
  1084. Text = "Say Hello view6 你",
  1085. };
  1086. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1087. Assert.False (view1.IsInitialized);
  1088. Assert.False (view2.IsInitialized);
  1089. Assert.False (view3.IsInitialized);
  1090. Assert.False (view4.IsInitialized);
  1091. Assert.False (view5.IsInitialized);
  1092. Assert.True (view1.AutoSize);
  1093. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1094. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1095. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1096. Assert.True (view2.AutoSize);
  1097. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1098. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1099. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1100. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1101. //Assert.True (view3.AutoSize);
  1102. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1103. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1104. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1105. //Assert.True (view4.AutoSize);
  1106. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1107. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1108. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1109. //Assert.True (view5.AutoSize);
  1110. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1111. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1112. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1113. //Assert.True (view6.AutoSize);
  1114. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1115. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1116. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1117. Application.Begin (Application.Top);
  1118. Assert.True (view1.IsInitialized);
  1119. Assert.True (view2.IsInitialized);
  1120. Assert.True (view3.IsInitialized);
  1121. Assert.True (view4.IsInitialized);
  1122. Assert.True (view5.IsInitialized);
  1123. Assert.True (view1.AutoSize);
  1124. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1125. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1126. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1127. Assert.True (view2.AutoSize);
  1128. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1129. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1130. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1131. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1132. //Assert.True (view3.AutoSize);
  1133. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1134. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1135. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1136. //Assert.True (view4.AutoSize);
  1137. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1138. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1139. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1140. //Assert.True (view5.AutoSize);
  1141. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1142. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1143. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1144. //Assert.True (view6.AutoSize);
  1145. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1146. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1147. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1148. }
  1149. [Fact, AutoInitShutdown]
  1150. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1151. {
  1152. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1153. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1154. Text = "Say Hello view2 你",
  1155. AutoSize = true,
  1156. TextDirection = TextDirection.TopBottom_LeftRight
  1157. };
  1158. Application.Top.Add (view1, view2);
  1159. var rs = Application.Begin (Application.Top);
  1160. Assert.True (view1.AutoSize);
  1161. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1162. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1163. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1164. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1165. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1166. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1167. Assert.True (view2.AutoSize);
  1168. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1169. //Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1170. //Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1171. //Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1172. //Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1173. //Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1174. //Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1175. view1.Frame = new Rect (0, 0, 25, 4);
  1176. bool firstIteration = false;
  1177. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1178. Assert.True (view1.AutoSize);
  1179. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1180. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1181. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1182. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1183. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1184. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1185. view2.Frame = new Rect (0, 0, 1, 25);
  1186. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1187. Assert.True (view2.AutoSize);
  1188. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1189. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1190. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1191. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1192. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  1193. //Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1194. //Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1195. }
  1196. [Fact, AutoInitShutdown]
  1197. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1198. {
  1199. var top = Application.Top;
  1200. var view1 = new View ();
  1201. Assert.False (view1.IsAdded);
  1202. Assert.Null (view1.X);
  1203. Assert.Null (view1.Y);
  1204. Assert.Null (view1.Width);
  1205. Assert.Null (view1.Height);
  1206. top.Add (view1);
  1207. Assert.True (view1.IsAdded);
  1208. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1209. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1210. Assert.Equal ("Absolute(0)", view1.Width.ToString ());
  1211. Assert.Equal ("Absolute(0)", view1.Height.ToString ());
  1212. var view2 = new View () {
  1213. X = Pos.Center (),
  1214. Y = Pos.Center (),
  1215. Width = Dim.Fill (),
  1216. Height = Dim.Fill ()
  1217. };
  1218. Assert.False (view2.IsAdded);
  1219. Assert.Equal ("Center", view2.X.ToString ());
  1220. Assert.Equal ("Center", view2.Y.ToString ());
  1221. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1222. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1223. top.Add (view2);
  1224. Assert.True (view2.IsAdded);
  1225. Assert.Equal ("Center", view2.X.ToString ());
  1226. Assert.Equal ("Center", view2.Y.ToString ());
  1227. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1228. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1229. }
  1230. [Fact]
  1231. public void SetRelativeLayout_PosCombine_Center_Plus_Absolute ()
  1232. {
  1233. var superView = new View () {
  1234. AutoSize = false,
  1235. Width = 10,
  1236. Height = 10
  1237. };
  1238. var testView = new View () {
  1239. AutoSize = false,
  1240. X = Pos.Center (),
  1241. Y = Pos.Center (),
  1242. Width = 1,
  1243. Height = 1
  1244. };
  1245. superView.Add (testView);
  1246. testView.SetRelativeLayout (superView.Frame);
  1247. Assert.Equal (4, testView.Frame.X);
  1248. Assert.Equal (4, testView.Frame.Y);
  1249. testView = new View () {
  1250. AutoSize = false,
  1251. X = Pos.Center () + 1,
  1252. Y = Pos.Center () + 1,
  1253. Width = 1,
  1254. Height = 1
  1255. };
  1256. superView.Add (testView);
  1257. testView.SetRelativeLayout (superView.Frame);
  1258. Assert.Equal (5, testView.Frame.X);
  1259. Assert.Equal (5, testView.Frame.Y);
  1260. testView = new View () {
  1261. AutoSize = false,
  1262. X = 1 + Pos.Center (),
  1263. Y = 1 + Pos.Center (),
  1264. Width = 1,
  1265. Height = 1
  1266. };
  1267. superView.Add (testView);
  1268. testView.SetRelativeLayout (superView.Frame);
  1269. Assert.Equal (5, testView.Frame.X);
  1270. Assert.Equal (5, testView.Frame.Y);
  1271. testView = new View () {
  1272. AutoSize = false,
  1273. X = 1 + Pos.Percent (50),
  1274. Y = Pos.Percent (50) + 1,
  1275. Width = 1,
  1276. Height = 1
  1277. };
  1278. superView.Add (testView);
  1279. testView.SetRelativeLayout (superView.Frame);
  1280. Assert.Equal (6, testView.Frame.X);
  1281. Assert.Equal (6, testView.Frame.Y);
  1282. testView = new View () {
  1283. AutoSize = false,
  1284. X = Pos.Percent (10) + Pos.Percent (40),
  1285. Y = Pos.Percent (10) + Pos.Percent (40),
  1286. Width = 1,
  1287. Height = 1
  1288. };
  1289. superView.Add (testView);
  1290. testView.SetRelativeLayout (superView.Frame);
  1291. Assert.Equal (5, testView.Frame.X);
  1292. Assert.Equal (5, testView.Frame.Y);
  1293. testView = new View () {
  1294. AutoSize = false,
  1295. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  1296. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  1297. Width = 1,
  1298. Height = 1
  1299. };
  1300. superView.Add (testView);
  1301. testView.SetRelativeLayout (superView.Frame);
  1302. Assert.Equal (5, testView.Frame.X);
  1303. Assert.Equal (5, testView.Frame.Y);
  1304. testView = new View () {
  1305. AutoSize = false,
  1306. X = Pos.Left (testView),
  1307. Y = Pos.Left (testView),
  1308. Width = 1,
  1309. Height = 1
  1310. };
  1311. superView.Add (testView);
  1312. testView.SetRelativeLayout (superView.Frame);
  1313. Assert.Equal (5, testView.Frame.X);
  1314. Assert.Equal (5, testView.Frame.Y);
  1315. testView = new View () {
  1316. AutoSize = false,
  1317. X = 1 + Pos.Left (testView),
  1318. Y = Pos.Top (testView) + 1,
  1319. Width = 1,
  1320. Height = 1
  1321. };
  1322. superView.Add (testView);
  1323. testView.SetRelativeLayout (superView.Frame);
  1324. Assert.Equal (6, testView.Frame.X);
  1325. Assert.Equal (6, testView.Frame.Y);
  1326. }
  1327. [Theory, AutoInitShutdown]
  1328. [InlineData (1)]
  1329. [InlineData (2)]
  1330. [InlineData (3)]
  1331. [InlineData (4)]
  1332. [InlineData (5)]
  1333. [InlineData (6)]
  1334. [InlineData (7)]
  1335. [InlineData (8)]
  1336. [InlineData (9)]
  1337. [InlineData (10)]
  1338. public void Dim_CenteredSubView_85_Percent_Height (int height)
  1339. {
  1340. var win = new Window () {
  1341. Width = Dim.Fill (),
  1342. Height = Dim.Fill ()
  1343. };
  1344. var subview = new Window () {
  1345. X = Pos.Center (),
  1346. Y = Pos.Center (),
  1347. Width = Dim.Percent (85),
  1348. Height = Dim.Percent (85)
  1349. };
  1350. win.Add (subview);
  1351. var rs = Application.Begin (win);
  1352. bool firstIteration = false;
  1353. ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  1354. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1355. var expected = string.Empty;
  1356. switch (height) {
  1357. case 1:
  1358. //Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
  1359. expected = @"
  1360. ────────────────────";
  1361. break;
  1362. case 2:
  1363. //Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
  1364. expected = @"
  1365. ┌──────────────────┐
  1366. └──────────────────┘
  1367. ";
  1368. break;
  1369. case 3:
  1370. //Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
  1371. expected = @"
  1372. ┌──────────────────┐
  1373. │ │
  1374. └──────────────────┘
  1375. ";
  1376. break;
  1377. case 4:
  1378. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1379. expected = @"
  1380. ┌──────────────────┐
  1381. │ ─────────────── │
  1382. │ │
  1383. └──────────────────┘";
  1384. break;
  1385. case 5:
  1386. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1387. expected = @"
  1388. ┌──────────────────┐
  1389. │ ┌─────────────┐ │
  1390. │ └─────────────┘ │
  1391. │ │
  1392. └──────────────────┘";
  1393. break;
  1394. case 6:
  1395. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1396. expected = @"
  1397. ┌──────────────────┐
  1398. │ ┌─────────────┐ │
  1399. │ │ │ │
  1400. │ └─────────────┘ │
  1401. │ │
  1402. └──────────────────┘";
  1403. break;
  1404. case 7:
  1405. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1406. expected = @"
  1407. ┌──────────────────┐
  1408. │ ┌─────────────┐ │
  1409. │ │ │ │
  1410. │ │ │ │
  1411. │ └─────────────┘ │
  1412. │ │
  1413. └──────────────────┘";
  1414. break;
  1415. case 8:
  1416. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1417. expected = @"
  1418. ┌──────────────────┐
  1419. │ ┌─────────────┐ │
  1420. │ │ │ │
  1421. │ │ │ │
  1422. │ │ │ │
  1423. │ └─────────────┘ │
  1424. │ │
  1425. └──────────────────┘";
  1426. break;
  1427. case 9:
  1428. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1429. expected = @"
  1430. ┌──────────────────┐
  1431. │ │
  1432. │ ┌─────────────┐ │
  1433. │ │ │ │
  1434. │ │ │ │
  1435. │ │ │ │
  1436. │ └─────────────┘ │
  1437. │ │
  1438. └──────────────────┘";
  1439. break;
  1440. case 10:
  1441. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  1442. expected = @"
  1443. ┌──────────────────┐
  1444. │ │
  1445. │ ┌─────────────┐ │
  1446. │ │ │ │
  1447. │ │ │ │
  1448. │ │ │ │
  1449. │ │ │ │
  1450. │ └─────────────┘ │
  1451. │ │
  1452. └──────────────────┘";
  1453. break;
  1454. }
  1455. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1456. }
  1457. [Theory, AutoInitShutdown]
  1458. [InlineData (1)]
  1459. [InlineData (2)]
  1460. [InlineData (3)]
  1461. [InlineData (4)]
  1462. [InlineData (5)]
  1463. [InlineData (6)]
  1464. [InlineData (7)]
  1465. [InlineData (8)]
  1466. [InlineData (9)]
  1467. [InlineData (10)]
  1468. public void Dim_CenteredSubView_85_Percent_Width (int width)
  1469. {
  1470. var win = new Window () {
  1471. Width = Dim.Fill (),
  1472. Height = Dim.Fill ()
  1473. };
  1474. var subview = new Window () {
  1475. X = Pos.Center (),
  1476. Y = Pos.Center (),
  1477. Width = Dim.Percent (85),
  1478. Height = Dim.Percent (85)
  1479. };
  1480. win.Add (subview);
  1481. var rs = Application.Begin (win);
  1482. bool firstIteration = false;
  1483. ((FakeDriver)Application.Driver).SetBufferSize (width, 7);
  1484. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1485. var expected = string.Empty;
  1486. switch (width) {
  1487. case 1:
  1488. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1489. expected = @"
  1490. │";
  1491. break;
  1492. case 2:
  1493. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1494. expected = @"
  1495. ┌┐
  1496. ││
  1497. ││
  1498. ││
  1499. ││
  1500. ││
  1501. └┘";
  1502. break;
  1503. case 3:
  1504. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  1505. expected = @"
  1506. ┌─┐
  1507. │ │
  1508. │ │
  1509. │ │
  1510. │ │
  1511. │ │
  1512. └─┘
  1513. ";
  1514. break;
  1515. case 4:
  1516. Assert.Equal (new Rect (0, 0, 1, 4), subview.Frame);
  1517. expected = @"
  1518. ┌──┐
  1519. ││ │
  1520. ││ │
  1521. ││ │
  1522. ││ │
  1523. │ │
  1524. └──┘";
  1525. break;
  1526. case 5:
  1527. Assert.Equal (new Rect (0, 0, 2, 4), subview.Frame);
  1528. expected = @"
  1529. ┌───┐
  1530. │┌┐ │
  1531. │││ │
  1532. │││ │
  1533. │└┘ │
  1534. │ │
  1535. └───┘";
  1536. break;
  1537. case 6:
  1538. Assert.Equal (new Rect (0, 0, 3, 4), subview.Frame);
  1539. expected = @"
  1540. ┌────┐
  1541. │┌─┐ │
  1542. ││ │ │
  1543. ││ │ │
  1544. │└─┘ │
  1545. │ │
  1546. └────┘";
  1547. break;
  1548. case 7:
  1549. Assert.Equal (new Rect (0, 0, 4, 4), subview.Frame);
  1550. expected = @"
  1551. ┌─────┐
  1552. │┌──┐ │
  1553. ││ │ │
  1554. ││ │ │
  1555. │└──┘ │
  1556. │ │
  1557. └─────┘";
  1558. break;
  1559. case 8:
  1560. Assert.Equal (new Rect (0, 0, 5, 4), subview.Frame);
  1561. expected = @"
  1562. ┌──────┐
  1563. │┌───┐ │
  1564. ││ │ │
  1565. ││ │ │
  1566. │└───┘ │
  1567. │ │
  1568. └──────┘";
  1569. break;
  1570. case 9:
  1571. Assert.Equal (new Rect (1, 0, 5, 4), subview.Frame);
  1572. expected = @"
  1573. ┌───────┐
  1574. │ ┌───┐ │
  1575. │ │ │ │
  1576. │ │ │ │
  1577. │ └───┘ │
  1578. │ │
  1579. └───────┘";
  1580. break;
  1581. case 10:
  1582. Assert.Equal (new Rect (1, 0, 6, 4), subview.Frame);
  1583. expected = @"
  1584. ┌────────┐
  1585. │ ┌────┐ │
  1586. │ │ │ │
  1587. │ │ │ │
  1588. │ └────┘ │
  1589. │ │
  1590. └────────┘";
  1591. break;
  1592. }
  1593. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1594. }
  1595. [Fact, AutoInitShutdown]
  1596. public void PosCombine_DimCombine_View_With_SubViews ()
  1597. {
  1598. var clicked = false;
  1599. var top = Application.Top;
  1600. var win1 = new Window () { Id = "win1", Width = 20, Height = 10 };
  1601. var btn = new Button ("ok");
  1602. var win2 = new Window () { Id = "win2", Y = Pos.Bottom (btn) + 1, Width = 10, Height = 3 };
  1603. var view1 = new View () { Id = "view1", Width = Dim.Fill (), Height = 1, CanFocus = true };
  1604. view1.MouseClick += (sender, e) => clicked = true;
  1605. var view2 = new View () { Id = "view2", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  1606. view1.Add (view2);
  1607. win2.Add (view1);
  1608. win1.Add (btn, win2);
  1609. top.Add (win1);
  1610. var rs = Application.Begin (top);
  1611. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1612. ┌──────────────────┐
  1613. │[ ok ] │
  1614. │ │
  1615. │┌────────┐ │
  1616. ││ │ │
  1617. │└────────┘ │
  1618. │ │
  1619. │ │
  1620. │ │
  1621. └──────────────────┘", output);
  1622. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1623. Assert.Equal (new Rect (0, 0, 6, 1), btn.Frame);
  1624. Assert.Equal (new Rect (0, 0, 20, 10), win1.Frame);
  1625. Assert.Equal (new Rect (0, 2, 10, 3), win2.Frame);
  1626. Assert.Equal (new Rect (0, 0, 8, 1), view1.Frame);
  1627. Assert.Equal (new Rect (0, 0, 7, 1), view2.Frame);
  1628. var foundView = View.FindDeepestView (top, 9, 4, out int rx, out int ry);
  1629. Assert.Equal (foundView, view1);
  1630. ReflectionTools.InvokePrivate (
  1631. typeof (Application),
  1632. "ProcessMouseEvent",
  1633. new MouseEvent () {
  1634. X = 9,
  1635. Y = 4,
  1636. Flags = MouseFlags.Button1Clicked
  1637. });
  1638. Assert.True (clicked);
  1639. Application.End (rs);
  1640. }
  1641. }
  1642. }