CoordinateTests.cs 8.0 KB

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