PosTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. [AutoInitShutdown]
  89. public void Bottom_Equal_Inside_Window ()
  90. {
  91. var win = new Window ();
  92. var label = new Label ("This should be the last line.") {
  93. ColorScheme = Colors.Menu,
  94. Width = Dim.Fill (),
  95. X = 0,
  96. Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border
  97. };
  98. win.Add (label);
  99. var top = Application.Top;
  100. top.Add (win);
  101. var rs = Application.Begin (top);
  102. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  103. Assert.True (label.AutoSize);
  104. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  105. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  106. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  107. string expected = @"
  108. ┌──────────────────────────────────────┐
  109. │ │
  110. │ │
  111. │ │
  112. │ │
  113. │ │
  114. │ │
  115. │ │
  116. │This should be the last line. │
  117. └──────────────────────────────────────┘
  118. ";
  119. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  120. Application.End (rs);
  121. }
  122. [Fact]
  123. [AutoInitShutdown]
  124. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  125. {
  126. var win = new Window ();
  127. var label = new Label ("This should be the last line.") {
  128. ColorScheme = Colors.Menu,
  129. Width = Dim.Fill (),
  130. X = 0, // keep unit test focused; don't use Center here
  131. Y = Pos.AnchorEnd (1)
  132. };
  133. win.Add (label);
  134. var top = Application.Top;
  135. top.Add (win);
  136. var rs = Application.Begin (top);
  137. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  138. Assert.True (label.AutoSize);
  139. Assert.Equal (29, label.Text.Length);
  140. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  141. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  142. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  143. string expected = @"
  144. ┌──────────────────────────────────────┐
  145. │ │
  146. │ │
  147. │ │
  148. │ │
  149. │ │
  150. │ │
  151. │ │
  152. │This should be the last line. │
  153. └──────────────────────────────────────┘
  154. ";
  155. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  156. Application.End (rs);
  157. }
  158. [Fact]
  159. [AutoInitShutdown]
  160. public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  161. {
  162. var win = new Window ();
  163. var label = new Label ("This should be the last line.") {
  164. ColorScheme = Colors.Menu,
  165. Width = Dim.Fill (),
  166. X = 0,
  167. Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
  168. };
  169. win.Add (label);
  170. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  171. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  172. var top = Application.Top;
  173. top.Add (win, menu, status);
  174. var rs = Application.Begin (top);
  175. Assert.True (label.AutoSize);
  176. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  177. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  178. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  179. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  180. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  181. string expected = @"
  182. Menu
  183. ┌──────────────────────────────────────────────────────────────────────────────┐
  184. │ │
  185. │ │
  186. │ │
  187. │ │
  188. │ │
  189. │ │
  190. │ │
  191. │ │
  192. │ │
  193. │ │
  194. │ │
  195. │ │
  196. │ │
  197. │ │
  198. │ │
  199. │ │
  200. │ │
  201. │ │
  202. │ │
  203. │ │
  204. │This should be the last line. │
  205. └──────────────────────────────────────────────────────────────────────────────┘
  206. F1 Help
  207. ";
  208. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  209. Application.End (rs);
  210. }
  211. [Fact]
  212. [AutoInitShutdown]
  213. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  214. {
  215. var win = new Window ();
  216. var label = new Label ("This should be the last line.") {
  217. ColorScheme = Colors.Menu,
  218. Width = Dim.Fill (),
  219. X = 0,
  220. Y = Pos.AnchorEnd (1)
  221. };
  222. win.Add (label);
  223. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  224. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  225. var top = Application.Top;
  226. top.Add (win, menu, status);
  227. var rs = Application.Begin (top);
  228. Assert.True (label.AutoSize);
  229. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  230. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  231. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  232. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  233. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  234. string expected = @"
  235. Menu
  236. ┌──────────────────────────────────────────────────────────────────────────────┐
  237. │ │
  238. │ │
  239. │ │
  240. │ │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. │ │
  246. │ │
  247. │ │
  248. │ │
  249. │ │
  250. │ │
  251. │ │
  252. │ │
  253. │ │
  254. │ │
  255. │ │
  256. │ │
  257. │This should be the last line. │
  258. └──────────────────────────────────────────────────────────────────────────────┘
  259. F1 Help
  260. ";
  261. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  262. Application.End (rs);
  263. }
  264. [Fact]
  265. public void AnchorEnd_Negative_Throws ()
  266. {
  267. Pos pos;
  268. int n = -1;
  269. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  270. }
  271. [Fact]
  272. public void At_SetsValue ()
  273. {
  274. var pos = Pos.At (0);
  275. Assert.Equal ("Absolute(0)", pos.ToString ());
  276. pos = Pos.At (5);
  277. Assert.Equal ("Absolute(5)", pos.ToString ());
  278. pos = Pos.At (-1);
  279. Assert.Equal ("Absolute(-1)", pos.ToString ());
  280. }
  281. [Fact]
  282. public void At_Equal ()
  283. {
  284. int n1 = 0;
  285. int n2 = 0;
  286. var pos1 = Pos.At (n1);
  287. var pos2 = Pos.At (n2);
  288. Assert.Equal (pos1, pos2);
  289. }
  290. [Fact]
  291. public void SetSide_Null_Throws ()
  292. {
  293. var pos = Pos.Left (null);
  294. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  295. pos = Pos.X (null);
  296. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  297. pos = Pos.Top (null);
  298. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  299. pos = Pos.Y (null);
  300. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  301. pos = Pos.Bottom (null);
  302. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  303. pos = Pos.Right (null);
  304. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  305. }
  306. // TODO: Test Left, Top, Right bottom Equal
  307. /// <summary>
  308. /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
  309. /// </summary>
  310. [Fact]
  311. [TestRespondersDisposed]
  312. public void PosSide_SetsValue ()
  313. {
  314. string side; // used in format string
  315. var testRect = Rect.Empty;
  316. int testInt = 0;
  317. Pos pos;
  318. // Pos.Left
  319. side = "x";
  320. testInt = 0;
  321. testRect = Rect.Empty;
  322. pos = Pos.Left (new View ());
  323. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  324. pos = Pos.Left (new View (testRect));
  325. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  326. testRect = new Rect (1, 2, 3, 4);
  327. pos = Pos.Left (new View (testRect));
  328. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  329. // Pos.Left(win) + 0
  330. pos = Pos.Left (new View (testRect)) + testInt;
  331. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  332. testInt = 1;
  333. // Pos.Left(win) +1
  334. pos = Pos.Left (new View (testRect)) + testInt;
  335. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  336. testInt = -1;
  337. // Pos.Left(win) -1
  338. pos = Pos.Left (new View (testRect)) - testInt;
  339. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  340. // Pos.X
  341. side = "x";
  342. testInt = 0;
  343. testRect = Rect.Empty;
  344. pos = Pos.X (new View ());
  345. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  346. pos = Pos.X (new View (testRect));
  347. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  348. testRect = new Rect (1, 2, 3, 4);
  349. pos = Pos.X (new View (testRect));
  350. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  351. // Pos.X(win) + 0
  352. pos = Pos.X (new View (testRect)) + testInt;
  353. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  354. testInt = 1;
  355. // Pos.X(win) +1
  356. pos = Pos.X (new View (testRect)) + testInt;
  357. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  358. testInt = -1;
  359. // Pos.X(win) -1
  360. pos = Pos.X (new View (testRect)) - testInt;
  361. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  362. // Pos.Top
  363. side = "y";
  364. testInt = 0;
  365. testRect = Rect.Empty;
  366. pos = Pos.Top (new View ());
  367. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  368. pos = Pos.Top (new View (testRect));
  369. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  370. testRect = new Rect (1, 2, 3, 4);
  371. pos = Pos.Top (new View (testRect));
  372. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  373. // Pos.Top(win) + 0
  374. pos = Pos.Top (new View (testRect)) + testInt;
  375. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  376. testInt = 1;
  377. // Pos.Top(win) +1
  378. pos = Pos.Top (new View (testRect)) + testInt;
  379. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  380. testInt = -1;
  381. // Pos.Top(win) -1
  382. pos = Pos.Top (new View (testRect)) - testInt;
  383. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  384. // Pos.Y
  385. side = "y";
  386. testInt = 0;
  387. testRect = Rect.Empty;
  388. pos = Pos.Y (new View ());
  389. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  390. pos = Pos.Y (new View (testRect));
  391. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  392. testRect = new Rect (1, 2, 3, 4);
  393. pos = Pos.Y (new View (testRect));
  394. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  395. // Pos.Y(win) + 0
  396. pos = Pos.Y (new View (testRect)) + testInt;
  397. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  398. testInt = 1;
  399. // Pos.Y(win) +1
  400. pos = Pos.Y (new View (testRect)) + testInt;
  401. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  402. testInt = -1;
  403. // Pos.Y(win) -1
  404. pos = Pos.Y (new View (testRect)) - testInt;
  405. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  406. // Pos.Bottom
  407. side = "bottom";
  408. testRect = Rect.Empty;
  409. testInt = 0;
  410. pos = Pos.Bottom (new View ());
  411. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  412. pos = Pos.Bottom (new View (testRect));
  413. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  414. testRect = new Rect (1, 2, 3, 4);
  415. pos = Pos.Bottom (new View (testRect));
  416. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  417. // Pos.Bottom(win) + 0
  418. pos = Pos.Bottom (new View (testRect)) + testInt;
  419. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  420. testInt = 1;
  421. // Pos.Bottom(win) +1
  422. pos = Pos.Bottom (new View (testRect)) + testInt;
  423. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  424. testInt = -1;
  425. // Pos.Bottom(win) -1
  426. pos = Pos.Bottom (new View (testRect)) - testInt;
  427. Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  428. #if DEBUG_IDISPOSABLE
  429. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  430. Responder.Instances.Clear ();
  431. #endif
  432. }
  433. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  434. [Fact]
  435. [TestRespondersDisposed]
  436. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  437. {
  438. // Setup Fake driver
  439. (Window win, Button button) setup ()
  440. {
  441. Application.Init (new FakeDriver ());
  442. Application.Iteration += (s, a) => {
  443. Application.RequestStop ();
  444. };
  445. var win = new Window () {
  446. X = 0,
  447. Y = 0,
  448. Width = Dim.Fill (),
  449. Height = Dim.Fill ()
  450. };
  451. Application.Top.Add (win);
  452. var button = new Button ("button") {
  453. X = Pos.Center ()
  454. };
  455. win.Add (button);
  456. return (win, button);
  457. }
  458. RunState rs;
  459. void cleanup (RunState rs)
  460. {
  461. // Cleanup
  462. Application.End (rs);
  463. // Shutdown must be called to safely clean up Application if Init has been called
  464. Application.Shutdown ();
  465. }
  466. // Test cases:
  467. var app = setup ();
  468. app.button.Y = Pos.Left (app.win);
  469. rs = Application.Begin (Application.Top);
  470. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  471. Application.RunLoop (rs);
  472. cleanup (rs);
  473. app = setup ();
  474. app.button.Y = Pos.X (app.win);
  475. rs = Application.Begin (Application.Top);
  476. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  477. Application.RunLoop (rs);
  478. cleanup (rs);
  479. app = setup ();
  480. app.button.Y = Pos.Top (app.win);
  481. rs = Application.Begin (Application.Top);
  482. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  483. Application.RunLoop (rs);
  484. cleanup (rs);
  485. app = setup ();
  486. app.button.Y = Pos.Y (app.win);
  487. rs = Application.Begin (Application.Top);
  488. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  489. Application.RunLoop (rs);
  490. cleanup (rs);
  491. app = setup ();
  492. app.button.Y = Pos.Bottom (app.win);
  493. rs = Application.Begin (Application.Top);
  494. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  495. Application.RunLoop (rs);
  496. cleanup (rs);
  497. app = setup ();
  498. app.button.Y = Pos.Right (app.win);
  499. rs = Application.Begin (Application.Top);
  500. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  501. Application.RunLoop (rs);
  502. cleanup (rs);
  503. }
  504. [Fact]
  505. public void Center_SetsValue ()
  506. {
  507. var pos = Pos.Center ();
  508. Assert.Equal ("Center", pos.ToString ());
  509. }
  510. [Fact]
  511. public void Percent_SetsValue ()
  512. {
  513. float f = 0;
  514. var pos = Pos.Percent (f);
  515. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  516. f = 0.5F;
  517. pos = Pos.Percent (f);
  518. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  519. f = 100;
  520. pos = Pos.Percent (f);
  521. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  522. }
  523. [Fact]
  524. public void Percent_Equal ()
  525. {
  526. float n1 = 0;
  527. float n2 = 0;
  528. var pos1 = Pos.Percent (n1);
  529. var pos2 = Pos.Percent (n2);
  530. Assert.Equal (pos1, pos2);
  531. n1 = n2 = 1;
  532. pos1 = Pos.Percent (n1);
  533. pos2 = Pos.Percent (n2);
  534. Assert.Equal (pos1, pos2);
  535. n1 = n2 = 0.5f;
  536. pos1 = Pos.Percent (n1);
  537. pos2 = Pos.Percent (n2);
  538. Assert.Equal (pos1, pos2);
  539. n1 = n2 = 100f;
  540. pos1 = Pos.Percent (n1);
  541. pos2 = Pos.Percent (n2);
  542. Assert.Equal (pos1, pos2);
  543. n1 = 0;
  544. n2 = 1;
  545. pos1 = Pos.Percent (n1);
  546. pos2 = Pos.Percent (n2);
  547. Assert.NotEqual (pos1, pos2);
  548. n1 = 0.5f;
  549. n2 = 1.5f;
  550. pos1 = Pos.Percent (n1);
  551. pos2 = Pos.Percent (n2);
  552. Assert.NotEqual (pos1, pos2);
  553. }
  554. [Fact]
  555. public void Percent_ThrowsOnIvalid ()
  556. {
  557. var pos = Pos.Percent (0);
  558. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  559. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  560. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  561. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  562. }
  563. [Fact]
  564. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  565. {
  566. Application.Init (new FakeDriver ());
  567. var t = Application.Top;
  568. var w = new Window (new Rect (1, 2, 4, 5));
  569. t.Add (w);
  570. t.Ready += (s, e) => {
  571. Assert.Equal (2, w.X = 2);
  572. Assert.Equal (2, w.Y = 2);
  573. };
  574. Application.Iteration += (s, a) => Application.RequestStop ();
  575. Application.Run ();
  576. Application.Shutdown ();
  577. }
  578. [Fact]
  579. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  580. {
  581. Application.Init (new FakeDriver ());
  582. var t = Application.Top;
  583. var w = new Window () {
  584. X = Pos.Left (t) + 2,
  585. Y = Pos.At (2)
  586. };
  587. var v = new View () {
  588. X = Pos.Center (),
  589. Y = Pos.Percent (10)
  590. };
  591. w.Add (v);
  592. t.Add (w);
  593. t.Ready += (s, e) => {
  594. v.LayoutStyle = LayoutStyle.Absolute;
  595. Assert.Equal (2, v.X = 2);
  596. Assert.Equal (2, v.Y = 2);
  597. };
  598. Application.Iteration += (s, a) => Application.RequestStop ();
  599. Application.Run ();
  600. Application.Shutdown ();
  601. }
  602. // DONE: Test PosCombine
  603. // DONE: Test operators
  604. // BUGBUG: v2 - This test is bogus. v1 and references it's superview's (f) superview (w).
  605. //[Fact]
  606. //public void PosCombine_Do_Not_Throws ()
  607. //{
  608. // Application.Init (new FakeDriver ());
  609. // var w = new Window () {
  610. // X = Pos.Left (Application.Top) + 2,
  611. // Y = Pos.Top (Application.Top) + 2
  612. // };
  613. // var f = new FrameView ();
  614. // var v1 = new View () {
  615. // X = Pos.Left (w) + 2,
  616. // Y = Pos.Top (w) + 2
  617. // };
  618. // var v2 = new View () {
  619. // X = Pos.Left (v1) + 2,
  620. // Y = Pos.Top (v1) + 2
  621. // };
  622. // f.Add (v1, v2);
  623. // w.Add (f);
  624. // Application.Top.Add (w);
  625. // f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  626. // f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  627. // Application.Top.LayoutComplete += (s, e) => {
  628. // Assert.Equal (0, Application.Top.Frame.X);
  629. // Assert.Equal (0, Application.Top.Frame.Y);
  630. // Assert.Equal (2, w.Frame.X);
  631. // Assert.Equal (2, w.Frame.Y);
  632. // Assert.Equal (2, f.Frame.X);
  633. // Assert.Equal (2, f.Frame.Y);
  634. // Assert.Equal (4, v1.Frame.X);
  635. // Assert.Equal (4, v1.Frame.Y);
  636. // Assert.Equal (6, v2.Frame.X);
  637. // Assert.Equal (6, v2.Frame.Y);
  638. // };
  639. // Application.Iteration += (s, a) => Application.RequestStop ();
  640. // Application.Run ();
  641. // Application.Shutdown ();
  642. //}
  643. [Fact]
  644. [TestRespondersDisposed]
  645. public void PosCombine_Will_Throws ()
  646. {
  647. Application.Init (new FakeDriver ());
  648. var t = Application.Top;
  649. var w = new Window () {
  650. X = Pos.Left (t) + 2,
  651. Y = Pos.Top (t) + 2
  652. };
  653. var f = new FrameView ();
  654. var v1 = new View () {
  655. X = Pos.Left (w) + 2,
  656. Y = Pos.Top (w) + 2
  657. };
  658. var v2 = new View () {
  659. X = Pos.Left (v1) + 2,
  660. Y = Pos.Top (v1) + 2
  661. };
  662. f.Add (v1); // v2 not added
  663. w.Add (f);
  664. t.Add (w);
  665. f.X = Pos.X (v2) - Pos.X (v1);
  666. f.Y = Pos.Y (v2) - Pos.Y (v1);
  667. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  668. Application.Shutdown ();
  669. v2.Dispose ();
  670. }
  671. [Fact]
  672. [TestRespondersDisposed]
  673. public void Pos_Add_Operator ()
  674. {
  675. Application.Init (new FakeDriver ());
  676. var top = Application.Top;
  677. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  678. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  679. int count = 0;
  680. field.KeyDown += (s, k) => {
  681. if (k.KeyCode == KeyCode.Enter) {
  682. field.Text = $"Label {count}";
  683. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  684. view.Add (label);
  685. Assert.Equal ($"Label {count}", label.Text);
  686. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  687. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  688. field.Y += 1;
  689. count++;
  690. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  691. }
  692. };
  693. Application.Iteration += (s, a) => {
  694. while (count < 20) {
  695. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  696. }
  697. Application.RequestStop ();
  698. };
  699. var win = new Window ();
  700. win.Add (view);
  701. win.Add (field);
  702. top.Add (win);
  703. Application.Run (top);
  704. Assert.Equal (20, count);
  705. // Shutdown must be called to safely clean up Application if Init has been called
  706. Application.Shutdown ();
  707. }
  708. [Fact]
  709. [TestRespondersDisposed]
  710. public void Pos_Subtract_Operator ()
  711. {
  712. Application.Init (new FakeDriver ());
  713. var top = Application.Top;
  714. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  715. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  716. int count = 20;
  717. var listLabels = new List<Label> ();
  718. for (int i = 0; i < count; i++) {
  719. field.Text = $"Label {i}";
  720. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  721. view.Add (label);
  722. Assert.Equal ($"Label {i}", label.Text);
  723. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  724. listLabels.Add (label);
  725. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  726. field.Y += 1;
  727. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  728. }
  729. field.KeyDown += (s, k) => {
  730. if (k.KeyCode == KeyCode.Enter) {
  731. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  732. view.Remove (listLabels [count - 1]);
  733. listLabels [count - 1].Dispose ();
  734. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  735. field.Y -= 1;
  736. count--;
  737. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  738. }
  739. };
  740. Application.Iteration += (s, a) => {
  741. while (count > 0) {
  742. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  743. }
  744. Application.RequestStop ();
  745. };
  746. var win = new Window ();
  747. win.Add (view);
  748. win.Add (field);
  749. top.Add (win);
  750. Application.Run (top);
  751. Assert.Equal (0, count);
  752. // Shutdown must be called to safely clean up Application if Init has been called
  753. Application.Shutdown ();
  754. }
  755. [Fact]
  756. [TestRespondersDisposed]
  757. public void Internal_Tests ()
  758. {
  759. var posFactor = new Pos.PosFactor (0.10F);
  760. Assert.Equal (10, posFactor.Anchor (100));
  761. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  762. Assert.Equal (99, posAnchorEnd.Anchor (100));
  763. var posCenter = new Pos.PosCenter ();
  764. Assert.Equal (50, posCenter.Anchor (100));
  765. var posAbsolute = new Pos.PosAbsolute (10);
  766. Assert.Equal (10, posAbsolute.Anchor (0));
  767. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  768. Assert.Equal (posCombine._left, posFactor);
  769. Assert.Equal (posCombine._right, posAbsolute);
  770. Assert.Equal (20, posCombine.Anchor (100));
  771. posCombine = new Pos.PosCombine (true, posAbsolute, posFactor);
  772. Assert.Equal (posCombine._left, posAbsolute);
  773. Assert.Equal (posCombine._right, posFactor);
  774. Assert.Equal (20, posCombine.Anchor (100));
  775. var view = new View (new Rect (20, 10, 20, 1));
  776. var posViewX = new Pos.PosView (view, 0);
  777. Assert.Equal (20, posViewX.Anchor (0));
  778. var posViewY = new Pos.PosView (view, 1);
  779. Assert.Equal (10, posViewY.Anchor (0));
  780. var posRight = new Pos.PosView (view, 2);
  781. Assert.Equal (40, posRight.Anchor (0));
  782. var posViewBottom = new Pos.PosView (view, 3);
  783. Assert.Equal (11, posViewBottom.Anchor (0));
  784. view.Dispose ();
  785. }
  786. [Fact]
  787. public void Function_SetsValue ()
  788. {
  789. string text = "Test";
  790. var pos = Pos.Function (() => text.Length);
  791. Assert.Equal ("PosFunc(4)", pos.ToString ());
  792. text = "New Test";
  793. Assert.Equal ("PosFunc(8)", pos.ToString ());
  794. text = "";
  795. Assert.Equal ("PosFunc(0)", pos.ToString ());
  796. }
  797. [Fact]
  798. public void Function_Equal ()
  799. {
  800. var f1 = () => 0;
  801. var f2 = () => 0;
  802. var pos1 = Pos.Function (f1);
  803. var pos2 = Pos.Function (f2);
  804. Assert.Equal (pos1, pos2);
  805. f2 = () => 1;
  806. pos2 = Pos.Function (f2);
  807. Assert.NotEqual (pos1, pos2);
  808. }
  809. [Theory]
  810. [AutoInitShutdown]
  811. [InlineData (true)]
  812. [InlineData (false)]
  813. public void PosPercentPlusOne (bool testHorizontal)
  814. {
  815. var container = new View {
  816. Width = 100,
  817. Height = 100
  818. };
  819. var label = new Label {
  820. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  821. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  822. Width = 10,
  823. Height = 10
  824. };
  825. container.Add (label);
  826. Application.Top.Add (container);
  827. Application.Top.LayoutSubviews ();
  828. Assert.Equal (100, container.Frame.Width);
  829. Assert.Equal (100, container.Frame.Height);
  830. if (testHorizontal) {
  831. Assert.Equal (61, label.Frame.X);
  832. Assert.Equal (1, label.Frame.Y);
  833. } else {
  834. Assert.Equal (1, label.Frame.X);
  835. Assert.Equal (61, label.Frame.Y);
  836. }
  837. }
  838. [Fact]
  839. public void PosCombine_Referencing_Same_View ()
  840. {
  841. var super = new View ("super") {
  842. Width = 10,
  843. Height = 10
  844. };
  845. var view1 = new View ("view1") {
  846. Width = 2,
  847. Height = 2
  848. };
  849. var view2 = new View ("view2") {
  850. Width = 2,
  851. Height = 2
  852. };
  853. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  854. super.Add (view1, view2);
  855. super.BeginInit ();
  856. super.EndInit ();
  857. var exception = Record.Exception (super.LayoutSubviews);
  858. Assert.Null (exception);
  859. Assert.Equal (new Rect (0, 0, 10, 10), super.Frame);
  860. Assert.Equal (new Rect (0, 0, 2, 2), view1.Frame);
  861. Assert.Equal (new Rect (8, 0, 2, 2), view2.Frame);
  862. super.Dispose ();
  863. }
  864. [Fact]
  865. public void DoNotReturnPosCombine ()
  866. {
  867. var v = new View () { Id = "V" };
  868. var pos = Pos.Left (v);
  869. Assert.Equal (
  870. "View(side=x,target=View(V)(0,0,0,0))",
  871. pos.ToString ());
  872. pos = Pos.X (v);
  873. Assert.Equal (
  874. "View(side=x,target=View(V)(0,0,0,0))",
  875. pos.ToString ());
  876. pos = Pos.Top (v);
  877. Assert.Equal (
  878. "View(side=y,target=View(V)(0,0,0,0))",
  879. pos.ToString ());
  880. pos = Pos.Y (v);
  881. Assert.Equal (
  882. "View(side=y,target=View(V)(0,0,0,0))",
  883. pos.ToString ());
  884. pos = Pos.Right (v);
  885. Assert.Equal (
  886. "View(side=right,target=View(V)(0,0,0,0))",
  887. pos.ToString ());
  888. pos = Pos.Bottom (v);
  889. Assert.Equal (
  890. "View(side=bottom,target=View(V)(0,0,0,0))",
  891. pos.ToString ());
  892. }
  893. }