ScreenToTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  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 ScreenToBounds_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.ScreenToBounds (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 ScreenToBounds_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.ScreenToBounds (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 ScreenToBounds_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.ScreenToFrame (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 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 ScreenToBounds_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. Point actual = view.ScreenToBounds (x, y);
  97. Assert.Equal (expectedX, actual.X);
  98. Assert.Equal (expectedY, actual.Y);
  99. }
  100. /// <summary>
  101. /// Tests that screen to view mapping works correctly when the view has no superview and there ARE Adornments on the
  102. /// view.
  103. /// </summary>
  104. [Theory]
  105. [InlineData (0, 0, 0, 0, 0, 0)]
  106. [InlineData (0, 0, 1, 1, 1, 1)]
  107. [InlineData (0, 0, 9, 9, 9, 9)]
  108. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  109. [InlineData (1, 1, 0, 0, -1, -1)]
  110. [InlineData (1, 1, 1, 1, 0, 0)]
  111. [InlineData (1, 1, 9, 9, 8, 8)]
  112. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  113. public void ScreenToView_NoSuper_HasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  114. {
  115. var view = new View
  116. {
  117. X = viewX,
  118. Y = viewY,
  119. Width = 10,
  120. Height = 10,
  121. BorderStyle = LineStyle.Single
  122. };
  123. Point actual = view.ScreenToFrame (x, y);
  124. Assert.Equal (expectedX, actual.X);
  125. Assert.Equal (expectedY, actual.Y);
  126. }
  127. /// <summary>
  128. /// Tests that screen to view mapping works correctly when the view has no superview and there are no Adornments on
  129. /// the view.
  130. /// </summary>
  131. [Theory]
  132. [InlineData (0, 0, 0, 0, 0, 0)]
  133. [InlineData (0, 0, 1, 1, 1, 1)]
  134. [InlineData (0, 0, 9, 9, 9, 9)]
  135. [InlineData (0, 0, 11, 11, 11, 11)] // it's ok for the view to return coordinates outside of its bounds
  136. [InlineData (1, 1, 0, 0, -1, -1)]
  137. [InlineData (1, 1, 1, 1, 0, 0)]
  138. [InlineData (1, 1, 9, 9, 8, 8)]
  139. [InlineData (1, 1, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  140. public void ScreenToView_NoSuper_NoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  141. {
  142. var view = new View { X = viewX, Y = viewY, Width = 10, Height = 10 };
  143. Point actual = view.ScreenToFrame (x, y);
  144. Assert.Equal (expectedX, actual.X);
  145. Assert.Equal (expectedY, actual.Y);
  146. }
  147. /// <summary>Tests that screen to view mapping works correctly when the view has as superview it DOES have Adornments</summary>
  148. [Theory]
  149. [InlineData (0, 0, 0, 0, -1, -1)] // it's ok for the view to return coordinates outside of its bounds
  150. [InlineData (0, 0, 1, 1, 0, 0)]
  151. [InlineData (0, 0, 9, 9, 8, 8)]
  152. [InlineData (0, 0, 11, 11, 10, 10)] // it's ok for the view to return coordinates outside of its bounds
  153. [InlineData (1, 1, 0, 0, -2, -2)]
  154. [InlineData (1, 1, 1, 1, -1, -1)]
  155. [InlineData (1, 1, 9, 9, 7, 7)]
  156. [InlineData (1, 1, 11, 11, 9, 9)] // it's ok for the view to return coordinates outside of its bounds
  157. public void ScreenToView_SuperHasAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  158. {
  159. var super = new View
  160. {
  161. X = 0,
  162. Y = 0,
  163. Width = 10,
  164. Height = 10,
  165. BorderStyle = LineStyle.Single
  166. };
  167. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  168. super.Add (view);
  169. Point actual = view.ScreenToFrame (x, y);
  170. Assert.Equal (expectedX, actual.X);
  171. Assert.Equal (expectedY, actual.Y);
  172. }
  173. /// <summary>Tests that screen to view mapping works correctly when the view has as superview it does not have Adornments</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 ScreenToView_SuperHasNoAdornments (int viewX, int viewY, int x, int y, int expectedX, int expectedY)
  184. {
  185. var super = new View { X = 0, Y = 0, Width = 10, Height = 10 };
  186. var view = new View { X = viewX, Y = viewY, Width = 5, Height = 5 };
  187. super.Add (view);
  188. Point actual = view.ScreenToFrame (x, y);
  189. Assert.Equal (expectedX, actual.X);
  190. Assert.Equal (expectedY, actual.Y);
  191. }
  192. }