tests_main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <AzTest/AzTest.h>
  9. #include <AzCore/Memory/OSAllocator.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  12. #include <AzCore/std/containers/vector.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzFramework/Asset/AssetBundleManifest.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <AzToolsFramework/Application/ToolsApplication.h>
  17. #include <AzToolsFramework/AssetBundle/AssetBundleAPI.h>
  18. #include <AzToolsFramework/AssetBundle/AssetBundleComponent.h>
  19. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  20. #include <AzCore/UnitTest/TestTypes.h>
  21. #include <AzCore/Debug/TraceMessageBus.h>
  22. #include <AzCore/UserSettings/UserSettingsComponent.h>
  23. class AssetBundleComponentTests
  24. : public UnitTest::LeakDetectionFixture,
  25. public AZ::Debug::TraceMessageBus::Handler
  26. {
  27. public:
  28. const char* sourcePakPath = "dir1/dir2/some_test_pak.pak";
  29. AZStd::vector<AZStd::string> fileEntriesHasCatalog;
  30. AZStd::vector<AZStd::string> fileEntriesNoCatalog;
  31. AZStd::string catalogPath;
  32. AzToolsFramework::ToolsApplication app;
  33. using AssetBundleCommandsBus = AzToolsFramework::AssetBundleCommandsBus;
  34. AZStd::string CreateCatalogPrefix() const
  35. {
  36. return AzToolsFramework::AssetBundleComponent::DeltaCatalogName;
  37. }
  38. protected:
  39. void SetUp() override
  40. {
  41. AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
  42. auto projectPathKey =
  43. AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_path";
  44. AZ::IO::FixedMaxPath enginePath;
  45. registry->Get(enginePath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  46. registry->Set(projectPathKey, (enginePath / "AutomatedTesting").Native());
  47. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*registry);
  48. AZ::ComponentApplication::Descriptor desc;
  49. desc.m_useExistingAllocator = true;
  50. app.Start(desc);
  51. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  52. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  53. // in the unit tests.
  54. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  55. catalogPath = AZStd::string::format("%s.111111.xml", CreateCatalogPrefix().c_str());
  56. // normalize paths before inserting them in the containers
  57. AZStd::string sourcePak(sourcePakPath);
  58. AzFramework::StringFunc::Path::Normalize(sourcePak);
  59. fileEntriesHasCatalog.push_back(sourcePak);
  60. AzFramework::StringFunc::Path::Normalize(catalogPath);
  61. fileEntriesHasCatalog.push_back(catalogPath);
  62. fileEntriesHasCatalog.push_back(AzFramework::AssetBundleManifest::s_manifestFileName);
  63. AZStd::string firstDummyPath("basePath/somePath1");
  64. AzFramework::StringFunc::Path::Normalize(firstDummyPath);
  65. fileEntriesHasCatalog.emplace_back(firstDummyPath);
  66. AZStd::string secondDummyPath("somePath2");
  67. AzFramework::StringFunc::Path::Normalize(secondDummyPath);
  68. fileEntriesHasCatalog.emplace_back(secondDummyPath);
  69. fileEntriesNoCatalog.push_back(sourcePak);
  70. fileEntriesNoCatalog.emplace_back(firstDummyPath);
  71. fileEntriesNoCatalog.emplace_back(secondDummyPath);
  72. }
  73. bool OnPreError([[maybe_unused]] const char* window, [[maybe_unused]] const char* fileName, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* message) override
  74. {
  75. return true;
  76. }
  77. void TearDown() override
  78. {
  79. app.Stop();
  80. }
  81. };
  82. TEST_F(AssetBundleComponentTests, HasManifest_ManifestInBundle_ExpectTrue)
  83. {
  84. AZStd::vector<AZStd::string> fileEntries;
  85. fileEntries.push_back(AzFramework::AssetBundleManifest::s_manifestFileName);
  86. EXPECT_TRUE(AzToolsFramework::AssetBundleComponent::HasManifest(fileEntries));
  87. }
  88. TEST_F(AssetBundleComponentTests, HasManifest_ManifestNotInBundle_ExpectFalse)
  89. {
  90. AZStd::vector<AZStd::string> fileEntries;
  91. fileEntries.push_back("randomString");
  92. EXPECT_FALSE(AzToolsFramework::AssetBundleComponent::HasManifest(fileEntries));
  93. }
  94. TEST_F(AssetBundleComponentTests, RemoveNonAssetEntries_HasManifest_NotFound)
  95. {
  96. AZStd::string normalizedSourcePakPath = sourcePakPath;
  97. AzFramework::StringFunc::Path::Normalize(normalizedSourcePakPath);
  98. AzFramework::AssetBundleManifest manifest;
  99. manifest.SetCatalogName(AZStd::string::format("%s.111111.xml", CreateCatalogPrefix().c_str()));
  100. bool result = AzToolsFramework::AssetBundleComponent::RemoveNonAssetFileEntries(fileEntriesHasCatalog, normalizedSourcePakPath, &manifest);
  101. EXPECT_TRUE(result);
  102. // check to make sure that sourcePakPath doesn't exist in fileEntriesHasCatalog
  103. auto itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), normalizedSourcePakPath);
  104. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  105. // check to make sure that manifest doesn't exist in fileEntriesHasCatalog
  106. itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), AZStd::string(AzFramework::AssetBundleManifest::s_manifestFileName));
  107. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  108. // check to make sure that the catalog doesn't exist in fileEntriesHasCatalog
  109. itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), manifest.GetCatalogName());
  110. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  111. }
  112. TEST_F(AssetBundleComponentTests, RemoveNonAssetEntries_HasManifestCatalog_FailedToFindCatalog)
  113. {
  114. AZStd::string normalizedSourcePakPath = sourcePakPath;
  115. AzFramework::StringFunc::Path::Normalize(normalizedSourcePakPath);
  116. AzFramework::AssetBundleManifest manifest;
  117. manifest.SetCatalogName(AZStd::string::format("%s.22222.xml", CreateCatalogPrefix().c_str()));
  118. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  119. bool result = AzToolsFramework::AssetBundleComponent::RemoveNonAssetFileEntries(fileEntriesHasCatalog, normalizedSourcePakPath, &manifest);
  120. EXPECT_FALSE(result);
  121. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  122. // check to make sure that sourcePakPath doesn't exist in fileEntriesHasCatalog
  123. auto itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), normalizedSourcePakPath);
  124. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  125. // check to make sure that manifest doesn't exist in fileEntriesHasCatalog
  126. itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), AZStd::string(AzFramework::AssetBundleManifest::s_manifestFileName));
  127. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  128. // check to make sure that the catalog doesn't exist in
  129. itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), manifest.GetCatalogName());
  130. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  131. }
  132. TEST_F(AssetBundleComponentTests, RemoveNonAssetEntries_PakAssetEntryWasRemoved_Success)
  133. {
  134. AZStd::string normalizedSourcePakPath = sourcePakPath;
  135. AzFramework::StringFunc::Path::Normalize(normalizedSourcePakPath);
  136. bool result = AzToolsFramework::AssetBundleComponent::RemoveNonAssetFileEntries(fileEntriesHasCatalog, normalizedSourcePakPath, nullptr);
  137. EXPECT_TRUE(result);
  138. // check to make sure that sourcePakPath doesn't exist in fileEntriesHasCatalog
  139. auto itr = AZStd::find(fileEntriesHasCatalog.begin(), fileEntriesHasCatalog.end(), normalizedSourcePakPath);
  140. EXPECT_EQ(itr, fileEntriesHasCatalog.end());
  141. }
  142. AZ_TOOLS_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);