PosTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using Terminal.Gui;
  8. using Xunit;
  9. // Alais Console to MockConsole so we don't accidentally use Console
  10. using Console = Terminal.Gui.FakeConsole;
  11. namespace Terminal.Gui.Core {
  12. public class PosTests {
  13. [Fact]
  14. public void New_Works ()
  15. {
  16. var pos = new Pos ();
  17. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  18. }
  19. [Fact]
  20. public void AnchorEnd_SetsValue ()
  21. {
  22. var n = 0;
  23. var pos = Pos.AnchorEnd ();
  24. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  25. n = 5;
  26. pos = Pos.AnchorEnd (n);
  27. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  28. }
  29. [Fact]
  30. public void AnchorEnd_Equal ()
  31. {
  32. var n1 = 0;
  33. var n2 = 0;
  34. var pos1 = Pos.AnchorEnd (n1);
  35. var pos2 = Pos.AnchorEnd (n2);
  36. Assert.Equal (pos1, pos2);
  37. // Test inequality
  38. n2 = 5;
  39. pos2 = Pos.AnchorEnd (n2);
  40. Assert.NotEqual (pos1, pos2);
  41. }
  42. [Fact]
  43. public void AnchorEnd_Negative_Throws ()
  44. {
  45. Pos pos;
  46. var n = -1;
  47. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  48. }
  49. [Fact]
  50. public void At_SetsValue ()
  51. {
  52. var pos = Pos.At (0);
  53. Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
  54. pos = Pos.At (5);
  55. Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
  56. pos = Pos.At (-1);
  57. Assert.Equal ("Pos.Absolute(-1)", pos.ToString ());
  58. }
  59. [Fact]
  60. public void At_Equal ()
  61. {
  62. var n1 = 0;
  63. var n2 = 0;
  64. var pos1 = Pos.At (n1);
  65. var pos2 = Pos.At (n2);
  66. Assert.Equal (pos1, pos2);
  67. }
  68. [Fact]
  69. public void SetSide_Null_Throws ()
  70. {
  71. var pos = Pos.Left (null);
  72. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  73. pos = Pos.X (null);
  74. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  75. pos = Pos.Top (null);
  76. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  77. pos = Pos.Y (null);
  78. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  79. pos = Pos.Bottom (null);
  80. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  81. pos = Pos.Right (null);
  82. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  83. }
  84. // TODO: Test Left, Top, Right bottom Equal
  85. /// <summary>
  86. /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
  87. /// </summary>
  88. [Fact]
  89. public void PosSide_SetsValue ()
  90. {
  91. string side; // used in format string
  92. var testRect = Rect.Empty;
  93. var testInt = 0;
  94. Pos pos;
  95. // Pos.Left
  96. side = "x";
  97. testInt = 0;
  98. testRect = Rect.Empty;
  99. pos = Pos.Left (new View ());
  100. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  101. pos = Pos.Left (new View (testRect));
  102. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  103. testRect = new Rect (1, 2, 3, 4);
  104. pos = Pos.Left (new View (testRect));
  105. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  106. // Pos.Left(win) + 0
  107. pos = Pos.Left (new View (testRect)) + testInt;
  108. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  109. testInt = 1;
  110. // Pos.Left(win) +1
  111. pos = Pos.Left (new View (testRect)) + testInt;
  112. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  113. testInt = -1;
  114. // Pos.Left(win) -1
  115. pos = Pos.Left (new View (testRect)) - testInt;
  116. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  117. // Pos.X
  118. side = "x";
  119. testInt = 0;
  120. testRect = Rect.Empty;
  121. pos = Pos.X (new View ());
  122. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  123. pos = Pos.X (new View (testRect));
  124. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  125. testRect = new Rect (1, 2, 3, 4);
  126. pos = Pos.X (new View (testRect));
  127. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  128. // Pos.X(win) + 0
  129. pos = Pos.X (new View (testRect)) + testInt;
  130. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  131. testInt = 1;
  132. // Pos.X(win) +1
  133. pos = Pos.X (new View (testRect)) + testInt;
  134. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  135. testInt = -1;
  136. // Pos.X(win) -1
  137. pos = Pos.X (new View (testRect)) - testInt;
  138. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  139. // Pos.Top
  140. side = "y";
  141. testInt = 0;
  142. testRect = Rect.Empty;
  143. pos = Pos.Top (new View ());
  144. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  145. pos = Pos.Top (new View (testRect));
  146. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  147. testRect = new Rect (1, 2, 3, 4);
  148. pos = Pos.Top (new View (testRect));
  149. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  150. // Pos.Top(win) + 0
  151. pos = Pos.Top (new View (testRect)) + testInt;
  152. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  153. testInt = 1;
  154. // Pos.Top(win) +1
  155. pos = Pos.Top (new View (testRect)) + testInt;
  156. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  157. testInt = -1;
  158. // Pos.Top(win) -1
  159. pos = Pos.Top (new View (testRect)) - testInt;
  160. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  161. // Pos.Y
  162. side = "y";
  163. testInt = 0;
  164. testRect = Rect.Empty;
  165. pos = Pos.Y (new View ());
  166. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  167. pos = Pos.Y (new View (testRect));
  168. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  169. testRect = new Rect (1, 2, 3, 4);
  170. pos = Pos.Y (new View (testRect));
  171. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  172. // Pos.Y(win) + 0
  173. pos = Pos.Y (new View (testRect)) + testInt;
  174. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  175. testInt = 1;
  176. // Pos.Y(win) +1
  177. pos = Pos.Y (new View (testRect)) + testInt;
  178. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  179. testInt = -1;
  180. // Pos.Y(win) -1
  181. pos = Pos.Y (new View (testRect)) - testInt;
  182. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  183. // Pos.Bottom
  184. side = "bottom";
  185. testRect = Rect.Empty;
  186. testInt = 0;
  187. pos = Pos.Bottom (new View ());
  188. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  189. pos = Pos.Bottom (new View (testRect));
  190. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  191. testRect = new Rect (1, 2, 3, 4);
  192. pos = Pos.Bottom (new View (testRect));
  193. Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  194. // Pos.Bottom(win) + 0
  195. pos = Pos.Bottom (new View (testRect)) + testInt;
  196. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  197. testInt = 1;
  198. // Pos.Bottom(win) +1
  199. pos = Pos.Bottom (new View (testRect)) + testInt;
  200. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  201. testInt = -1;
  202. // Pos.Bottom(win) -1
  203. pos = Pos.Bottom (new View (testRect)) - testInt;
  204. Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
  205. }
  206. // See: https://github.com/migueldeicaza/gui.cs/issues/504
  207. [Fact]
  208. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  209. {
  210. // Setup Fake driver
  211. (Window win, Button button) setup ()
  212. {
  213. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  214. Application.Iteration = () => {
  215. Application.RequestStop ();
  216. };
  217. var win = new Window ("window") {
  218. X = 0,
  219. Y = 0,
  220. Width = Dim.Fill (),
  221. Height = Dim.Fill (),
  222. };
  223. Application.Top.Add (win);
  224. var button = new Button ("button") {
  225. X = Pos.Center (),
  226. };
  227. win.Add (button);
  228. return (win, button);
  229. }
  230. Application.RunState rs;
  231. void cleanup (Application.RunState rs)
  232. {
  233. // Cleanup
  234. Application.End (rs);
  235. // Shutdown must be called to safely clean up Application if Init has been called
  236. Application.Shutdown ();
  237. }
  238. // Test cases:
  239. var app = setup ();
  240. app.button.Y = Pos.Left (app.win);
  241. rs = Application.Begin (Application.Top);
  242. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  243. Application.RunLoop (rs);
  244. cleanup (rs);
  245. app = setup ();
  246. app.button.Y = Pos.X (app.win);
  247. rs = Application.Begin (Application.Top);
  248. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  249. Application.RunLoop (rs);
  250. cleanup (rs);
  251. app = setup ();
  252. app.button.Y = Pos.Top (app.win);
  253. rs = Application.Begin (Application.Top);
  254. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  255. Application.RunLoop (rs);
  256. cleanup (rs);
  257. app = setup ();
  258. app.button.Y = Pos.Y (app.win);
  259. rs = Application.Begin (Application.Top);
  260. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  261. Application.RunLoop (rs);
  262. cleanup (rs);
  263. app = setup ();
  264. app.button.Y = Pos.Bottom (app.win);
  265. rs = Application.Begin (Application.Top);
  266. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  267. Application.RunLoop (rs);
  268. cleanup (rs);
  269. app = setup ();
  270. app.button.Y = Pos.Right (app.win);
  271. rs = Application.Begin (Application.Top);
  272. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  273. Application.RunLoop (rs);
  274. cleanup (rs);
  275. }
  276. [Fact]
  277. public void Center_SetsValue ()
  278. {
  279. var pos = Pos.Center ();
  280. Assert.Equal ("Pos.Center", pos.ToString ());
  281. }
  282. [Fact]
  283. public void Percent_SetsValue ()
  284. {
  285. float f = 0;
  286. var pos = Pos.Percent (f);
  287. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  288. f = 0.5F;
  289. pos = Pos.Percent (f);
  290. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  291. f = 100;
  292. pos = Pos.Percent (f);
  293. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  294. }
  295. [Fact]
  296. public void Percent_Equal ()
  297. {
  298. var n1 = 0;
  299. var n2 = 0;
  300. var pos1 = Pos.Percent (n1);
  301. var pos2 = Pos.Percent (n2);
  302. // BUGBUG: Pos.Percent should support equality
  303. Assert.NotEqual (pos1, pos2);
  304. }
  305. [Fact]
  306. public void Percent_ThrowsOnIvalid ()
  307. {
  308. var pos = Pos.Percent (0);
  309. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  310. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  311. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  312. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  313. }
  314. [Fact]
  315. public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  316. {
  317. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  318. var t = Application.Top;
  319. var w = new Window ("w") {
  320. X = Pos.Left (t) + 2,
  321. Y = Pos.At (2)
  322. };
  323. var v = new View ("v") {
  324. X = Pos.Center (),
  325. Y = Pos.Percent (10)
  326. };
  327. w.Add (v);
  328. t.Add (w);
  329. t.Ready += () => {
  330. Assert.Equal (2, w.X = 2);
  331. Assert.Equal (2, w.Y = 2);
  332. Assert.Throws<ArgumentException> (() => v.X = 2);
  333. Assert.Throws<ArgumentException> (() => v.Y = 2);
  334. };
  335. Application.Iteration += () => Application.RequestStop ();
  336. Application.Run ();
  337. Application.Shutdown ();
  338. }
  339. [Fact]
  340. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  341. {
  342. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  343. var t = Application.Top;
  344. var w = new Window (new Rect (1, 2, 4, 5), "w");
  345. t.Add (w);
  346. t.Ready += () => {
  347. Assert.Equal (2, w.X = 2);
  348. Assert.Equal (2, w.Y = 2);
  349. };
  350. Application.Iteration += () => Application.RequestStop ();
  351. Application.Run ();
  352. Application.Shutdown ();
  353. }
  354. [Fact]
  355. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  356. {
  357. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  358. var t = Application.Top;
  359. var w = new Window ("w") {
  360. X = Pos.Left (t) + 2,
  361. Y = Pos.At (2)
  362. };
  363. var v = new View ("v") {
  364. X = Pos.Center (),
  365. Y = Pos.Percent (10)
  366. };
  367. w.Add (v);
  368. t.Add (w);
  369. t.Ready += () => {
  370. v.LayoutStyle = LayoutStyle.Absolute;
  371. Assert.Equal (2, v.X = 2);
  372. Assert.Equal (2, v.Y = 2);
  373. };
  374. Application.Iteration += () => Application.RequestStop ();
  375. Application.Run ();
  376. Application.Shutdown ();
  377. }
  378. // DONE: Test PosCombine
  379. // DONE: Test operators
  380. [Fact]
  381. public void PosCombine_Do_Not_Throws ()
  382. {
  383. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  384. var t = Application.Top;
  385. var w = new Window ("w") {
  386. X = Pos.Left (t) + 2,
  387. Y = Pos.Top (t) + 2
  388. };
  389. var f = new FrameView ("f");
  390. var v1 = new View ("v1") {
  391. X = Pos.Left (w) + 2,
  392. Y = Pos.Top (w) + 2
  393. };
  394. var v2 = new View ("v2") {
  395. X = Pos.Left (v1) + 2,
  396. Y = Pos.Top (v1) + 2
  397. };
  398. f.Add (v1, v2);
  399. w.Add (f);
  400. t.Add (w);
  401. f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
  402. f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
  403. t.Ready += () => {
  404. Assert.Equal (0, t.Frame.X);
  405. Assert.Equal (0, t.Frame.Y);
  406. Assert.Equal (2, w.Frame.X);
  407. Assert.Equal (2, w.Frame.Y);
  408. Assert.Equal (2, f.Frame.X);
  409. Assert.Equal (2, f.Frame.Y);
  410. Assert.Equal (4, v1.Frame.X);
  411. Assert.Equal (4, v1.Frame.Y);
  412. Assert.Equal (6, v2.Frame.X);
  413. Assert.Equal (6, v2.Frame.Y);
  414. };
  415. Application.Iteration += () => Application.RequestStop ();
  416. Application.Run ();
  417. Application.Shutdown ();
  418. }
  419. [Fact]
  420. public void PosCombine_Will_Throws ()
  421. {
  422. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  423. var t = Application.Top;
  424. var w = new Window ("w") {
  425. X = Pos.Left (t) + 2,
  426. Y = Pos.Top (t) + 2
  427. };
  428. var f = new FrameView ("f");
  429. var v1 = new View ("v1") {
  430. X = Pos.Left (w) + 2,
  431. Y = Pos.Top (w) + 2
  432. };
  433. var v2 = new View ("v2") {
  434. X = Pos.Left (v1) + 2,
  435. Y = Pos.Top (v1) + 2
  436. };
  437. f.Add (v1); // v2 not added
  438. w.Add (f);
  439. t.Add (w);
  440. f.X = Pos.X (v2) - Pos.X (v1);
  441. f.Y = Pos.Y (v2) - Pos.Y (v1);
  442. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  443. Application.Shutdown ();
  444. }
  445. [Fact]
  446. public void Pos_Add_Operator ()
  447. {
  448. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  449. var top = Application.Top;
  450. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  451. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  452. var count = 0;
  453. field.KeyDown += (k) => {
  454. if (k.KeyEvent.Key == Key.Enter) {
  455. field.Text = $"Label {count}";
  456. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  457. view.Add (label);
  458. Assert.Equal ($"Label {count}", label.Text);
  459. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  460. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  461. field.Y += 1;
  462. count++;
  463. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  464. }
  465. };
  466. Application.Iteration += () => {
  467. while (count < 20) {
  468. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  469. }
  470. Application.RequestStop ();
  471. };
  472. var win = new Window ();
  473. win.Add (view);
  474. win.Add (field);
  475. top.Add (win);
  476. Application.Run (top);
  477. Assert.Equal (20, count);
  478. // Shutdown must be called to safely clean up Application if Init has been called
  479. Application.Shutdown ();
  480. }
  481. [Fact]
  482. public void Pos_Subtract_Operator ()
  483. {
  484. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  485. var top = Application.Top;
  486. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  487. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  488. var count = 20;
  489. var listLabels = new List<Label> ();
  490. for (int i = 0; i < count; i++) {
  491. field.Text = $"Label {i}";
  492. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  493. view.Add (label);
  494. Assert.Equal ($"Label {i}", label.Text);
  495. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  496. listLabels.Add (label);
  497. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  498. field.Y += 1;
  499. Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.ToString ());
  500. }
  501. field.KeyDown += (k) => {
  502. if (k.KeyEvent.Key == Key.Enter) {
  503. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  504. view.Remove (listLabels [count - 1]);
  505. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  506. field.Y -= 1;
  507. count--;
  508. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  509. }
  510. };
  511. Application.Iteration += () => {
  512. while (count > 0) {
  513. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  514. }
  515. Application.RequestStop ();
  516. };
  517. var win = new Window ();
  518. win.Add (view);
  519. win.Add (field);
  520. top.Add (win);
  521. Application.Run (top);
  522. Assert.Equal (0, count);
  523. // Shutdown must be called to safely clean up Application if Init has been called
  524. Application.Shutdown ();
  525. }
  526. }
  527. }