LayoutTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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]
  200. [AutoInitShutdown]
  201. public void DimFill_SizedCorrectly ()
  202. {
  203. var view = new View () {
  204. Width = Dim.Fill (),
  205. Height = Dim.Fill (),
  206. BorderStyle = LineStyle.Single,
  207. };
  208. Application.Top.Add (view);
  209. var rs = Application.Begin (Application.Top);
  210. ((FakeDriver)Application.Driver).SetBufferSize (32, 5);
  211. //view.SetNeedsLayout ();
  212. Application.Top.LayoutSubviews ();
  213. //view.SetRelativeLayout (new Rect (0, 0, 32, 5));
  214. Assert.Equal (32, view.Frame.Width);
  215. Assert.Equal (5, view.Frame.Height);
  216. }
  217. [Fact] [AutoInitShutdown]
  218. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  219. {
  220. string text = $"First line{Environment.NewLine}Second line";
  221. var horizontalView = new View () {
  222. Width = 20,
  223. Height = 1,
  224. Text = text
  225. };
  226. var verticalView = new View () {
  227. Y = 3,
  228. Height = 20,
  229. Width = 1,
  230. Text = text,
  231. TextDirection = TextDirection.TopBottom_LeftRight
  232. };
  233. var win = new Window () {
  234. Width = Dim.Fill (),
  235. Height = Dim.Fill (),
  236. Text = "Window"
  237. };
  238. win.Add (horizontalView, verticalView);
  239. Application.Top.Add (win);
  240. var rs = Application.Begin (Application.Top);
  241. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  242. Assert.False (horizontalView.AutoSize);
  243. Assert.False (verticalView.AutoSize);
  244. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  245. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  246. string expected = @"
  247. ┌──────────────────────────────┐
  248. │First line Second li │
  249. │ │
  250. │ │
  251. │F │
  252. │i │
  253. │r │
  254. │s │
  255. │t │
  256. │ │
  257. │l │
  258. │i │
  259. │n │
  260. │e │
  261. │ │
  262. │S │
  263. │e │
  264. │c │
  265. │o │
  266. │n │
  267. │d │
  268. │ │
  269. │l │
  270. │i │
  271. │ │
  272. │ │
  273. │ │
  274. │ │
  275. │ │
  276. │ │
  277. │ │
  278. └──────────────────────────────┘
  279. ";
  280. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  281. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  282. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  283. Application.Top.Draw ();
  284. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  285. expected = @"
  286. ┌──────────────────────────────┐
  287. │First line Second li │
  288. │ │
  289. │ │
  290. │最 │
  291. │初 │
  292. │の │
  293. │行 │
  294. │ │
  295. │二 │
  296. │行 │
  297. │目 │
  298. │ │
  299. │ │
  300. │ │
  301. │ │
  302. │ │
  303. │ │
  304. │ │
  305. │ │
  306. │ │
  307. │ │
  308. │ │
  309. │ │
  310. │ │
  311. │ │
  312. │ │
  313. │ │
  314. │ │
  315. │ │
  316. │ │
  317. └──────────────────────────────┘
  318. ";
  319. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  320. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  321. Application.End (rs);
  322. }
  323. // Tested in AbsoluteLayoutTests.cs
  324. // public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  325. [Theory] [AutoInitShutdown]
  326. [InlineData (1)]
  327. [InlineData (2)]
  328. [InlineData (3)]
  329. [InlineData (4)]
  330. [InlineData (5)]
  331. [InlineData (6)]
  332. [InlineData (7)]
  333. [InlineData (8)]
  334. [InlineData (9)]
  335. [InlineData (10)]
  336. public void Dim_CenteredSubView_85_Percent_Height (int height)
  337. {
  338. var win = new Window () {
  339. Width = Dim.Fill (),
  340. Height = Dim.Fill ()
  341. };
  342. var subview = new Window () {
  343. X = Pos.Center (),
  344. Y = Pos.Center (),
  345. Width = Dim.Percent (85),
  346. Height = Dim.Percent (85)
  347. };
  348. win.Add (subview);
  349. var rs = Application.Begin (win);
  350. bool firstIteration = false;
  351. ((FakeDriver)Application.Driver).SetBufferSize (20, height);
  352. Application.RunIteration (ref rs, ref firstIteration);
  353. string expected = string.Empty;
  354. switch (height) {
  355. case 1:
  356. //Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
  357. expected = @"
  358. ────────────────────";
  359. break;
  360. case 2:
  361. //Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
  362. expected = @"
  363. ┌──────────────────┐
  364. └──────────────────┘
  365. ";
  366. break;
  367. case 3:
  368. //Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
  369. expected = @"
  370. ┌──────────────────┐
  371. │ │
  372. └──────────────────┘
  373. ";
  374. break;
  375. case 4:
  376. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  377. expected = @"
  378. ┌──────────────────┐
  379. │ ─────────────── │
  380. │ │
  381. └──────────────────┘";
  382. break;
  383. case 5:
  384. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  385. expected = @"
  386. ┌──────────────────┐
  387. │ ┌─────────────┐ │
  388. │ └─────────────┘ │
  389. │ │
  390. └──────────────────┘";
  391. break;
  392. case 6:
  393. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  394. expected = @"
  395. ┌──────────────────┐
  396. │ ┌─────────────┐ │
  397. │ │ │ │
  398. │ └─────────────┘ │
  399. │ │
  400. └──────────────────┘";
  401. break;
  402. case 7:
  403. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  404. expected = @"
  405. ┌──────────────────┐
  406. │ ┌─────────────┐ │
  407. │ │ │ │
  408. │ │ │ │
  409. │ └─────────────┘ │
  410. │ │
  411. └──────────────────┘";
  412. break;
  413. case 8:
  414. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  415. expected = @"
  416. ┌──────────────────┐
  417. │ ┌─────────────┐ │
  418. │ │ │ │
  419. │ │ │ │
  420. │ │ │ │
  421. │ └─────────────┘ │
  422. │ │
  423. └──────────────────┘";
  424. break;
  425. case 9:
  426. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  427. expected = @"
  428. ┌──────────────────┐
  429. │ │
  430. │ ┌─────────────┐ │
  431. │ │ │ │
  432. │ │ │ │
  433. │ │ │ │
  434. │ └─────────────┘ │
  435. │ │
  436. └──────────────────┘";
  437. break;
  438. case 10:
  439. //Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
  440. expected = @"
  441. ┌──────────────────┐
  442. │ │
  443. │ ┌─────────────┐ │
  444. │ │ │ │
  445. │ │ │ │
  446. │ │ │ │
  447. │ │ │ │
  448. │ └─────────────┘ │
  449. │ │
  450. └──────────────────┘";
  451. break;
  452. }
  453. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  454. Application.End (rs);
  455. }
  456. [Theory] [AutoInitShutdown]
  457. [InlineData (1)]
  458. [InlineData (2)]
  459. [InlineData (3)]
  460. [InlineData (4)]
  461. [InlineData (5)]
  462. [InlineData (6)]
  463. [InlineData (7)]
  464. [InlineData (8)]
  465. [InlineData (9)]
  466. [InlineData (10)]
  467. public void Dim_CenteredSubView_85_Percent_Width (int width)
  468. {
  469. var win = new Window () {
  470. Width = Dim.Fill (),
  471. Height = Dim.Fill ()
  472. };
  473. var subview = new Window () {
  474. X = Pos.Center (),
  475. Y = Pos.Center (),
  476. Width = Dim.Percent (85),
  477. Height = Dim.Percent (85)
  478. };
  479. win.Add (subview);
  480. var rs = Application.Begin (win);
  481. bool firstIteration = false;
  482. ((FakeDriver)Application.Driver).SetBufferSize (width, 7);
  483. Application.RunIteration (ref rs, ref firstIteration);
  484. string expected = string.Empty;
  485. switch (width) {
  486. case 1:
  487. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  488. expected = @"
  489. │";
  490. break;
  491. case 2:
  492. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  493. expected = @"
  494. ┌┐
  495. ││
  496. ││
  497. ││
  498. ││
  499. ││
  500. └┘";
  501. break;
  502. case 3:
  503. Assert.Equal (new Rect (0, 0, 0, 4), subview.Frame);
  504. expected = @"
  505. ┌─┐
  506. │ │
  507. │ │
  508. │ │
  509. │ │
  510. │ │
  511. └─┘";
  512. break;
  513. case 4:
  514. Assert.Equal (new Rect (0, 0, 1, 4), subview.Frame);
  515. expected = @"
  516. ┌──┐
  517. ││ │
  518. ││ │
  519. ││ │
  520. ││ │
  521. │ │
  522. └──┘";
  523. break;
  524. case 5:
  525. Assert.Equal (new Rect (0, 0, 2, 4), subview.Frame);
  526. expected = @"
  527. ┌───┐
  528. │┌┐ │
  529. │││ │
  530. │││ │
  531. │└┘ │
  532. │ │
  533. └───┘";
  534. break;
  535. case 6:
  536. Assert.Equal (new Rect (0, 0, 3, 4), subview.Frame);
  537. expected = @"
  538. ┌────┐
  539. │┌─┐ │
  540. ││ │ │
  541. ││ │ │
  542. │└─┘ │
  543. │ │
  544. └────┘";
  545. break;
  546. case 7:
  547. Assert.Equal (new Rect (0, 0, 4, 4), subview.Frame);
  548. expected = @"
  549. ┌─────┐
  550. │┌──┐ │
  551. ││ │ │
  552. ││ │ │
  553. │└──┘ │
  554. │ │
  555. └─────┘";
  556. break;
  557. case 8:
  558. Assert.Equal (new Rect (0, 0, 5, 4), subview.Frame);
  559. expected = @"
  560. ┌──────┐
  561. │┌───┐ │
  562. ││ │ │
  563. ││ │ │
  564. │└───┘ │
  565. │ │
  566. └──────┘";
  567. break;
  568. case 9:
  569. Assert.Equal (new Rect (1, 0, 5, 4), subview.Frame);
  570. expected = @"
  571. ┌───────┐
  572. │ ┌───┐ │
  573. │ │ │ │
  574. │ │ │ │
  575. │ └───┘ │
  576. │ │
  577. └───────┘";
  578. break;
  579. case 10:
  580. Assert.Equal (new Rect (1, 0, 6, 4), subview.Frame);
  581. expected = @"
  582. ┌────────┐
  583. │ ┌────┐ │
  584. │ │ │ │
  585. │ │ │ │
  586. │ └────┘ │
  587. │ │
  588. └────────┘";
  589. break;
  590. }
  591. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  592. Application.End (rs);
  593. }
  594. [Fact] [AutoInitShutdown]
  595. public void PosCombine_DimCombine_View_With_SubViews ()
  596. {
  597. bool clicked = false;
  598. var top = Application.Top;
  599. var win1 = new Window () { Id = "win1", Width = 20, Height = 10 };
  600. var view1 = new View ("view1");
  601. var win2 = new Window () { Id = "win2", Y = Pos.Bottom (view1) + 1, Width = 10, Height = 3 };
  602. var view2 = new View () { Id = "view2", Width = Dim.Fill (), Height = 1, CanFocus = true };
  603. view2.MouseClick += (sender, e) => clicked = true;
  604. var view3 = new View () { Id = "view3", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  605. view2.Add (view3);
  606. win2.Add (view2);
  607. win1.Add (view1, win2);
  608. top.Add (win1);
  609. var rs = Application.Begin (top);
  610. TestHelpers.AssertDriverContentsWithFrameAre (@"
  611. ┌──────────────────┐
  612. │view1 │
  613. │ │
  614. │┌────────┐ │
  615. ││ │ │
  616. │└────────┘ │
  617. │ │
  618. │ │
  619. │ │
  620. └──────────────────┘", _output);
  621. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  622. Assert.Equal (new Rect (0, 0, 5, 1), view1.Frame);
  623. Assert.Equal (new Rect (0, 0, 20, 10), win1.Frame);
  624. Assert.Equal (new Rect (0, 2, 10, 3), win2.Frame);
  625. Assert.Equal (new Rect (0, 0, 8, 1), view2.Frame);
  626. Assert.Equal (new Rect (0, 0, 7, 1), view3.Frame);
  627. var foundView = View.FindDeepestView (top, 9, 4, out int rx, out int ry);
  628. Assert.Equal (foundView, view2);
  629. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  630. X = 9,
  631. Y = 4,
  632. Flags = MouseFlags.Button1Clicked
  633. }));
  634. Assert.True (clicked);
  635. Application.End (rs);
  636. }
  637. [Fact] [TestRespondersDisposed]
  638. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  639. {
  640. Application.Init (new FakeDriver ());
  641. var top = Application.Top;
  642. var view = new View ("view") {
  643. Y = -2,
  644. Height = 10,
  645. TextDirection = TextDirection.TopBottom_LeftRight
  646. };
  647. top.Add (view);
  648. Application.Iteration += (s, a) => {
  649. Assert.Equal (-2, view.Y);
  650. Application.RequestStop ();
  651. };
  652. try {
  653. Application.Run ();
  654. } catch (IndexOutOfRangeException ex) {
  655. // After the fix this exception will not be caught.
  656. Assert.IsType<IndexOutOfRangeException> (ex);
  657. }
  658. // Shutdown must be called to safely clean up Application if Init has been called
  659. Application.Shutdown ();
  660. }
  661. [Fact] [TestRespondersDisposed]
  662. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  663. {
  664. Application.Init (new FakeDriver ());
  665. var top = Application.Top;
  666. var view = new View ("view") { X = -2 };
  667. top.Add (view);
  668. Application.Iteration += (s, a) => {
  669. Assert.Equal (-2, view.X);
  670. Application.RequestStop ();
  671. };
  672. try {
  673. Application.Run ();
  674. } catch (IndexOutOfRangeException ex) {
  675. // After the fix this exception will not be caught.
  676. Assert.IsType<IndexOutOfRangeException> (ex);
  677. }
  678. // Shutdown must be called to safely clean up Application if Init has been called
  679. Application.Shutdown ();
  680. }
  681. // Was named AutoSize_Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  682. // but doesn't actually have anything to do with AutoSize.
  683. [Fact]
  684. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  685. {
  686. Application.Init (new FakeDriver ());
  687. var t = Application.Top;
  688. var w = new Window () {
  689. X = Pos.Left (t) + 2,
  690. Y = Pos.At (2)
  691. };
  692. var v = new View () {
  693. X = Pos.Center (),
  694. Y = Pos.Percent (10)
  695. };
  696. w.Add (v);
  697. t.Add (w);
  698. t.Ready += (s, e) => {
  699. v.Frame = new Rect (2, 2, 10, 10);
  700. Assert.Equal (2, v.X = 2);
  701. Assert.Equal (2, v.Y = 2);
  702. };
  703. Application.Iteration += (s, a) => Application.RequestStop ();
  704. Application.Run ();
  705. Application.Shutdown ();
  706. }
  707. }