ToScreenTests.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. using Xunit.Abstractions;
  2. using static System.Net.Mime.MediaTypeNames;
  3. namespace Terminal.Gui.LayoutTests;
  4. /// <summary>
  5. /// Test the <see cref="View.FrameToScreen"/> and <see cref="View.ViewportToScreen"/> methods.
  6. /// DOES NOT TEST Adornment.xxxToScreen methods. Those are in ./Adornment/ToScreenTests.cs
  7. /// </summary>
  8. /// <param name="output"></param>
  9. public class ToScreenTests (ITestOutputHelper output)
  10. {
  11. private readonly ITestOutputHelper _output = output;
  12. // Test FrameToScreen
  13. [Theory]
  14. [InlineData (0, 0, 0, 0)]
  15. [InlineData (1, 0, 1, 0)]
  16. [InlineData (0, 1, 0, 1)]
  17. [InlineData (1, 1, 1, 1)]
  18. [InlineData (10, 10, 10, 10)]
  19. public void FrameToScreen_NoSuperView (int frameX, int frameY, int expectedScreenX, int expectedScreenY)
  20. {
  21. var view = new View { X = frameX, Y = frameY, Width = 10, Height = 10 };
  22. view.Layout ();
  23. var expected = new Rectangle (expectedScreenX, expectedScreenY, 10, 10);
  24. Rectangle actual = view.FrameToScreen ();
  25. Assert.Equal (expected, actual);
  26. }
  27. [Theory]
  28. [InlineData (0, 0, 0, 0, 0)]
  29. [InlineData (1, 0, 0, 1, 1)]
  30. [InlineData (2, 0, 0, 2, 2)]
  31. [InlineData (1, 1, 0, 2, 1)]
  32. [InlineData (1, 0, 1, 1, 2)]
  33. [InlineData (1, 1, 1, 2, 2)]
  34. [InlineData (1, 10, 10, 11, 11)]
  35. public void FrameToScreen_SuperView (
  36. int superOffset,
  37. int frameX,
  38. int frameY,
  39. int expectedScreenX,
  40. int expectedScreenY
  41. )
  42. {
  43. var super = new View { X = superOffset, Y = superOffset, Width = 20, Height = 20 };
  44. var view = new View { X = frameX, Y = frameY, Width = 10, Height = 10 };
  45. super.Add (view);
  46. super.Layout ();
  47. var expected = new Rectangle (expectedScreenX, expectedScreenY, 10, 10);
  48. Rectangle actual = view.FrameToScreen ();
  49. Assert.Equal (expected, actual);
  50. }
  51. [Theory]
  52. [InlineData (0, 0)]
  53. [InlineData (1, 1)]
  54. [InlineData (-1, -1)]
  55. [InlineData (11, 11)]
  56. public void FrameToScreen_NoSuperView_WithoutAdornments (int x, int expectedX)
  57. {
  58. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  59. // Arrange
  60. var frame = new Rectangle (x, 0, 10, 10);
  61. var view = new View ();
  62. view.Frame = frame;
  63. // Act
  64. var screen = view.FrameToScreen ();
  65. // Assert
  66. Assert.Equal (expectedX, screen.X);
  67. }
  68. [Theory]
  69. [InlineData (0, 0)]
  70. [InlineData (1, 1)]
  71. [InlineData (-1, -1)]
  72. [InlineData (11, 11)]
  73. public void FrameToScreen_NoSuperView_WithAdornments (int x, int expectedX)
  74. {
  75. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  76. // Arrange
  77. var frame = new Rectangle (x, 0, 10, 10);
  78. var view = new View ();
  79. view.BorderStyle = LineStyle.Single;
  80. view.Frame = frame;
  81. // Act
  82. var screen = view.FrameToScreen ();
  83. // Assert
  84. Assert.Equal (expectedX, screen.X);
  85. }
  86. [Theory]
  87. [InlineData (0, 1)]
  88. [InlineData (1, 2)]
  89. [InlineData (-1, 0)]
  90. [InlineData (11, 12)]
  91. public void FrameToScreen_NoSuperView_WithAdornment_WithSubview (int x, int expectedX)
  92. {
  93. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  94. // Arrange
  95. var frame = new Rectangle (x, 0, 10, 10);
  96. var view = new View ();
  97. view.BorderStyle = LineStyle.Single;
  98. view.Frame = frame;
  99. var subviewOfBorder = new View ()
  100. {
  101. X = 1, // screen should be 1
  102. Y = 0,
  103. Width = 1,
  104. Height = 1
  105. };
  106. view.Border.Add (subviewOfBorder);
  107. view.BeginInit ();
  108. view.EndInit ();
  109. // Act
  110. var screen = subviewOfBorder.FrameToScreen ();
  111. // Assert
  112. Assert.Equal (expectedX, screen.X);
  113. }
  114. [Theory]
  115. [InlineData (0, 3)]
  116. [InlineData (1, 4)]
  117. [InlineData (-1, 2)]
  118. [InlineData (11, 14)]
  119. public void FrameToScreen_Adornment_WithSubview_WithSubview (int topX, int expectedX)
  120. {
  121. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  122. // Arrange
  123. var adornmentFrame = new Rectangle (topX, 0, 10, 10);
  124. var adornment = new Adornment ();
  125. adornment.Frame = adornmentFrame;
  126. adornment.Thickness = new (1);
  127. var subviewOfAdornment = new View ()
  128. {
  129. Id = "subviewOfAdornment",
  130. X = 1, // screen should be 1
  131. Y = 0,
  132. Width = 1,
  133. Height = 1,
  134. };
  135. var subviewOfSubview = new View ()
  136. {
  137. Id = "subviewOfSubview",
  138. X = 2, // screen should be 3 (the subviewOfAdornment location is 1)
  139. Y = 0,
  140. Width = 1,
  141. Height = 1
  142. };
  143. subviewOfAdornment.Add (subviewOfSubview);
  144. adornment.Add (subviewOfAdornment);
  145. adornment.BeginInit ();
  146. adornment.EndInit ();
  147. // Act
  148. var screen = subviewOfSubview.FrameToScreen ();
  149. // Assert
  150. Assert.Equal (expectedX, screen.X);
  151. }
  152. [Theory]
  153. [InlineData (0, 0)]
  154. [InlineData (1, 1)]
  155. [InlineData (-1, -1)]
  156. [InlineData (11, 11)]
  157. public void FrameToScreen_SuperView_WithoutAdornments (int x, int expectedX)
  158. {
  159. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  160. // Arrange
  161. var frame = new Rectangle (x, 0, 10, 10);
  162. var superView = new View ()
  163. {
  164. X = 0,
  165. Y = 0,
  166. Height = Dim.Fill (),
  167. Width = Dim.Fill ()
  168. };
  169. var view = new View ();
  170. view.Frame = frame;
  171. superView.Add (view);
  172. superView.LayoutSubviews ();
  173. // Act
  174. var screen = view.FrameToScreen ();
  175. // Assert
  176. Assert.Equal (expectedX, screen.X);
  177. }
  178. [Theory]
  179. [InlineData (0, 1)]
  180. [InlineData (1, 2)]
  181. [InlineData (-1, 0)]
  182. [InlineData (11, 12)]
  183. public void FrameToScreen_SuperView_WithAdornments (int x, int expectedX)
  184. {
  185. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  186. // Arrange
  187. var frame = new Rectangle (x, 0, 10, 10);
  188. var superView = new View ()
  189. {
  190. X = 0,
  191. Y = 0,
  192. Height = Dim.Fill (),
  193. Width = Dim.Fill ()
  194. };
  195. superView.BorderStyle = LineStyle.Single;
  196. var view = new View ();
  197. view.Frame = frame;
  198. superView.Add (view);
  199. superView.LayoutSubviews ();
  200. // Act
  201. var screen = view.FrameToScreen ();
  202. // Assert
  203. Assert.Equal (expectedX, screen.X);
  204. }
  205. [Theory]
  206. [InlineData (0, 0)]
  207. [InlineData (1, 1)]
  208. [InlineData (-1, -1)]
  209. [InlineData (11, 11)]
  210. public void FrameToScreen_NestedSuperView_WithoutAdornments (int x, int expectedX)
  211. {
  212. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  213. // Arrange
  214. var frame = new Rectangle (x, 0, 10, 10);
  215. var superSuperView = new View ()
  216. {
  217. X = 0,
  218. Y = 0,
  219. Height = Dim.Fill (),
  220. Width = Dim.Fill ()
  221. };
  222. var superView = new View ()
  223. {
  224. X = 0,
  225. Y = 0,
  226. Height = Dim.Fill (),
  227. Width = Dim.Fill ()
  228. };
  229. superSuperView.Add (superView);
  230. var view = new View ();
  231. view.Frame = frame;
  232. superView.Add (view);
  233. superView.LayoutSubviews ();
  234. // Act
  235. var screen = view.FrameToScreen ();
  236. // Assert
  237. Assert.Equal (expectedX, screen.X);
  238. }
  239. [Theory]
  240. [InlineData (0, 2)]
  241. [InlineData (1, 3)]
  242. [InlineData (-1, 1)]
  243. [InlineData (11, 13)]
  244. public void FrameToScreen_NestedSuperView_WithAdornments (int x, int expectedX)
  245. {
  246. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  247. // Arrange
  248. var frame = new Rectangle (x, 0, 10, 10);
  249. var superSuperView = new View ()
  250. {
  251. X = 0,
  252. Y = 0,
  253. Height = Dim.Fill (),
  254. Width = Dim.Fill ()
  255. };
  256. superSuperView.BorderStyle = LineStyle.Single;
  257. var superView = new View ()
  258. {
  259. X = 0,
  260. Y = 0,
  261. Height = Dim.Fill (),
  262. Width = Dim.Fill ()
  263. };
  264. superSuperView.Add (superView);
  265. superView.BorderStyle = LineStyle.Single;
  266. var view = new View ();
  267. view.Frame = frame;
  268. superView.Add (view);
  269. superSuperView.Layout ();
  270. // Act
  271. var screen = view.FrameToScreen ();
  272. // Assert
  273. Assert.Equal (expectedX, screen.X);
  274. }
  275. // ContentToScreen tests ----------------------
  276. [Fact]
  277. public void ContentToScreen_With_Positive_Content_Location ()
  278. {
  279. View view = new ()
  280. {
  281. X = 1,
  282. Y = 1,
  283. Width = 10,
  284. Height = 10
  285. };
  286. view.Layout ();
  287. view.SetContentSize (new (20, 20));
  288. Point testPoint = new (0, 0);
  289. Assert.Equal (new Point (1, 1), view.ContentToScreen (testPoint));
  290. }
  291. [Theory]
  292. [InlineData (0, 0, 1)]
  293. [InlineData (1, 0, 2)]
  294. [InlineData (-1, 0, 0)]
  295. [InlineData (0, 1, 2)]
  296. [InlineData (1, 1, 3)]
  297. [InlineData (-1, 1, 1)]
  298. [InlineData (0, -1, 0)]
  299. [InlineData (1, -1, 1)]
  300. [InlineData (-1, -1, -1)]
  301. public void ContentToScreen_NoSuperView_WithAdornments (int frameX, int contentX, int expectedX)
  302. {
  303. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  304. // Arrange
  305. var frame = new Rectangle (frameX, 0, 10, 10);
  306. var view = new View ();
  307. view.Frame = frame;
  308. view.SetContentSize (new (20, 20));
  309. view.BorderStyle = LineStyle.Single;
  310. // Act
  311. var screen = view.ContentToScreen (new (contentX, 0));
  312. // Assert
  313. Assert.Equal (expectedX, screen.X);
  314. }
  315. [Theory]
  316. [InlineData (0, 0, 0)]
  317. [InlineData (1, 0, 1)]
  318. [InlineData (-1, 0, -1)]
  319. [InlineData (11, 0, 11)]
  320. [InlineData (0, 1, 1)]
  321. [InlineData (1, 1, 2)]
  322. [InlineData (-1, 1, 0)]
  323. [InlineData (11, 1, 12)]
  324. [InlineData (0, -1, -1)]
  325. [InlineData (1, -1, 0)]
  326. [InlineData (-1, -1, -2)]
  327. [InlineData (11, -1, 10)]
  328. public void ContentToScreen_SuperView_WithoutAdornments (int frameX, int contentX, int expectedX)
  329. {
  330. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  331. // Arrange
  332. var frame = new Rectangle (frameX, 0, 10, 10);
  333. var superView = new View ()
  334. {
  335. X = 0,
  336. Y = 0,
  337. Height = Dim.Fill (),
  338. Width = Dim.Fill ()
  339. };
  340. var view = new View ();
  341. view.Frame = frame;
  342. view.SetContentSize (new (20, 20));
  343. superView.Add (view);
  344. superView.LayoutSubviews ();
  345. // Act
  346. var screen = view.ContentToScreen (new (contentX, 0));
  347. // Assert
  348. Assert.Equal (expectedX, screen.X);
  349. }
  350. //[Theory]
  351. //[InlineData (0, 0, 1)]
  352. //[InlineData (1, 0, 2)]
  353. //[InlineData (-1, 0, 0)]
  354. //[InlineData (11, 0, 12)]
  355. //[InlineData (0, 1, 2)]
  356. //[InlineData (1, 1, 3)]
  357. //[InlineData (-1, 1, 1)]
  358. //[InlineData (11, 1, 13)]
  359. //[InlineData (0, -1, 0)]
  360. //[InlineData (1, -1, 1)]
  361. //[InlineData (-1, -1, -1)]
  362. //[InlineData (11, -1, 11)]
  363. //public void ContentToScreen_SuperView_WithAdornments (int frameX, int ContentX, int expectedX)
  364. //{
  365. // // We test with only X because Y is equivalent. Height/Width are irrelevant.
  366. // // Arrange
  367. // var frame = new Rectangle (frameX, 0, 10, 10);
  368. // var superView = new View ()
  369. // {
  370. // X = 0,
  371. // Y = 0,
  372. // Height = Dim.Fill (),
  373. // Width = Dim.Fill ()
  374. // };
  375. // superView.BorderStyle = LineStyle.Single;
  376. // var view = new View ();
  377. // view.Frame = frame;
  378. // superView.Add (view);
  379. // superView.LayoutSubviews ();
  380. // // Act
  381. // var screen = view.ContentToScreen (new (ContentX, 0, 0, 0));
  382. // // Assert
  383. // Assert.Equal (expectedX, screen.X);
  384. //}
  385. //[Theory]
  386. //[InlineData (0, 0, 0)]
  387. //[InlineData (1, 0, 1)]
  388. //[InlineData (-1, 0, -1)]
  389. //[InlineData (11, 0, 11)]
  390. //[InlineData (0, 1, 1)]
  391. //[InlineData (1, 1, 2)]
  392. //[InlineData (-1, 1, 0)]
  393. //[InlineData (11, 1, 12)]
  394. //[InlineData (0, -1, -1)]
  395. //[InlineData (1, -1, 0)]
  396. //[InlineData (-1, -1, -2)]
  397. //[InlineData (11, -1, 10)]
  398. //public void ContentToScreen_NestedSuperView_WithoutAdornments (int frameX, int ContentX, int expectedX)
  399. //{
  400. // // We test with only X because Y is equivalent. Height/Width are irrelevant.
  401. // // Arrange
  402. // var frame = new Rectangle (frameX, 0, 10, 10);
  403. // var superSuperView = new View ()
  404. // {
  405. // X = 0,
  406. // Y = 0,
  407. // Height = Dim.Fill (),
  408. // Width = Dim.Fill ()
  409. // };
  410. // var superView = new View ()
  411. // {
  412. // X = 0,
  413. // Y = 0,
  414. // Height = Dim.Fill (),
  415. // Width = Dim.Fill ()
  416. // };
  417. // superSuperView.Add (superView);
  418. // var view = new View ();
  419. // view.Frame = frame;
  420. // superView.Add (view);
  421. // superView.LayoutSubviews ();
  422. // // Act
  423. // var screen = view.ContentToScreen (new (ContentX, 0, 0, 0));
  424. // // Assert
  425. // Assert.Equal (expectedX, screen.X);
  426. //}
  427. //[Theory]
  428. //[InlineData (0, 0, 2)]
  429. //[InlineData (1, 0, 3)]
  430. //[InlineData (-1, 0, 1)]
  431. //[InlineData (11, 0, 13)]
  432. //[InlineData (0, 1, 3)]
  433. //[InlineData (1, 1, 4)]
  434. //[InlineData (-1, 1, 2)]
  435. //[InlineData (11, 1, 14)]
  436. //[InlineData (0, -1, 1)]
  437. //[InlineData (1, -1, 2)]
  438. //[InlineData (-1, -1, 0)]
  439. //[InlineData (11, -1, 12)]
  440. //public void ContentToScreen_NestedSuperView_WithAdornments (int frameX, int ContentX, int expectedX)
  441. //{
  442. // // We test with only X because Y is equivalent. Height/Width are irrelevant.
  443. // // Arrange
  444. // var frame = new Rectangle (frameX, 0, 10, 10);
  445. // var superSuperView = new View ()
  446. // {
  447. // X = 0,
  448. // Y = 0,
  449. // Height = Dim.Fill (),
  450. // Width = Dim.Fill ()
  451. // };
  452. // superSuperView.BorderStyle = LineStyle.Single;
  453. // var superView = new View ()
  454. // {
  455. // X = 0,
  456. // Y = 0,
  457. // Height = Dim.Fill (),
  458. // Width = Dim.Fill ()
  459. // };
  460. // superSuperView.Add (superView);
  461. // superView.BorderStyle = LineStyle.Single;
  462. // var view = new View ();
  463. // view.Frame = frame;
  464. // superView.Add (view);
  465. // superView.LayoutSubviews ();
  466. // // Act
  467. // var screen = view.ContentToScreen (new (ContentX, 0, 0, 0));
  468. // // Assert
  469. // Assert.Equal (expectedX, screen.X);
  470. //}
  471. //[Theory]
  472. //[InlineData (0, 0, 3)]
  473. //[InlineData (1, 0, 4)]
  474. //[InlineData (-1, 0, 2)]
  475. //[InlineData (11, 0, 14)]
  476. //[InlineData (0, 1, 4)]
  477. //[InlineData (1, 1, 5)]
  478. //[InlineData (-1, 1, 3)]
  479. //[InlineData (11, 1, 15)]
  480. //[InlineData (0, -1, 2)]
  481. //[InlineData (1, -1, 3)]
  482. //[InlineData (-1, -1, 1)]
  483. //[InlineData (11, -1, 13)]
  484. //public void ContentToScreen_Positive_NestedSuperView_WithAdornments (int frameX, int testX, int expectedX)
  485. //{
  486. // // We test with only X because Y is equivalent. Height/Width are irrelevant.
  487. // // Arrange
  488. // var frame = new Rectangle (frameX, 0, 10, 10);
  489. // var superSuperView = new View ()
  490. // {
  491. // X = 0,
  492. // Y = 0,
  493. // Height = Dim.Fill (),
  494. // Width = Dim.Fill ()
  495. // };
  496. // superSuperView.BorderStyle = LineStyle.Single;
  497. // var superView = new View ()
  498. // {
  499. // X = 0,
  500. // Y = 0,
  501. // Height = Dim.Fill (),
  502. // Width = Dim.Fill ()
  503. // };
  504. // superSuperView.Add (superView);
  505. // superView.BorderStyle = LineStyle.Single;
  506. // var view = new View ();
  507. // view.Frame = frame;
  508. // view.SetContentSize (new (11, 11));
  509. // view.Content = view.Content with { Location = new (1, 1) };
  510. // superView.Add (view);
  511. // superView.LayoutSubviews ();
  512. // // Act
  513. // var screen = view.ContentToScreen (new (testX, 0, 0, 0));
  514. // // Assert
  515. // Assert.Equal (expectedX, screen.X);
  516. //}
  517. // ViewportToScreen tests ----------------------
  518. [Fact]
  519. public void ViewportToScreen_With_Positive_Viewport_Location ()
  520. {
  521. View view = new ()
  522. {
  523. Width = 10,
  524. Height = 10,
  525. ViewportSettings = ViewportSettings.AllowNegativeLocation
  526. };
  527. view.Layout ();
  528. Rectangle testRect = new Rectangle (0, 0, 1, 1);
  529. Assert.Equal (new Point (0, 0), view.ViewportToScreen (testRect).Location);
  530. view.Viewport = view.Viewport with { Location = new Point (1, 1) };
  531. Assert.Equal (new Rectangle (1, 1, 10, 10), view.Viewport);
  532. Assert.Equal (new Point (0, 0), view.ViewportToScreen (testRect).Location);
  533. }
  534. [Theory]
  535. [InlineData (0, 0, 0)]
  536. [InlineData (1, 0, 1)]
  537. [InlineData (-1, 0, -1)]
  538. [InlineData (11, 0, 11)]
  539. public void ViewportToScreen_NoSuperView_WithoutAdornments (int frameX, int viewportX, int expectedX)
  540. {
  541. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  542. // Arrange
  543. var frame = new Rectangle (frameX, 0, 10, 10);
  544. var view = new View ();
  545. view.Frame = frame;
  546. view.Layout ();
  547. // Act
  548. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  549. // Assert
  550. Assert.Equal (expectedX, screen.X);
  551. }
  552. [Theory]
  553. [InlineData (0, 0, 1)]
  554. [InlineData (1, 0, 2)]
  555. [InlineData (-1, 0, 0)]
  556. [InlineData (11, 0, 12)]
  557. [InlineData (0, 1, 2)]
  558. [InlineData (1, 1, 3)]
  559. [InlineData (-1, 1, 1)]
  560. [InlineData (11, 1, 13)]
  561. [InlineData (0, -1, 0)]
  562. [InlineData (1, -1, 1)]
  563. [InlineData (-1, -1, -1)]
  564. [InlineData (11, -1, 11)]
  565. public void ViewportToScreen_NoSuperView_WithAdornments (int frameX, int viewportX, int expectedX)
  566. {
  567. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  568. // Arrange
  569. var frame = new Rectangle (frameX, 0, 10, 10);
  570. var view = new View ();
  571. view.BorderStyle = LineStyle.Single;
  572. view.Frame = frame;
  573. // Act
  574. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  575. // Assert
  576. Assert.Equal (expectedX, screen.X);
  577. }
  578. [Theory]
  579. [InlineData (0, 0, 0)]
  580. [InlineData (1, 0, 1)]
  581. [InlineData (-1, 0, -1)]
  582. [InlineData (11, 0, 11)]
  583. [InlineData (0, 1, 1)]
  584. [InlineData (1, 1, 2)]
  585. [InlineData (-1, 1, 0)]
  586. [InlineData (11, 1, 12)]
  587. [InlineData (0, -1, -1)]
  588. [InlineData (1, -1, 0)]
  589. [InlineData (-1, -1, -2)]
  590. [InlineData (11, -1, 10)]
  591. public void ViewportToScreen_SuperView_WithoutAdornments (int frameX, int viewportX, int expectedX)
  592. {
  593. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  594. // Arrange
  595. var frame = new Rectangle (frameX, 0, 10, 10);
  596. var superView = new View ()
  597. {
  598. X = 0,
  599. Y = 0,
  600. Height = Dim.Fill (),
  601. Width = Dim.Fill ()
  602. };
  603. var view = new View ();
  604. view.Frame = frame;
  605. superView.Add (view);
  606. superView.LayoutSubviews ();
  607. // Act
  608. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  609. // Assert
  610. Assert.Equal (expectedX, screen.X);
  611. }
  612. [Theory]
  613. [InlineData (0, 0, 1)]
  614. [InlineData (1, 0, 2)]
  615. [InlineData (-1, 0, 0)]
  616. [InlineData (11, 0, 12)]
  617. [InlineData (0, 1, 2)]
  618. [InlineData (1, 1, 3)]
  619. [InlineData (-1, 1, 1)]
  620. [InlineData (11, 1, 13)]
  621. [InlineData (0, -1, 0)]
  622. [InlineData (1, -1, 1)]
  623. [InlineData (-1, -1, -1)]
  624. [InlineData (11, -1, 11)]
  625. public void ViewportToScreen_SuperView_WithAdornments (int frameX, int viewportX, int expectedX)
  626. {
  627. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  628. // Arrange
  629. var frame = new Rectangle (frameX, 0, 10, 10);
  630. var superView = new View ()
  631. {
  632. X = 0,
  633. Y = 0,
  634. Height = Dim.Fill (),
  635. Width = Dim.Fill ()
  636. };
  637. superView.BorderStyle = LineStyle.Single;
  638. var view = new View ();
  639. view.Frame = frame;
  640. superView.Add (view);
  641. superView.LayoutSubviews ();
  642. // Act
  643. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  644. // Assert
  645. Assert.Equal (expectedX, screen.X);
  646. }
  647. [Theory]
  648. [InlineData (0, 0, 0)]
  649. [InlineData (1, 0, 1)]
  650. [InlineData (-1, 0, -1)]
  651. [InlineData (11, 0, 11)]
  652. [InlineData (0, 1, 1)]
  653. [InlineData (1, 1, 2)]
  654. [InlineData (-1, 1, 0)]
  655. [InlineData (11, 1, 12)]
  656. [InlineData (0, -1, -1)]
  657. [InlineData (1, -1, 0)]
  658. [InlineData (-1, -1, -2)]
  659. [InlineData (11, -1, 10)]
  660. public void ViewportToScreen_NestedSuperView_WithoutAdornments (int frameX, int viewportX, int expectedX)
  661. {
  662. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  663. // Arrange
  664. var frame = new Rectangle (frameX, 0, 10, 10);
  665. var superSuperView = new View ()
  666. {
  667. X = 0,
  668. Y = 0,
  669. Height = Dim.Fill (),
  670. Width = Dim.Fill ()
  671. };
  672. var superView = new View ()
  673. {
  674. X = 0,
  675. Y = 0,
  676. Height = Dim.Fill (),
  677. Width = Dim.Fill ()
  678. };
  679. superSuperView.Add (superView);
  680. var view = new View ();
  681. view.Frame = frame;
  682. superView.Add (view);
  683. superView.LayoutSubviews ();
  684. // Act
  685. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  686. // Assert
  687. Assert.Equal (expectedX, screen.X);
  688. }
  689. [Theory]
  690. [InlineData (0, 0, 2)]
  691. [InlineData (1, 0, 3)]
  692. [InlineData (-1, 0, 1)]
  693. [InlineData (11, 0, 13)]
  694. [InlineData (0, 1, 3)]
  695. [InlineData (1, 1, 4)]
  696. [InlineData (-1, 1, 2)]
  697. [InlineData (11, 1, 14)]
  698. [InlineData (0, -1, 1)]
  699. [InlineData (1, -1, 2)]
  700. [InlineData (-1, -1, 0)]
  701. [InlineData (11, -1, 12)]
  702. public void ViewportToScreen_NestedSuperView_WithAdornments (int frameX, int viewportX, int expectedX)
  703. {
  704. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  705. // Arrange
  706. var frame = new Rectangle (frameX, 0, 10, 10);
  707. var superSuperView = new View ()
  708. {
  709. X = 0,
  710. Y = 0,
  711. Height = Dim.Fill (),
  712. Width = Dim.Fill ()
  713. };
  714. superSuperView.BorderStyle = LineStyle.Single;
  715. var superView = new View ()
  716. {
  717. X = 0,
  718. Y = 0,
  719. Height = Dim.Fill (),
  720. Width = Dim.Fill ()
  721. };
  722. superSuperView.Add (superView);
  723. superView.BorderStyle = LineStyle.Single;
  724. var view = new View ();
  725. view.Frame = frame;
  726. superView.Add (view);
  727. superView.Layout ();
  728. // Act
  729. var screen = view.ViewportToScreen (new Point (viewportX, 0));
  730. // Assert
  731. Assert.Equal (expectedX, screen.X);
  732. }
  733. [Theory]
  734. [InlineData (0, 0, 2)]
  735. [InlineData (1, 0, 3)]
  736. [InlineData (-1, 0, 1)]
  737. [InlineData (11, 0, 13)]
  738. [InlineData (0, 1, 3)]
  739. [InlineData (1, 1, 4)]
  740. [InlineData (-1, 1, 2)]
  741. [InlineData (11, 1, 14)]
  742. [InlineData (0, -1, 1)]
  743. [InlineData (1, -1, 2)]
  744. [InlineData (-1, -1, 0)]
  745. [InlineData (11, -1, 12)]
  746. public void ViewportToScreen_Positive_NestedSuperView_WithAdornments (int frameX, int testX, int expectedX)
  747. {
  748. // We test with only X because Y is equivalent. Height/Width are irrelevant.
  749. // Arrange
  750. var frame = new Rectangle (frameX, 0, 10, 10);
  751. var superSuperView = new View ()
  752. {
  753. X = 0,
  754. Y = 0,
  755. Height = Dim.Fill (),
  756. Width = Dim.Fill ()
  757. };
  758. superSuperView.BorderStyle = LineStyle.Single;
  759. var superView = new View ()
  760. {
  761. X = 0,
  762. Y = 0,
  763. Height = Dim.Fill (),
  764. Width = Dim.Fill ()
  765. };
  766. superSuperView.Add (superView);
  767. superView.BorderStyle = LineStyle.Single;
  768. var view = new View ();
  769. view.Frame = frame;
  770. view.SetContentSize (new (11, 11));
  771. view.Viewport = view.Viewport with { Location = new (1, 1) };
  772. superView.Add (view);
  773. superView.LayoutSubviews ();
  774. // Act
  775. var screen = view.ViewportToScreen (new Point (testX, 0));
  776. // Assert
  777. Assert.Equal (expectedX, screen.X);
  778. }
  779. [Fact]
  780. [AutoInitShutdown]
  781. public void ScreenToView_ViewToScreen_GetViewsUnderMouse_Full_Top ()
  782. {
  783. Application.Top = new ();
  784. Application.Top.BorderStyle = LineStyle.Single;
  785. var view = new View
  786. {
  787. X = 3,
  788. Y = 2,
  789. Width = 10,
  790. Height = 1,
  791. Text = "0123456789"
  792. };
  793. Application.Top.Add (view);
  794. var rs = Application.Begin (Application.Top);
  795. Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  796. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  797. Assert.Equal (new (0, 0, 80, 25), Application.Top.Frame);
  798. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  799. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  800. Assert.Equal (new (0, 0, 20, 10), Application.Top.Frame);
  801. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  802. @"
  803. ┌──────────────────┐
  804. │ │
  805. │ │
  806. │ 0123456789 │
  807. │ │
  808. │ │
  809. │ │
  810. │ │
  811. │ │
  812. └──────────────────┘"
  813. ,
  814. _output
  815. );
  816. // top
  817. Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (0, 0)));
  818. Point screen = Application.Top.Margin.ViewportToScreen (new Point (0, 0));
  819. Assert.Equal (0, screen.X);
  820. Assert.Equal (0, screen.Y);
  821. screen = Application.Top.Border.ViewportToScreen (new Point (0, 0));
  822. Assert.Equal (0, screen.X);
  823. Assert.Equal (0, screen.Y);
  824. screen = Application.Top.Padding.ViewportToScreen (new Point (0, 0));
  825. Assert.Equal (1, screen.X);
  826. Assert.Equal (1, screen.Y);
  827. screen = Application.Top.ViewportToScreen (new Point (0, 0));
  828. Assert.Equal (1, screen.X);
  829. Assert.Equal (1, screen.Y);
  830. screen = Application.Top.ViewportToScreen (new Point (-1, -1));
  831. Assert.Equal (0, screen.X);
  832. Assert.Equal (0, screen.Y);
  833. var found = View.GetViewsUnderMouse (new Point(0, 0)).LastOrDefault ();
  834. Assert.Equal (Application.Top.Border, found);
  835. Assert.Equal (0, found.Frame.X);
  836. Assert.Equal (0, found.Frame.Y);
  837. Assert.Equal (new (3, 2), Application.Top.ScreenToFrame (new (3, 2)));
  838. screen = Application.Top.ViewportToScreen (new Point (3, 2));
  839. Assert.Equal (4, screen.X);
  840. Assert.Equal (3, screen.Y);
  841. found = View.GetViewsUnderMouse (new Point(screen.X, screen.Y)).LastOrDefault ();
  842. Assert.Equal (view, found);
  843. //Assert.Equal (0, found.FrameToScreen ().X);
  844. //Assert.Equal (0, found.FrameToScreen ().Y);
  845. found = View.GetViewsUnderMouse (new Point(3, 2)).LastOrDefault ();
  846. Assert.Equal (Application.Top, found);
  847. //Assert.Equal (3, found.FrameToScreen ().X);
  848. //Assert.Equal (2, found.FrameToScreen ().Y);
  849. Assert.Equal (new (13, 2), Application.Top.ScreenToFrame (new (13, 2)));
  850. screen = Application.Top.ViewportToScreen (new Point (12, 2));
  851. Assert.Equal (13, screen.X);
  852. Assert.Equal (3, screen.Y);
  853. found = View.GetViewsUnderMouse (new Point(screen.X, screen.Y)).LastOrDefault ();
  854. Assert.Equal (view, found);
  855. //Assert.Equal (9, found.FrameToScreen ().X);
  856. //Assert.Equal (0, found.FrameToScreen ().Y);
  857. screen = Application.Top.ViewportToScreen (new Point (13, 2));
  858. Assert.Equal (14, screen.X);
  859. Assert.Equal (3, screen.Y);
  860. found = View.GetViewsUnderMouse (new Point(13, 2)).LastOrDefault ();
  861. Assert.Equal (Application.Top, found);
  862. //Assert.Equal (13, found.FrameToScreen ().X);
  863. //Assert.Equal (2, found.FrameToScreen ().Y);
  864. Assert.Equal (new (14, 3), Application.Top.ScreenToFrame (new (14, 3)));
  865. screen = Application.Top.ViewportToScreen (new Point (14, 3));
  866. Assert.Equal (15, screen.X);
  867. Assert.Equal (4, screen.Y);
  868. found = View.GetViewsUnderMouse (new Point(14, 3)).LastOrDefault ();
  869. Assert.Equal (Application.Top, found);
  870. //Assert.Equal (14, found.FrameToScreen ().X);
  871. //Assert.Equal (3, found.FrameToScreen ().Y);
  872. // view
  873. Assert.Equal (new (-4, -3), view.ScreenToFrame (new (0, 0)));
  874. screen = view.Margin.ViewportToScreen (new Point (-3, -2));
  875. Assert.Equal (1, screen.X);
  876. Assert.Equal (1, screen.Y);
  877. screen = view.Border.ViewportToScreen (new Point (-3, -2));
  878. Assert.Equal (1, screen.X);
  879. Assert.Equal (1, screen.Y);
  880. screen = view.Padding.ViewportToScreen (new Point (-3, -2));
  881. Assert.Equal (1, screen.X);
  882. Assert.Equal (1, screen.Y);
  883. screen = view.ViewportToScreen (new Point (-3, -2));
  884. Assert.Equal (1, screen.X);
  885. Assert.Equal (1, screen.Y);
  886. screen = view.ViewportToScreen (new Point (-4, -3));
  887. Assert.Equal (0, screen.X);
  888. Assert.Equal (0, screen.Y);
  889. found = View.GetViewsUnderMouse (new Point(0, 0)).LastOrDefault ();
  890. Assert.Equal (Application.Top.Border, found);
  891. Assert.Equal (new (-1, -1), view.ScreenToFrame (new (3, 2)));
  892. screen = view.ViewportToScreen (new Point (0, 0));
  893. Assert.Equal (4, screen.X);
  894. Assert.Equal (3, screen.Y);
  895. found = View.GetViewsUnderMouse (new Point(4, 3)).LastOrDefault ();
  896. Assert.Equal (view, found);
  897. Assert.Equal (new (9, -1), view.ScreenToFrame (new (13, 2)));
  898. screen = view.ViewportToScreen (new Point (10, 0));
  899. Assert.Equal (14, screen.X);
  900. Assert.Equal (3, screen.Y);
  901. found = View.GetViewsUnderMouse (new Point(14, 3)).LastOrDefault ();
  902. Assert.Equal (Application.Top, found);
  903. Assert.Equal (new (10, 0), view.ScreenToFrame (new (14, 3)));
  904. screen = view.ViewportToScreen (new Point (11, 1));
  905. Assert.Equal (15, screen.X);
  906. Assert.Equal (4, screen.Y);
  907. found = View.GetViewsUnderMouse (new Point(15, 4)).LastOrDefault ();
  908. Assert.Equal (Application.Top, found);
  909. Application.Top.Dispose ();
  910. Application.ResetState (ignoreDisposed: true);
  911. }
  912. [Fact]
  913. [AutoInitShutdown]
  914. public void ScreenToView_ViewToScreen_GetViewsUnderMouse_Smaller_Top ()
  915. {
  916. Application.Top = new ()
  917. {
  918. X = 3,
  919. Y = 2,
  920. Width = 20,
  921. Height = 10,
  922. BorderStyle = LineStyle.Single
  923. };
  924. var view = new View
  925. {
  926. X = 3,
  927. Y = 2,
  928. Width = 10,
  929. Height = 1,
  930. Text = "0123456789"
  931. };
  932. Application.Top.Add (view);
  933. Application.Begin (Application.Top);
  934. Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  935. Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  936. Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame);
  937. ((FakeDriver)Application.Driver!).SetBufferSize (30, 20);
  938. Assert.Equal (new (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  939. Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  940. Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame);
  941. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  942. @"
  943. ┌──────────────────┐
  944. │ │
  945. │ │
  946. │ 0123456789 │
  947. │ │
  948. │ │
  949. │ │
  950. │ │
  951. │ │
  952. └──────────────────┘"
  953. ,
  954. _output
  955. );
  956. // mean the output started at col 3 and line 2
  957. // which result with a width of 23 and a height of 10 on the output
  958. Assert.Equal (new (3, 2, 23, 10), frame);
  959. // top
  960. Assert.Equal (new (-3, -2), Application.Top.ScreenToFrame (new (0, 0)));
  961. Point screen = Application.Top.Margin.ViewportToScreen (new Point (-3, -2));
  962. Assert.Equal (0, screen.X);
  963. Assert.Equal (0, screen.Y);
  964. screen = Application.Top.Border.ViewportToScreen (new Point (-3, -2));
  965. Assert.Equal (0, screen.X);
  966. Assert.Equal (0, screen.Y);
  967. screen = Application.Top.Padding.ViewportToScreen (new Point (-3, -2));
  968. Assert.Equal (1, screen.X);
  969. Assert.Equal (1, screen.Y);
  970. screen = Application.Top.ViewportToScreen (new Point (-3, -2));
  971. Assert.Equal (1, screen.X);
  972. Assert.Equal (1, screen.Y);
  973. screen = Application.Top.ViewportToScreen (new Point (-4, -3));
  974. Assert.Equal (0, screen.X);
  975. Assert.Equal (0, screen.Y);
  976. var found = View.GetViewsUnderMouse (new Point(-4, -3)).LastOrDefault ();
  977. Assert.Null (found);
  978. Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (3, 2)));
  979. screen = Application.Top.ViewportToScreen (new Point (0, 0));
  980. Assert.Equal (4, screen.X);
  981. Assert.Equal (3, screen.Y);
  982. Assert.Equal (Application.Top.Border, View.GetViewsUnderMouse (new Point(3, 2)).LastOrDefault ());
  983. //Assert.Equal (0, found.FrameToScreen ().X);
  984. //Assert.Equal (0, found.FrameToScreen ().Y);
  985. Assert.Equal (new (10, 0), Application.Top.ScreenToFrame (new (13, 2)));
  986. screen = Application.Top.ViewportToScreen (new Point (10, 0));
  987. Assert.Equal (14, screen.X);
  988. Assert.Equal (3, screen.Y);
  989. Assert.Equal (Application.Top.Border, View.GetViewsUnderMouse (new Point(13, 2)).LastOrDefault ());
  990. //Assert.Equal (10, found.FrameToScreen ().X);
  991. //Assert.Equal (0, found.FrameToScreen ().Y);
  992. Assert.Equal (new (11, 1), Application.Top.ScreenToFrame (new (14, 3)));
  993. screen = Application.Top.ViewportToScreen (new Point (11, 1));
  994. Assert.Equal (15, screen.X);
  995. Assert.Equal (4, screen.Y);
  996. Assert.Equal (Application.Top, View.GetViewsUnderMouse (new Point(14, 3)).LastOrDefault ());
  997. // view
  998. Assert.Equal (new (-7, -5), view.ScreenToFrame (new (0, 0)));
  999. screen = view.Margin.ViewportToScreen (new Point (-6, -4));
  1000. Assert.Equal (1, screen.X);
  1001. Assert.Equal (1, screen.Y);
  1002. screen = view.Border.ViewportToScreen (new Point (-6, -4));
  1003. Assert.Equal (1, screen.X);
  1004. Assert.Equal (1, screen.Y);
  1005. screen = view.Padding.ViewportToScreen (new Point (-6, -4));
  1006. Assert.Equal (1, screen.X);
  1007. Assert.Equal (1, screen.Y);
  1008. screen = view.ViewportToScreen (new Point (-6, -4));
  1009. Assert.Equal (1, screen.X);
  1010. Assert.Equal (1, screen.Y);
  1011. Assert.Null (View.GetViewsUnderMouse (new Point(1, 1)).LastOrDefault ());
  1012. Assert.Equal (new (-4, -3), view.ScreenToFrame (new (3, 2)));
  1013. screen = view.ViewportToScreen (new Point (-3, -2));
  1014. Assert.Equal (4, screen.X);
  1015. Assert.Equal (3, screen.Y);
  1016. Assert.Equal (Application.Top, View.GetViewsUnderMouse (new Point(4, 3)).LastOrDefault ());
  1017. Assert.Equal (new (-1, -1), view.ScreenToFrame (new (6, 4)));
  1018. screen = view.ViewportToScreen (new Point (0, 0));
  1019. Assert.Equal (7, screen.X);
  1020. Assert.Equal (5, screen.Y);
  1021. Assert.Equal (view, View.GetViewsUnderMouse (new Point(7, 5)).LastOrDefault ());
  1022. Assert.Equal (new (6, -1), view.ScreenToFrame (new (13, 4)));
  1023. screen = view.ViewportToScreen (new Point (7, 0));
  1024. Assert.Equal (14, screen.X);
  1025. Assert.Equal (5, screen.Y);
  1026. Assert.Equal (view, View.GetViewsUnderMouse (new Point(14, 5)).LastOrDefault ());
  1027. Assert.Equal (new (7, -2), view.ScreenToFrame (new (14, 3)));
  1028. screen = view.ViewportToScreen (new Point (8, -1));
  1029. Assert.Equal (15, screen.X);
  1030. Assert.Equal (4, screen.Y);
  1031. Assert.Equal (Application.Top, View.GetViewsUnderMouse (new Point(15, 4)).LastOrDefault ());
  1032. Assert.Equal (new (16, -2), view.ScreenToFrame (new (23, 3)));
  1033. screen = view.ViewportToScreen (new Point (17, -1));
  1034. Assert.Equal (24, screen.X);
  1035. Assert.Equal (4, screen.Y);
  1036. Assert.Null (View.GetViewsUnderMouse (new Point(24, 4)).LastOrDefault ());
  1037. Application.Top.Dispose ();
  1038. }
  1039. }