PosTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 System.Runtime.InteropServices.WindowsRuntime;
  8. using Terminal.Gui;
  9. using Xunit;
  10. // Alais Console to MockConsole so we don't accidentally use Console
  11. using Console = Terminal.Gui.FakeConsole;
  12. namespace Terminal.Gui {
  13. public class PosTests {
  14. [Fact]
  15. public void New_Works ()
  16. {
  17. var pos = new Pos ();
  18. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  19. }
  20. [Fact]
  21. public void AnchorEnd_SetsValue ()
  22. {
  23. var n = 0;
  24. var pos = Pos.AnchorEnd ();
  25. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  26. n = 5;
  27. pos = Pos.AnchorEnd (n);
  28. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  29. }
  30. [Fact]
  31. public void AnchorEnd_Equal ()
  32. {
  33. var n1 = 0;
  34. var n2 = 0;
  35. var pos1 = Pos.AnchorEnd (n1);
  36. var pos2 = Pos.AnchorEnd (n2);
  37. Assert.Equal (pos1, pos2);
  38. // Test inequality
  39. n2 = 5;
  40. pos2 = Pos.AnchorEnd (n2);
  41. Assert.NotEqual (pos1, pos2);
  42. }
  43. [Fact]
  44. public void AnchorEnd_Negative_Throws ()
  45. {
  46. Pos pos;
  47. var n = -1;
  48. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  49. }
  50. [Fact]
  51. public void At_SetsValue ()
  52. {
  53. var pos = Pos.At (0);
  54. Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
  55. pos = Pos.At (5);
  56. Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
  57. pos = Pos.At (-1);
  58. Assert.Equal ("Pos.Absolute(-1)", pos.ToString ());
  59. }
  60. [Fact]
  61. public void At_Equal ()
  62. {
  63. var n1 = 0;
  64. var n2 = 0;
  65. var pos1 = Pos.At (n1);
  66. var pos2 = Pos.At (n2);
  67. Assert.Equal (pos1, pos2);
  68. }
  69. [Fact]
  70. public void SetSide_Null_Throws ()
  71. {
  72. var pos = Pos.Left (null);
  73. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  74. pos = Pos.X (null);
  75. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  76. pos = Pos.Top (null);
  77. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  78. pos = Pos.Y(null);
  79. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  80. pos = Pos.Bottom (null);
  81. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  82. pos = Pos.Right (null);
  83. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  84. }
  85. // TODO: Test Left, Top, Right bottom Equal
  86. /// <summary>
  87. /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
  88. /// </summary>
  89. [Fact]
  90. public void PosSide_SetsValue ()
  91. {
  92. string side; // used in format string
  93. var testRect = Rect.Empty;
  94. var testInt = 0;
  95. Pos pos;
  96. // Pos.Left
  97. side = "x";
  98. testInt = 0;
  99. testRect = Rect.Empty;
  100. pos = Pos.Left (new View ());
  101. 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 ());
  102. pos = Pos.Left (new View (testRect));
  103. 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 ());
  104. testRect = new Rect (1, 2, 3, 4);
  105. pos = Pos.Left (new View (testRect));
  106. 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 ());
  107. // Pos.Left(win) + 0
  108. pos = Pos.Left (new View (testRect)) + testInt;
  109. 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 ());
  110. testInt = 1;
  111. // Pos.Left(win) +1
  112. pos = Pos.Left (new View (testRect)) + testInt;
  113. 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 ());
  114. testInt = -1;
  115. // Pos.Left(win) -1
  116. pos = Pos.Left (new View (testRect)) - testInt;
  117. 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 ());
  118. // Pos.X
  119. side = "x";
  120. testInt = 0;
  121. testRect = Rect.Empty;
  122. pos = Pos.X (new View ());
  123. 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 ());
  124. pos = Pos.X (new View (testRect));
  125. 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 ());
  126. testRect = new Rect (1, 2, 3, 4);
  127. pos = Pos.X (new View (testRect));
  128. 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 ());
  129. // Pos.X(win) + 0
  130. pos = Pos.X (new View (testRect)) + testInt;
  131. 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 ());
  132. testInt = 1;
  133. // Pos.X(win) +1
  134. pos = Pos.X (new View (testRect)) + testInt;
  135. 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 ());
  136. testInt = -1;
  137. // Pos.X(win) -1
  138. pos = Pos.X (new View (testRect)) - testInt;
  139. 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 ());
  140. // Pos.Top
  141. side = "y";
  142. testInt = 0;
  143. testRect = Rect.Empty;
  144. pos = Pos.Top (new View ());
  145. 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 ());
  146. pos = Pos.Top (new View (testRect));
  147. 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 ());
  148. testRect = new Rect (1, 2, 3, 4);
  149. pos = Pos.Top (new View (testRect));
  150. 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 ());
  151. // Pos.Top(win) + 0
  152. pos = Pos.Top (new View (testRect)) + testInt;
  153. 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 ());
  154. testInt = 1;
  155. // Pos.Top(win) +1
  156. pos = Pos.Top (new View (testRect)) + testInt;
  157. 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 ());
  158. testInt = -1;
  159. // Pos.Top(win) -1
  160. pos = Pos.Top (new View (testRect)) - testInt;
  161. 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 ());
  162. // Pos.Y
  163. side = "y";
  164. testInt = 0;
  165. testRect = Rect.Empty;
  166. pos = Pos.Y (new View ());
  167. 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 ());
  168. pos = Pos.Y (new View (testRect));
  169. 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 ());
  170. testRect = new Rect (1, 2, 3, 4);
  171. pos = Pos.Y (new View (testRect));
  172. 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 ());
  173. // Pos.Y(win) + 0
  174. pos = Pos.Y (new View (testRect)) + testInt;
  175. 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 ());
  176. testInt = 1;
  177. // Pos.Y(win) +1
  178. pos = Pos.Y (new View (testRect)) + testInt;
  179. 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 ());
  180. testInt = -1;
  181. // Pos.Y(win) -1
  182. pos = Pos.Y (new View (testRect)) - testInt;
  183. 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 ());
  184. // Pos.Bottom
  185. side = "bottom";
  186. testRect = Rect.Empty;
  187. testInt = 0;
  188. pos = Pos.Bottom (new View ());
  189. 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 ());
  190. pos = Pos.Bottom (new View (testRect));
  191. 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 ());
  192. testRect = new Rect (1, 2, 3, 4);
  193. pos = Pos.Bottom (new View (testRect));
  194. 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 ());
  195. // Pos.Bottom(win) + 0
  196. pos = Pos.Bottom (new View (testRect)) + testInt;
  197. 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 ());
  198. testInt = 1;
  199. // Pos.Bottom(win) +1
  200. pos = Pos.Bottom (new View (testRect)) + testInt;
  201. 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 ());
  202. testInt = -1;
  203. // Pos.Bottom(win) -1
  204. pos = Pos.Bottom (new View (testRect)) - testInt;
  205. 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 ());
  206. }
  207. // See: https://github.com/migueldeicaza/gui.cs/issues/504
  208. [Fact]
  209. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  210. {
  211. // Setup Fake driver
  212. (Window win, Button button) setup ()
  213. {
  214. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  215. Application.Iteration = () => {
  216. Application.RequestStop ();
  217. };
  218. var win = new Window ("window") {
  219. X = 0,
  220. Y = 0,
  221. Width = Dim.Fill (),
  222. Height = Dim.Fill (),
  223. };
  224. Application.Top.Add (win);
  225. var button = new Button ("button") {
  226. X = Pos.Center (),
  227. };
  228. win.Add (button);
  229. return (win, button);
  230. }
  231. void cleanup ()
  232. {
  233. // Cleanup
  234. Application.Shutdown ();
  235. }
  236. // Test cases:
  237. var app = setup ();
  238. app.button.Y = Pos.Left (app.win);
  239. Application.Run ();
  240. cleanup ();
  241. app = setup ();
  242. app.button.Y = Pos.X (app.win);
  243. Application.Run ();
  244. cleanup ();
  245. app = setup ();
  246. app.button.Y = Pos.Top (app.win);
  247. Application.Run ();
  248. cleanup ();
  249. app = setup ();
  250. app.button.Y = Pos.Y (app.win);
  251. Application.Run ();
  252. cleanup ();
  253. app = setup ();
  254. app.button.Y = Pos.Bottom (app.win);
  255. Application.Run ();
  256. cleanup ();
  257. app = setup ();
  258. app.button.Y = Pos.Right (app.win);
  259. Application.Run ();
  260. cleanup ();
  261. }
  262. [Fact]
  263. public void Center_SetsValue ()
  264. {
  265. var pos = Pos.Center ();
  266. Assert.Equal ("Pos.Center", pos.ToString ());
  267. }
  268. [Fact]
  269. public void Percent_SetsValue ()
  270. {
  271. float f = 0;
  272. var pos = Pos.Percent (f);
  273. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  274. f = 0.5F;
  275. pos = Pos.Percent (f);
  276. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  277. f = 100;
  278. pos = Pos.Percent (f);
  279. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  280. }
  281. [Fact]
  282. public void Percent_Equal ()
  283. {
  284. var n1 = 0;
  285. var n2 = 0;
  286. var pos1 = Pos.Percent (n1);
  287. var pos2 = Pos.Percent (n2);
  288. // BUGBUG: Pos.Percent should support equality
  289. Assert.NotEqual (pos1, pos2);
  290. }
  291. [Fact]
  292. public void Percent_ThrowsOnIvalid ()
  293. {
  294. var pos = Pos.Percent (0);
  295. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  296. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  297. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  298. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  299. }
  300. // TODO: Test PosCombine
  301. // TODO: Test operators
  302. }
  303. }