PosTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. float n1 = 0;
  407. float n2 = 0;
  408. var pos1 = Pos.Percent (n1);
  409. var pos2 = Pos.Percent (n2);
  410. Assert.Equal (pos1, pos2);
  411. n1 = n2 = 1;
  412. pos1 = Pos.Percent (n1);
  413. pos2 = Pos.Percent (n2);
  414. Assert.Equal (pos1, pos2);
  415. n1 = n2 = 0.5f;
  416. pos1 = Pos.Percent (n1);
  417. pos2 = Pos.Percent (n2);
  418. Assert.Equal (pos1, pos2);
  419. n1 = n2 = 100f;
  420. pos1 = Pos.Percent (n1);
  421. pos2 = Pos.Percent (n2);
  422. Assert.Equal (pos1, pos2);
  423. n1 = 0;
  424. n2 = 1;
  425. pos1 = Pos.Percent (n1);
  426. pos2 = Pos.Percent (n2);
  427. Assert.NotEqual (pos1, pos2);
  428. n1 = 0.5f;
  429. n2 = 1.5f;
  430. pos1 = Pos.Percent (n1);
  431. pos2 = Pos.Percent (n2);
  432. Assert.NotEqual (pos1, pos2);
  433. }
  434. [Fact]
  435. public void Percent_ThrowsOnIvalid ()
  436. {
  437. var pos = Pos.Percent (0);
  438. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  439. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  440. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  441. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  442. }
  443. [Fact]
  444. public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  445. {
  446. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  447. var t = Application.Top;
  448. var w = new Window ("w") {
  449. X = Pos.Left (t) + 2,
  450. Y = Pos.At (2)
  451. };
  452. var v = new View ("v") {
  453. X = Pos.Center (),
  454. Y = Pos.Percent (10)
  455. };
  456. w.Add (v);
  457. t.Add (w);
  458. t.Ready += () => {
  459. Assert.Equal (2, w.X = 2);
  460. Assert.Equal (2, w.Y = 2);
  461. Assert.Throws<ArgumentException> (() => v.X = 2);
  462. Assert.Throws<ArgumentException> (() => v.Y = 2);
  463. };
  464. Application.Iteration += () => Application.RequestStop ();
  465. Application.Run ();
  466. Application.Shutdown ();
  467. }
  468. [Fact]
  469. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  470. {
  471. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  472. var t = Application.Top;
  473. var w = new Window (new Rect (1, 2, 4, 5), "w");
  474. t.Add (w);
  475. t.Ready += () => {
  476. Assert.Equal (2, w.X = 2);
  477. Assert.Equal (2, w.Y = 2);
  478. };
  479. Application.Iteration += () => Application.RequestStop ();
  480. Application.Run ();
  481. Application.Shutdown ();
  482. }
  483. [Fact]
  484. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  485. {
  486. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  487. var t = Application.Top;
  488. var w = new Window ("w") {
  489. X = Pos.Left (t) + 2,
  490. Y = Pos.At (2)
  491. };
  492. var v = new View ("v") {
  493. X = Pos.Center (),
  494. Y = Pos.Percent (10)
  495. };
  496. w.Add (v);
  497. t.Add (w);
  498. t.Ready += () => {
  499. v.LayoutStyle = LayoutStyle.Absolute;
  500. Assert.Equal (2, v.X = 2);
  501. Assert.Equal (2, v.Y = 2);
  502. };
  503. Application.Iteration += () => Application.RequestStop ();
  504. Application.Run ();
  505. Application.Shutdown ();
  506. }
  507. // DONE: Test PosCombine
  508. // DONE: Test operators
  509. [Fact]
  510. public void PosCombine_Do_Not_Throws ()
  511. {
  512. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  513. var t = Application.Top;
  514. var w = new Window ("w") {
  515. X = Pos.Left (t) + 2,
  516. Y = Pos.Top (t) + 2
  517. };
  518. var f = new FrameView ("f");
  519. var v1 = new View ("v1") {
  520. X = Pos.Left (w) + 2,
  521. Y = Pos.Top (w) + 2
  522. };
  523. var v2 = new View ("v2") {
  524. X = Pos.Left (v1) + 2,
  525. Y = Pos.Top (v1) + 2
  526. };
  527. f.Add (v1, v2);
  528. w.Add (f);
  529. t.Add (w);
  530. f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
  531. f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
  532. t.Ready += () => {
  533. Assert.Equal (0, t.Frame.X);
  534. Assert.Equal (0, t.Frame.Y);
  535. Assert.Equal (2, w.Frame.X);
  536. Assert.Equal (2, w.Frame.Y);
  537. Assert.Equal (2, f.Frame.X);
  538. Assert.Equal (2, f.Frame.Y);
  539. Assert.Equal (4, v1.Frame.X);
  540. Assert.Equal (4, v1.Frame.Y);
  541. Assert.Equal (6, v2.Frame.X);
  542. Assert.Equal (6, v2.Frame.Y);
  543. };
  544. Application.Iteration += () => Application.RequestStop ();
  545. Application.Run ();
  546. Application.Shutdown ();
  547. }
  548. [Fact]
  549. public void PosCombine_Will_Throws ()
  550. {
  551. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  552. var t = Application.Top;
  553. var w = new Window ("w") {
  554. X = Pos.Left (t) + 2,
  555. Y = Pos.Top (t) + 2
  556. };
  557. var f = new FrameView ("f");
  558. var v1 = new View ("v1") {
  559. X = Pos.Left (w) + 2,
  560. Y = Pos.Top (w) + 2
  561. };
  562. var v2 = new View ("v2") {
  563. X = Pos.Left (v1) + 2,
  564. Y = Pos.Top (v1) + 2
  565. };
  566. f.Add (v1); // v2 not added
  567. w.Add (f);
  568. t.Add (w);
  569. f.X = Pos.X (v2) - Pos.X (v1);
  570. f.Y = Pos.Y (v2) - Pos.Y (v1);
  571. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  572. Application.Shutdown ();
  573. }
  574. [Fact]
  575. public void Pos_Add_Operator ()
  576. {
  577. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  578. var top = Application.Top;
  579. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  580. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  581. var count = 0;
  582. field.KeyDown += (k) => {
  583. if (k.KeyEvent.Key == Key.Enter) {
  584. field.Text = $"Label {count}";
  585. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  586. view.Add (label);
  587. Assert.Equal ($"Label {count}", label.Text);
  588. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  589. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  590. field.Y += 1;
  591. count++;
  592. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  593. }
  594. };
  595. Application.Iteration += () => {
  596. while (count < 20) {
  597. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  598. }
  599. Application.RequestStop ();
  600. };
  601. var win = new Window ();
  602. win.Add (view);
  603. win.Add (field);
  604. top.Add (win);
  605. Application.Run (top);
  606. Assert.Equal (20, count);
  607. // Shutdown must be called to safely clean up Application if Init has been called
  608. Application.Shutdown ();
  609. }
  610. [Fact]
  611. public void Pos_Subtract_Operator ()
  612. {
  613. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  614. var top = Application.Top;
  615. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  616. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  617. var count = 20;
  618. var listLabels = new List<Label> ();
  619. for (int i = 0; i < count; i++) {
  620. field.Text = $"Label {i}";
  621. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  622. view.Add (label);
  623. Assert.Equal ($"Label {i}", label.Text);
  624. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  625. listLabels.Add (label);
  626. Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
  627. field.Y += 1;
  628. Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.ToString ());
  629. }
  630. field.KeyDown += (k) => {
  631. if (k.KeyEvent.Key == Key.Enter) {
  632. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  633. view.Remove (listLabels [count - 1]);
  634. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  635. field.Y -= 1;
  636. count--;
  637. Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
  638. }
  639. };
  640. Application.Iteration += () => {
  641. while (count > 0) {
  642. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  643. }
  644. Application.RequestStop ();
  645. };
  646. var win = new Window ();
  647. win.Add (view);
  648. win.Add (field);
  649. top.Add (win);
  650. Application.Run (top);
  651. Assert.Equal (0, count);
  652. // Shutdown must be called to safely clean up Application if Init has been called
  653. Application.Shutdown ();
  654. }
  655. [Fact]
  656. public void Internal_Tests ()
  657. {
  658. var posFactor = new Pos.PosFactor (0.10F);
  659. Assert.Equal (10, posFactor.Anchor (100));
  660. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  661. Assert.Equal (99, posAnchorEnd.Anchor (100));
  662. var posCenter = new Pos.PosCenter ();
  663. Assert.Equal (50, posCenter.Anchor (100));
  664. var posAbsolute = new Pos.PosAbsolute (10);
  665. Assert.Equal (10, posAbsolute.Anchor (0));
  666. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  667. Assert.Equal (posCombine.left, posFactor);
  668. Assert.Equal (posCombine.right, posAbsolute);
  669. Assert.Equal (20, posCombine.Anchor (100));
  670. var view = new View (new Rect (20, 10, 20, 1));
  671. var posViewX = new Pos.PosView (view, 0);
  672. Assert.Equal (20, posViewX.Anchor (0));
  673. var posViewY = new Pos.PosView (view, 1);
  674. Assert.Equal (10, posViewY.Anchor (0));
  675. var posRight = new Pos.PosView (view, 2);
  676. Assert.Equal (40, posRight.Anchor (0));
  677. var posViewBottom = new Pos.PosView (view, 3);
  678. Assert.Equal (11, posViewBottom.Anchor (0));
  679. }
  680. [Fact]
  681. public void Function_SetsValue ()
  682. {
  683. var text = "Test";
  684. var pos = Pos.Function (() => text.Length);
  685. Assert.Equal ("Pos.PosFunc(4)", pos.ToString ());
  686. text = "New Test";
  687. Assert.Equal ("Pos.PosFunc(8)", pos.ToString ());
  688. text = "";
  689. Assert.Equal ("Pos.PosFunc(0)", pos.ToString ());
  690. }
  691. [Fact]
  692. public void Function_Equal ()
  693. {
  694. var f1 = () => 0;
  695. var f2 = () => 0;
  696. var pos1 = Pos.Function (f1);
  697. var pos2 = Pos.Function (f2);
  698. Assert.Equal (pos1, pos2);
  699. f2 = () => 1;
  700. pos2 = Pos.Function (f2);
  701. Assert.NotEqual (pos1, pos2);
  702. }
  703. }
  704. }