AnimGraphParameterConditionCommandTests.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <EMotionFX/CommandSystem/Source/AnimGraphParameterCommands.h>
  10. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  11. #include <EMotionFX/Source/AnimGraph.h>
  12. #include <EMotionFX/Source/AnimGraphParameterCondition.h>
  13. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  14. #include <EMotionFX/Source/AnimGraphStateTransition.h>
  15. #include <EMotionFX/Source/Parameter/FloatSliderParameter.h>
  16. #include <EMotionFX/Source/Parameter/Parameter.h>
  17. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  18. #include <Tests/AnimGraphFixture.h>
  19. namespace EMotionFX
  20. {
  21. TEST_F(AnimGraphFixture, AnimGraphParameterCondition_UndoRemoveParameterTest)
  22. {
  23. AZStd::string result;
  24. AZStd::string commandString;
  25. CommandSystem::CommandManager commandManager;
  26. AnimGraphStateMachine* node1 = aznew AnimGraphStateMachine();
  27. m_animGraph->GetRootStateMachine()->AddChildNode(node1);
  28. m_animGraph->GetRootStateMachine()->SetEntryState(node1);
  29. AnimGraphStateMachine* node2 = aznew AnimGraphStateMachine();
  30. m_animGraph->GetRootStateMachine()->AddChildNode(node2);
  31. AnimGraphStateTransition* transition = AddTransition(node1, node2, 1.0f);
  32. AnimGraphParameterCondition* condition = aznew AnimGraphParameterCondition();
  33. transition->AddCondition(condition);
  34. m_animGraph->InitAfterLoading();
  35. // Add parameter to the anim graph and the instance.
  36. const AZStd::string parameterName = "Parameter1";
  37. {
  38. AZStd::unique_ptr<EMotionFX::Parameter> newParameter(EMotionFX::ParameterFactory::Create(azrtti_typeid<FloatSliderParameter>()));
  39. newParameter->SetName(parameterName);
  40. CommandSystem::ConstructCreateParameterCommand(commandString, m_animGraph.get(),
  41. newParameter.get(),
  42. InvalidIndex);
  43. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  44. }
  45. condition->SetParameterName(parameterName);
  46. // 1. Remove the parameter including unlinking it from the parameter condition.
  47. MCore::CommandGroup commandGroup;
  48. CommandSystem::BuildRemoveParametersCommandGroup(m_animGraph.get(), /*parameterNamesToBeRemoved=*/{ parameterName }, &commandGroup);
  49. EXPECT_TRUE(commandManager.ExecuteCommandGroup(commandGroup, result)) << result.c_str();
  50. EXPECT_EQ(m_animGraph->GetNumParameters(), 0) << "The parameter should not be present anymore.";
  51. EXPECT_EQ(condition->GetParameterName(), "") << "The condition should not be linked to the removed parameter anymore.";
  52. // 2. Undo.
  53. EXPECT_TRUE(commandManager.Undo(result)) << result.c_str();
  54. EXPECT_EQ(m_animGraph->GetNumParameters(), 1) << "The parameter should be back again.";
  55. condition = azdynamic_cast<AnimGraphParameterCondition*>(transition->GetCondition(0));
  56. EXPECT_EQ(condition->GetParameterName(), parameterName) << "The condition should be linked to the parameter again.";
  57. // 3. Redo.
  58. EXPECT_TRUE(commandManager.Redo(result)) << result.c_str();
  59. condition = azdynamic_cast<AnimGraphParameterCondition*>(transition->GetCondition(0));
  60. EXPECT_EQ(m_animGraph->GetNumParameters(), 0) << "The parameter should not be present anymore.";
  61. EXPECT_EQ(condition->GetParameterName(), "") << "The condition should not be linked to the removed parameter anymore.";
  62. }
  63. } // namespace EMotionFX