LayoutTests.cs 55 KB

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