SceneBuilderTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 <AssetBuilderSDK/AssetBuilderSDK.h>
  10. #include <AzCore/IO/FileIO.h>
  11. #include <AzCore/IO/Path/Path.h>
  12. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/UnitTest/Mocks/MockFileIOBase.h>
  15. #include <AzCore/UserSettings/UserSettingsComponent.h>
  16. #include <AzFramework/StringFunc/StringFunc.h>
  17. #include <AzToolsFramework/Application/ToolsApplication.h>
  18. #include <SceneAPI/SceneCore/Events/AssetImportRequest.h>
  19. #include <SceneBuilder/SceneBuilderWorker.h>
  20. #include <SceneAPI/SceneCore/Events/ExportProductList.h>
  21. #include <Tests/FileIOBaseTestTypes.h>
  22. using namespace AZ;
  23. using namespace SceneBuilder;
  24. class SceneBuilderTests
  25. : public UnitTest::AllocatorsFixture
  26. , public UnitTest::TraceBusRedirector
  27. {
  28. protected:
  29. void SetUp() override
  30. {
  31. AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
  32. auto projectPathKey =
  33. AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_path";
  34. registry->Set(projectPathKey, "AutomatedTesting");
  35. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*registry);
  36. m_app.Start(AZ::ComponentApplication::Descriptor());
  37. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  38. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  39. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  40. // in the unit tests.
  41. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  42. m_workingDirectory = m_app.GetExecutableFolder();
  43. AZ::IO::FileIOBase::GetInstance()->SetAlias("@products@", m_workingDirectory);
  44. }
  45. void TearDown() override
  46. {
  47. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  48. m_app.Stop();
  49. }
  50. void TestSuccessCase(const SceneAPI::Events::ExportProduct& exportProduct,
  51. const AssetBuilderSDK::ProductPathDependencySet& expectedPathDependencies,
  52. const AZStd::vector<AZ::Uuid>& expectedProductDependencies)
  53. {
  54. SceneBuilderWorker worker;
  55. AssetBuilderSDK::JobProduct jobProduct(exportProduct.m_filename, exportProduct.m_assetType, 0);
  56. worker.PopulateProductDependencies(exportProduct, m_workingDirectory, jobProduct);
  57. ASSERT_EQ(expectedPathDependencies.size(), jobProduct.m_pathDependencies.size());
  58. if (expectedPathDependencies.size() > 0)
  59. {
  60. for (const AssetBuilderSDK::ProductPathDependency& dependency : expectedPathDependencies)
  61. {
  62. ASSERT_TRUE(jobProduct.m_pathDependencies.find(dependency) != jobProduct.m_pathDependencies.end());
  63. }
  64. }
  65. ASSERT_EQ(expectedProductDependencies.size(), jobProduct.m_dependencies.size());
  66. if (expectedProductDependencies.size() > 0)
  67. {
  68. for (const AssetBuilderSDK::ProductDependency& dependency : jobProduct.m_dependencies)
  69. {
  70. ASSERT_TRUE(AZStd::find(expectedProductDependencies.begin(), expectedProductDependencies.end(), dependency.m_dependencyId.m_guid) != expectedProductDependencies.end());
  71. }
  72. }
  73. }
  74. void TestSuccessCase(const SceneAPI::Events::ExportProduct& exportProduct,
  75. const AssetBuilderSDK::ProductPathDependency* expectedPathDependency = nullptr,
  76. const AZ::Uuid* expectedProductDependency = nullptr)
  77. {
  78. AssetBuilderSDK::ProductPathDependencySet expectedPathDependencies;
  79. if (expectedPathDependency)
  80. {
  81. expectedPathDependencies.emplace(*expectedPathDependency);
  82. }
  83. AZStd::vector<AZ::Uuid> expectedProductDependencies;
  84. if (expectedProductDependency)
  85. {
  86. expectedProductDependencies.push_back(*expectedProductDependency);
  87. }
  88. TestSuccessCase(exportProduct, expectedPathDependencies, expectedProductDependencies);
  89. }
  90. void TestSuccessCaseNoDependencies(const SceneAPI::Events::ExportProduct& exportProduct)
  91. {
  92. AssetBuilderSDK::ProductPathDependencySet expectedPathDependencies;
  93. AZStd::vector<AZ::Uuid> expectedProductDependencies;
  94. TestSuccessCase(exportProduct, expectedPathDependencies, expectedProductDependencies);
  95. }
  96. AzToolsFramework::ToolsApplication m_app;
  97. const char* m_workingDirectory = nullptr;
  98. };
  99. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_NoDependencies)
  100. {
  101. SceneAPI::Events::ExportProduct exportProduct("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), u8(0), AZStd::nullopt);
  102. TestSuccessCaseNoDependencies(exportProduct);
  103. }
  104. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_PathDependencyOnSourceAsset)
  105. {
  106. #if AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  107. const char* absolutePathToFile = "C:/some/test/file.mtl";
  108. #else
  109. const char* absolutePathToFile = "/some/test/file.mtl";
  110. #endif // AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  111. AssetBuilderSDK::ProductPathDependency expectedPathDependency(absolutePathToFile, AssetBuilderSDK::ProductPathDependencyType::SourceFile);
  112. SceneAPI::Events::ExportProduct product("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), u8(0), AZStd::nullopt);
  113. product.m_legacyPathDependencies.push_back(absolutePathToFile);
  114. TestSuccessCase(product, &expectedPathDependency);
  115. }
  116. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_PathDependencyOnProductAsset)
  117. {
  118. const char* relativeDependencyPathToFile = "some/test/file.mtl";
  119. AssetBuilderSDK::ProductPathDependency expectedPathDependency(relativeDependencyPathToFile, AssetBuilderSDK::ProductPathDependencyType::ProductFile);
  120. SceneAPI::Events::ExportProduct product("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), u8(0), AZStd::nullopt);
  121. product.m_legacyPathDependencies.push_back(relativeDependencyPathToFile);
  122. TestSuccessCase(product, &expectedPathDependency);
  123. }
  124. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_PathDependencyOnSourceAndProductAsset)
  125. {
  126. const char* relativeDependencyPathToFile = "some/test/file.mtl";
  127. #if AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  128. const char* absolutePathToFile = "C:/some/test/file.mtl";
  129. #else
  130. const char* absolutePathToFile = "/some/test/file.mtl";
  131. #endif // AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  132. SceneAPI::Events::ExportProduct exportProduct("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), u8(0), AZStd::nullopt);
  133. exportProduct.m_legacyPathDependencies.push_back(absolutePathToFile);
  134. exportProduct.m_legacyPathDependencies.push_back(relativeDependencyPathToFile);
  135. AssetBuilderSDK::ProductPathDependencySet expectedPathDependencies = {
  136. {absolutePathToFile, AssetBuilderSDK::ProductPathDependencyType::SourceFile},
  137. {relativeDependencyPathToFile, AssetBuilderSDK::ProductPathDependencyType::ProductFile}
  138. };
  139. TestSuccessCase(exportProduct, expectedPathDependencies, {});
  140. }
  141. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_ProductDependency)
  142. {
  143. AZ::Uuid dependencyId = AZ::Uuid::CreateRandom();
  144. SceneAPI::Events::ExportProduct exportProduct("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), AZ::u8(0), AZStd::nullopt);
  145. exportProduct.m_productDependencies.push_back(SceneAPI::Events::ExportProduct("testDependencyFile", dependencyId, AZ::Data::AssetType::CreateNull(), AZ::u8(0), AZStd::nullopt));
  146. TestSuccessCase(exportProduct, nullptr, &dependencyId);
  147. }
  148. TEST_F(SceneBuilderTests, SceneBuilderWorker_ExportProductDependencies_ProductAndPathDependencies)
  149. {
  150. AZ::Uuid dependencyId = AZ::Uuid::CreateRandom();
  151. SceneAPI::Events::ExportProduct exportProduct("testExportFile", AZ::Uuid::CreateRandom(), AZ::Data::AssetType::CreateNull(), AZ::u8(0), AZStd::nullopt);
  152. exportProduct.m_productDependencies.push_back(SceneAPI::Events::ExportProduct("testDependencyFile", dependencyId, AZ::Data::AssetType::CreateNull(), AZ::u8(0), AZStd::nullopt));
  153. const char* relativeDependencyPathToFile = "some/test/file.mtl";
  154. #if AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  155. const char* absolutePathToFile = "C:/some/test/file.mtl";
  156. #else
  157. const char* absolutePathToFile = "/some/test/file.mtl";
  158. #endif // AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS
  159. exportProduct.m_legacyPathDependencies.push_back(absolutePathToFile);
  160. exportProduct.m_legacyPathDependencies.push_back(relativeDependencyPathToFile);
  161. AssetBuilderSDK::ProductPathDependencySet expectedPathDependencies = {
  162. {absolutePathToFile, AssetBuilderSDK::ProductPathDependencyType::SourceFile},
  163. {relativeDependencyPathToFile, AssetBuilderSDK::ProductPathDependencyType::ProductFile}
  164. };
  165. TestSuccessCase(exportProduct, expectedPathDependencies, { dependencyId });
  166. }
  167. struct ImportHandler
  168. : SceneAPI::Events::AssetImportRequestBus::Handler
  169. {
  170. ImportHandler()
  171. {
  172. BusConnect();
  173. }
  174. ~ImportHandler() override
  175. {
  176. BusDisconnect();
  177. }
  178. void GetManifestDependencyPaths(AZStd::vector<AZStd::string>& paths) override
  179. {
  180. paths.emplace_back("/scriptFilename");
  181. paths.emplace_back("/layer1/layer2/0/target");
  182. }
  183. void GetManifestExtension(AZStd::string& result) override
  184. {
  185. result = ".test";
  186. }
  187. void GetGeneratedManifestExtension(AZStd::string& result) override
  188. {
  189. result = ".test.gen";
  190. }
  191. };
  192. using SourceDependencyTests = UnitTest::ScopedAllocatorSetupFixture;
  193. namespace SourceDependencyJson
  194. {
  195. constexpr const char* TestJson = R"JSON(
  196. {
  197. "values": [
  198. {
  199. "$type": "Test1",
  200. "scriptFilename": "a/test/path.png"
  201. },
  202. {
  203. "$type": "Test2",
  204. "layer1" : {
  205. "layer2" : [
  206. {
  207. "target": "value.png",
  208. "otherData": "value2.png"
  209. },
  210. {
  211. "target" : "wrong.png"
  212. }
  213. ]
  214. }
  215. }
  216. ]
  217. }
  218. )JSON";
  219. }
  220. TEST_F(SourceDependencyTests, SourceDependencyTest)
  221. {
  222. ImportHandler handler;
  223. AZStd::vector<AssetBuilderSDK::SourceFileDependency> dependencies;
  224. SceneBuilderWorker::PopulateSourceDependencies(SourceDependencyJson::TestJson, dependencies);
  225. ASSERT_EQ(dependencies.size(), 2);
  226. ASSERT_STREQ(dependencies[0].m_sourceFileDependencyPath.c_str(), "a/test/path.png");
  227. ASSERT_STREQ(dependencies[1].m_sourceFileDependencyPath.c_str(), "value.png");
  228. }
  229. struct SettingsRegistryMock : AZ::Interface<SettingsRegistryInterface>::Registrar
  230. {
  231. bool Get(FixedValueString& result, AZStd::string_view) const override
  232. {
  233. result = "cache";
  234. return true;
  235. }
  236. void SetApplyPatchSettings(const AZ::JsonApplyPatchSettings& /*applyPatchSettings*/) override{}
  237. void GetApplyPatchSettings(AZ::JsonApplyPatchSettings& /*applyPatchSettings*/) override{}
  238. MOCK_CONST_METHOD1(GetType, Type (AZStd::string_view));
  239. MOCK_CONST_METHOD2(Visit, bool (Visitor&, AZStd::string_view));
  240. MOCK_CONST_METHOD2(Visit, bool (const VisitorCallback&, AZStd::string_view));
  241. MOCK_METHOD1(RegisterNotifier, NotifyEventHandler (const NotifyCallback&));
  242. MOCK_METHOD1(RegisterNotifier, NotifyEventHandler (NotifyCallback&&));
  243. MOCK_METHOD1(RegisterPreMergeEvent, PreMergeEventHandler (const PreMergeEventCallback&));
  244. MOCK_METHOD1(RegisterPreMergeEvent, PreMergeEventHandler (PreMergeEventCallback&&));
  245. MOCK_METHOD1(RegisterPostMergeEvent, PostMergeEventHandler (const PostMergeEventCallback&));
  246. MOCK_METHOD1(RegisterPostMergeEvent, PostMergeEventHandler (PostMergeEventCallback&&));
  247. MOCK_CONST_METHOD2(Get, bool (bool&, AZStd::string_view));
  248. MOCK_CONST_METHOD2(Get, bool (s64&, AZStd::string_view));
  249. MOCK_CONST_METHOD2(Get, bool (u64&, AZStd::string_view));
  250. MOCK_CONST_METHOD2(Get, bool (double&, AZStd::string_view));
  251. MOCK_CONST_METHOD2(Get, bool (AZStd::string&, AZStd::string_view));
  252. MOCK_CONST_METHOD3(GetObject, bool (void*, Uuid, AZStd::string_view));
  253. MOCK_METHOD2(Set, bool (AZStd::string_view, bool));
  254. MOCK_METHOD2(Set, bool (AZStd::string_view, s64));
  255. MOCK_METHOD2(Set, bool (AZStd::string_view, u64));
  256. MOCK_METHOD2(Set, bool (AZStd::string_view, double));
  257. MOCK_METHOD2(Set, bool (AZStd::string_view, AZStd::string_view));
  258. MOCK_METHOD2(Set, bool (AZStd::string_view, const char*));
  259. MOCK_METHOD3(SetObject, bool (AZStd::string_view, const void*, Uuid));
  260. MOCK_METHOD1(Remove, bool (AZStd::string_view));
  261. MOCK_METHOD3(MergeCommandLineArgument, bool (AZStd::string_view, AZStd::string_view, const CommandLineArgumentSettings&));
  262. MOCK_METHOD2(MergeSettings, bool (AZStd::string_view, Format));
  263. MOCK_METHOD4(MergeSettingsFile, bool (AZStd::string_view, Format, AZStd::string_view, AZStd::vector<char>*));
  264. MOCK_METHOD5(MergeSettingsFolder, bool (AZStd::string_view, const Specializations&, AZStd::string_view, AZStd::string_view, AZStd::vector<char>*));
  265. MOCK_METHOD1(SetUseFileIO, void (bool));
  266. };
  267. struct SourceDependencyMockedIOTests : UnitTest::ScopedAllocatorSetupFixture
  268. , UnitTest::SetRestoreFileIOBaseRAII
  269. {
  270. SourceDependencyMockedIOTests()
  271. : UnitTest::SetRestoreFileIOBaseRAII(m_ioMock)
  272. {
  273. }
  274. void SetUp() override
  275. {
  276. using namespace ::testing;
  277. ON_CALL(m_ioMock, Open(_, _, _))
  278. .WillByDefault(Invoke(
  279. [](auto, auto, IO::HandleType& handle)
  280. {
  281. handle = 1234;
  282. return AZ::IO::Result(AZ::IO::ResultCode::Success);
  283. }));
  284. ON_CALL(m_ioMock, Size(An<AZ::IO::HandleType>(), _)).WillByDefault(Invoke([](auto, AZ::u64& size)
  285. {
  286. size = strlen(SourceDependencyJson::TestJson);
  287. return AZ::IO::ResultCode::Success;
  288. }));
  289. EXPECT_CALL(m_ioMock, Read(_, _, _, _, _))
  290. .WillRepeatedly(Invoke(
  291. [](auto, void* buffer, auto, auto, AZ::u64* bytesRead)
  292. {
  293. memcpy(buffer, SourceDependencyJson::TestJson, strlen(SourceDependencyJson::TestJson));
  294. *bytesRead = strlen(SourceDependencyJson::TestJson);
  295. return AZ::IO::ResultCode::Success;
  296. }));
  297. EXPECT_CALL(m_ioMock, Close(_)).WillRepeatedly(Return(AZ::IO::ResultCode::Success));
  298. }
  299. IO::NiceFileIOBaseMock m_ioMock;
  300. };
  301. TEST_F(SourceDependencyMockedIOTests, RegularManifestHasPriority)
  302. {
  303. ImportHandler handler;
  304. SettingsRegistryMock settingsRegistry;
  305. AssetBuilderSDK::CreateJobsRequest request;
  306. AssetBuilderSDK::CreateJobsResponse response;
  307. request.m_sourceFile = "file.fbx";
  308. using namespace ::testing;
  309. AZStd::string genPath = AZStd::string("cache").append(1, AZ_TRAIT_OS_PATH_SEPARATOR).append("file.fbx.test.gen");
  310. EXPECT_CALL(m_ioMock, Exists(StrEq("file.fbx.test"))).WillRepeatedly(Return(true));
  311. EXPECT_CALL(m_ioMock, Exists(StrEq(genPath.c_str()))).Times(Exactly(0));
  312. ASSERT_TRUE(SceneBuilderWorker::ManifestDependencyCheck(request, response));
  313. ASSERT_EQ(response.m_sourceFileDependencyList.size(), 2);
  314. }
  315. TEST_F(SourceDependencyMockedIOTests, GeneratedManifestTest)
  316. {
  317. ImportHandler handler;
  318. SettingsRegistryMock settingsRegistry;
  319. AssetBuilderSDK::CreateJobsRequest request;
  320. AssetBuilderSDK::CreateJobsResponse response;
  321. request.m_sourceFile = "file.fbx";
  322. using namespace ::testing;
  323. AZStd::string genPath = AZStd::string("cache").append(1, AZ_TRAIT_OS_PATH_SEPARATOR).append("file.fbx.test.gen");
  324. EXPECT_CALL(m_ioMock, Exists(StrEq("file.fbx.test"))).WillRepeatedly(Return(false));
  325. EXPECT_CALL(m_ioMock, Exists(StrEq(genPath.c_str()))).WillRepeatedly(Return(true));
  326. ASSERT_TRUE(SceneBuilderWorker::ManifestDependencyCheck(request, response));
  327. ASSERT_EQ(response.m_sourceFileDependencyList.size(), 2);
  328. }