PosTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 ForceValidatePosDim_True_Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  565. {
  566. Application.Init (new FakeDriver ());
  567. var t = Application.Top;
  568. var w = new Window () {
  569. X = Pos.Left (t) + 2,
  570. Y = Pos.At (2)
  571. };
  572. var v = new View () {
  573. X = Pos.Center (),
  574. Y = Pos.Percent (10),
  575. ValidatePosDim = true
  576. };
  577. w.Add (v);
  578. t.Add (w);
  579. t.Ready += (s, e) => {
  580. Assert.Equal (2, w.X = 2);
  581. Assert.Equal (2, w.Y = 2);
  582. Assert.Throws<ArgumentException> (() => v.X = 2);
  583. Assert.Throws<ArgumentException> (() => v.Y = 2);
  584. };
  585. Application.Iteration += (s, a) => Application.RequestStop ();
  586. Application.Run ();
  587. Application.Shutdown ();
  588. }
  589. [Fact]
  590. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  591. {
  592. Application.Init (new FakeDriver ());
  593. var t = Application.Top;
  594. var w = new Window (new Rect (1, 2, 4, 5));
  595. t.Add (w);
  596. t.Ready += (s, e) => {
  597. Assert.Equal (2, w.X = 2);
  598. Assert.Equal (2, w.Y = 2);
  599. };
  600. Application.Iteration += (s, a) => Application.RequestStop ();
  601. Application.Run ();
  602. Application.Shutdown ();
  603. }
  604. [Fact]
  605. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  606. {
  607. Application.Init (new FakeDriver ());
  608. var t = Application.Top;
  609. var w = new Window () {
  610. X = Pos.Left (t) + 2,
  611. Y = Pos.At (2)
  612. };
  613. var v = new View () {
  614. X = Pos.Center (),
  615. Y = Pos.Percent (10)
  616. };
  617. w.Add (v);
  618. t.Add (w);
  619. t.Ready += (s, e) => {
  620. v.LayoutStyle = LayoutStyle.Absolute;
  621. Assert.Equal (2, v.X = 2);
  622. Assert.Equal (2, v.Y = 2);
  623. };
  624. Application.Iteration += (s, a) => Application.RequestStop ();
  625. Application.Run ();
  626. Application.Shutdown ();
  627. }
  628. // DONE: Test PosCombine
  629. // DONE: Test operators
  630. // BUGBUG: v2 - This test is bogus. v1 and references it's superview's (f) superview (w).
  631. //[Fact]
  632. //public void PosCombine_Do_Not_Throws ()
  633. //{
  634. // Application.Init (new FakeDriver ());
  635. // var w = new Window () {
  636. // X = Pos.Left (Application.Top) + 2,
  637. // Y = Pos.Top (Application.Top) + 2
  638. // };
  639. // var f = new FrameView ();
  640. // var v1 = new View () {
  641. // X = Pos.Left (w) + 2,
  642. // Y = Pos.Top (w) + 2
  643. // };
  644. // var v2 = new View () {
  645. // X = Pos.Left (v1) + 2,
  646. // Y = Pos.Top (v1) + 2
  647. // };
  648. // f.Add (v1, v2);
  649. // w.Add (f);
  650. // Application.Top.Add (w);
  651. // f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  652. // f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  653. // Application.Top.LayoutComplete += (s, e) => {
  654. // Assert.Equal (0, Application.Top.Frame.X);
  655. // Assert.Equal (0, Application.Top.Frame.Y);
  656. // Assert.Equal (2, w.Frame.X);
  657. // Assert.Equal (2, w.Frame.Y);
  658. // Assert.Equal (2, f.Frame.X);
  659. // Assert.Equal (2, f.Frame.Y);
  660. // Assert.Equal (4, v1.Frame.X);
  661. // Assert.Equal (4, v1.Frame.Y);
  662. // Assert.Equal (6, v2.Frame.X);
  663. // Assert.Equal (6, v2.Frame.Y);
  664. // };
  665. // Application.Iteration += (s, a) => Application.RequestStop ();
  666. // Application.Run ();
  667. // Application.Shutdown ();
  668. //}
  669. [Fact]
  670. [TestRespondersDisposed]
  671. public void PosCombine_Will_Throws ()
  672. {
  673. Application.Init (new FakeDriver ());
  674. var t = Application.Top;
  675. var w = new Window () {
  676. X = Pos.Left (t) + 2,
  677. Y = Pos.Top (t) + 2
  678. };
  679. var f = new FrameView ();
  680. var v1 = new View () {
  681. X = Pos.Left (w) + 2,
  682. Y = Pos.Top (w) + 2
  683. };
  684. var v2 = new View () {
  685. X = Pos.Left (v1) + 2,
  686. Y = Pos.Top (v1) + 2
  687. };
  688. f.Add (v1); // v2 not added
  689. w.Add (f);
  690. t.Add (w);
  691. f.X = Pos.X (v2) - Pos.X (v1);
  692. f.Y = Pos.Y (v2) - Pos.Y (v1);
  693. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  694. Application.Shutdown ();
  695. v2.Dispose ();
  696. }
  697. [Fact]
  698. [TestRespondersDisposed]
  699. public void Pos_Add_Operator ()
  700. {
  701. Application.Init (new FakeDriver ());
  702. var top = Application.Top;
  703. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  704. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  705. int count = 0;
  706. field.KeyDown += (s, k) => {
  707. if (k.KeyCode == KeyCode.Enter) {
  708. field.Text = $"Label {count}";
  709. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  710. view.Add (label);
  711. Assert.Equal ($"Label {count}", label.Text);
  712. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  713. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  714. field.Y += 1;
  715. count++;
  716. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  717. }
  718. };
  719. Application.Iteration += (s, a) => {
  720. while (count < 20) {
  721. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  722. }
  723. Application.RequestStop ();
  724. };
  725. var win = new Window ();
  726. win.Add (view);
  727. win.Add (field);
  728. top.Add (win);
  729. Application.Run (top);
  730. Assert.Equal (20, count);
  731. // Shutdown must be called to safely clean up Application if Init has been called
  732. Application.Shutdown ();
  733. }
  734. [Fact]
  735. [TestRespondersDisposed]
  736. public void Pos_Subtract_Operator ()
  737. {
  738. Application.Init (new FakeDriver ());
  739. var top = Application.Top;
  740. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  741. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  742. int count = 20;
  743. var listLabels = new List<Label> ();
  744. for (int i = 0; i < count; i++) {
  745. field.Text = $"Label {i}";
  746. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  747. view.Add (label);
  748. Assert.Equal ($"Label {i}", label.Text);
  749. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  750. listLabels.Add (label);
  751. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  752. field.Y += 1;
  753. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  754. }
  755. field.KeyDown += (s, k) => {
  756. if (k.KeyCode == KeyCode.Enter) {
  757. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  758. view.Remove (listLabels [count - 1]);
  759. listLabels [count - 1].Dispose ();
  760. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  761. field.Y -= 1;
  762. count--;
  763. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  764. }
  765. };
  766. Application.Iteration += (s, a) => {
  767. while (count > 0) {
  768. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  769. }
  770. Application.RequestStop ();
  771. };
  772. var win = new Window ();
  773. win.Add (view);
  774. win.Add (field);
  775. top.Add (win);
  776. Application.Run (top);
  777. Assert.Equal (0, count);
  778. // Shutdown must be called to safely clean up Application if Init has been called
  779. Application.Shutdown ();
  780. }
  781. [Fact]
  782. [TestRespondersDisposed]
  783. public void Internal_Tests ()
  784. {
  785. var posFactor = new Pos.PosFactor (0.10F);
  786. Assert.Equal (10, posFactor.Anchor (100));
  787. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  788. Assert.Equal (99, posAnchorEnd.Anchor (100));
  789. var posCenter = new Pos.PosCenter ();
  790. Assert.Equal (50, posCenter.Anchor (100));
  791. var posAbsolute = new Pos.PosAbsolute (10);
  792. Assert.Equal (10, posAbsolute.Anchor (0));
  793. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  794. Assert.Equal (posCombine._left, posFactor);
  795. Assert.Equal (posCombine._right, posAbsolute);
  796. Assert.Equal (20, posCombine.Anchor (100));
  797. posCombine = new Pos.PosCombine (true, posAbsolute, posFactor);
  798. Assert.Equal (posCombine._left, posAbsolute);
  799. Assert.Equal (posCombine._right, posFactor);
  800. Assert.Equal (20, posCombine.Anchor (100));
  801. var view = new View (new Rect (20, 10, 20, 1));
  802. var posViewX = new Pos.PosView (view, 0);
  803. Assert.Equal (20, posViewX.Anchor (0));
  804. var posViewY = new Pos.PosView (view, 1);
  805. Assert.Equal (10, posViewY.Anchor (0));
  806. var posRight = new Pos.PosView (view, 2);
  807. Assert.Equal (40, posRight.Anchor (0));
  808. var posViewBottom = new Pos.PosView (view, 3);
  809. Assert.Equal (11, posViewBottom.Anchor (0));
  810. view.Dispose ();
  811. }
  812. [Fact]
  813. public void Function_SetsValue ()
  814. {
  815. string text = "Test";
  816. var pos = Pos.Function (() => text.Length);
  817. Assert.Equal ("PosFunc(4)", pos.ToString ());
  818. text = "New Test";
  819. Assert.Equal ("PosFunc(8)", pos.ToString ());
  820. text = "";
  821. Assert.Equal ("PosFunc(0)", pos.ToString ());
  822. }
  823. [Fact]
  824. public void Function_Equal ()
  825. {
  826. var f1 = () => 0;
  827. var f2 = () => 0;
  828. var pos1 = Pos.Function (f1);
  829. var pos2 = Pos.Function (f2);
  830. Assert.Equal (pos1, pos2);
  831. f2 = () => 1;
  832. pos2 = Pos.Function (f2);
  833. Assert.NotEqual (pos1, pos2);
  834. }
  835. [Theory]
  836. [AutoInitShutdown]
  837. [InlineData (true)]
  838. [InlineData (false)]
  839. public void PosPercentPlusOne (bool testHorizontal)
  840. {
  841. var container = new View {
  842. Width = 100,
  843. Height = 100
  844. };
  845. var label = new Label {
  846. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  847. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  848. Width = 10,
  849. Height = 10
  850. };
  851. container.Add (label);
  852. Application.Top.Add (container);
  853. Application.Top.LayoutSubviews ();
  854. Assert.Equal (100, container.Frame.Width);
  855. Assert.Equal (100, container.Frame.Height);
  856. if (testHorizontal) {
  857. Assert.Equal (61, label.Frame.X);
  858. Assert.Equal (1, label.Frame.Y);
  859. } else {
  860. Assert.Equal (1, label.Frame.X);
  861. Assert.Equal (61, label.Frame.Y);
  862. }
  863. }
  864. [Fact]
  865. public void PosCombine_Referencing_Same_View ()
  866. {
  867. var super = new View ("super") {
  868. Width = 10,
  869. Height = 10
  870. };
  871. var view1 = new View ("view1") {
  872. Width = 2,
  873. Height = 2
  874. };
  875. var view2 = new View ("view2") {
  876. Width = 2,
  877. Height = 2
  878. };
  879. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  880. super.Add (view1, view2);
  881. super.BeginInit ();
  882. super.EndInit ();
  883. var exception = Record.Exception (super.LayoutSubviews);
  884. Assert.Null (exception);
  885. Assert.Equal (new Rect (0, 0, 10, 10), super.Frame);
  886. Assert.Equal (new Rect (0, 0, 2, 2), view1.Frame);
  887. Assert.Equal (new Rect (8, 0, 2, 2), view2.Frame);
  888. super.Dispose ();
  889. }
  890. [Fact]
  891. public void DoNotReturnPosCombine ()
  892. {
  893. var v = new View () { Id = "V" };
  894. var pos = Pos.Left (v);
  895. Assert.Equal (
  896. "View(side=x,target=View(V)(0,0,0,0))",
  897. pos.ToString ());
  898. pos = Pos.X (v);
  899. Assert.Equal (
  900. "View(side=x,target=View(V)(0,0,0,0))",
  901. pos.ToString ());
  902. pos = Pos.Top (v);
  903. Assert.Equal (
  904. "View(side=y,target=View(V)(0,0,0,0))",
  905. pos.ToString ());
  906. pos = Pos.Y (v);
  907. Assert.Equal (
  908. "View(side=y,target=View(V)(0,0,0,0))",
  909. pos.ToString ());
  910. pos = Pos.Right (v);
  911. Assert.Equal (
  912. "View(side=right,target=View(V)(0,0,0,0))",
  913. pos.ToString ());
  914. pos = Pos.Bottom (v);
  915. Assert.Equal (
  916. "View(side=bottom,target=View(V)(0,0,0,0))",
  917. pos.ToString ());
  918. }
  919. }