PosTests.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. using Xunit.Abstractions;
  10. // Alias Console to MockConsole so we don't accidentally use Console
  11. using Console = Terminal.Gui.FakeConsole;
  12. namespace Terminal.Gui.ViewTests {
  13. public class LayoutTests_PosTests {
  14. readonly ITestOutputHelper output;
  15. public LayoutTests_PosTests (ITestOutputHelper output)
  16. {
  17. this.output = output;
  18. }
  19. [Fact]
  20. public void New_Works ()
  21. {
  22. var pos = new Pos ();
  23. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  24. }
  25. [Fact]
  26. public void AnchorEnd_SetsValue ()
  27. {
  28. var n = 0;
  29. var pos = Pos.AnchorEnd ();
  30. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  31. n = 5;
  32. pos = Pos.AnchorEnd (n);
  33. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  34. }
  35. [Fact]
  36. public void AnchorEnd_Equal ()
  37. {
  38. var n1 = 0;
  39. var n2 = 0;
  40. var pos1 = Pos.AnchorEnd (n1);
  41. var pos2 = Pos.AnchorEnd (n2);
  42. Assert.Equal (pos1, pos2);
  43. // Test inequality
  44. n2 = 5;
  45. pos2 = Pos.AnchorEnd (n2);
  46. Assert.NotEqual (pos1, pos2);
  47. }
  48. [Fact]
  49. [AutoInitShutdown]
  50. public void AnchorEnd_Equal_Inside_Window ()
  51. {
  52. var viewWidth = 10;
  53. var viewHeight = 1;
  54. var tv = new TextView () {
  55. X = Pos.AnchorEnd (viewWidth),
  56. Y = Pos.AnchorEnd (viewHeight),
  57. Width = viewWidth,
  58. Height = viewHeight
  59. };
  60. var win = new Window ();
  61. win.Add (tv);
  62. var top = Application.Top;
  63. top.Add (win);
  64. var rs = Application.Begin (top);
  65. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  66. Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
  67. Assert.Equal (new Rect (68, 22, 10, 1), tv.Frame);
  68. Application.End (rs);
  69. }
  70. [Fact]
  71. [AutoInitShutdown]
  72. public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  73. {
  74. var viewWidth = 10;
  75. var viewHeight = 1;
  76. var tv = new TextView () {
  77. X = Pos.AnchorEnd (viewWidth),
  78. Y = Pos.AnchorEnd (viewHeight),
  79. Width = viewWidth,
  80. Height = viewHeight
  81. };
  82. var win = new Window ();
  83. win.Add (tv);
  84. var menu = new MenuBar ();
  85. var status = new StatusBar ();
  86. var top = Application.Top;
  87. top.Add (win, menu, status);
  88. var rs = Application.Begin (top);
  89. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  90. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  91. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  92. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  93. Assert.Equal (new Rect (68, 20, 10, 1), tv.Frame);
  94. Application.End (rs);
  95. }
  96. [Fact]
  97. [AutoInitShutdown]
  98. public void Bottom_Equal_Inside_Window ()
  99. {
  100. var win = new Window ();
  101. var label = new Label ("This should be the last line.") {
  102. TextAlignment = TextAlignment.Centered,
  103. ColorScheme = Colors.Menu,
  104. Width = Dim.Fill (),
  105. X = Pos.Center (),
  106. Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border
  107. };
  108. win.Add (label);
  109. var top = Application.Top;
  110. top.Add (win);
  111. var rs = Application.Begin (top);
  112. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  113. Assert.True (label.AutoSize);
  114. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  115. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  116. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  117. var expected = @"
  118. ┌──────────────────────────────────────┐
  119. │ │
  120. │ │
  121. │ │
  122. │ │
  123. │ │
  124. │ │
  125. │ │
  126. │ This should be the last line. │
  127. └──────────────────────────────────────┘
  128. ";
  129. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  130. Application.End (rs);
  131. }
  132. [Fact]
  133. [AutoInitShutdown]
  134. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  135. {
  136. var win = new Window ();
  137. var label = new Label ("This should be the last line.") {
  138. TextAlignment = TextAlignment.Centered,
  139. ColorScheme = Colors.Menu,
  140. Width = Dim.Fill (),
  141. X = Pos.Center (),
  142. Y = Pos.AnchorEnd (1)
  143. };
  144. win.Add (label);
  145. var top = Application.Top;
  146. top.Add (win);
  147. var rs = Application.Begin (top);
  148. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  149. Assert.True (label.AutoSize);
  150. Assert.Equal (29, label.Text.Length);
  151. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  152. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  153. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  154. var expected = @"
  155. ┌──────────────────────────────────────┐
  156. │ │
  157. │ │
  158. │ │
  159. │ │
  160. │ │
  161. │ │
  162. │ │
  163. │ This should be the last line. │
  164. └──────────────────────────────────────┘
  165. ";
  166. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  167. Application.End (rs);
  168. }
  169. [Fact]
  170. [AutoInitShutdown]
  171. public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  172. {
  173. var win = new Window ();
  174. var label = new Label ("This should be the last line.") {
  175. TextAlignment = TextAlignment.Centered,
  176. ColorScheme = Colors.Menu,
  177. Width = Dim.Fill (),
  178. X = Pos.Center (),
  179. Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
  180. };
  181. win.Add (label);
  182. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  183. var status = new StatusBar (new StatusItem [] { new (Key.F1, "~F1~ Help", null) });
  184. var top = Application.Top;
  185. top.Add (win, menu, status);
  186. var rs = Application.Begin (top);
  187. Assert.True (label.AutoSize);
  188. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  189. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  190. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  191. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  192. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  193. var expected = @"
  194. Menu
  195. ┌──────────────────────────────────────────────────────────────────────────────┐
  196. │ │
  197. │ │
  198. │ │
  199. │ │
  200. │ │
  201. │ │
  202. │ │
  203. │ │
  204. │ │
  205. │ │
  206. │ │
  207. │ │
  208. │ │
  209. │ │
  210. │ │
  211. │ │
  212. │ │
  213. │ │
  214. │ │
  215. │ │
  216. │ This should be the last line. │
  217. └──────────────────────────────────────────────────────────────────────────────┘
  218. F1 Help
  219. ";
  220. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  221. Application.End (rs);
  222. }
  223. [Fact]
  224. [AutoInitShutdown]
  225. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  226. {
  227. var win = new Window ();
  228. var label = new Label ("This should be the last line.") {
  229. TextAlignment = TextAlignment.Centered,
  230. ColorScheme = Colors.Menu,
  231. Width = Dim.Fill (),
  232. X = Pos.Center (),
  233. Y = Pos.AnchorEnd (1)
  234. };
  235. win.Add (label);
  236. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  237. var status = new StatusBar (new StatusItem [] { new (Key.F1, "~F1~ Help", null) });
  238. var top = Application.Top;
  239. top.Add (win, menu, status);
  240. var rs = Application.Begin (top);
  241. Assert.True (label.AutoSize);
  242. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  243. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  244. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  245. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  246. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  247. var expected = @"
  248. Menu
  249. ┌──────────────────────────────────────────────────────────────────────────────┐
  250. │ │
  251. │ │
  252. │ │
  253. │ │
  254. │ │
  255. │ │
  256. │ │
  257. │ │
  258. │ │
  259. │ │
  260. │ │
  261. │ │
  262. │ │
  263. │ │
  264. │ │
  265. │ │
  266. │ │
  267. │ │
  268. │ │
  269. │ │
  270. │ This should be the last line. │
  271. └──────────────────────────────────────────────────────────────────────────────┘
  272. F1 Help
  273. ";
  274. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  275. Application.End (rs);
  276. }
  277. [Fact]
  278. public void AnchorEnd_Negative_Throws ()
  279. {
  280. Pos pos;
  281. var n = -1;
  282. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  283. }
  284. [Fact]
  285. public void At_SetsValue ()
  286. {
  287. var pos = Pos.At (0);
  288. Assert.Equal ("Absolute(0)", pos.ToString ());
  289. pos = Pos.At (5);
  290. Assert.Equal ("Absolute(5)", pos.ToString ());
  291. pos = Pos.At (-1);
  292. Assert.Equal ("Absolute(-1)", pos.ToString ());
  293. }
  294. [Fact]
  295. public void At_Equal ()
  296. {
  297. var n1 = 0;
  298. var n2 = 0;
  299. var pos1 = Pos.At (n1);
  300. var pos2 = Pos.At (n2);
  301. Assert.Equal (pos1, pos2);
  302. }
  303. [Fact]
  304. public void SetSide_Null_Throws ()
  305. {
  306. var pos = Pos.Left (null);
  307. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  308. pos = Pos.X (null);
  309. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  310. pos = Pos.Top (null);
  311. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  312. pos = Pos.Y (null);
  313. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  314. pos = Pos.Bottom (null);
  315. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  316. pos = Pos.Right (null);
  317. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  318. }
  319. // TODO: Test Left, Top, Right bottom Equal
  320. /// <summary>
  321. /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
  322. /// </summary>
  323. [Fact]
  324. public void PosSide_SetsValue ()
  325. {
  326. string side; // used in format string
  327. var testRect = Rect.Empty;
  328. var testInt = 0;
  329. Pos pos;
  330. // Pos.Left
  331. side = "x";
  332. testInt = 0;
  333. testRect = Rect.Empty;
  334. pos = Pos.Left (new View ());
  335. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  336. pos = Pos.Left (new View (testRect));
  337. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  338. testRect = new Rect (1, 2, 3, 4);
  339. pos = Pos.Left (new View (testRect));
  340. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  341. // Pos.Left(win) + 0
  342. pos = Pos.Left (new View (testRect)) + testInt;
  343. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  344. testInt = 1;
  345. // Pos.Left(win) +1
  346. pos = Pos.Left (new View (testRect)) + testInt;
  347. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  348. testInt = -1;
  349. // Pos.Left(win) -1
  350. pos = Pos.Left (new View (testRect)) - testInt;
  351. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  352. // Pos.X
  353. side = "x";
  354. testInt = 0;
  355. testRect = Rect.Empty;
  356. pos = Pos.X (new View ());
  357. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  358. pos = Pos.X (new View (testRect));
  359. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  360. testRect = new Rect (1, 2, 3, 4);
  361. pos = Pos.X (new View (testRect));
  362. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  363. // Pos.X(win) + 0
  364. pos = Pos.X (new View (testRect)) + testInt;
  365. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  366. testInt = 1;
  367. // Pos.X(win) +1
  368. pos = Pos.X (new View (testRect)) + testInt;
  369. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  370. testInt = -1;
  371. // Pos.X(win) -1
  372. pos = Pos.X (new View (testRect)) - testInt;
  373. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  374. // Pos.Top
  375. side = "y";
  376. testInt = 0;
  377. testRect = Rect.Empty;
  378. pos = Pos.Top (new View ());
  379. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  380. pos = Pos.Top (new View (testRect));
  381. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  382. testRect = new Rect (1, 2, 3, 4);
  383. pos = Pos.Top (new View (testRect));
  384. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  385. // Pos.Top(win) + 0
  386. pos = Pos.Top (new View (testRect)) + testInt;
  387. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  388. testInt = 1;
  389. // Pos.Top(win) +1
  390. pos = Pos.Top (new View (testRect)) + testInt;
  391. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  392. testInt = -1;
  393. // Pos.Top(win) -1
  394. pos = Pos.Top (new View (testRect)) - testInt;
  395. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  396. // Pos.Y
  397. side = "y";
  398. testInt = 0;
  399. testRect = Rect.Empty;
  400. pos = Pos.Y (new View ());
  401. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  402. pos = Pos.Y (new View (testRect));
  403. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  404. testRect = new Rect (1, 2, 3, 4);
  405. pos = Pos.Y (new View (testRect));
  406. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  407. // Pos.Y(win) + 0
  408. pos = Pos.Y (new View (testRect)) + testInt;
  409. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  410. testInt = 1;
  411. // Pos.Y(win) +1
  412. pos = Pos.Y (new View (testRect)) + testInt;
  413. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  414. testInt = -1;
  415. // Pos.Y(win) -1
  416. pos = Pos.Y (new View (testRect)) - testInt;
  417. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  418. // Pos.Bottom
  419. side = "bottom";
  420. testRect = Rect.Empty;
  421. testInt = 0;
  422. pos = Pos.Bottom (new View ());
  423. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  424. pos = Pos.Bottom (new View (testRect));
  425. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  426. testRect = new Rect (1, 2, 3, 4);
  427. pos = Pos.Bottom (new View (testRect));
  428. Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  429. // Pos.Bottom(win) + 0
  430. pos = Pos.Bottom (new View (testRect)) + testInt;
  431. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  432. testInt = 1;
  433. // Pos.Bottom(win) +1
  434. pos = Pos.Bottom (new View (testRect)) + testInt;
  435. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  436. testInt = -1;
  437. // Pos.Bottom(win) -1
  438. pos = Pos.Bottom (new View (testRect)) - testInt;
  439. Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
  440. #if DEBUG_IDISPOSABLE
  441. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  442. Responder.Instances.Clear ();
  443. #endif
  444. }
  445. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  446. [Fact]
  447. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  448. {
  449. // Setup Fake driver
  450. (Window win, Button button) setup ()
  451. {
  452. Application.Init (new FakeDriver ());
  453. Application.Iteration = () => {
  454. Application.RequestStop ();
  455. };
  456. var win = new Window () {
  457. X = 0,
  458. Y = 0,
  459. Width = Dim.Fill (),
  460. Height = Dim.Fill (),
  461. };
  462. Application.Top.Add (win);
  463. var button = new Button ("button") {
  464. X = Pos.Center (),
  465. };
  466. win.Add (button);
  467. return (win, button);
  468. }
  469. RunState rs;
  470. void cleanup (RunState rs)
  471. {
  472. // Cleanup
  473. Application.End (rs);
  474. // Shutdown must be called to safely clean up Application if Init has been called
  475. Application.Shutdown ();
  476. }
  477. // Test cases:
  478. var app = setup ();
  479. app.button.Y = Pos.Left (app.win);
  480. rs = Application.Begin (Application.Top);
  481. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  482. Application.RunLoop (rs);
  483. cleanup (rs);
  484. app = setup ();
  485. app.button.Y = Pos.X (app.win);
  486. rs = Application.Begin (Application.Top);
  487. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  488. Application.RunLoop (rs);
  489. cleanup (rs);
  490. app = setup ();
  491. app.button.Y = Pos.Top (app.win);
  492. rs = Application.Begin (Application.Top);
  493. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  494. Application.RunLoop (rs);
  495. cleanup (rs);
  496. app = setup ();
  497. app.button.Y = Pos.Y (app.win);
  498. rs = Application.Begin (Application.Top);
  499. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  500. Application.RunLoop (rs);
  501. cleanup (rs);
  502. app = setup ();
  503. app.button.Y = Pos.Bottom (app.win);
  504. rs = Application.Begin (Application.Top);
  505. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  506. Application.RunLoop (rs);
  507. cleanup (rs);
  508. app = setup ();
  509. app.button.Y = Pos.Right (app.win);
  510. rs = Application.Begin (Application.Top);
  511. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  512. Application.RunLoop (rs);
  513. cleanup (rs);
  514. #if DEBUG_IDISPOSABLE
  515. Assert.Empty (Responder.Instances);
  516. #endif
  517. }
  518. [Fact]
  519. public void Center_SetsValue ()
  520. {
  521. var pos = Pos.Center ();
  522. Assert.Equal ("Center", pos.ToString ());
  523. }
  524. [Fact]
  525. public void Percent_SetsValue ()
  526. {
  527. float f = 0;
  528. var pos = Pos.Percent (f);
  529. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  530. f = 0.5F;
  531. pos = Pos.Percent (f);
  532. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  533. f = 100;
  534. pos = Pos.Percent (f);
  535. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  536. }
  537. [Fact]
  538. public void Percent_Equal ()
  539. {
  540. float n1 = 0;
  541. float n2 = 0;
  542. var pos1 = Pos.Percent (n1);
  543. var pos2 = Pos.Percent (n2);
  544. Assert.Equal (pos1, pos2);
  545. n1 = n2 = 1;
  546. pos1 = Pos.Percent (n1);
  547. pos2 = Pos.Percent (n2);
  548. Assert.Equal (pos1, pos2);
  549. n1 = n2 = 0.5f;
  550. pos1 = Pos.Percent (n1);
  551. pos2 = Pos.Percent (n2);
  552. Assert.Equal (pos1, pos2);
  553. n1 = n2 = 100f;
  554. pos1 = Pos.Percent (n1);
  555. pos2 = Pos.Percent (n2);
  556. Assert.Equal (pos1, pos2);
  557. n1 = 0;
  558. n2 = 1;
  559. pos1 = Pos.Percent (n1);
  560. pos2 = Pos.Percent (n2);
  561. Assert.NotEqual (pos1, pos2);
  562. n1 = 0.5f;
  563. n2 = 1.5f;
  564. pos1 = Pos.Percent (n1);
  565. pos2 = Pos.Percent (n2);
  566. Assert.NotEqual (pos1, pos2);
  567. }
  568. [Fact]
  569. public void Percent_ThrowsOnIvalid ()
  570. {
  571. var pos = Pos.Percent (0);
  572. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  573. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  574. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  575. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  576. }
  577. [Fact]
  578. public void ForceValidatePosDim_True_Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  579. {
  580. Application.Init (new FakeDriver ());
  581. var t = Application.Top;
  582. var w = new Window () {
  583. X = Pos.Left (t) + 2,
  584. Y = Pos.At (2)
  585. };
  586. var v = new View () {
  587. X = Pos.Center (),
  588. Y = Pos.Percent (10),
  589. ForceValidatePosDim = true
  590. };
  591. w.Add (v);
  592. t.Add (w);
  593. t.Ready += (s, e) => {
  594. Assert.Equal (2, w.X = 2);
  595. Assert.Equal (2, w.Y = 2);
  596. Assert.Throws<ArgumentException> (() => v.X = 2);
  597. Assert.Throws<ArgumentException> (() => v.Y = 2);
  598. };
  599. Application.Iteration += () => Application.RequestStop ();
  600. Application.Run ();
  601. Application.Shutdown ();
  602. }
  603. [Fact]
  604. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  605. {
  606. Application.Init (new FakeDriver ());
  607. var t = Application.Top;
  608. var w = new Window (new Rect (1, 2, 4, 5));
  609. t.Add (w);
  610. t.Ready += (s, e) => {
  611. Assert.Equal (2, w.X = 2);
  612. Assert.Equal (2, w.Y = 2);
  613. };
  614. Application.Iteration += () => Application.RequestStop ();
  615. Application.Run ();
  616. Application.Shutdown ();
  617. }
  618. [Fact]
  619. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  620. {
  621. Application.Init (new FakeDriver ());
  622. var t = Application.Top;
  623. var w = new Window () {
  624. X = Pos.Left (t) + 2,
  625. Y = Pos.At (2)
  626. };
  627. var v = new View () {
  628. X = Pos.Center (),
  629. Y = Pos.Percent (10)
  630. };
  631. w.Add (v);
  632. t.Add (w);
  633. t.Ready += (s, e) => {
  634. v.LayoutStyle = LayoutStyle.Absolute;
  635. Assert.Equal (2, v.X = 2);
  636. Assert.Equal (2, v.Y = 2);
  637. };
  638. Application.Iteration += () => Application.RequestStop ();
  639. Application.Run ();
  640. Application.Shutdown ();
  641. }
  642. // DONE: Test PosCombine
  643. // DONE: Test operators
  644. // BUGBUG: v2 - This test is bogus. v1 and references it's superview's (f) superview (w).
  645. //[Fact]
  646. //public void PosCombine_Do_Not_Throws ()
  647. //{
  648. // Application.Init (new FakeDriver ());
  649. // var w = new Window () {
  650. // X = Pos.Left (Application.Top) + 2,
  651. // Y = Pos.Top (Application.Top) + 2
  652. // };
  653. // var f = new FrameView ();
  654. // var v1 = new View () {
  655. // X = Pos.Left (w) + 2,
  656. // Y = Pos.Top (w) + 2
  657. // };
  658. // var v2 = new View () {
  659. // X = Pos.Left (v1) + 2,
  660. // Y = Pos.Top (v1) + 2
  661. // };
  662. // f.Add (v1, v2);
  663. // w.Add (f);
  664. // Application.Top.Add (w);
  665. // f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  666. // f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  667. // Application.Top.LayoutComplete += (s, e) => {
  668. // Assert.Equal (0, Application.Top.Frame.X);
  669. // Assert.Equal (0, Application.Top.Frame.Y);
  670. // Assert.Equal (2, w.Frame.X);
  671. // Assert.Equal (2, w.Frame.Y);
  672. // Assert.Equal (2, f.Frame.X);
  673. // Assert.Equal (2, f.Frame.Y);
  674. // Assert.Equal (4, v1.Frame.X);
  675. // Assert.Equal (4, v1.Frame.Y);
  676. // Assert.Equal (6, v2.Frame.X);
  677. // Assert.Equal (6, v2.Frame.Y);
  678. // };
  679. // Application.Iteration += () => Application.RequestStop ();
  680. // Application.Run ();
  681. // Application.Shutdown ();
  682. //}
  683. [Fact]
  684. public void PosCombine_Will_Throws ()
  685. {
  686. Application.Init (new FakeDriver ());
  687. var t = Application.Top;
  688. var w = new Window () {
  689. X = Pos.Left (t) + 2,
  690. Y = Pos.Top (t) + 2
  691. };
  692. var f = new FrameView ();
  693. var v1 = new View () {
  694. X = Pos.Left (w) + 2,
  695. Y = Pos.Top (w) + 2
  696. };
  697. var v2 = new View () {
  698. X = Pos.Left (v1) + 2,
  699. Y = Pos.Top (v1) + 2
  700. };
  701. f.Add (v1); // v2 not added
  702. w.Add (f);
  703. t.Add (w);
  704. f.X = Pos.X (v2) - Pos.X (v1);
  705. f.Y = Pos.Y (v2) - Pos.Y (v1);
  706. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  707. Application.Shutdown ();
  708. v2.Dispose ();
  709. #if DEBUG_IDISPOSABLE
  710. Assert.Empty (Responder.Instances);
  711. #endif
  712. }
  713. [Fact]
  714. public void Pos_Add_Operator ()
  715. {
  716. Application.Init (new FakeDriver ());
  717. var top = Application.Top;
  718. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  719. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  720. var count = 0;
  721. field.KeyDown += (s, k) => {
  722. if (k.KeyEvent.Key == Key.Enter) {
  723. field.Text = $"Label {count}";
  724. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  725. view.Add (label);
  726. Assert.Equal ($"Label {count}", label.Text);
  727. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  728. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  729. field.Y += 1;
  730. count++;
  731. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  732. }
  733. };
  734. Application.Iteration += () => {
  735. while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  736. Application.RequestStop ();
  737. };
  738. var win = new Window ();
  739. win.Add (view);
  740. win.Add (field);
  741. top.Add (win);
  742. Application.Run (top);
  743. Assert.Equal (20, count);
  744. // Shutdown must be called to safely clean up Application if Init has been called
  745. Application.Shutdown ();
  746. #if DEBUG_IDISPOSABLE
  747. Assert.Empty (Responder.Instances);
  748. #endif
  749. }
  750. [Fact]
  751. public void Pos_Subtract_Operator ()
  752. {
  753. Application.Init (new FakeDriver ());
  754. var top = Application.Top;
  755. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  756. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  757. var count = 20;
  758. var listLabels = new List<Label> ();
  759. for (int i = 0; i < count; i++) {
  760. field.Text = $"Label {i}";
  761. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  762. view.Add (label);
  763. Assert.Equal ($"Label {i}", label.Text);
  764. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  765. listLabels.Add (label);
  766. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  767. field.Y += 1;
  768. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  769. }
  770. field.KeyDown += (s, k) => {
  771. if (k.KeyEvent.Key == Key.Enter) {
  772. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  773. view.Remove (listLabels [count - 1]);
  774. listLabels [count - 1].Dispose ();
  775. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  776. field.Y -= 1;
  777. count--;
  778. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  779. }
  780. };
  781. Application.Iteration += () => {
  782. while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  783. Application.RequestStop ();
  784. };
  785. var win = new Window ();
  786. win.Add (view);
  787. win.Add (field);
  788. top.Add (win);
  789. Application.Run (top);
  790. Assert.Equal (0, count);
  791. // Shutdown must be called to safely clean up Application if Init has been called
  792. Application.Shutdown ();
  793. #if DEBUG_IDISPOSABLE
  794. Assert.Empty (Responder.Instances);
  795. #endif
  796. }
  797. [Fact]
  798. public void Internal_Tests ()
  799. {
  800. var posFactor = new Pos.PosFactor (0.10F);
  801. Assert.Equal (10, posFactor.Anchor (100));
  802. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  803. Assert.Equal (99, posAnchorEnd.Anchor (100));
  804. var posCenter = new Pos.PosCenter ();
  805. Assert.Equal (50, posCenter.Anchor (100));
  806. var posAbsolute = new Pos.PosAbsolute (10);
  807. Assert.Equal (10, posAbsolute.Anchor (0));
  808. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  809. Assert.Equal (posCombine.left, posFactor);
  810. Assert.Equal (posCombine.right, posAbsolute);
  811. Assert.Equal (20, posCombine.Anchor (100));
  812. posCombine = new Pos.PosCombine (true, posAbsolute, posFactor);
  813. Assert.Equal (posCombine.left, posAbsolute);
  814. Assert.Equal (posCombine.right, posFactor);
  815. Assert.Equal (20, posCombine.Anchor (100));
  816. var view = new View (new Rect (20, 10, 20, 1));
  817. var posViewX = new Pos.PosView (view, 0);
  818. Assert.Equal (20, posViewX.Anchor (0));
  819. var posViewY = new Pos.PosView (view, 1);
  820. Assert.Equal (10, posViewY.Anchor (0));
  821. var posRight = new Pos.PosView (view, 2);
  822. Assert.Equal (40, posRight.Anchor (0));
  823. var posViewBottom = new Pos.PosView (view, 3);
  824. Assert.Equal (11, posViewBottom.Anchor (0));
  825. view.Dispose ();
  826. }
  827. [Fact]
  828. public void Function_SetsValue ()
  829. {
  830. var text = "Test";
  831. var pos = Pos.Function (() => text.Length);
  832. Assert.Equal ("PosFunc(4)", pos.ToString ());
  833. text = "New Test";
  834. Assert.Equal ("PosFunc(8)", pos.ToString ());
  835. text = "";
  836. Assert.Equal ("PosFunc(0)", pos.ToString ());
  837. }
  838. [Fact]
  839. public void Function_Equal ()
  840. {
  841. var f1 = () => 0;
  842. var f2 = () => 0;
  843. var pos1 = Pos.Function (f1);
  844. var pos2 = Pos.Function (f2);
  845. Assert.Equal (pos1, pos2);
  846. f2 = () => 1;
  847. pos2 = Pos.Function (f2);
  848. Assert.NotEqual (pos1, pos2);
  849. }
  850. [Theory, AutoInitShutdown]
  851. [InlineData (true)]
  852. [InlineData (false)]
  853. public void PosPercentPlusOne (bool testHorizontal)
  854. {
  855. var container = new View {
  856. Width = 100,
  857. Height = 100,
  858. };
  859. var label = new Label {
  860. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  861. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  862. Width = 10,
  863. Height = 10,
  864. };
  865. container.Add (label);
  866. Application.Top.Add (container);
  867. Application.Top.LayoutSubviews ();
  868. Assert.Equal (100, container.Frame.Width);
  869. Assert.Equal (100, container.Frame.Height);
  870. if (testHorizontal) {
  871. Assert.Equal (61, label.Frame.X);
  872. Assert.Equal (1, label.Frame.Y);
  873. } else {
  874. Assert.Equal (1, label.Frame.X);
  875. Assert.Equal (61, label.Frame.Y);
  876. }
  877. }
  878. [Fact]
  879. public void PosCombine_Referencing_Same_View ()
  880. {
  881. var super = new View ("super") {
  882. Width = 10,
  883. Height = 10
  884. };
  885. var view1 = new View ("view1") {
  886. Width = 2,
  887. Height = 2,
  888. };
  889. var view2 = new View ("view2") {
  890. Width = 2,
  891. Height = 2,
  892. };
  893. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  894. super.Add (view1, view2);
  895. super.BeginInit ();
  896. super.EndInit ();
  897. var exception = Record.Exception (super.LayoutSubviews);
  898. Assert.Null (exception);
  899. Assert.Equal (new Rect (0, 0, 10, 10), super.Frame);
  900. Assert.Equal (new Rect (0, 0, 2, 2), view1.Frame);
  901. Assert.Equal (new Rect (8, 0, 2, 2), view2.Frame);
  902. super.Dispose ();
  903. }
  904. }
  905. }