AssetValidationTestShared.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <AzTest/AzTest.h>
  10. #include <AssetValidationSystemComponent.h>
  11. #include <AzFramework/API/ApplicationAPI.h>
  12. #include <AzFramework/Asset/AssetSeedList.h>
  13. #include <AzFramework/Asset/AssetSystemBus.h>
  14. #include <AzFramework/IO/LocalFileIO.h>
  15. #include <AzCore/UnitTest/TestTypes.h>
  16. #include <AzCore/Settings/SettingsRegistryImpl.h>
  17. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  18. #include <Tests/FileIOBaseTestTypes.h>
  19. namespace UnitTest
  20. {
  21. constexpr int NumTestAssets = 10;
  22. constexpr char ProjectName[] = "UnitTest";
  23. class MockValidationComponent
  24. : public AssetValidation::AssetValidationSystemComponent
  25. , public AZ::Data::AssetCatalogRequestBus::Handler
  26. {
  27. public:
  28. AZ_CLASS_ALLOCATOR(MockValidationComponent, AZ::SystemAllocator)
  29. MockValidationComponent()
  30. {
  31. for (int i = 0; i < NumTestAssets; ++i)
  32. {
  33. m_assetIds[i] = AZ::Data::AssetId(AZ::Uuid::CreateRandom(), 0);
  34. }
  35. AZ::Data::AssetCatalogRequestBus::Handler::BusConnect();
  36. AssetValidation::AssetValidationSystemComponent::Activate();
  37. }
  38. ~MockValidationComponent()
  39. {
  40. AssetValidation::AssetValidationSystemComponent::Deactivate();
  41. AZ::Data::AssetCatalogRequestBus::Handler::BusDisconnect();
  42. }
  43. void SeedMode() override
  44. {
  45. AssetValidation::AssetValidationSystemComponent::SeedMode();
  46. }
  47. bool IsKnownAsset(const char* fileName) override
  48. {
  49. return AssetValidation::AssetValidationSystemComponent::IsKnownAsset(fileName);
  50. }
  51. bool AddSeedAssetId(AZ::Data::AssetId assetId, AZ::u32 sourceId) override
  52. {
  53. return AssetValidation::AssetValidationSystemComponent::AddSeedAssetId(assetId, sourceId);
  54. }
  55. AZ::Data::AssetInfo GetAssetInfoById(const AZ::Data::AssetId& id) override
  56. {
  57. AZ::Data::AssetInfo result;
  58. for (int slotNum = 0; slotNum < NumTestAssets; ++slotNum)
  59. {
  60. const AZ::Data::AssetId& assetId = m_assetIds[slotNum];
  61. if (assetId == id)
  62. {
  63. result.m_assetId = id;
  64. // Internal paths should be lower cased as from the cache
  65. result.m_relativePath = AZStd::string::format("assetpath%d", slotNum);
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. AZ::Outcome<AZStd::vector<AZ::Data::ProductDependency>, AZStd::string> GetAllProductDependencies(const AZ::Data::AssetId& id) override
  72. {
  73. AZStd::vector<AZ::Data::ProductDependency> dependencyList;
  74. bool foundAsset{ false };
  75. for (const AZ::Data::AssetId& assetId : m_assetIds)
  76. {
  77. if (assetId == id)
  78. {
  79. foundAsset = true;
  80. }
  81. else if (foundAsset)
  82. {
  83. AZ::Data::ProductDependency thisDependency;
  84. thisDependency.m_assetId = assetId;
  85. dependencyList.emplace_back(AZ::Data::ProductDependency(assetId, {}));
  86. }
  87. }
  88. if (dependencyList.size())
  89. {
  90. return AZ::Success(dependencyList);
  91. }
  92. return AZ::Failure(AZStd::string("Asset not found"));
  93. }
  94. bool TestAddSeedsFor(const AzFramework::AssetSeedList& seedList, AZ::u32 sourceId)
  95. {
  96. return AddSeedsFor(seedList, sourceId);
  97. }
  98. bool TestRemoveSeedsFor(const AzFramework::AssetSeedList& seedList, AZ::u32 sourceId)
  99. {
  100. return RemoveSeedsFor(seedList, sourceId);
  101. }
  102. bool TestAddSeedList(const char* seedListName)
  103. {
  104. return AddSeedList(seedListName);
  105. }
  106. bool TestRemoveSeedList(const char* seedListName)
  107. {
  108. return RemoveSeedList(seedListName);
  109. }
  110. AZ::Outcome<AzFramework::AssetSeedList, AZStd::string> LoadSeedList(const char* fileName, AZStd::string& seedFilepath) override
  111. {
  112. if (m_validSeedPath == fileName)
  113. {
  114. seedFilepath = fileName;
  115. return AZ::Success(m_validSeedList);
  116. }
  117. return AZ::Failure(AZStd::string("Invalid List"));
  118. }
  119. AzFramework::AssetSeedList m_validSeedList;
  120. AZStd::string m_validSeedPath;
  121. AZ::Data::AssetId m_assetIds[NumTestAssets];
  122. };
  123. struct AssetValidationTest
  124. : UnitTest::LeakDetectionFixture
  125. , UnitTest::SetRestoreFileIOBaseRAII
  126. , AzFramework::ApplicationRequests::Bus::Handler
  127. {
  128. AssetValidationTest()
  129. : UnitTest::SetRestoreFileIOBaseRAII(m_fileIO)
  130. {
  131. if (!AZ::SettingsRegistry::Get())
  132. {
  133. AZ::SettingsRegistry::Register(&m_registry);
  134. auto projectPathKey = AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey)
  135. + "/project_path";
  136. m_registry.Set(projectPathKey, (AZ::IO::FixedMaxPath(m_tempDir.GetDirectory()) / "AutomatedTesting").Native());
  137. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(m_registry);
  138. // Set the engine root scan up path to the temporary directory
  139. constexpr AZStd::string_view InternalScanUpEngineRootKey{ "/O3DE/Runtime/Internal/engine_root_scan_up_path" };
  140. m_registry.Set(InternalScanUpEngineRootKey, m_tempDir.GetDirectory());
  141. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(m_registry);
  142. }
  143. }
  144. void NormalizePath(AZStd::string&) override
  145. {
  146. AZ_Assert(false, "Not implemented");
  147. }
  148. void NormalizePathKeepCase(AZStd::string&) override
  149. {
  150. AZ_Assert(false, "Not implemented");
  151. }
  152. void CalculateBranchTokenForEngineRoot([[maybe_unused]] AZStd::string& token) const override
  153. {
  154. AZ_Assert(false, "Not implemented");
  155. }
  156. void SetUp() override
  157. {
  158. AzFramework::ApplicationRequests::Bus::Handler::BusConnect();
  159. }
  160. void TearDown() override
  161. {
  162. AzFramework::ApplicationRequests::Bus::Handler::BusDisconnect();
  163. }
  164. bool CreateDummyFile(const AZ::IO::Path& path, AZStd::string_view contents) const;
  165. AZ::IO::LocalFileIO m_fileIO;
  166. AZ::Test::ScopedAutoTempDirectory m_tempDir;
  167. AZ::SettingsRegistryImpl m_registry;
  168. };
  169. }