CoordinateTests.cs 7.8 KB

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