PosTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. }
  236. // Test cases:
  237. var app = setup ();
  238. app.button.Y = Pos.Left (app.win);
  239. rs = Application.Begin (Application.Top);
  240. Application.Run ();
  241. cleanup (rs);
  242. app = setup ();
  243. app.button.Y = Pos.X (app.win);
  244. rs = Application.Begin (Application.Top);
  245. Application.Run ();
  246. cleanup (rs);
  247. app = setup ();
  248. app.button.Y = Pos.Top (app.win);
  249. rs = Application.Begin (Application.Top);
  250. Application.Run ();
  251. cleanup (rs);
  252. app = setup ();
  253. app.button.Y = Pos.Y (app.win);
  254. rs = Application.Begin (Application.Top);
  255. Application.Run ();
  256. cleanup (rs);
  257. app = setup ();
  258. app.button.Y = Pos.Bottom (app.win);
  259. rs = Application.Begin (Application.Top);
  260. Application.Run ();
  261. cleanup (rs);
  262. app = setup ();
  263. app.button.Y = Pos.Right (app.win);
  264. rs = Application.Begin (Application.Top);
  265. Application.Run ();
  266. cleanup (rs);
  267. }
  268. [Fact]
  269. public void Center_SetsValue ()
  270. {
  271. var pos = Pos.Center ();
  272. Assert.Equal ("Pos.Center", pos.ToString ());
  273. }
  274. [Fact]
  275. public void Percent_SetsValue ()
  276. {
  277. float f = 0;
  278. var pos = Pos.Percent (f);
  279. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  280. f = 0.5F;
  281. pos = Pos.Percent (f);
  282. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  283. f = 100;
  284. pos = Pos.Percent (f);
  285. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  286. }
  287. [Fact]
  288. public void Percent_Equal ()
  289. {
  290. var n1 = 0;
  291. var n2 = 0;
  292. var pos1 = Pos.Percent (n1);
  293. var pos2 = Pos.Percent (n2);
  294. // BUGBUG: Pos.Percent should support equality
  295. Assert.NotEqual (pos1, pos2);
  296. }
  297. [Fact]
  298. public void Percent_ThrowsOnIvalid ()
  299. {
  300. var pos = Pos.Percent (0);
  301. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  302. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  303. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  304. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  305. }
  306. [Fact]
  307. public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  308. {
  309. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  310. var t = Application.Top;
  311. var w = new Window ("w") {
  312. X = Pos.Left (t) + 2,
  313. Y = Pos.At (2)
  314. };
  315. var v = new View ("v") {
  316. X = Pos.Center (),
  317. Y = Pos.Percent (10)
  318. };
  319. w.Add (v);
  320. t.Add (w);
  321. t.Ready += () => {
  322. Assert.Equal (2, w.X = 2);
  323. Assert.Equal (2, w.Y = 2);
  324. Assert.Throws<ArgumentException> (() => v.X = 2);
  325. Assert.Throws<ArgumentException> (() => v.Y = 2);
  326. };
  327. Application.Iteration += () => Application.RequestStop ();
  328. Application.Run ();
  329. Application.Shutdown ();
  330. }
  331. [Fact]
  332. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  333. {
  334. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  335. var t = Application.Top;
  336. var w = new Window (new Rect (1, 2, 4, 5), "w");
  337. t.Add (w);
  338. t.Ready += () => {
  339. Assert.Equal (2, w.X = 2);
  340. Assert.Equal (2, w.Y = 2);
  341. };
  342. Application.Iteration += () => Application.RequestStop ();
  343. Application.Run ();
  344. Application.Shutdown ();
  345. }
  346. [Fact]
  347. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  348. {
  349. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  350. var t = Application.Top;
  351. var w = new Window ("w") {
  352. X = Pos.Left (t) + 2,
  353. Y = Pos.At (2)
  354. };
  355. var v = new View ("v") {
  356. X = Pos.Center (),
  357. Y = Pos.Percent (10)
  358. };
  359. w.Add (v);
  360. t.Add (w);
  361. t.Ready += () => {
  362. v.LayoutStyle = LayoutStyle.Absolute;
  363. Assert.Equal (2, v.X = 2);
  364. Assert.Equal (2, v.Y = 2);
  365. };
  366. Application.Iteration += () => Application.RequestStop ();
  367. Application.Run ();
  368. Application.Shutdown ();
  369. }
  370. // DONE: Test PosCombine
  371. // DONE: Test operators
  372. [Fact]
  373. public void PosCombine_Do_Not_Throws ()
  374. {
  375. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  376. var t = Application.Top;
  377. var w = new Window ("w") {
  378. X = Pos.Left (t) + 2,
  379. Y = Pos.Top (t) + 2
  380. };
  381. var f = new FrameView ("f");
  382. var v1 = new View ("v1") {
  383. X = Pos.Left (w) + 2,
  384. Y = Pos.Top (w) + 2
  385. };
  386. var v2 = new View ("v2") {
  387. X = Pos.Left (v1) + 2,
  388. Y = Pos.Top (v1) + 2
  389. };
  390. f.Add (v1, v2);
  391. w.Add (f);
  392. t.Add (w);
  393. f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
  394. f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
  395. t.Ready += () => {
  396. Assert.Equal (0, t.Frame.X);
  397. Assert.Equal (0, t.Frame.Y);
  398. Assert.Equal (2, w.Frame.X);
  399. Assert.Equal (2, w.Frame.Y);
  400. Assert.Equal (2, f.Frame.X);
  401. Assert.Equal (2, f.Frame.Y);
  402. Assert.Equal (4, v1.Frame.X);
  403. Assert.Equal (4, v1.Frame.Y);
  404. Assert.Equal (6, v2.Frame.X);
  405. Assert.Equal (6, v2.Frame.Y);
  406. };
  407. Application.Iteration += () => Application.RequestStop ();
  408. Application.Run ();
  409. Application.Shutdown ();
  410. }
  411. [Fact]
  412. public void PosCombine_Will_Throws ()
  413. {
  414. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  415. var t = Application.Top;
  416. var w = new Window ("w") {
  417. X = Pos.Left (t) + 2,
  418. Y = Pos.Top (t) + 2
  419. };
  420. var f = new FrameView ("f");
  421. var v1 = new View ("v1") {
  422. X = Pos.Left (w) + 2,
  423. Y = Pos.Top (w) + 2
  424. };
  425. var v2 = new View ("v2") {
  426. X = Pos.Left (v1) + 2,
  427. Y = Pos.Top (v1) + 2
  428. };
  429. f.Add (v1); // v2 not added
  430. w.Add (f);
  431. t.Add (w);
  432. f.X = Pos.X (v2) - Pos.X (v1);
  433. f.Y = Pos.Y (v2) - Pos.Y (v1);
  434. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  435. Application.Shutdown ();
  436. }
  437. }
  438. }