ScreenToTests.cs 11 KB

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