AudioSystemTest.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 <AzCore/Memory/OSAllocator.h>
  11. #include <AzCore/IO/Streamer/FileRequest.h>
  12. #include <AzCore/std/containers/map.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzCore/StringFunc/StringFunc.h>
  15. #include <AzFramework/IO/LocalFileIO.h>
  16. #include <AudioAllocators.h>
  17. #include <ATLComponents.h>
  18. #include <ATLUtils.h>
  19. #include <ATL.h>
  20. #include <AudioProxy.h>
  21. #include <Mocks/ATLEntitiesMock.h>
  22. #include <Mocks/AudioSystemImplementationMock.h>
  23. #include <Mocks/FileCacheManagerMock.h>
  24. #include <Mocks/IConsoleMock.h>
  25. #include <Mocks/ISystemMock.h>
  26. using namespace Audio;
  27. using ::testing::NiceMock;
  28. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);
  29. /*
  30. // This is an example of a test fixture:
  31. // Use it to set up a common scenario across multiple related unit tests.
  32. // Each test will execute SetUp and TearDown before and after running the test.
  33. class AudioExampleTestFixture
  34. : public ::testing::Test
  35. {
  36. public:
  37. AudioExampleTestFixture()
  38. {
  39. // use ctor to initialize data
  40. }
  41. void SetUp() override
  42. {
  43. // additional setup, called before the test begins
  44. }
  45. void TearDown() override
  46. {
  47. // called after test ends
  48. }
  49. // data used for testing...
  50. };
  51. // To test using this fixture, do:
  52. TEST_F(AudioExampleTestFixture, ThingUnderTest_WhatThisTests_ExpectedResult)
  53. {
  54. ...
  55. }
  56. */
  57. TEST(ATLWorldPositionTest, TransformGetColumn_GetColumn_Passes)
  58. {
  59. SATLWorldPosition position;
  60. AZ::Vector3 column0 = position.GetRightVec();
  61. EXPECT_TRUE(column0.IsClose(AZ::Vector3::CreateAxisX()));
  62. AZ::Vector3 column1 = position.GetForwardVec();
  63. EXPECT_TRUE(column1.IsClose(AZ::Vector3::CreateAxisY()));
  64. AZ::Vector3 column2 = position.GetUpVec();
  65. EXPECT_TRUE(column2.IsClose(AZ::Vector3::CreateAxisZ()));
  66. }
  67. TEST(ATLWorldPositionTest, TransformNormalize_NormalizeNonUnitVectors_GivesUnitLengthVectors)
  68. {
  69. AZ::Matrix3x4 matrix3x4Test;
  70. matrix3x4Test.SetBasisX(AZ::Vector3::CreateAxisX());
  71. matrix3x4Test.SetBasisY(1.f, 2.f, 1.f);
  72. matrix3x4Test.SetBasisZ(1.f, 1.f, 2.f);
  73. SATLWorldPosition position(matrix3x4Test);
  74. position.NormalizeForwardVec();
  75. AZ::Vector3 forward = position.GetForwardVec();
  76. EXPECT_TRUE(AZ::IsClose(forward.GetLength(), 1.f, 1e-3f));
  77. position.NormalizeUpVec();
  78. AZ::Vector3 up = position.GetUpVec();
  79. EXPECT_TRUE(AZ::IsClose(up.GetLength(), 1.f, 1e-3f));
  80. }
  81. TEST(ATLWorldPositionTest, TransformNormalize_NormalizeZeroVectors_GivesBasisVectors)
  82. {
  83. AZ::Matrix3x4 matrix3x4Test = AZ::Matrix3x4::CreateZero();
  84. SATLWorldPosition position(matrix3x4Test);
  85. EXPECT_EQ(position.GetForwardVec(), AZ::Vector3::CreateZero());
  86. EXPECT_EQ(position.GetUpVec(), AZ::Vector3::CreateZero());
  87. position.NormalizeForwardVec();
  88. EXPECT_EQ(position.GetForwardVec(), AZ::Vector3::CreateAxisY());
  89. position.NormalizeUpVec();
  90. EXPECT_EQ(position.GetUpVec(), AZ::Vector3::CreateAxisZ());
  91. }
  92. // Tests related to new PhysX-compatible raycast code.
  93. constexpr TAudioObjectID testAudioObjectId = 123;
  94. class ATLAudioObjectTest : public ::testing::Test
  95. {
  96. public:
  97. RaycastProcessor& GetRaycastProcessor(CATLAudioObject& audioObject) const
  98. {
  99. return audioObject.m_raycastProcessor;
  100. }
  101. const RaycastProcessor& GetRaycastProcessor(const CATLAudioObject& audioObject) const
  102. {
  103. return audioObject.m_raycastProcessor;
  104. }
  105. };
  106. TEST_F(ATLAudioObjectTest, SetRaycastCalcType_SetAllTypes_AffectsCanRunRaycasts)
  107. {
  108. RaycastProcessor::s_raycastsEnabled = true;
  109. CATLAudioObject audioObject(testAudioObjectId, nullptr);
  110. audioObject.SetRaycastCalcType(Audio::ObstructionType::SingleRay);
  111. EXPECT_TRUE(audioObject.CanRunRaycasts());
  112. audioObject.SetRaycastCalcType(Audio::ObstructionType::Ignore);
  113. EXPECT_FALSE(audioObject.CanRunRaycasts());
  114. audioObject.SetRaycastCalcType(Audio::ObstructionType::MultiRay);
  115. EXPECT_TRUE(audioObject.CanRunRaycasts());
  116. }
  117. TEST_F(ATLAudioObjectTest, OnAudioRaycastResults_MultiRaycastZeroDistanceHits_ZeroObstructionAndOcclusion)
  118. {
  119. RaycastProcessor::s_raycastsEnabled = true;
  120. CATLAudioObject audioObject(testAudioObjectId, nullptr);
  121. RaycastProcessor& raycastProcessor = GetRaycastProcessor(audioObject);
  122. audioObject.SetRaycastCalcType(Audio::ObstructionType::MultiRay);
  123. for (AZStd::decay_t<decltype(Audio::s_maxRaysPerObject)> i = 0; i < Audio::s_maxRaysPerObject; ++i)
  124. {
  125. raycastProcessor.SetupTestRay(i);
  126. }
  127. // maximum number of hits, but we don't set the distance in any of them.
  128. AZStd::vector<AzPhysics::SceneQueryHit> hits(Audio::s_maxHitResultsPerRaycast);
  129. AudioRaycastResult hitResults(AZStd::move(hits), testAudioObjectId, 0);
  130. audioObject.OnAudioRaycastResults(hitResults);
  131. raycastProcessor.Update(17.f);
  132. // Now get the contribution amounts. In this case multiple hits w/ zero distance,
  133. // both obstruction & occlusion should be zero.
  134. SATLSoundPropagationData data;
  135. audioObject.GetObstOccData(data);
  136. EXPECT_NEAR(data.fObstruction, 0.f, AZ::Constants::FloatEpsilon);
  137. EXPECT_NEAR(data.fOcclusion, 0.f, AZ::Constants::FloatEpsilon);
  138. }
  139. TEST_F(ATLAudioObjectTest, OnAudioRaycastResults_SingleRaycastHit_NonZeroObstruction)
  140. {
  141. RaycastProcessor::s_raycastsEnabled = true;
  142. CATLAudioObject audioObject(testAudioObjectId, nullptr);
  143. RaycastProcessor& raycastProcessor = GetRaycastProcessor(audioObject);
  144. audioObject.SetRaycastCalcType(Audio::ObstructionType::SingleRay);
  145. raycastProcessor.SetupTestRay(0);
  146. AZStd::vector<AzPhysics::SceneQueryHit> hits(3); // three hits
  147. hits[0].m_distance = 10.f;
  148. hits[1].m_distance = 11.f;
  149. hits[2].m_distance = 12.f;
  150. AudioRaycastResult hitResults(AZStd::move(hits), testAudioObjectId, 0);
  151. audioObject.OnAudioRaycastResults(hitResults);
  152. raycastProcessor.Update(0.17f);
  153. // Now get the contribution amounts. In this case a single ray had three hits,
  154. // and the obstruction value will be non-zero.
  155. SATLSoundPropagationData data;
  156. audioObject.GetObstOccData(data);
  157. EXPECT_GT(data.fObstruction, 0.f);
  158. EXPECT_LE(data.fObstruction, 1.f);
  159. EXPECT_NEAR(data.fOcclusion, 0.f, AZ::Constants::FloatEpsilon);
  160. }
  161. TEST_F(ATLAudioObjectTest, OnAudioRaycastResults_MultiRaycastHit_NonZeroOcclusion)
  162. {
  163. RaycastProcessor::s_raycastsEnabled = true;
  164. CATLAudioObject audioObject(testAudioObjectId, nullptr);
  165. RaycastProcessor& raycastProcessor = GetRaycastProcessor(audioObject);
  166. audioObject.SetRaycastCalcType(Audio::ObstructionType::MultiRay);
  167. for (AZStd::decay_t<decltype(Audio::s_maxRaysPerObject)> i = 1; i < Audio::s_maxRaysPerObject; ++i)
  168. {
  169. raycastProcessor.SetupTestRay(i);
  170. }
  171. AZStd::vector<AzPhysics::SceneQueryHit> hits(3); // three hits
  172. hits[0].m_distance = 10.f;
  173. hits[1].m_distance = 11.f;
  174. hits[2].m_distance = 12.f;
  175. AudioRaycastResult hitResults(AZStd::move(hits), testAudioObjectId, 1);
  176. audioObject.OnAudioRaycastResults(hitResults);
  177. hitResults.m_rayIndex++; // 2
  178. audioObject.OnAudioRaycastResults(hitResults);
  179. hitResults.m_rayIndex++; // 3
  180. audioObject.OnAudioRaycastResults(hitResults);
  181. hitResults.m_rayIndex++; // 4
  182. audioObject.OnAudioRaycastResults(hitResults);
  183. raycastProcessor.Update(17.f);
  184. // Now get the contribution amounts. In this case a single ray had three hits,
  185. // and the obstruction value will be non-zero.
  186. SATLSoundPropagationData data;
  187. audioObject.GetObstOccData(data);
  188. EXPECT_NEAR(data.fObstruction, 0.f, AZ::Constants::FloatEpsilon);
  189. EXPECT_GT(data.fOcclusion, 0.f);
  190. EXPECT_LE(data.fOcclusion, 1.f);
  191. }
  192. class AudioRaycastManager_Test
  193. : public AudioRaycastManager
  194. {
  195. public:
  196. // Helpers
  197. size_t GetNumRequests() const
  198. {
  199. return m_raycastRequests.size();
  200. }
  201. size_t GetNumResults() const
  202. {
  203. return m_raycastResults.size();
  204. }
  205. };
  206. TEST(AudioRaycastManagerTest, AudioRaycastRequest_FullProcessFlow_CorrectRequestAndResultCounts)
  207. {
  208. AudioRaycastManager_Test raycastManager;
  209. AzPhysics::RayCastRequest physicsRequest;
  210. physicsRequest.m_direction = AZ::Vector3::CreateAxisX();
  211. physicsRequest.m_distance = 5.f;
  212. physicsRequest.m_maxResults = Audio::s_maxHitResultsPerRaycast;
  213. physicsRequest.m_reportMultipleHits = true;
  214. AudioRaycastRequest raycastRequest(AZStd::move(physicsRequest), testAudioObjectId, 0);
  215. EXPECT_EQ(0, raycastManager.GetNumRequests());
  216. EXPECT_EQ(0, raycastManager.GetNumResults());
  217. raycastManager.PushAudioRaycastRequest(raycastRequest);
  218. EXPECT_EQ(1, raycastManager.GetNumRequests());
  219. EXPECT_EQ(0, raycastManager.GetNumResults());
  220. raycastManager.OnPhysicsSubtickFinished();
  221. EXPECT_EQ(0, raycastManager.GetNumRequests());
  222. EXPECT_EQ(1, raycastManager.GetNumResults());
  223. raycastManager.ProcessRaycastResults(17.f); // milliseconds
  224. EXPECT_EQ(0, raycastManager.GetNumRequests());
  225. EXPECT_EQ(0, raycastManager.GetNumResults());
  226. }
  227. //---------------//
  228. // Test ATLUtils //
  229. //---------------//
  230. class ATLUtilsTestFixture
  231. : public ::testing::Test
  232. {
  233. protected:
  234. using KeyType = AZStd::string;
  235. using ValType = int;
  236. using MapType = AZStd::map<KeyType, ValType, AZStd::less<KeyType>, Audio::AudioSystemStdAllocator>;
  237. void SetUp() override
  238. {
  239. m_testMap["Hello"] = 10;
  240. m_testMap["World"] = 15;
  241. m_testMap["GoodBye"] = 20;
  242. m_testMap["Orange"] = 25;
  243. m_testMap["Apple"] = 30;
  244. }
  245. void TearDown() override
  246. {
  247. m_testMap.clear();
  248. }
  249. MapType m_testMap;
  250. };
  251. TEST_F(ATLUtilsTestFixture, FindPlace_ContainerContainsItem_FindsItem)
  252. {
  253. MapType::iterator placeIterator;
  254. EXPECT_TRUE(FindPlace(m_testMap, KeyType { "Hello" }, placeIterator));
  255. EXPECT_NE(placeIterator, m_testMap.end());
  256. }
  257. TEST_F(ATLUtilsTestFixture, FindPlace_ContainerDoesntContainItem_FindsNone)
  258. {
  259. MapType::iterator placeIterator;
  260. EXPECT_FALSE(FindPlace(m_testMap, KeyType { "goodbye" }, placeIterator));
  261. EXPECT_EQ(placeIterator, m_testMap.end());
  262. }
  263. TEST_F(ATLUtilsTestFixture, FindPlaceConst_ContainerContainsItem_FindsItem)
  264. {
  265. MapType::const_iterator placeIterator;
  266. EXPECT_TRUE(FindPlaceConst(m_testMap, KeyType { "Orange" }, placeIterator));
  267. EXPECT_NE(placeIterator, m_testMap.end());
  268. }
  269. TEST_F(ATLUtilsTestFixture, FindPlaceConst_ContainerDoesntContainItem_FindsNone)
  270. {
  271. MapType::const_iterator placeIterator;
  272. EXPECT_FALSE(FindPlaceConst(m_testMap, KeyType { "Bananas" }, placeIterator));
  273. EXPECT_EQ(placeIterator, m_testMap.end());
  274. }
  275. //-------------------//
  276. // Test Audio::Flags //
  277. //-------------------//
  278. TEST(AudioFlagsTest, AudioFlags_ZeroFlags_NoFlagsAreSet)
  279. {
  280. const AZ::u8 noFlags = 0;
  281. const AZ::u8 allFlags = static_cast<AZ::u8>(~0);
  282. Audio::Flags<AZ::u8> testFlags;
  283. EXPECT_FALSE(testFlags.AreAnyFlagsActive(allFlags));
  284. EXPECT_FALSE(testFlags.AreMultipleFlagsActive());
  285. EXPECT_FALSE(testFlags.IsOneFlagActive());
  286. EXPECT_EQ(testFlags.GetRawFlags(), noFlags);
  287. }
  288. TEST(AudioFlagsTest, AudioFlags_OneFlag_OneFlagIsSet)
  289. {
  290. const AZ::u8 flagBit = 1 << 4;
  291. Audio::Flags<AZ::u8> testFlags(flagBit);
  292. EXPECT_FALSE(testFlags.AreAnyFlagsActive(static_cast<AZ::u8>(~flagBit)));
  293. EXPECT_TRUE(testFlags.AreAnyFlagsActive(flagBit));
  294. EXPECT_TRUE(testFlags.AreAnyFlagsActive(flagBit | 1));
  295. EXPECT_TRUE(testFlags.AreAllFlagsActive(flagBit));
  296. EXPECT_FALSE(testFlags.AreAllFlagsActive(flagBit | 1));
  297. EXPECT_FALSE(testFlags.AreMultipleFlagsActive());
  298. EXPECT_TRUE(testFlags.IsOneFlagActive());
  299. EXPECT_EQ(testFlags.GetRawFlags(), flagBit);
  300. }
  301. TEST(AudioFlagsTest, AudioFlags_MultipleFlags_MultipleFlagsAreSet)
  302. {
  303. const AZ::u8 flagBits = (1 << 5) | (1 << 2) | (1 << 3);
  304. Audio::Flags<AZ::u8> testFlags(flagBits);
  305. EXPECT_FALSE(testFlags.AreAnyFlagsActive(static_cast<AZ::u8>(~flagBits)));
  306. EXPECT_TRUE(testFlags.AreAnyFlagsActive(flagBits));
  307. EXPECT_TRUE(testFlags.AreAllFlagsActive(flagBits));
  308. EXPECT_FALSE(testFlags.AreAllFlagsActive(flagBits | 1));
  309. EXPECT_TRUE(testFlags.AreMultipleFlagsActive());
  310. EXPECT_FALSE(testFlags.IsOneFlagActive());
  311. EXPECT_EQ(testFlags.GetRawFlags(), flagBits);
  312. }
  313. TEST(AudioFlagsTest, AudioFlags_AddAndClear_FlagsAreCorrect)
  314. {
  315. const AZ::u8 flagBits = (1 << 2) | (1 << 6);
  316. Audio::Flags<AZ::u8> testFlags;
  317. Audio::Flags<AZ::u8> zeroFlags;
  318. testFlags.AddFlags(flagBits);
  319. EXPECT_TRUE(testFlags != zeroFlags);
  320. testFlags.ClearFlags(flagBits);
  321. EXPECT_TRUE(testFlags == zeroFlags);
  322. }
  323. TEST(AudioFlagsTest, AudioFlags_SetAndClearAll_FlagsAreCorrect)
  324. {
  325. const AZ::u8 flagBits = (1 << 3) | (1 << 5) | (1 << 7);
  326. Audio::Flags<AZ::u8> testFlags;
  327. Audio::Flags<AZ::u8> zeroFlags;
  328. testFlags.SetFlags(flagBits, true);
  329. EXPECT_TRUE(testFlags != zeroFlags);
  330. EXPECT_EQ(testFlags.GetRawFlags(), flagBits);
  331. testFlags.SetFlags((1 << 3), false);
  332. EXPECT_TRUE(testFlags != zeroFlags);
  333. EXPECT_NE(testFlags.GetRawFlags(), flagBits);
  334. testFlags.ClearAllFlags();
  335. EXPECT_TRUE(testFlags == zeroFlags);
  336. }
  337. //-------------------------//
  338. // Test CATLDebugNameStore //
  339. //-------------------------//
  340. #if !defined(AUDIO_RELEASE)
  341. class ATLDebugNameStoreTestFixture
  342. : public ::testing::Test
  343. {
  344. public:
  345. ATLDebugNameStoreTestFixture()
  346. : m_audioObjectName("SomeAudioObject1")
  347. , m_audioTriggerName("SomeAudioTrigger1")
  348. , m_audioRtpcName("SomeAudioRtpc1")
  349. , m_audioSwitchName("SomeAudioSwitch1")
  350. , m_audioSwitchStateName("SomeAudioSwitchState1")
  351. , m_audioEnvironmentName("SomeAudioEnvironment1")
  352. , m_audioPreloadRequestName("SomeAudioPreloadRequest1")
  353. {}
  354. void SetUp() override
  355. {
  356. }
  357. void TearDown() override
  358. {
  359. }
  360. protected:
  361. CATLDebugNameStore m_atlNames;
  362. AZStd::string m_audioObjectName;
  363. AZStd::string m_audioTriggerName;
  364. AZStd::string m_audioRtpcName;
  365. AZStd::string m_audioSwitchName;
  366. AZStd::string m_audioSwitchStateName;
  367. AZStd::string m_audioEnvironmentName;
  368. AZStd::string m_audioPreloadRequestName;
  369. };
  370. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioObject_Success)
  371. {
  372. auto audioObjectID = AudioStringToID<TAudioObjectID>(m_audioObjectName.c_str());
  373. bool added = m_atlNames.AddAudioObject(audioObjectID, m_audioObjectName.c_str());
  374. EXPECT_TRUE(added);
  375. added = m_atlNames.AddAudioObject(audioObjectID, m_audioObjectName.c_str());
  376. EXPECT_FALSE(added);
  377. }
  378. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioObjectAndLookupName_FindsName)
  379. {
  380. auto audioObjectID = AudioStringToID<TAudioObjectID>(m_audioObjectName.c_str());
  381. m_atlNames.AddAudioObject(audioObjectID, m_audioObjectName.c_str());
  382. EXPECT_STREQ(m_atlNames.LookupAudioObjectName(audioObjectID), m_audioObjectName.c_str());
  383. }
  384. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioTrigger_Success)
  385. {
  386. auto audioTriggerID = AudioStringToID<TAudioControlID>(m_audioTriggerName.c_str());
  387. bool added = m_atlNames.AddAudioTrigger(audioTriggerID, m_audioTriggerName.c_str());
  388. EXPECT_TRUE(added);
  389. added = m_atlNames.AddAudioTrigger(audioTriggerID, m_audioTriggerName.c_str());
  390. EXPECT_FALSE(added);
  391. }
  392. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioTriggerAndLookupName_FindsName)
  393. {
  394. auto audioTriggerID = AudioStringToID<TAudioControlID>(m_audioTriggerName.c_str());
  395. m_atlNames.AddAudioTrigger(audioTriggerID, m_audioTriggerName.c_str());
  396. EXPECT_STREQ(m_atlNames.LookupAudioTriggerName(audioTriggerID), m_audioTriggerName.c_str());
  397. }
  398. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioRtpc_Success)
  399. {
  400. auto audioRtpcID = AudioStringToID<TAudioControlID>(m_audioRtpcName.c_str());
  401. bool added = m_atlNames.AddAudioRtpc(audioRtpcID, m_audioRtpcName.c_str());
  402. EXPECT_TRUE(added);
  403. added = m_atlNames.AddAudioRtpc(audioRtpcID, m_audioRtpcName.c_str());
  404. EXPECT_FALSE(added);
  405. }
  406. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioRtpcAndLookupName_FindsName)
  407. {
  408. auto audioRtpcID = AudioStringToID<TAudioControlID>(m_audioRtpcName.c_str());
  409. m_atlNames.AddAudioRtpc(audioRtpcID, m_audioRtpcName.c_str());
  410. EXPECT_STREQ(m_atlNames.LookupAudioRtpcName(audioRtpcID), m_audioRtpcName.c_str());
  411. }
  412. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioSwitch_Success)
  413. {
  414. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  415. bool added = m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  416. EXPECT_TRUE(added);
  417. added = m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  418. EXPECT_FALSE(added);
  419. }
  420. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioSwitchAndLookupName_FindsName)
  421. {
  422. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  423. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  424. EXPECT_STREQ(m_atlNames.LookupAudioSwitchName(audioSwitchID), m_audioSwitchName.c_str());
  425. }
  426. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioSwitchState_Success)
  427. {
  428. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  429. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  430. auto audioSwitchStateID = AudioStringToID<TAudioSwitchStateID>(m_audioSwitchStateName.c_str());
  431. bool added = m_atlNames.AddAudioSwitchState(audioSwitchID, audioSwitchStateID, m_audioSwitchStateName.c_str());
  432. EXPECT_TRUE(added);
  433. added = m_atlNames.AddAudioSwitchState(audioSwitchID, audioSwitchStateID, m_audioSwitchStateName.c_str());
  434. EXPECT_FALSE(added);
  435. }
  436. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioSwitchStateAndLookupNames_FindsNames)
  437. {
  438. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  439. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  440. auto audioSwitchStateID = AudioStringToID<TAudioSwitchStateID>(m_audioSwitchStateName.c_str());
  441. m_atlNames.AddAudioSwitchState(audioSwitchID, audioSwitchStateID, m_audioSwitchStateName.c_str());
  442. EXPECT_STREQ(m_atlNames.LookupAudioSwitchName(audioSwitchID), m_audioSwitchName.c_str());
  443. EXPECT_STREQ(m_atlNames.LookupAudioSwitchStateName(audioSwitchID, audioSwitchStateID), m_audioSwitchStateName.c_str());
  444. }
  445. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioPreload_Success)
  446. {
  447. auto audioPreloadID = AudioStringToID<TAudioPreloadRequestID>(m_audioPreloadRequestName.c_str());
  448. bool added = m_atlNames.AddAudioPreloadRequest(audioPreloadID, m_audioPreloadRequestName.c_str());
  449. EXPECT_TRUE(added);
  450. added = m_atlNames.AddAudioPreloadRequest(audioPreloadID, m_audioPreloadRequestName.c_str());
  451. EXPECT_FALSE(added);
  452. }
  453. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioPreloadAndLookupName_FindsName)
  454. {
  455. auto audioPreloadID = AudioStringToID<TAudioPreloadRequestID>(m_audioPreloadRequestName.c_str());
  456. m_atlNames.AddAudioPreloadRequest(audioPreloadID, m_audioPreloadRequestName.c_str());
  457. EXPECT_STREQ(m_atlNames.LookupAudioPreloadRequestName(audioPreloadID), m_audioPreloadRequestName.c_str());
  458. }
  459. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioEnvironment_Success)
  460. {
  461. auto audioEnvironmentID = AudioStringToID<TAudioEnvironmentID>(m_audioEnvironmentName.c_str());
  462. bool added = m_atlNames.AddAudioEnvironment(audioEnvironmentID, m_audioEnvironmentName.c_str());
  463. EXPECT_TRUE(added);
  464. added = m_atlNames.AddAudioEnvironment(audioEnvironmentID, m_audioEnvironmentName.c_str());
  465. EXPECT_FALSE(added);
  466. }
  467. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_AddAudioEnvironmentAndLookupName_FindsName)
  468. {
  469. auto audioEnvironmentID = AudioStringToID<TAudioEnvironmentID>(m_audioEnvironmentName.c_str());
  470. m_atlNames.AddAudioEnvironment(audioEnvironmentID, m_audioEnvironmentName.c_str());
  471. EXPECT_STREQ(m_atlNames.LookupAudioEnvironmentName(audioEnvironmentID), m_audioEnvironmentName.c_str());
  472. }
  473. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioObjectNotFound_Fails)
  474. {
  475. auto audioObjectID = AudioStringToID<TAudioObjectID>(m_audioObjectName.c_str());
  476. bool removed = m_atlNames.RemoveAudioObject(audioObjectID);
  477. EXPECT_FALSE(removed);
  478. }
  479. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioTriggerNotFound_Fails)
  480. {
  481. auto audioTriggerID = AudioStringToID<TAudioControlID>(m_audioTriggerName.c_str());
  482. bool removed = m_atlNames.RemoveAudioTrigger(audioTriggerID);
  483. EXPECT_FALSE(removed);
  484. }
  485. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioRtpcNotFound_Fails)
  486. {
  487. auto audioRtpcID = AudioStringToID<TAudioControlID>(m_audioRtpcName.c_str());
  488. bool removed = m_atlNames.RemoveAudioRtpc(audioRtpcID);
  489. EXPECT_FALSE(removed);
  490. }
  491. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioSwitchNotFound_Fails)
  492. {
  493. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  494. bool removed = m_atlNames.RemoveAudioSwitch(audioSwitchID);
  495. EXPECT_FALSE(removed);
  496. }
  497. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioSwitchStateNotFound_Fails)
  498. {
  499. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  500. auto audioSwitchStateID = AudioStringToID<TAudioSwitchStateID>(m_audioSwitchStateName.c_str());
  501. bool removed = m_atlNames.RemoveAudioSwitchState(audioSwitchID, audioSwitchStateID);
  502. EXPECT_FALSE(removed);
  503. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  504. removed = m_atlNames.RemoveAudioSwitchState(audioSwitchID, audioSwitchStateID);
  505. EXPECT_FALSE(removed);
  506. }
  507. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioPreloadRequestNotFound_Fails)
  508. {
  509. auto audioPreloadID = AudioStringToID<TAudioPreloadRequestID>(m_audioPreloadRequestName.c_str());
  510. bool removed = m_atlNames.RemoveAudioPreloadRequest(audioPreloadID);
  511. EXPECT_FALSE(removed);
  512. }
  513. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioEnvironmentNotFound_Fails)
  514. {
  515. auto audioEnvironmentID = AudioStringToID<TAudioEnvironmentID>(m_audioEnvironmentName.c_str());
  516. bool removed = m_atlNames.RemoveAudioEnvironment(audioEnvironmentID);
  517. EXPECT_FALSE(removed);
  518. }
  519. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioObjectAndLookupName_FindsNone)
  520. {
  521. auto audioObjectID = AudioStringToID<TAudioObjectID>(m_audioObjectName.c_str());
  522. bool added = m_atlNames.AddAudioObject(audioObjectID, m_audioObjectName.c_str());
  523. bool removed = m_atlNames.RemoveAudioObject(audioObjectID);
  524. EXPECT_TRUE(added && removed);
  525. EXPECT_EQ(m_atlNames.LookupAudioObjectName(audioObjectID), nullptr);
  526. }
  527. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioTriggerAndLookupName_FindsNone)
  528. {
  529. auto audioTriggerID = AudioStringToID<TAudioControlID>(m_audioTriggerName.c_str());
  530. bool added = m_atlNames.AddAudioTrigger(audioTriggerID, m_audioTriggerName.c_str());
  531. bool removed = m_atlNames.RemoveAudioTrigger(audioTriggerID);
  532. EXPECT_TRUE(added && removed);
  533. EXPECT_EQ(m_atlNames.LookupAudioTriggerName(audioTriggerID), nullptr);
  534. }
  535. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioRtpcAndLookupName_FindsNone)
  536. {
  537. auto audioRtpcID = AudioStringToID<TAudioControlID>(m_audioRtpcName.c_str());
  538. bool added = m_atlNames.AddAudioRtpc(audioRtpcID, m_audioRtpcName.c_str());
  539. bool removed = m_atlNames.RemoveAudioRtpc(audioRtpcID);
  540. EXPECT_TRUE(added && removed);
  541. EXPECT_EQ(m_atlNames.LookupAudioRtpcName(audioRtpcID), nullptr);
  542. }
  543. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioSwitchAndLookupName_FindsNone)
  544. {
  545. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  546. bool added = m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  547. bool removed = m_atlNames.RemoveAudioSwitch(audioSwitchID);
  548. EXPECT_TRUE(added && removed);
  549. EXPECT_EQ(m_atlNames.LookupAudioSwitchName(audioSwitchID), nullptr);
  550. }
  551. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioSwitchStateAndLookupName_FindsNone)
  552. {
  553. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  554. auto audioSwitchStateID = AudioStringToID<TAudioSwitchStateID>(m_audioSwitchStateName.c_str());
  555. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  556. bool added = m_atlNames.AddAudioSwitchState(audioSwitchID, audioSwitchStateID, m_audioSwitchStateName.c_str());
  557. bool removed = m_atlNames.RemoveAudioSwitchState(audioSwitchID, audioSwitchStateID);
  558. EXPECT_TRUE(added && removed);
  559. EXPECT_EQ(m_atlNames.LookupAudioSwitchStateName(audioSwitchID, audioSwitchStateID), nullptr);
  560. }
  561. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioPreloadRequestAndLookupName_FindsNone)
  562. {
  563. auto audioPreloadID = AudioStringToID<TAudioPreloadRequestID>(m_audioPreloadRequestName.c_str());
  564. bool added = m_atlNames.AddAudioPreloadRequest(audioPreloadID, m_audioPreloadRequestName.c_str());
  565. bool removed = m_atlNames.RemoveAudioPreloadRequest(audioPreloadID);
  566. EXPECT_TRUE(added && removed);
  567. EXPECT_EQ(m_atlNames.LookupAudioPreloadRequestName(audioPreloadID), nullptr);
  568. }
  569. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_RemoveAudioEnvironmentAndLookupName_FindsNone)
  570. {
  571. auto audioEnvironmentID = AudioStringToID<TAudioEnvironmentID>(m_audioEnvironmentName.c_str());
  572. bool added = m_atlNames.AddAudioEnvironment(audioEnvironmentID, m_audioEnvironmentName.c_str());
  573. bool removed = m_atlNames.RemoveAudioEnvironment(audioEnvironmentID);
  574. EXPECT_TRUE(added && removed);
  575. EXPECT_EQ(m_atlNames.LookupAudioEnvironmentName(audioEnvironmentID), nullptr);
  576. }
  577. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupGlobalAudioObjectName_FindsName)
  578. {
  579. const char* globalAudioObjectName = m_atlNames.LookupAudioObjectName(GLOBAL_AUDIO_OBJECT_ID);
  580. EXPECT_STREQ(globalAudioObjectName, "GlobalAudioObject");
  581. }
  582. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioObjectName_FindsName)
  583. {
  584. auto audioObjectID = AudioStringToID<TAudioObjectID>(m_audioObjectName.c_str());
  585. m_atlNames.AddAudioObject(audioObjectID, m_audioObjectName.c_str());
  586. EXPECT_STREQ(m_atlNames.LookupAudioObjectName(audioObjectID), m_audioObjectName.c_str());
  587. }
  588. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioTriggerName_FindsName)
  589. {
  590. auto audioTriggerID = AudioStringToID<TAudioControlID>(m_audioTriggerName.c_str());
  591. m_atlNames.AddAudioTrigger(audioTriggerID, m_audioTriggerName.c_str());
  592. EXPECT_STREQ(m_atlNames.LookupAudioTriggerName(audioTriggerID), m_audioTriggerName.c_str());
  593. }
  594. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioRtpcName_FindsName)
  595. {
  596. auto audioRtpcID = AudioStringToID<TAudioControlID>(m_audioRtpcName.c_str());
  597. m_atlNames.AddAudioRtpc(audioRtpcID, m_audioRtpcName.c_str());
  598. EXPECT_STREQ(m_atlNames.LookupAudioRtpcName(audioRtpcID), m_audioRtpcName.c_str());
  599. }
  600. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioSwitchName_FindsName)
  601. {
  602. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  603. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  604. EXPECT_STREQ(m_atlNames.LookupAudioSwitchName(audioSwitchID), m_audioSwitchName.c_str());
  605. }
  606. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioSwitchStateName_FindsName)
  607. {
  608. auto audioSwitchID = AudioStringToID<TAudioControlID>(m_audioSwitchName.c_str());
  609. auto audioSwitchStateID = AudioStringToID<TAudioSwitchStateID>(m_audioSwitchStateName.c_str());
  610. m_atlNames.AddAudioSwitch(audioSwitchID, m_audioSwitchName.c_str());
  611. m_atlNames.AddAudioSwitchState(audioSwitchID, audioSwitchStateID, m_audioSwitchStateName.c_str());
  612. EXPECT_STREQ(m_atlNames.LookupAudioSwitchStateName(audioSwitchID, audioSwitchStateID), m_audioSwitchStateName.c_str());
  613. }
  614. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioPreloadRequestName_FindsName)
  615. {
  616. auto audioPreloadID = AudioStringToID<TAudioPreloadRequestID>(m_audioPreloadRequestName.c_str());
  617. m_atlNames.AddAudioPreloadRequest(audioPreloadID, m_audioPreloadRequestName.c_str());
  618. EXPECT_STREQ(m_atlNames.LookupAudioPreloadRequestName(audioPreloadID), m_audioPreloadRequestName.c_str());
  619. }
  620. TEST_F(ATLDebugNameStoreTestFixture, ATLDebugNameStore_LookupAudioEnvironmentName_FindsName)
  621. {
  622. auto audioEnvironmentID = AudioStringToID<TAudioEnvironmentID>(m_audioEnvironmentName.c_str());
  623. m_atlNames.AddAudioEnvironment(audioEnvironmentID, m_audioEnvironmentName.c_str());
  624. EXPECT_STREQ(m_atlNames.LookupAudioEnvironmentName(audioEnvironmentID), m_audioEnvironmentName.c_str());
  625. }
  626. #endif
  627. //-----------------------//
  628. // Test CATLXmlProcessor //
  629. //-----------------------//
  630. class ATLPreloadXmlParsingTestFixture
  631. : public ::testing::Test
  632. {
  633. public:
  634. ATLPreloadXmlParsingTestFixture()
  635. : m_triggers()
  636. , m_rtpcs()
  637. , m_switches()
  638. , m_environments()
  639. , m_preloads()
  640. , m_mockFileCacheManager(m_preloads)
  641. , m_xmlProcessor(m_triggers, m_rtpcs, m_switches, m_environments, m_preloads, m_mockFileCacheManager)
  642. {
  643. #if !defined(AUDIO_RELEASE)
  644. m_xmlProcessor.SetDebugNameStore(&m_mockDebugNameStore);
  645. #endif
  646. }
  647. void SetUp() override
  648. {
  649. m_prevFileIO = AZ::IO::FileIOBase::GetInstance();
  650. if (m_prevFileIO)
  651. {
  652. AZ::IO::FileIOBase::SetInstance(nullptr);
  653. }
  654. // Replace with a new LocalFileIO...
  655. m_fileIO = AZStd::make_unique<AZ::IO::LocalFileIO>();
  656. AZ::IO::FileIOBase::SetInstance(m_fileIO.get());
  657. AZStd::string rootFolder(AZ::Test::GetCurrentExecutablePath());
  658. AZ::StringFunc::Path::Join(rootFolder.c_str(), "Test.Assets/Gems/AudioSystem/ATLData", rootFolder);
  659. // Set up paths...
  660. #if !defined(AUDIO_RELEASE)
  661. m_xmlProcessor.SetRootPath(m_audioTestAlias);
  662. #endif
  663. m_fileIO->SetAlias(m_audioTestAlias, rootFolder.c_str());
  664. }
  665. void TearDown() override
  666. {
  667. // Destroy our LocalFileIO...
  668. m_fileIO->ClearAlias(m_audioTestAlias);
  669. m_fileIO.reset();
  670. // Replace the old fileIO (if any)...
  671. AZ::IO::FileIOBase::SetInstance(nullptr);
  672. if (m_prevFileIO)
  673. {
  674. AZ::IO::FileIOBase::SetInstance(m_prevFileIO);
  675. m_prevFileIO = nullptr;
  676. }
  677. }
  678. void TestSuccessfulPreloadParsing(const char* controlsFolder, int numExpectedPreloads, int numExpectedBanksPerPreload)
  679. {
  680. using ::testing::_;
  681. using ::testing::InvokeWithoutArgs;
  682. ON_CALL(m_mockFileCacheManager, TryAddFileCacheEntry(_, eADS_GLOBAL, _))
  683. .WillByDefault(InvokeWithoutArgs(GenerateNewId));
  684. m_xmlProcessor.ParsePreloadsData(controlsFolder, eADS_GLOBAL);
  685. EXPECT_EQ(m_preloads.size(), numExpectedPreloads);
  686. for (auto preloadPair : m_preloads)
  687. {
  688. EXPECT_EQ(preloadPair.second->m_cFileEntryIDs.size(), numExpectedBanksPerPreload);
  689. }
  690. m_xmlProcessor.ClearPreloadsData(eADS_ALL);
  691. }
  692. protected:
  693. TATLTriggerLookup m_triggers;
  694. TATLRtpcLookup m_rtpcs;
  695. TATLSwitchLookup m_switches;
  696. TATLEnvironmentLookup m_environments;
  697. TATLPreloadRequestLookup m_preloads;
  698. CATLXmlProcessor m_xmlProcessor;
  699. NiceMock<FileCacheManagerMock> m_mockFileCacheManager;
  700. private:
  701. #if !defined(AUDIO_RELEASE)
  702. NiceMock<ATLDebugNameStoreMock> m_mockDebugNameStore;
  703. #endif // !AUDIO_RELEASE
  704. const char* m_audioTestAlias { "@audiotestroot@" };
  705. AZ::IO::FileIOBase* m_prevFileIO { nullptr };
  706. AZStd::unique_ptr<AZ::IO::LocalFileIO> m_fileIO;
  707. static TAudioFileEntryID GenerateNewId()
  708. {
  709. static TAudioFileEntryID s_id = INVALID_AUDIO_FILE_ENTRY_ID;
  710. return ++s_id;
  711. }
  712. };
  713. // Legacy Preload Xml Format...
  714. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  715. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_LegacyOnePreloadOneBank_Success)
  716. #else
  717. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_LegacyOnePreloadOneBank_Success)
  718. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  719. {
  720. TestSuccessfulPreloadParsing("Legacy/OneOne", 1, 1);
  721. }
  722. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  723. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_LegacyMultiplePreloadsMultipleBanks_Success)
  724. #else
  725. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_LegacyMultiplePreloadsMultipleBanks_Success)
  726. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  727. {
  728. TestSuccessfulPreloadParsing("Legacy/MultipleMultiple", 2, 2);
  729. }
  730. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  731. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_LegacyMultiplePreloadsOneBank_Success)
  732. #else
  733. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_LegacyMultiplePreloadsOneBank_Success)
  734. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  735. {
  736. TestSuccessfulPreloadParsing("Legacy/MultipleOne", 2, 1);
  737. }
  738. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  739. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_LegacyOnePreloadMultipleBanks_Success)
  740. #else
  741. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_LegacyOnePreloadMultipleBanks_Success)
  742. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  743. {
  744. TestSuccessfulPreloadParsing("Legacy/OneMultiple", 1, 2);
  745. }
  746. // New Preload Xml Format...
  747. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  748. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_OnePreloadOneBank_Success)
  749. #else
  750. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_OnePreloadOneBank_Success)
  751. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  752. {
  753. TestSuccessfulPreloadParsing("OneOne", 1, 1);
  754. }
  755. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  756. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_MultiplePreloadsMultipleBanks_Success)
  757. #else
  758. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_MultiplePreloadsMultipleBanks_Success)
  759. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  760. {
  761. TestSuccessfulPreloadParsing("MultipleMultiple", 2, 2);
  762. }
  763. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  764. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_MultiplePreloadsOneBank_Success)
  765. #else
  766. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_MultiplePreloadsOneBank_Success)
  767. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  768. {
  769. TestSuccessfulPreloadParsing("MultipleOne", 2, 1);
  770. }
  771. #if AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  772. TEST_F(ATLPreloadXmlParsingTestFixture, DISABLED_ParsePreloadsXml_OnePreloadMultipleBanks_Success)
  773. #else
  774. TEST_F(ATLPreloadXmlParsingTestFixture, ParsePreloadsXml_OnePreloadMultipleBanks_Success)
  775. #endif // AZ_TRAIT_DISABLE_FAILED_AUDIO_SYSTEM_TESTS
  776. {
  777. TestSuccessfulPreloadParsing("OneMultiple", 1, 2);
  778. }
  779. //-----------------------------//
  780. // Test CAudioTranslationLayer //
  781. //-----------------------------//
  782. class ATLTestFixture
  783. : public ::testing::Test
  784. {
  785. public:
  786. void SetUp() override
  787. {
  788. m_requestStatus = EAudioRequestStatus::None;
  789. m_callbackCaller = [this](AudioRequestVariant&& requestVariant)
  790. {
  791. AZStd::visit(
  792. [this](auto&& request)
  793. {
  794. if (request.m_callback)
  795. {
  796. request.m_callback(request);
  797. }
  798. m_requestStatus = request.m_status;
  799. },
  800. requestVariant);
  801. };
  802. m_atl.Initialize();
  803. m_impl.BusConnect();
  804. }
  805. void TearDown() override
  806. {
  807. m_impl.BusDisconnect();
  808. m_atl.ShutDown();
  809. m_callbackCaller.clear();
  810. }
  811. protected:
  812. NiceMock<AudioSystemImplMock> m_impl;
  813. NiceMock<AudioSystemMock> m_sys;
  814. CAudioTranslationLayer m_atl;
  815. CAudioProxy m_proxy;
  816. AudioRequestVariant m_requestHolder;
  817. AZStd::function<void(AudioRequestVariant&&)> m_callbackCaller;
  818. EAudioRequestStatus m_requestStatus;
  819. };
  820. TEST_F(ATLTestFixture, ATLProcessRequest_CheckCallback_WasCalled)
  821. {
  822. Audio::SystemRequest::GetFocus getFocus;
  823. bool callbackRan = false;
  824. getFocus.m_callback = [&callbackRan]([[maybe_unused]] const Audio::SystemRequest::GetFocus& request)
  825. {
  826. callbackRan = true;
  827. };
  828. EXPECT_CALL(m_sys, PushCallback).WillOnce(m_callbackCaller);
  829. m_atl.ProcessRequest(AZStd::move(getFocus));
  830. EXPECT_TRUE(callbackRan);
  831. }
  832. TEST_F(ATLTestFixture, ATLProcessRequest_CheckResult_Matches)
  833. {
  834. Audio::SystemRequest::LoseFocus loseFocus;
  835. loseFocus.m_callback = [](const Audio::SystemRequest::LoseFocus& request)
  836. {
  837. // Force a particular result status...
  838. const_cast<Audio::SystemRequest::LoseFocus&>(request).m_status = EAudioRequestStatus::PartialSuccess;
  839. };
  840. EXPECT_CALL(m_sys, PushCallback).WillOnce(m_callbackCaller);
  841. m_atl.ProcessRequest(AZStd::move(loseFocus));
  842. EXPECT_EQ(m_requestStatus, EAudioRequestStatus::PartialSuccess);
  843. }
  844. TEST_F(ATLTestFixture, ATLProcessRequest_SimulateInitShutdown_ExpectedResults)
  845. {
  846. // Don't need to do anything in the callbacks this time,
  847. // but still need to supply them because it sets the m_requestStatus variable.
  848. // Use the impl mock to simulate a scucessful init/shutdown pair.
  849. // Then check the result.
  850. Audio::SystemRequest::Initialize initialize;
  851. initialize.m_callback = [](const Audio::SystemRequest::Initialize&)
  852. {
  853. };
  854. Audio::SystemRequest::Shutdown shutdown;
  855. shutdown.m_callback = [](const Audio::SystemRequest::Shutdown&)
  856. {
  857. };
  858. EXPECT_CALL(m_sys, PushCallback).WillRepeatedly(m_callbackCaller);
  859. using ::testing::Return;
  860. using ::testing::_;
  861. EXPECT_CALL(m_impl, Initialize).WillOnce(Return(EAudioRequestStatus::Success));
  862. EXPECT_CALL(m_impl, NewGlobalAudioObjectData(_)).WillOnce(Return(nullptr));
  863. EXPECT_CALL(m_impl, GetImplSubPath).WillOnce(Return("test_subpath"));
  864. m_atl.ProcessRequest(AZStd::move(initialize));
  865. EXPECT_EQ(m_requestStatus, EAudioRequestStatus::Success);
  866. m_requestStatus = EAudioRequestStatus::None;
  867. EXPECT_CALL(m_impl, ShutDown).WillOnce(Return(EAudioRequestStatus::Success));
  868. EXPECT_CALL(m_impl, Release).WillOnce(Return(EAudioRequestStatus::Success));
  869. m_atl.ProcessRequest(AZStd::move(shutdown));
  870. EXPECT_EQ(m_requestStatus, EAudioRequestStatus::Success);
  871. }
  872. TEST_F(ATLTestFixture, AudioProxy_SimulateQueuedCommands_NumCommandsExecutedMatches)
  873. {
  874. EXPECT_EQ(m_proxy.GetAudioObjectID(), INVALID_AUDIO_OBJECT_ID);
  875. constexpr TAudioObjectID objectId{ 2000 };
  876. // Setup what PushRequest will do when 'Initialize' is called on the proxy...
  877. EXPECT_CALL(m_sys, PushRequest)
  878. .WillOnce(
  879. [this](AudioRequestVariant&& requestVariant)
  880. {
  881. AZStd::visit(
  882. [](auto&& request)
  883. {
  884. using T = AZStd::decay_t<decltype(request)>;
  885. if constexpr (AZStd::is_same_v<T, Audio::SystemRequest::ReserveObject>)
  886. {
  887. request.m_objectId = objectId;
  888. }
  889. },
  890. requestVariant);
  891. // Hold onto the request before executing the callback so we can queue up additional requests.
  892. m_requestHolder = AZStd::move(requestVariant);
  893. });
  894. // 1. Initialize the audio proxy
  895. m_proxy.Initialize("test_proxy");
  896. // Confirm the proxy object still doesn't have an ID...
  897. EXPECT_EQ(m_proxy.GetAudioObjectID(), INVALID_AUDIO_OBJECT_ID);
  898. constexpr AZ::u32 numCommands = 2;
  899. AZ::u32 commandCount = 0;
  900. // Setup what PushRequests will do when additional commands are queued...
  901. EXPECT_CALL(m_sys, PushRequests)
  902. .WillOnce(
  903. [&commandCount](AudioRequestsQueue& queue)
  904. {
  905. commandCount += aznumeric_cast<AZ::u32>(queue.size());
  906. });
  907. // 2. Call additional commands on the proxy
  908. m_proxy.SetPosition(AZ::Vector3::CreateOne());
  909. m_proxy.SetRtpcValue(TAudioControlID{ 123 }, 0.765f);
  910. // Calling functions on the proxy before it's received an ID
  911. // shouldn't get pushed to the audio system yet.
  912. EXPECT_EQ(commandCount, 0);
  913. // 3. Now execute the initialize callback, which "gives" the ID to the proxy
  914. // and will also execute the queued commands.
  915. m_callbackCaller(AZStd::move(m_requestHolder));
  916. // Check that the proxy has the expected ID and expected number of commands
  917. // were fake-pushed.
  918. EXPECT_EQ(m_proxy.GetAudioObjectID(), objectId);
  919. EXPECT_EQ(commandCount, numCommands);
  920. // Resets data on the audio proxy object
  921. EXPECT_CALL(m_sys, PushRequest).WillOnce(::testing::Return());
  922. m_proxy.Release();
  923. }