BlendTreeParameterNodeTests.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/std/containers/vector.h>
  9. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  10. #include <EMotionFX/Source/AnimGraph.h>
  11. #include <EMotionFX/Source/EMotionFXManager.h>
  12. #include <Tests/TestAssetCode/AnimGraphFactory.h>
  13. #include <Tests/UI/CommandRunnerFixture.h>
  14. namespace EMotionFX
  15. {
  16. class BlendTreeParameterNodeTests
  17. : public CommandRunnerFixture
  18. {
  19. public:
  20. void SetUp() override
  21. {
  22. CommandRunnerFixture::SetUp();
  23. m_animGraph = AnimGraphFactory::Create<OneBlendTreeParameterNodeAnimGraph>();
  24. }
  25. void TearDown() override
  26. {
  27. m_animGraph.reset();
  28. CommandRunnerFixture::TearDown();
  29. }
  30. OneBlendTreeParameterNodeAnimGraph* GetAnimGraph() const
  31. {
  32. return m_animGraph.get();
  33. }
  34. private:
  35. AZStd::unique_ptr < OneBlendTreeParameterNodeAnimGraph> m_animGraph;
  36. };
  37. TEST_F(BlendTreeParameterNodeTests, RenameParameter)
  38. {
  39. const char* startParameterName = "Parameter0";
  40. const char* renamedParameterName = "RenamedParameter0";
  41. BlendTreeParameterNode* parameterNode = GetAnimGraph()->GetParameterNode();
  42. // Add new parameter to the anim graph and check if a output port got added for the parameter node.
  43. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 0);
  44. ExecuteCommands({std::string{"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name "} + startParameterName});
  45. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  46. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), startParameterName);
  47. // Rename anim graph parameter and check if the output port of the parameter node also got renamed.
  48. ExecuteCommands({std::string{"AnimGraphAdjustParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name "} + startParameterName + " -newName " + renamedParameterName});
  49. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  50. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), renamedParameterName);
  51. // Undo and redo
  52. ExecuteCommands({"UNDO"});
  53. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), startParameterName);
  54. ExecuteCommands({"REDO"});
  55. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), renamedParameterName);
  56. }
  57. TEST_F(BlendTreeParameterNodeTests, RemoveParameterFromMask)
  58. {
  59. RecordProperty("test_case_id", "C20655311");
  60. using StringVector = AZStd::vector<AZStd::string>;
  61. BlendTreeParameterNode* parameterNode = GetAnimGraph()->GetParameterNode();
  62. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 0);
  63. ExecuteCommands({
  64. {"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name P0"},
  65. {"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name P1"},
  66. {"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name P2"},
  67. });
  68. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 3);
  69. EXPECT_EQ(parameterNode->GetParameterIndex(0), 0);
  70. EXPECT_EQ(parameterNode->GetParameterIndex(1), 1);
  71. EXPECT_EQ(parameterNode->GetParameterIndex(2), 2);
  72. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{}));
  73. parameterNode->SetParameters(StringVector{"P1", "P2"});
  74. parameterNode->Reinit();
  75. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 2);
  76. // Port 0 maps to parameter 1, port 1 maps to parameter 2
  77. EXPECT_EQ(parameterNode->GetParameterIndex(0), 1);
  78. EXPECT_EQ(parameterNode->GetParameterIndex(1), 2);
  79. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{"P1", "P2"}));
  80. ExecuteCommands({{"AnimGraphRemoveParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -name P0"}});
  81. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 2);
  82. // All the parameters in the mask shifted, ports and parameter indices line up again
  83. EXPECT_EQ(parameterNode->GetParameterIndex(0), 0);
  84. EXPECT_EQ(parameterNode->GetParameterIndex(1), 1);
  85. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{"P1", "P2"}));
  86. ExecuteCommands({{"AnimGraphRemoveParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -name P1"}});
  87. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  88. EXPECT_EQ(parameterNode->GetParameterIndex(0), 0);
  89. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{"P2"}));
  90. ExecuteCommands({{"UNDO"}});
  91. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 2);
  92. EXPECT_EQ(parameterNode->GetParameterIndex(0), 0);
  93. EXPECT_EQ(parameterNode->GetParameterIndex(1), 1);
  94. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{"P1", "P2"}));
  95. ExecuteCommands({{"UNDO"}});
  96. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 2);
  97. // Port 0 maps to parameter 1, port 1 maps to parameter 2
  98. EXPECT_EQ(parameterNode->GetParameterIndex(0), 1);
  99. EXPECT_EQ(parameterNode->GetParameterIndex(1), 2);
  100. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{"P1", "P2"}));
  101. }
  102. TEST_F(BlendTreeParameterNodeTests, ParameterMaskExercise0)
  103. {
  104. using StringVector = AZStd::vector<AZStd::string>;
  105. const char* parameterName0 = "Param0";
  106. const char* parameterName1 = "Param1";
  107. const char* parameterName2 = "Param2";
  108. BlendTreeParameterNode* parameterNode = GetAnimGraph()->GetParameterNode();
  109. // 1. Add two parameters (Param0 and Param1) to the anim graph and check if a output port got added for the parameter node.
  110. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 0);
  111. ExecuteCommands({ std::string{"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name "} + parameterName0 });
  112. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  113. ExecuteCommands({ std::string{"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name "} + parameterName1 });
  114. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 2);
  115. // 2. Change the parameter mask to contain Param1.
  116. parameterNode->SetParameters(StringVector{ "Param1" });
  117. parameterNode->Reinit();
  118. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  119. // 3. Add the 3rd parameter (Param2). The parameter mask should stay the same.
  120. ExecuteCommands({ std::string{"AnimGraphCreateParameter -animGraphID " + std::to_string(GetAnimGraph()->GetID()) + " -type {2ED6BBAF-5C82-4EAA-8678-B220667254F2} -name "} + parameterName2 });
  121. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  122. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{ "Param1" }));
  123. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), parameterName1);
  124. // 4. Undo the 3rd step. The parameter mask should stay the same.
  125. ExecuteCommands({ "UNDO" });
  126. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  127. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{ "Param1" }));
  128. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), parameterName1);
  129. // 5. Undo "Add Param1". Now the mask should be empty, and the output port should contain Param0.
  130. ExecuteCommands({ "UNDO" });
  131. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  132. EXPECT_TRUE(parameterNode->GetParameters().empty());
  133. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), parameterName0);
  134. // 6. Redo "Add Param1". Now the mask should restore and contain Param1, and the outport should contain Param1 as well.
  135. ExecuteCommands({ "REDO" });
  136. EXPECT_EQ(parameterNode->GetOutputPorts().size(), 1);
  137. EXPECT_THAT(parameterNode->GetParameters(), ::testing::Pointwise(::testing::Eq(), StringVector{ "Param1" }));
  138. EXPECT_STREQ(parameterNode->GetOutputPorts()[0].GetName(), parameterName1);
  139. }
  140. } // namespace EMotionFX