UnitTestUtilities.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #pragma once
  9. #include <utilities/AssetUtilEBusHelper.h>
  10. #include <utilities/assetUtils.h>
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Interface/Interface.h>
  13. #include <gmock/gmock.h>
  14. #include <AzCore/UnitTest/Mocks/MockFileIOBase.h>
  15. #include <AssetManager/FileStateCache.h>
  16. #include <AzToolsFramework/Asset/AssetUtils.h>
  17. namespace UnitTests
  18. {
  19. //! Utility class meant to check that a specific number of errors occur - will cause a test failure if any unexpected errors occur
  20. //! This does not suppress anything unless Begin has been called
  21. struct TraceBusErrorChecker : AZ::Debug::TraceMessageBus::Handler
  22. {
  23. TraceBusErrorChecker();
  24. virtual ~TraceBusErrorChecker();
  25. void Begin();
  26. void End(int expectedCount);
  27. private:
  28. bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override;
  29. bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  30. bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  31. void RecordError(const char* fileName, int line, const char* func, const char* message);
  32. bool m_expectingFailure = false;
  33. AZStd::vector<AZStd::string> m_suppressedMessages;
  34. };
  35. struct MockMultiBuilderInfoHandler : public AssetProcessor::AssetBuilderInfoBus::Handler
  36. {
  37. ~MockMultiBuilderInfoHandler() override;
  38. struct AssetBuilderExtraInfo
  39. {
  40. QString m_jobFingerprint;
  41. QString m_dependencyFilePath;
  42. QString m_jobDependencyFilePath;
  43. QString m_analysisFingerprint;
  44. AZStd::vector<AZ::u32> m_subIdDependencies;
  45. };
  46. //! AssetProcessor::AssetBuilderInfoBus Interface
  47. void GetMatchingBuildersInfo(const AZStd::string& assetPath, AssetProcessor::BuilderInfoList& builderInfoList) override;
  48. void GetAllBuildersInfo(AssetProcessor::BuilderInfoList& builderInfoList) override;
  49. void CreateJobs(
  50. AssetBuilderExtraInfo extraInfo,
  51. const AssetBuilderSDK::CreateJobsRequest& request,
  52. AssetBuilderSDK::CreateJobsResponse& response);
  53. void ProcessJob(
  54. AssetBuilderExtraInfo extraInfo,
  55. const AssetBuilderSDK::ProcessJobRequest& request,
  56. AssetBuilderSDK::ProcessJobResponse& response);
  57. void CreateBuilderDesc(
  58. const QString& builderName,
  59. const QString& builderId,
  60. const AZStd::vector<AssetBuilderSDK::AssetBuilderPattern>& builderPatterns,
  61. const AssetBuilderExtraInfo& extraInfo);
  62. // Use this version if you intend to update the extraInfo struct dynamically (be sure extraInfo does not go out of scope)
  63. void CreateBuilderDescInfoRef(
  64. const QString& builderName,
  65. const QString& builderId,
  66. const AZStd::vector<AssetBuilderSDK::AssetBuilderPattern>& builderPatterns,
  67. const AssetBuilderExtraInfo& extraInfo);
  68. void CreateBuilderDesc(
  69. const QString& builderName,
  70. const QString& builderId,
  71. const AZStd::vector<AssetBuilderSDK::AssetBuilderPattern>& builderPatterns,
  72. const AssetBuilderSDK::CreateJobFunction& createJobsFunction,
  73. const AssetBuilderSDK::ProcessJobFunction& processJobFunction,
  74. AZStd::optional<QString> analysisFingerprint = AZStd::nullopt);
  75. AZStd::vector<AssetUtilities::BuilderFilePatternMatcher> m_matcherBuilderPatterns;
  76. AZStd::unordered_map<AZ::Uuid, AssetBuilderSDK::AssetBuilderDesc> m_builderDescMap;
  77. int m_createJobsCount = 0;
  78. };
  79. class MockComponentApplication
  80. : public AZ::ComponentApplicationBus::Handler
  81. {
  82. public:
  83. MockComponentApplication();
  84. ~MockComponentApplication();
  85. public:
  86. MOCK_METHOD1(FindEntity, AZ::Entity* (const AZ::EntityId&));
  87. MOCK_METHOD1(AddEntity, bool(AZ::Entity*));
  88. MOCK_METHOD0(Destroy, void());
  89. MOCK_METHOD1(RegisterComponentDescriptor, void(const AZ::ComponentDescriptor*));
  90. MOCK_METHOD1(UnregisterComponentDescriptor, void(const AZ::ComponentDescriptor*));
  91. MOCK_METHOD1(RegisterEntityAddedEventHandler, void(AZ::EntityAddedEvent::Handler&));
  92. MOCK_METHOD1(RegisterEntityRemovedEventHandler, void(AZ::EntityRemovedEvent::Handler&));
  93. MOCK_METHOD1(RegisterEntityActivatedEventHandler, void(AZ::EntityActivatedEvent::Handler&));
  94. MOCK_METHOD1(RegisterEntityDeactivatedEventHandler, void(AZ::EntityDeactivatedEvent::Handler&));
  95. MOCK_METHOD1(SignalEntityActivated, void(AZ::Entity*));
  96. MOCK_METHOD1(SignalEntityDeactivated, void(AZ::Entity*));
  97. MOCK_METHOD1(RemoveEntity, bool(AZ::Entity*));
  98. MOCK_METHOD1(DeleteEntity, bool(const AZ::EntityId&));
  99. MOCK_METHOD1(GetEntityName, AZStd::string(const AZ::EntityId&));
  100. MOCK_METHOD1(EnumerateEntities, void(const ComponentApplicationRequests::EntityCallback&));
  101. MOCK_METHOD0(GetApplication, AZ::ComponentApplication* ());
  102. MOCK_METHOD0(GetSerializeContext, AZ::SerializeContext* ());
  103. MOCK_METHOD0(GetJsonRegistrationContext, AZ::JsonRegistrationContext* ());
  104. MOCK_METHOD0(GetBehaviorContext, AZ::BehaviorContext* ());
  105. MOCK_CONST_METHOD0(GetEngineRoot, const char* ());
  106. MOCK_CONST_METHOD0(GetExecutableFolder, const char* ());
  107. MOCK_CONST_METHOD1(QueryApplicationType, void(AZ::ApplicationTypeQuery&));
  108. };
  109. struct MockPathConversion : AZ::Interface<AssetProcessor::IPathConversion>::Registrar
  110. {
  111. MockPathConversion(const char* scanfolder = "c:/somepath");
  112. bool ConvertToRelativePath(QString fullFileName, QString& databaseSourceName, QString& scanFolderName) const override;
  113. const AssetProcessor::ScanFolderInfo* GetScanFolderForFile(const QString& /*fullFileName*/) const override;
  114. const AssetProcessor::ScanFolderInfo* GetScanFolderById(AZ::s64 /*id*/) const override;
  115. void SetScanFolder(const AssetProcessor::ScanFolderInfo& scanFolderInfo);
  116. private:
  117. AssetProcessor::ScanFolderInfo m_scanFolderInfo;
  118. };
  119. struct MockMultiPathConversion : AZ::Interface<AssetProcessor::IPathConversion>::Registrar
  120. {
  121. bool ConvertToRelativePath(QString fullFileName, QString& databaseSourceName, QString& scanFolderName) const override;
  122. const AssetProcessor::ScanFolderInfo* GetScanFolderForFile(const QString& fullFileName) const override;
  123. const AssetProcessor::ScanFolderInfo* GetScanFolderById(AZ::s64 id) const override;
  124. void AddScanfolder(QString path, QString name);
  125. private:
  126. AZStd::vector<AssetProcessor::ScanFolderInfo> m_scanFolderInfo;
  127. };
  128. struct MockVirtualFileIO
  129. {
  130. static constexpr AZ::u32 ComputeHandle(AZ::IO::PathView path);
  131. MockVirtualFileIO();
  132. ~MockVirtualFileIO();
  133. AZ::IO::FileIOBase* m_priorFileIO = nullptr;
  134. AZStd::unordered_map<AZ::IO::HandleType, AZStd::pair<AZStd::string, AZStd::string>> m_mockFiles;
  135. AZStd::unique_ptr<testing::NiceMock<AZ::IO::MockFileIOBase>> m_fileIOMock;
  136. };
  137. struct MockFileStateCache : AssetProcessor::FileStateBase
  138. {
  139. bool GetFileInfo(const QString& absolutePath, AssetProcessor::FileStateInfo* foundFileInfo) const override;
  140. bool Exists(const QString& absolutePath) const override;
  141. bool GetHash(const QString& absolutePath, FileHash* foundHash) override;
  142. void RegisterForDeleteEvent(AZ::Event<AssetProcessor::FileStateInfo>::Handler& handler) override;
  143. AZ::Event<AssetProcessor::FileStateInfo> m_deleteEvent;
  144. };
  145. }