AnimGraphCommandTests.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <Tests/AnimGraphFixture.h>
  9. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  10. #include <EMotionFX/Source/ActorInstance.h>
  11. #include <EMotionFX/Source/AnimGraphBindPoseNode.h>
  12. #include <EMotionFX/Source/AnimGraphInstance.h>
  13. #include <EMotionFX/Source/AnimGraphManager.h>
  14. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  15. #include <EMotionFX/Source/AnimGraphNode.h>
  16. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  17. namespace EMotionFX
  18. {
  19. class ActivateAnimGraphCommandFixture
  20. : public AnimGraphFixture
  21. {
  22. public:
  23. AnimGraphStateMachine* AddSubStateMachine(AnimGraphNode* parentState)
  24. {
  25. AnimGraphStateMachine* stateMachine = aznew AnimGraphStateMachine();
  26. parentState->AddChildNode(stateMachine);
  27. AnimGraphBindPoseNode* bindPose = aznew AnimGraphBindPoseNode();
  28. stateMachine->AddChildNode(bindPose);
  29. stateMachine->SetEntryState(bindPose);
  30. return stateMachine;
  31. }
  32. void ConstructGraph()
  33. {
  34. AnimGraphFixture::ConstructGraph();
  35. AnimGraphStateMachine* sm1 = AddSubStateMachine(m_rootStateMachine);
  36. AddSubStateMachine(m_rootStateMachine);
  37. m_rootStateMachine->SetEntryState(sm1);
  38. }
  39. void CheckStateMachinesAreInEntryStates(const AnimGraph* animGraph, AnimGraphInstance* instance)
  40. {
  41. AZStd::vector<AnimGraphNode*> stateMachines;
  42. animGraph->RecursiveCollectNodesOfType(azrtti_typeid<AnimGraphStateMachine>(), &stateMachines);
  43. for (AnimGraphNode* state : stateMachines)
  44. {
  45. AnimGraphStateMachine* stateMachine = static_cast<AnimGraphStateMachine*>(state);
  46. const AZStd::vector<AnimGraphNode*>& activeStates = stateMachine->GetActiveStates(instance);
  47. EXPECT_EQ(activeStates.size(), 1);
  48. EXPECT_TRUE(activeStates[0] == stateMachine->GetEntryState());
  49. }
  50. }
  51. };
  52. TEST_F(ActivateAnimGraphCommandFixture, ActivateAnimGraph)
  53. {
  54. CommandSystem::CommandManager commandManager;
  55. AZStd::string command;
  56. AZStd::string result;
  57. m_actorInstance->SetAnimGraphInstance(nullptr);
  58. EXPECT_TRUE(m_actorInstance->GetAnimGraphInstance() == nullptr);
  59. command = AZStd::string::format("ActivateAnimGraph -actorInstanceID %d -animGraphID %d -motionSetID %d\n",
  60. m_actorInstance->GetID(), m_animGraph->GetID(), m_motionSet->GetID());
  61. EXPECT_TRUE(commandManager.ExecuteCommand(command, result));
  62. AnimGraphInstance* newInstance = m_actorInstance->GetAnimGraphInstance();
  63. EXPECT_TRUE(newInstance != nullptr);
  64. EXPECT_TRUE(newInstance->GetAnimGraph() == m_animGraph.get());
  65. CheckStateMachinesAreInEntryStates(m_animGraph.get(), newInstance);
  66. }
  67. TEST_F(ActivateAnimGraphCommandFixture, ActivateAnimGraph_InvalidParameters)
  68. {
  69. CommandSystem::CommandManager commandManager;
  70. AZStd::string command;
  71. AZStd::string result;
  72. command = AZStd::string::format("ActivateAnimGraph -actorInstanceIndex %d -animGraphID %d -motionSetID %d\n",
  73. m_actorInstance->GetID(), m_animGraph->GetID(), m_motionSet->GetID());
  74. EXPECT_FALSE(commandManager.ExecuteCommand(command, result));
  75. command = AZStd::string::format("ActivateAnimGraph -actorInstanceID %d -animGraphIndex %d -motionSetID %d\n",
  76. m_actorInstance->GetID(), m_animGraph->GetID(), m_motionSet->GetID());
  77. EXPECT_FALSE(commandManager.ExecuteCommand(command, result));
  78. command = AZStd::string::format("ActivateAnimGraph -actorInstanceID %d -animGraphID %d -motionSetIndex %d\n",
  79. m_actorInstance->GetID(), m_animGraph->GetID(), m_motionSet->GetID());
  80. EXPECT_FALSE(commandManager.ExecuteCommand(command, result));
  81. }
  82. ///////////////////////////////////////////////////////////////////////////
  83. class LoadAnimGraphCommandTests
  84. : public AnimGraphFixture
  85. {
  86. public:
  87. void ConstructGraph() override
  88. {
  89. AnimGraphFixture::ConstructGraph();
  90. AnimGraphNode* stateA = aznew AnimGraphMotionNode();
  91. stateA->SetName("A");
  92. m_rootStateMachine->AddChildNode(stateA);
  93. m_rootStateMachine->SetEntryState(stateA);
  94. AnimGraphNode* stateB = aznew AnimGraphMotionNode();
  95. stateB->SetName("B");
  96. m_rootStateMachine->AddChildNode(stateB);
  97. AddTransition(stateA, stateB, 1.0f);
  98. // Save the anim graph to disk.
  99. AZ::SerializeContext* context = nullptr;
  100. AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  101. EXPECT_TRUE(context != nullptr) << "Serialize context is not valid.";
  102. EXPECT_TRUE(m_animGraph->SaveToFile(m_filename, context)) << "Saving anim graph to " << m_filename << " failed.";
  103. }
  104. public:
  105. const char* m_filename = "TestAnimGraph.animgraph";
  106. };
  107. // Note: Disabled tests as they fail on Jenkins. Loading the anim graph fails in AZ::Utils::LoadObjectFromFile<AnimGraph>() after saving the anim graph to disk successfully.
  108. class LoadAnimGraphCommandTestsBoolParam
  109. : public LoadAnimGraphCommandTests
  110. , public ::testing::WithParamInterface<bool>
  111. {
  112. };
  113. INSTANTIATE_TEST_CASE_P(LoadAnimGraphCommandTests, LoadAnimGraphCommandTestsBoolParam, ::testing::Bool());
  114. TEST_F(LoadAnimGraphCommandTests, DISABLED_LoadAnimGraph)
  115. {
  116. CommandSystem::CommandManager commandManager;
  117. AZStd::string command;
  118. AZStd::string result;
  119. command = AZStd::string::format("LoadAnimGraph -filename \"%s\"", m_filename);
  120. EXPECT_TRUE(commandManager.ExecuteCommand(command, result)) << result.c_str();
  121. const AnimGraph* loadedAnimGraph = EMotionFX::GetAnimGraphManager().FindAnimGraphByFileName(m_filename);
  122. EXPECT_TRUE(loadedAnimGraph != nullptr);
  123. EXPECT_FALSE(loadedAnimGraph->GetIsOwnedByRuntime());
  124. EXPECT_FALSE(loadedAnimGraph->GetIsOwnedByAsset());
  125. EXPECT_NE(m_animGraph->GetID(), loadedAnimGraph->GetID())
  126. << "The id of the original anim graph does not differ from the loaded one, which means that the loading routine just returned the loaded anim graph.";
  127. }
  128. TEST_F(LoadAnimGraphCommandTests, DISABLED_LoadAnimGraphTwice)
  129. {
  130. CommandSystem::CommandManager commandManager;
  131. AZStd::string command;
  132. AZStd::string result;
  133. command = AZStd::string::format("LoadAnimGraph -filename \"%s\"", m_filename);
  134. // Load the anim graph the first time.
  135. EXPECT_TRUE(commandManager.ExecuteCommand(command, result)) << result.c_str();
  136. const int firstAnimGraphId = AzFramework::StringFunc::ToInt(result.c_str());
  137. const AnimGraph* firstAnimGraph = EMotionFX::GetAnimGraphManager().FindAnimGraphByID(firstAnimGraphId);
  138. EXPECT_TRUE(firstAnimGraph != nullptr);
  139. EXPECT_FALSE(firstAnimGraph->GetIsOwnedByRuntime());
  140. EXPECT_FALSE(firstAnimGraph->GetIsOwnedByAsset());
  141. // Load the anim graph again.
  142. EXPECT_TRUE(commandManager.ExecuteCommand(command, result)) << result.c_str();
  143. const int secondAnimGraphId = AzFramework::StringFunc::ToInt(result.c_str());
  144. const AnimGraph* secondAnimGraph = EMotionFX::GetAnimGraphManager().FindAnimGraphByID(secondAnimGraphId);
  145. EXPECT_TRUE(secondAnimGraph != nullptr);
  146. EXPECT_FALSE(secondAnimGraph->GetIsOwnedByRuntime());
  147. EXPECT_FALSE(secondAnimGraph->GetIsOwnedByAsset());
  148. EXPECT_EQ(secondAnimGraph->GetID(), secondAnimGraph->GetID())
  149. << "The second load should be skipped as the anim graph already got loaded. If the ids are not equal, it means the anim graph got loaded twice.";
  150. }
  151. TEST_P(LoadAnimGraphCommandTestsBoolParam, DISABLED_LoadAnimGraphAfterAssetLoad)
  152. {
  153. CommandSystem::CommandManager commandManager;
  154. AZStd::string command;
  155. AZStd::string result;
  156. command = AZStd::string::format("LoadAnimGraph -filename \"%s\"", m_filename);
  157. // Load the anim graph once and fake that it got loaded by the asset system (faking a reference graph load).
  158. EXPECT_TRUE(commandManager.ExecuteCommand(command, result)) << result.c_str();
  159. const AZ::u32 assetAnimGraphId = static_cast<AZ::u32>(AzFramework::StringFunc::ToInt(result.c_str()));
  160. AnimGraph* assetAnimGraph = EMotionFX::GetAnimGraphManager().FindAnimGraphByID(assetAnimGraphId);
  161. EXPECT_TRUE(assetAnimGraph != nullptr);
  162. if (GetParam())
  163. {
  164. assetAnimGraph->SetIsOwnedByRuntime(true);
  165. }
  166. else
  167. {
  168. assetAnimGraph->SetIsOwnedByAsset(true);
  169. }
  170. // Load the anim graph again, this time normally.
  171. EXPECT_TRUE(commandManager.ExecuteCommand(command, result)) << result.c_str();
  172. const int loadedAnimGraphId = AzFramework::StringFunc::ToInt(result.c_str());
  173. const AnimGraph* loadedAnimGraph = EMotionFX::GetAnimGraphManager().FindAnimGraphByID(loadedAnimGraphId);
  174. EXPECT_TRUE(loadedAnimGraph != nullptr);
  175. EXPECT_FALSE(loadedAnimGraph->GetIsOwnedByRuntime());
  176. EXPECT_FALSE(loadedAnimGraph->GetIsOwnedByAsset());
  177. EXPECT_NE(loadedAnimGraph->GetID(), assetAnimGraph->GetID())
  178. << "As the first loaded anim graph pretends to be loaded by the asset system, the second and normal load should force load a second anim graph.";
  179. }
  180. } // namespace EMotionFX