CanAddMotionToAnimGraphNode.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <QPushButton>
  10. #include <QAction>
  11. #include <QtTest>
  12. #include <QComboBox>
  13. #include <Tests/UI/AnimGraphUIFixture.h>
  14. #include <EMotionFX/Source/AnimGraphManager.h>
  15. #include <EMotionFX/Source/AnimGraphObject.h>
  16. #include <EMotionFX/Source/AnimGraphObjectFactory.h>
  17. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  18. #include <Editor/PropertyWidgets/MotionSetMotionIdHandler.h>
  19. #include <EMotionStudio/Plugins/StandardPlugins/Source/MotionSetsWindow/MotionSetsWindowPlugin.h>
  20. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  21. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphViewWidget.h>
  22. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphWidget.h>
  23. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AttributesWindow.h>
  24. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphEditor.h>
  25. namespace EMotionFX
  26. {
  27. TEST_F(AnimGraphUIFixture, CanAddMotionToAnimGraphNode)
  28. {
  29. RecordProperty("test_case_id", "C2187169");
  30. // Create a motion set and add a motion to it.
  31. auto motionSetPlugin = static_cast<EMStudio::MotionSetsWindowPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::MotionSetsWindowPlugin::CLASS_ID));
  32. ASSERT_TRUE(motionSetPlugin) << "No motion sets plugin found";
  33. const EMStudio::MotionSetManagementWindow* managementWindow = motionSetPlugin->GetManagementWindow();
  34. ASSERT_TRUE(managementWindow) << "No motion sets management window found";
  35. const EMStudio::MotionSetWindow* motionSetWindow = motionSetPlugin->GetMotionSetWindow();
  36. ASSERT_TRUE(motionSetWindow) << "No motion set window found";
  37. // Check there aren't any motion sets yet.
  38. const size_t oldNumMotionSets = EMotionFX::GetMotionManager().GetNumMotionSets();
  39. // Find the action to create a new motion set and press it.
  40. QWidget* addMotionSetButton = GetWidgetWithNameFromNamedToolbar(managementWindow, "MotionSetManagementWindow.ToolBar", "MotionSetManagementWindow.ToolBar.AddNewMotionSet");
  41. ASSERT_TRUE(addMotionSetButton) << "Unable to find Add Motion Set button.";
  42. QTest::mouseClick(addMotionSetButton, Qt::LeftButton);
  43. // Make sure the new motion set has been created.
  44. ASSERT_EQ(EMotionFX::GetMotionManager().GetNumMotionSets(), oldNumMotionSets + 1) << "Failed to create motion set.";
  45. EMotionFX::MotionSet* motionSet = EMotionFX::GetMotionManager().GetMotionSet(oldNumMotionSets);
  46. // Ensure new motion set is selected.
  47. motionSetPlugin->SetSelectedSet(motionSet);
  48. // It should be empty at the moment.
  49. const size_t numMotions = motionSet->GetNumMotionEntries();
  50. EXPECT_EQ(numMotions, 0);
  51. // Find the action to add a motion to the set and press it.
  52. QWidget* addMotionButton = GetWidgetWithNameFromNamedToolbar(motionSetWindow, "MotionSetWindow.ToolBar", "MotionSetWindow.ToolBar.AddANewEntry");
  53. ASSERT_TRUE(addMotionButton) << "No Add Motion to Motion Set button found";
  54. QTest::mouseClick(addMotionButton, Qt::LeftButton);
  55. // There should now be a new more motion.
  56. const size_t numMotionsAfterCreate = motionSet->GetNumMotionEntries();
  57. ASSERT_EQ(numMotionsAfterCreate, numMotions + 1) << "Failed to create new motion.";
  58. AZStd::unordered_map<AZStd::string, MotionSet::MotionEntry*> motions = motionSet->GetMotionEntries();
  59. // The newly created motion should be called "<undefined>".
  60. MotionSet::MotionEntry* motion = motions["<undefined>"];
  61. ASSERT_TRUE(motion) << "no \"<undefined>\" motion found";
  62. // Create a motion node in the anim graph.
  63. EMotionFX::AnimGraph* animGraph = CreateAnimGraph();
  64. ASSERT_TRUE(animGraph) << "Failed to find anim graph";
  65. AnimGraphNode* node = AddNodeToAnimGraph(animGraph, "Motion");
  66. ASSERT_TRUE(node) << "Failed to find create motion node in anim graph.";
  67. AnimGraphMotionNode* motionNode = azdynamic_cast<AnimGraphMotionNode*>(node);
  68. size_t numMotionsInNode = motionNode->GetNumMotions();
  69. EXPECT_EQ(numMotionsInNode, 0);
  70. EMStudio::NodeGraph* nodeGraph = GetActiveNodeGraph();
  71. // Find the corresponding GraphNode and click on it to select it.
  72. nodeGraph->SelectAllNodes();
  73. AZStd::vector<EMStudio::GraphNode*> nodes = nodeGraph->GetSelectedGraphNodes();
  74. ASSERT_NE(nodes.size(), 0) << "Failed to select motion node in anim graph.";
  75. EMStudio::GraphNode* newNode = nodes[0];
  76. QRect nodeLocalRect = newNode->GetFinalRect();
  77. QTest::mouseClick(m_blendGraphWidget, Qt::LeftButton, {}, nodeLocalRect.center());
  78. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  79. // RPE should have now set up the attributes editor.
  80. EMStudio::AttributesWindow* attributesWindow = m_animGraphPlugin->GetAttributesWindow();
  81. ASSERT_TRUE(attributesWindow) << "Failed to find AttributesWindow.";
  82. EMotionFX::AnimGraphEditor* animGraphEditor = attributesWindow->GetAnimGraphEditor();
  83. ASSERT_TRUE(animGraphEditor) << "Failed to find AnimGraphEditor in AttributesWindow.";
  84. // Select our motion set in the combo widget (index 0 is the select motion set instruction text).
  85. QComboBox* combo = animGraphEditor->GetMotionSetComboBox();
  86. ASSERT_TRUE(combo) << "Unable to get MotionSetComboBox from AnimGraphEditor.";
  87. combo->setCurrentIndex(1);
  88. // Find the picker add button in the attributes window and press it.
  89. const MotionSetMotionIdPicker* idPicker = attributesWindow->findChild<MotionSetMotionIdPicker*>();
  90. ASSERT_TRUE(idPicker) << "Failed to find MotionSetMotionIdPicker in AttributesWindow.";
  91. // Click the add motion button.
  92. QPushButton* pickerButton = idPicker->findChild<QPushButton*>("EMFX.MotionSetMotionIdPicker.PickButton");
  93. ASSERT_TRUE(pickerButton) << "Failed to find PickButton in MotionSetMotionIdPicker.";
  94. QTest::mouseClick(pickerButton, Qt::LeftButton);
  95. // The motion picker dialog should now be open.
  96. EMStudio::MotionSetSelectionWindow* pickWindow = idPicker->findChild< EMStudio::MotionSetSelectionWindow*>();
  97. ASSERT_TRUE(pickWindow) << "Failed to find MotionSetSelectionWindow.";
  98. pickWindow->GetHierarchyWidget()->SelectItemsWithText("<undefined>");
  99. // Select the motion we created earlier and press the OK button.
  100. pickWindow->GetHierarchyWidget()->SelectItemsWithText("<undefined>");
  101. QPushButton* okButton = pickWindow->findChild<QPushButton*>("EMFX.MotionSetSelectionWindow.Ok");
  102. ASSERT_TRUE(okButton) << "Failed to find OK button in MotionSetSelectionWindow.";
  103. QTest::mouseClick(okButton, Qt::LeftButton);
  104. // The motion should now have been pushed back to the node: check it.
  105. const size_t numMotionsInNodeAfter = motionNode->GetNumMotions();
  106. ASSERT_EQ(numMotionsInNodeAfter, numMotionsInNode + 1) << "Failed to add motion to motion node.";
  107. const char* motionName = motionNode->GetMotionId(0);
  108. ASSERT_STREQ(motionName, "<undefined>") << "Failed to find added motion in motion node";
  109. // Delete the created node.
  110. QTest::keyClick(m_blendGraphWidget, Qt::Key_Delete);
  111. }
  112. } // namespace EMotionFX