3
0

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