SceneBuilderTests.cpp 15 KB

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