PosTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 = new ();
  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 = new ();
  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. (Toplevel top, 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. var top = new Toplevel ();
  258. top.Add (win);
  259. var button = new Button { X = Pos.Center (), Text = "button" };
  260. win.Add (button);
  261. return (top, win, button);
  262. }
  263. RunState rs;
  264. void cleanup (RunState rs)
  265. {
  266. // Cleanup
  267. Application.End (rs);
  268. Application.Top.Dispose ();
  269. // Shutdown must be called to safely clean up Application if Init has been called
  270. Application.Shutdown ();
  271. }
  272. // Test cases:
  273. (Toplevel top, Window win, Button button) app = setup ();
  274. app.button.Y = Pos.Left (app.win);
  275. rs = Application.Begin (app.top);
  276. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  277. Application.RunLoop (rs);
  278. cleanup (rs);
  279. app = setup ();
  280. app.button.Y = Pos.X (app.win);
  281. rs = Application.Begin (app.top);
  282. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  283. Application.RunLoop (rs);
  284. cleanup (rs);
  285. app = setup ();
  286. app.button.Y = Pos.Top (app.win);
  287. rs = Application.Begin (app.top);
  288. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  289. Application.RunLoop (rs);
  290. cleanup (rs);
  291. app = setup ();
  292. app.button.Y = Pos.Y (app.win);
  293. rs = Application.Begin (app.top);
  294. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  295. Application.RunLoop (rs);
  296. cleanup (rs);
  297. app = setup ();
  298. app.button.Y = Pos.Bottom (app.win);
  299. rs = Application.Begin (app.top);
  300. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  301. Application.RunLoop (rs);
  302. cleanup (rs);
  303. app = setup ();
  304. app.button.Y = Pos.Right (app.win);
  305. rs = Application.Begin (app.top);
  306. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  307. Application.RunLoop (rs);
  308. cleanup (rs);
  309. }
  310. [Fact]
  311. public void New_Works ()
  312. {
  313. var pos = new Pos ();
  314. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  315. }
  316. [Fact]
  317. public void Percent_Equal ()
  318. {
  319. float n1 = 0;
  320. float n2 = 0;
  321. Pos pos1 = Pos.Percent (n1);
  322. Pos pos2 = Pos.Percent (n2);
  323. Assert.Equal (pos1, pos2);
  324. n1 = n2 = 1;
  325. pos1 = Pos.Percent (n1);
  326. pos2 = Pos.Percent (n2);
  327. Assert.Equal (pos1, pos2);
  328. n1 = n2 = 0.5f;
  329. pos1 = Pos.Percent (n1);
  330. pos2 = Pos.Percent (n2);
  331. Assert.Equal (pos1, pos2);
  332. n1 = n2 = 100f;
  333. pos1 = Pos.Percent (n1);
  334. pos2 = Pos.Percent (n2);
  335. Assert.Equal (pos1, pos2);
  336. n1 = 0;
  337. n2 = 1;
  338. pos1 = Pos.Percent (n1);
  339. pos2 = Pos.Percent (n2);
  340. Assert.NotEqual (pos1, pos2);
  341. n1 = 0.5f;
  342. n2 = 1.5f;
  343. pos1 = Pos.Percent (n1);
  344. pos2 = Pos.Percent (n2);
  345. Assert.NotEqual (pos1, pos2);
  346. }
  347. [Fact]
  348. public void Percent_SetsValue ()
  349. {
  350. float f = 0;
  351. Pos pos = Pos.Percent (f);
  352. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  353. f = 0.5F;
  354. pos = Pos.Percent (f);
  355. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  356. f = 100;
  357. pos = Pos.Percent (f);
  358. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  359. }
  360. [Fact]
  361. public void Percent_ThrowsOnIvalid ()
  362. {
  363. Pos pos = Pos.Percent (0);
  364. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  365. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  366. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  367. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  368. }
  369. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  370. // TODO: A new test that calls SetRelativeLayout directly is needed.
  371. [Fact]
  372. [TestRespondersDisposed]
  373. public void Pos_Add_Operator ()
  374. {
  375. Application.Init (new FakeDriver ());
  376. Toplevel top = new ();
  377. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  378. var field = new TextField { X = 0, Y = 0, Width = 20 };
  379. var count = 0;
  380. field.KeyDown += (s, k) =>
  381. {
  382. if (k.KeyCode == KeyCode.Enter)
  383. {
  384. field.Text = $"View {count}";
  385. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  386. view.Add (view2);
  387. Assert.Equal ($"View {count}", view2.Text);
  388. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  389. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  390. field.Y += 1;
  391. count++;
  392. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  393. }
  394. };
  395. Application.Iteration += (s, a) =>
  396. {
  397. while (count < 20)
  398. {
  399. field.NewKeyDownEvent (Key.Enter);
  400. }
  401. Application.RequestStop ();
  402. };
  403. var win = new Window ();
  404. win.Add (view);
  405. win.Add (field);
  406. top.Add (win);
  407. Application.Run (top);
  408. Assert.Equal (20, count);
  409. top.Dispose ();
  410. // Shutdown must be called to safely clean up Application if Init has been called
  411. Application.Shutdown ();
  412. }
  413. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  414. // TODO: A new test that calls SetRelativeLayout directly is needed.
  415. [Fact]
  416. [TestRespondersDisposed]
  417. public void Pos_Subtract_Operator ()
  418. {
  419. Application.Init (new FakeDriver ());
  420. Toplevel top = new ();
  421. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  422. var field = new TextField { X = 0, Y = 0, Width = 20 };
  423. var count = 20;
  424. List<View> listViews = new ();
  425. for (var i = 0; i < count; i++)
  426. {
  427. field.Text = $"View {i}";
  428. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  429. view.Add (view2);
  430. Assert.Equal ($"View {i}", view2.Text);
  431. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  432. listViews.Add (view2);
  433. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  434. field.Y += 1;
  435. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  436. }
  437. field.KeyDown += (s, k) =>
  438. {
  439. if (k.KeyCode == KeyCode.Enter)
  440. {
  441. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  442. view.Remove (listViews [count - 1]);
  443. listViews [count - 1].Dispose ();
  444. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  445. field.Y -= 1;
  446. count--;
  447. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  448. }
  449. };
  450. Application.Iteration += (s, a) =>
  451. {
  452. while (count > 0)
  453. {
  454. field.NewKeyDownEvent (Key.Enter);
  455. }
  456. Application.RequestStop ();
  457. };
  458. var win = new Window ();
  459. win.Add (view);
  460. win.Add (field);
  461. top.Add (win);
  462. Application.Run (top);
  463. Assert.Equal (0, count);
  464. top.Dispose ();
  465. // Shutdown must be called to safely clean up Application if Init has been called
  466. Application.Shutdown ();
  467. }
  468. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  469. // TODO: A new test that calls SetRelativeLayout directly is needed.
  470. [Fact]
  471. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  472. {
  473. Application.Init (new FakeDriver ());
  474. Toplevel t = new ();
  475. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  476. t.Add (w);
  477. t.Ready += (s, e) =>
  478. {
  479. Assert.Equal (2, w.X = 2);
  480. Assert.Equal (2, w.Y = 2);
  481. };
  482. Application.Iteration += (s, a) => Application.RequestStop ();
  483. Application.Run (t);
  484. t.Dispose ();
  485. Application.Shutdown ();
  486. }
  487. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  488. // TODO: A new test that calls SetRelativeLayout directly is needed.
  489. [Fact]
  490. public void PosCombine_Referencing_Same_View ()
  491. {
  492. var super = new View { Width = 10, Height = 10, Text = "super" };
  493. var view1 = new View { Width = 2, Height = 2, Text = "view1" };
  494. var view2 = new View { Width = 2, Height = 2, Text = "view2" };
  495. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  496. super.Add (view1, view2);
  497. super.BeginInit ();
  498. super.EndInit ();
  499. Exception exception = Record.Exception (super.LayoutSubviews);
  500. Assert.Null (exception);
  501. Assert.Equal (new Rectangle (0, 0, 10, 10), super.Frame);
  502. Assert.Equal (new Rectangle (0, 0, 2, 2), view1.Frame);
  503. Assert.Equal (new Rectangle (8, 0, 2, 2), view2.Frame);
  504. super.Dispose ();
  505. }
  506. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  507. // TODO: A new test that calls SetRelativeLayout directly is needed.
  508. [Fact]
  509. [TestRespondersDisposed]
  510. public void PosCombine_Will_Throws ()
  511. {
  512. Application.Init (new FakeDriver ());
  513. Toplevel t = new ();
  514. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  515. var f = new FrameView ();
  516. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  517. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  518. f.Add (v1); // v2 not added
  519. w.Add (f);
  520. t.Add (w);
  521. f.X = Pos.X (v2) - Pos.X (v1);
  522. f.Y = Pos.Y (v2) - Pos.Y (v1);
  523. Assert.Throws<InvalidOperationException> (() => Application.Run (t));
  524. t.Dispose ();
  525. Application.Shutdown ();
  526. v2.Dispose ();
  527. }
  528. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  529. // TODO: A new test that calls SetRelativeLayout directly is needed.
  530. [Theory]
  531. [AutoInitShutdown]
  532. [InlineData (true)]
  533. [InlineData (false)]
  534. public void PosPercentPlusOne (bool testHorizontal)
  535. {
  536. var container = new View { Width = 100, Height = 100 };
  537. var view = new View
  538. {
  539. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  540. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  541. Width = 10,
  542. Height = 10
  543. };
  544. container.Add (view);
  545. var top = new Toplevel ();
  546. top.Add (container);
  547. top.LayoutSubviews ();
  548. Assert.Equal (100, container.Frame.Width);
  549. Assert.Equal (100, container.Frame.Height);
  550. if (testHorizontal)
  551. {
  552. Assert.Equal (61, view.Frame.X);
  553. Assert.Equal (1, view.Frame.Y);
  554. }
  555. else
  556. {
  557. Assert.Equal (1, view.Frame.X);
  558. Assert.Equal (61, view.Frame.Y);
  559. }
  560. }
  561. // TODO: Test Left, Top, Right bottom Equal
  562. /// <summary>Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations</summary>
  563. [Fact]
  564. [TestRespondersDisposed]
  565. public void PosSide_SetsValue ()
  566. {
  567. string side; // used in format string
  568. var testRect = Rectangle.Empty;
  569. var testInt = 0;
  570. Pos pos;
  571. // Pos.Left
  572. side = "x";
  573. testInt = 0;
  574. testRect = Rectangle.Empty;
  575. pos = Pos.Left (new View ());
  576. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  577. pos = Pos.Left (new View { Frame = testRect });
  578. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  579. testRect = new Rectangle (1, 2, 3, 4);
  580. pos = Pos.Left (new View { Frame = testRect });
  581. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  582. // Pos.Left(win) + 0
  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. testInt = -1;
  596. // Pos.Left(win) -1
  597. pos = Pos.Left (new View { Frame = testRect }) - testInt;
  598. Assert.Equal (
  599. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  600. pos.ToString ()
  601. );
  602. // Pos.X
  603. side = "x";
  604. testInt = 0;
  605. testRect = Rectangle.Empty;
  606. pos = Pos.X (new View ());
  607. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  608. pos = Pos.X (new View { Frame = testRect });
  609. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  610. testRect = new Rectangle (1, 2, 3, 4);
  611. pos = Pos.X (new View { Frame = testRect });
  612. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  613. // Pos.X(win) + 0
  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. testInt = -1;
  627. // Pos.X(win) -1
  628. pos = Pos.X (new View { Frame = testRect }) - testInt;
  629. Assert.Equal (
  630. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  631. pos.ToString ()
  632. );
  633. // Pos.Top
  634. side = "y";
  635. testInt = 0;
  636. testRect = Rectangle.Empty;
  637. pos = Pos.Top (new View ());
  638. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  639. pos = Pos.Top (new View { Frame = testRect });
  640. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  641. testRect = new Rectangle (1, 2, 3, 4);
  642. pos = Pos.Top (new View { Frame = testRect });
  643. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  644. // Pos.Top(win) + 0
  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. testInt = -1;
  658. // Pos.Top(win) -1
  659. pos = Pos.Top (new View { Frame = testRect }) - testInt;
  660. Assert.Equal (
  661. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  662. pos.ToString ()
  663. );
  664. // Pos.Y
  665. side = "y";
  666. testInt = 0;
  667. testRect = Rectangle.Empty;
  668. pos = Pos.Y (new View ());
  669. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  670. pos = Pos.Y (new View { Frame = testRect });
  671. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  672. testRect = new Rectangle (1, 2, 3, 4);
  673. pos = Pos.Y (new View { Frame = testRect });
  674. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  675. // Pos.Y(win) + 0
  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. testInt = -1;
  689. // Pos.Y(win) -1
  690. pos = Pos.Y (new View { Frame = testRect }) - testInt;
  691. Assert.Equal (
  692. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  693. pos.ToString ()
  694. );
  695. // Pos.Bottom
  696. side = "bottom";
  697. testRect = Rectangle.Empty;
  698. testInt = 0;
  699. pos = Pos.Bottom (new View ());
  700. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  701. pos = Pos.Bottom (new View { Frame = testRect });
  702. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  703. testRect = new Rectangle (1, 2, 3, 4);
  704. pos = Pos.Bottom (new View { Frame = testRect });
  705. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  706. // Pos.Bottom(win) + 0
  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. testInt = -1;
  720. // Pos.Bottom(win) -1
  721. pos = Pos.Bottom (new View { Frame = testRect }) - testInt;
  722. Assert.Equal (
  723. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  724. pos.ToString ()
  725. );
  726. #if DEBUG_IDISPOSABLE
  727. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  728. Responder.Instances.Clear ();
  729. #endif
  730. }
  731. [Fact]
  732. public void SetSide_Null_Throws ()
  733. {
  734. Pos pos = Pos.Left (null);
  735. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  736. pos = Pos.X (null);
  737. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  738. pos = Pos.Top (null);
  739. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  740. pos = Pos.Y (null);
  741. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  742. pos = Pos.Bottom (null);
  743. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  744. pos = Pos.Right (null);
  745. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  746. }
  747. }