MockInterfaces.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <AzCore/Component/Entity.h>
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Time/ITime.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  14. #include <AzFramework/Physics/PhysicsScene.h>
  15. #include <AzTest/AzTest.h>
  16. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  17. #include <RecastNavigation/RecastNavigationMeshBus.h>
  18. namespace RecastNavigationTests
  19. {
  20. class MockShapeComponent
  21. : public AZ::Component
  22. , public LmbrCentral::ShapeComponentRequestsBus::Handler
  23. {
  24. public:
  25. AZ_COMPONENT(MockShapeComponent,
  26. "{A9406916-365D-4C72-9F4C-2A3E5220CE2B}");
  27. static void Reflect(AZ::ReflectContext*) {}
  28. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  29. {
  30. provided.push_back(AZ_CRC_CE("AxisAlignedBoxShapeService"));
  31. }
  32. void Activate() override
  33. {
  34. LmbrCentral::ShapeComponentRequestsBus::Handler::BusConnect(GetEntityId());
  35. }
  36. void Deactivate() override
  37. {
  38. LmbrCentral::ShapeComponentRequestsBus::Handler::BusDisconnect();
  39. }
  40. AZ::Aabb GetEncompassingAabb() const override
  41. {
  42. return AZ::Aabb::CreateCenterHalfExtents(AZ::Vector3::CreateZero(), AZ::Vector3::CreateOne() * 10);
  43. }
  44. MOCK_CONST_METHOD0(GetShapeType, AZ::Crc32());
  45. MOCK_CONST_METHOD2(GetTransformAndLocalBounds, void(AZ::Transform&, AZ::Aabb&));
  46. MOCK_CONST_METHOD1(IsPointInside, bool(const AZ::Vector3&));
  47. MOCK_CONST_METHOD1(DistanceSquaredFromPoint, float(const AZ::Vector3&));
  48. };
  49. class MockDebug : public AzFramework::DebugDisplayRequestBus::Handler
  50. {
  51. public:
  52. MockDebug()
  53. {
  54. AzFramework::DebugDisplayRequestBus::Handler::BusConnect(AzFramework::g_defaultSceneEntityDebugDisplayId);
  55. }
  56. ~MockDebug() override
  57. {
  58. AzFramework::DebugDisplayRequestBus::Handler::BusDisconnect();
  59. }
  60. };
  61. struct Wait : public RecastNavigation::RecastNavigationMeshNotificationBus::Handler
  62. {
  63. explicit Wait(AZ::EntityId id)
  64. {
  65. RecastNavigation::RecastNavigationMeshNotificationBus::Handler::BusConnect(id);
  66. }
  67. ~Wait() override
  68. {
  69. RecastNavigation::RecastNavigationMeshNotificationBus::Handler::BusDisconnect();
  70. }
  71. void OnNavigationMeshUpdated(AZ::EntityId) override
  72. {
  73. m_updatedCalls++;
  74. }
  75. int m_updatedCalls = 0;
  76. void OnNavigationMeshBeganRecalculating(AZ::EntityId) override
  77. {
  78. m_recalculatingCalls++;
  79. }
  80. int m_recalculatingCalls = 0;
  81. void Reset()
  82. {
  83. m_updatedCalls = 0;
  84. m_recalculatingCalls = 0;
  85. }
  86. void BlockUntilNavigationMeshRecalculating(AZ::TimeMs timeout = AZ::TimeMs{ 2000 }) const
  87. {
  88. const AZ::TimeMs timeStep{ 5 };
  89. AZ::TimeMs current{ 0 };
  90. while (current < timeout && m_recalculatingCalls == 0)
  91. {
  92. // Nav mesh notifications occurs on main threads via ticks.
  93. AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.1f, AZ::ScriptTimePoint{});
  94. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(static_cast<int>(timeStep)));
  95. current += timeStep;
  96. }
  97. }
  98. void BlockUntilCalled(AZ::TimeMs timeout = AZ::TimeMs{ 2000 }) const
  99. {
  100. const AZ::TimeMs timeStep{ 5 };
  101. AZ::TimeMs current{ 0 };
  102. while (current < timeout && m_updatedCalls == 0)
  103. {
  104. // Nav mesh notifications occurs on main threads via ticks.
  105. AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.1f, AZ::ScriptTimePoint{});
  106. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(static_cast<int>(timeStep)));
  107. current += timeStep;
  108. }
  109. }
  110. };
  111. struct MockTransforms : AZ::TransformBus::MultiHandler
  112. {
  113. explicit MockTransforms(const AZStd::vector<AZ::EntityId>& entities)
  114. {
  115. for (AZ::EntityId id : entities)
  116. {
  117. AZ::TransformBus::MultiHandler::BusConnect(id);
  118. }
  119. }
  120. ~MockTransforms() override
  121. {
  122. AZ::TransformBus::MultiHandler::BusDisconnect();
  123. }
  124. void BindTransformChangedEventHandler(AZ::TransformChangedEvent::Handler&) override {}
  125. void BindParentChangedEventHandler(AZ::ParentChangedEvent::Handler&) override {}
  126. void BindChildChangedEventHandler(AZ::ChildChangedEvent::Handler&) override {}
  127. void NotifyChildChangedEvent(AZ::ChildChangeType, AZ::EntityId) override {}
  128. MOCK_METHOD0(GetLocalTM, const AZ::Transform& ());
  129. MOCK_METHOD0(GetWorldTM, const AZ::Transform& ());
  130. MOCK_METHOD0(IsStaticTransform, bool());
  131. };
  132. }