PosTests.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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, TestRespondersDisposed]
  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, TestRespondersDisposed]
  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 += (s, a) => {
  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. }
  515. [Fact]
  516. public void Center_SetsValue ()
  517. {
  518. var pos = Pos.Center ();
  519. Assert.Equal ("Center", pos.ToString ());
  520. }
  521. [Fact]
  522. public void Percent_SetsValue ()
  523. {
  524. float f = 0;
  525. var pos = Pos.Percent (f);
  526. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  527. f = 0.5F;
  528. pos = Pos.Percent (f);
  529. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  530. f = 100;
  531. pos = Pos.Percent (f);
  532. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  533. }
  534. [Fact]
  535. public void Percent_Equal ()
  536. {
  537. float n1 = 0;
  538. float n2 = 0;
  539. var pos1 = Pos.Percent (n1);
  540. var pos2 = Pos.Percent (n2);
  541. Assert.Equal (pos1, pos2);
  542. n1 = n2 = 1;
  543. pos1 = Pos.Percent (n1);
  544. pos2 = Pos.Percent (n2);
  545. Assert.Equal (pos1, pos2);
  546. n1 = n2 = 0.5f;
  547. pos1 = Pos.Percent (n1);
  548. pos2 = Pos.Percent (n2);
  549. Assert.Equal (pos1, pos2);
  550. n1 = n2 = 100f;
  551. pos1 = Pos.Percent (n1);
  552. pos2 = Pos.Percent (n2);
  553. Assert.Equal (pos1, pos2);
  554. n1 = 0;
  555. n2 = 1;
  556. pos1 = Pos.Percent (n1);
  557. pos2 = Pos.Percent (n2);
  558. Assert.NotEqual (pos1, pos2);
  559. n1 = 0.5f;
  560. n2 = 1.5f;
  561. pos1 = Pos.Percent (n1);
  562. pos2 = Pos.Percent (n2);
  563. Assert.NotEqual (pos1, pos2);
  564. }
  565. [Fact]
  566. public void Percent_ThrowsOnIvalid ()
  567. {
  568. var pos = Pos.Percent (0);
  569. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  570. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  571. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  572. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  573. }
  574. [Fact]
  575. public void ForceValidatePosDim_True_Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
  576. {
  577. Application.Init (new FakeDriver ());
  578. var t = Application.Top;
  579. var w = new Window () {
  580. X = Pos.Left (t) + 2,
  581. Y = Pos.At (2)
  582. };
  583. var v = new View () {
  584. X = Pos.Center (),
  585. Y = Pos.Percent (10),
  586. ForceValidatePosDim = true
  587. };
  588. w.Add (v);
  589. t.Add (w);
  590. t.Ready += (s, e) => {
  591. Assert.Equal (2, w.X = 2);
  592. Assert.Equal (2, w.Y = 2);
  593. Assert.Throws<ArgumentException> (() => v.X = 2);
  594. Assert.Throws<ArgumentException> (() => v.Y = 2);
  595. };
  596. Application.Iteration += (s, a) => Application.RequestStop ();
  597. Application.Run ();
  598. Application.Shutdown ();
  599. }
  600. [Fact]
  601. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  602. {
  603. Application.Init (new FakeDriver ());
  604. var t = Application.Top;
  605. var w = new Window (new Rect (1, 2, 4, 5));
  606. t.Add (w);
  607. t.Ready += (s, e) => {
  608. Assert.Equal (2, w.X = 2);
  609. Assert.Equal (2, w.Y = 2);
  610. };
  611. Application.Iteration += (s, a) => Application.RequestStop ();
  612. Application.Run ();
  613. Application.Shutdown ();
  614. }
  615. [Fact]
  616. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  617. {
  618. Application.Init (new FakeDriver ());
  619. var t = Application.Top;
  620. var w = new Window () {
  621. X = Pos.Left (t) + 2,
  622. Y = Pos.At (2)
  623. };
  624. var v = new View () {
  625. X = Pos.Center (),
  626. Y = Pos.Percent (10)
  627. };
  628. w.Add (v);
  629. t.Add (w);
  630. t.Ready += (s, e) => {
  631. v.LayoutStyle = LayoutStyle.Absolute;
  632. Assert.Equal (2, v.X = 2);
  633. Assert.Equal (2, v.Y = 2);
  634. };
  635. Application.Iteration += (s, a) => Application.RequestStop ();
  636. Application.Run ();
  637. Application.Shutdown ();
  638. }
  639. // DONE: Test PosCombine
  640. // DONE: Test operators
  641. // BUGBUG: v2 - This test is bogus. v1 and references it's superview's (f) superview (w).
  642. //[Fact]
  643. //public void PosCombine_Do_Not_Throws ()
  644. //{
  645. // Application.Init (new FakeDriver ());
  646. // var w = new Window () {
  647. // X = Pos.Left (Application.Top) + 2,
  648. // Y = Pos.Top (Application.Top) + 2
  649. // };
  650. // var f = new FrameView ();
  651. // var v1 = new View () {
  652. // X = Pos.Left (w) + 2,
  653. // Y = Pos.Top (w) + 2
  654. // };
  655. // var v2 = new View () {
  656. // X = Pos.Left (v1) + 2,
  657. // Y = Pos.Top (v1) + 2
  658. // };
  659. // f.Add (v1, v2);
  660. // w.Add (f);
  661. // Application.Top.Add (w);
  662. // f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  663. // f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  664. // Application.Top.LayoutComplete += (s, e) => {
  665. // Assert.Equal (0, Application.Top.Frame.X);
  666. // Assert.Equal (0, Application.Top.Frame.Y);
  667. // Assert.Equal (2, w.Frame.X);
  668. // Assert.Equal (2, w.Frame.Y);
  669. // Assert.Equal (2, f.Frame.X);
  670. // Assert.Equal (2, f.Frame.Y);
  671. // Assert.Equal (4, v1.Frame.X);
  672. // Assert.Equal (4, v1.Frame.Y);
  673. // Assert.Equal (6, v2.Frame.X);
  674. // Assert.Equal (6, v2.Frame.Y);
  675. // };
  676. // Application.Iteration += (s, a) => Application.RequestStop ();
  677. // Application.Run ();
  678. // Application.Shutdown ();
  679. //}
  680. [Fact, TestRespondersDisposed]
  681. public void PosCombine_Will_Throws ()
  682. {
  683. Application.Init (new FakeDriver ());
  684. var t = Application.Top;
  685. var w = new Window () {
  686. X = Pos.Left (t) + 2,
  687. Y = Pos.Top (t) + 2
  688. };
  689. var f = new FrameView ();
  690. var v1 = new View () {
  691. X = Pos.Left (w) + 2,
  692. Y = Pos.Top (w) + 2
  693. };
  694. var v2 = new View () {
  695. X = Pos.Left (v1) + 2,
  696. Y = Pos.Top (v1) + 2
  697. };
  698. f.Add (v1); // v2 not added
  699. w.Add (f);
  700. t.Add (w);
  701. f.X = Pos.X (v2) - Pos.X (v1);
  702. f.Y = Pos.Y (v2) - Pos.Y (v1);
  703. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  704. Application.Shutdown ();
  705. v2.Dispose ();
  706. }
  707. [Fact, TestRespondersDisposed]
  708. public void Pos_Add_Operator ()
  709. {
  710. Application.Init (new FakeDriver ());
  711. var top = Application.Top;
  712. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  713. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  714. var count = 0;
  715. field.KeyDown += (s, k) => {
  716. if (k.KeyEvent.Key == Key.Enter) {
  717. field.Text = $"Label {count}";
  718. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  719. view.Add (label);
  720. Assert.Equal ($"Label {count}", label.Text);
  721. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  722. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  723. field.Y += 1;
  724. count++;
  725. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  726. }
  727. };
  728. Application.Iteration += (s, a) => {
  729. while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  730. Application.RequestStop ();
  731. };
  732. var win = new Window ();
  733. win.Add (view);
  734. win.Add (field);
  735. top.Add (win);
  736. Application.Run (top);
  737. Assert.Equal (20, count);
  738. // Shutdown must be called to safely clean up Application if Init has been called
  739. Application.Shutdown ();
  740. }
  741. [Fact, TestRespondersDisposed]
  742. public void Pos_Subtract_Operator ()
  743. {
  744. Application.Init (new FakeDriver ());
  745. var top = Application.Top;
  746. var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
  747. var field = new TextField () { X = 0, Y = 0, Width = 20 };
  748. var count = 20;
  749. var listLabels = new List<Label> ();
  750. for (int i = 0; i < count; i++) {
  751. field.Text = $"Label {i}";
  752. var label = new Label (field.Text) { X = 0, Y = field.Y, Width = 20 };
  753. view.Add (label);
  754. Assert.Equal ($"Label {i}", label.Text);
  755. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  756. listLabels.Add (label);
  757. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  758. field.Y += 1;
  759. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  760. }
  761. field.KeyDown += (s, k) => {
  762. if (k.KeyEvent.Key == Key.Enter) {
  763. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  764. view.Remove (listLabels [count - 1]);
  765. listLabels [count - 1].Dispose ();
  766. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  767. field.Y -= 1;
  768. count--;
  769. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  770. }
  771. };
  772. Application.Iteration += (s, a) => {
  773. while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  774. Application.RequestStop ();
  775. };
  776. var win = new Window ();
  777. win.Add (view);
  778. win.Add (field);
  779. top.Add (win);
  780. Application.Run (top);
  781. Assert.Equal (0, count);
  782. // Shutdown must be called to safely clean up Application if Init has been called
  783. Application.Shutdown ();
  784. }
  785. [Fact, TestRespondersDisposed]
  786. public void Internal_Tests ()
  787. {
  788. var posFactor = new Pos.PosFactor (0.10F);
  789. Assert.Equal (10, posFactor.Anchor (100));
  790. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  791. Assert.Equal (99, posAnchorEnd.Anchor (100));
  792. var posCenter = new Pos.PosCenter ();
  793. Assert.Equal (50, posCenter.Anchor (100));
  794. var posAbsolute = new Pos.PosAbsolute (10);
  795. Assert.Equal (10, posAbsolute.Anchor (0));
  796. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  797. Assert.Equal (posCombine.left, posFactor);
  798. Assert.Equal (posCombine.right, posAbsolute);
  799. Assert.Equal (20, posCombine.Anchor (100));
  800. posCombine = new Pos.PosCombine (true, posAbsolute, posFactor);
  801. Assert.Equal (posCombine.left, posAbsolute);
  802. Assert.Equal (posCombine.right, posFactor);
  803. Assert.Equal (20, posCombine.Anchor (100));
  804. var view = new View (new Rect (20, 10, 20, 1));
  805. var posViewX = new Pos.PosView (view, 0);
  806. Assert.Equal (20, posViewX.Anchor (0));
  807. var posViewY = new Pos.PosView (view, 1);
  808. Assert.Equal (10, posViewY.Anchor (0));
  809. var posRight = new Pos.PosView (view, 2);
  810. Assert.Equal (40, posRight.Anchor (0));
  811. var posViewBottom = new Pos.PosView (view, 3);
  812. Assert.Equal (11, posViewBottom.Anchor (0));
  813. view.Dispose ();
  814. }
  815. [Fact]
  816. public void Function_SetsValue ()
  817. {
  818. var text = "Test";
  819. var pos = Pos.Function (() => text.Length);
  820. Assert.Equal ("PosFunc(4)", pos.ToString ());
  821. text = "New Test";
  822. Assert.Equal ("PosFunc(8)", pos.ToString ());
  823. text = "";
  824. Assert.Equal ("PosFunc(0)", pos.ToString ());
  825. }
  826. [Fact]
  827. public void Function_Equal ()
  828. {
  829. var f1 = () => 0;
  830. var f2 = () => 0;
  831. var pos1 = Pos.Function (f1);
  832. var pos2 = Pos.Function (f2);
  833. Assert.Equal (pos1, pos2);
  834. f2 = () => 1;
  835. pos2 = Pos.Function (f2);
  836. Assert.NotEqual (pos1, pos2);
  837. }
  838. [Theory, AutoInitShutdown]
  839. [InlineData (true)]
  840. [InlineData (false)]
  841. public void PosPercentPlusOne (bool testHorizontal)
  842. {
  843. var container = new View {
  844. Width = 100,
  845. Height = 100,
  846. };
  847. var label = new Label {
  848. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  849. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  850. Width = 10,
  851. Height = 10,
  852. };
  853. container.Add (label);
  854. Application.Top.Add (container);
  855. Application.Top.LayoutSubviews ();
  856. Assert.Equal (100, container.Frame.Width);
  857. Assert.Equal (100, container.Frame.Height);
  858. if (testHorizontal) {
  859. Assert.Equal (61, label.Frame.X);
  860. Assert.Equal (1, label.Frame.Y);
  861. } else {
  862. Assert.Equal (1, label.Frame.X);
  863. Assert.Equal (61, label.Frame.Y);
  864. }
  865. }
  866. [Fact]
  867. public void PosCombine_Referencing_Same_View ()
  868. {
  869. var super = new View ("super") {
  870. Width = 10,
  871. Height = 10
  872. };
  873. var view1 = new View ("view1") {
  874. Width = 2,
  875. Height = 2,
  876. };
  877. var view2 = new View ("view2") {
  878. Width = 2,
  879. Height = 2,
  880. };
  881. view2.X = Pos.AnchorEnd () - (Pos.Right (view2) - Pos.Left (view2));
  882. super.Add (view1, view2);
  883. super.BeginInit ();
  884. super.EndInit ();
  885. var exception = Record.Exception (super.LayoutSubviews);
  886. Assert.Null (exception);
  887. Assert.Equal (new Rect (0, 0, 10, 10), super.Frame);
  888. Assert.Equal (new Rect (0, 0, 2, 2), view1.Frame);
  889. Assert.Equal (new Rect (8, 0, 2, 2), view2.Frame);
  890. super.Dispose ();
  891. }
  892. }
  893. }