CanEditParameters.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <gtest/gtest.h>
  9. #include <QtTest>
  10. #include <QToolBar>
  11. #include <QPushButton>
  12. #include <AzToolsFramework/UI/PropertyEditor/PropertyCheckBoxCtrl.hxx>
  13. #include <AzToolsFramework/UI/PropertyEditor/PropertyStringLineEditCtrl.hxx>
  14. #include <AzToolsFramework/UI/PropertyEditor/PropertyRowWidget.hxx>
  15. #include <AzToolsFramework/UI/PropertyEditor/ReflectedPropertyEditor.hxx>
  16. #include <EMotionFX/Source/AnimGraphManager.h>
  17. #include <EMotionFX/Source/Parameter/FloatSliderParameter.h>
  18. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  19. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  20. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphViewWidget.h>
  21. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/ParameterCreateEditWidget.h>
  22. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/ParameterWindow.h>
  23. #include <Tests/UI/UIFixture.h>
  24. namespace EMotionFX
  25. {
  26. using TestParametersFixture = UIFixture;
  27. TEST_F(TestParametersFixture, CanChangeParameter)
  28. {
  29. // This test uses UIFixture to check that when parameters are added and edited in the Animgraph,
  30. // the results from the RPE are correctly updated in the Paramter itself.
  31. const AZ::u32 animGraphId = 64;
  32. AZStd::string commandResult;
  33. RecordProperty("test_case_id", "C5522322");
  34. EMStudio::GetMainWindow()->ApplicationModeChanged("AnimGraph");
  35. EMStudio::AnimGraphPlugin* animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  36. ASSERT_TRUE(animGraphPlugin) << "Anim graph plugin not found.";
  37. ASSERT_FALSE(animGraphPlugin->GetActiveAnimGraph()) << "No anim graph should be activated.";
  38. ASSERT_EQ(EMotionFX::GetAnimGraphManager().GetNumAnimGraphs(), 0) << "Anim graph manager should contain 0 anim graph.";
  39. // Create empty anim graph and select it.
  40. const AZStd::string command = AZStd::string::format("CreateAnimGraph -animGraphID %d", animGraphId);
  41. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(command, commandResult)) << commandResult.c_str();
  42. AnimGraph* newGraph = GetAnimGraphManager().FindAnimGraphByID(animGraphId);
  43. EXPECT_NE(newGraph, nullptr) << "Cannot find newly created anim graph.";
  44. // The empty graph should contain one node (the root statemachine).
  45. ASSERT_TRUE(newGraph && newGraph->GetNumNodes() == 1) << "An empty anim graph should be activated.";
  46. ASSERT_EQ(EMotionFX::GetAnimGraphManager().GetNumAnimGraphs(), 1) << "Anim graph manager should contain 1 anim graph.";
  47. EMStudio::ParameterWindow* parameterWindow = animGraphPlugin->GetParameterWindow();
  48. ASSERT_TRUE(parameterWindow) << "Anim graph parameter window is invalid.";
  49. // Normally users press the + button and a context menu appears with the options to either add a parameter or a group.
  50. // We are bypassing the context menu and directly call the add parameter slot.
  51. parameterWindow->OnAddParameter();
  52. // Find parameter window.
  53. EMStudio::ParameterCreateEditWidget* parameterCreateWidget = qobject_cast<EMStudio::ParameterCreateEditWidget*>(FindTopLevelWidget("ParameterCreateEditWidget"));
  54. ASSERT_NE(parameterCreateWidget, nullptr) << "Cannot find anim graph parameter create/edit widget. Is the anim graph selected?";
  55. // Find the Create button
  56. QPushButton* createButton = parameterCreateWidget->findChild<QPushButton*>("EMFX.ParameterCreateEditWidget.CreateApplyButton");
  57. EXPECT_TRUE(createButton)<< "Cannot find Create Button";
  58. // Send the left button click directly to the button
  59. QTest::mouseClick(createButton, Qt::LeftButton);
  60. // Check we only have the one Parameter
  61. size_t numParameters = newGraph->GetNumParameters();
  62. EXPECT_EQ(numParameters, 1) << "Not just 1 parameter";
  63. const RangedValueParameter<float, FloatParameter>* parameter = reinterpret_cast<const RangedValueParameter<float, FloatParameter>* >(newGraph->FindValueParameter(0));
  64. EXPECT_TRUE(parameter) << "could not find the Parameter";
  65. // Find the parameter in the window
  66. QTreeWidget* treeWidget = parameterWindow->findChild<QTreeWidget*>("AnimGraphParamWindow");
  67. EXPECT_TRUE(treeWidget) << "Cannot find the QTreeWidget";
  68. // Select the node containing the parameter
  69. QTreeWidgetItem* parameterNodeItem = treeWidget->invisibleRootItem()->child(0);
  70. EXPECT_TRUE(parameterNodeItem) << "Parameter 0 not found";
  71. parameterNodeItem->setSelected(true);
  72. // Check that the values we are interested are set to their initial value
  73. EXPECT_TRUE(parameter->GetHasMinValue()) << "MinValue should be true";
  74. EXPECT_TRUE(parameter->GetHasMaxValue()) << "MaxValue should be true";
  75. // Find parameter widget.
  76. parameterCreateWidget = qobject_cast<EMStudio::ParameterCreateEditWidget*>(FindTopLevelWidget("ParameterCreateEditWidget"));
  77. ASSERT_NE(parameterCreateWidget, nullptr) << "Cannot find anim graph parameter create/edit widget. Is the anim graph selected?";
  78. // Get the parameter widget that holds ReflectedPropertyEditor
  79. AzToolsFramework::ReflectedPropertyEditor * parameterWidget = parameterCreateWidget->findChild<AzToolsFramework::ReflectedPropertyEditor*>("EMFX.ParameterCreateEditWidget.ReflectedPropertyEditor.ParameterEditorWidget");
  80. ASSERT_TRUE(parameterWidget) << "Cannot find the parameterWidget";
  81. AzToolsFramework::PropertyRowWidget* minimumWidget = reinterpret_cast<AzToolsFramework::PropertyRowWidget*>(GetNamedPropertyRowWidgetFromReflectedPropertyEditor(parameterWidget, "Has minimum"));
  82. EXPECT_TRUE(minimumWidget) << "Has minimum checkbox not found";
  83. AzToolsFramework::PropertyCheckBoxCtrl* checkBoxCtrl = reinterpret_cast< AzToolsFramework::PropertyCheckBoxCtrl* >(minimumWidget->GetChildWidget());
  84. EXPECT_TRUE(checkBoxCtrl) << "CheckBoxCtrl not found";
  85. QCheckBox* checkBox = checkBoxCtrl->GetCheckBox();
  86. EXPECT_TRUE(checkBox) << "CheckBox not found";
  87. checkBox->click();
  88. AzToolsFramework::PropertyRowWidget* maximumWidget = reinterpret_cast<AzToolsFramework::PropertyRowWidget*>(GetNamedPropertyRowWidgetFromReflectedPropertyEditor(parameterWidget, "Has maximum"));
  89. EXPECT_TRUE(maximumWidget) << "Has maximum checkbox not found";
  90. checkBoxCtrl = reinterpret_cast<AzToolsFramework::PropertyCheckBoxCtrl*>(maximumWidget->GetChildWidget());
  91. EXPECT_TRUE(checkBoxCtrl) << "CheckBoxCtrl not found";
  92. checkBox = checkBoxCtrl->GetCheckBox();
  93. EXPECT_TRUE(checkBox) << "CheckBox not found";
  94. checkBox->click();
  95. // Find the Apply button, until the changes are applied the values will not be updated
  96. QPushButton* applyButton = parameterCreateWidget->findChild<QPushButton*>("EMFX.ParameterCreateEditWidget.CreateApplyButton");
  97. EXPECT_TRUE(applyButton) << "Cannot find Apply Button";
  98. // Send the left button click directly to the button
  99. QTest::mouseClick(applyButton, Qt::LeftButton);
  100. EXPECT_FALSE(parameter->GetHasMinValue()) << "MinValue should be false";
  101. EXPECT_FALSE(parameter->GetHasMaxValue()) << "MaxValue should be false";
  102. }
  103. TEST_F(TestParametersFixture, CanRenameParameterGroup)
  104. {
  105. AZStd::string commandResult;
  106. RecordProperty("test_case_id", "C5522320");
  107. EMStudio::GetMainWindow()->ApplicationModeChanged("AnimGraph");
  108. // Create anim graph.
  109. const AZ::u32 animGraphId = 64;
  110. AZStd::string command = AZStd::string::format("CreateAnimGraph -animGraphID %d", animGraphId);
  111. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(command, commandResult)) << commandResult.c_str();
  112. AnimGraph* newGraph = GetAnimGraphManager().FindAnimGraphByID(animGraphId);
  113. EXPECT_NE(newGraph, nullptr) << "Cannot find newly created anim graph.";
  114. // Create parameter group.
  115. const AZStd::string initialGroupName = "Parameter Group 0";
  116. command = AZStd::string::format("AnimGraphAddGroupParameter -animGraphID %d -name \"%s\"", animGraphId, initialGroupName.c_str());
  117. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(command, commandResult)) << commandResult.c_str();
  118. EXPECT_EQ(newGraph->GetNumParameters(), 1) << "The newly created group should show up as a parameter.";
  119. const GroupParameter* groupParameter = azdynamic_cast<const GroupParameter*>(newGraph->FindParameter(0));
  120. EXPECT_TRUE(groupParameter) << "Cannot find the newly created parameter group.";
  121. // Get parameter window.
  122. EMStudio::AnimGraphPlugin* animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  123. ASSERT_NE(animGraphPlugin, nullptr) << "Anim graph plugin not found.";
  124. EMStudio::ParameterWindow* parameterWindow = animGraphPlugin->GetParameterWindow();
  125. ASSERT_NE(parameterWindow, nullptr) << "Anim graph parameter window is not valid.";
  126. QTreeWidget* treeWidget = parameterWindow->findChild<QTreeWidget*>("AnimGraphParamWindow");
  127. ASSERT_NE(treeWidget, nullptr) << "Cannot find parameter tree widget.";
  128. // Select the parameter group.
  129. QTreeWidgetItem* parameterNodeItem = treeWidget->invisibleRootItem()->child(0);
  130. ASSERT_NE(parameterNodeItem, nullptr) << "Cannot select parameter group.";
  131. parameterNodeItem->setSelected(true);
  132. // Find edit parameter window and the line edit control for the name.
  133. EMStudio::ParameterCreateEditWidget* parameterCreateWidget = qobject_cast<EMStudio::ParameterCreateEditWidget*>(FindTopLevelWidget("ParameterCreateEditWidget"));
  134. ASSERT_NE(parameterCreateWidget, nullptr) << "Cannot find the edit parameter window.";
  135. AzToolsFramework::ReflectedPropertyEditor* parameterWidget = parameterCreateWidget->findChild<AzToolsFramework::ReflectedPropertyEditor*>("EMFX.ParameterCreateEditWidget.ReflectedPropertyEditor.ParameterEditorWidget");
  136. ASSERT_NE(parameterWidget, nullptr) << "Cannot find the reflected property editor.";
  137. AzToolsFramework::PropertyRowWidget* nameWidget = reinterpret_cast<AzToolsFramework::PropertyRowWidget*>(GetNamedPropertyRowWidgetFromReflectedPropertyEditor(parameterWidget, "Name"));
  138. ASSERT_NE(nameWidget, nullptr) << "Name widget not found";
  139. AzToolsFramework::PropertyStringLineEditCtrl* lineEditCtrl = reinterpret_cast< AzToolsFramework::PropertyStringLineEditCtrl* >(nameWidget->GetChildWidget());
  140. ASSERT_NE(lineEditCtrl, nullptr) << "Line edit control not found.";
  141. // Change the group name by editing the line edit.
  142. const AZStd::string changedGroupName = "Changed Group Name";
  143. lineEditCtrl->UpdateValue(changedGroupName.c_str());
  144. // Find and click the apply button.
  145. QPushButton* applyButton = parameterCreateWidget->findChild<QPushButton*>("EMFX.ParameterCreateEditWidget.CreateApplyButton");
  146. EXPECT_NE(applyButton, nullptr) << "Cannot find Apply button.";
  147. QTest::mouseClick(applyButton, Qt::LeftButton);
  148. // Data verification: Check if the group parameter name changed.
  149. EXPECT_STREQ(groupParameter->GetName().c_str(), changedGroupName.c_str());
  150. // Undo.
  151. EXPECT_TRUE(CommandSystem::GetCommandManager()->Undo(commandResult)) << commandResult.c_str();
  152. EXPECT_STREQ(groupParameter->GetName().c_str(), initialGroupName.c_str());
  153. // Redo.
  154. EXPECT_TRUE(CommandSystem::GetCommandManager()->Redo(commandResult)) << commandResult.c_str();
  155. EXPECT_STREQ(groupParameter->GetName().c_str(), changedGroupName.c_str());
  156. }
  157. } // namespace EMotionFX