PosTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. namespace Terminal.Gui.ViewTests;
  4. public class PosTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public PosTests (ITestOutputHelper output) { _output = output; }
  8. [Fact]
  9. public void AnchorEnd_Equal ()
  10. {
  11. var n1 = 0;
  12. var n2 = 0;
  13. Pos pos1 = Pos.AnchorEnd (n1);
  14. Pos pos2 = Pos.AnchorEnd (n2);
  15. Assert.Equal (pos1, pos2);
  16. // Test inequality
  17. n2 = 5;
  18. pos2 = Pos.AnchorEnd (n2);
  19. Assert.NotEqual (pos1, pos2);
  20. }
  21. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  22. // TODO: A new test that calls SetRelativeLayout directly is needed.
  23. [Fact]
  24. [AutoInitShutdown]
  25. public void AnchorEnd_Equal_Inside_Window ()
  26. {
  27. var viewWidth = 10;
  28. var viewHeight = 1;
  29. var tv = new TextView
  30. {
  31. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  32. };
  33. var win = new Window ();
  34. win.Add (tv);
  35. Toplevel top = Application.Top;
  36. top.Add (win);
  37. RunState rs = Application.Begin (top);
  38. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  39. Assert.Equal (new Rectangle (0, 0, 80, 25), win.Frame);
  40. Assert.Equal (new Rectangle (68, 22, 10, 1), tv.Frame);
  41. Application.End (rs);
  42. }
  43. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  44. // TODO: A new test that calls SetRelativeLayout directly is needed.
  45. [Fact]
  46. [AutoInitShutdown]
  47. public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  48. {
  49. var viewWidth = 10;
  50. var viewHeight = 1;
  51. var tv = new TextView
  52. {
  53. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  54. };
  55. var win = new Window ();
  56. win.Add (tv);
  57. var menu = new MenuBar ();
  58. var status = new StatusBar ();
  59. Toplevel top = Application.Top;
  60. top.Add (win, menu, status);
  61. RunState rs = Application.Begin (top);
  62. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  63. Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
  64. Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
  65. Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
  66. Assert.Equal (new Rectangle (68, 20, 10, 1), tv.Frame);
  67. Application.End (rs);
  68. }
  69. [Fact]
  70. public void AnchorEnd_Negative_Throws ()
  71. {
  72. Pos pos;
  73. int n = -1;
  74. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  75. }
  76. [Fact]
  77. public void AnchorEnd_SetsValue ()
  78. {
  79. var n = 0;
  80. Pos pos = Pos.AnchorEnd ();
  81. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  82. n = 5;
  83. pos = Pos.AnchorEnd (n);
  84. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  85. }
  86. // This test used to be Dialog_In_Window_With_TextField_And_Button_AnchorEnd in DialogTests.
  87. [Fact]
  88. [SetupFakeDriver]
  89. public void AnchorEnd_View_And_Button ()
  90. {
  91. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  92. var b = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  93. var frame = new FrameView { Width = 18, Height = 3 };
  94. Assert.Equal (16, frame.Bounds.Width);
  95. Button btn = null;
  96. int Btn_Width () { return btn?.Bounds.Width ?? 0; }
  97. btn = new Button { Text = "Ok", X = Pos.AnchorEnd () - Pos.Function (Btn_Width) };
  98. var view = new View
  99. {
  100. Text = "0123456789abcdefghij",
  101. // Dim.Fill (1) fills remaining space minus 1 (16 - 1 = 15)
  102. // Dim.Function (Btn_Width) is 6
  103. // Width should be 15 - 6 = 9
  104. Width = Dim.Fill (1) - Dim.Function (Btn_Width),
  105. Height = 1
  106. };
  107. frame.Add (btn, view);
  108. frame.BeginInit ();
  109. frame.EndInit ();
  110. frame.Draw ();
  111. Assert.Equal (6, btn.Bounds.Width);
  112. Assert.Equal (10, btn.Frame.X); // frame.Bounds.Width (16) - btn.Frame.Width (6) = 10
  113. Assert.Equal (0, btn.Frame.Y);
  114. Assert.Equal (6, btn.Frame.Width);
  115. Assert.Equal (1, btn.Frame.Height);
  116. Assert.Equal (9, view.Bounds.Width); // frame.Bounds.Width (16) - Dim.Fill (1) - Dim.Function (6) = 9
  117. Assert.Equal (0, view.Frame.X);
  118. Assert.Equal (0, view.Frame.Y);
  119. Assert.Equal (9, view.Frame.Width);
  120. Assert.Equal (1, view.Frame.Height);
  121. var expected = $@"
  122. ┌────────────────┐
  123. │012345678 {
  124. b
  125. }│
  126. └────────────────┘
  127. ";
  128. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  129. }
  130. [Fact]
  131. public void At_Equal ()
  132. {
  133. var n1 = 0;
  134. var n2 = 0;
  135. Pos pos1 = Pos.At (n1);
  136. Pos pos2 = Pos.At (n2);
  137. Assert.Equal (pos1, pos2);
  138. }
  139. [Fact]
  140. public void At_SetsValue ()
  141. {
  142. Pos pos = Pos.At (0);
  143. Assert.Equal ("Absolute(0)", pos.ToString ());
  144. pos = Pos.At (5);
  145. Assert.Equal ("Absolute(5)", pos.ToString ());
  146. pos = Pos.At (-1);
  147. Assert.Equal ("Absolute(-1)", pos.ToString ());
  148. }
  149. [Fact]
  150. public void Center_SetsValue ()
  151. {
  152. Pos pos = Pos.Center ();
  153. Assert.Equal ("Center", pos.ToString ());
  154. }
  155. [Fact]
  156. public void DoNotReturnPosCombine ()
  157. {
  158. var v = new View { Id = "V" };
  159. Pos pos = Pos.Left (v);
  160. Assert.Equal (
  161. $"View(side=x,target=View(V){v.Frame})",
  162. pos.ToString ()
  163. );
  164. pos = Pos.X (v);
  165. Assert.Equal (
  166. $"View(side=x,target=View(V){v.Frame})",
  167. pos.ToString ()
  168. );
  169. pos = Pos.Top (v);
  170. Assert.Equal (
  171. $"View(side=y,target=View(V){v.Frame})",
  172. pos.ToString ()
  173. );
  174. pos = Pos.Y (v);
  175. Assert.Equal (
  176. $"View(side=y,target=View(V){v.Frame})",
  177. pos.ToString ()
  178. );
  179. pos = Pos.Right (v);
  180. Assert.Equal (
  181. $"View(side=right,target=View(V){v.Frame})",
  182. pos.ToString ()
  183. );
  184. pos = Pos.Bottom (v);
  185. Assert.Equal (
  186. $"View(side=bottom,target=View(V){v.Frame})",
  187. pos.ToString ()
  188. );
  189. }
  190. [Fact]
  191. public void Function_Equal ()
  192. {
  193. Func<int> f1 = () => 0;
  194. Func<int> f2 = () => 0;
  195. Pos pos1 = Pos.Function (f1);
  196. Pos pos2 = Pos.Function (f2);
  197. Assert.Equal (pos1, pos2);
  198. f2 = () => 1;
  199. pos2 = Pos.Function (f2);
  200. Assert.NotEqual (pos1, pos2);
  201. }
  202. [Fact]
  203. public void Function_SetsValue ()
  204. {
  205. var text = "Test";
  206. Pos pos = Pos.Function (() => text.Length);
  207. Assert.Equal ("PosFunc(4)", pos.ToString ());
  208. text = "New Test";
  209. Assert.Equal ("PosFunc(8)", pos.ToString ());
  210. text = "";
  211. Assert.Equal ("PosFunc(0)", pos.ToString ());
  212. }
  213. [Fact]
  214. [TestRespondersDisposed]
  215. public void Internal_Tests ()
  216. {
  217. var posFactor = new Pos.PosFactor (0.10F);
  218. Assert.Equal (10, posFactor.Anchor (100));
  219. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  220. Assert.Equal (99, posAnchorEnd.Anchor (100));
  221. var posCenter = new Pos.PosCenter ();
  222. Assert.Equal (50, posCenter.Anchor (100));
  223. var posAbsolute = new Pos.PosAbsolute (10);
  224. Assert.Equal (10, posAbsolute.Anchor (0));
  225. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  226. Assert.Equal (posCombine._left, posFactor);
  227. Assert.Equal (posCombine._right, posAbsolute);
  228. Assert.Equal (20, posCombine.Anchor (100));
  229. posCombine = new Pos.PosCombine (true, posAbsolute, posFactor);
  230. Assert.Equal (posCombine._left, posAbsolute);
  231. Assert.Equal (posCombine._right, posFactor);
  232. Assert.Equal (20, posCombine.Anchor (100));
  233. var view = new View { Frame = new Rectangle (20, 10, 20, 1) };
  234. var posViewX = new Pos.PosView (view, 0);
  235. Assert.Equal (20, posViewX.Anchor (0));
  236. var posViewY = new Pos.PosView (view, 1);
  237. Assert.Equal (10, posViewY.Anchor (0));
  238. var posRight = new Pos.PosView (view, 2);
  239. Assert.Equal (40, posRight.Anchor (0));
  240. var posViewBottom = new Pos.PosView (view, 3);
  241. Assert.Equal (11, posViewBottom.Anchor (0));
  242. view.Dispose ();
  243. }
  244. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  245. // TODO: A new test that calls SetRelativeLayout directly is needed.
  246. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  247. [Fact]
  248. [TestRespondersDisposed]
  249. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  250. {
  251. // Setup Fake driver
  252. (Window win, Button button) setup ()
  253. {
  254. Application.Init (new FakeDriver ());
  255. Application.Iteration += (s, a) => { Application.RequestStop (); };
  256. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  257. Application.Top.Add (win);
  258. var button = new Button { X = Pos.Center (), Text = "button" };
  259. win.Add (button);
  260. return (win, button);
  261. }
  262. RunState rs;
  263. void cleanup (RunState rs)
  264. {
  265. // Cleanup
  266. Application.End (rs);
  267. // Shutdown must be called to safely clean up Application if Init has been called
  268. Application.Shutdown ();
  269. }
  270. // Test cases:
  271. (Window win, Button button) app = setup ();
  272. app.button.Y = Pos.Left (app.win);
  273. rs = Application.Begin (Application.Top);
  274. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  275. Application.RunLoop (rs);
  276. cleanup (rs);
  277. app = setup ();
  278. app.button.Y = Pos.X (app.win);
  279. rs = Application.Begin (Application.Top);
  280. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  281. Application.RunLoop (rs);
  282. cleanup (rs);
  283. app = setup ();
  284. app.button.Y = Pos.Top (app.win);
  285. rs = Application.Begin (Application.Top);
  286. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  287. Application.RunLoop (rs);
  288. cleanup (rs);
  289. app = setup ();
  290. app.button.Y = Pos.Y (app.win);
  291. rs = Application.Begin (Application.Top);
  292. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  293. Application.RunLoop (rs);
  294. cleanup (rs);
  295. app = setup ();
  296. app.button.Y = Pos.Bottom (app.win);
  297. rs = Application.Begin (Application.Top);
  298. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  299. Application.RunLoop (rs);
  300. cleanup (rs);
  301. app = setup ();
  302. app.button.Y = Pos.Right (app.win);
  303. rs = Application.Begin (Application.Top);
  304. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  305. Application.RunLoop (rs);
  306. cleanup (rs);
  307. }
  308. [Fact]
  309. public void New_Works ()
  310. {
  311. var pos = new Pos ();
  312. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  313. }
  314. [Fact]
  315. public void Percent_Equal ()
  316. {
  317. float n1 = 0;
  318. float n2 = 0;
  319. Pos pos1 = Pos.Percent (n1);
  320. Pos pos2 = Pos.Percent (n2);
  321. Assert.Equal (pos1, pos2);
  322. n1 = n2 = 1;
  323. pos1 = Pos.Percent (n1);
  324. pos2 = Pos.Percent (n2);
  325. Assert.Equal (pos1, pos2);
  326. n1 = n2 = 0.5f;
  327. pos1 = Pos.Percent (n1);
  328. pos2 = Pos.Percent (n2);
  329. Assert.Equal (pos1, pos2);
  330. n1 = n2 = 100f;
  331. pos1 = Pos.Percent (n1);
  332. pos2 = Pos.Percent (n2);
  333. Assert.Equal (pos1, pos2);
  334. n1 = 0;
  335. n2 = 1;
  336. pos1 = Pos.Percent (n1);
  337. pos2 = Pos.Percent (n2);
  338. Assert.NotEqual (pos1, pos2);
  339. n1 = 0.5f;
  340. n2 = 1.5f;
  341. pos1 = Pos.Percent (n1);
  342. pos2 = Pos.Percent (n2);
  343. Assert.NotEqual (pos1, pos2);
  344. }
  345. [Fact]
  346. public void Percent_SetsValue ()
  347. {
  348. float f = 0;
  349. Pos pos = Pos.Percent (f);
  350. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  351. f = 0.5F;
  352. pos = Pos.Percent (f);
  353. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  354. f = 100;
  355. pos = Pos.Percent (f);
  356. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  357. }
  358. [Fact]
  359. public void Percent_ThrowsOnIvalid ()
  360. {
  361. Pos pos = Pos.Percent (0);
  362. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  363. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  364. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  365. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  366. }
  367. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  368. // TODO: A new test that calls SetRelativeLayout directly is needed.
  369. [Fact]
  370. [TestRespondersDisposed]
  371. public void Pos_Add_Operator ()
  372. {
  373. Application.Init (new FakeDriver ());
  374. Toplevel top = Application.Top;
  375. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  376. var field = new TextField { X = 0, Y = 0, Width = 20 };
  377. var count = 0;
  378. field.KeyDown += (s, k) =>
  379. {
  380. if (k.KeyCode == KeyCode.Enter)
  381. {
  382. field.Text = $"View {count}";
  383. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  384. view.Add (view2);
  385. Assert.Equal ($"View {count}", view2.Text);
  386. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  387. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  388. field.Y += 1;
  389. count++;
  390. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  391. }
  392. };
  393. Application.Iteration += (s, a) =>
  394. {
  395. while (count < 20)
  396. {
  397. field.NewKeyDownEvent (Key.Enter);
  398. }
  399. Application.RequestStop ();
  400. };
  401. var win = new Window ();
  402. win.Add (view);
  403. win.Add (field);
  404. top.Add (win);
  405. Application.Run (top);
  406. Assert.Equal (20, count);
  407. // Shutdown must be called to safely clean up Application if Init has been called
  408. Application.Shutdown ();
  409. }
  410. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  411. // TODO: A new test that calls SetRelativeLayout directly is needed.
  412. [Fact]
  413. [TestRespondersDisposed]
  414. public void Pos_Subtract_Operator ()
  415. {
  416. Application.Init (new FakeDriver ());
  417. Toplevel top = Application.Top;
  418. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  419. var field = new TextField { X = 0, Y = 0, Width = 20 };
  420. var count = 20;
  421. List<View> listViews = new ();
  422. for (var i = 0; i < count; i++)
  423. {
  424. field.Text = $"View {i}";
  425. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  426. view.Add (view2);
  427. Assert.Equal ($"View {i}", view2.Text);
  428. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  429. listViews.Add (view2);
  430. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  431. field.Y += 1;
  432. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  433. }
  434. field.KeyDown += (s, k) =>
  435. {
  436. if (k.KeyCode == KeyCode.Enter)
  437. {
  438. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  439. view.Remove (listViews [count - 1]);
  440. listViews [count - 1].Dispose ();
  441. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  442. field.Y -= 1;
  443. count--;
  444. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  445. }
  446. };
  447. Application.Iteration += (s, a) =>
  448. {
  449. while (count > 0)
  450. {
  451. field.NewKeyDownEvent (Key.Enter);
  452. }
  453. Application.RequestStop ();
  454. };
  455. var win = new Window ();
  456. win.Add (view);
  457. win.Add (field);
  458. top.Add (win);
  459. Application.Run (top);
  460. Assert.Equal (0, count);
  461. // Shutdown must be called to safely clean up Application if Init has been called
  462. Application.Shutdown ();
  463. }
  464. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  465. // TODO: A new test that calls SetRelativeLayout directly is needed.
  466. [Fact]
  467. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  468. {
  469. Application.Init (new FakeDriver ());
  470. Toplevel t = Application.Top;
  471. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  472. t.Add (w);
  473. t.Ready += (s, e) =>
  474. {
  475. Assert.Equal (2, w.X = 2);
  476. Assert.Equal (2, w.Y = 2);
  477. };
  478. Application.Iteration += (s, a) => Application.RequestStop ();
  479. Application.Run ();
  480. Application.Shutdown ();
  481. }
  482. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  483. // TODO: A new test that calls SetRelativeLayout directly is needed.
  484. [Fact]
  485. public void PosCombine_Referencing_Same_View ()
  486. {
  487. var super = new View { Width = 10, Height = 10, Text = "super" };
  488. var view1 = new View { Width = 2, Height = 2, Text = "view1" };
  489. var view2 = new View { Width = 2, Height = 2, Text = "view2" };
  490. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  491. super.Add (view1, view2);
  492. super.BeginInit ();
  493. super.EndInit ();
  494. Exception exception = Record.Exception (super.LayoutSubviews);
  495. Assert.Null (exception);
  496. Assert.Equal (new Rectangle (0, 0, 10, 10), super.Frame);
  497. Assert.Equal (new Rectangle (0, 0, 2, 2), view1.Frame);
  498. Assert.Equal (new Rectangle (8, 0, 2, 2), view2.Frame);
  499. super.Dispose ();
  500. }
  501. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  502. // TODO: A new test that calls SetRelativeLayout directly is needed.
  503. [Fact]
  504. [TestRespondersDisposed]
  505. public void PosCombine_Will_Throws ()
  506. {
  507. Application.Init (new FakeDriver ());
  508. Toplevel t = Application.Top;
  509. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  510. var f = new FrameView ();
  511. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  512. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  513. f.Add (v1); // v2 not added
  514. w.Add (f);
  515. t.Add (w);
  516. f.X = Pos.X (v2) - Pos.X (v1);
  517. f.Y = Pos.Y (v2) - Pos.Y (v1);
  518. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  519. Application.Shutdown ();
  520. v2.Dispose ();
  521. }
  522. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  523. // TODO: A new test that calls SetRelativeLayout directly is needed.
  524. [Theory]
  525. [AutoInitShutdown]
  526. [InlineData (true)]
  527. [InlineData (false)]
  528. public void PosPercentPlusOne (bool testHorizontal)
  529. {
  530. var container = new View { Width = 100, Height = 100 };
  531. var view = new View
  532. {
  533. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  534. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  535. Width = 10,
  536. Height = 10
  537. };
  538. container.Add (view);
  539. Application.Top.Add (container);
  540. Application.Top.LayoutSubviews ();
  541. Assert.Equal (100, container.Frame.Width);
  542. Assert.Equal (100, container.Frame.Height);
  543. if (testHorizontal)
  544. {
  545. Assert.Equal (61, view.Frame.X);
  546. Assert.Equal (1, view.Frame.Y);
  547. }
  548. else
  549. {
  550. Assert.Equal (1, view.Frame.X);
  551. Assert.Equal (61, view.Frame.Y);
  552. }
  553. }
  554. // TODO: Test Left, Top, Right bottom Equal
  555. /// <summary>Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations</summary>
  556. [Fact]
  557. [TestRespondersDisposed]
  558. public void PosSide_SetsValue ()
  559. {
  560. string side; // used in format string
  561. var testRect = Rectangle.Empty;
  562. var testInt = 0;
  563. Pos pos;
  564. // Pos.Left
  565. side = "x";
  566. testInt = 0;
  567. testRect = Rectangle.Empty;
  568. pos = Pos.Left (new View ());
  569. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  570. pos = Pos.Left (new View { Frame = testRect });
  571. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  572. testRect = new Rectangle (1, 2, 3, 4);
  573. pos = Pos.Left (new View { Frame = testRect });
  574. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  575. // Pos.Left(win) + 0
  576. pos = Pos.Left (new View { Frame = testRect }) + testInt;
  577. Assert.Equal (
  578. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  579. pos.ToString ()
  580. );
  581. testInt = 1;
  582. // Pos.Left(win) +1
  583. pos = Pos.Left (new View { Frame = testRect }) + testInt;
  584. Assert.Equal (
  585. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  586. pos.ToString ()
  587. );
  588. testInt = -1;
  589. // Pos.Left(win) -1
  590. pos = Pos.Left (new View { Frame = testRect }) - testInt;
  591. Assert.Equal (
  592. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  593. pos.ToString ()
  594. );
  595. // Pos.X
  596. side = "x";
  597. testInt = 0;
  598. testRect = Rectangle.Empty;
  599. pos = Pos.X (new View ());
  600. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  601. pos = Pos.X (new View { Frame = testRect });
  602. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  603. testRect = new Rectangle (1, 2, 3, 4);
  604. pos = Pos.X (new View { Frame = testRect });
  605. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  606. // Pos.X(win) + 0
  607. pos = Pos.X (new View { Frame = testRect }) + testInt;
  608. Assert.Equal (
  609. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  610. pos.ToString ()
  611. );
  612. testInt = 1;
  613. // Pos.X(win) +1
  614. pos = Pos.X (new View { Frame = testRect }) + testInt;
  615. Assert.Equal (
  616. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  617. pos.ToString ()
  618. );
  619. testInt = -1;
  620. // Pos.X(win) -1
  621. pos = Pos.X (new View { Frame = testRect }) - testInt;
  622. Assert.Equal (
  623. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  624. pos.ToString ()
  625. );
  626. // Pos.Top
  627. side = "y";
  628. testInt = 0;
  629. testRect = Rectangle.Empty;
  630. pos = Pos.Top (new View ());
  631. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  632. pos = Pos.Top (new View { Frame = testRect });
  633. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  634. testRect = new Rectangle (1, 2, 3, 4);
  635. pos = Pos.Top (new View { Frame = testRect });
  636. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  637. // Pos.Top(win) + 0
  638. pos = Pos.Top (new View { Frame = testRect }) + testInt;
  639. Assert.Equal (
  640. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  641. pos.ToString ()
  642. );
  643. testInt = 1;
  644. // Pos.Top(win) +1
  645. pos = Pos.Top (new View { Frame = testRect }) + testInt;
  646. Assert.Equal (
  647. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  648. pos.ToString ()
  649. );
  650. testInt = -1;
  651. // Pos.Top(win) -1
  652. pos = Pos.Top (new View { Frame = testRect }) - testInt;
  653. Assert.Equal (
  654. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  655. pos.ToString ()
  656. );
  657. // Pos.Y
  658. side = "y";
  659. testInt = 0;
  660. testRect = Rectangle.Empty;
  661. pos = Pos.Y (new View ());
  662. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  663. pos = Pos.Y (new View { Frame = testRect });
  664. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  665. testRect = new Rectangle (1, 2, 3, 4);
  666. pos = Pos.Y (new View { Frame = testRect });
  667. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  668. // Pos.Y(win) + 0
  669. pos = Pos.Y (new View { Frame = testRect }) + testInt;
  670. Assert.Equal (
  671. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  672. pos.ToString ()
  673. );
  674. testInt = 1;
  675. // Pos.Y(win) +1
  676. pos = Pos.Y (new View { Frame = testRect }) + testInt;
  677. Assert.Equal (
  678. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  679. pos.ToString ()
  680. );
  681. testInt = -1;
  682. // Pos.Y(win) -1
  683. pos = Pos.Y (new View { Frame = testRect }) - testInt;
  684. Assert.Equal (
  685. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  686. pos.ToString ()
  687. );
  688. // Pos.Bottom
  689. side = "bottom";
  690. testRect = Rectangle.Empty;
  691. testInt = 0;
  692. pos = Pos.Bottom (new View ());
  693. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  694. pos = Pos.Bottom (new View { Frame = testRect });
  695. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  696. testRect = new Rectangle (1, 2, 3, 4);
  697. pos = Pos.Bottom (new View { Frame = testRect });
  698. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  699. // Pos.Bottom(win) + 0
  700. pos = Pos.Bottom (new View { Frame = testRect }) + testInt;
  701. Assert.Equal (
  702. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  703. pos.ToString ()
  704. );
  705. testInt = 1;
  706. // Pos.Bottom(win) +1
  707. pos = Pos.Bottom (new View { Frame = testRect }) + testInt;
  708. Assert.Equal (
  709. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  710. pos.ToString ()
  711. );
  712. testInt = -1;
  713. // Pos.Bottom(win) -1
  714. pos = Pos.Bottom (new View { Frame = testRect }) - testInt;
  715. Assert.Equal (
  716. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  717. pos.ToString ()
  718. );
  719. #if DEBUG_IDISPOSABLE
  720. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  721. Responder.Instances.Clear ();
  722. #endif
  723. }
  724. [Fact]
  725. public void SetSide_Null_Throws ()
  726. {
  727. Pos pos = Pos.Left (null);
  728. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  729. pos = Pos.X (null);
  730. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  731. pos = Pos.Top (null);
  732. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  733. pos = Pos.Y (null);
  734. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  735. pos = Pos.Bottom (null);
  736. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  737. pos = Pos.Right (null);
  738. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  739. }
  740. }