PosTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. Application.Run ();
  243. cleanup (rs);
  244. app = setup ();
  245. app.button.Y = Pos.X (app.win);
  246. rs = Application.Begin (Application.Top);
  247. Application.Run ();
  248. cleanup (rs);
  249. app = setup ();
  250. app.button.Y = Pos.Top (app.win);
  251. rs = Application.Begin (Application.Top);
  252. Application.Run ();
  253. cleanup (rs);
  254. app = setup ();
  255. app.button.Y = Pos.Y (app.win);
  256. rs = Application.Begin (Application.Top);
  257. Application.Run ();
  258. cleanup (rs);
  259. app = setup ();
  260. app.button.Y = Pos.Bottom (app.win);
  261. rs = Application.Begin (Application.Top);
  262. Application.Run ();
  263. cleanup (rs);
  264. app = setup ();
  265. app.button.Y = Pos.Right (app.win);
  266. rs = Application.Begin (Application.Top);
  267. Application.Run ();
  268. cleanup (rs);
  269. }
  270. [Fact]
  271. public void Center_SetsValue ()
  272. {
  273. var pos = Pos.Center ();
  274. Assert.Equal ("Pos.Center", pos.ToString ());
  275. }
  276. [Fact]
  277. public void Percent_SetsValue ()
  278. {
  279. float f = 0;
  280. var pos = Pos.Percent (f);
  281. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  282. f = 0.5F;
  283. pos = Pos.Percent (f);
  284. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  285. f = 100;
  286. pos = Pos.Percent (f);
  287. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  288. }
  289. [Fact]
  290. public void Percent_Equal ()
  291. {
  292. var n1 = 0;
  293. var n2 = 0;
  294. var pos1 = Pos.Percent (n1);
  295. var pos2 = Pos.Percent (n2);
  296. // BUGBUG: Pos.Percent should support equality
  297. Assert.NotEqual (pos1, pos2);
  298. }
  299. [Fact]
  300. public void Percent_ThrowsOnIvalid ()
  301. {
  302. var pos = Pos.Percent (0);
  303. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  304. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  305. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  306. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  307. }
  308. [Fact]
  309. public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  310. {
  311. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  312. var t = Application.Top;
  313. var w = new Window ("w") {
  314. X = Pos.Left (t) + 2,
  315. Y = Pos.At (2)
  316. };
  317. var v = new View ("v") {
  318. X = Pos.Center (),
  319. Y = Pos.Percent (10)
  320. };
  321. w.Add (v);
  322. t.Add (w);
  323. t.Ready += () => {
  324. Assert.Equal (2, w.X = 2);
  325. Assert.Equal (2, w.Y = 2);
  326. Assert.Throws<ArgumentException> (() => v.X = 2);
  327. Assert.Throws<ArgumentException> (() => v.Y = 2);
  328. };
  329. Application.Iteration += () => Application.RequestStop ();
  330. Application.Run ();
  331. Application.Shutdown ();
  332. }
  333. [Fact]
  334. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  335. {
  336. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  337. var t = Application.Top;
  338. var w = new Window (new Rect (1, 2, 4, 5), "w");
  339. t.Add (w);
  340. t.Ready += () => {
  341. Assert.Equal (2, w.X = 2);
  342. Assert.Equal (2, w.Y = 2);
  343. };
  344. Application.Iteration += () => Application.RequestStop ();
  345. Application.Run ();
  346. Application.Shutdown ();
  347. }
  348. [Fact]
  349. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  350. {
  351. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  352. var t = Application.Top;
  353. var w = new Window ("w") {
  354. X = Pos.Left (t) + 2,
  355. Y = Pos.At (2)
  356. };
  357. var v = new View ("v") {
  358. X = Pos.Center (),
  359. Y = Pos.Percent (10)
  360. };
  361. w.Add (v);
  362. t.Add (w);
  363. t.Ready += () => {
  364. v.LayoutStyle = LayoutStyle.Absolute;
  365. Assert.Equal (2, v.X = 2);
  366. Assert.Equal (2, v.Y = 2);
  367. };
  368. Application.Iteration += () => Application.RequestStop ();
  369. Application.Run ();
  370. Application.Shutdown ();
  371. }
  372. // DONE: Test PosCombine
  373. // DONE: Test operators
  374. [Fact]
  375. public void PosCombine_Do_Not_Throws ()
  376. {
  377. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  378. var t = Application.Top;
  379. var w = new Window ("w") {
  380. X = Pos.Left (t) + 2,
  381. Y = Pos.Top (t) + 2
  382. };
  383. var f = new FrameView ("f");
  384. var v1 = new View ("v1") {
  385. X = Pos.Left (w) + 2,
  386. Y = Pos.Top (w) + 2
  387. };
  388. var v2 = new View ("v2") {
  389. X = Pos.Left (v1) + 2,
  390. Y = Pos.Top (v1) + 2
  391. };
  392. f.Add (v1, v2);
  393. w.Add (f);
  394. t.Add (w);
  395. f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
  396. f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
  397. t.Ready += () => {
  398. Assert.Equal (0, t.Frame.X);
  399. Assert.Equal (0, t.Frame.Y);
  400. Assert.Equal (2, w.Frame.X);
  401. Assert.Equal (2, w.Frame.Y);
  402. Assert.Equal (2, f.Frame.X);
  403. Assert.Equal (2, f.Frame.Y);
  404. Assert.Equal (4, v1.Frame.X);
  405. Assert.Equal (4, v1.Frame.Y);
  406. Assert.Equal (6, v2.Frame.X);
  407. Assert.Equal (6, v2.Frame.Y);
  408. };
  409. Application.Iteration += () => Application.RequestStop ();
  410. Application.Run ();
  411. Application.Shutdown ();
  412. }
  413. [Fact]
  414. public void PosCombine_Will_Throws ()
  415. {
  416. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  417. var t = Application.Top;
  418. var w = new Window ("w") {
  419. X = Pos.Left (t) + 2,
  420. Y = Pos.Top (t) + 2
  421. };
  422. var f = new FrameView ("f");
  423. var v1 = new View ("v1") {
  424. X = Pos.Left (w) + 2,
  425. Y = Pos.Top (w) + 2
  426. };
  427. var v2 = new View ("v2") {
  428. X = Pos.Left (v1) + 2,
  429. Y = Pos.Top (v1) + 2
  430. };
  431. f.Add (v1); // v2 not added
  432. w.Add (f);
  433. t.Add (w);
  434. f.X = Pos.X (v2) - Pos.X (v1);
  435. f.Y = Pos.Y (v2) - Pos.Y (v1);
  436. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  437. Application.Shutdown ();
  438. }
  439. [Fact]
  440. public void Pos_Add_Operator ()
  441. {
  442. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  443. var top = Application.Top;
  444. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  445. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  446. var count = 0;
  447. field.KeyDown += (k) => {
  448. if (k.KeyEvent.Key == Key.Enter) {
  449. field.Text = $"Label {count}";
  450. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  451. view.Add (label);
  452. Assert.Equal ($"Label {count}", label.Text);
  453. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  454. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  455. field.Y += 1;
  456. count++;
  457. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  458. }
  459. };
  460. Application.Iteration += () => {
  461. while (count < 20) {
  462. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  463. }
  464. Application.RequestStop ();
  465. };
  466. var win = new Window ();
  467. win.Add (view);
  468. win.Add (field);
  469. top.Add (win);
  470. Application.Run (top);
  471. Assert.Equal (20, count);
  472. // Shutdown must be called to safely clean up Application if Init has been called
  473. Application.Shutdown ();
  474. }
  475. [Fact]
  476. public void Pos_Subtract_Operator ()
  477. {
  478. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  479. var top = Application.Top;
  480. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  481. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  482. var count = 20;
  483. var listLabels = new List<Label> ();
  484. for (int i = 0; i < count; i++) {
  485. field.Text = $"Label {i}";
  486. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  487. view.Add (label);
  488. Assert.Equal ($"Label {i}", label.Text);
  489. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  490. listLabels.Add (label);
  491. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  492. field.Y += 1;
  493. Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.ToString ());
  494. }
  495. field.KeyDown += (k) => {
  496. if (k.KeyEvent.Key == Key.Enter) {
  497. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  498. view.Remove (listLabels [count - 1]);
  499. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  500. field.Y -= 1;
  501. count--;
  502. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  503. }
  504. };
  505. Application.Iteration += () => {
  506. while (count > 0) {
  507. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  508. }
  509. Application.RequestStop ();
  510. };
  511. var win = new Window ();
  512. win.Add (view);
  513. win.Add (field);
  514. top.Add (win);
  515. Application.Run (top);
  516. Assert.Equal (0, count);
  517. // Shutdown must be called to safely clean up Application if Init has been called
  518. Application.Shutdown ();
  519. }
  520. }
  521. }