WhiteBoxPhysicsTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "Components/EditorWhiteBoxColliderComponent.h"
  9. #include "Components/WhiteBoxColliderComponent.h"
  10. #include "WhiteBox/EditorWhiteBoxComponentBus.h"
  11. #include "WhiteBox/WhiteBoxToolApi.h"
  12. #include "WhiteBoxTestFixtures.h"
  13. #include "WhiteBoxTestUtil.h"
  14. #include <AzCore/Component/ComponentApplicationBus.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/Slice/SliceAssetHandler.h>
  17. #include <AzQtComponents/Utilities/QtPluginPaths.h>
  18. #include <AzTest/GemTestEnvironment.h>
  19. #include <AzToolsFramework/Entity/EditorEntityContextComponent.h>
  20. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  21. namespace UnitTest
  22. {
  23. class EditorWhiteBoxPhysicsTestEnvironment : public AZ::Test::GemTestEnvironment
  24. {
  25. // AZ::Test::GemTestEnvironment overrides ...
  26. void AddGemsAndComponents() override;
  27. AZ::ComponentApplication* CreateApplicationInstance() override;
  28. void PostSystemEntityActivate() override;
  29. public:
  30. EditorWhiteBoxPhysicsTestEnvironment() = default;
  31. ~EditorWhiteBoxPhysicsTestEnvironment() override = default;
  32. };
  33. void EditorWhiteBoxPhysicsTestEnvironment::AddGemsAndComponents()
  34. {
  35. AddDynamicModulePaths({"PhysX.Editor.Gem"});
  36. AddComponentDescriptors(
  37. {WhiteBox::EditorWhiteBoxComponent::CreateDescriptor(),
  38. WhiteBox::WhiteBoxComponent::CreateDescriptor(),
  39. WhiteBox::WhiteBoxColliderComponent::CreateDescriptor(),
  40. WhiteBox::EditorWhiteBoxColliderComponent::CreateDescriptor()});
  41. }
  42. AZ::ComponentApplication* EditorWhiteBoxPhysicsTestEnvironment::CreateApplicationInstance()
  43. {
  44. // Using ToolsTestApplication to have AzFramework and AzToolsFramework components.
  45. return aznew UnitTest::ToolsTestApplication("EditorWhiteBoxPhysics");
  46. }
  47. void EditorWhiteBoxPhysicsTestEnvironment::PostSystemEntityActivate()
  48. {
  49. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  50. }
  51. class WhiteBoxPhysicsFixture : public ::testing::Test
  52. {
  53. };
  54. TEST_F(WhiteBoxPhysicsFixture, EditorWhiteBoxColliderComponentCanBeAddedToAnEmptyWhiteBoxComponent)
  55. {
  56. // given
  57. // create an entity with a transform and editor white box component
  58. AZ::Entity entity;
  59. entity.CreateComponent<AzToolsFramework::Components::TransformComponent>();
  60. auto editorWhiteBoxComponent = entity.CreateComponent<WhiteBox::EditorWhiteBoxComponent>();
  61. entity.Init();
  62. entity.Activate();
  63. WhiteBox::WhiteBoxMesh* whiteBox = nullptr;
  64. WhiteBox::EditorWhiteBoxComponentRequestBus::EventResult(
  65. whiteBox, AZ::EntityComponentIdPair(entity.GetId(), editorWhiteBoxComponent->GetId()),
  66. &WhiteBox::EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
  67. // clear all data from the white box mesh
  68. WhiteBox::Api::Clear(*whiteBox);
  69. // error messages present in EditorWhiteBoxComponent prior to fix for empty WhiteBoxMesh
  70. UnitTest::ErrorHandler physxCookFailed("Failed to cook triangle mesh. Please check the data is correct");
  71. UnitTest::ErrorHandler colliderCookFailed("Failed to cook mesh data");
  72. UnitTest::ErrorHandler invalidShape("Trying to add an invalid shape");
  73. UnitTest::ErrorHandler invalidConfiguration("Unable to create a shape from configuration");
  74. UnitTest::ErrorHandler physxError("TriangleMesh::loadFromDesc: desc.isValid() failed!");
  75. // when
  76. // add an editor white box collider component
  77. entity.Deactivate();
  78. entity.CreateComponent<WhiteBox::EditorWhiteBoxColliderComponent>();
  79. entity.Activate();
  80. // then
  81. // ensure none of the previous error messages are reported
  82. EXPECT_EQ(physxCookFailed.GetWarningCount(), 0);
  83. EXPECT_EQ(colliderCookFailed.GetWarningCount(), 0);
  84. EXPECT_EQ(invalidShape.GetErrorCount(), 0);
  85. EXPECT_EQ(invalidConfiguration.GetErrorCount(), 0);
  86. EXPECT_EQ(physxError.GetErrorCount(), 0);
  87. }
  88. } // namespace UnitTest
  89. // required to support running integration tests with Qt and PhysX
  90. AZTEST_EXPORT int AZ_UNIT_TEST_HOOK_NAME(int argc, char** argv)
  91. {
  92. ::testing::InitGoogleMock(&argc, argv);
  93. AzQtComponents::PrepareQtPaths();
  94. QApplication app(argc, argv);
  95. AZ::Test::printUnusedParametersWarning(argc, argv);
  96. AZ::Test::addTestEnvironments({new UnitTest::EditorWhiteBoxPhysicsTestEnvironment()});
  97. int result = RUN_ALL_TESTS();
  98. return result;
  99. }
  100. IMPLEMENT_TEST_EXECUTABLE_MAIN();