SourceDependencyTests.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <QCoreApplication>
  9. #include <native/tests/assetmanager/SourceDependencyTests.h>
  10. #include <native/unittests/UnitTestUtils.h>
  11. #include <AzFramework/IO/LocalFileIO.h>
  12. #include <AzCore/Utils/Utils.h>
  13. namespace UnitTests
  14. {
  15. void SourceDependencyTests::SetUp()
  16. {
  17. AssetManagerTestingBase::SetUp();
  18. AZ::Data::AssetId::Reflect(m_serializeContext.get());
  19. AZ::Data::AssetData::Reflect(m_serializeContext.get());
  20. using namespace AssetBuilderSDK;
  21. m_uuidInterface = AZ::Interface<AssetProcessor::IUuidRequests>::Get();
  22. ASSERT_TRUE(m_uuidInterface);
  23. m_uuidInterface->EnableGenerationForTypes({ ".stage1" });
  24. m_assetProcessorManager->SetMetaCreationDelay(MetadataProcessingDelayMs);
  25. CreateBuilder("stage1", "*.stage1", "stage2", false, ProductOutputFlags::ProductAsset);
  26. ProcessFileMultiStage(1, true);
  27. QCoreApplication::processEvents();
  28. }
  29. TEST_F(SourceDependencyTests, ExistingSourceAndProductDependency_OnCatalogStartup_LegacyUuidsUpgraded)
  30. {
  31. // Test that source and product dependencies using legacy UUIDs are upgraded during catalog setup
  32. using namespace AssetProcessor;
  33. using namespace AzToolsFramework::AssetDatabase;
  34. SourceAssetReference testA = SourceAssetReference(m_testFilePath.c_str());
  35. AZ::IO::Path scanFolderDir(m_scanfolder.m_scanFolder);
  36. AZStd::string testFilename = "testB.stage1";
  37. SourceAssetReference testB(scanFolderDir / testFilename);
  38. AZ::Utils::WriteFile("unit test file", testB.AbsolutePath().c_str());
  39. auto testAUuids = m_uuidInterface->GetLegacyUuids(testA);
  40. ASSERT_TRUE(testAUuids);
  41. auto testBUuids = m_uuidInterface->GetLegacyUuids(testB);
  42. ASSERT_TRUE(testBUuids);
  43. // Inject a source dependency into the db using legacy UUIDs on both ends
  44. // A depends on -> B
  45. auto builderId = AZ::Uuid::CreateRandom();
  46. SourceFileDependencyEntry dep{ builderId,
  47. *testAUuids.GetValue().begin(),
  48. PathOrUuid(*testBUuids.GetValue().begin()),
  49. SourceFileDependencyEntry::DEP_SourceToSource,
  50. true,
  51. "" };
  52. ASSERT_TRUE(m_stateData->SetSourceFileDependency(dep));
  53. ProductDatabaseEntryContainer products;
  54. EXPECT_TRUE(m_stateData->GetProductsBySourceNameScanFolderID(testA.RelativePath().c_str(), testA.ScanFolderId(), products));
  55. ASSERT_EQ(products.size(), 1);
  56. // Inject a product dependency with B's legacy UUID. A depends on -> B
  57. ProductDependencyDatabaseEntry productDep{ products[0].m_productID, *testBUuids.GetValue().begin(), 0, {}, "pc", 1 };
  58. EXPECT_TRUE(m_stateData->SetProductDependency(productDep));
  59. // Run the catalog startup which handles the updating
  60. auto catalog = AZStd::make_unique<AssetCatalog>(nullptr, m_platformConfig.get());
  61. catalog->BuildRegistry();
  62. // Check that the source dependency db entry has been updated
  63. auto testAUuid = m_uuidInterface->GetUuid(testA).GetValue();
  64. auto testBUuid = m_uuidInterface->GetUuid(testB).GetValue();
  65. SourceFileDependencyEntryContainer updatedEntry;
  66. EXPECT_TRUE(m_stateData->GetSourceFileDependenciesByBuilderGUIDAndSource(builderId, testAUuid, SourceFileDependencyEntry::DEP_Any, updatedEntry));
  67. ASSERT_EQ(updatedEntry.size(), 1);
  68. EXPECT_STREQ(updatedEntry[0].m_dependsOnSource.ToString().c_str(), testBUuid.ToFixedString(false, false).c_str());
  69. EXPECT_STREQ(updatedEntry[0].m_sourceGuid.ToFixedString().c_str(), testAUuid.ToFixedString().c_str());
  70. // Check that the product dependency db entry has been updated
  71. ProductDependencyDatabaseEntryContainer productDependencies;
  72. EXPECT_TRUE(m_stateData->GetProductDependencies(productDependencies));
  73. ASSERT_EQ(productDependencies.size(), 1);
  74. EXPECT_STREQ(productDependencies[0].m_dependencySourceGuid.ToFixedString().c_str(), testBUuid.ToFixedString().c_str());
  75. }
  76. TEST_F(SourceDependencyTests, NewlyCreatedSourceAndProductDependency_UpgradedBeforeSaving)
  77. {
  78. // Test that a Source dependency using a legacy UUID reference is upgraded before being saved to the database during processing
  79. using namespace AssetProcessor;
  80. using namespace AzToolsFramework::AssetDatabase;
  81. using namespace AssetBuilderSDK;
  82. SourceAssetReference testA = SourceAssetReference(m_testFilePath.c_str());
  83. AZ::IO::Path scanFolderDir(m_scanfolder.m_scanFolder);
  84. AZStd::string testFilename = "testB.src";
  85. SourceAssetReference testB(scanFolderDir / testFilename);
  86. AZ::Utils::WriteFile("unit test file", testB.AbsolutePath().c_str());
  87. auto testAUuids = m_uuidInterface->GetLegacyUuids(testA);
  88. ASSERT_TRUE(testAUuids);
  89. auto testBUuids = m_uuidInterface->GetLegacyUuids(testB);
  90. ASSERT_TRUE(testBUuids);
  91. // Builder which will say B depends on legacy A with a source dependency and a product dependency
  92. const auto testASubId = 0;
  93. auto builderId = AZ::Uuid::CreateRandom();
  94. m_builderInfoHandler.CreateBuilderDesc(
  95. "DependencyBuilder",
  96. builderId.ToFixedString().c_str(),
  97. { AssetBuilderPattern{ "*.src", AssetBuilderPattern::Wildcard } },
  98. CreateJobStage("DependencyBuilder", false, PathOrUuid(*testAUuids.GetValue().begin())),
  99. ProcessJobStage("bin", ProductOutputFlags::ProductAsset, false, AZ::Data::AssetId(*testAUuids.GetValue().begin(), testASubId)),
  100. "fingerprint");
  101. // Process the file
  102. ProcessFileMultiStage(1, false, testB);
  103. // Fetch and check the source dependency
  104. SourceFileDependencyEntryContainer dependencies;
  105. EXPECT_TRUE(m_stateData->GetSourceFileDependenciesByBuilderGUIDAndSource(
  106. builderId, m_uuidInterface->GetUuid(testB).GetValue(), SourceFileDependencyEntry::DEP_Any, dependencies));
  107. auto testAUuid = m_uuidInterface->GetUuid(testA).GetValue();
  108. ASSERT_EQ(dependencies.size(), 1);
  109. EXPECT_STREQ(
  110. dependencies[0].m_dependsOnSource.GetUuid().ToFixedString().c_str(),
  111. testAUuid.ToFixedString().c_str());
  112. // Fetch and check the product dependency
  113. ProductDependencyDatabaseEntryContainer productDependencies;
  114. EXPECT_TRUE(m_stateData->GetProductDependencies(productDependencies));
  115. ASSERT_EQ(productDependencies.size(), 1);
  116. EXPECT_STREQ(productDependencies[0].m_dependencySourceGuid.ToFixedString().c_str(), testAUuid.ToFixedString().c_str());
  117. }
  118. } // namespace UnitTests