PrefabCreateTests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <AzToolsFramework/Entity/EditorEntityHelpers.h>
  9. #include <Prefab/PrefabTestFixture.h>
  10. namespace UnitTest
  11. {
  12. using PrefabCreateTests = PrefabTestFixture;
  13. TEST_F(PrefabCreateTests, CreatePrefabFromEntitiesAndValidateChildEntityOrder)
  14. {
  15. // Level
  16. // | Car
  17. // | Engine (entity)
  18. // | Wheel (instance)
  19. // | Tire
  20. // | Battery (entity)
  21. const AZStd::string carPrefabName = "CarPrefab";
  22. const AZStd::string wheelPrefabName = "WheelPrefab";
  23. const AZStd::string tireEntityName = "Tire";
  24. const AZStd::string engineEntityName = "Engine";
  25. const AZStd::string batteryEntityName = "Battery";
  26. AZ::IO::Path engineRootPath;
  27. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  28. AZ::IO::Path carPrefabFilepath = engineRootPath / carPrefabName;
  29. AZ::IO::Path wheelPrefabFilepath = engineRootPath / wheelPrefabName;
  30. // Create an Engine entity first
  31. AZ::EntityId engineEntityId = CreateEditorEntityUnderRoot(engineEntityName);
  32. // Create the Wheel instance after the Engine entity
  33. AZ::EntityId tireEntityId = CreateEditorEntityUnderRoot(tireEntityName);
  34. AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
  35. // Create the Battery entity after the Wheel instance
  36. AZ::EntityId batteryEntityId = CreateEditorEntityUnderRoot(batteryEntityName);
  37. // Create a Car prefab from the entities and isntance
  38. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { engineEntityId, batteryEntityId, wheelContainerId });
  39. auto carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  40. ASSERT_TRUE(carInstance.has_value());
  41. ASSERT_EQ(carInstance->get().GetContainerEntityId(), carContainerId);
  42. // Validate the child entity order of the car instance
  43. AzToolsFramework::EntityOrderArray entityOrderArray = AzToolsFramework::GetEntityChildOrder(carContainerId);
  44. EXPECT_EQ(entityOrderArray.size(), 3);
  45. AZStd::string entityName;
  46. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, entityOrderArray[0]);
  47. EXPECT_EQ(entityName, engineEntityName);
  48. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, entityOrderArray[1]);
  49. EXPECT_EQ(entityName, wheelPrefabName);
  50. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, entityOrderArray[2]);
  51. EXPECT_EQ(entityName, batteryEntityName);
  52. }
  53. } // namespace UnitTest