PosTests.cs 32 KB

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