PrefabAssetPathChangeTestFixture.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/PrefabAssetPathChangeTestFixture.h>
  9. namespace UnitTest
  10. {
  11. void PrefabAssetPathChangeTestFixture::SetUpEditorFixtureImpl()
  12. {
  13. PrefabTestFixture::SetUpEditorFixtureImpl();
  14. m_settingsRegistryInterface->Get(m_projectPath, AZ::SettingsRegistryMergeUtils::FilePathKey_ProjectPath);
  15. }
  16. InstanceOptionalReference PrefabAssetPathChangeTestFixture::CreatePrefabInstance(
  17. AZStd::string_view folderPath,
  18. AZStd::string_view fileName)
  19. {
  20. const AZStd::string entityName = "Entity1";
  21. // Create and instantiate a prefab
  22. AZ::IO::Path prefabFilepath = GetAbsoluteFilePathName(folderPath, fileName);
  23. AZ::EntityId editorEntityId = CreateEditorEntityUnderRoot(entityName);
  24. AZ::EntityId containerId = CreateEditorPrefab(prefabFilepath, { editorEntityId });
  25. InstanceOptionalReference prefabInstance = m_instanceEntityMapperInterface->FindOwningInstance(containerId);
  26. return prefabInstance;
  27. }
  28. void PrefabAssetPathChangeTestFixture::ChangePrefabFileName(
  29. AZStd::string_view folderPath,
  30. AZStd::string_view fromFileName,
  31. AZStd::string_view toFileName)
  32. {
  33. AZ::IO::Path fromAbsoluteFilePath = GetAbsoluteFilePathName(folderPath, fromFileName);
  34. AZ::IO::Path toAbsoluteFilePath = GetAbsoluteFilePathName(folderPath, toFileName);
  35. SendFilePathNameChangeEvent(fromAbsoluteFilePath.c_str(), toAbsoluteFilePath.c_str());
  36. PropagateAllTemplateChanges();
  37. }
  38. void PrefabAssetPathChangeTestFixture::ChangePrefabFolderPath(AZStd::string_view fromFolderPath, AZStd::string_view toFolderPath)
  39. {
  40. AZ::IO::Path fromAbsoluteFolderPath = GetAbsoluteFilePathName(fromFolderPath, "");
  41. AZ::IO::Path toAbsoluteFolderPath = GetAbsoluteFilePathName(toFolderPath, "");
  42. SendFolderPathNameChangeEvent(fromAbsoluteFolderPath.c_str(), toAbsoluteFolderPath.c_str());
  43. PropagateAllTemplateChanges();
  44. }
  45. void PrefabAssetPathChangeTestFixture::SendFolderPathNameChangeEvent(AZStd::string_view fromPath, AZStd::string_view toPath)
  46. {
  47. AzToolsFramework::AssetBrowser::AssetBrowserFileActionNotificationBus::Broadcast(
  48. &AzToolsFramework::AssetBrowser::AssetBrowserFileActionNotificationBus::Events::OnSourceFolderPathNameChanged,
  49. fromPath,
  50. toPath);
  51. }
  52. void PrefabAssetPathChangeTestFixture::SendFilePathNameChangeEvent(AZStd::string_view fromPath, AZStd::string_view toPath)
  53. {
  54. AzToolsFramework::AssetBrowser::AssetBrowserFileActionNotificationBus::Broadcast(
  55. &AzToolsFramework::AssetBrowser::AssetBrowserFileActionNotificationBus::Events::OnSourceFilePathNameChanged,
  56. fromPath,
  57. toPath);
  58. }
  59. AZ::IO::Path PrefabAssetPathChangeTestFixture::GetAbsoluteFilePathName(AZStd::string_view folderPath, AZStd::string_view fileName)
  60. {
  61. AZ::IO::Path absolutePath(m_projectPath, AZ::IO::PosixPathSeparator);
  62. absolutePath /= folderPath;
  63. absolutePath /= fileName;
  64. return absolutePath;
  65. }
  66. AZ::IO::Path PrefabAssetPathChangeTestFixture::GetPrefabFilePathForSerialization(AZStd::string_view folderPath, AZStd::string_view fileName)
  67. {
  68. AZ::IO::Path absolutePath = GetAbsoluteFilePathName(folderPath, fileName);
  69. return m_prefabLoaderInterface->GenerateRelativePath(absolutePath);
  70. }
  71. } // namespace UnitTest