EditorTransformComponentTests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/UnitTest.h>
  10. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  11. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  12. namespace AzToolsFramework
  13. {
  14. struct TransformTestEntityHierarchy
  15. {
  16. AZ::EntityId m_parentId;
  17. AZ::EntityId m_childId;
  18. AZ::EntityId m_grandchild1Id;
  19. AZ::EntityId m_grandchild2Id;
  20. };
  21. class EditorTransformComponentTest
  22. : public UnitTest::ToolsApplicationFixture<>
  23. {
  24. public:
  25. static TransformTestEntityHierarchy BuildTestHierarchy()
  26. {
  27. TransformTestEntityHierarchy result;
  28. result.m_parentId = UnitTest::CreateDefaultEditorEntity("Parent");
  29. result.m_childId = UnitTest::CreateDefaultEditorEntity("Child");
  30. result.m_grandchild1Id = UnitTest::CreateDefaultEditorEntity("Grandchild1");
  31. result.m_grandchild2Id = UnitTest::CreateDefaultEditorEntity("Grandchild2");
  32. // Set parent-child relationships
  33. AZ::TransformBus::Event(result.m_childId, &AZ::TransformBus::Events::SetParent, result.m_parentId);
  34. AZ::TransformBus::Event(result.m_grandchild1Id, &AZ::TransformBus::Events::SetParent, result.m_childId);
  35. AZ::TransformBus::Event(result.m_grandchild2Id, &AZ::TransformBus::Events::SetParent, result.m_childId);
  36. return result;
  37. }
  38. };
  39. TEST_F(EditorTransformComponentTest, TransformTests_EntityHasParent_WorldScaleInheritsParentScale)
  40. {
  41. TransformTestEntityHierarchy hierarchy = BuildTestHierarchy();
  42. // Set scale to parent entity
  43. const float parentScale = 2.0f;
  44. AZ::TransformBus::Event(hierarchy.m_parentId, &AZ::TransformInterface::SetLocalUniformScale, parentScale);
  45. // Set scale to child entity
  46. const float childScale = 5.0f;
  47. AZ::TransformBus::Event(hierarchy.m_childId, &AZ::TransformInterface::SetLocalUniformScale, childScale);
  48. const float expectedScale = childScale * parentScale;
  49. float childWorldScale = 1.0f;
  50. AZ::TransformBus::EventResult(childWorldScale, hierarchy.m_childId, &AZ::TransformBus::Events::GetWorldUniformScale);
  51. EXPECT_NEAR(childWorldScale, expectedScale, AZ::Constants::Tolerance);
  52. }
  53. TEST_F(EditorTransformComponentTest, TransformTests_GetChildren_DirectChildrenMatchHierarchy)
  54. {
  55. TransformTestEntityHierarchy hierarchy = BuildTestHierarchy();
  56. EntityIdList children;
  57. AZ::TransformBus::EventResult(children, hierarchy.m_parentId, &AZ::TransformBus::Events::GetChildren);
  58. EXPECT_EQ(children.size(), 1);
  59. EXPECT_EQ(children[0], hierarchy.m_childId);
  60. }
  61. TEST_F(EditorTransformComponentTest, TransformTests_GetAllDescendants_AllDescendantsMatchHierarchy)
  62. {
  63. TransformTestEntityHierarchy hierarchy = BuildTestHierarchy();
  64. EntityIdList descendants;
  65. AZ::TransformBus::EventResult(descendants, hierarchy.m_parentId, &AZ::TransformBus::Events::GetAllDescendants);
  66. // Order of descendants here and in other test cases depends on TransformHierarchyInformationBus
  67. // Sorting it to get predictable order and be able to verify by index
  68. std::sort(descendants.begin(), descendants.end());
  69. EXPECT_EQ(descendants.size(), 3);
  70. EXPECT_EQ(descendants[0], hierarchy.m_childId);
  71. EXPECT_EQ(descendants[1], hierarchy.m_grandchild1Id);
  72. EXPECT_EQ(descendants[2], hierarchy.m_grandchild2Id);
  73. }
  74. TEST_F(EditorTransformComponentTest, TransformTests_GetEntityAndAllDescendants_AllDescendantsMatchHierarchyAndResultIncludesParentEntity)
  75. {
  76. TransformTestEntityHierarchy hierarchy = BuildTestHierarchy();
  77. EntityIdList entityAndDescendants;
  78. AZ::TransformBus::EventResult(entityAndDescendants, hierarchy.m_parentId, &AZ::TransformBus::Events::GetEntityAndAllDescendants);
  79. std::sort(entityAndDescendants.begin(), entityAndDescendants.end());
  80. EXPECT_EQ(entityAndDescendants.size(), 4);
  81. EXPECT_EQ(entityAndDescendants[0], hierarchy.m_parentId);
  82. EXPECT_EQ(entityAndDescendants[1], hierarchy.m_childId);
  83. EXPECT_EQ(entityAndDescendants[2], hierarchy.m_grandchild1Id);
  84. EXPECT_EQ(entityAndDescendants[3], hierarchy.m_grandchild2Id);
  85. }
  86. } // namespace AzToolsFramework