PrefabInstantiateTests.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <Prefab/PrefabTestFixture.h>
  9. namespace UnitTest
  10. {
  11. using PrefabInstantiateTest = PrefabTestFixture;
  12. TEST_F(PrefabInstantiateTest, PrefabInstantiate_InstantiateInvalidTemplate_InstantiateFails)
  13. {
  14. AZ_TEST_START_TRACE_SUPPRESSION;
  15. EXPECT_FALSE(m_prefabSystemComponent->InstantiatePrefab(AzToolsFramework::Prefab::InvalidTemplateId));
  16. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  17. }
  18. TEST_F(PrefabInstantiateTest, PrefabInstantiate_NoNestingTemplate_InstantiateSucceeds)
  19. {
  20. AZ::Entity* newEntity = CreateEntity("New Entity");
  21. AddRequiredEditorComponents({ newEntity->GetId() });
  22. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> firstInstance = m_prefabSystemComponent->CreatePrefab({ newEntity }, {}, "test/path");
  23. ASSERT_TRUE(firstInstance);
  24. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> secondInstance = m_prefabSystemComponent->InstantiatePrefab(firstInstance->GetTemplateId());
  25. ASSERT_TRUE(secondInstance);
  26. CompareInstances(*firstInstance, *secondInstance, true, false);
  27. }
  28. TEST_F(PrefabInstantiateTest, PrefabInstantiate_TripleNestingTemplate_InstantiateSucceeds)
  29. {
  30. AZ::Entity* newEntity = CreateEntity("New Entity");
  31. AddRequiredEditorComponents({ newEntity->GetId() });
  32. // Build a 3 level deep nested Template
  33. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> firstInstance = m_prefabSystemComponent->CreatePrefab({ newEntity }, {}, "test/path1");
  34. ASSERT_TRUE(firstInstance);
  35. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> secondInstance = m_prefabSystemComponent->CreatePrefab({},
  36. MakeInstanceList(AZStd::move(firstInstance)), "test/path2");
  37. ASSERT_TRUE(secondInstance);
  38. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> thirdInstance = m_prefabSystemComponent->CreatePrefab({},
  39. MakeInstanceList(AZStd::move(secondInstance)), "test/path3");
  40. ASSERT_TRUE(thirdInstance);
  41. //Instantiate it
  42. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> fourthInstance =
  43. m_prefabSystemComponent->InstantiatePrefab(thirdInstance->GetTemplateId());
  44. ASSERT_TRUE(fourthInstance);
  45. CompareInstances(*thirdInstance, *fourthInstance, true, false);
  46. }
  47. TEST_F(PrefabInstantiateTest, PrefabInstantiate_Instantiate10Times_InstantiatesSucceed)
  48. {
  49. AZ::Entity* newEntity = CreateEntity("New Entity");
  50. AddRequiredEditorComponents({ newEntity->GetId() });
  51. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> firstInstance = m_prefabSystemComponent->CreatePrefab({ newEntity }, {}, "test/path");
  52. // Store the generated instances so that the unique_ptrs are destroyed at the end of the test
  53. // This allows us to have all the instances around at the same time
  54. AZStd::vector<AZStd::unique_ptr<AzToolsFramework::Prefab::Instance>> newInstances;
  55. for (int instanceCount = 0; instanceCount < 10; ++instanceCount)
  56. {
  57. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance>
  58. newInstance(m_prefabSystemComponent->InstantiatePrefab(firstInstance->GetTemplateId()));
  59. ASSERT_TRUE(newInstance);
  60. CompareInstances(*firstInstance, *newInstance, true, false);
  61. newInstances.push_back(AZStd::move(newInstance));
  62. }
  63. }
  64. }