3
0

AnimGraphNodeTests.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.h>
  11. #include <QWidget>
  12. #include <QComboBox>
  13. #include <QModelIndex>
  14. #include <qrect.h>
  15. #include <Tests/UI/UIFixture.h>
  16. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  17. #include <EMotionFX/Source/AnimGraphEntryNode.h>
  18. #include <EMotionFX/Source/AnimGraphHubNode.h>
  19. #include <EMotionFX/Source/AnimGraphManager.h>
  20. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/NodeGraph.h>
  21. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  22. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  23. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphViewWidget.h>
  24. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphWidget.h>
  25. #include <EMotionFX/Source/MotionManager.h>
  26. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  27. #include <EMotionFX/Tools/EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphModel.h>
  28. #include <GraphCanvas/Widgets/NodePalette/NodePaletteTreeView.h>
  29. namespace EMotionFX
  30. {
  31. TEST_F(UIFixture, CanAddAnimGraphNode)
  32. {
  33. // This test checks that you can add a node to an animgraph
  34. RecordProperty("test_case_id", "C22083482");
  35. // Set up Animgraph
  36. AnimGraph* animGraph = nullptr;
  37. EMStudio::AnimGraphPlugin* animGraphPlugin = nullptr;
  38. {
  39. // Set up an empty animgraph
  40. const AZ::u32 animGraphId = 64;
  41. MCore::CommandGroup group;
  42. // Create empty anim graph
  43. group.AddCommandString(AZStd::string::format("CreateAnimGraph -animGraphID %d", animGraphId));
  44. // Run Commands
  45. AZStd::string commandResult;
  46. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommandGroup(group, commandResult)) << commandResult.c_str();
  47. // Get useful components
  48. animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  49. ASSERT_NE(animGraphPlugin, nullptr) << "Anim graph plugin not found.";
  50. animGraph = GetAnimGraphManager().FindAnimGraphByID(animGraphId);
  51. ASSERT_NE(animGraph, nullptr) << "Cannot find newly created anim graph.";
  52. }
  53. // Grab needed objects
  54. EMStudio::BlendGraphWidget* graphWidget = animGraphPlugin->GetGraphWidget();
  55. const AZStd::vector<EMotionFX::AnimGraphNode*> selectedAnimGraphNodes;
  56. // Check that there is no node before action
  57. EMotionFX::AnimGraphNode* parentNode = graphWidget->GetActiveGraph()->GetModelIndex().data(EMStudio::AnimGraphModel::ROLE_NODE_POINTER).value<EMotionFX::AnimGraphNode*>();
  58. ASSERT_EQ(parentNode->GetNumChildNodes(), 0) << "Node was not created according to root node";
  59. // Nodes to test addition capabilities
  60. const AZStd::vector<QString> graphNodeTypeNames{"Motion", "Entry", "Hub"};
  61. // We need to offset subsequent requests for context menu because if we
  62. // keep the same point, we'll "click" on a node created during the
  63. // first iteration of the loop and in such case, menu won't have entry for
  64. // node creation.
  65. int clickYOffset = 0;
  66. for (QString nodeName : graphNodeTypeNames)
  67. {
  68. // Right Click on GraphWidget
  69. const QRect graphRect = graphWidget->rect();
  70. const QPoint clickPoint{ graphRect.center().x(), graphRect.center().y() + clickYOffset };
  71. clickYOffset += 150;
  72. graphWidget->OnContextMenuEvent(graphWidget, clickPoint, graphWidget->LocalToGlobal(clickPoint), animGraphPlugin, selectedAnimGraphNodes, true, false, animGraphPlugin->GetActionFilter());
  73. // Grab the node index from the tree view in the graphWidget context menu
  74. auto* tree = UIFixture::GetFirstChildOfType<GraphCanvas::NodePaletteTreeView>(graphWidget);
  75. ASSERT_TRUE(tree);
  76. const QModelIndex idx = UIFixture::GetIndexFromName(tree, nodeName);
  77. ASSERT_TRUE(idx.isValid());
  78. // Selection should spawn the node
  79. tree->setCurrentIndex(idx);
  80. // Need one pass of the event loop so that context menu can be destroyed by
  81. // QObject::deleteLater. Otherwise, UIFixture::GetFirstChildOfType<GraphCanvas::NodePaletteTreeView>
  82. // will pick up the tree view from the first iteration each time and they shouldn't be reused.
  83. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  84. }
  85. // Make sure animgraph node was created
  86. ASSERT_EQ(parentNode->GetNumChildNodes(), graphNodeTypeNames.size()) << "Node was not created according to root node";
  87. // Make sure animgraph node was created via models
  88. for (QString nodeName : graphNodeTypeNames)
  89. {
  90. const AZStd::string actualName = (nodeName.toStdString() + "0").c_str();
  91. AnimGraphNode* animGraphNode = animGraph->RecursiveFindNodeByName(actualName.c_str());
  92. ASSERT_NE(animGraphNode, nullptr) << "Motion Node was not found by animpraph object";
  93. EMStudio::AnimGraphModel& animGraphModel = animGraphPlugin->GetAnimGraphModel();
  94. const QModelIndex nodeModelIndex = animGraphModel.FindFirstModelIndex(animGraphNode);
  95. ASSERT_TRUE(nodeModelIndex.isValid()) << "Node was not created according to model";
  96. }
  97. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  98. }
  99. }