No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NavigationTests.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Reflection;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.TestTools;
  6. namespace UnityEngine.UI.Tests
  7. {
  8. [TestFixture]
  9. class NavigationTests
  10. {
  11. GameObject canvasRoot;
  12. Selectable topLeftSelectable;
  13. Selectable bottomLeftSelectable;
  14. Selectable topRightSelectable;
  15. Selectable bottomRightSelectable;
  16. [SetUp]
  17. public void TestSetup()
  18. {
  19. canvasRoot = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
  20. GameObject topLeftGO = new GameObject("topLeftGO", typeof(RectTransform), typeof(CanvasRenderer), typeof(Selectable));
  21. topLeftGO.transform.SetParent(canvasRoot.transform);
  22. (topLeftGO.transform as RectTransform).anchoredPosition = new Vector2(50, 200);
  23. topLeftSelectable = topLeftGO.GetComponent<Selectable>();
  24. GameObject bottomLeftGO = new GameObject("bottomLeftGO", typeof(RectTransform), typeof(CanvasRenderer), typeof(Selectable));
  25. bottomLeftGO.transform.SetParent(canvasRoot.transform);
  26. (bottomLeftGO.transform as RectTransform).anchoredPosition = new Vector2(50, 50);
  27. bottomLeftSelectable = bottomLeftGO.GetComponent<Selectable>();
  28. GameObject topRightGO = new GameObject("topRightGO", typeof(RectTransform), typeof(CanvasRenderer), typeof(Selectable));
  29. topRightGO.transform.SetParent(canvasRoot.transform);
  30. (topRightGO.transform as RectTransform).anchoredPosition = new Vector2(200, 200);
  31. topRightSelectable = topRightGO.GetComponent<Selectable>();
  32. GameObject bottomRightGO = new GameObject("bottomRightGO", typeof(RectTransform), typeof(CanvasRenderer), typeof(Selectable));
  33. bottomRightGO.transform.SetParent(canvasRoot.transform);
  34. (bottomRightGO.transform as RectTransform).anchoredPosition = new Vector2(200, 50);
  35. bottomRightSelectable = bottomRightGO.GetComponent<Selectable>();
  36. }
  37. [TearDown]
  38. public void TearDown()
  39. {
  40. GameObject.DestroyImmediate(canvasRoot);
  41. }
  42. [Test]
  43. public void FindSelectableOnRight_ReturnsNextSelectableRightOfTarget()
  44. {
  45. Selectable selectableRightOfTopLeft = topLeftSelectable.FindSelectableOnRight();
  46. Selectable selectableRightOfBottomLeft = bottomLeftSelectable.FindSelectableOnRight();
  47. Assert.AreEqual(topRightSelectable, selectableRightOfTopLeft, "Wrong selectable to right of Top Left Selectable");
  48. Assert.AreEqual(bottomRightSelectable, selectableRightOfBottomLeft, "Wrong selectable to right of Bottom Left Selectable");
  49. }
  50. [Test]
  51. public void FindSelectableOnLeft_ReturnsNextSelectableLeftOfTarget()
  52. {
  53. Selectable selectableLeftOfTopRight = topRightSelectable.FindSelectableOnLeft();
  54. Selectable selectableLeftOfBottomRight = bottomRightSelectable.FindSelectableOnLeft();
  55. Assert.AreEqual(topLeftSelectable, selectableLeftOfTopRight, "Wrong selectable to left of Top Right Selectable");
  56. Assert.AreEqual(bottomLeftSelectable, selectableLeftOfBottomRight, "Wrong selectable to left of Bottom Right Selectable");
  57. }
  58. [Test]
  59. public void FindSelectableOnRDown_ReturnsNextSelectableBelowTarget()
  60. {
  61. Selectable selectableDownOfTopLeft = topLeftSelectable.FindSelectableOnDown();
  62. Selectable selectableDownOfTopRight = topRightSelectable.FindSelectableOnDown();
  63. Assert.AreEqual(bottomLeftSelectable, selectableDownOfTopLeft, "Wrong selectable to Bottom of Top Left Selectable");
  64. Assert.AreEqual(bottomRightSelectable, selectableDownOfTopRight, "Wrong selectable to Bottom of top Right Selectable");
  65. }
  66. [Test]
  67. public void FindSelectableOnUp_ReturnsNextSelectableAboveTarget()
  68. {
  69. Selectable selectableUpOfBottomLeft = bottomLeftSelectable.FindSelectableOnUp();
  70. Selectable selectableUpOfBottomRight = bottomRightSelectable.FindSelectableOnUp();
  71. Assert.AreEqual(topLeftSelectable, selectableUpOfBottomLeft, "Wrong selectable to Up of bottom Left Selectable");
  72. Assert.AreEqual(topRightSelectable, selectableUpOfBottomRight, "Wrong selectable to Up of bottom Right Selectable");
  73. }
  74. [Test]
  75. public void FindSelectableOnRight__WrappingEnabled_ReturnsFurthestSelectableOnLeft()
  76. {
  77. Navigation nav = topRightSelectable.navigation;
  78. nav.wrapAround = true;
  79. nav.mode = Navigation.Mode.Horizontal;
  80. topRightSelectable.navigation = nav;
  81. nav = bottomRightSelectable.navigation;
  82. nav.wrapAround = true;
  83. nav.mode = Navigation.Mode.Horizontal;
  84. bottomRightSelectable.navigation = nav;
  85. Selectable selectableRightOfTopRight = topRightSelectable.FindSelectableOnRight();
  86. Selectable selectableRightOfBottomRight = bottomRightSelectable.FindSelectableOnRight();
  87. Assert.AreEqual(bottomLeftSelectable, selectableRightOfTopRight, "Wrong selectable to right of Top Right Selectable");
  88. Assert.AreEqual(topLeftSelectable, selectableRightOfBottomRight, "Wrong selectable to right of Bottom Right Selectable");
  89. }
  90. [Test]
  91. public void FindSelectableOnLeft_WrappingEnabled_ReturnsFurthestSelectableOnRight()
  92. {
  93. Navigation nav = topLeftSelectable.navigation;
  94. nav.wrapAround = true;
  95. nav.mode = Navigation.Mode.Horizontal;
  96. topLeftSelectable.navigation = nav;
  97. nav = bottomLeftSelectable.navigation;
  98. nav.wrapAround = true;
  99. nav.mode = Navigation.Mode.Horizontal;
  100. bottomLeftSelectable.navigation = nav;
  101. Selectable selectableLeftOfTopLeft = topLeftSelectable.FindSelectableOnLeft();
  102. Selectable selectableLeftOfBottomLeft = bottomLeftSelectable.FindSelectableOnLeft();
  103. Assert.AreEqual(bottomRightSelectable, selectableLeftOfTopLeft, "Wrong selectable to left of Top Left Selectable");
  104. Assert.AreEqual(topRightSelectable, selectableLeftOfBottomLeft, "Wrong selectable to left of Bottom Left Selectable");
  105. }
  106. [Test]
  107. public void FindSelectableOnDown_WrappingEnabled_ReturnsFurthestSelectableAbove()
  108. {
  109. Navigation nav = bottomLeftSelectable.navigation;
  110. nav.wrapAround = true;
  111. nav.mode = Navigation.Mode.Vertical;
  112. bottomLeftSelectable.navigation = nav;
  113. nav = bottomRightSelectable.navigation;
  114. nav.wrapAround = true;
  115. nav.mode = Navigation.Mode.Vertical;
  116. bottomRightSelectable.navigation = nav;
  117. Selectable selectableDownOfBottomLeft = bottomLeftSelectable.FindSelectableOnDown();
  118. Selectable selectableDownOfBottomRight = bottomRightSelectable.FindSelectableOnDown();
  119. Assert.AreEqual(topRightSelectable, selectableDownOfBottomLeft, "Wrong selectable to Bottom of Bottom Left Selectable");
  120. Assert.AreEqual(topLeftSelectable, selectableDownOfBottomRight, "Wrong selectable to Bottom of Bottom Right Selectable");
  121. }
  122. [Test]
  123. public void FindSelectableOnUp_WrappingEnabled_ReturnsFurthestSelectableBelow()
  124. {
  125. Navigation nav = topLeftSelectable.navigation;
  126. nav.wrapAround = true;
  127. nav.mode = Navigation.Mode.Vertical;
  128. topLeftSelectable.navigation = nav;
  129. nav = topRightSelectable.navigation;
  130. nav.wrapAround = true;
  131. nav.mode = Navigation.Mode.Vertical;
  132. topRightSelectable.navigation = nav;
  133. Selectable selectableUpOfTopLeft = topLeftSelectable.FindSelectableOnUp();
  134. Selectable selectableUpOfTopRight = topRightSelectable.FindSelectableOnUp();
  135. Assert.AreEqual(bottomRightSelectable, selectableUpOfTopLeft, "Wrong selectable to Up of Top Left Selectable");
  136. Assert.AreEqual(bottomLeftSelectable, selectableUpOfTopRight, "Wrong selectable to Up of Top Right Selectable");
  137. }
  138. }
  139. }