ScreenToTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.LayoutTests;
  3. /// <summary>Tests for view coordinate mapping (e.g. <see cref="View.ScreenToFrame"/> etc...).</summary>
  4. public class ScreenToTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public ScreenToTests (ITestOutputHelper output) { _output = output; }
  8. /// <summary>
  9. /// Tests that screen to bounds mapping works correctly when the view has no superview and there ARE Adornments on the
  10. /// view.
  11. /// </summary>
  12. [Theory]
  13. [InlineData (0, 0, 0, 0, -1, -1)]
  14. [InlineData (0, 0, 1, 1, 0, 0)]
  15. [InlineData (0, 0, 9, 9, 8, 8)]
  16. [InlineData (0, 0, 11, 11, 10, 10)]
  17. [InlineData (1, 1, 0, 0, -2, -2)]
  18. [InlineData (1, 1, 1, 1, -1, -1)]
  19. [InlineData (1, 1, 9, 9, 7, 7)]
  20. [InlineData (1, 1, 11, 11, 9, 9)]
  21. public void ScreenToViewport_NoSuper_HasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  22. {
  23. var view = new View
  24. {
  25. X = viewX,
  26. Y = viewY,
  27. Width = 10,
  28. Height = 10,
  29. BorderStyle = LineStyle.Single
  30. };
  31. view.Layout ();
  32. Point actual = view.ScreenToViewport (new (x, y));
  33. Assert.Equal (expectedX, actual.X);
  34. Assert.Equal (expectedY, actual.Y);
  35. }
  36. /// <summary>
  37. /// Tests that screen to bounds mapping works correctly when the view has no superview and there are no Adornments on
  38. /// the view.
  39. /// </summary>
  40. [Theory]
  41. [InlineData (0, 0, 0, 0, 0, 0)]
  42. [InlineData (0, 0, 1, 1, 1, 1)]
  43. [InlineData (0, 0, 9, 9, 9, 9)]
  44. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  45. [InlineData (1, 1, 0, 0, -1, -1)]
  46. [InlineData (1, 1, 1, 1, 0, 0)]
  47. [InlineData (1, 1, 9, 9, 8, 8)]
  48. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  49. public void ScreenToViewport_NoSuper_NoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  50. {
  51. var view = new View { X = viewX, Y = viewY, Width = 10, Height = 10 };
  52. view.Layout ();
  53. Point actual = view.ScreenToViewport (new (x, y));
  54. Assert.Equal (expectedX, actual.X);
  55. Assert.Equal (expectedY, actual.Y);
  56. }
  57. /// <summary>Tests that screen to bounds mapping works correctly when the view has as superview it DOES have Adornments</summary>
  58. [Theory]
  59. [InlineData (0, 0, 0, 0, -1, -1)] // it's ok for the view to return coordinates outside of its bounds
  60. [InlineData (0, 0, 1, 1, 0, 0)]
  61. [InlineData (0, 0, 9, 9, 8, 8)]
  62. [InlineData (0, 0, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  63. [InlineData (1, 1, 0, 0, -2, -2)]
  64. [InlineData (1, 1, 1, 1, -1, -1)]
  65. [InlineData (1, 1, 9, 9, 7, 7)]
  66. [InlineData (1, 1, 11, 11, 9, 9)] // it's ok for the view to return coordinates outside of its bounds
  67. public void ScreenToViewport_SuperHasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  68. {
  69. var super = new View
  70. {
  71. X = 0,
  72. Y = 0,
  73. Width = 10,
  74. Height = 10,
  75. BorderStyle = LineStyle.Single
  76. };
  77. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  78. super.Add (view);
  79. super.Layout ();
  80. Point actual = view.ScreenToViewport (new (x, y));
  81. Assert.Equal (expectedX, actual.X);
  82. Assert.Equal (expectedY, actual.Y);
  83. }
  84. /// <summary>Tests that screen to bounds mapping works correctly when the view has as superview it DOES have Adornments</summary>
  85. [Theory]
  86. [InlineData (0, 0, 0, 0, -1, -1)] // it's ok for the view to return coordinates outside of its bounds
  87. [InlineData (0, 0, 1, 1, 0, 0)]
  88. [InlineData (0, 0, 9, 9, 8, 8)]
  89. [InlineData (0, 0, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  90. [InlineData (1, 1, 0, 0, -2, -2)]
  91. [InlineData (1, 1, 1, 1, -1, -1)]
  92. [InlineData (1, 1, 9, 9, 7, 7)]
  93. [InlineData (1, 1, 11, 11, 9, 9)] // it's ok for the view to return coordinates outside of its bounds
  94. public void ScreenToViewport_SuperHasAdornments_Positive_Viewport (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  95. {
  96. var super = new View
  97. {
  98. X = 0,
  99. Y = 0,
  100. Width = 10,
  101. Height = 10,
  102. BorderStyle = LineStyle.Single,
  103. };
  104. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  105. view.SetContentSize (new (6, 6));
  106. super.Add (view);
  107. super.Layout ();
  108. view.Viewport = new (1, 1, 5, 5);
  109. Point actual = view.ScreenToViewport (new (x, y));
  110. Assert.Equal (expectedX, actual.X);
  111. Assert.Equal (expectedY, actual.Y);
  112. }
  113. /// <summary>Tests that screen to bounds mapping works correctly when the view has as superview it does not have Adornments</summary>
  114. [Theory]
  115. [InlineData (0, 0, 0, 0, 0, 0)]
  116. [InlineData (0, 0, 1, 1, 1, 1)]
  117. [InlineData (0, 0, 9, 9, 9, 9)]
  118. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  119. [InlineData (1, 1, 0, 0, -1, -1)]
  120. [InlineData (1, 1, 1, 1, 0, 0)]
  121. [InlineData (1, 1, 9, 9, 8, 8)]
  122. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  123. public void ScreenToViewport_SuperHasNoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  124. {
  125. var super = new View { X = 0, Y = 0, Width = 10, Height = 10 };
  126. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  127. super.Add (view);
  128. super.Layout ();
  129. Point actual = view.ScreenToViewport (new (x, y));
  130. Assert.Equal (expectedX, actual.X);
  131. Assert.Equal (expectedY, actual.Y);
  132. }
  133. /// <summary>Tests that screen to bounds mapping works correctly when the view has as superview it DOES have Adornments</summary>
  134. [Theory]
  135. [InlineData (0, 0, 0, 0, -2, -2)] // it's ok for the view to return coordinates outside of its bounds
  136. [InlineData (0, 0, 1, 1, -1, -1)]
  137. [InlineData (0, 0, 9, 9, 7, 7)]
  138. //[InlineData (0, 0, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  139. //[InlineData (1, 1, 0, 0, -2, -2)]
  140. //[InlineData (1, 1, 1, 1, -1, -1)]
  141. //[InlineData (1, 1, 9, 9, 7, 7)]
  142. //[InlineData (1, 1, 11, 11, 9, 9)] // it's ok for the view to return coordinates outside of its bounds
  143. public void ScreenToViewport_HasAdornments_Positive_Viewport (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  144. {
  145. var super = new View
  146. {
  147. X = 0,
  148. Y = 0,
  149. Width = 10,
  150. Height = 10,
  151. BorderStyle = LineStyle.Single,
  152. };
  153. var view = new View
  154. {
  155. X = viewX, Y = viewY, Width = 5, Height = 5,
  156. BorderStyle = LineStyle.Single,
  157. };
  158. view.SetContentSize (new (10, 10));
  159. super.Add (view);
  160. super.Layout ();
  161. view.Viewport = view.Viewport with { Location = new (1, 1) };
  162. Point actual = view.ScreenToViewport (new (x, y));
  163. Assert.Equal (expectedX, actual.X);
  164. Assert.Equal (expectedY, actual.Y);
  165. }
  166. /// <summary>
  167. /// Tests that screen to view mapping works correctly when the view has no superview and there ARE Adornments on the
  168. /// view.
  169. /// </summary>
  170. [Theory]
  171. [InlineData (0, 0, 0, 0, 0, 0)]
  172. [InlineData (0, 0, 1, 1, 1, 1)]
  173. [InlineData (0, 0, 9, 9, 9, 9)]
  174. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  175. [InlineData (1, 1, 0, 0, -1, -1)]
  176. [InlineData (1, 1, 1, 1, 0, 0)]
  177. [InlineData (1, 1, 9, 9, 8, 8)]
  178. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  179. public void ScreenToFrame_NoSuper_HasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  180. {
  181. var view = new View
  182. {
  183. X = viewX,
  184. Y = viewY,
  185. Width = 10,
  186. Height = 10,
  187. BorderStyle = LineStyle.Single
  188. };
  189. view.Layout ();
  190. Point actual = view.ScreenToFrame (new (x, y));
  191. Assert.Equal (expectedX, actual.X);
  192. Assert.Equal (expectedY, actual.Y);
  193. }
  194. /// <summary>
  195. /// Tests that screen to view mapping works correctly when the view has no superview and there are no Adornments on
  196. /// the view.
  197. /// </summary>
  198. [Theory]
  199. [InlineData (0, 0, 0, 0, 0, 0)]
  200. [InlineData (0, 0, 1, 1, 1, 1)]
  201. [InlineData (0, 0, 9, 9, 9, 9)]
  202. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  203. [InlineData (1, 1, 0, 0, -1, -1)]
  204. [InlineData (1, 1, 1, 1, 0, 0)]
  205. [InlineData (1, 1, 9, 9, 8, 8)]
  206. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  207. public void ScreenToFrame_NoSuper_NoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  208. {
  209. var view = new View { X = viewX, Y = viewY, Width = 10, Height = 10 };
  210. view.Layout ();
  211. Point actual = view.ScreenToFrame (new (x, y));
  212. Assert.Equal (expectedX, actual.X);
  213. Assert.Equal (expectedY, actual.Y);
  214. }
  215. /// <summary>Tests that screen to view mapping works correctly when the view has as superview it DOES have Adornments</summary>
  216. [Theory]
  217. [InlineData (0, 0, 0, 0, -1, -1)] // it's ok for the view to return coordinates outside of its bounds
  218. [InlineData (0, 0, 1, 1, 0, 0)]
  219. [InlineData (0, 0, 9, 9, 8, 8)]
  220. [InlineData (0, 0, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  221. [InlineData (1, 1, 0, 0, -2, -2)]
  222. [InlineData (1, 1, 1, 1, -1, -1)]
  223. [InlineData (1, 1, 9, 9, 7, 7)]
  224. [InlineData (1, 1, 11, 11, 9, 9)] // it's ok for the view to return coordinates outside of its bounds
  225. public void ScreenToFrame_SuperHasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  226. {
  227. var super = new View
  228. {
  229. X = 0,
  230. Y = 0,
  231. Width = 10,
  232. Height = 10,
  233. BorderStyle = LineStyle.Single
  234. };
  235. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  236. super.Add (view);
  237. super.Layout ();
  238. Point actual = view.ScreenToFrame (new (x, y));
  239. Assert.Equal (expectedX, actual.X);
  240. Assert.Equal (expectedY, actual.Y);
  241. }
  242. /// <summary>Tests that screen to view mapping works correctly when the view has as superview it does not have Adornments</summary>
  243. [Theory]
  244. [InlineData (0, 0, 0, 0, 0, 0)]
  245. [InlineData (0, 0, 1, 1, 1, 1)]
  246. [InlineData (0, 0, 9, 9, 9, 9)]
  247. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  248. [InlineData (1, 1, 0, 0, -1, -1)]
  249. [InlineData (1, 1, 1, 1, 0, 0)]
  250. [InlineData (1, 1, 9, 9, 8, 8)]
  251. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  252. public void ScreenToView_SuperHasNoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  253. {
  254. var super = new View { X = 0, Y = 0, Width = 10, Height = 10 };
  255. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  256. super.Add (view);
  257. super.Layout ();
  258. Point actual = view.ScreenToFrame (new (x, y));
  259. Assert.Equal (expectedX, actual.X);
  260. Assert.Equal (expectedY, actual.Y);
  261. }
  262. }