ScreenToTests.cs 11 KB

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