PosTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. // Alias 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. [AutoInitShutdown]
  44. public void AnchorEnd_Equal_Inside_Window ()
  45. {
  46. var viewWidth = 10;
  47. var viewHeight = 1;
  48. var tv = new TextView () {
  49. X = Pos.AnchorEnd (viewWidth),
  50. Y = Pos.AnchorEnd (viewHeight),
  51. Width = viewWidth,
  52. Height = viewHeight
  53. };
  54. var win = new Window ();
  55. win.Add (tv);
  56. var top = Application.Top;
  57. top.Add (win);
  58. Application.Begin (top);
  59. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  60. Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
  61. Assert.Equal (new Rect (1, 1, 78, 23), win.Subviews[0].Frame);
  62. Assert.Equal ("ContentView()({X=1,Y=1,Width=78,Height=23})", win.Subviews [0].ToString());
  63. Assert.Equal (new Rect (1, 1, 79, 24), new Rect (
  64. win.Subviews[0].Frame.Left, win.Subviews [0].Frame.Top,
  65. win.Subviews [0].Frame.Right, win.Subviews[0].Frame.Bottom));
  66. Assert.Equal (new Rect (68, 22, 10, 1), tv.Frame);
  67. }
  68. [Fact]
  69. [AutoInitShutdown]
  70. public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  71. {
  72. var viewWidth = 10;
  73. var viewHeight = 1;
  74. var tv = new TextView () {
  75. X = Pos.AnchorEnd (viewWidth),
  76. Y = Pos.AnchorEnd (viewHeight),
  77. Width = viewWidth,
  78. Height = viewHeight
  79. };
  80. var win = new Window ();
  81. win.Add (tv);
  82. var menu = new MenuBar ();
  83. var status = new StatusBar ();
  84. var top = Application.Top;
  85. top.Add (win, menu, status);
  86. Application.Begin (top);
  87. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  88. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  89. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  90. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  91. Assert.Equal (new Rect (1, 1, 78, 21), win.Subviews [0].Frame);
  92. Assert.Equal (new Rect (1, 1, 79, 22), new Rect (
  93. win.Subviews [0].Frame.Left, win.Subviews [0].Frame.Top,
  94. win.Subviews [0].Frame.Right, win.Subviews [0].Frame.Bottom));
  95. Assert.Equal (new Rect (68, 20, 10, 1), tv.Frame);
  96. }
  97. [Fact]
  98. [AutoInitShutdown]
  99. public void Bottom_Equal_Inside_Window ()
  100. {
  101. var win = new Window ();
  102. var label = new Label ("This should be the last line.") {
  103. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  104. ColorScheme = Colors.Menu,
  105. Width = Dim.Fill (),
  106. X = Pos.Center (),
  107. Y = Pos.Bottom (win) - 4 // two lines top border more two lines above border
  108. };
  109. win.Add (label);
  110. var top = Application.Top;
  111. top.Add (win);
  112. Application.Begin (top);
  113. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  114. Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
  115. Assert.Equal (new Rect (1, 1, 78, 23), win.Subviews [0].Frame);
  116. Assert.Equal ("ContentView()({X=1,Y=1,Width=78,Height=23})", win.Subviews [0].ToString ());
  117. Assert.Equal (new Rect (0, 0, 80, 25), new Rect (
  118. win.Frame.Left, win.Frame.Top,
  119. win.Frame.Right, win.Frame.Bottom));
  120. Assert.Equal (new Rect (0, 21, 78, 1), label.Frame);
  121. }
  122. [Fact]
  123. [AutoInitShutdown]
  124. public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  125. {
  126. var win = new Window ();
  127. var label = new Label ("This should be the last line.") {
  128. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  129. ColorScheme = Colors.Menu,
  130. Width = Dim.Fill (),
  131. X = Pos.Center (),
  132. Y = Pos.Bottom (win) - 4 // two lines top border more two lines above border
  133. };
  134. win.Add (label);
  135. var menu = new MenuBar ();
  136. var status = new StatusBar ();
  137. var top = Application.Top;
  138. top.Add (win, menu, status);
  139. Application.Begin (top);
  140. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  141. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  142. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  143. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  144. Assert.Equal (new Rect (1, 1, 78, 21), win.Subviews [0].Frame);
  145. Assert.Equal (new Rect (0, 1, 80, 24), new Rect (
  146. win.Frame.Left, win.Frame.Top,
  147. win.Frame.Right, win.Frame.Bottom));
  148. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  149. }
  150. [Fact]
  151. public void AnchorEnd_Negative_Throws ()
  152. {
  153. Pos pos;
  154. var n = -1;
  155. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  156. }
  157. [Fact]
  158. public void At_SetsValue ()
  159. {
  160. var pos = Pos.At (0);
  161. Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
  162. pos = Pos.At (5);
  163. Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
  164. pos = Pos.At (-1);
  165. Assert.Equal ("Pos.Absolute(-1)", pos.ToString ());
  166. }
  167. [Fact]
  168. public void At_Equal ()
  169. {
  170. var n1 = 0;
  171. var n2 = 0;
  172. var pos1 = Pos.At (n1);
  173. var pos2 = Pos.At (n2);
  174. Assert.Equal (pos1, pos2);
  175. }
  176. [Fact]
  177. public void SetSide_Null_Throws ()
  178. {
  179. var pos = Pos.Left (null);
  180. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  181. pos = Pos.X (null);
  182. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  183. pos = Pos.Top (null);
  184. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  185. pos = Pos.Y (null);
  186. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  187. pos = Pos.Bottom (null);
  188. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  189. pos = Pos.Right (null);
  190. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  191. }
  192. // TODO: Test Left, Top, Right bottom Equal
  193. /// <summary>
  194. /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
  195. /// </summary>
  196. [Fact]
  197. public void PosSide_SetsValue ()
  198. {
  199. string side; // used in format string
  200. var testRect = Rect.Empty;
  201. var testInt = 0;
  202. Pos pos;
  203. // Pos.Left
  204. side = "x";
  205. testInt = 0;
  206. testRect = Rect.Empty;
  207. pos = Pos.Left (new View ());
  208. 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 ());
  209. pos = Pos.Left (new View (testRect));
  210. 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 ());
  211. testRect = new Rect (1, 2, 3, 4);
  212. pos = Pos.Left (new View (testRect));
  213. 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 ());
  214. // Pos.Left(win) + 0
  215. pos = Pos.Left (new View (testRect)) + testInt;
  216. 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 ());
  217. testInt = 1;
  218. // Pos.Left(win) +1
  219. pos = Pos.Left (new View (testRect)) + testInt;
  220. 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 ());
  221. testInt = -1;
  222. // Pos.Left(win) -1
  223. pos = Pos.Left (new View (testRect)) - testInt;
  224. 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 ());
  225. // Pos.X
  226. side = "x";
  227. testInt = 0;
  228. testRect = Rect.Empty;
  229. pos = Pos.X (new View ());
  230. 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 ());
  231. pos = Pos.X (new View (testRect));
  232. 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 ());
  233. testRect = new Rect (1, 2, 3, 4);
  234. pos = Pos.X (new View (testRect));
  235. 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 ());
  236. // Pos.X(win) + 0
  237. pos = Pos.X (new View (testRect)) + testInt;
  238. 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 ());
  239. testInt = 1;
  240. // Pos.X(win) +1
  241. pos = Pos.X (new View (testRect)) + testInt;
  242. 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 ());
  243. testInt = -1;
  244. // Pos.X(win) -1
  245. pos = Pos.X (new View (testRect)) - testInt;
  246. 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 ());
  247. // Pos.Top
  248. side = "y";
  249. testInt = 0;
  250. testRect = Rect.Empty;
  251. pos = Pos.Top (new View ());
  252. 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 ());
  253. pos = Pos.Top (new View (testRect));
  254. 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 ());
  255. testRect = new Rect (1, 2, 3, 4);
  256. pos = Pos.Top (new View (testRect));
  257. 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 ());
  258. // Pos.Top(win) + 0
  259. pos = Pos.Top (new View (testRect)) + testInt;
  260. 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 ());
  261. testInt = 1;
  262. // Pos.Top(win) +1
  263. pos = Pos.Top (new View (testRect)) + testInt;
  264. 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 ());
  265. testInt = -1;
  266. // Pos.Top(win) -1
  267. pos = Pos.Top (new View (testRect)) - testInt;
  268. 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 ());
  269. // Pos.Y
  270. side = "y";
  271. testInt = 0;
  272. testRect = Rect.Empty;
  273. pos = Pos.Y (new View ());
  274. 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 ());
  275. pos = Pos.Y (new View (testRect));
  276. 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 ());
  277. testRect = new Rect (1, 2, 3, 4);
  278. pos = Pos.Y (new View (testRect));
  279. 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 ());
  280. // Pos.Y(win) + 0
  281. pos = Pos.Y (new View (testRect)) + testInt;
  282. 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 ());
  283. testInt = 1;
  284. // Pos.Y(win) +1
  285. pos = Pos.Y (new View (testRect)) + testInt;
  286. 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 ());
  287. testInt = -1;
  288. // Pos.Y(win) -1
  289. pos = Pos.Y (new View (testRect)) - testInt;
  290. 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 ());
  291. // Pos.Bottom
  292. side = "bottom";
  293. testRect = Rect.Empty;
  294. testInt = 0;
  295. pos = Pos.Bottom (new View ());
  296. 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 ());
  297. pos = Pos.Bottom (new View (testRect));
  298. 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 ());
  299. testRect = new Rect (1, 2, 3, 4);
  300. pos = Pos.Bottom (new View (testRect));
  301. 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 ());
  302. // Pos.Bottom(win) + 0
  303. pos = Pos.Bottom (new View (testRect)) + testInt;
  304. 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 ());
  305. testInt = 1;
  306. // Pos.Bottom(win) +1
  307. pos = Pos.Bottom (new View (testRect)) + testInt;
  308. 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 ());
  309. testInt = -1;
  310. // Pos.Bottom(win) -1
  311. pos = Pos.Bottom (new View (testRect)) - testInt;
  312. 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 ());
  313. }
  314. // See: https://github.com/migueldeicaza/gui.cs/issues/504
  315. [Fact]
  316. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  317. {
  318. // Setup Fake driver
  319. (Window win, Button button) setup ()
  320. {
  321. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  322. Application.Iteration = () => {
  323. Application.RequestStop ();
  324. };
  325. var win = new Window ("window") {
  326. X = 0,
  327. Y = 0,
  328. Width = Dim.Fill (),
  329. Height = Dim.Fill (),
  330. };
  331. Application.Top.Add (win);
  332. var button = new Button ("button") {
  333. X = Pos.Center (),
  334. };
  335. win.Add (button);
  336. return (win, button);
  337. }
  338. Application.RunState rs;
  339. void cleanup (Application.RunState rs)
  340. {
  341. // Cleanup
  342. Application.End (rs);
  343. // Shutdown must be called to safely clean up Application if Init has been called
  344. Application.Shutdown ();
  345. }
  346. // Test cases:
  347. var app = setup ();
  348. app.button.Y = Pos.Left (app.win);
  349. rs = Application.Begin (Application.Top);
  350. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  351. Application.RunLoop (rs);
  352. cleanup (rs);
  353. app = setup ();
  354. app.button.Y = Pos.X (app.win);
  355. rs = Application.Begin (Application.Top);
  356. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  357. Application.RunLoop (rs);
  358. cleanup (rs);
  359. app = setup ();
  360. app.button.Y = Pos.Top (app.win);
  361. rs = Application.Begin (Application.Top);
  362. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  363. Application.RunLoop (rs);
  364. cleanup (rs);
  365. app = setup ();
  366. app.button.Y = Pos.Y (app.win);
  367. rs = Application.Begin (Application.Top);
  368. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  369. Application.RunLoop (rs);
  370. cleanup (rs);
  371. app = setup ();
  372. app.button.Y = Pos.Bottom (app.win);
  373. rs = Application.Begin (Application.Top);
  374. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  375. Application.RunLoop (rs);
  376. cleanup (rs);
  377. app = setup ();
  378. app.button.Y = Pos.Right (app.win);
  379. rs = Application.Begin (Application.Top);
  380. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  381. Application.RunLoop (rs);
  382. cleanup (rs);
  383. }
  384. [Fact]
  385. public void Center_SetsValue ()
  386. {
  387. var pos = Pos.Center ();
  388. Assert.Equal ("Pos.Center", pos.ToString ());
  389. }
  390. [Fact]
  391. public void Percent_SetsValue ()
  392. {
  393. float f = 0;
  394. var pos = Pos.Percent (f);
  395. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  396. f = 0.5F;
  397. pos = Pos.Percent (f);
  398. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  399. f = 100;
  400. pos = Pos.Percent (f);
  401. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  402. }
  403. [Fact]
  404. public void Percent_Equal ()
  405. {
  406. var n1 = 0;
  407. var n2 = 0;
  408. var pos1 = Pos.Percent (n1);
  409. var pos2 = Pos.Percent (n2);
  410. // BUGBUG: Pos.Percent should support equality
  411. Assert.NotEqual (pos1, pos2);
  412. }
  413. [Fact]
  414. public void Percent_ThrowsOnIvalid ()
  415. {
  416. var pos = Pos.Percent (0);
  417. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  418. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  419. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  420. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  421. }
  422. [Fact]
  423. public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  424. {
  425. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  426. var t = Application.Top;
  427. var w = new Window ("w") {
  428. X = Pos.Left (t) + 2,
  429. Y = Pos.At (2)
  430. };
  431. var v = new View ("v") {
  432. X = Pos.Center (),
  433. Y = Pos.Percent (10)
  434. };
  435. w.Add (v);
  436. t.Add (w);
  437. t.Ready += () => {
  438. Assert.Equal (2, w.X = 2);
  439. Assert.Equal (2, w.Y = 2);
  440. Assert.Throws<ArgumentException> (() => v.X = 2);
  441. Assert.Throws<ArgumentException> (() => v.Y = 2);
  442. };
  443. Application.Iteration += () => Application.RequestStop ();
  444. Application.Run ();
  445. Application.Shutdown ();
  446. }
  447. [Fact]
  448. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  449. {
  450. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  451. var t = Application.Top;
  452. var w = new Window (new Rect (1, 2, 4, 5), "w");
  453. t.Add (w);
  454. t.Ready += () => {
  455. Assert.Equal (2, w.X = 2);
  456. Assert.Equal (2, w.Y = 2);
  457. };
  458. Application.Iteration += () => Application.RequestStop ();
  459. Application.Run ();
  460. Application.Shutdown ();
  461. }
  462. [Fact]
  463. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  464. {
  465. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  466. var t = Application.Top;
  467. var w = new Window ("w") {
  468. X = Pos.Left (t) + 2,
  469. Y = Pos.At (2)
  470. };
  471. var v = new View ("v") {
  472. X = Pos.Center (),
  473. Y = Pos.Percent (10)
  474. };
  475. w.Add (v);
  476. t.Add (w);
  477. t.Ready += () => {
  478. v.LayoutStyle = LayoutStyle.Absolute;
  479. Assert.Equal (2, v.X = 2);
  480. Assert.Equal (2, v.Y = 2);
  481. };
  482. Application.Iteration += () => Application.RequestStop ();
  483. Application.Run ();
  484. Application.Shutdown ();
  485. }
  486. // DONE: Test PosCombine
  487. // DONE: Test operators
  488. [Fact]
  489. public void PosCombine_Do_Not_Throws ()
  490. {
  491. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  492. var t = Application.Top;
  493. var w = new Window ("w") {
  494. X = Pos.Left (t) + 2,
  495. Y = Pos.Top (t) + 2
  496. };
  497. var f = new FrameView ("f");
  498. var v1 = new View ("v1") {
  499. X = Pos.Left (w) + 2,
  500. Y = Pos.Top (w) + 2
  501. };
  502. var v2 = new View ("v2") {
  503. X = Pos.Left (v1) + 2,
  504. Y = Pos.Top (v1) + 2
  505. };
  506. f.Add (v1, v2);
  507. w.Add (f);
  508. t.Add (w);
  509. f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
  510. f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
  511. t.Ready += () => {
  512. Assert.Equal (0, t.Frame.X);
  513. Assert.Equal (0, t.Frame.Y);
  514. Assert.Equal (2, w.Frame.X);
  515. Assert.Equal (2, w.Frame.Y);
  516. Assert.Equal (2, f.Frame.X);
  517. Assert.Equal (2, f.Frame.Y);
  518. Assert.Equal (4, v1.Frame.X);
  519. Assert.Equal (4, v1.Frame.Y);
  520. Assert.Equal (6, v2.Frame.X);
  521. Assert.Equal (6, v2.Frame.Y);
  522. };
  523. Application.Iteration += () => Application.RequestStop ();
  524. Application.Run ();
  525. Application.Shutdown ();
  526. }
  527. [Fact]
  528. public void PosCombine_Will_Throws ()
  529. {
  530. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  531. var t = Application.Top;
  532. var w = new Window ("w") {
  533. X = Pos.Left (t) + 2,
  534. Y = Pos.Top (t) + 2
  535. };
  536. var f = new FrameView ("f");
  537. var v1 = new View ("v1") {
  538. X = Pos.Left (w) + 2,
  539. Y = Pos.Top (w) + 2
  540. };
  541. var v2 = new View ("v2") {
  542. X = Pos.Left (v1) + 2,
  543. Y = Pos.Top (v1) + 2
  544. };
  545. f.Add (v1); // v2 not added
  546. w.Add (f);
  547. t.Add (w);
  548. f.X = Pos.X (v2) - Pos.X (v1);
  549. f.Y = Pos.Y (v2) - Pos.Y (v1);
  550. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  551. Application.Shutdown ();
  552. }
  553. [Fact]
  554. public void Pos_Add_Operator ()
  555. {
  556. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  557. var top = Application.Top;
  558. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  559. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  560. var count = 0;
  561. field.KeyDown += (k) => {
  562. if (k.KeyEvent.Key == Key.Enter) {
  563. field.Text = $"Label {count}";
  564. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  565. view.Add (label);
  566. Assert.Equal ($"Label {count}", label.Text);
  567. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  568. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  569. field.Y += 1;
  570. count++;
  571. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  572. }
  573. };
  574. Application.Iteration += () => {
  575. while (count < 20) {
  576. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  577. }
  578. Application.RequestStop ();
  579. };
  580. var win = new Window ();
  581. win.Add (view);
  582. win.Add (field);
  583. top.Add (win);
  584. Application.Run (top);
  585. Assert.Equal (20, count);
  586. // Shutdown must be called to safely clean up Application if Init has been called
  587. Application.Shutdown ();
  588. }
  589. [Fact]
  590. public void Pos_Subtract_Operator ()
  591. {
  592. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  593. var top = Application.Top;
  594. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  595. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  596. var count = 20;
  597. var listLabels = new List<Label> ();
  598. for (int i = 0; i < count; i++) {
  599. field.Text = $"Label {i}";
  600. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  601. view.Add (label);
  602. Assert.Equal ($"Label {i}", label.Text);
  603. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  604. listLabels.Add (label);
  605. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  606. field.Y += 1;
  607. Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.ToString ());
  608. }
  609. field.KeyDown += (k) => {
  610. if (k.KeyEvent.Key == Key.Enter) {
  611. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  612. view.Remove (listLabels [count - 1]);
  613. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  614. field.Y -= 1;
  615. count--;
  616. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  617. }
  618. };
  619. Application.Iteration += () => {
  620. while (count > 0) {
  621. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  622. }
  623. Application.RequestStop ();
  624. };
  625. var win = new Window ();
  626. win.Add (view);
  627. win.Add (field);
  628. top.Add (win);
  629. Application.Run (top);
  630. Assert.Equal (0, count);
  631. // Shutdown must be called to safely clean up Application if Init has been called
  632. Application.Shutdown ();
  633. }
  634. [Fact]
  635. public void Internal_Tests ()
  636. {
  637. var posFactor = new Pos.PosFactor (0.10F);
  638. Assert.Equal (10, posFactor.Anchor (100));
  639. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  640. Assert.Equal (99, posAnchorEnd.Anchor (100));
  641. var posCenter = new Pos.PosCenter ();
  642. Assert.Equal (50, posCenter.Anchor (100));
  643. var posAbsolute = new Pos.PosAbsolute (10);
  644. Assert.Equal (10, posAbsolute.Anchor (0));
  645. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  646. Assert.Equal (posCombine.left, posFactor);
  647. Assert.Equal (posCombine.right, posAbsolute);
  648. Assert.Equal (20, posCombine.Anchor (100));
  649. var view = new View (new Rect (20, 10, 20, 1));
  650. var posViewX = new Pos.PosView (view, 0);
  651. Assert.Equal (20, posViewX.Anchor (0));
  652. var posViewY = new Pos.PosView (view, 1);
  653. Assert.Equal (10, posViewY.Anchor (0));
  654. var posRight = new Pos.PosView (view, 2);
  655. Assert.Equal (40, posRight.Anchor (0));
  656. var posViewBottom = new Pos.PosView (view, 3);
  657. Assert.Equal (11, posViewBottom.Anchor (0));
  658. }
  659. }
  660. }