AudioEngineWwiseBuilderTest.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <AzTest/Utils.h>
  10. #include <WwiseBuilderComponent.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/IO/Path/Path.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/UserSettings/UserSettingsComponent.h>
  15. #include <AzCore/Utils/Utils.h>
  16. #include <AzFramework/IO/LocalFileIO.h>
  17. #include <AzToolsFramework/Application/ToolsApplication.h>
  18. using namespace WwiseBuilder;
  19. class WwiseBuilderTests
  20. : public ::testing::Test
  21. {
  22. protected:
  23. void SetUp() override
  24. {
  25. m_app.Start(AZ::ComponentApplication::Descriptor());
  26. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  27. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  28. // in the unit tests.
  29. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  30. const AZStd::string engineRoot = AZ::Test::GetEngineRootPath();
  31. AZ::IO::FileIOBase::GetInstance()->SetAlias("@engroot@", engineRoot.c_str());
  32. AZ::IO::Path assetRoot(AZ::Utils::GetProjectPath());
  33. assetRoot /= "Cache";
  34. AZ::IO::FileIOBase::GetInstance()->SetAlias("@root@", assetRoot.c_str());
  35. AZ::IO::FileIOBase::GetInstance()->SetAlias("@products@", assetRoot.c_str());
  36. }
  37. void TearDown() override
  38. {
  39. m_app.Stop();
  40. }
  41. AZStd::string GetRequestPath(AZStd::string_view fileName)
  42. {
  43. constexpr char requestPath[] = "Sounds/wwise/";
  44. return AZStd::string::format("%s%.*s", requestPath, aznumeric_cast<int>(fileName.size()), fileName.data());
  45. }
  46. AZStd::string GetTestFileAliasedPath(AZStd::string_view fileName)
  47. {
  48. constexpr char testFileFolder[] = "@engroot@/Gems/AudioEngineWwise/Code/Tests/Sounds/wwise/";
  49. return AZStd::string::format("%s%.*s", testFileFolder, aznumeric_cast<int>(fileName.size()), fileName.data());
  50. }
  51. AZStd::string GetTestFileFullPath(AZStd::string_view fileName)
  52. {
  53. AZStd::string aliasedPath = GetTestFileAliasedPath(fileName);
  54. char resolvedPath[AZ_MAX_PATH_LEN];
  55. AZ::IO::FileIOBase::GetInstance()->ResolvePath(aliasedPath.c_str(), resolvedPath, AZ_MAX_PATH_LEN);
  56. return AZStd::string(resolvedPath);
  57. }
  58. void TestFailureCase(AZStd::string_view fileName)
  59. {
  60. WwiseBuilderWorker worker;
  61. AssetBuilderSDK::ProductPathDependencySet resolvedPaths;
  62. AZStd::string relativeRequestPath = GetRequestPath(fileName);
  63. AZStd::string absoluteRequestPath = GetTestFileFullPath(fileName);
  64. AZ::Outcome<AZStd::string, AZStd::string> result = worker.GatherProductDependencies(absoluteRequestPath, relativeRequestPath, resolvedPaths);
  65. EXPECT_FALSE(result.IsSuccess());
  66. EXPECT_EQ(resolvedPaths.size(), 0);
  67. }
  68. void TestSuccessCase(AZStd::string_view fileName, AZStd::vector<const char*>& expectedDependencies, bool expectWarning = false)
  69. {
  70. WwiseBuilderWorker worker;
  71. AssetBuilderSDK::ProductPathDependencySet resolvedPaths;
  72. size_t referencedFilesCount = expectedDependencies.size();
  73. AssetBuilderSDK::ProductPathDependencySet expectedResolvedPaths;
  74. for (const char* path : expectedDependencies)
  75. {
  76. expectedResolvedPaths.emplace(path, AssetBuilderSDK::ProductPathDependencyType::ProductFile);
  77. }
  78. AZStd::string relativeRequestPath = GetRequestPath(fileName);
  79. AZStd::string absoluteRequestPath = GetTestFileFullPath(fileName);
  80. AZ::Outcome<AZStd::string, AZStd::string> result = worker.GatherProductDependencies(absoluteRequestPath, relativeRequestPath, resolvedPaths);
  81. EXPECT_TRUE(result.IsSuccess());
  82. EXPECT_EQ(!result.GetValue().empty(), expectWarning);
  83. EXPECT_EQ(resolvedPaths.size(), referencedFilesCount);
  84. if (referencedFilesCount > 0)
  85. {
  86. for (const AssetBuilderSDK::ProductPathDependency& dependency : expectedResolvedPaths)
  87. {
  88. EXPECT_TRUE(resolvedPaths.find(dependency) != resolvedPaths.end());
  89. }
  90. }
  91. }
  92. void TestSuccessCase(AZStd::string_view fileName, const char* expectedDependency, bool expectWarning = false)
  93. {
  94. AZStd::vector<const char*> expectedDependencies;
  95. expectedDependencies.push_back(expectedDependency);
  96. TestSuccessCase(fileName, expectedDependencies, expectWarning);
  97. }
  98. void TestSuccessCaseNoDependencies(AZStd::string_view fileName, bool expectWarning = false)
  99. {
  100. AZStd::vector<const char*> expectedDependencies;
  101. TestSuccessCase(fileName, expectedDependencies, expectWarning);
  102. }
  103. private:
  104. AzToolsFramework::ToolsApplication m_app;
  105. };
  106. TEST_F(WwiseBuilderTests, WwiseBuilder_EmptyFile_ExpectFailure)
  107. {
  108. // Should fail in WwiseBuilderWorker::GatherProductDependencies, when checking for the size of the file.
  109. TestFailureCase("test_bank1.bnk");
  110. }
  111. TEST_F(WwiseBuilderTests, WwiseBuilder_MalformedMetadata_ParsingFailed_ExpectFailure)
  112. {
  113. // Should fail in WwiseBuilderWorker::GatherProductDependencies, trying to parse the json file.
  114. TestFailureCase("test_bank2.bnk");
  115. }
  116. TEST_F(WwiseBuilderTests, WwiseBuilder_MalformedMetadata_NoRootObject_ExpectFailure)
  117. {
  118. // Should fail in WwiseBuilderWorker::GatherProductDependencies after calling Internal::GetDependenciesFromMetadata,
  119. // which should return an AZ::Failure when the json data's root element isn't an object.
  120. TestFailureCase("test_bank3.bnk");
  121. }
  122. TEST_F(WwiseBuilderTests, WwiseBuilder_MalformedMetadata_DependencyFieldWrongType_ExpectFailure)
  123. {
  124. // Should fail in WwiseBuilderWorker::GatherProductDependencies after calling Internal::GetDependenciesFromMetadata,
  125. // which should return an AZ::Failure when the dependency field is not an array.
  126. TestFailureCase("test_bank4.bnk");
  127. }
  128. TEST_F(WwiseBuilderTests, WwiseBuilder_InitBank_NoMetadata_NoDependencies)
  129. {
  130. TestSuccessCaseNoDependencies("init.bnk");
  131. }
  132. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_NoMetadata_NoDependencies)
  133. {
  134. // Should generate a warning after trying to find metadata for the given bank, when the bank is not the init bank.
  135. // Warning should be about not being able to generate full dependency information without the metadata file.
  136. TestSuccessCaseNoDependencies("test_doesNotExist.bnk", true);
  137. }
  138. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_OneDependency)
  139. {
  140. TestSuccessCase("test_bank5.bnk", "Sounds/wwise/init.bnk");
  141. }
  142. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_MultipleDependencies)
  143. {
  144. AZStd::vector<const char*> expectedPaths = {
  145. "Sounds/wwise/init.bnk",
  146. "Sounds/wwise/test_bank2.bnk",
  147. "Sounds/wwise/12345678.wem"
  148. };
  149. TestSuccessCase("test_bank6.bnk", expectedPaths);
  150. }
  151. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_DependencyArrayNonexistent_OneDefaultDependency)
  152. {
  153. // Should generate a warning when trying to get dependency info from metadata file, but the dependency field does
  154. // not exist. Warning should be describing that a dependency on the init bank was added by default, but
  155. // the full dependency list could not be generated.
  156. TestSuccessCase("test_bank7.bnk", "Sounds/wwise/init.bnk", true);
  157. }
  158. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_NoElementsInDependencyArray_OneDefaultDependency)
  159. {
  160. // Should generate a warning when trying to get dependency info from metadata file, but the dependency field is
  161. // an empty array. Warning should be describing that a dependency on the init bank was added by default.
  162. TestSuccessCase("test_bank8.bnk", "Sounds/wwise/init.bnk", true);
  163. }
  164. TEST_F(WwiseBuilderTests, WwiseBuilder_ContentBank_MissingInitBankDependency_MultipleDependencies)
  165. {
  166. // Should generate a warning when trying to get dependency info from metadata file, but the dependency info in the
  167. // metadata doesn't include the init bank. Warning should be describing that a dependency on the init bank was
  168. // added by default.
  169. AZStd::vector<const char*> expectedPaths = {
  170. "Sounds/wwise/init.bnk",
  171. "Sounds/wwise/test_bank2.bnk",
  172. "Sounds/wwise/12345678.wem"
  173. };
  174. TestSuccessCase("test_bank9.bnk", expectedPaths, true);
  175. }