NodeReplacementSystemTest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 <AzCore/Settings/SettingsRegistryImpl.h>
  9. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  10. #include <AzCore/UnitTest/MockComponentApplication.h>
  11. #include <Editor/Include/ScriptCanvas/Components/NodeReplacementSystem.h>
  12. #include <Include/ScriptCanvas/Core/Node.h>
  13. #include <Include/ScriptCanvas/Libraries/Core/Method.h>
  14. #include <Include/ScriptCanvas/Utils/VersioningUtils.h>
  15. #include <Tests/Framework/ScriptCanvasUnitTestFixture.h>
  16. namespace ScriptCanvasEditorUnitTest
  17. {
  18. using namespace AZ;
  19. using namespace ScriptCanvas;
  20. using namespace ScriptCanvasEditor;
  21. static constexpr const int ValidGraphId = 1234567890;
  22. static constexpr const char ValidClassMethodNodeKey1[] = "{E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF}_Old Test Class1_Old Test Method1";
  23. static constexpr const char ValidFreeMethodNodeKey1[] = "{E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF}_Old Test Free Method1";
  24. static constexpr const char ValidFreeMethodNodeKey2[] = "{E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF}_Old Test Free Method2";
  25. static constexpr const char ValidOldCustomNodeKey[] = "{F1030112-BA70-4786-BBEB-43ACADA5B846}";
  26. static constexpr const char ValidNewMethodNodeKey[] = "{E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF}";
  27. static constexpr const char ValidNodeReplacement1[] =
  28. R"({
  29. "O3DE": {
  30. "NodeReplacement": {
  31. "ScriptCanvas1": [
  32. {
  33. "OldNode" : {
  34. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  35. "Class": "Old Test Class1",
  36. "Method": "Old Test Method1"
  37. },
  38. "NewNode" : {
  39. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  40. "Class": "New Test Class1",
  41. "Method": "New Test Method1"
  42. }
  43. },
  44. {
  45. "OldNode" : {
  46. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  47. "Method": "Old Test Free Method1"
  48. },
  49. "NewNode" : {
  50. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  51. "Class": "New Test Class1",
  52. "Method": "New Test Method1"
  53. }
  54. }
  55. ]
  56. }
  57. }
  58. })";
  59. static constexpr const char ValidNodeReplacement2[] =
  60. R"({
  61. "O3DE": {
  62. "NodeReplacement": {
  63. "ScriptCanvas2": [
  64. {
  65. "OldNode" : {
  66. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  67. "Method": "Old Test Free Method2"
  68. },
  69. "NewNode" : {
  70. "Uuid": "E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF",
  71. "Class": "New Test Class2",
  72. "Method": "New Test Method2"
  73. }
  74. }
  75. ]
  76. }
  77. }
  78. })";
  79. class OldTestClass
  80. {
  81. public:
  82. AZ_RTTI(OldTestClass, "{A34DB600-4479-4FAC-A049-93FC6AB7C5D0}");
  83. AZ_CLASS_ALLOCATOR(OldTestClass, AZ::SystemAllocator);
  84. OldTestClass() = default;
  85. virtual ~OldTestClass() = default;
  86. void OldTestMethod() {}
  87. };
  88. class OldTestCustomNode
  89. : public Node
  90. {
  91. public:
  92. AZ_COMPONENT(OldTestCustomNode, ValidOldCustomNodeKey, Node);
  93. };
  94. class ScriptCanvasEditorUnitTest
  95. : public ScriptCanvasUnitTest::ScriptCanvasUnitTestFixture
  96. {
  97. public:
  98. void SetUp() override
  99. {
  100. ScriptCanvasUnitTest::ScriptCanvasUnitTestFixture::SetUp();
  101. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  102. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  103. m_componentApplicationMock = AZStd::make_unique<testing::NiceMock<UnitTest::MockComponentApplication>>();
  104. }
  105. void TearDown() override
  106. {
  107. m_componentApplicationMock.reset();
  108. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  109. m_settingsRegistry.reset();
  110. ScriptCanvasUnitTest::ScriptCanvasUnitTestFixture::TearDown();
  111. }
  112. protected:
  113. AZStd::unique_ptr<AZ::SettingsRegistryImpl> m_settingsRegistry;
  114. AZStd::unique_ptr<testing::NiceMock<UnitTest::MockComponentApplication>> m_componentApplicationMock;
  115. };
  116. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingClassMethodMetadata)
  117. {
  118. auto replacementId = NodeReplacementSystem::GenerateReplacementId(
  119. AZ::Uuid::CreateString("E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF"), "Old Test Class1", "Old Test Method1");
  120. EXPECT_EQ(replacementId, ValidClassMethodNodeKey1);
  121. }
  122. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingClassMethodMetadataFromJson)
  123. {
  124. auto replacementId = NodeReplacementSystem::GenerateReplacementId(
  125. AZ::Uuid::CreateString("E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF"), "Old Test Class1", "Old Test Method1");
  126. EXPECT_EQ(replacementId, ValidClassMethodNodeKey1);
  127. }
  128. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingFreeMethodMetadata)
  129. {
  130. auto replacementId = NodeReplacementSystem::GenerateReplacementId(
  131. AZ::Uuid::CreateString("E42861BD-1956-45AE-8DD7-CCFC1E3E5ACF"), "", "Old Test Free Method1");
  132. EXPECT_EQ(replacementId, ValidFreeMethodNodeKey1);
  133. }
  134. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingCustomNodeMetadata)
  135. {
  136. AZ::Uuid testUuid = AZ::Uuid::CreateRandom();
  137. auto replacementId = NodeReplacementSystem::GenerateReplacementId(testUuid);
  138. EXPECT_EQ(replacementId, testUuid.ToFixedString().c_str());
  139. }
  140. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingClassMethodNode)
  141. {
  142. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  143. .WillByDefault(
  144. []([[maybe_unused]] AZ::Entity*)
  145. {
  146. return true;
  147. });
  148. AZ::BehaviorContext testBehaviorContext;
  149. testBehaviorContext.Class<OldTestClass>("Old Test Class1").Method("Old Test Method1", &OldTestClass::OldTestMethod);
  150. ON_CALL(*m_componentApplicationMock, GetBehaviorContext())
  151. .WillByDefault(
  152. [&testBehaviorContext]()
  153. {
  154. return &testBehaviorContext;
  155. });
  156. Nodes::Core::Method* testMethodNode = aznew Nodes::Core::Method();
  157. AZ::Entity* testMethodEntity = aznew AZ::Entity();
  158. testMethodEntity->Init();
  159. testMethodEntity->AddComponent(testMethodNode);
  160. testMethodNode->InitializeBehaviorMethod({}, "Old Test Class1", "Old Test Method1", PropertyStatus::None);
  161. auto replacementId = NodeReplacementSystem::GenerateReplacementId(testMethodNode);
  162. EXPECT_EQ(replacementId, ValidClassMethodNodeKey1);
  163. testMethodEntity->Reset();
  164. delete testMethodEntity;
  165. testMethodEntity = nullptr;
  166. testMethodNode = nullptr;
  167. }
  168. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingFreeMethodNode)
  169. {
  170. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  171. .WillByDefault(
  172. []([[maybe_unused]] AZ::Entity*)
  173. {
  174. return true;
  175. });
  176. AZ::BehaviorContext testBehaviorContext;
  177. testBehaviorContext.Method("Old Test Free Method1", [](){});
  178. ON_CALL(*m_componentApplicationMock, GetBehaviorContext())
  179. .WillByDefault(
  180. [&testBehaviorContext]()
  181. {
  182. return &testBehaviorContext;
  183. });
  184. Nodes::Core::Method* testMethodNode = aznew Nodes::Core::Method();
  185. AZ::Entity* testMethodEntity = aznew AZ::Entity();
  186. testMethodEntity->Init();
  187. testMethodEntity->AddComponent(testMethodNode);
  188. testMethodNode->InitializeBehaviorMethod({}, "", "Old Test Free Method1", PropertyStatus::None);
  189. auto replacementId = NodeReplacementSystem::GenerateReplacementId(testMethodNode);
  190. EXPECT_EQ(replacementId, ValidFreeMethodNodeKey1);
  191. testMethodEntity->Reset();
  192. delete testMethodEntity;
  193. testMethodEntity = nullptr;
  194. testMethodNode = nullptr;
  195. }
  196. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetExpectedKey_WhileGivingCustomNode)
  197. {
  198. OldTestCustomNode* testCustomNode = aznew OldTestCustomNode();
  199. auto replacementId = NodeReplacementSystem::GenerateReplacementId(testCustomNode);
  200. EXPECT_EQ(replacementId, ValidOldCustomNodeKey);
  201. delete testCustomNode;
  202. testCustomNode = nullptr;
  203. }
  204. TEST_F(ScriptCanvasEditorUnitTest, GenerateReplacementId_GetEmptyKey_WhileGivingNullPointer)
  205. {
  206. auto replacementId = NodeReplacementSystem::GenerateReplacementId(nullptr);
  207. EXPECT_EQ(replacementId, "");
  208. }
  209. TEST_F(ScriptCanvasEditorUnitTest, GetNodeReplacementConfiguration_GetValidConfig_WhileLookingForExistingMethodKey)
  210. {
  211. m_settingsRegistry->MergeSettings(ValidNodeReplacement1, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  212. m_settingsRegistry->MergeSettings(ValidNodeReplacement2, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  213. NodeReplacementSystem testSystem;
  214. testSystem.LoadReplacementMetadata();
  215. auto config = testSystem.GetNodeReplacementConfiguration(ValidClassMethodNodeKey1);
  216. EXPECT_TRUE(config.IsValid());
  217. EXPECT_EQ(config.m_className, "New Test Class1");
  218. EXPECT_EQ(config.m_methodName, "New Test Method1");
  219. }
  220. TEST_F(ScriptCanvasEditorUnitTest, GetNodeReplacementConfiguration_GetValidConfig_WhileLoadingMultipleNodeReplacement)
  221. {
  222. m_settingsRegistry->MergeSettings(ValidNodeReplacement1, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  223. m_settingsRegistry->MergeSettings(ValidNodeReplacement2, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  224. NodeReplacementSystem testSystem;
  225. testSystem.LoadReplacementMetadata();
  226. auto config = testSystem.GetNodeReplacementConfiguration(ValidFreeMethodNodeKey2);
  227. EXPECT_TRUE(config.IsValid());
  228. EXPECT_EQ(config.m_className, "New Test Class2");
  229. EXPECT_EQ(config.m_methodName, "New Test Method2");
  230. }
  231. TEST_F(ScriptCanvasEditorUnitTest, GetNodeReplacementConfiguration_GetInvalidConfig_WhileLookingForNonExistingKey)
  232. {
  233. m_settingsRegistry->MergeSettings(ValidNodeReplacement1, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  234. NodeReplacementSystem testSystem;
  235. testSystem.LoadReplacementMetadata();
  236. auto config = testSystem.GetNodeReplacementConfiguration("");
  237. EXPECT_FALSE(config.IsValid());
  238. }
  239. TEST_F(ScriptCanvasEditorUnitTest, LoadReplacementMetadata_GetValidConfig_WhileBroadcastResultAfterLoading)
  240. {
  241. m_settingsRegistry->MergeSettings(ValidNodeReplacement1, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  242. NodeReplacementSystem testSystem;
  243. testSystem.LoadReplacementMetadata();
  244. NodeReplacementConfiguration config;
  245. ScriptCanvasEditor::NodeReplacementRequestBus::BroadcastResult(
  246. config, &ScriptCanvasEditor::NodeReplacementRequestBus::Events::GetNodeReplacementConfiguration, ValidClassMethodNodeKey1);
  247. EXPECT_TRUE(config.IsValid());
  248. EXPECT_EQ(config.m_className, "New Test Class1");
  249. EXPECT_EQ(config.m_methodName, "New Test Method1");
  250. }
  251. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetEmptyReport_WhileGraphIdInvalid)
  252. {
  253. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  254. .WillByDefault(
  255. []([[maybe_unused]] AZ::Entity*)
  256. {
  257. return true;
  258. });
  259. NodeReplacementConfiguration config;
  260. OldTestCustomNode* testCustomNode = aznew OldTestCustomNode();
  261. AZ::Entity* testCustomEntity = aznew AZ::Entity();
  262. testCustomEntity->Init();
  263. testCustomEntity->AddComponent(testCustomNode);
  264. NodeReplacementSystem testSystem;
  265. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(), testCustomNode, config);
  266. EXPECT_TRUE(report.IsEmpty());
  267. delete testCustomNode;
  268. testCustomNode = nullptr;
  269. delete testCustomEntity;
  270. testCustomEntity = nullptr;
  271. }
  272. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetEmptyReport_WhileNodeIsNotAttachedToEntity)
  273. {
  274. NodeReplacementConfiguration config;
  275. OldTestCustomNode* testCustomNode = aznew OldTestCustomNode();
  276. NodeReplacementSystem testSystem;
  277. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testCustomNode, config);
  278. EXPECT_TRUE(report.IsEmpty());
  279. delete testCustomNode;
  280. testCustomNode = nullptr;
  281. }
  282. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetEmptyReport_WhileSerializeContextIsNull)
  283. {
  284. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  285. .WillByDefault(
  286. []([[maybe_unused]] AZ::Entity*)
  287. {
  288. return true;
  289. });
  290. ON_CALL(*m_componentApplicationMock, GetSerializeContext())
  291. .WillByDefault(
  292. []()
  293. {
  294. return nullptr;
  295. });
  296. NodeReplacementConfiguration config;
  297. OldTestCustomNode* testCustomNode = aznew OldTestCustomNode();
  298. AZ::Entity* testCustomEntity = aznew AZ::Entity();
  299. testCustomEntity->Init();
  300. testCustomEntity->AddComponent(testCustomNode);
  301. NodeReplacementSystem testSystem;
  302. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testCustomNode, config);
  303. EXPECT_TRUE(report.IsEmpty());
  304. delete testCustomNode;
  305. testCustomNode = nullptr;
  306. delete testCustomEntity;
  307. testCustomEntity = nullptr;
  308. }
  309. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetEmptyReport_WhileSerializeContextHasNoReplacementNode)
  310. {
  311. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  312. .WillByDefault(
  313. []([[maybe_unused]] AZ::Entity*)
  314. {
  315. return true;
  316. });
  317. AZ::SerializeContext testSerializeContext;
  318. ON_CALL(*m_componentApplicationMock, GetSerializeContext())
  319. .WillByDefault(
  320. [&testSerializeContext]()
  321. {
  322. return &testSerializeContext;
  323. });
  324. NodeReplacementConfiguration config;
  325. config.m_type = AZ::Uuid::CreateRandom();
  326. OldTestCustomNode* testCustomNode = aznew OldTestCustomNode();
  327. AZ::Entity* testCustomEntity = aznew AZ::Entity();
  328. testCustomEntity->Init();
  329. testCustomEntity->AddComponent(testCustomNode);
  330. NodeReplacementSystem testSystem;
  331. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testCustomNode, config);
  332. EXPECT_TRUE(report.IsEmpty());
  333. delete testCustomNode;
  334. testCustomNode = nullptr;
  335. delete testCustomEntity;
  336. testCustomEntity = nullptr;
  337. }
  338. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetValidReport_WhileNoDataSlotMethodTopologyMatch)
  339. {
  340. AZ::BehaviorContext testBehaviorContext;
  341. testBehaviorContext.Method("Old Test Free Method", [](){});
  342. testBehaviorContext.Method("New Test Free Method", [](){});
  343. ON_CALL(*m_componentApplicationMock, GetBehaviorContext())
  344. .WillByDefault(
  345. [&testBehaviorContext]()
  346. {
  347. return &testBehaviorContext;
  348. });
  349. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  350. .WillByDefault(
  351. []([[maybe_unused]] AZ::Entity*)
  352. {
  353. return true;
  354. });
  355. AZ::SerializeContext testSerializeContext;
  356. Nodes::Core::Method::Reflect(&testSerializeContext);
  357. ON_CALL(*m_componentApplicationMock, GetSerializeContext())
  358. .WillByDefault(
  359. [&testSerializeContext]()
  360. {
  361. return &testSerializeContext;
  362. });
  363. NodeReplacementConfiguration config;
  364. config.m_type = AZ::Uuid(ValidNewMethodNodeKey);
  365. config.m_methodName = "New Test Free Method";
  366. Nodes::Core::Method* testMethodNode = aznew ScriptCanvas::Nodes::Core::Method();
  367. AZ::Entity* testMethodEntity = aznew AZ::Entity();
  368. testMethodEntity->Init();
  369. testMethodEntity->AddComponent(testMethodNode);
  370. testMethodNode->InitializeBehaviorMethod({}, "", "Old Test Free Method", PropertyStatus::None);
  371. NodeReplacementSystem testSystem;
  372. AZ_TEST_START_TRACE_SUPPRESSION;
  373. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testMethodNode, config);
  374. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  375. EXPECT_FALSE(report.IsEmpty());
  376. EXPECT_EQ(report.m_oldSlotsToNewSlots.size(), 2); // two execution slots
  377. testMethodNode = nullptr;
  378. testMethodEntity->Reset();
  379. delete testMethodEntity;
  380. testMethodEntity = nullptr;
  381. report.m_newNode = nullptr;
  382. }
  383. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetValidReport_WhileDataSlotMethodTopologyMatch)
  384. {
  385. AZ::BehaviorContext testBehaviorContext;
  386. testBehaviorContext.Method("Old Test Free Method", [](float input){ return input;});
  387. testBehaviorContext.Method("New Test Free Method", [](float input){ return input;});
  388. ON_CALL(*m_componentApplicationMock, GetBehaviorContext())
  389. .WillByDefault(
  390. [&testBehaviorContext]()
  391. {
  392. return &testBehaviorContext;
  393. });
  394. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  395. .WillByDefault(
  396. []([[maybe_unused]] AZ::Entity*)
  397. {
  398. return true;
  399. });
  400. AZ::SerializeContext testSerializeContext;
  401. Nodes::Core::Method::Reflect(&testSerializeContext);
  402. ON_CALL(*m_componentApplicationMock, GetSerializeContext())
  403. .WillByDefault(
  404. [&testSerializeContext]()
  405. {
  406. return &testSerializeContext;
  407. });
  408. NodeReplacementConfiguration config;
  409. config.m_type = AZ::Uuid(ValidNewMethodNodeKey);
  410. config.m_methodName = "New Test Free Method";
  411. Nodes::Core::Method* testMethodNode = aznew ScriptCanvas::Nodes::Core::Method();
  412. AZ::Entity* testMethodEntity = aznew AZ::Entity();
  413. testMethodEntity->Init();
  414. testMethodEntity->AddComponent(testMethodNode);
  415. testMethodNode->InitializeBehaviorMethod({}, "", "Old Test Free Method", PropertyStatus::None);
  416. NodeReplacementSystem testSystem;
  417. AZ_TEST_START_TRACE_SUPPRESSION;
  418. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testMethodNode, config);
  419. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  420. EXPECT_FALSE(report.IsEmpty());
  421. EXPECT_EQ(report.m_oldSlotsToNewSlots.size(), 4); // two execution slots and two data slots
  422. testMethodNode = nullptr;
  423. testMethodEntity->Reset();
  424. delete testMethodEntity;
  425. testMethodEntity = nullptr;
  426. report.m_newNode = nullptr;
  427. }
  428. TEST_F(ScriptCanvasEditorUnitTest, ReplaceNodeByReplacementConfiguration_GetEmptyReport_WhileMethodTopologyNotMatch)
  429. {
  430. AZ::BehaviorContext testBehaviorContext;
  431. testBehaviorContext.Method("Old Test Free Method", [](AZStd::string input){ return input;});
  432. testBehaviorContext.Method("New Test Free Method", [](float input){ return input;});
  433. ON_CALL(*m_componentApplicationMock, GetBehaviorContext())
  434. .WillByDefault(
  435. [&testBehaviorContext]()
  436. {
  437. return &testBehaviorContext;
  438. });
  439. ON_CALL(*m_componentApplicationMock, AddEntity(::testing::_))
  440. .WillByDefault(
  441. []([[maybe_unused]] AZ::Entity*)
  442. {
  443. return true;
  444. });
  445. AZ::SerializeContext testSerializeContext;
  446. Nodes::Core::Method::Reflect(&testSerializeContext);
  447. ON_CALL(*m_componentApplicationMock, GetSerializeContext())
  448. .WillByDefault(
  449. [&testSerializeContext]()
  450. {
  451. return &testSerializeContext;
  452. });
  453. NodeReplacementConfiguration config;
  454. config.m_type = AZ::Uuid(ValidNewMethodNodeKey);
  455. config.m_methodName = "New Test Free Method";
  456. Nodes::Core::Method* testMethodNode = aznew ScriptCanvas::Nodes::Core::Method();
  457. AZ::Entity* testMethodEntity = aznew AZ::Entity();
  458. testMethodEntity->Init();
  459. testMethodEntity->AddComponent(testMethodNode);
  460. testMethodNode->InitializeBehaviorMethod({}, "", "Old Test Free Method", PropertyStatus::None);
  461. NodeReplacementSystem testSystem;
  462. AZ_TEST_START_TRACE_SUPPRESSION;
  463. auto report = testSystem.ReplaceNodeByReplacementConfiguration(AZ::EntityId(ValidGraphId), testMethodNode, config);
  464. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  465. EXPECT_TRUE(report.IsEmpty());
  466. testMethodEntity->Reset();
  467. delete testMethodEntity;
  468. testMethodEntity = nullptr;
  469. testMethodNode = nullptr;
  470. }
  471. TEST_F(ScriptCanvasEditorUnitTest, UnloadReplacementMetadata_GetInvalidConfig_WhileBroadcastResultAfterUnloading)
  472. {
  473. m_settingsRegistry->MergeSettings(ValidNodeReplacement1, AZ::SettingsRegistryInterface::Format::JsonMergePatch);
  474. NodeReplacementSystem testSystem;
  475. testSystem.LoadReplacementMetadata();
  476. testSystem.UnloadReplacementMetadata();
  477. NodeReplacementConfiguration config;
  478. ScriptCanvasEditor::NodeReplacementRequestBus::BroadcastResult(
  479. config, &ScriptCanvasEditor::NodeReplacementRequestBus::Events::GetNodeReplacementConfiguration, ValidClassMethodNodeKey1);
  480. EXPECT_FALSE(config.IsValid());
  481. }
  482. }