EditorSphereShapeComponentTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <EditorShapeTestUtils.h>
  9. #include <LmbrCentralReflectionTest.h>
  10. #include <Shape/EditorSphereShapeComponent.h>
  11. namespace LmbrCentral
  12. {
  13. // Serialized legacy EditorSphereShapeComponent v1.
  14. const char kEditorSphereComponentVersion1[] =
  15. R"DELIMITER(<ObjectStream version="1">
  16. <Class name="EditorSphereShapeComponent" field="element" version="1" type="{2EA56CBF-63C8-41D9-84D5-0EC2BECE748E}">
  17. <Class name="EditorComponentBase" field="BaseClass1" version="1" type="{D5346BD4-7F20-444E-B370-327ACD03D4A0}">
  18. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  19. <Class name="AZ::u64" field="Id" value="11428802534905560348" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  20. </Class>
  21. </Class>
  22. <Class name="SphereShapeConfig" field="Configuration" version="1" type="{4AADFD75-48A7-4F31-8F30-FE4505F09E35}">
  23. <Class name="float" field="Radius" value="0.5700000" type="{EA2C3E90-AFBE-44D4-A90D-FAAF79BAF93D}"/>
  24. </Class>
  25. </Class>
  26. </ObjectStream>)DELIMITER";
  27. class LoadEditorSphereShapeComponentFromVersion1
  28. : public LoadEditorComponentTest<EditorSphereShapeComponent>
  29. {
  30. protected:
  31. const char* GetSourceDataBuffer() const override { return kEditorSphereComponentVersion1; }
  32. };
  33. TEST_F(LoadEditorSphereShapeComponentFromVersion1, Application_IsRunning)
  34. {
  35. ASSERT_NE(GetApplication(), nullptr);
  36. }
  37. TEST_F(LoadEditorSphereShapeComponentFromVersion1, Components_Load)
  38. {
  39. EXPECT_NE(m_object.get(), nullptr);
  40. }
  41. TEST_F(LoadEditorSphereShapeComponentFromVersion1, EditorComponent_Found)
  42. {
  43. EXPECT_EQ(m_entity->GetComponents().size(), 2);
  44. EXPECT_NE(m_entity->FindComponent(m_object->GetId()), nullptr);
  45. }
  46. TEST_F(LoadEditorSphereShapeComponentFromVersion1, Radius_MatchesSourceData)
  47. {
  48. float radius = 0.0f;
  49. SphereShapeComponentRequestsBus::EventResult(
  50. radius, m_entity->GetId(), &SphereShapeComponentRequests::GetRadius);
  51. EXPECT_FLOAT_EQ(radius, 0.57f);
  52. }
  53. class EditorSphereShapeComponentFixture
  54. : public UnitTest::ToolsApplicationFixture<>
  55. {
  56. public:
  57. void SetUpEditorFixtureImpl() override;
  58. void TearDownEditorFixtureImpl() override;
  59. AZStd::unique_ptr<AZ::ComponentDescriptor> m_editorCapsuleShapeComponentDescriptor;
  60. AZStd::unique_ptr<AZ::ComponentDescriptor> m_editorSphereShapeComponentDescriptor;
  61. AZ::Entity* m_entity = nullptr;
  62. AZ::EntityId m_entityId;
  63. AZ::EntityComponentIdPair m_entityComponentIdPair;
  64. };
  65. void EditorSphereShapeComponentFixture::SetUpEditorFixtureImpl()
  66. {
  67. AZ::SerializeContext* serializeContext = nullptr;
  68. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  69. m_editorSphereShapeComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(EditorSphereShapeComponent::CreateDescriptor());
  70. ShapeComponentConfig::Reflect(serializeContext);
  71. SphereShape::Reflect(serializeContext);
  72. m_editorSphereShapeComponentDescriptor->Reflect(serializeContext);
  73. UnitTest::CreateDefaultEditorEntity("CapsuleShapeComponentEntity", &m_entity);
  74. m_entityId = m_entity->GetId();
  75. m_entity->Deactivate();
  76. m_entityComponentIdPair =
  77. AZ::EntityComponentIdPair(m_entityId, m_entity->CreateComponent(EditorSphereShapeComponentTypeId)->GetId());
  78. m_entity->Activate();
  79. }
  80. void EditorSphereShapeComponentFixture::TearDownEditorFixtureImpl()
  81. {
  82. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
  83. &AzToolsFramework::EditorEntityContextRequestBus::Events::DestroyEditorEntity, m_entityId);
  84. m_entity = nullptr;
  85. m_entityId.SetInvalid();
  86. m_editorSphereShapeComponentDescriptor.reset();
  87. }
  88. using EditorSphereShapeComponentManipulatorFixture =
  89. UnitTest::IndirectCallManipulatorViewportInteractionFixtureMixin<EditorSphereShapeComponentFixture>;
  90. void SetUpSphereShapeComponent(
  91. AZ::EntityId entityId,
  92. const AZ::Transform& transform,
  93. const AZ::Vector3& translationOffset,
  94. float radius)
  95. {
  96. AZ::TransformBus::Event(entityId, &AZ::TransformBus::Events::SetWorldTM, transform);
  97. ShapeComponentRequestsBus::Event(entityId, &ShapeComponentRequests::SetTranslationOffset, translationOffset);
  98. SphereShapeComponentRequestsBus::Event(entityId, &SphereShapeComponentRequests::SetRadius, radius);
  99. }
  100. TEST_F(EditorSphereShapeComponentManipulatorFixture, SphereShapeRadiusManipulatorScalesCorrectly)
  101. {
  102. AZ::Transform sphereTransform(AZ::Vector3(6.0f, -3.0f, 2.0f), AZ::Quaternion::CreateIdentity(), 0.5f);
  103. const float radius = 3.0f;
  104. const AZ::Vector3 translationOffset(-3.0f, -5.0f, 2.0f);
  105. SetUpSphereShapeComponent(m_entity->GetId(), sphereTransform, translationOffset, radius);
  106. EnterComponentMode(m_entityId, EditorSphereShapeComponentTypeId);
  107. // position the camera so it is looking at the sphere
  108. AzFramework::SetCameraTransform(m_cameraState, AZ::Transform::CreateTranslation(AZ::Vector3(5.0f, -15.0f, 2.5f)));
  109. const AZ::Vector3 worldStart(6.0f, -5.5f, 3.0f);
  110. const AZ::Vector3 worldEnd(6.5f, -5.5f, 3.0f);
  111. DragMouse(m_cameraState, m_actionDispatcher.get(), worldStart, worldEnd);
  112. ExpectSphereRadius(m_entity->GetId(), 4.0f);
  113. }
  114. } // namespace LmbrCentral