AnimGraphParameterCommandsTests.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <AzCore/UnitTest/TestTypes.h>
  10. #include <EMotionFX/CommandSystem/Source/CommandSystemConfig.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <MCore/Source/Command.h>
  13. #include <MCore/Source/StringIdPool.h>
  14. #include <EMotionFX/CommandSystem/Source/AnimGraphConnectionCommands.h>
  15. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  16. #include <AzCore/std/sort.h>
  17. #include <EMotionFX/Source/Allocators.h>
  18. #include <EMotionFX/Source/ActorInstance.h>
  19. #include <EMotionFX/Source/ActorManager.h>
  20. #include <EMotionFX/Source/AnimGraph.h>
  21. #include <EMotionFX/Source/AnimGraphInstance.h>
  22. #include <EMotionFX/Source/AnimGraphManager.h>
  23. #include <EMotionFX/Source/AnimGraphNode.h>
  24. #include <EMotionFX/Source/AnimGraphParameterCondition.h>
  25. #include <EMotionFX/Source/AnimGraphTagCondition.h>
  26. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  27. #include <EMotionFX/Source/BlendTreeParameterNode.h>
  28. #include <EMotionFX/Source/Parameter/Parameter.h>
  29. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  30. #include <EMotionFX/Source/Parameter/ValueParameter.h>
  31. #include <MCore/Source/LogManager.h>
  32. #include <MCore/Source/AttributeFactory.h>
  33. #include <MCore/Source/ReflectionSerializer.h>
  34. #include <Tests/Matchers.h>
  35. namespace AnimGraphParameterCommandsTests
  36. {
  37. namespace MCore
  38. {
  39. using ::MCore::Command;
  40. using ::MCore::CommandGroup;
  41. using ::MCore::CommandLine;
  42. using ::MCore::CommandSyntax;
  43. using ::MCore::MCORE_MEMCATEGORY_COMMANDSYSTEM;
  44. using ::MCore::MCORE_DEFAULT_ALIGNMENT;
  45. using ::MCore::AlignedAllocate;
  46. using ::MCore::AlignedFree;
  47. using ::MCore::GetStringIdPool;
  48. using ::MCore::ReflectionSerializer;
  49. using ::MCore::LogWarning;
  50. } // namespace MCore
  51. namespace EMotionFX
  52. {
  53. // Import real implementations that are not mocked
  54. using ::EMotionFX::AnimGraphNodeId;
  55. using ::EMotionFX::AnimGraphConnectionId;
  56. using ::EMotionFX::AnimGraphAllocator;
  57. // Forward declare types that will be mocked
  58. class Actor;
  59. class AnimGraph;
  60. class AnimGraphInstance;
  61. class AnimGraphNode;
  62. class AnimGraphObject;
  63. class AnimGraphObjectData;
  64. class AnimGraphStateTransition;
  65. class AnimGraphTransitionCondition;
  66. class GroupParameter;
  67. class Parameter;
  68. class ParameterFactory;
  69. class ValueParameter;
  70. class BlendTreeConnection;
  71. using ParameterVector = AZStd::vector<Parameter*>;
  72. using ValueParameterVector = AZStd::vector<ValueParameter*>;
  73. using GroupParameterVector = AZStd::vector<GroupParameter*>;
  74. } // namespace EMotionFX
  75. namespace CommandSystem
  76. {
  77. using ::CommandSystem::SelectionList;
  78. void DeleteNodeConnection([[maybe_unused]] MCore::CommandGroup* commandGroup, [[maybe_unused]] const EMotionFX::AnimGraphNode* targetNode, [[maybe_unused]] const EMotionFX::BlendTreeConnection* connection, [[maybe_unused]] bool updateUniqueData = true) {}
  79. } // namespace CommandSystem
  80. #include <Tests/Mocks/ActorManager.h>
  81. #include <Tests/Mocks/AnimGraph.h>
  82. #include <Tests/Mocks/AnimGraphInstance.h>
  83. #include <Tests/Mocks/AnimGraphManager.h>
  84. #include <Tests/Mocks/AnimGraphObject.h>
  85. #include <Tests/Mocks/AnimGraphNode.h>
  86. #include <Tests/Mocks/AnimGraphStateTransition.h>
  87. #include <Tests/Mocks/EMotionFXManager.h>
  88. #include <Tests/Mocks/ObjectAffectedByParameterChanges.h>
  89. #include <Tests/Mocks/Parameter.h>
  90. #include <Tests/Mocks/ParameterFactory.h>
  91. #include <Tests/Mocks/GroupParameter.h>
  92. #include <Tests/Mocks/ValueParameter.h>
  93. #include <Tests/Mocks/CommandManager.h>
  94. #include <Tests/Mocks/CommandSystemCommandManager.h>
  95. #include <Tests/Mocks/BlendTreeParameterNode.h>
  96. #include <EMotionFX/CommandSystem/Source/AnimGraphParameterCommands_Interface.inl>
  97. #include <EMotionFX/CommandSystem/Source/AnimGraphParameterCommands_Impl.inl>
  98. class TestParameter
  99. : public EMotionFX::ValueParameter
  100. {
  101. public:
  102. AZ_RTTI(TestParameter, "{6C91B0BE-EFCF-4270-A356-28B1C4612CCE}", EMotionFX::ValueParameter);
  103. };
  104. class AnimGraphParameterCommandsFixture
  105. : public UnitTest::LeakDetectionFixture
  106. {
  107. public:
  108. void SetUp() override
  109. {
  110. UnitTest::LeakDetectionFixture::SetUp();
  111. ::MCore::Initializer::Init(); // create the MCoreSystem object for MCore containers
  112. }
  113. void TearDown() override
  114. {
  115. ::MCore::Initializer::Shutdown();
  116. UnitTest::LeakDetectionFixture::TearDown();
  117. }
  118. };
  119. TEST_F(AnimGraphParameterCommandsFixture, CreatingAParameterUpdatesObjectsAfterParameterIsAddedToInstances)
  120. {
  121. EMotionFX::EMotionFXManager& manager = EMotionFX::GetEMotionFX();
  122. EMotionFX::AnimGraphManager animGraphManager;
  123. EMotionFX::AnimGraph animGraph;
  124. EMotionFX::AnimGraphInstance animGraphInstance0;
  125. EMotionFX::BlendTreeParameterNode parameterNode;
  126. TestParameter parameter;
  127. EXPECT_CALL(manager, GetAnimGraphManager())
  128. .WillRepeatedly(testing::Return(&animGraphManager));
  129. EXPECT_CALL(animGraph, GetID())
  130. .WillRepeatedly(testing::Return(0));
  131. EXPECT_CALL(animGraph, GetNumParameters())
  132. .WillOnce(testing::Return(0));
  133. EXPECT_CALL(animGraph, AddParameter(&parameter, nullptr))
  134. .WillOnce(testing::Return(true));
  135. EXPECT_CALL(animGraph, FindParameterByName(StrEq("testParameter")))
  136. .WillOnce(testing::Return(nullptr));
  137. EXPECT_CALL(animGraph, FindParameterIndex(&parameter))
  138. .WillOnce(testing::Return(AZ::Success<size_t>(0)));
  139. EXPECT_CALL(animGraph, FindParameter(0))
  140. .WillOnce(testing::Return(&parameter));
  141. EXPECT_CALL(animGraph, FindValueParameterIndex(&parameter))
  142. .WillOnce(testing::Return(AZ::Success<size_t>(0)));
  143. EXPECT_CALL(animGraph, GetNumAnimGraphInstances())
  144. .WillRepeatedly(testing::Return(1));
  145. EXPECT_CALL(animGraph, GetAnimGraphInstance(0))
  146. .WillRepeatedly(testing::Return(&animGraphInstance0));
  147. AZStd::vector<EMotionFX::AnimGraphObject*> objectsAffectedByParameterChanges {&parameterNode};
  148. EXPECT_CALL(animGraph, RecursiveCollectObjectsOfType(azrtti_typeid<EMotionFX::ObjectAffectedByParameterChanges>(), testing::_))
  149. .WillRepeatedly(testing::SetArgReferee<1>(objectsAffectedByParameterChanges));
  150. EXPECT_CALL(animGraph, GetDirtyFlag())
  151. .WillOnce(testing::Return(false));
  152. EXPECT_CALL(animGraph, SetDirtyFlag(true));
  153. EXPECT_CALL(animGraphManager, FindAnimGraphByID(animGraph.GetID()))
  154. .WillRepeatedly(testing::Return(&animGraph));
  155. EXPECT_CALL(animGraphManager, RecursiveCollectObjectsAffectedBy(&animGraph, testing::_));
  156. EXPECT_CALL(*EMotionFX::Internal::GetParameterFactory(), CreateImpl(azrtti_typeid<TestParameter>()))
  157. .WillOnce(testing::Return(&parameter));
  158. EXPECT_CALL(parameter, SetName(StrEq("testParameter")));
  159. EXPECT_CALL(parameter, SetDescription(StrEq("The Test Parameter Description")));
  160. {
  161. testing::InSequence sequence;
  162. // AnimGraphInstance::InsertParameterValue must be called before
  163. // AnimGraphNode::ParameterAdded
  164. EXPECT_CALL(animGraphInstance0, InsertParameterValue(0));
  165. EXPECT_CALL(parameterNode, ParameterAdded(StrEq("testParameter")));
  166. }
  167. MCore::CommandLine parameters(R"(-name testParameter -animGraphID 0 -type "{6C91B0BE-EFCF-4270-A356-28B1C4612CCE}" -description "The Test Parameter Description")");
  168. AZStd::string outResult;
  169. CommandSystem::CommandAnimGraphCreateParameter command;
  170. EXPECT_TRUE(command.Execute(parameters, outResult)) << outResult.c_str();
  171. }
  172. } // namespace AnimGraphParameterCommandsTests