PosTests.cs 22 KB

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