SliceStabilityTestFramework.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #pragma once
  9. #include <AzCore/std/functional.h>
  10. #include <AzCore/UnitTest/Mocks/MockFileIOBase.h>
  11. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  12. #include <AzToolsFramework/Entity/SliceEditorEntityOwnershipServiceBus.h>
  13. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  14. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  15. namespace UnitTest
  16. {
  17. class SliceStabilityTest
  18. : public ToolsApplicationFixture<>,
  19. public AzToolsFramework::AssetSystemRequestBus::Handler,
  20. public AzToolsFramework::EditorRequestBus::Handler,
  21. public AzToolsFramework::SliceEditorEntityOwnershipServiceNotificationBus::Handler
  22. {
  23. public:
  24. //! Creates an entity within the EditorEntityContext and supplies it required components
  25. //! @param entityName The name the created entity will use
  26. //! @param entityList The created entity will be placed at the back of the provided entityList
  27. //! @param parentId The EntityId to be assigned as the parent of the created entity.
  28. //! Defaults to an invalid id
  29. //! @return The EntityId of the created Entity, or an invalid id if operation failed
  30. AZ::EntityId CreateEditorEntity(const char* entityName, AzToolsFramework::EntityIdList& entityList, const AZ::EntityId& parentId = AZ::EntityId());
  31. //! Creates a new slice asset out of the provided entityList and generates the first slice instance using the provided entityList
  32. //! @param sliceAssetName Name of the newly created slice Asset
  33. //! @param entityList Created slice will be comprised of the entity hierarchy found in entityList
  34. //! entities need to exist within the EditorEntityContext and those same entities will be promoted into the first slice instance of the created slice
  35. //! @param sliceAddress The SliceInstanceAddress of the first slice instance generated out of the entities found in entityList
  36. //! @return The AssetId of the newly created slice, or an invalid id if operation failed
  37. AZ::Data::AssetId CreateSlice(AZStd::string sliceAssetName, AzToolsFramework::EntityIdList entityList, AZ::SliceComponent::SliceInstanceAddress& sliceAddress);
  38. //! Pushes a set of entities to an existing slice asset generated via CreateSlice
  39. //! @param sliceInstanceAddress An instance of the slice being pushed to
  40. //! Note: The act of pushing to a slice destroys and remakes all existing instances sliceInstanceAddress will be updated to represent the remade slice instance.
  41. //! Any copies of sliceInstanceAddress from before this operation are invalid.
  42. //! @param entitiesToPush A list of entities to push to the slice.
  43. //! If an entity in the list is already in the slice instance then it will be pushed as an updated entity
  44. //! If an entity in the list is not in the slice instance then it will be pushed as an added entity
  45. //! @return true if the push succeeded, or false if the operation failed
  46. bool PushEntitiesToSlice(AZ::SliceComponent::SliceInstanceAddress& sliceInstanceAddress, const AzToolsFramework::EntityIdList& entitiesToPush);
  47. //! Instantiates a slice into the EditorEntityContext using an existing slice Asset created via CreateSlice
  48. //! @param sliceAssetId The asset id of the slice being instantiated
  49. //! @param entityList All newly instantiated entities will be added to the back of entityList
  50. //! @param parent The EntityId to be assigned as the parent of the slice instance entities.
  51. //! Defaults to an invalid id
  52. //! @return The SliceInstanceAddress of the new slice instance, or an invalid address if operation failed
  53. AZ::SliceComponent::SliceInstanceAddress InstantiateEditorSlice(AZ::Data::AssetId sliceAssetId, AzToolsFramework::EntityIdList& entityList, const AZ::EntityId& parent = AZ::EntityId());
  54. //! Performs a reparent of entity to newParent. Handles any slice hierarchy manipulation needed
  55. //! @param entity The entity being reparented
  56. //! @param newParent The new parent in the reparent operation
  57. void ReparentEntity(AZ::EntityId& entity, const AZ::EntityId& newParent);
  58. //! Helper that searches for an entityId within a list of entities
  59. //! @param entityId The EntityId being searched for
  60. //! @param entityList the list to search in
  61. //! @return The Entity* whose EntityId matches entityId and is found in entityList.
  62. //! Returns nullptr if not found
  63. static AZ::Entity* FindEntityInList(const AZ::EntityId& entityId, const AZ::SliceComponent::EntityList& entityList);
  64. //! Helper that searches for an entity within the EditorEntityContext
  65. //! @param entityId The entityId being searched for
  66. //! @return The Entity* whose id matches entityId and is found in the EditorEntityContext
  67. //! Returns nullptr if not found
  68. static AZ::Entity* FindEntityInEditor(const AZ::EntityId& entityId);
  69. class SliceOperationValidator
  70. {
  71. public:
  72. SliceOperationValidator();
  73. ~SliceOperationValidator();
  74. void SetSerializeContext(AZ::SerializeContext* serializeContext);
  75. //! Clones the provided entities out of the EditorEntityContext and caches them for Compare operations
  76. //! @param entitiesToCapture List of EntityIDs that is used to search the EditorEntityContext and clone the respective Entity* into a cache for Compare operations
  77. //! @return Returns whether the capture was successful
  78. //! Can fail if there is already a cached capture or the entities could not be found in the EditorEntityContext
  79. bool Capture(const AzToolsFramework::EntityIdList& entitiesToCapture);
  80. //! Does a DataPatch compare of the reflected fields of a captured EntityList and the Instantiated entities found in instanceToCompare
  81. //! @param instanceToCompare A SliceInstanceAddress whose InstantiatedContainer will be diffed against a previously captured EntityList using DataPatch
  82. bool Compare(const AZ::SliceComponent::SliceInstanceAddress& instanceToCompare);
  83. //! Resets the current capture so a new one can be made
  84. void Reset();
  85. private:
  86. bool SortCapture(const AzToolsFramework::EntityList& orderToMatch);
  87. AZ::SerializeContext* m_serializeContext;
  88. AZ::SliceComponent::EntityList m_entityStateCapture;
  89. };
  90. class EntityReferenceComponent
  91. : public AzToolsFramework::Components::EditorComponentBase
  92. {
  93. public:
  94. AZ_EDITOR_COMPONENT(EntityReferenceComponent, "{3628F6A3-DFAD-4C1E-B9DE-EFBB1B6915C3}");
  95. void Init() override {}
  96. void Activate() override {}
  97. void Deactivate() override {}
  98. static void Reflect(AZ::ReflectContext* reflection);
  99. AZ::EntityId m_entityReference;
  100. };
  101. SliceOperationValidator m_validator;
  102. private:
  103. void SetUpEditorFixtureImpl() override;
  104. void TearDownEditorFixtureImpl() override;
  105. /*
  106. * SliceEditorEntityOwnershipServiceNotificationBus
  107. */
  108. void OnSliceInstantiated(const AZ::Data::AssetId& sliceAssetId, AZ::SliceComponent::SliceInstanceAddress& sliceAddress, const AzFramework::SliceInstantiationTicket& ticket) override;
  109. void OnSliceInstantiationFailed(const AZ::Data::AssetId& sliceAssetId, const AzFramework::SliceInstantiationTicket& ticket) override;
  110. /*
  111. * EditorRequestBus
  112. */
  113. void CreateEditorRepresentation(AZ::Entity* entity) override;
  114. void BrowseForAssets(AzToolsFramework::AssetBrowser::AssetSelectionModel& selection) override { AZ_UNUSED(selection); }
  115. /*
  116. * AssetSystemRequestBus
  117. */
  118. bool GetRelativeProductPathFromFullSourceOrProductPath([[maybe_unused]] const AZStd::string& fullPath, [[maybe_unused]] AZStd::string& relativeProductPath) override { return false; }
  119. bool GenerateRelativeSourcePath(
  120. [[maybe_unused]] const AZStd::string& sourcePath, [[maybe_unused]] AZStd::string& relativePath,
  121. [[maybe_unused]] AZStd::string& watchFolder) override { return false; }
  122. bool GetFullSourcePathFromRelativeProductPath([[maybe_unused]] const AZStd::string& relPath, [[maybe_unused]] AZStd::string& fullSourcePath) override { return false; }
  123. bool GetAssetInfoById([[maybe_unused]] const AZ::Data::AssetId& assetId, [[maybe_unused]] const AZ::Data::AssetType& assetType, [[maybe_unused]] const AZStd::string& platformName, [[maybe_unused]] AZ::Data::AssetInfo& assetInfo, [[maybe_unused]] AZStd::string& rootFilePath) override { return false; }
  124. bool GetSourceInfoBySourcePath(const char* sourcePath, AZ::Data::AssetInfo& assetInfo, AZStd::string& watchFolder) override;
  125. bool GetSourceInfoBySourceUUID([[maybe_unused]] const AZ::Uuid& sourceUuid, [[maybe_unused]] AZ::Data::AssetInfo& assetInfo, [[maybe_unused]] AZStd::string& watchFolder) override { return false; }
  126. bool GetScanFolders([[maybe_unused]] AZStd::vector<AZStd::string>& scanFolders) override { return false; }
  127. bool GetAssetSafeFolders([[maybe_unused]] AZStd::vector<AZStd::string>& assetSafeFolders) override { return false; }
  128. bool IsAssetPlatformEnabled([[maybe_unused]] const char* platform) override { return false; }
  129. int GetPendingAssetsForPlatform([[maybe_unused]] const char* platform) override { return -1; }
  130. bool GetAssetsProducedBySourceUUID([[maybe_unused]] const AZ::Uuid& sourceUuid, [[maybe_unused]] AZStd::vector<AZ::Data::AssetInfo>& productsAssetInfo) override { return false; }
  131. bool ClearFingerprintForAsset([[maybe_unused]] const AZStd::string& sourcePath) override { return false; }
  132. AZStd::unique_ptr<testing::NiceMock<AZ::IO::MockFileIOBase>> m_fileIOMock;
  133. AZ::IO::FileIOBase* m_priorFileIO = nullptr;
  134. AZStd::unordered_map<AZ::Data::AssetId, AZStd::vector<AZ::SliceComponent::SliceInstanceAddress>> m_createdSlices;
  135. AZ::Data::AssetId m_newSliceId;
  136. AzFramework::SliceInstantiationTicket m_ticket;
  137. static constexpr const char* m_relativeSourceAssetRoot = "Test/";
  138. };
  139. }