AssetCatalogUnitTests.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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 <AzCore/base.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. #endif
  13. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  14. #include <AzToolsFramework/API/AssetDatabaseBus.h>
  15. #include <QCoreApplication>
  16. #include <native/unittests/UnitTestUtils.h> // for UnitTestUtils like CreateDummyFile / AssertAbsorber.
  17. #include <native/tests/MockAssetDatabaseRequestsHandler.h>
  18. #include <native/resourcecompiler/RCBuilder.h>
  19. #include "AssetManager/FileStateCache.h"
  20. #include <tests/UnitTestUtilities.h>
  21. #include <utilities/UuidManager.h>
  22. #include <AzToolsFramework/Metadata/MetadataManager.h>
  23. namespace AssetProcessor
  24. {
  25. using namespace AZ;
  26. using namespace AZ::Data;
  27. using namespace testing;
  28. using namespace UnitTestUtils;
  29. using namespace UnitTest;
  30. using namespace AzFramework::AssetSystem;
  31. using namespace AzToolsFramework::AssetSystem;
  32. using namespace AzToolsFramework::AssetDatabase;
  33. class AssetCatalogForUnitTest : public AssetProcessor::AssetCatalog
  34. {
  35. public:
  36. AssetCatalogForUnitTest(QObject* parent, AssetProcessor::PlatformConfiguration* platformConfiguration)
  37. : AssetCatalog(parent, platformConfiguration) {}
  38. // prevent automatic save on shutdown, no point in doing that in unit test mode, just wastes time.
  39. virtual ~AssetCatalogForUnitTest()
  40. {
  41. ClearDirtyFlag();
  42. }
  43. void ClearDirtyFlag()
  44. {
  45. m_catalogIsDirty = false;
  46. }
  47. AzFramework::AssetRegistry& GetRegistry(QString platformKey)
  48. {
  49. return m_registries[platformKey];
  50. }
  51. protected:
  52. };
  53. class AssetCatalogTest
  54. : public LeakDetectionFixture
  55. {
  56. protected:
  57. // store all data we create here so that it can be destroyed on shutdown before we remove allocators
  58. struct DataMembers
  59. {
  60. MockAssetDatabaseRequestsHandler m_databaseLocationListener;
  61. FileStatePassthrough m_fileStateCache;
  62. QDir m_assetRootSourceDir;
  63. QDir m_priorAssetRoot;
  64. AssetDatabaseConnection m_dbConn;
  65. UnitTestUtils::ScopedDir m_scopedDir;
  66. PlatformConfiguration m_config;
  67. AZStd::unique_ptr<AssetCatalogForUnitTest> m_assetCatalog;
  68. QDir m_cacheRootDir; // where the 'cache' folder lives.
  69. QString m_gameName;
  70. AssertAbsorber m_absorber;
  71. AZStd::string m_databaseLocation;
  72. QCoreApplication coreApp;
  73. AzToolsFramework::UuidUtilComponent m_uuidUtil;
  74. AzToolsFramework::MetadataManager m_metadataManager;
  75. AssetProcessor::UuidManager m_uuidManager;
  76. int argc = 0;
  77. DataMembers() : coreApp(argc, nullptr)
  78. {
  79. }
  80. };
  81. // The component application creates and returns a system entity, but doesn't keep track of it
  82. // It's the responsibility of whatever owns the component application to also track and manage the lifetime
  83. // of this entity.
  84. AZ::Entity* m_systemEntity = nullptr;
  85. DataMembers* m_data = nullptr;
  86. AZStd::unique_ptr<AZ::ComponentApplication> m_app; // the app is created seperately so that we can control its lifetime.
  87. ::UnitTests::MockVirtualFileIO m_virtualFileIO;
  88. void SetUp() override
  89. {
  90. m_app.reset(aznew AZ::ComponentApplication());
  91. AZ::ComponentApplication::Descriptor desc;
  92. desc.m_useExistingAllocator = true;
  93. AZ::ComponentApplication::StartupParameters startupParameters;
  94. startupParameters.m_loadSettingsRegistry = false;
  95. m_systemEntity = m_app->Create(desc, startupParameters);
  96. m_data = azcreate(DataMembers, ());
  97. AssetUtilities::ComputeAssetRoot(m_data->m_priorAssetRoot);
  98. AssetUtilities::ResetAssetRoot();
  99. m_data->m_assetRootSourceDir = QDir(m_data->m_databaseLocationListener.GetAssetRootDir().c_str());
  100. m_data->m_scopedDir.Setup(m_data->m_assetRootSourceDir.path());
  101. m_data->m_gameName = AssetUtilities::ComputeProjectName("AutomatedTesting"); // uses the above file.
  102. AssetUtilities::ResetAssetRoot();
  103. QDir newRoot; // throwaway dummy var - we just want to invoke the below function
  104. AssetUtilities::ComputeAssetRoot(newRoot, &m_data->m_assetRootSourceDir);
  105. auto settingsRegistry = AZ::SettingsRegistry::Get();
  106. auto cacheRootKey =
  107. AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_cache_path";
  108. settingsRegistry->Set(cacheRootKey, m_data->m_assetRootSourceDir.absoluteFilePath("Cache").toUtf8().constData());
  109. auto projectPathKey =
  110. AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_path";
  111. AZ::IO::FixedMaxPath enginePath;
  112. settingsRegistry->Get(enginePath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  113. settingsRegistry->Set(projectPathKey, (enginePath / "AutomatedTesting").Native());
  114. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*settingsRegistry);
  115. AssetUtilities::ComputeProjectCacheRoot(m_data->m_cacheRootDir);
  116. QString normalizedCacheRoot = AssetUtilities::NormalizeDirectoryPath(m_data->m_cacheRootDir.absolutePath());
  117. m_data->m_cacheRootDir = QDir(normalizedCacheRoot);
  118. // create the files we'll use for this test:
  119. QSet<QString> expectedFiles;
  120. // set up some interesting files:
  121. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("rootfile2.txt");
  122. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder1/rootfile1.txt"); // note: Must override the actual root file
  123. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder1/basefile.txt");
  124. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/basefile.txt");
  125. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/aaa/basefile.txt");
  126. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/aaa/bbb/basefile.txt");
  127. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/aaa/bbb/ccc/basefile.txt");
  128. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/aaa/bbb/ccc/ddd/basefile.txt");
  129. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/BaseFile.txt"); // note the case upper here
  130. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder8/a/b/c/test.txt");
  131. // subfolder3 is not recursive so none of these should show up in any scan or override check
  132. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/aaa/basefile.txt");
  133. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/aaa/bbb/basefile.txt");
  134. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/aaa/bbb/ccc/basefile.txt");
  135. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/uniquefile.txt"); // only exists in subfolder3
  136. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/uniquefile.ignore"); // only exists in subfolder3
  137. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/rootfile3.txt"); // must override rootfile3 in root
  138. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("rootfile1.txt");
  139. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("rootfile3.txt");
  140. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("unrecognised.file"); // a file that should not be recognised
  141. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("unrecognised2.file"); // a file that should not be recognised
  142. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder1/test/test.format"); // a file that should be recognised
  143. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("test.format"); // a file that should NOT be recognised
  144. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/somefile.xxx");
  145. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/savebackup/test.txt");//file that should be excluded
  146. expectedFiles << m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/somerandomfile.random");
  147. for (const QString& expect : expectedFiles)
  148. {
  149. CreateDummyFileAZ(expect.toUtf8().constData());
  150. }
  151. m_data->m_dbConn.OpenDatabase();
  152. BuildConfig(m_data->m_assetRootSourceDir, &(m_data->m_dbConn), m_data->m_config);
  153. m_data->m_assetCatalog.reset(new AssetCatalogForUnitTest(nullptr, &(m_data->m_config)));
  154. }
  155. void TearDown() override
  156. {
  157. // if you EXPECT warnings/asserts/errors you need to check in your test, and you need to also
  158. // reset it before returning from your test.
  159. EXPECT_EQ(m_data->m_absorber.m_numAssertsAbsorbed, 0);
  160. EXPECT_EQ(m_data->m_absorber.m_numErrorsAbsorbed, 0);
  161. EXPECT_EQ(m_data->m_absorber.m_numWarningsAbsorbed, 0);
  162. AssetUtilities::ResetAssetRoot();
  163. azdestroy(m_data);
  164. m_systemEntity = nullptr;
  165. m_app->Destroy();
  166. m_app.reset();
  167. }
  168. // -- utility functions to create default state data --
  169. // Adds a scan folder to the config and to the database
  170. void AddScanFolder(ScanFolderInfo scanFolderInfo, PlatformConfiguration& config, AssetDatabaseConnection* dbConn)
  171. {
  172. ScanFolderDatabaseEntry newScanFolder(
  173. scanFolderInfo.ScanPath().toStdString().c_str(),
  174. scanFolderInfo.GetDisplayName().toStdString().c_str(),
  175. scanFolderInfo.GetPortableKey().toStdString().c_str(),
  176. scanFolderInfo.IsRoot());
  177. dbConn->SetScanFolder(newScanFolder);
  178. scanFolderInfo.SetScanFolderID(newScanFolder.m_scanFolderID);
  179. config.AddScanFolder(scanFolderInfo);
  180. }
  181. virtual void AddScanFolders(
  182. const QDir& tempPath, AssetDatabaseConnection* dbConn, PlatformConfiguration& config,
  183. const AZStd::vector<AssetBuilderSDK::PlatformInfo>& platforms)
  184. {
  185. // PATH DisplayName PortKey root recurse platforms order
  186. AddScanFolder(ScanFolderInfo(tempPath.filePath("subfolder4"), "subfolder4", "subfolder4", false, false, platforms, -6), config, dbConn); // subfolder 4 overrides subfolder3
  187. AddScanFolder(ScanFolderInfo(tempPath.filePath("subfolder3"), "subfolder3", "subfolder3", false, false, platforms, -5), config, dbConn); // subfolder 3 overrides subfolder2
  188. AddScanFolder(ScanFolderInfo(tempPath.filePath("subfolder2"), "subfolder2", "subfolder2", false, true, platforms, -2), config, dbConn); // subfolder 2 overrides subfolder1
  189. AddScanFolder(ScanFolderInfo(tempPath.filePath("subfolder1"), "subfolder1", "subfolder1", false, true, platforms, -1), config, dbConn); // subfolder1 overrides root
  190. AddScanFolder(ScanFolderInfo(tempPath.absolutePath(), "temp", "tempfolder", true, false, platforms, 0), config, dbConn); // add the root
  191. }
  192. // build some default configs.
  193. void BuildConfig(const QDir& tempPath, AssetDatabaseConnection* dbConn, PlatformConfiguration& config)
  194. {
  195. config.EnablePlatform({ "pc" ,{ "desktop", "renderer" } }, true);
  196. config.EnablePlatform({ "android" ,{ "mobile", "renderer" } }, true);
  197. config.EnablePlatform({ "fandango" ,{ "console", "renderer" } }, false);
  198. AZStd::vector<AssetBuilderSDK::PlatformInfo> platforms;
  199. config.PopulatePlatformsForScanFolder(platforms);
  200. AddScanFolders(tempPath, dbConn, config, platforms);
  201. config.AddMetaDataType("exportsettings", QString());
  202. AssetRecognizer rec;
  203. rec.m_name = "random files";
  204. rec.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher("*.random", AssetBuilderSDK::AssetBuilderPattern::Wildcard);
  205. rec.m_platformSpecs.insert({"pc", AssetInternalSpec::Copy});
  206. config.AddRecognizer(rec);
  207. const char* builderTxt1Name = "txt files";
  208. rec.m_name = builderTxt1Name;
  209. rec.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher("*.txt", AssetBuilderSDK::AssetBuilderPattern::Wildcard);
  210. rec.m_platformSpecs.insert({"pc", AssetInternalSpec::Copy});
  211. rec.m_platformSpecs.insert({"android", AssetInternalSpec::Copy});
  212. config.AddRecognizer(rec);
  213. // Ignore recognizer
  214. AssetRecognizer ignore_rec;
  215. ignore_rec.m_name = "ignore files";
  216. ignore_rec.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher("*.ignore", AssetBuilderSDK::AssetBuilderPattern::Wildcard);
  217. ignore_rec.m_platformSpecs.insert({"pc", AssetInternalSpec::Copy});
  218. ignore_rec.m_platformSpecs.insert({"android", AssetInternalSpec::Skip});
  219. config.AddRecognizer(ignore_rec);
  220. ExcludeAssetRecognizer excludeRecogniser;
  221. excludeRecogniser.m_name = "backup";
  222. excludeRecogniser.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher("(^|.+/)savebackup/.*", AssetBuilderSDK::AssetBuilderPattern::Regex);
  223. config.AddExcludeRecognizer(excludeRecogniser);
  224. }
  225. // Adds a source file and job entry to the database, jobId is output
  226. bool AddSourceAndJob(const char* scanFolder, const char* sourceRelPath, AssetDatabaseConnection* dbConn, AZ::s64& jobId, AZ::Uuid assetId = AZ::Uuid::CreateRandom())
  227. {
  228. ScanFolderDatabaseEntry scanFolderEntry;
  229. bool result = dbConn->GetScanFolderByPortableKey(scanFolder, scanFolderEntry);
  230. if (!result)
  231. {
  232. return false;
  233. }
  234. SourceDatabaseEntry sourceEntry(scanFolderEntry.m_scanFolderID, sourceRelPath, assetId, "fingerprint1");
  235. dbConn->SetSource(sourceEntry);
  236. JobDatabaseEntry jobEntry(sourceEntry.m_sourceID, "test", 1234, "pc", assetId, AzToolsFramework::AssetSystem::JobStatus::Completed, 12345);
  237. result = dbConn->SetJob(jobEntry);
  238. jobId = jobEntry.m_jobID;
  239. return result;
  240. };
  241. bool AddSourceAndJobForMultiplePlatforms(
  242. const char* scanFolder,
  243. const char* sourceRelPath,
  244. AssetDatabaseConnection* dbConn,
  245. AZStd::map<AZStd::string, AZ::s64>& platformsToJobIds,
  246. const AZStd::vector<AZStd::string>& platforms,
  247. AZ::Uuid assetId = AZ::Uuid::CreateRandom())
  248. {
  249. ScanFolderDatabaseEntry scanFolderEntry;
  250. bool result = dbConn->GetScanFolderByPortableKey(scanFolder, scanFolderEntry);
  251. if (!result)
  252. {
  253. return false;
  254. }
  255. SourceDatabaseEntry sourceEntry(scanFolderEntry.m_scanFolderID, sourceRelPath, assetId, "fingerprint1");
  256. dbConn->SetSource(sourceEntry);
  257. for (const AZStd::string& platform : platforms)
  258. {
  259. JobDatabaseEntry jobEntry(sourceEntry.m_sourceID, "test", 1234, platform.c_str(), assetId, AzToolsFramework::AssetSystem::JobStatus::Completed, 12345);
  260. result = dbConn->SetJob(jobEntry);
  261. if (!result)
  262. {
  263. return false;
  264. }
  265. platformsToJobIds[platform] = jobEntry.m_jobID;
  266. }
  267. return true;
  268. };
  269. // Calls the GetRelativeProductPathFromFullSourceOrProductPath function and checks the return results, returning true if it matches both of the expected results
  270. bool TestGetRelativeProductPath(const QString fileToCheck, bool expectedToFind, AZStd::initializer_list<const char*> expectedPaths)
  271. {
  272. bool relPathfound = false;
  273. AZStd::string relPath;
  274. AZStd::string fullPath(fileToCheck.toUtf8().constData());
  275. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(relPathfound, &AzToolsFramework::AssetSystem::AssetSystemRequest::GetRelativeProductPathFromFullSourceOrProductPath, fullPath, relPath);
  276. if (relPathfound != expectedToFind)
  277. {
  278. return false;
  279. }
  280. for (auto& path : expectedPaths)
  281. {
  282. if (relPath == path)
  283. {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. // Calls the GetFullSourcePathFromRelativeProductPath function and checks the return results, returning true if it matches both of
  290. // the expected results
  291. bool TestGetFullSourcePath(const QString& fileToCheck, const QDir& tempPath, bool expectToFind, const char* expectedPath)
  292. {
  293. bool fullPathfound = false;
  294. AZStd::string fullPath;
  295. AZStd::string relPath(fileToCheck.toUtf8().constData());
  296. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(fullPathfound, &AzToolsFramework::AssetSystem::AssetSystemRequest::GetFullSourcePathFromRelativeProductPath, relPath, fullPath);
  297. if (fullPathfound != expectToFind)
  298. {
  299. return false;
  300. }
  301. QString output(fullPath.c_str());
  302. output.remove(0, tempPath.path().length() + 1); //adding one for the native separator
  303. return (output == expectedPath);
  304. }
  305. AZ::s64 CreateProductAndGetProductId(AZ::s64 jobId, const AZStd::string& productPath, const AZ::u32 productSubId)
  306. {
  307. ProductDatabaseEntry product(
  308. jobId,
  309. productSubId,
  310. m_data->m_cacheRootDir.relativeFilePath(productPath.c_str()).toStdString().c_str(),
  311. AZ::Data::AssetType::CreateRandom());
  312. bool result = m_data->m_dbConn.SetProduct(product);
  313. EXPECT_TRUE(result);
  314. return product.m_productID;
  315. }
  316. };
  317. class AssetCatalogTestWithProducts
  318. : public AssetCatalogTest
  319. {
  320. public:
  321. void SetUp() override
  322. {
  323. AssetCatalogTest::SetUp();
  324. // Add a source file with 4 products
  325. AZ::s64 jobId;
  326. bool result = AddSourceAndJob("subfolder3", "BaseFile.txt", &(m_data->m_dbConn), jobId);
  327. EXPECT_TRUE(result);
  328. AZ::u32 productSubId = 0;
  329. for (auto& relativeProductPath : { "subfolder3/basefilez.arc2", "subfolder3/basefileaz.azm2", "subfolder3/basefile.arc2", "subfolder3/basefile.azm2" })
  330. {
  331. ProductDatabaseEntry newProduct(jobId, productSubId++, m_data->m_cacheRootDir.relativeFilePath(relativeProductPath).toStdString().c_str(), AZ::Data::AssetType::CreateRandom());
  332. m_data->m_dbConn.SetProduct(newProduct);
  333. }
  334. }
  335. };
  336. TEST_F(AssetCatalogTest, EmptyConstructors_Sanity)
  337. {
  338. // make sure constructors do not crash or misbehave when given empty names
  339. QString fileToCheck = "";
  340. // empty requests should generate an assert since it is a programming error to call this API with bad data.
  341. // however, the app should not crash even if the assert is absorbed.
  342. GetRelativeProductPathFromFullSourceOrProductPathRequest request(fileToCheck.toUtf8().constData());
  343. ASSERT_EQ(m_data->m_absorber.m_numAssertsAbsorbed, 1);
  344. GetFullSourcePathFromRelativeProductPathRequest sourceRequest(fileToCheck.toUtf8().constData());
  345. ASSERT_EQ(m_data->m_absorber.m_numAssertsAbsorbed, 2);
  346. // reset the absorber before we leave this assert-test, so that it doesn't cause failure of the test itself
  347. m_data->m_absorber.Clear();
  348. ASSERT_TRUE(TestGetRelativeProductPath("", false, { "" }));
  349. ASSERT_TRUE(TestGetFullSourcePath("", m_data->m_assetRootSourceDir, false, ""));
  350. }
  351. TEST_F(AssetCatalogTestWithProducts, GetRelativePath_GivenRootPath_ReturnsFailure)
  352. {
  353. // Failure case
  354. #if defined(AZ_PLATFORM_WINDOWS)
  355. QString fileToCheck = "d:\\test.txt";
  356. #else
  357. QString fileToCheck = "/test.txt"; // rooted
  358. #endif
  359. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, false, { fileToCheck.toStdString().c_str() }));
  360. }
  361. TEST_F(AssetCatalogTestWithProducts, RelativePath)
  362. {
  363. // feed it a relative path with a TAB in the front :)
  364. QString fileToCheck = "\test.txt";
  365. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "\test.txt" }));
  366. }
  367. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_WithGameName_ReturnsFileInGameFolder)
  368. {
  369. // feed it a product path with a platform name, returns it
  370. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/aaa/basefile.txt");
  371. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa/basefile.txt" }));
  372. }
  373. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_WithoutGameName_ReturnsFileInRootFolder)
  374. {
  375. // feed it a product path, just the file name since its supposed to be a root file
  376. QString fileToCheck = m_data->m_cacheRootDir.filePath("pc/basefile.txt");
  377. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "basefile.txt" }));
  378. }
  379. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_BadCasingInPlatform_ReturnsRelativePath)
  380. {
  381. // feed it a product path but with poor casing (test 1: the pc platform is not matching case)
  382. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("Pc") + "/aaa/basefile.txt");
  383. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa/basefile.txt" }));
  384. }
  385. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_BadCasingInGameName_ReturnsRelativePath)
  386. {
  387. //feed it a product path but with poor casing (test 2: the gameName is not matching case)
  388. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/aaa/basefile.txt");
  389. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa/basefile.txt" }));
  390. }
  391. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_FolderName_ReturnsFolderNameOnly)
  392. {
  393. // feed it a product path that resolves to a directory name instead of a file.
  394. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/aaa");
  395. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa" }));
  396. }
  397. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_FolderNameExtraSlash_ReturnsFolderNameOnlyNoExtraSlash)
  398. {
  399. // make sure it doesn't keep any trailing slashes.
  400. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/aaa/"); // extra trailing slash
  401. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa" })); // the API should never result in a trailing slash
  402. }
  403. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_FolderNameExtraWrongWaySlash_ReturnsFolderNameOnlyNoExtraWrongSlash)
  404. {
  405. // make sure it doesn't keep any trailing slashes.
  406. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/aaa\\"); // extra trailing wrongway slash
  407. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa" })); // the API should never result in a trailing slash
  408. }
  409. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_RelativeDirectoryNameWhichDoesNotExist_ReturnsFolderNameOnly)
  410. {
  411. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/nonexistantfolder"); // extra trailing wrongway slash
  412. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "nonexistantfolder" }));
  413. }
  414. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_RelativeDirectoryNameWhichDoesNotExistWithExtraSlash_ReturnsFolderNameOnly)
  415. {
  416. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/nonexistantfolder/"); // extra trailing wrongway slash
  417. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "nonexistantfolder" }));
  418. }
  419. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_RelativeDirectoryNameWhichDoesNotExistWithExtraWrongWaySlash_ReturnsFolderNameOnly)
  420. {
  421. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "\\nonexistantfolder\\"); // extra trailing wrongway slash
  422. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "nonexistantfolder" }));
  423. }
  424. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_RelativePathToSourceFile_ReturnsProductFilePath)
  425. {
  426. QString fileToCheck = m_data->m_assetRootSourceDir.absoluteFilePath("subfolder3/BaseFile.txt");
  427. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "basefilez.arc2", "basefileaz.azm2",
  428. "basefile.arc2", "basefile.azm2" }));
  429. }
  430. TEST_F(AssetCatalogTestWithProducts, GetRelativeProductPathFromFullSourceOrProductPath_RelativePathToSourceFile_BadCasing_ReturnsProductFilePath)
  431. {
  432. // note that the casing of the source file is not correct. It must still work.
  433. QString fileToCheck = m_data->m_assetRootSourceDir.absoluteFilePath("subfolder2/aaa/basefile.txt");
  434. ASSERT_TRUE(TestGetRelativeProductPath(fileToCheck, true, { "aaa/basefile.txt" }));
  435. }
  436. struct MockConnection : AssetProcessor::ConnectionBus::Handler
  437. {
  438. MockConnection(int connectionId)
  439. {
  440. BusConnect(connectionId);
  441. }
  442. ~MockConnection()
  443. {
  444. BusDisconnect();
  445. }
  446. virtual size_t Send([[maybe_unused]] unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message)
  447. {
  448. auto* bulkMessage = azrtti_cast<const BulkAssetNotificationMessage*>(&message);
  449. EXPECT_TRUE(bulkMessage);
  450. EXPECT_EQ(bulkMessage->m_type, AssetNotificationMessage::AssetChanged);
  451. EXPECT_GT(bulkMessage->m_messages.size(), 0);
  452. m_messages += bulkMessage->m_messages.size();
  453. return sizeof(message);
  454. }
  455. virtual size_t SendRaw(unsigned int /*type*/, unsigned int /*serial*/, const QByteArray& /*data*/)
  456. {
  457. GTEST_NONFATAL_FAILURE_("Not supported");
  458. return 0;
  459. }
  460. virtual size_t SendPerPlatform(
  461. unsigned int /*serial*/, const AzFramework::AssetSystem::BaseAssetProcessorMessage& /*message*/, const QString& /*platform*/)
  462. {
  463. GTEST_NONFATAL_FAILURE_("Not supported");
  464. return 0;
  465. }
  466. virtual size_t SendRawPerPlatform(
  467. unsigned int /*type*/, unsigned int /*serial*/, const QByteArray& /*data*/, const QString& /*platform*/)
  468. {
  469. GTEST_NONFATAL_FAILURE_("Not supported");
  470. return 0;
  471. }
  472. virtual unsigned int SendRequest(
  473. const AzFramework::AssetSystem::BaseAssetProcessorMessage& /*message*/, const ResponseCallback& /*callback*/)
  474. {
  475. GTEST_NONFATAL_FAILURE_("Not supported");
  476. return 0;
  477. }
  478. virtual size_t SendResponse(unsigned int /*serial*/, const AzFramework::AssetSystem::BaseAssetProcessorMessage& /*message*/)
  479. {
  480. GTEST_NONFATAL_FAILURE_("Not supported");
  481. return 0;
  482. }
  483. virtual void RemoveResponseHandler(unsigned int /*serial*/)
  484. {
  485. GTEST_NONFATAL_FAILURE_("Not supported");
  486. }
  487. size_t m_messages = 0;
  488. };
  489. TEST_F(AssetCatalogTestWithProducts, SendAssetUpdateOnConnect)
  490. {
  491. static constexpr int ConnId = 1;
  492. AssetNotificationMessage message;
  493. message.m_type = AssetNotificationMessage::AssetChanged;
  494. message.m_data = "filea.png";
  495. message.m_assetId = AZ::Data::AssetId("{4DBBC5A7-ACEE-4084-A435-9CA8AA05B01B}");
  496. message.m_assetType = AZ::Data::AssetType("{01E432B8-4252-40F5-86CC-4CB554004C49}");
  497. message.m_platform = "pc";
  498. message.m_sizeBytes = 10;
  499. // Add 2 assets to the catalog
  500. m_data->m_assetCatalog->OnAssetMessage(message);
  501. message.m_data = "fileb.png";
  502. message.m_assetId = AZ::Data::AssetId("{29AA7E27-4A80-4443-8DFD-6FC459833BD2}");
  503. m_data->m_assetCatalog->OnAssetMessage(message);
  504. // Simulate a connection afterwards
  505. MockConnection mockConnection(ConnId);
  506. MockConnection android(ConnId + 1);
  507. EXPECT_EQ(mockConnection.m_messages, 0);
  508. EXPECT_EQ(android.m_messages, 0);
  509. m_data->m_assetCatalog->OnConnect(ConnId, { "pc" });
  510. // Should recieve both asset messages
  511. EXPECT_EQ(mockConnection.m_messages, 2);
  512. m_data->m_assetCatalog->OnConnect(ConnId + 1, { "android" });
  513. EXPECT_EQ(android.m_messages, 0); // No assets for the android platform
  514. EXPECT_EQ(mockConnection.m_messages, 2); // No extra messages for the pc platform
  515. }
  516. class AssetCatalogTestRelativeSourcePath : public AssetCatalogTest
  517. {
  518. public:
  519. QDir GetRoot()
  520. {
  521. // Return an OS-friendly absolute root directory for our tests ("C:/sourceRoot" or "/sourceRoot"). It doesn't
  522. // need to exist, it just needs to be an absolute path.
  523. return QDir::root().filePath("sourceRoot");
  524. }
  525. // Set up custom scan folders for the "relative source path" tests, so that we can try out specific combinations of watch folders
  526. void AddScanFolders(
  527. [[maybe_unused]] const QDir& tempPath, AssetDatabaseConnection* dbConn, PlatformConfiguration& config,
  528. const AZStd::vector<AssetBuilderSDK::PlatformInfo>& platforms) override
  529. {
  530. QDir root = GetRoot();
  531. // This will set up the following watch folders, in highest to lowest priority:
  532. // /sourceRoot/recurseNested/nested (recurse)
  533. // /sourceRoot/noRecurse (no recurse)
  534. // /sourceRoot/recurseNotNested (recurse)
  535. // /sourceRoot/recurseNested (recurse)
  536. AddScanFolder(
  537. ScanFolderInfo(root.filePath("recurseNested/nested"), "nested", "nested", false, true, platforms, -4), config, dbConn);
  538. AddScanFolder(
  539. ScanFolderInfo(root.filePath("noRecurse"), "noRecurse", "noRecurse", false, false, platforms, -3), config, dbConn);
  540. AddScanFolder(
  541. ScanFolderInfo(root.filePath("recurseNotNested"), "recurseNotNested", "recurseNotNested", false, true, platforms, -2),
  542. config, dbConn);
  543. AddScanFolder(
  544. ScanFolderInfo(root.filePath("recurseNested"), "recurseNested", "recurseNested", false, true, platforms, -1),
  545. config, dbConn);
  546. }
  547. // Calls the GenerateRelativeSourcePath function and validates that the results match the expected inputs.
  548. void TestGetRelativeSourcePath(
  549. const AZStd::string& sourcePath, bool expectedToFind, const AZStd::string& expectedPath, const AZStd::string& expectedRoot)
  550. {
  551. bool relPathFound = false;
  552. AZStd::string relPath;
  553. AZStd::string rootFolder;
  554. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
  555. relPathFound, &AzToolsFramework::AssetSystem::AssetSystemRequest::GenerateRelativeSourcePath, sourcePath,
  556. relPath, rootFolder);
  557. EXPECT_EQ(relPathFound, expectedToFind);
  558. EXPECT_EQ(relPath, expectedPath);
  559. EXPECT_EQ(rootFolder, expectedRoot);
  560. }
  561. };
  562. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_EmptySourcePath_ReturnsNoMatch)
  563. {
  564. // Test passes in an empty source path, which shouldn't produce a valid result.
  565. // Input: empty source path
  566. // Output: empty, not found result
  567. TestGetRelativeSourcePath("", false, "", "");
  568. }
  569. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_AbsolutePathOutsideWatchFolders_ReturnsNoMatch)
  570. {
  571. // Test passes in an invalid absolute source path, which shouldn't produce a valid result.
  572. // Input: "/sourceRoot/noWatchFolder/test.txt"
  573. // Output: not found result, which also returns the input as the relative file name
  574. QDir watchFolder = GetRoot().filePath("noWatchFolder/");
  575. QString fileToCheck = watchFolder.filePath("test.txt");
  576. TestGetRelativeSourcePath(fileToCheck.toUtf8().constData(), false, fileToCheck.toUtf8().constData(), "");
  577. }
  578. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_AbsolutePathUnderWatchFolder_ReturnsRelativePath)
  579. {
  580. // Test passes in a valid absolute source path, which should produce a valid relative path
  581. // Input: "/sourceRoot/noRecurse/test.txt"
  582. // Output: "test.txt" in folder "/sourceRoot/noRecurse/"
  583. QDir watchFolder = GetRoot().filePath("noRecurse/");
  584. QString fileToCheck = watchFolder.filePath("test.txt");
  585. TestGetRelativeSourcePath(fileToCheck.toUtf8().constData(), true, "test.txt", watchFolder.path().toUtf8().constData());
  586. }
  587. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_AbsolutePathUnderNestedWatchFolders_ReturnsRelativePath)
  588. {
  589. // Test passes in a valid absolute source path that matches a watch folder and a nested watch folder.
  590. // The output relative path should match the nested folder, because the nested folder has a higher priority registered with the AP.
  591. // Input: "/sourceRoot/recurseNested/nested/test.txt"
  592. // Output: "test.txt" in folder "/sourceRoot/recurseNested/nested/"
  593. QDir watchFolder = GetRoot().filePath("recurseNested/nested/");
  594. QString fileToCheck = watchFolder.filePath("test.txt");
  595. TestGetRelativeSourcePath(fileToCheck.toUtf8().constData(), true, "test.txt", watchFolder.path().toUtf8().constData());
  596. }
  597. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_BareFileNameValidInWatchFolder_ReturnsHighestPriorityWatchFolder)
  598. {
  599. // Test passes in a simple file name. The output should be relative to the highest-priority watch folder.
  600. // Input: "test.txt"
  601. // Output: "test.txt" in folder "/sourceRoot/recurseNested/nested/"
  602. QDir watchFolder = GetRoot().filePath("recurseNested/nested/");
  603. TestGetRelativeSourcePath("test.txt", true, "test.txt", watchFolder.path().toUtf8().constData());
  604. }
  605. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathValidInWatchFolder_ReturnsHighestPriorityWatchFolder)
  606. {
  607. // Test passes in a relative path. The output should preserve the relative path, but list it as relative to the highest-priority
  608. // watch folder.
  609. // Input: "a/b/c/test.txt"
  610. // Output: "a/b/c/test.txt" in folder "/sourceRoot/recurseNested/nested/"
  611. QDir watchFolder = GetRoot().filePath("recurseNested/nested/");
  612. TestGetRelativeSourcePath("a/b/c/test.txt", true, "a/b/c/test.txt", watchFolder.path().toUtf8().constData());
  613. }
  614. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathNotInWatchFolder_ReturnsNoMatch)
  615. {
  616. // Test passes in a relative path that "backs up" two directories. This will be invalid, because no matter which watch directory
  617. // we start at, the result will be outside of any watch directory.
  618. // Input: "../../test.txt"
  619. // Output: not found result, which also returns the input as the relative file name
  620. TestGetRelativeSourcePath("../../test.txt", false, "../../test.txt", "");
  621. }
  622. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathValidFromNestedWatchFolder_ReturnsOuterFolder)
  623. {
  624. // Test passes in a relative path that "backs up" one directory. This will produce a valid result, because we can back up from
  625. // the "recurseNested/nested/" watch folder to "recurseNested", which is also a valid watch folder.
  626. // Input: "../test.txt"
  627. // Output: "test.txt" in folder "/sourceRoot/recurseNested"
  628. QDir watchFolder = GetRoot().filePath("recurseNested/");
  629. TestGetRelativeSourcePath("../test.txt", true, "test.txt", watchFolder.path().toUtf8().constData());
  630. }
  631. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathMovesToParentWatchFolder_ReturnsOuterFolder)
  632. {
  633. // Test passes in a relative path that backs up one directory and then forward into a directory. This will produce a valid
  634. // result, because it can validly start in the highest-priority watch folder (recurseNested/nested), move back one into the
  635. // outer watch folder (recurseNested), and then have a subdirectory within it.
  636. // Note that it would also be valid to move from recurseNested to recurseNotNested, but that won't be the result of this test
  637. // because that's a lower-priority match.
  638. // Input: "../recurseNotNested/test.txt"
  639. // Output: "recurseNotNested/test.txt" in folder "/sourceRoot/recurseNested/"
  640. QDir watchFolder = GetRoot().filePath("recurseNested/");
  641. TestGetRelativeSourcePath("../recurseNotNested/test.txt", true, "recurseNotNested/test.txt", watchFolder.path().toUtf8().constData());
  642. }
  643. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathMovesToSiblingWatchFolder_ReturnsSiblingFolder)
  644. {
  645. // Test passes in a relative path that backs up two directories and then forward into a directory. This will produce a valid
  646. // result, because it can validly start in the recurseNested/nested folder, move back two folders, then forward into the sibling
  647. // recurseNotNested folder. The result will be a relative path to the sibling folder.
  648. // Input: "../../recurseNotNested/test.txt"
  649. // Output: "test.txt" in folder "/sourceRoot/recurseNotNested/"
  650. QDir watchFolder = GetRoot().filePath("recurseNotNested/");
  651. TestGetRelativeSourcePath("../../recurseNotNested/test.txt", true, "test.txt", watchFolder.path().toUtf8().constData());
  652. }
  653. TEST_F(AssetCatalogTestRelativeSourcePath, GenerateRelativeSourcePath_RelativePathBacksOutOfWatchFolder_ReturnsNoMatch)
  654. {
  655. // Test passes in a relative path that adds a directory, then "backs up" three directories. This will be invalid, because no
  656. // matter which watch directory we start at, the result will be outside of any watch directory.
  657. // Input: "../test.txt"
  658. // Output: "test.txt" in folder "/sourceRoot/recurseNested"
  659. TestGetRelativeSourcePath("a/../../../test.txt", false, "a/../../../test.txt", "");
  660. }
  661. class AssetCatalogTest_GetFullSourcePath
  662. : public AssetCatalogTest
  663. {
  664. public:
  665. void SetUp() override
  666. {
  667. AssetCatalogTest::SetUp();
  668. // ----- Test the ProcessGetFullAssetPath function on product files
  669. {
  670. QStringList pcouts;
  671. pcouts.push_back(m_data->m_cacheRootDir.filePath(QString("pc") + "/subfolder3/randomfileoutput.random"));
  672. pcouts.push_back(m_data->m_cacheRootDir.filePath(QString("pc") + "/subfolder3/randomfileoutput.random1"));
  673. pcouts.push_back(m_data->m_cacheRootDir.filePath(QString("pc") + "/subfolder3/randomfileoutput.random2"));
  674. AZ::s64 jobId;
  675. ASSERT_TRUE(AddSourceAndJob("subfolder3", "somerandomfile.random", &(m_data->m_dbConn), jobId));
  676. AZ::u32 productSubID = 0;
  677. for (auto& product : pcouts)
  678. {
  679. ProductDatabaseEntry newProduct(jobId, productSubID++, m_data->m_cacheRootDir.relativeFilePath(product).toStdString().c_str(), AZ::Data::AssetType::CreateRandom());
  680. ASSERT_TRUE(m_data->m_dbConn.SetProduct(newProduct));
  681. }
  682. }
  683. }
  684. };
  685. TEST_F(AssetCatalogTest_GetFullSourcePath, NormalUsage_ReturnsAbsolutePathToSource)
  686. {
  687. // feed it an relative product, and expect a full, absolute source file path in return.
  688. QString fileToCheck = "subfolder3/randomfileoutput.random1";
  689. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  690. }
  691. TEST_F(AssetCatalogTest_GetFullSourcePath, SecondProduct_ReturnsAbsolutePathToSource)
  692. {
  693. // feed it another relative product from the same source file, expect the same source.
  694. QString fileToCheck = "subfolder3/randomfileoutput.random2";
  695. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  696. }
  697. TEST_F(AssetCatalogTest_GetFullSourcePath, IncorrectSeperators_ReturnsAbsolutePathToSource)
  698. {
  699. //feed it the same relative product with different separators
  700. QString fileToCheck = "subfolder3\\randomfileoutput.random2";
  701. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  702. }
  703. TEST_F(AssetCatalogTest_GetFullSourcePath, SourcePath_ReturnsSourcePath)
  704. {
  705. // feed it a full path to a source file, we expect that path back
  706. QString fileToCheck = m_data->m_assetRootSourceDir.filePath("somefolder/somefile.txt");
  707. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "somefolder/somefile.txt"));
  708. }
  709. TEST_F(AssetCatalogTest_GetFullSourcePath, AliasedCachePath_ReturnsAbsolutePathToSource)
  710. {
  711. //feed it a path with alias and asset id
  712. QString fileToCheck = "@products@/subfolder3/randomfileoutput.random1";
  713. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  714. }
  715. TEST_F(AssetCatalogTest_GetFullSourcePath, InvalidAlias_ReturnsAbsolutePathToSource)
  716. {
  717. //feed it a path with some random alias and asset id
  718. QString fileToCheck = "@somerandomalias@/subfolder3/randomfileoutput.random1";
  719. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  720. }
  721. TEST_F(AssetCatalogTest_GetFullSourcePath, InvalidAliasMissingSeperator_ReturnsAbsolutePathToSource)
  722. {
  723. //feed it a path with some random alias and asset id but no separator
  724. QString fileToCheck = "@somerandomalias@subfolder3/randomfileoutput.random1";
  725. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  726. }
  727. TEST_F(AssetCatalogTest_GetFullSourcePath, InvalidSourcePathContainingCacheAlias_ReturnsAbsolutePathToSource)
  728. {
  729. //feed it a path with alias and input name
  730. QString fileToCheck = "@products@/somerandomfile.random";
  731. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  732. }
  733. TEST_F(AssetCatalogTest_GetFullSourcePath, AbsolutePathToCache_ReturnsAbsolutePathToSource)
  734. {
  735. //feed it an absolute path with cacheroot
  736. QString fileToCheck = m_data->m_cacheRootDir.filePath(QString("pc") + "/subfolder3/randomfileoutput.random1");
  737. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  738. }
  739. TEST_F(AssetCatalogTest_GetFullSourcePath, ProductNameIncludingPlatformAndGameName_ReturnsAbsolutePathToSource)
  740. {
  741. //feed it a productName directly
  742. QString fileToCheck = QString("pc") + "/subfolder3/randomfileoutput.random1";
  743. EXPECT_TRUE(TestGetFullSourcePath(fileToCheck, m_data->m_assetRootSourceDir, true, "subfolder3/somerandomfile.random"));
  744. }
  745. class AssetCatalogTest_AssetInfo
  746. : public AssetCatalogTest
  747. {
  748. public:
  749. struct AssetCatalogTest_AssetInfo_DataMembers
  750. {
  751. AssetId m_assetA = AssetId(Uuid::CreateRandom(), 0);
  752. Uuid m_assetALegacyUuid = Uuid::CreateRandom();
  753. AssetType m_assetAType = AssetType::CreateRandom();
  754. AZStd::string m_assetAFileFilter = "*.source";
  755. AZStd::string m_subfolder1AbsolutePath;
  756. AZStd::string m_assetASourceRelPath = "assetA.source";
  757. AZStd::string m_assetAProductRelPath = "assetA.product";
  758. AZStd::string m_assetAFullPath;
  759. AZStd::string m_assetAProductFullPath;
  760. AZStd::string m_assetTestString = "Its the Asset A";
  761. AZStd::string m_productTestString = "Its a product A";
  762. UnitTests::MockVirtualFileIO m_virtualFileIO;
  763. AzToolsFramework::UuidUtilComponent m_uuidUtil;
  764. AzToolsFramework::MetadataManager m_metadataManager;
  765. AssetProcessor::UuidManager m_uuidManager;
  766. };
  767. AssetCatalogTest_AssetInfo_DataMembers* m_customDataMembers = nullptr;
  768. void SetUp() override
  769. {
  770. AssetCatalogTest::SetUp();
  771. m_customDataMembers = azcreate(AssetCatalogTest_AssetInfo_DataMembers, ());
  772. m_customDataMembers->m_subfolder1AbsolutePath = m_data->m_assetRootSourceDir.absoluteFilePath("subfolder1").toStdString().c_str();
  773. AzFramework::StringFunc::Path::Join(m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_assetAFullPath);
  774. // since we use the mock virtual fileio, we must use the AZ version that creates mock files
  775. CreateDummyFileAZ(m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetTestString.c_str());
  776. AzFramework::StringFunc::Path::Join(m_data->m_cacheRootDir.absolutePath().toUtf8().constData(), m_customDataMembers->m_assetAProductRelPath.c_str(), m_customDataMembers->m_assetAProductFullPath);
  777. CreateDummyFileAZ(m_customDataMembers->m_assetAProductFullPath.c_str(), m_customDataMembers->m_productTestString.c_str());
  778. }
  779. bool GetAssetInfoById(bool expectedResult, AZStd::string expectedRelPath, AZStd::string expectedRootPath, AssetType assetType)
  780. {
  781. bool result = false;
  782. AssetInfo assetInfo;
  783. AZStd::string rootPath;
  784. const AZStd::string platformName = ""; // Empty for default
  785. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(result, &AssetSystemRequest::GetAssetInfoById, m_customDataMembers->m_assetA, assetType, platformName, assetInfo, rootPath);
  786. if (result != expectedResult)
  787. {
  788. return false;
  789. }
  790. if (expectedResult)
  791. {
  792. EXPECT_EQ(assetInfo.m_assetId, m_customDataMembers->m_assetA);
  793. EXPECT_EQ(assetInfo.m_assetType, m_customDataMembers->m_assetAType);
  794. EXPECT_EQ(assetInfo.m_relativePath, expectedRelPath);
  795. EXPECT_EQ(assetInfo.m_sizeBytes, m_customDataMembers->m_assetTestString.size());
  796. EXPECT_EQ(rootPath, expectedRootPath);
  797. return (assetInfo.m_assetId == m_customDataMembers->m_assetA)
  798. && (assetInfo.m_assetType == m_customDataMembers->m_assetAType)
  799. && (assetInfo.m_relativePath == expectedRelPath)
  800. && (assetInfo.m_sizeBytes == m_customDataMembers->m_assetTestString.size())
  801. && (rootPath == expectedRootPath);
  802. }
  803. return true;
  804. };
  805. bool GetAssetInfoByIdPair(bool expectedResult, AZStd::string expectedRelPath, AZStd::string expectedRootPath)
  806. {
  807. // First test without providing the assetType
  808. bool result = GetAssetInfoById(expectedResult, expectedRelPath, expectedRootPath, AssetType::CreateNull());
  809. // If successful, test again, this time providing the assetType
  810. if (result)
  811. {
  812. result = GetAssetInfoById(expectedResult, expectedRelPath, expectedRootPath, m_customDataMembers->m_assetAType);
  813. }
  814. return result;
  815. };
  816. bool GetSourceInfoBySourcePath(
  817. bool expectedResult,
  818. const AZStd::string& sourcePath,
  819. const AZ::Uuid& expectedUuid,
  820. const AZStd::string& expectedRelPath,
  821. const AZStd::string& expectedRootPath,
  822. const AZ::Data::AssetType& expectedAssetType = AZ::Data::s_invalidAssetType) // sources will have an asset type if registered using AzToolsFramework::ToolsAssetSystemRequests::RegisterSourceAssetType
  823. {
  824. bool result = false;
  825. AssetInfo assetInfo;
  826. AZStd::string rootPath;
  827. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(result, &AssetSystemRequest::GetSourceInfoBySourcePath, sourcePath.c_str(), assetInfo, rootPath);
  828. if (result != expectedResult)
  829. {
  830. return false;
  831. }
  832. if (expectedResult)
  833. {
  834. return (assetInfo.m_assetId == expectedUuid)
  835. && (assetInfo.m_assetType == expectedAssetType)
  836. && (assetInfo.m_relativePath == expectedRelPath)
  837. && (assetInfo.m_sizeBytes == m_customDataMembers->m_assetTestString.size())
  838. && (rootPath == expectedRootPath);
  839. }
  840. return true;
  841. };
  842. void TearDown() override
  843. {
  844. azdestroy(m_customDataMembers);
  845. AssetCatalogTest::TearDown();
  846. }
  847. };
  848. TEST_F(AssetCatalogTest_AssetInfo, Sanity_InvalidCalls)
  849. {
  850. //Test 1: Asset not in database
  851. EXPECT_TRUE(GetAssetInfoByIdPair(false, "", ""));
  852. EXPECT_TRUE(GetSourceInfoBySourcePath(false, "", AZ::Uuid::CreateNull(), "", ""));
  853. }
  854. TEST_F(AssetCatalogTest_AssetInfo, Sanity_InvalidPath)
  855. {
  856. auto* ebus = AzToolsFramework::AssetSystemRequestBus::FindFirstHandler();
  857. AZ::Data::AssetInfo assetInfo;
  858. AZStd::string watchFolder;
  859. EXPECT_FALSE(ebus->GetSourceInfoBySourcePath("G:/random/folder/does/not/exist.png", assetInfo, watchFolder)); // Absolute path
  860. EXPECT_FALSE(ebus->GetSourceInfoBySourcePath("random/folder/does/not/exist.png", assetInfo, watchFolder)); // Relative path
  861. }
  862. TEST_F(AssetCatalogTest_AssetInfo, FindAssetNotRegisteredAsSource_FindsProduct)
  863. {
  864. // Setup: Add asset to database
  865. AZ::s64 jobId;
  866. EXPECT_TRUE(AddSourceAndJob("subfolder1", m_customDataMembers->m_assetASourceRelPath.c_str(), &(m_data->m_dbConn), jobId, m_customDataMembers->m_assetA.m_guid));
  867. ProductDatabaseEntry newProductEntry(jobId, 0, m_customDataMembers->m_assetAProductRelPath.c_str(), m_customDataMembers->m_assetAType);
  868. m_data->m_dbConn.SetProduct(newProductEntry);
  869. // Test 2: Asset in database, not registered as source asset
  870. // note that when asking for products, a performance improvement causes the catalog to use its REGISTRY
  871. // rather than the database to ask for products, so to set this up the registry must be present and must have the asset registered within it
  872. AzFramework::AssetSystem::AssetNotificationMessage message(m_customDataMembers->m_assetAProductRelPath.c_str(), AssetNotificationMessage::AssetChanged, m_customDataMembers->m_assetAType, "pc");
  873. message.m_sizeBytes = m_customDataMembers->m_productTestString.size();
  874. message.m_assetId = AZ::Data::AssetId(m_customDataMembers->m_assetA.m_guid, 0);
  875. message.m_platform = "pc";
  876. m_data->m_assetCatalog->OnAssetMessage(message);
  877. // also of note: When looking up products, you don't get a root path since they are all in the cache.
  878. // its important here that we specifically get an empty root path.
  879. EXPECT_TRUE(GetAssetInfoByIdPair(true, m_customDataMembers->m_assetAProductRelPath, ""));
  880. // this call has to work with both full and relative path.
  881. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  882. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  883. }
  884. TEST_F(AssetCatalogTest_AssetInfo, FindAssetInBuildQueue_FindsSource)
  885. {
  886. // Setup: Add a source to queue.
  887. m_data->m_assetCatalog->OnSourceQueued(
  888. m_customDataMembers->m_assetA.m_guid,
  889. { m_customDataMembers->m_assetALegacyUuid },
  890. AssetProcessor::SourceAssetReference(
  891. m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetASourceRelPath.c_str()));
  892. // TEST: Asset in queue, not registered as source asset
  893. EXPECT_TRUE(GetAssetInfoByIdPair(false, "", ""));
  894. // this call should STILL work even after the above call to "OnSourceQueued" since its explicitly asking for the source details.
  895. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  896. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  897. }
  898. TEST_F(AssetCatalogTest_AssetInfo, FindAssetInBuildQueue_RegisteredAsSourceType_StillFindsSource)
  899. {
  900. // Setup: Add a source to queue.
  901. m_data->m_assetCatalog->OnSourceQueued(
  902. m_customDataMembers->m_assetA.m_guid,
  903. { m_customDataMembers->m_assetALegacyUuid },
  904. AssetProcessor::SourceAssetReference(
  905. m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetASourceRelPath.c_str()));
  906. // Register as source type
  907. AzToolsFramework::ToolsAssetSystemBus::Broadcast(&AzToolsFramework::ToolsAssetSystemRequests::RegisterSourceAssetType, m_customDataMembers->m_assetAType, m_customDataMembers->m_assetAFileFilter.c_str());
  908. // Test: Asset in queue, registered as source asset
  909. EXPECT_TRUE(GetAssetInfoByIdPair(true, m_customDataMembers->m_assetASourceRelPath, m_customDataMembers->m_subfolder1AbsolutePath));
  910. // these calls are identical to the two in the prior test, but should continue to work even though we have registered the asset type as a source asset type.
  911. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  912. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  913. }
  914. TEST_F(AssetCatalogTest_AssetInfo, FindSource_FinishedProcessing_RegisteredAsSource_FindsSource)
  915. {
  916. // Register as source type
  917. AzToolsFramework::ToolsAssetSystemBus::Broadcast(&AzToolsFramework::ToolsAssetSystemRequests::RegisterSourceAssetType, m_customDataMembers->m_assetAType, m_customDataMembers->m_assetAFileFilter.c_str());
  918. // Setup: Add a source to queue, then notify its finished and add it to the database (simulates a full pipeline)
  919. AZ::s64 jobId;
  920. EXPECT_TRUE(AddSourceAndJob("subfolder1", m_customDataMembers->m_assetASourceRelPath.c_str(), &(m_data->m_dbConn), jobId, m_customDataMembers->m_assetA.m_guid));
  921. m_data->m_assetCatalog->OnSourceQueued(
  922. m_customDataMembers->m_assetA.m_guid,
  923. { m_customDataMembers->m_assetALegacyUuid },
  924. AssetProcessor::SourceAssetReference(
  925. m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetASourceRelPath.c_str()));
  926. m_data->m_assetCatalog->OnSourceFinished(m_customDataMembers->m_assetA.m_guid, { m_customDataMembers->m_assetALegacyUuid });
  927. ProductDatabaseEntry assetAEntry(jobId, 0, m_customDataMembers->m_assetAProductRelPath.c_str(), m_customDataMembers->m_assetAType);
  928. m_data->m_dbConn.SetProduct(assetAEntry);
  929. // TEST: Asset in database, registered as source asset
  930. EXPECT_TRUE(GetAssetInfoByIdPair(true, m_customDataMembers->m_assetASourceRelPath, m_customDataMembers->m_subfolder1AbsolutePath));
  931. // at this point the details about the asset in question is no longer in memory, only the database. However, these calls should continue find the
  932. // information, because the system is supposed check both the database AND the in-memory queue in the to find the info being requested.
  933. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  934. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetA.m_guid, m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  935. }
  936. TEST_F(AssetCatalogTest_AssetInfo, FindSource_NotProcessed_NotInQueue_FindsSource)
  937. {
  938. EXPECT_TRUE(UnitTestUtils::CreateDummyFileAZ(m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetTestString.c_str()));
  939. // Get accurate UUID based on source database name instead of using the one that was randomly generated
  940. auto expectedSourceUuid = AssetUtilities::GetSourceUuid(SourceAssetReference(m_customDataMembers->m_assetAFullPath.c_str()));
  941. ASSERT_TRUE(expectedSourceUuid);
  942. // These calls should find the information even though the asset is not in the database and hasn't been queued up yet
  943. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), expectedSourceUuid.GetValue(), m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  944. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), expectedSourceUuid.GetValue(), m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str()));
  945. }
  946. TEST_F(AssetCatalogTest_AssetInfo, FindSource_NotProcessed_NotInQueue_RegisteredAsSourceType_FindsSource)
  947. {
  948. EXPECT_TRUE(UnitTestUtils::CreateDummyFileAZ(
  949. m_customDataMembers->m_assetAFullPath.c_str(), m_customDataMembers->m_assetTestString.c_str()));
  950. // Get accurate UUID based on source database name instead of using the one that was randomly generated
  951. auto expectedSourceUuid = AssetUtilities::GetSourceUuid(SourceAssetReference(m_customDataMembers->m_assetAFullPath.c_str()));
  952. ASSERT_TRUE(expectedSourceUuid);
  953. // Register as source type
  954. AzToolsFramework::ToolsAssetSystemBus::Broadcast(&AzToolsFramework::ToolsAssetSystemRequests::RegisterSourceAssetType, m_customDataMembers->m_assetAType, m_customDataMembers->m_assetAFileFilter.c_str());
  955. // These calls should find the information even though the asset is not in the database and hasn't been queued up yet
  956. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetASourceRelPath.c_str(), expectedSourceUuid.GetValue(), m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  957. EXPECT_TRUE(GetSourceInfoBySourcePath(true, m_customDataMembers->m_assetAFullPath.c_str(), expectedSourceUuid.GetValue(), m_customDataMembers->m_assetASourceRelPath.c_str(), m_customDataMembers->m_subfolder1AbsolutePath.c_str(), m_customDataMembers->m_assetAType));
  958. }
  959. // this test can take an extremely long time due to its number of assets x number of iterations.
  960. // keep it in the periodic suite.
  961. TEST_F(AssetCatalogTest, Multithread_AccessCatalogWhileInitializing_IsThreadSafe_SUITE_periodic)
  962. {
  963. static constexpr int NumTestAssets = 100;
  964. static constexpr int NumUpdateIterations = 100;
  965. using namespace AssetProcessor;
  966. using namespace AzFramework::AssetSystem;
  967. PlatformConfiguration config;
  968. config.EnablePlatform(AssetBuilderSDK::PlatformInfo("pc", { "test" }));
  969. {
  970. using namespace AzToolsFramework::AssetDatabase;
  971. auto& db = m_data->m_dbConn;
  972. for (int i = 0; i < NumTestAssets; ++i)
  973. {
  974. SourceAssetReference sourceAsset(1, AZStd::to_string(i).c_str());
  975. UnitTestUtils::CreateDummyFileAZ(sourceAsset.AbsolutePath().c_str());
  976. SourceDatabaseEntry sourceEntry;
  977. sourceEntry.m_sourceName = sourceAsset.RelativePath().c_str();
  978. sourceEntry.m_scanFolderPK = sourceAsset.ScanFolderId();
  979. sourceEntry.m_sourceGuid = AssetUtilities::GetSourceUuid(sourceAsset).GetValueOr(AZ::Uuid());
  980. db.SetSource(sourceEntry);
  981. JobDatabaseEntry jobEntry;
  982. jobEntry.m_sourcePK = sourceEntry.m_sourceID;
  983. jobEntry.m_platform = "pc";
  984. jobEntry.m_jobRunKey = i + 1;
  985. db.SetJob(jobEntry);
  986. ProductDatabaseEntry productEntry;
  987. productEntry.m_jobPK = jobEntry.m_jobID;
  988. productEntry.m_productName = AZStd::to_string(i) + ".product";
  989. db.SetProduct(productEntry);
  990. }
  991. }
  992. AZStd::thread_desc threadDesc;
  993. threadDesc.m_name = "AssetCatalog Thread";
  994. AZStd::thread catalogThread(threadDesc, [this]()
  995. {
  996. m_data->m_assetCatalog->BuildRegistry();
  997. }
  998. );
  999. AssetNotificationMessage message("some/path/image.png", AssetNotificationMessage::NotificationType::AssetChanged, AZ::Data::AssetType::CreateRandom(), "pc");
  1000. message.m_assetId = AZ::Data::AssetId{ "{C1A73521-E770-475F-8D91-30DF88E4D4C9}" };
  1001. for (int i = 0; i < NumUpdateIterations; ++i)
  1002. {
  1003. m_data->m_assetCatalog->OnAssetMessage(message);
  1004. }
  1005. catalogThread.join();
  1006. }
  1007. class AssetCatalogTestForProductDependencies
  1008. : public AssetCatalogTest
  1009. {
  1010. public:
  1011. void SetUp() override
  1012. {
  1013. AssetCatalogTest::SetUp();
  1014. m_platforms.push_back("pc");
  1015. m_platforms.push_back("android");
  1016. // 4 products for one platform, 1 product for the other.
  1017. m_platformToProductsForSourceWithDifferentProducts["pc"].push_back("subfolder3/basefilez.arc2");
  1018. m_platformToProductsForSourceWithDifferentProducts["pc"].push_back("subfolder3/basefileaz.azm2");
  1019. m_platformToProductsForSourceWithDifferentProducts["pc"].push_back("subfolder3/basefile.arc2");
  1020. m_platformToProductsForSourceWithDifferentProducts["pc"].push_back("subfolder3/basefile.azm2");
  1021. m_platformToProductsForSourceWithDifferentProducts["android"].push_back("subfolder3/androidexclusivefile.azm2");
  1022. m_sourceFileWithDifferentProductsPerPlatform = AZ::Uuid::CreateString("{38032FC9-2838-4D6A-9DA0-79E5E4F20C1B}");
  1023. m_sourceFileWithDependency = AZ::Uuid::CreateString("{807C4174-1D19-42AD-B8BC-A59291D9388C}");
  1024. // Setup:
  1025. // 2 source files: MultiplatformFile.txt and FileWithDependency.txt.
  1026. // MultiplatformFile.txt has different products on different platforms.
  1027. // FileWithDependency.txt has the same product on each platform, but these products have different product dependencies per platform.
  1028. // This setup is meant to mimic a pattern we've seen with materials and mipmaps: Mipmap generation settings can be different per platform,
  1029. // resulting in image processing jobs having different products per platform. Because of this, the material jobs will then have different
  1030. // dependencies per platform, because each material will depend on a referenced texture and all of that texture's mipmaps.
  1031. // Add a source file with 4 products on pc, but 1 on android
  1032. bool result = AddSourceAndJobForMultiplePlatforms(
  1033. "subfolder3",
  1034. "MultiplatformFile.txt",
  1035. &(m_data->m_dbConn),
  1036. m_sourceFileWithDifferentProductsJobsPerPlatform,
  1037. m_platforms,
  1038. m_sourceFileWithDifferentProductsPerPlatform);
  1039. EXPECT_TRUE(result);
  1040. // Add a source file with 1 product on each platform, that has different dependencies per platform.
  1041. AZStd::map<AZStd::string, AZ::s64> sourceFileWithSameProductsJobsPerPlatform;
  1042. result = AddSourceAndJobForMultiplePlatforms("subfolder3", "FileWithDependency.txt", &(m_data->m_dbConn), sourceFileWithSameProductsJobsPerPlatform, m_platforms, m_sourceFileWithDependency);
  1043. EXPECT_TRUE(result);
  1044. const AZStd::string fileWithDependencyProductPath = "subfolder3/androidexclusivefile.azm2";
  1045. for (const AZStd::string& platform : m_platforms)
  1046. {
  1047. m_platformToSourceIdToProductIds[platform][m_sourceFileWithDependency].push_back(
  1048. CreateProductAndGetProductId(sourceFileWithSameProductsJobsPerPlatform[platform], fileWithDependencyProductPath, 0));
  1049. }
  1050. }
  1051. void CreateProducts()
  1052. {
  1053. for (const AZStd::string& platform : m_platforms)
  1054. {
  1055. AZ::u32 productSubId = 0;
  1056. for (const auto& relativeProductPath : m_platformToProductsForSourceWithDifferentProducts[platform])
  1057. {
  1058. m_sourceWithMultipleProductsPlatformToProductIds[platform].push_back(
  1059. CreateProductAndGetProductId(m_sourceFileWithDifferentProductsJobsPerPlatform[platform], relativeProductPath, productSubId++));
  1060. }
  1061. }
  1062. }
  1063. // Containers are stored in unique pointers so they can be destroyed in teardown before the allocators are freed.
  1064. AZStd::vector<AZStd::string> m_platforms;
  1065. AZStd::map<AZStd::string, AZStd::vector<AZStd::string>> m_platformToProductsForSourceWithDifferentProducts;
  1066. AZ::Uuid m_sourceFileWithDifferentProductsPerPlatform;
  1067. AZ::Uuid m_sourceFileWithDependency;
  1068. AZStd::map<AZStd::string, AZ::s64> m_sourceFileWithDifferentProductsJobsPerPlatform;
  1069. AZStd::map<AZStd::string, AZStd::map<AZ::Uuid, AZStd::vector<AZ::s64>>> m_platformToSourceIdToProductIds;
  1070. AZStd::map<AZStd::string, AZStd::vector<AZ::s64>> m_sourceWithMultipleProductsPlatformToProductIds;
  1071. };
  1072. TEST_F(AssetCatalogTestForProductDependencies, SaveCatalog_DifferentDependenciesPerPlatform_CorrectDependenciesSavedToCatalog)
  1073. {
  1074. CreateProducts();
  1075. for (const AZStd::string& platform : m_platforms)
  1076. {
  1077. AZ::s64 productIdForPlatform = m_platformToSourceIdToProductIds[platform][m_sourceFileWithDependency][0];
  1078. for (AZ::s64 subIdAndProductIndex : m_sourceWithMultipleProductsPlatformToProductIds[platform])
  1079. {
  1080. // SubId matches index.
  1081. ProductDependencyDatabaseEntry productDependency(
  1082. productIdForPlatform,
  1083. m_sourceFileWithDifferentProductsPerPlatform,
  1084. aznumeric_cast<AZ::u32>(subIdAndProductIndex),
  1085. /*dependencyFlags*/ 0,
  1086. platform,
  1087. true);
  1088. bool result = m_data->m_dbConn.SetProductDependency(productDependency);
  1089. EXPECT_TRUE(result);
  1090. // Don't need to cache anything at this point, the dependency ID isn't tracked in the catalog.
  1091. }
  1092. }
  1093. m_data->m_assetCatalog->BuildRegistry();
  1094. // Verify that the dependencies are correct.
  1095. // Without the bug fix to AssetCatalog, every platform's registry included the dependencies for all other platforms.
  1096. const AZ::Data::AssetId productWithDependency(m_sourceFileWithDependency, 0);
  1097. for (const AZStd::string& platform : m_platforms)
  1098. {
  1099. AzFramework::AssetRegistry& registry = m_data->m_assetCatalog->GetRegistry(platform.c_str());
  1100. EXPECT_EQ(registry.m_assetDependencies[productWithDependency].size(), m_sourceWithMultipleProductsPlatformToProductIds[platform].size());
  1101. }
  1102. }
  1103. TEST_F(AssetCatalogTestForProductDependencies, SaveCatalog_DifferentDependenciesPerPlatformResolvedFromPaths_CorrectDependenciesSavedToCatalog)
  1104. {
  1105. // Setup:
  1106. // 2 source files: MultiplatformFile.txt and FileWithDependency.txt.
  1107. // MultiplatformFile.txt has different products on different platforms.
  1108. // FileWithDependency.txt has the same product on each platform, but these products have different product dependencies per platform.
  1109. // FileWithDependency.txt initially emits dependencies as path dependencies, which are resolved later into asset IDs.
  1110. // This test differs from the previous test in that it forces OnDependencyResolved to be called, which is where we've seen
  1111. // bugs in the past related to the asset catalog.
  1112. // Set up the path dependencies.
  1113. AZStd::vector<ProductDependencyDatabaseEntry> productDependencies;
  1114. for (const AZStd::string& platform : m_platforms)
  1115. {
  1116. AZ::s64 productIdForPlatform = m_platformToSourceIdToProductIds[platform][m_sourceFileWithDependency][0];
  1117. for (const auto& relativeProductPath : m_platformToProductsForSourceWithDifferentProducts[platform])
  1118. {
  1119. // SubId matches index.
  1120. ProductDependencyDatabaseEntry productDependency(
  1121. productIdForPlatform,
  1122. AZ::Uuid::CreateNull(),
  1123. /*subId*/ 0,
  1124. /*dependencyFlags*/ 0,
  1125. platform,
  1126. false,
  1127. relativeProductPath);
  1128. bool result = m_data->m_dbConn.SetProductDependency(productDependency);
  1129. EXPECT_TRUE(result);
  1130. productDependencies.push_back(productDependency);
  1131. }
  1132. }
  1133. // Create the products that match the path dependencies.
  1134. CreateProducts();
  1135. // Resolve the path dependencies.
  1136. AZStd::map<AZStd::string, size_t> platformToProductIdIndex;
  1137. for (const AZStd::string& platform : m_platforms)
  1138. {
  1139. platformToProductIdIndex[platform] = 0;
  1140. }
  1141. QDir cacheRoot;
  1142. EXPECT_TRUE(AssetUtilities::ComputeProjectCacheRoot(cacheRoot));
  1143. for (ProductDependencyDatabaseEntry& productDependency : productDependencies)
  1144. {
  1145. // These were generated in this same order previously, but it also doesn't
  1146. // matter to this test which dependencies are upgraded from paths to asset ID,
  1147. // what matters is calling AssetCatalog::OnDependencyResolved to replace paths with asset IDs.
  1148. AZ::s64 subId = m_sourceWithMultipleProductsPlatformToProductIds[productDependency.m_platform][platformToProductIdIndex[productDependency.m_platform]];
  1149. platformToProductIdIndex[productDependency.m_platform]++;
  1150. productDependency.m_dependencySubID = aznumeric_cast<AZ::u32>(subId);
  1151. productDependency.m_dependencySourceGuid = m_sourceFileWithDifferentProductsPerPlatform;
  1152. productDependency.m_unresolvedPath = AZStd::string();
  1153. QString platformGameDir = QDir(cacheRoot.absoluteFilePath(productDependency.m_platform.c_str())).filePath(AssetUtilities::ComputeProjectName().toLower());
  1154. QString assetCatalogFile = QDir(platformGameDir).filePath("assetcatalog.xml");
  1155. QFileInfo fileInfo(assetCatalogFile);
  1156. EXPECT_FALSE(fileInfo.exists());
  1157. m_data->m_assetCatalog->OnDependencyResolved(m_sourceFileWithDependency, productDependency);
  1158. // process all events
  1159. QCoreApplication::processEvents(QEventLoop::AllEvents);
  1160. // This ensures that no save catalog event was queued when we resolve dependency
  1161. EXPECT_FALSE(fileInfo.exists());
  1162. }
  1163. // Verify the catalog is correct.
  1164. m_data->m_assetCatalog->BuildRegistry();
  1165. // Verify that the dependencies are correct.
  1166. // Without the bug fix to AssetCatalog, every platform's registry included the dependencies for all other platforms.
  1167. const AZ::Data::AssetId productWithDependency(m_sourceFileWithDependency, 0);
  1168. for (const AZStd::string& platform : m_platforms)
  1169. {
  1170. AzFramework::AssetRegistry& registry = m_data->m_assetCatalog->GetRegistry(platform.c_str());
  1171. EXPECT_EQ(registry.m_assetDependencies[productWithDependency].size(), m_sourceWithMultipleProductsPlatformToProductIds[platform].size());
  1172. }
  1173. EXPECT_TRUE(true);
  1174. }
  1175. } // namespace AssetProcessor