LayoutTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 = 0;
  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 = 75;
  113. Assert.True (v.TrySetWidth (60, out rWidth));
  114. Assert.Equal (60, rWidth);
  115. }
  116. [Fact] [AutoInitShutdown]
  117. public void TrySetHeight_ForceValidatePosDim ()
  118. {
  119. var top = new View () {
  120. X = 0,
  121. Y = 0,
  122. Height = 20
  123. };
  124. var v = new View () {
  125. Height = Dim.Fill (),
  126. ValidatePosDim = true
  127. };
  128. top.Add (v);
  129. Assert.False (v.TrySetHeight (10, out int rHeight));
  130. Assert.Equal (10, rHeight);
  131. v.Height = Dim.Fill (1);
  132. Assert.False (v.TrySetHeight (10, out rHeight));
  133. Assert.Equal (9, rHeight);
  134. v.Height = 0;
  135. Assert.True (v.TrySetHeight (10, out rHeight));
  136. Assert.Equal (10, rHeight);
  137. Assert.False (v.IsInitialized);
  138. Application.Top.Add (top);
  139. Application.Begin (Application.Top);
  140. Assert.True (v.IsInitialized);
  141. v.Height = 15;
  142. Assert.True (v.TrySetHeight (5, out rHeight));
  143. Assert.Equal (5, rHeight);
  144. }
  145. [Fact] [TestRespondersDisposed]
  146. public void GetCurrentWidth_TrySetWidth ()
  147. {
  148. var top = new View () {
  149. X = 0,
  150. Y = 0,
  151. Width = 80
  152. };
  153. var v = new View () {
  154. Width = Dim.Fill ()
  155. };
  156. top.Add (v);
  157. top.BeginInit ();
  158. top.EndInit ();
  159. top.LayoutSubviews ();
  160. Assert.False (v.AutoSize);
  161. Assert.True (v.TrySetWidth (0, out _));
  162. Assert.Equal (80, v.Frame.Width);
  163. v.Width = Dim.Fill (1);
  164. top.LayoutSubviews ();
  165. Assert.True (v.TrySetWidth (0, out _));
  166. Assert.Equal (79, v.Frame.Width);
  167. v.AutoSize = true;
  168. top.LayoutSubviews ();
  169. Assert.True (v.TrySetWidth (0, out _));
  170. top.Dispose ();
  171. }
  172. [Fact]
  173. public void GetCurrentHeight_TrySetHeight ()
  174. {
  175. var top = new View () {
  176. X = 0,
  177. Y = 0,
  178. Height = 20
  179. };
  180. var v = new View () {
  181. Height = Dim.Fill ()
  182. };
  183. top.Add (v);
  184. top.BeginInit ();
  185. top.EndInit ();
  186. top.LayoutSubviews ();
  187. Assert.False (v.AutoSize);
  188. Assert.True (v.TrySetHeight (0, out _));
  189. Assert.Equal (20, v.Frame.Height);
  190. v.Height = Dim.Fill (1);
  191. top.LayoutSubviews ();
  192. Assert.True (v.TrySetHeight (0, out _));
  193. Assert.Equal (19, v.Frame.Height);
  194. v.AutoSize = true;
  195. top.LayoutSubviews ();
  196. Assert.True (v.TrySetHeight (0, out _));
  197. top.Dispose ();
  198. }
  199. [Fact] [AutoInitShutdown]
  200. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  201. {
  202. string text = $"First line{Environment.NewLine}Second line";
  203. var horizontalView = new View () {
  204. Width = 20,
  205. Height = 1,
  206. Text = text
  207. };
  208. var verticalView = new View () {
  209. Y = 3,
  210. Height = 20,
  211. Width = 1,
  212. Text = text,
  213. TextDirection = TextDirection.TopBottom_LeftRight
  214. };
  215. var win = new Window () {
  216. Width = Dim.Fill (),
  217. Height = Dim.Fill (),
  218. Text = "Window"
  219. };
  220. win.Add (horizontalView, verticalView);
  221. Application.Top.Add (win);
  222. var rs = Application.Begin (Application.Top);
  223. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  224. Assert.False (horizontalView.AutoSize);
  225. Assert.False (verticalView.AutoSize);
  226. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  227. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  228. string expected = @"
  229. ┌──────────────────────────────┐
  230. │First line Second li │
  231. │ │
  232. │ │
  233. │F │
  234. │i │
  235. │r │
  236. │s │
  237. │t │
  238. │ │
  239. │l │
  240. │i │
  241. │n │
  242. │e │
  243. │ │
  244. │S │
  245. │e │
  246. │c │
  247. │o │
  248. │n │
  249. │d │
  250. │ │
  251. │l │
  252. │i │
  253. │ │
  254. │ │
  255. │ │
  256. │ │
  257. │ │
  258. │ │
  259. │ │
  260. └──────────────────────────────┘
  261. ";
  262. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  263. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  264. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  265. Application.Top.Draw ();
  266. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  267. expected = @"
  268. ┌──────────────────────────────┐
  269. │First line Second li │
  270. │ │
  271. │ │
  272. │最 │
  273. │初 │
  274. │の │
  275. │行 │
  276. │ │
  277. │二 │
  278. │行 │
  279. │目 │
  280. │ │
  281. │ │
  282. │ │
  283. │ │
  284. │ │
  285. │ │
  286. │ │
  287. │ │
  288. │ │
  289. │ │
  290. │ │
  291. │ │
  292. │ │
  293. │ │
  294. │ │
  295. │ │
  296. │ │
  297. │ │
  298. │ │
  299. └──────────────────────────────┘
  300. ";
  301. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  302. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  303. Application.End (rs);
  304. }
  305. [Fact] [AutoInitShutdown]
  306. public void TextDirection_Toggle ()
  307. {
  308. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  309. var view = new View ();
  310. win.Add (view);
  311. Application.Top.Add (win);
  312. var rs = Application.Begin (Application.Top);
  313. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  314. Assert.Equal (new Rect (0, 0, 22, 22), win.Frame);
  315. Assert.Equal (new Rect (0, 0, 22, 22), win.Margin.Frame);
  316. Assert.Equal (new Rect (0, 0, 22, 22), win.Border.Frame);
  317. Assert.Equal (new Rect (1, 1, 20, 20), win.Padding.Frame);
  318. Assert.False (view.AutoSize);
  319. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  320. Assert.Equal (Rect.Empty, view.Frame);
  321. Assert.Equal ("Absolute(0)", view.X.ToString ());
  322. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  323. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  324. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  325. string expected = @"
  326. ┌────────────────────┐
  327. │ │
  328. │ │
  329. │ │
  330. │ │
  331. │ │
  332. │ │
  333. │ │
  334. │ │
  335. │ │
  336. │ │
  337. │ │
  338. │ │
  339. │ │
  340. │ │
  341. │ │
  342. │ │
  343. │ │
  344. │ │
  345. │ │
  346. │ │
  347. └────────────────────┘
  348. ";
  349. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  350. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  351. view.Text = "Hello World";
  352. view.Width = 11;
  353. view.Height = 1;
  354. win.LayoutSubviews ();
  355. Application.Refresh ();
  356. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  357. Assert.Equal ("Absolute(0)", view.X.ToString ());
  358. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  359. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  360. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  361. expected = @"
  362. ┌────────────────────┐
  363. │Hello World │
  364. │ │
  365. │ │
  366. │ │
  367. │ │
  368. │ │
  369. │ │
  370. │ │
  371. │ │
  372. │ │
  373. │ │
  374. │ │
  375. │ │
  376. │ │
  377. │ │
  378. │ │
  379. │ │
  380. │ │
  381. │ │
  382. │ │
  383. └────────────────────┘
  384. ";
  385. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  386. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  387. view.AutoSize = true;
  388. view.Text = "Hello Worlds";
  389. Application.Refresh ();
  390. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  391. Assert.Equal ("Absolute(0)", view.X.ToString ());
  392. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  393. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  394. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  395. expected = @"
  396. ┌────────────────────┐
  397. │Hello Worlds │
  398. │ │
  399. │ │
  400. │ │
  401. │ │
  402. │ │
  403. │ │
  404. │ │
  405. │ │
  406. │ │
  407. │ │
  408. │ │
  409. │ │
  410. │ │
  411. │ │
  412. │ │
  413. │ │
  414. │ │
  415. │ │
  416. │ │
  417. └────────────────────┘
  418. ";
  419. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  420. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  421. view.TextDirection = TextDirection.TopBottom_LeftRight;
  422. Application.Refresh ();
  423. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  424. Assert.Equal ("Absolute(0)", view.X.ToString ());
  425. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  426. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  427. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  428. expected = @"
  429. ┌────────────────────┐
  430. │H │
  431. │e │
  432. │l │
  433. │l │
  434. │o │
  435. │ │
  436. │W │
  437. │o │
  438. │r │
  439. │l │
  440. │d │
  441. │s │
  442. │ │
  443. │ │
  444. │ │
  445. │ │
  446. │ │
  447. │ │
  448. │ │
  449. │ │
  450. └────────────────────┘
  451. ";
  452. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  453. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  454. view.AutoSize = false;
  455. view.Height = 1;
  456. Application.Refresh ();
  457. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  458. Assert.Equal ("Absolute(0)", view.X.ToString ());
  459. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  460. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  461. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  462. expected = @"
  463. ┌────────────────────┐
  464. │HelloWorlds │
  465. │ │
  466. │ │
  467. │ │
  468. │ │
  469. │ │
  470. │ │
  471. │ │
  472. │ │
  473. │ │
  474. │ │
  475. │ │
  476. │ │
  477. │ │
  478. │ │
  479. │ │
  480. │ │
  481. │ │
  482. │ │
  483. │ │
  484. └────────────────────┘
  485. ";
  486. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  487. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  488. view.PreserveTrailingSpaces = true;
  489. Application.Refresh ();
  490. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  491. Assert.Equal ("Absolute(0)", view.X.ToString ());
  492. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  493. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  494. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  495. expected = @"
  496. ┌────────────────────┐
  497. │Hello World │
  498. │ │
  499. │ │
  500. │ │
  501. │ │
  502. │ │
  503. │ │
  504. │ │
  505. │ │
  506. │ │
  507. │ │
  508. │ │
  509. │ │
  510. │ │
  511. │ │
  512. │ │
  513. │ │
  514. │ │
  515. │ │
  516. │ │
  517. └────────────────────┘
  518. ";
  519. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  520. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  521. view.PreserveTrailingSpaces = false;
  522. var f = view.Frame;
  523. view.Width = f.Height;
  524. view.Height = f.Width;
  525. view.TextDirection = TextDirection.TopBottom_LeftRight;
  526. Application.Refresh ();
  527. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  528. Assert.Equal ("Absolute(0)", view.X.ToString ());
  529. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  530. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  531. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  532. expected = @"
  533. ┌────────────────────┐
  534. │H │
  535. │e │
  536. │l │
  537. │l │
  538. │o │
  539. │ │
  540. │W │
  541. │o │
  542. │r │
  543. │l │
  544. │d │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. └────────────────────┘
  555. ";
  556. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  557. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  558. view.AutoSize = true;
  559. Application.Refresh ();
  560. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  561. Assert.Equal ("Absolute(0)", view.X.ToString ());
  562. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  563. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  564. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  565. expected = @"
  566. ┌────────────────────┐
  567. │H │
  568. │e │
  569. │l │
  570. │l │
  571. │o │
  572. │ │
  573. │W │
  574. │o │
  575. │r │
  576. │l │
  577. │d │
  578. │s │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. │ │
  586. │ │
  587. └────────────────────┘
  588. ";
  589. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  590. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  591. Application.End (rs);
  592. }
  593. [Fact] [AutoInitShutdown]
  594. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  595. {
  596. string text = $"Fi_nish 終";
  597. var horizontalView = new View () {
  598. Id = "horizontalView",
  599. AutoSize = true,
  600. HotKeySpecifier = (Rune)'_',
  601. Text = text
  602. };
  603. var verticalView = new View () {
  604. Id = "verticalView",
  605. Y = 3,
  606. AutoSize = true,
  607. HotKeySpecifier = (Rune)'_',
  608. Text = text,
  609. TextDirection = TextDirection.TopBottom_LeftRight
  610. };
  611. var win = new Window () {
  612. Id = "win",
  613. Width = Dim.Fill (),
  614. Height = Dim.Fill (),
  615. Text = "Window"
  616. };
  617. win.Add (horizontalView, verticalView);
  618. Application.Top.Add (win);
  619. var rs = Application.Begin (Application.Top);
  620. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  621. Assert.True (horizontalView.AutoSize);
  622. Assert.True (verticalView.AutoSize);
  623. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  624. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  625. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  626. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  627. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  628. // BUGBUG - v2 - With v1 AutoSize = true Width/Height should always grow or keep initial value,
  629. // but in v2, autosize will be replaced by Dim.Fit. Disabling test for now.
  630. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  631. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  632. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  633. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  634. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  635. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  636. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  637. string expected = @"
  638. ┌────────────────────┐
  639. │Finish 終 │
  640. │ │
  641. │ │
  642. │F │
  643. │i │
  644. │n │
  645. │i │
  646. │s │
  647. │h │
  648. │ │
  649. │終 │
  650. │ │
  651. │ │
  652. │ │
  653. │ │
  654. │ │
  655. │ │
  656. │ │
  657. │ │
  658. │ │
  659. └────────────────────┘
  660. ";
  661. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  662. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  663. verticalView.Text = $"最初_の行二行目";
  664. Application.Top.Draw ();
  665. Assert.True (horizontalView.AutoSize);
  666. Assert.True (verticalView.AutoSize);
  667. // height was initialized with 8 and can only grow or keep initial value
  668. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  669. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  670. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  671. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  672. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  673. expected = @"
  674. ┌────────────────────┐
  675. │Finish 終 │
  676. │ │
  677. │ │
  678. │最 │
  679. │初 │
  680. │の │
  681. │行 │
  682. │二 │
  683. │行 │
  684. │目 │
  685. │ │
  686. │ │
  687. │ │
  688. │ │
  689. │ │
  690. │ │
  691. │ │
  692. │ │
  693. │ │
  694. │ │
  695. └────────────────────┘
  696. ";
  697. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  698. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  699. Application.End (rs);
  700. }
  701. // Tested in AbsoluteLayoutTests.cs
  702. // public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  703. [Theory] [AutoInitShutdown]
  704. [InlineData (1)]
  705. [InlineData (2)]
  706. [InlineData (3)]
  707. [InlineData (4)]
  708. [InlineData (5)]
  709. [InlineData (6)]
  710. [InlineData (7)]
  711. [InlineData (8)]
  712. [InlineData (9)]
  713. [InlineData (10)]
  714. public void Dim_CenteredSubView_85_Percent_Height (int height)
  715. {
  716. var win = new Window () {
  717. Width = Dim.Fill (),
  718. Height = Dim.Fill ()
  719. };
  720. var subview = new Window () {
  721. X = Pos.Center (),
  722. Y = Pos.Center (),
  723. Width = Dim.Percent (85),
  724. Height = Dim.Percent (85)
  725. };
  726. win.Add (subview);
  727. var rs = Application.Begin (win);
  728. bool firstIteration = false;
  729. ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  730. Application.RunIteration (ref rs, ref firstIteration);
  731. string expected = string.Empty;
  732. switch (height) {
  733. case 1:
  734. //Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
  735. expected = @"
  736. ────────────────────";
  737. break;
  738. case 2:
  739. //Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
  740. expected = @"
  741. ┌──────────────────┐
  742. └──────────────────┘
  743. ";
  744. break;
  745. case 3:
  746. //Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
  747. expected = @"
  748. ┌──────────────────┐
  749. │ │
  750. └──────────────────┘
  751. ";
  752. break;
  753. case 4:
  754. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  755. expected = @"
  756. ┌──────────────────┐
  757. │ ─────────────── │
  758. │ │
  759. └──────────────────┘";
  760. break;
  761. case 5:
  762. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  763. expected = @"
  764. ┌──────────────────┐
  765. │ ┌─────────────┐ │
  766. │ └─────────────┘ │
  767. │ │
  768. └──────────────────┘";
  769. break;
  770. case 6:
  771. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  772. expected = @"
  773. ┌──────────────────┐
  774. │ ┌─────────────┐ │
  775. │ │ │ │
  776. │ └─────────────┘ │
  777. │ │
  778. └──────────────────┘";
  779. break;
  780. case 7:
  781. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  782. expected = @"
  783. ┌──────────────────┐
  784. │ ┌─────────────┐ │
  785. │ │ │ │
  786. │ │ │ │
  787. │ └─────────────┘ │
  788. │ │
  789. └──────────────────┘";
  790. break;
  791. case 8:
  792. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  793. expected = @"
  794. ┌──────────────────┐
  795. │ ┌─────────────┐ │
  796. │ │ │ │
  797. │ │ │ │
  798. │ │ │ │
  799. │ └─────────────┘ │
  800. │ │
  801. └──────────────────┘";
  802. break;
  803. case 9:
  804. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  805. expected = @"
  806. ┌──────────────────┐
  807. │ │
  808. │ ┌─────────────┐ │
  809. │ │ │ │
  810. │ │ │ │
  811. │ │ │ │
  812. │ └─────────────┘ │
  813. │ │
  814. └──────────────────┘";
  815. break;
  816. case 10:
  817. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  818. expected = @"
  819. ┌──────────────────┐
  820. │ │
  821. │ ┌─────────────┐ │
  822. │ │ │ │
  823. │ │ │ │
  824. │ │ │ │
  825. │ │ │ │
  826. │ └─────────────┘ │
  827. │ │
  828. └──────────────────┘";
  829. break;
  830. }
  831. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  832. Application.End (rs);
  833. }
  834. [Theory] [AutoInitShutdown]
  835. [InlineData (1)]
  836. [InlineData (2)]
  837. [InlineData (3)]
  838. [InlineData (4)]
  839. [InlineData (5)]
  840. [InlineData (6)]
  841. [InlineData (7)]
  842. [InlineData (8)]
  843. [InlineData (9)]
  844. [InlineData (10)]
  845. public void Dim_CenteredSubView_85_Percent_Width (int width)
  846. {
  847. var win = new Window () {
  848. Width = Dim.Fill (),
  849. Height = Dim.Fill ()
  850. };
  851. var subview = new Window () {
  852. X = Pos.Center (),
  853. Y = Pos.Center (),
  854. Width = Dim.Percent (85),
  855. Height = Dim.Percent (85)
  856. };
  857. win.Add (subview);
  858. var rs = Application.Begin (win);
  859. bool firstIteration = false;
  860. ((FakeDriver)Application.Driver).SetBufferSize (width, 7);
  861. Application.RunIteration (ref rs, ref firstIteration);
  862. string expected = string.Empty;
  863. switch (width) {
  864. case 1:
  865. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  866. expected = @"
  867. │";
  868. break;
  869. case 2:
  870. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  871. expected = @"
  872. ┌┐
  873. ││
  874. ││
  875. ││
  876. ││
  877. ││
  878. └┘";
  879. break;
  880. case 3:
  881. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  882. expected = @"
  883. ┌─┐
  884. │ │
  885. │ │
  886. │ │
  887. │ │
  888. │ │
  889. └─┘";
  890. break;
  891. case 4:
  892. Assert.Equal (new Rect (0, 0, 1, 4), subview.Frame);
  893. expected = @"
  894. ┌──┐
  895. ││ │
  896. ││ │
  897. ││ │
  898. ││ │
  899. │ │
  900. └──┘";
  901. break;
  902. case 5:
  903. Assert.Equal (new Rect (0, 0, 2, 4), subview.Frame);
  904. expected = @"
  905. ┌───┐
  906. │┌┐ │
  907. │││ │
  908. │││ │
  909. │└┘ │
  910. │ │
  911. └───┘";
  912. break;
  913. case 6:
  914. Assert.Equal (new Rect (0, 0, 3, 4), subview.Frame);
  915. expected = @"
  916. ┌────┐
  917. │┌─┐ │
  918. ││ │ │
  919. ││ │ │
  920. │└─┘ │
  921. │ │
  922. └────┘";
  923. break;
  924. case 7:
  925. Assert.Equal (new Rect (0, 0, 4, 4), subview.Frame);
  926. expected = @"
  927. ┌─────┐
  928. │┌──┐ │
  929. ││ │ │
  930. ││ │ │
  931. │└──┘ │
  932. │ │
  933. └─────┘";
  934. break;
  935. case 8:
  936. Assert.Equal (new Rect (0, 0, 5, 4), subview.Frame);
  937. expected = @"
  938. ┌──────┐
  939. │┌───┐ │
  940. ││ │ │
  941. ││ │ │
  942. │└───┘ │
  943. │ │
  944. └──────┘";
  945. break;
  946. case 9:
  947. Assert.Equal (new Rect (1, 0, 5, 4), subview.Frame);
  948. expected = @"
  949. ┌───────┐
  950. │ ┌───┐ │
  951. │ │ │ │
  952. │ │ │ │
  953. │ └───┘ │
  954. │ │
  955. └───────┘";
  956. break;
  957. case 10:
  958. Assert.Equal (new Rect (1, 0, 6, 4), subview.Frame);
  959. expected = @"
  960. ┌────────┐
  961. │ ┌────┐ │
  962. │ │ │ │
  963. │ │ │ │
  964. │ └────┘ │
  965. │ │
  966. └────────┘";
  967. break;
  968. }
  969. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  970. Application.End (rs);
  971. }
  972. [Fact] [AutoInitShutdown]
  973. public void PosCombine_DimCombine_View_With_SubViews ()
  974. {
  975. bool clicked = false;
  976. var top = Application.Top;
  977. var win1 = new Window () { Id = "win1", Width = 20, Height = 10 };
  978. var label = new Label ("[ ok ]");
  979. var win2 = new Window () { Id = "win2", Y = Pos.Bottom (label) + 1, Width = 10, Height = 3 };
  980. var view1 = new View () { Id = "view1", Width = Dim.Fill (), Height = 1, CanFocus = true };
  981. view1.MouseClick += (sender, e) => clicked = true;
  982. var view2 = new View () { Id = "view2", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  983. view1.Add (view2);
  984. win2.Add (view1);
  985. win1.Add (label, win2);
  986. top.Add (win1);
  987. var rs = Application.Begin (top);
  988. TestHelpers.AssertDriverContentsWithFrameAre (@"
  989. ┌──────────────────┐
  990. │[ ok ] │
  991. │ │
  992. │┌────────┐ │
  993. ││ │ │
  994. │└────────┘ │
  995. │ │
  996. │ │
  997. │ │
  998. └──────────────────┘", _output);
  999. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1000. Assert.Equal (new Rect (0, 0, 6, 1), label.Frame);
  1001. Assert.Equal (new Rect (0, 0, 20, 10), win1.Frame);
  1002. Assert.Equal (new Rect (0, 2, 10, 3), win2.Frame);
  1003. Assert.Equal (new Rect (0, 0, 8, 1), view1.Frame);
  1004. Assert.Equal (new Rect (0, 0, 7, 1), view2.Frame);
  1005. var foundView = View.FindDeepestView (top, 9, 4, out int rx, out int ry);
  1006. Assert.Equal (foundView, view1);
  1007. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  1008. X = 9,
  1009. Y = 4,
  1010. Flags = MouseFlags.Button1Clicked
  1011. }));
  1012. Assert.True (clicked);
  1013. Application.End (rs);
  1014. }
  1015. [Fact] [TestRespondersDisposed]
  1016. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  1017. {
  1018. Application.Init (new FakeDriver ());
  1019. var top = Application.Top;
  1020. var view = new View ("view") {
  1021. Y = -2,
  1022. Height = 10,
  1023. TextDirection = TextDirection.TopBottom_LeftRight
  1024. };
  1025. top.Add (view);
  1026. Application.Iteration += (s, a) => {
  1027. Assert.Equal (-2, view.Y);
  1028. Application.RequestStop ();
  1029. };
  1030. try {
  1031. Application.Run ();
  1032. } catch (IndexOutOfRangeException ex) {
  1033. // After the fix this exception will not be caught.
  1034. Assert.IsType<IndexOutOfRangeException> (ex);
  1035. }
  1036. // Shutdown must be called to safely clean up Application if Init has been called
  1037. Application.Shutdown ();
  1038. }
  1039. [Fact] [TestRespondersDisposed]
  1040. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  1041. {
  1042. Application.Init (new FakeDriver ());
  1043. var top = Application.Top;
  1044. var view = new View ("view") { X = -2 };
  1045. top.Add (view);
  1046. Application.Iteration += (s, a) => {
  1047. Assert.Equal (-2, view.X);
  1048. Application.RequestStop ();
  1049. };
  1050. try {
  1051. Application.Run ();
  1052. } catch (IndexOutOfRangeException ex) {
  1053. // After the fix this exception will not be caught.
  1054. Assert.IsType<IndexOutOfRangeException> (ex);
  1055. }
  1056. // Shutdown must be called to safely clean up Application if Init has been called
  1057. Application.Shutdown ();
  1058. }
  1059. }