GetViewsUnderMouseTests.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #nullable enable
  2. namespace Terminal.Gui.ViewTests;
  3. public class GetViewsUnderMouseTests
  4. {
  5. [Theory]
  6. [InlineData (0, 0, 0, 0, 0, -1, -1, null)]
  7. [InlineData (0, 0, 0, 0, 0, 0, 0, typeof (Toplevel))]
  8. [InlineData (0, 0, 0, 0, 0, 1, 1, typeof (Toplevel))]
  9. [InlineData (0, 0, 0, 0, 0, 4, 4, typeof (Toplevel))]
  10. [InlineData (0, 0, 0, 0, 0, 9, 9, typeof (Toplevel))]
  11. [InlineData (0, 0, 0, 0, 0, 10, 10, null)]
  12. [InlineData (1, 1, 0, 0, 0, -1, -1, null)]
  13. [InlineData (1, 1, 0, 0, 0, 0, 0, null)]
  14. [InlineData (1, 1, 0, 0, 0, 1, 1, typeof (Toplevel))]
  15. [InlineData (1, 1, 0, 0, 0, 4, 4, typeof (Toplevel))]
  16. [InlineData (1, 1, 0, 0, 0, 9, 9, typeof (Toplevel))]
  17. [InlineData (1, 1, 0, 0, 0, 10, 10, typeof (Toplevel))]
  18. [InlineData (0, 0, 1, 0, 0, -1, -1, null)]
  19. [InlineData (0, 0, 1, 0, 0, 0, 0, typeof (Margin))]
  20. [InlineData (0, 0, 1, 0, 0, 1, 1, typeof (Toplevel))]
  21. [InlineData (0, 0, 1, 0, 0, 4, 4, typeof (Toplevel))]
  22. [InlineData (0, 0, 1, 0, 0, 9, 9, typeof (Margin))]
  23. [InlineData (0, 0, 1, 0, 0, 10, 10, null)]
  24. [InlineData (0, 0, 1, 1, 0, -1, -1, null)]
  25. [InlineData (0, 0, 1, 1, 0, 0, 0, typeof (Margin))]
  26. [InlineData (0, 0, 1, 1, 0, 1, 1, typeof (Border))]
  27. [InlineData (0, 0, 1, 1, 0, 4, 4, typeof (Toplevel))]
  28. [InlineData (0, 0, 1, 1, 0, 9, 9, typeof (Margin))]
  29. [InlineData (0, 0, 1, 1, 0, 10, 10, null)]
  30. [InlineData (0, 0, 1, 1, 1, -1, -1, null)]
  31. [InlineData (0, 0, 1, 1, 1, 0, 0, typeof (Margin))]
  32. [InlineData (0, 0, 1, 1, 1, 1, 1, typeof (Border))]
  33. [InlineData (0, 0, 1, 1, 1, 2, 2, typeof (Padding))]
  34. [InlineData (0, 0, 1, 1, 1, 4, 4, typeof (Toplevel))]
  35. [InlineData (0, 0, 1, 1, 1, 9, 9, typeof (Margin))]
  36. [InlineData (0, 0, 1, 1, 1, 10, 10, null)]
  37. [InlineData (1, 1, 1, 0, 0, -1, -1, null)]
  38. [InlineData (1, 1, 1, 0, 0, 0, 0, null)]
  39. [InlineData (1, 1, 1, 0, 0, 1, 1, typeof (Margin))]
  40. [InlineData (1, 1, 1, 0, 0, 4, 4, typeof (Toplevel))]
  41. [InlineData (1, 1, 1, 0, 0, 9, 9, typeof (Toplevel))]
  42. [InlineData (1, 1, 1, 0, 0, 10, 10, typeof (Margin))]
  43. [InlineData (1, 1, 1, 1, 0, -1, -1, null)]
  44. [InlineData (1, 1, 1, 1, 0, 0, 0, null)]
  45. [InlineData (1, 1, 1, 1, 0, 1, 1, typeof (Margin))]
  46. [InlineData (1, 1, 1, 1, 0, 4, 4, typeof (Toplevel))]
  47. [InlineData (1, 1, 1, 1, 0, 9, 9, typeof (Border))]
  48. [InlineData (1, 1, 1, 1, 0, 10, 10, typeof (Margin))]
  49. [InlineData (1, 1, 1, 1, 1, -1, -1, null)]
  50. [InlineData (1, 1, 1, 1, 1, 0, 0, null)]
  51. [InlineData (1, 1, 1, 1, 1, 1, 1, typeof (Margin))]
  52. [InlineData (1, 1, 1, 1, 1, 2, 2, typeof (Border))]
  53. [InlineData (1, 1, 1, 1, 1, 3, 3, typeof (Padding))]
  54. [InlineData (1, 1, 1, 1, 1, 4, 4, typeof (Toplevel))]
  55. [InlineData (1, 1, 1, 1, 1, 8, 8, typeof (Padding))]
  56. [InlineData (1, 1, 1, 1, 1, 9, 9, typeof (Border))]
  57. [InlineData (1, 1, 1, 1, 1, 10, 10, typeof (Margin))]
  58. public void GetViewsUnderMouse_Top_Adornments_Returns_Correct_View (int frameX, int frameY, int marginThickness, int borderThickness, int paddingThickness, int testX, int testY, Type? expectedViewType)
  59. {
  60. // Arrange
  61. Application.Top = new ()
  62. {
  63. Frame = new Rectangle (frameX, frameY, 10, 10),
  64. };
  65. Application.Top.Margin.Thickness = new Thickness (marginThickness);
  66. Application.Top.Border.Thickness = new Thickness (borderThickness);
  67. Application.Top.Padding.Thickness = new Thickness (paddingThickness);
  68. var location = new Point (testX, testY);
  69. // Act
  70. var viewsUnderMouse = View.GetViewsUnderMouse (location);
  71. // Assert
  72. if (expectedViewType == null)
  73. {
  74. Assert.Empty (viewsUnderMouse);
  75. }
  76. else
  77. {
  78. Assert.Contains (viewsUnderMouse, v => v?.GetType () == expectedViewType);
  79. }
  80. Application.Top.Dispose ();
  81. Application.ResetState (ignoreDisposed: true);
  82. }
  83. [Theory]
  84. [InlineData (0, 0)]
  85. [InlineData (1, 1)]
  86. [InlineData (2, 2)]
  87. public void GetViewsUnderMouse_Returns_Top_If_No_SubViews (int testX, int testY)
  88. {
  89. // Arrange
  90. Application.Top = new ()
  91. {
  92. Frame = new Rectangle (0, 0, 10, 10)
  93. };
  94. var location = new Point (testX, testY);
  95. // Act
  96. var viewsUnderMouse = View.GetViewsUnderMouse (location);
  97. // Assert
  98. Assert.Contains (viewsUnderMouse, v => v == Application.Top);
  99. Application.Top.Dispose ();
  100. Application.ResetState (ignoreDisposed: true);
  101. }
  102. [Theory]
  103. [InlineData (0, 0)]
  104. [InlineData (2, 1)]
  105. [InlineData (20, 20)]
  106. public void GetViewsUnderMouse_Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
  107. {
  108. // Arrange
  109. var view = new View
  110. {
  111. Frame = new Rectangle (0, 0, 10, 10)
  112. };
  113. var location = new Point (testX, testY);
  114. // Act
  115. var viewsUnderMouse = View.GetViewsUnderMouse (location);
  116. // Assert
  117. Assert.Empty (viewsUnderMouse);
  118. }
  119. [Theory]
  120. [InlineData (0, 0)]
  121. [InlineData (2, 1)]
  122. [InlineData (20, 20)]
  123. public void GetViewsUnderMouse_Returns_Null_If_Start_Not_Visible (int testX, int testY)
  124. {
  125. // Arrange
  126. var view = new View
  127. {
  128. Frame = new Rectangle (0, 0, 10, 10),
  129. Visible = false
  130. };
  131. var location = new Point (testX, testY);
  132. // Act
  133. var viewsUnderMouse = View.GetViewsUnderMouse (location);
  134. // Assert
  135. Assert.Empty (viewsUnderMouse);
  136. }
  137. [Theory]
  138. [InlineData (0, 0, false)]
  139. [InlineData (1, 1, true)]
  140. [InlineData (9, 9, false)]
  141. [InlineData (10, 10, false)]
  142. [InlineData (6, 7, true)]
  143. [InlineData (1, 2, true)]
  144. [InlineData (5, 6, true)]
  145. public void GetViewsUnderMouse_Returns_Correct_If_SubViews (int testX, int testY, bool expected)
  146. {
  147. // Arrange
  148. Application.Top = new ()
  149. {
  150. Frame = new Rectangle (0, 0, 10, 10)
  151. };
  152. var subView = new View
  153. {
  154. Frame = new Rectangle (1, 1, 8, 8)
  155. };
  156. Application.Top.Add (subView);
  157. var location = new Point (testX, testY);
  158. // Act
  159. var viewsUnderMouse = View.GetViewsUnderMouse (location);
  160. // Assert
  161. if (expected)
  162. {
  163. Assert.Contains (viewsUnderMouse, v => v == subView);
  164. }
  165. else
  166. {
  167. Assert.DoesNotContain (viewsUnderMouse, v => v == subView);
  168. }
  169. Application.Top.Dispose ();
  170. Application.ResetState (ignoreDisposed: true);
  171. }
  172. [Theory]
  173. [InlineData (0, 0, 0, 0, 0, -1, -1, null)]
  174. [InlineData (0, 0, 0, 0, 0, 0, 0, typeof (View))]
  175. [InlineData (0, 0, 0, 0, 0, 1, 1, typeof (View))]
  176. [InlineData (0, 0, 0, 0, 0, 4, 4, typeof (View))]
  177. [InlineData (0, 0, 0, 0, 0, 9, 9, typeof (View))]
  178. [InlineData (0, 0, 0, 0, 0, 10, 10, null)]
  179. [InlineData (1, 1, 0, 0, 0, -1, -1, null)]
  180. [InlineData (1, 1, 0, 0, 0, 0, 0, null)]
  181. [InlineData (1, 1, 0, 0, 0, 1, 1, typeof (View))]
  182. [InlineData (1, 1, 0, 0, 0, 4, 4, typeof (View))]
  183. [InlineData (1, 1, 0, 0, 0, 9, 9, typeof (View))]
  184. [InlineData (1, 1, 0, 0, 0, 10, 10, typeof (View))]
  185. [InlineData (0, 0, 1, 0, 0, -1, -1, null)]
  186. [InlineData (0, 0, 1, 0, 0, 0, 0, typeof (Margin))]
  187. [InlineData (0, 0, 1, 0, 0, 1, 1, typeof (View))]
  188. [InlineData (0, 0, 1, 0, 0, 4, 4, typeof (View))]
  189. [InlineData (0, 0, 1, 0, 0, 9, 9, typeof (Margin))]
  190. [InlineData (0, 0, 1, 0, 0, 10, 10, null)]
  191. [InlineData (0, 0, 1, 1, 0, -1, -1, null)]
  192. [InlineData (0, 0, 1, 1, 0, 0, 0, typeof (Margin))]
  193. [InlineData (0, 0, 1, 1, 0, 1, 1, typeof (Border))]
  194. [InlineData (0, 0, 1, 1, 0, 4, 4, typeof (View))]
  195. [InlineData (0, 0, 1, 1, 0, 9, 9, typeof (Margin))]
  196. [InlineData (0, 0, 1, 1, 0, 10, 10, null)]
  197. [InlineData (0, 0, 1, 1, 1, -1, -1, null)]
  198. [InlineData (0, 0, 1, 1, 1, 0, 0, typeof (Margin))]
  199. [InlineData (0, 0, 1, 1, 1, 1, 1, typeof (Border))]
  200. [InlineData (0, 0, 1, 1, 1, 2, 2, typeof (Padding))]
  201. [InlineData (0, 0, 1, 1, 1, 4, 4, typeof (View))]
  202. [InlineData (0, 0, 1, 1, 1, 9, 9, typeof (Margin))]
  203. [InlineData (0, 0, 1, 1, 1, 10, 10, null)]
  204. [InlineData (1, 1, 1, 0, 0, -1, -1, null)]
  205. [InlineData (1, 1, 1, 0, 0, 0, 0, null)]
  206. [InlineData (1, 1, 1, 0, 0, 1, 1, typeof (Margin))]
  207. [InlineData (1, 1, 1, 0, 0, 4, 4, typeof (View))]
  208. [InlineData (1, 1, 1, 0, 0, 9, 9, typeof (View))]
  209. [InlineData (1, 1, 1, 0, 0, 10, 10, typeof (Margin))]
  210. [InlineData (1, 1, 1, 1, 0, -1, -1, null)]
  211. [InlineData (1, 1, 1, 1, 0, 0, 0, null)]
  212. [InlineData (1, 1, 1, 1, 0, 1, 1, typeof (Margin))]
  213. [InlineData (1, 1, 1, 1, 0, 4, 4, typeof (View))]
  214. [InlineData (1, 1, 1, 1, 0, 9, 9, typeof (Border))]
  215. [InlineData (1, 1, 1, 1, 0, 10, 10, typeof (Margin))]
  216. [InlineData (1, 1, 1, 1, 1, -1, -1, null)]
  217. [InlineData (1, 1, 1, 1, 1, 0, 0, null)]
  218. [InlineData (1, 1, 1, 1, 1, 1, 1, typeof (Margin))]
  219. [InlineData (1, 1, 1, 1, 1, 2, 2, typeof (Border))]
  220. [InlineData (1, 1, 1, 1, 1, 3, 3, typeof (Padding))]
  221. [InlineData (1, 1, 1, 1, 1, 4, 4, typeof (View))]
  222. [InlineData (1, 1, 1, 1, 1, 8, 8, typeof (Padding))]
  223. [InlineData (1, 1, 1, 1, 1, 9, 9, typeof (Border))]
  224. [InlineData (1, 1, 1, 1, 1, 10, 10, typeof (Margin))]
  225. public void Contains (int frameX, int frameY, int marginThickness, int borderThickness, int paddingThickness, int testX, int testY, Type? expectedAdornmentType)
  226. {
  227. var view = new View ()
  228. {
  229. X = frameX, Y = frameY,
  230. Width = 10, Height = 10,
  231. };
  232. view.Margin.Thickness = new Thickness (marginThickness);
  233. view.Border.Thickness = new Thickness (borderThickness);
  234. view.Padding.Thickness = new Thickness (paddingThickness);
  235. Type? containedType = null;
  236. if (view.Contains (new (testX, testY)))
  237. {
  238. containedType = view.GetType ();
  239. }
  240. if (view.Margin.Contains (new (testX, testY)))
  241. {
  242. containedType = view.Margin.GetType ();
  243. }
  244. if (view.Border.Contains (new (testX, testY)))
  245. {
  246. containedType = view.Border.GetType ();
  247. }
  248. if (view.Padding.Contains (new (testX, testY)))
  249. {
  250. containedType = view.Padding.GetType ();
  251. }
  252. Assert.Equal (expectedAdornmentType, containedType);
  253. }
  254. // Test that GetViewsUnderMouse returns the correct view if the start view has no subviews
  255. [Theory]
  256. [InlineData (0, 0)]
  257. [InlineData (1, 1)]
  258. [InlineData (2, 2)]
  259. public void Returns_Start_If_No_SubViews (int testX, int testY)
  260. {
  261. Application.Top = new ()
  262. {
  263. Width = 10, Height = 10,
  264. };
  265. Assert.Same (Application.Top, View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault());
  266. Application.Top.Dispose ();
  267. Application.ResetState (ignoreDisposed: true);
  268. }
  269. // Test that GetViewsUnderMouse returns null if the start view has no subviews and coords are outside the view
  270. [Theory]
  271. [InlineData (0, 0)]
  272. [InlineData (2, 1)]
  273. [InlineData (20, 20)]
  274. public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
  275. {
  276. Application.Top = new ()
  277. {
  278. X = 1, Y = 2,
  279. Width = 10, Height = 10,
  280. };
  281. Assert.Null (View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault());
  282. Application.Top.Dispose ();
  283. Application.ResetState (ignoreDisposed: true);
  284. }
  285. [Theory]
  286. [InlineData (0, 0)]
  287. [InlineData (2, 1)]
  288. [InlineData (20, 20)]
  289. public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
  290. {
  291. Application.Top = new ()
  292. {
  293. X = 1, Y = 2,
  294. Width = 10, Height = 10,
  295. Visible = false,
  296. };
  297. Assert.Null (View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault());
  298. Application.Top.Dispose ();
  299. Application.ResetState (ignoreDisposed: true);
  300. }
  301. // Test that GetViewsUnderMouse returns the correct view if the start view has subviews
  302. [Theory]
  303. [InlineData (0, 0, false)]
  304. [InlineData (1, 1, false)]
  305. [InlineData (9, 9, false)]
  306. [InlineData (10, 10, false)]
  307. [InlineData (6, 7, false)]
  308. [InlineData (1, 2, true)]
  309. [InlineData (5, 6, true)]
  310. public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
  311. {
  312. Application.Top = new ()
  313. {
  314. Width = 10, Height = 10,
  315. };
  316. var subview = new View ()
  317. {
  318. X = 1, Y = 2,
  319. Width = 5, Height = 5,
  320. };
  321. Application.Top.Add (subview);
  322. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  323. Assert.Equal (expectedSubViewFound, found == subview);
  324. Application.Top.Dispose ();
  325. Application.ResetState (ignoreDisposed: true);
  326. }
  327. [Theory]
  328. [InlineData (0, 0, false)]
  329. [InlineData (1, 1, false)]
  330. [InlineData (9, 9, false)]
  331. [InlineData (10, 10, false)]
  332. [InlineData (6, 7, false)]
  333. [InlineData (1, 2, false)]
  334. [InlineData (5, 6, false)]
  335. public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
  336. {
  337. Application.Top = new ()
  338. {
  339. Width = 10, Height = 10,
  340. };
  341. var subview = new View ()
  342. {
  343. X = 1, Y = 2,
  344. Width = 5, Height = 5,
  345. Visible = false
  346. };
  347. Application.Top.Add (subview);
  348. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  349. Assert.Equal (expectedSubViewFound, found == subview);
  350. Application.Top.Dispose ();
  351. Application.ResetState (ignoreDisposed: true);
  352. }
  353. [Theory]
  354. [InlineData (0, 0, false)]
  355. [InlineData (1, 1, false)]
  356. [InlineData (9, 9, false)]
  357. [InlineData (10, 10, false)]
  358. [InlineData (6, 7, false)]
  359. [InlineData (1, 2, false)]
  360. [InlineData (5, 6, false)]
  361. public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
  362. {
  363. Application.Top = new ()
  364. {
  365. Width = 10, Height = 10,
  366. Visible = false
  367. };
  368. var subview = new View ()
  369. {
  370. X = 1, Y = 2,
  371. Width = 5, Height = 5,
  372. };
  373. Application.Top.Add (subview);
  374. subview.Visible = true;
  375. Assert.True (subview.Visible);
  376. Assert.False (Application.Top.Visible);
  377. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  378. Assert.Equal (expectedSubViewFound, found == subview);
  379. Application.Top.Dispose ();
  380. Application.ResetState (ignoreDisposed: true);
  381. }
  382. // Test that GetViewsUnderMouse works if the start view has positive Adornments
  383. [Theory]
  384. [InlineData (0, 0, false)]
  385. [InlineData (1, 1, false)]
  386. [InlineData (9, 9, false)]
  387. [InlineData (10, 10, false)]
  388. [InlineData (7, 8, false)]
  389. [InlineData (1, 2, false)]
  390. [InlineData (2, 3, true)]
  391. [InlineData (5, 6, true)]
  392. [InlineData (6, 7, true)]
  393. public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  394. {
  395. Application.Top = new ()
  396. {
  397. Width = 10, Height = 10,
  398. };
  399. Application.Top.Margin.Thickness = new Thickness (1);
  400. var subview = new View ()
  401. {
  402. X = 1, Y = 2,
  403. Width = 5, Height = 5,
  404. };
  405. Application.Top.Add (subview);
  406. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  407. Assert.Equal (expectedSubViewFound, found == subview);
  408. Application.Top.Dispose ();
  409. Application.ResetState (ignoreDisposed: true);
  410. }
  411. // Test that GetViewsUnderMouse works if the start view has offset Viewport location
  412. [Theory]
  413. [InlineData (1, 0, 0, true)]
  414. [InlineData (1, 1, 1, true)]
  415. [InlineData (1, 2, 2, false)]
  416. [InlineData (-1, 3, 3, true)]
  417. [InlineData (-1, 2, 2, true)]
  418. [InlineData (-1, 1, 1, false)]
  419. [InlineData (-1, 0, 0, false)]
  420. public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound)
  421. {
  422. Application.Top = new ()
  423. {
  424. Width = 10, Height = 10,
  425. ViewportSettings = ViewportSettings.AllowNegativeLocation
  426. };
  427. Application.Top.Viewport = new (offset, offset, 10, 10);
  428. var subview = new View ()
  429. {
  430. X = 1, Y = 1,
  431. Width = 2, Height = 2,
  432. };
  433. Application.Top.Add (subview);
  434. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  435. Assert.Equal (expectedSubViewFound, found == subview);
  436. Application.Top.Dispose ();
  437. Application.ResetState (ignoreDisposed: true);
  438. }
  439. [Theory]
  440. [InlineData (9, 9, true)]
  441. [InlineData (0, 0, false)]
  442. [InlineData (1, 1, false)]
  443. [InlineData (10, 10, false)]
  444. [InlineData (7, 8, false)]
  445. [InlineData (1, 2, false)]
  446. [InlineData (2, 3, false)]
  447. [InlineData (5, 6, false)]
  448. [InlineData (6, 7, false)]
  449. public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  450. {
  451. Application.Top = new ()
  452. {
  453. Width = 10, Height = 10,
  454. };
  455. Application.Top.Padding.Thickness = new Thickness (1);
  456. var subview = new View ()
  457. {
  458. X = Pos.AnchorEnd (1), Y = Pos.AnchorEnd (1),
  459. Width = 1, Height = 1,
  460. };
  461. Application.Top.Padding.Add (subview);
  462. Application.Top.BeginInit ();
  463. Application.Top.EndInit ();
  464. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  465. Assert.Equal (expectedSubViewFound, found == subview);
  466. Application.Top.Dispose ();
  467. Application.ResetState (ignoreDisposed: true);
  468. }
  469. [Theory]
  470. [InlineData (0, 0, typeof (Margin))]
  471. [InlineData (9, 9, typeof (Margin))]
  472. [InlineData (1, 1, typeof (Border))]
  473. [InlineData (8, 8, typeof (Border))]
  474. [InlineData (2, 2, typeof (Padding))]
  475. [InlineData (7, 7, typeof (Padding))]
  476. [InlineData (5, 5, typeof (Toplevel))]
  477. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  478. {
  479. Application.Top = new ()
  480. {
  481. Width = 10, Height = 10,
  482. };
  483. Application.Top.Margin.Thickness = new Thickness (1);
  484. Application.Top.Border.Thickness = new Thickness (1);
  485. Application.Top.Padding.Thickness = new Thickness (1);
  486. var subview = new View ()
  487. {
  488. X = 1, Y = 1,
  489. Width = 1, Height = 1,
  490. };
  491. Application.Top.Add (subview);
  492. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  493. Assert.Equal (expectedAdornmentType, found!.GetType ());
  494. Application.Top.Dispose ();
  495. Application.ResetState (ignoreDisposed: true);
  496. }
  497. // Test that GetViewsUnderMouse works if the subview has positive Adornments
  498. [Theory]
  499. [InlineData (0, 0, false)]
  500. [InlineData (1, 1, false)]
  501. [InlineData (9, 9, false)]
  502. [InlineData (10, 10, false)]
  503. [InlineData (7, 8, false)]
  504. [InlineData (6, 7, false)]
  505. [InlineData (1, 2, false)]
  506. [InlineData (5, 6, false)]
  507. [InlineData (2, 3, true)]
  508. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  509. {
  510. Application.Top = new ()
  511. {
  512. Width = 10, Height = 10,
  513. };
  514. var subview = new View ()
  515. {
  516. X = 1, Y = 2,
  517. Width = 5, Height = 5,
  518. };
  519. subview.Margin.Thickness = new Thickness (1);
  520. Application.Top.Add (subview);
  521. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  522. Assert.Equal (expectedSubViewFound, found == subview);
  523. Application.Top.Dispose ();
  524. Application.ResetState (ignoreDisposed: true);
  525. }
  526. [Theory]
  527. [InlineData (0, 0, false)]
  528. [InlineData (1, 1, false)]
  529. [InlineData (9, 9, false)]
  530. [InlineData (10, 10, false)]
  531. [InlineData (7, 8, false)]
  532. [InlineData (6, 7, false)]
  533. [InlineData (1, 2, false)]
  534. [InlineData (5, 6, false)]
  535. [InlineData (6, 5, false)]
  536. [InlineData (5, 5, true)]
  537. public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  538. {
  539. Application.Top = new ()
  540. {
  541. Width = 10, Height = 10,
  542. };
  543. // A subview with + Padding
  544. var subview = new View ()
  545. {
  546. X = 1, Y = 1,
  547. Width = 5, Height = 5,
  548. };
  549. subview.Padding.Thickness = new (1);
  550. // This subview will be at the bottom-right-corner of subview
  551. // So screen-relative location will be X + Width - 1 = 5
  552. var paddingSubview = new View ()
  553. {
  554. X = Pos.AnchorEnd (1),
  555. Y = Pos.AnchorEnd (1),
  556. Width = 1,
  557. Height = 1,
  558. };
  559. subview.Padding.Add (paddingSubview);
  560. Application.Top.Add (subview);
  561. Application.Top.BeginInit ();
  562. Application.Top.EndInit ();
  563. var found = View.GetViewsUnderMouse(new Point(testX, testY)).LastOrDefault();
  564. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  565. Application.Top.Dispose ();
  566. Application.ResetState (ignoreDisposed: true);
  567. }
  568. [Theory]
  569. [InlineData (0, 0, false)]
  570. [InlineData (1, 1, false)]
  571. [InlineData (9, 9, false)]
  572. [InlineData (10, 10, false)]
  573. [InlineData (7, 8, false)]
  574. [InlineData (6, 7, false)]
  575. [InlineData (1, 2, false)]
  576. [InlineData (5, 6, false)]
  577. [InlineData (6, 5, false)]
  578. [InlineData (5, 5, true)]
  579. public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  580. {
  581. Application.Top = new ()
  582. {
  583. Width = 10, Height = 10,
  584. };
  585. // A subview with + Padding
  586. var subview = new View ()
  587. {
  588. X = 1, Y = 1,
  589. Width = 5, Height = 5,
  590. };
  591. subview.Padding.Thickness = new (1);
  592. // Scroll the subview
  593. subview.SetContentSize (new (10, 10));
  594. subview.Viewport = subview.Viewport with { Location = new (1, 1) };
  595. // This subview will be at the bottom-right-corner of subview
  596. // So screen-relative location will be X + Width - 1 = 5
  597. var paddingSubview = new View ()
  598. {
  599. X = Pos.AnchorEnd (1),
  600. Y = Pos.AnchorEnd (1),
  601. Width = 1,
  602. Height = 1,
  603. };
  604. subview.Padding.Add (paddingSubview);
  605. Application.Top.Add (subview);
  606. Application.Top.BeginInit ();
  607. Application.Top.EndInit ();
  608. var found = View.GetViewsUnderMouse(new (testX, testY)).LastOrDefault();
  609. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  610. Application.Top.Dispose ();
  611. Application.ResetState (ignoreDisposed: true);
  612. }
  613. // Test that GetViewsUnderMouse works with nested subviews
  614. [Theory]
  615. [InlineData (0, 0, -1)]
  616. [InlineData (9, 9, -1)]
  617. [InlineData (10, 10, -1)]
  618. [InlineData (1, 1, 0)]
  619. [InlineData (1, 2, 0)]
  620. [InlineData (2, 2, 1)]
  621. [InlineData (3, 3, 2)]
  622. [InlineData (5, 5, 2)]
  623. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  624. {
  625. Application.Top = new ()
  626. {
  627. Width = 10, Height = 10
  628. };
  629. int numSubViews = 3;
  630. List<View> subviews = new List<View> ();
  631. for (int i = 0; i < numSubViews; i++)
  632. {
  633. var subview = new View ()
  634. {
  635. X = 1, Y = 1,
  636. Width = 5, Height = 5,
  637. };
  638. subviews.Add (subview);
  639. if (i > 0)
  640. {
  641. subviews [i - 1].Add (subview);
  642. }
  643. }
  644. Application.Top.Add (subviews [0]);
  645. var found = View.GetViewsUnderMouse(new (testX, testY)).LastOrDefault();
  646. Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!));
  647. Application.Top.Dispose ();
  648. Application.ResetState (ignoreDisposed: true);
  649. }
  650. }