ApplicationManagerTests.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "ApplicationManagerTests.h"
  9. #include <QCoreApplication>
  10. #include <tests/assetmanager/MockAssetProcessorManager.h>
  11. #include <tests/assetmanager/MockFileProcessor.h>
  12. #include <AzToolsFramework/Archive/ArchiveComponent.h>
  13. #include <native/FileWatcher/FileWatcher.h>
  14. #include <native/utilities/AssetServerHandler.h>
  15. #include <native/resourcecompiler/rcjob.h>
  16. #include <AzCore/Utils/Utils.h>
  17. #include <AzCore/Settings/SettingsRegistryImpl.h>
  18. #include <unittests/UnitTestUtils.h>
  19. namespace UnitTests
  20. {
  21. void ApplicationManagerTest::SetUp()
  22. {
  23. LeakDetectionFixture::SetUp();
  24. AZ::IO::Path assetRootDir(m_databaseLocationListener.GetAssetRootDir());
  25. // We need a QCoreApplication to run the event loop
  26. int argc = 0;
  27. m_coreApplication = AZStd::make_unique<QCoreApplication>(argc, nullptr);
  28. m_applicationManager = AZStd::make_unique<MockBatchApplicationManager>(&argc, nullptr);
  29. m_applicationManager->m_platformConfiguration = new AssetProcessor::PlatformConfiguration{ nullptr };
  30. m_applicationManager->m_fileStateCache = AZStd::make_unique<AssetProcessor::FileStateCache>();
  31. m_mockAPM = AZStd::make_unique<MockAssetProcessorManager>(m_applicationManager->m_platformConfiguration, nullptr);
  32. m_applicationManager->m_assetProcessorManager = m_mockAPM.get();
  33. AZStd::vector<AssetBuilderSDK::PlatformInfo> platforms;
  34. m_applicationManager->m_platformConfiguration->EnablePlatform(AssetBuilderSDK::PlatformInfo{ "pc", { "tag" } });
  35. m_applicationManager->m_platformConfiguration->PopulatePlatformsForScanFolder(platforms);
  36. m_applicationManager->m_platformConfiguration->AddScanFolder(
  37. AssetProcessor::ScanFolderInfo{ assetRootDir.c_str(), "test", "test", true, true, platforms });
  38. m_apmThread = AZStd::make_unique<QThread>(nullptr);
  39. m_apmThread->setObjectName("APM Thread");
  40. m_applicationManager->m_assetProcessorManager->moveToThread(m_apmThread.get());
  41. m_apmThread->start();
  42. m_fileProcessorThread = AZStd::make_unique<QThread>(nullptr);
  43. m_fileProcessorThread->setObjectName("File Processor Thread");
  44. auto fileProcessor = AZStd::make_unique<MockFileProcessor>(m_applicationManager->m_platformConfiguration);
  45. fileProcessor->moveToThread(m_fileProcessorThread.get());
  46. m_mockFileProcessor = fileProcessor.get();
  47. m_applicationManager->m_fileProcessor = AZStd::move(fileProcessor); // The manager is taking ownership
  48. m_fileProcessorThread->start();
  49. m_applicationManager->InitUuidManager();
  50. auto fileWatcher = AZStd::make_unique<FileWatcher>();
  51. m_fileWatcher = fileWatcher.get();
  52. // This is what we're testing, it will set up connections between the fileWatcher and the 2 QObject handlers we'll check
  53. m_applicationManager->InitFileMonitor(AZStd::move(fileWatcher)); // The manager is going to take ownership of the file watcher
  54. m_applicationManager->InitUuidManager();
  55. }
  56. void ApplicationManagerTest::TearDown()
  57. {
  58. m_applicationManager->DestroyFileMonitor();
  59. m_apmThread->quit();
  60. m_fileProcessorThread->quit();
  61. m_apmThread->wait();
  62. m_fileProcessorThread->wait();
  63. m_mockAPM = nullptr;
  64. LeakDetectionFixture::TearDown();
  65. }
  66. using BatchApplicationManagerTest = ::UnitTest::LeakDetectionFixture;
  67. TEST_F(ApplicationManagerTest, FileWatcherEventsTriggered_ProperlySignalledOnCorrectThread_SUITE_sandbox)
  68. {
  69. AZ::IO::Path assetRootDir(m_databaseLocationListener.GetAssetRootDir());
  70. Q_EMIT m_fileWatcher->fileAdded((assetRootDir / "test").c_str());
  71. Q_EMIT m_fileWatcher->fileModified((assetRootDir / "test2").c_str());
  72. Q_EMIT m_fileWatcher->fileRemoved((assetRootDir / "test3").c_str());
  73. EXPECT_TRUE(m_mockAPM->m_events[Added].WaitAndCheck()) << "APM Added event failed";
  74. EXPECT_TRUE(m_mockAPM->m_events[Modified].WaitAndCheck()) << "APM Modified event failed";
  75. EXPECT_TRUE(m_mockAPM->m_events[Deleted].WaitAndCheck()) << "APM Deleted event failed";
  76. EXPECT_TRUE(m_mockFileProcessor->m_events[Added].WaitAndCheck()) << "File Processor Added event failed";
  77. EXPECT_TRUE(m_mockFileProcessor->m_events[Deleted].WaitAndCheck()) << "File Processor Deleted event failed";
  78. }
  79. TEST(AssetProcessorAZApplicationTest, AssetProcessorAZApplication_ArchiveComponent_Exists)
  80. {
  81. int argc = 0;
  82. AssetProcessorAZApplication assetProcessorAZApplication {&argc, nullptr};
  83. auto componentList = assetProcessorAZApplication.GetRequiredSystemComponents();
  84. auto iterator = AZStd::find(componentList.begin(), componentList.end(), azrtti_typeid<AzToolsFramework::ArchiveComponent>());
  85. EXPECT_NE(iterator, componentList.end()) << "AzToolsFramework::ArchiveComponent is not a required component";
  86. }
  87. TEST(AssetProcessorAssetServerHandler, AssetServerHandler_FutureCalls_FailsNoExceptions)
  88. {
  89. char executablePath[AZ_MAX_PATH_LEN];
  90. AZ::Utils::GetExecutablePath(executablePath, AZ_MAX_PATH_LEN);
  91. struct MockAssetServerInfoBus final
  92. : public AssetProcessor::AssetServerInfoBus::Handler
  93. {
  94. explicit MockAssetServerInfoBus(const char* filename)
  95. : m_filename(filename)
  96. {
  97. AssetProcessor::AssetServerInfoBus::Handler::BusConnect();
  98. }
  99. ~MockAssetServerInfoBus()
  100. {
  101. AssetProcessor::AssetServerInfoBus::Handler::BusDisconnect();
  102. }
  103. const AZStd::string& ComputeArchiveFilePath(const AssetProcessor::BuilderParams&) override
  104. {
  105. return m_filename;
  106. }
  107. AZStd::string m_filename;
  108. };
  109. AssetProcessor::JobDetails jobDetails;
  110. jobDetails.m_jobEntry.m_sourceFileUUID = AZ::Uuid::CreateRandom();
  111. jobDetails.m_checkServer = true;
  112. AssetProcessor::RCJob rcJob;
  113. rcJob.Init(jobDetails);
  114. AssetProcessor::BuilderParams buildParams { &rcJob };
  115. buildParams.m_processJobRequest.m_sourceFile = executablePath;
  116. buildParams.m_serverKey = "fake.product";
  117. // These should fail, but not throw an exception
  118. // mock storing an archive
  119. {
  120. MockAssetServerInfoBus mockAssetServerInfoBus("fake.asset");
  121. AssetProcessor::AssetServerHandler assetServerHandler{};
  122. AZStd::vector<AZStd::string> files({ buildParams.m_serverKey.toUtf8().toStdString().c_str() });
  123. EXPECT_FALSE(assetServerHandler.StoreJobResult(buildParams, files));
  124. }
  125. // mock retrieving an archive
  126. {
  127. MockAssetServerInfoBus mockAssetServerInfoBus(executablePath);
  128. AssetProcessor::AssetServerHandler assetServerHandler{};
  129. EXPECT_FALSE(assetServerHandler.RetrieveJobResult(buildParams));
  130. }
  131. }
  132. }