PosTests.cs 21 KB

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