JobDependencySubIdTests.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <native/tests/assetmanager/JobDependencySubIdTests.h>
  9. #include <native/unittests/UnitTestUtils.h>
  10. #include <QApplication>
  11. namespace UnitTests
  12. {
  13. void JobDependencySubIdTest::CreateTestData(AZ::u64 hashA, AZ::u64 hashB, bool useSubId)
  14. {
  15. using namespace AzToolsFramework::AssetDatabase;
  16. SourceDatabaseEntry source1{ m_scanfolder.m_scanFolderID, "parent.txt", AZ::Uuid::CreateRandom(), "fingerprint" };
  17. SourceDatabaseEntry source2{ m_scanfolder.m_scanFolderID, "child.txt", AZ::Uuid::CreateRandom(), "fingerprint" };
  18. m_parentFile = AZ::IO::Path(m_scanfolder.m_scanFolder) / "parent.txt";
  19. m_childFile = AZ::IO::Path(m_scanfolder.m_scanFolder) / "child.txt";
  20. UnitTestUtils::CreateDummyFile(m_parentFile.Native().c_str(), QString("tempdata"));
  21. UnitTestUtils::CreateDummyFile(m_childFile.Native().c_str(), QString("tempdata"));
  22. ASSERT_TRUE(m_stateData->SetSource(source1));
  23. ASSERT_TRUE(m_stateData->SetSource(source2));
  24. JobDatabaseEntry job1{
  25. source1.m_sourceID, "Mock Job", 1234, "pc", m_builderInfoHandler.m_builderDescMap.begin()->second.m_busId, AzToolsFramework::AssetSystem::JobStatus::Completed, 999
  26. };
  27. ASSERT_TRUE(m_stateData->SetJob(job1));
  28. ProductDatabaseEntry product1{ job1.m_jobID, 0, "pc/product.txt", m_assetType,
  29. AZ::Uuid::CreateName("product.txt"), hashA, static_cast<int>(AssetBuilderSDK::ProductOutputFlags::ProductAsset) };
  30. ProductDatabaseEntry product2{ job1.m_jobID, 777, "pc/product777.txt", m_assetType,
  31. AZ::Uuid::CreateName("product777.txt"), hashB, static_cast<int>(AssetBuilderSDK::ProductOutputFlags::ProductAsset) };
  32. ASSERT_TRUE(m_stateData->SetProduct(product1));
  33. ASSERT_TRUE(m_stateData->SetProduct(product2));
  34. SourceFileDependencyEntry dependency1{ AZ::Uuid::CreateRandom(),
  35. source2.m_sourceGuid,
  36. PathOrUuid(source1.m_sourceName),
  37. SourceFileDependencyEntry::DEP_JobToJob,
  38. 0,
  39. useSubId ? AZStd::to_string(product2.m_subID).c_str() : "" };
  40. ASSERT_TRUE(m_stateData->SetSourceFileDependency(dependency1));
  41. }
  42. void JobDependencySubIdTest::RunTest(bool firstProductChanged, bool secondProductChanged)
  43. {
  44. AZ::IO::Path cacheDir(m_databaseLocationListener.GetAssetRootDir());
  45. cacheDir /= "Cache";
  46. cacheDir /= "pc";
  47. AZStd::string productFilename = "product.txt";
  48. AZStd::string product2Filename = "product777.txt";
  49. AZStd::string productPath = (cacheDir / productFilename).AsPosix().c_str();
  50. AZStd::string product2Path = (cacheDir / product2Filename).AsPosix().c_str();
  51. UnitTestUtils::CreateDummyFile(productPath.c_str(), "unit test file");
  52. UnitTestUtils::CreateDummyFile(product2Path.c_str(), "unit test file");
  53. AZ::u64 hashA = firstProductChanged ? 0 : AssetUtilities::GetFileHash(productPath.c_str());
  54. AZ::u64 hashB = secondProductChanged ? 0 : AssetUtilities::GetFileHash(product2Path.c_str());
  55. CreateTestData(hashA, hashB, true);
  56. QMetaObject::invokeMethod(
  57. m_assetProcessorManager.get(), "AssessModifiedFile", Qt::QueuedConnection, Q_ARG(QString, m_parentFile.c_str()));
  58. QCoreApplication::processEvents();
  59. m_assetProcessorManager->CheckActiveFiles(1);
  60. QCoreApplication::processEvents();
  61. m_assetProcessorManager->CheckActiveFiles(0);
  62. m_assetProcessorManager->CheckFilesToExamine(1);
  63. QCoreApplication::processEvents();
  64. m_assetProcessorManager->CheckJobEntries(1);
  65. AZStd::vector<AssetProcessor::JobDetails> jobDetailsList;
  66. QObject::connect(
  67. m_assetProcessorManager.get(), &AssetProcessor::AssetProcessorManager::AssetToProcess,
  68. [&jobDetailsList](AssetProcessor::JobDetails jobDetails)
  69. {
  70. jobDetailsList.push_back(jobDetails);
  71. });
  72. QCoreApplication::processEvents();
  73. ASSERT_EQ(jobDetailsList.size(), 1);
  74. AssetBuilderSDK::ProcessJobResponse response;
  75. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Success;
  76. response.m_outputProducts.push_back(AssetBuilderSDK::JobProduct(productFilename, m_assetType, 0));
  77. response.m_outputProducts.push_back(AssetBuilderSDK::JobProduct(product2Filename, m_assetType, 777));
  78. m_assetProcessorManager->AssetProcessed(jobDetailsList[0].m_jobEntry, response);
  79. // We're only really interested in ActiveFiles but check the others to be sure
  80. m_assetProcessorManager->CheckFilesToExamine(0);
  81. m_assetProcessorManager->CheckActiveFiles(secondProductChanged ? 1 : 0); // The 2nd product is the one we have a dependency on, only if that changed should we see the other file process
  82. m_assetProcessorManager->CheckJobEntries(0);
  83. }
  84. TEST_F(JobDependencySubIdTest, RegularJobDependency_NoSubId_ProcessDependent)
  85. {
  86. CreateTestData(0, 0, false);
  87. QMetaObject::invokeMethod(
  88. m_assetProcessorManager.get(), "AssessModifiedFile", Qt::QueuedConnection, Q_ARG(QString, m_parentFile.c_str()));
  89. QCoreApplication::processEvents();
  90. m_assetProcessorManager->CheckActiveFiles(1);
  91. QCoreApplication::processEvents();
  92. m_assetProcessorManager->CheckActiveFiles(0);
  93. m_assetProcessorManager->CheckFilesToExamine(2);
  94. }
  95. TEST_F(JobDependencySubIdTest, JobDependencyWithSubID_SameHash_DependentDoesNotProcess)
  96. {
  97. RunTest(false, false);
  98. }
  99. TEST_F(JobDependencySubIdTest, JobDependencyWithSubID_DifferentHashOnCorrectSubId_DependentProcesses)
  100. {
  101. RunTest(false, true);
  102. }
  103. TEST_F(JobDependencySubIdTest, JobDependencyWithSubID_BothHashesDifferent_DependentProcesses)
  104. {
  105. // Should be the same result as above but check just in case
  106. RunTest(true, true);
  107. }
  108. TEST_F(JobDependencySubIdTest, JobDependencyWithSubID_DifferentHashOnIncorrectSubId_DependentDoesNotProcess)
  109. {
  110. RunTest(true, false);
  111. }
  112. }