ToScreenTests.cs 37 KB

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