3
0

test_UiMarkupButtonComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #if defined(LYSHINE_INTERNAL_UNIT_TEST)
  9. namespace
  10. {
  11. void FindClickableTextRectIndexFromCanvasSpacePointTests()
  12. {
  13. // Empty case (no clickable rects)
  14. {
  15. AZ::Vector2 mousePos = AZ::Vector2::CreateZero();
  16. UiClickableTextInterface::ClickableTextRects clickableTextRects;
  17. int index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  18. AZ_Assert(index < 0, "Test failed");
  19. }
  20. // Zero-sized rect, mouse pos exactly on "rect"
  21. {
  22. UiClickableTextInterface::ClickableTextRects clickableTextRects;
  23. UiClickableTextInterface::ClickableTextRect textRect;
  24. textRect.rect.top = textRect.rect.left = textRect.rect.bottom = textRect.rect.right = 0.0f;
  25. clickableTextRects.push_back(textRect);
  26. AZ::Vector2 mousePos = AZ::Vector2::CreateZero();
  27. int index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  28. AZ_Assert(index == 0, "Test failed");
  29. }
  30. // Mouse pos: single rect
  31. {
  32. UiClickableTextInterface::ClickableTextRects clickableTextRects;
  33. UiClickableTextInterface::ClickableTextRect textRect;
  34. textRect.rect.top = textRect.rect.left = 1.0f;
  35. textRect.rect.bottom = textRect.rect.right = 100.0f;
  36. clickableTextRects.push_back(textRect);
  37. AZ::Vector2 mousePos = AZ::Vector2(2.0f, 2.0f);
  38. int index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  39. AZ_Assert(index == 0, "Test failed");
  40. mousePos = AZ::Vector2(1.0f, 1.0f);
  41. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  42. AZ_Assert(index == 0, "Test failed");
  43. mousePos = AZ::Vector2(100.0f, 100.0f);
  44. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  45. AZ_Assert(index == 0, "Test failed");
  46. mousePos = AZ::Vector2::CreateZero();
  47. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  48. AZ_Assert(index < 0, "Test failed");
  49. mousePos = AZ::Vector2(2.0f, 101.0f);
  50. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  51. AZ_Assert(index < 0, "Test failed");
  52. mousePos = AZ::Vector2(101.0f, 2.0f);
  53. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  54. AZ_Assert(index < 0, "Test failed");
  55. mousePos = AZ::Vector2(101.0f, 101.0f);
  56. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  57. AZ_Assert(index < 0, "Test failed");
  58. }
  59. // Mouse pos: multiple rects
  60. {
  61. UiClickableTextInterface::ClickableTextRects clickableTextRects;
  62. UiClickableTextInterface::ClickableTextRect textRect;
  63. textRect.rect.top = textRect.rect.left = 1.0f;
  64. textRect.rect.bottom = textRect.rect.right = 100.0f;
  65. clickableTextRects.push_back(textRect);
  66. textRect.rect.top = textRect.rect.left = 101.0f;
  67. textRect.rect.bottom = textRect.rect.right = 200.0f;
  68. clickableTextRects.push_back(textRect);
  69. AZ::Vector2 mousePos = AZ::Vector2(2.0f, 2.0f);
  70. int index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  71. AZ_Assert(index == 0, "Test failed");
  72. mousePos = AZ::Vector2(1.0f, 1.0f);
  73. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  74. AZ_Assert(index == 0, "Test failed");
  75. mousePos = AZ::Vector2(100.0f, 100.0f);
  76. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  77. AZ_Assert(index == 0, "Test failed");
  78. mousePos = AZ::Vector2(102.0f, 102.0f);
  79. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  80. AZ_Assert(index == 1, "Test failed");
  81. mousePos = AZ::Vector2(101.0f, 101.0f);
  82. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  83. AZ_Assert(index == 1, "Test failed");
  84. mousePos = AZ::Vector2(200.0f, 200.0f);
  85. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  86. AZ_Assert(index == 1, "Test failed");
  87. mousePos = AZ::Vector2::CreateZero();
  88. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  89. AZ_Assert(index < 0, "Test failed");
  90. mousePos = AZ::Vector2(1.0f, 101.0f);
  91. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  92. AZ_Assert(index < 0, "Test failed");
  93. mousePos = AZ::Vector2(101.0f, 1.0f);
  94. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  95. AZ_Assert(index < 0, "Test failed");
  96. mousePos = AZ::Vector2(101.0f, 201.0f);
  97. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  98. AZ_Assert(index < 0, "Test failed");
  99. mousePos = AZ::Vector2(201.0f, 101.0f);
  100. index = FindClickableTextRectIndexFromCanvasSpacePoint(mousePos, clickableTextRects);
  101. AZ_Assert(index < 0, "Test failed");
  102. }
  103. }
  104. }
  105. void UiMarkupButtonComponent::UnitTest(CLyShine* /* lyshine */, IConsoleCmdArgs* /* cmdArgs */)
  106. {
  107. FindClickableTextRectIndexFromCanvasSpacePointTests();
  108. }
  109. #endif