EntryStateTests.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <QAction>
  10. #include <QtTest/QtTest>
  11. #include <qtoolbar.h>
  12. #include <QWidget>
  13. #include <QComboBox>
  14. #include <qrect.h>
  15. #include <Tests/UI/AnimGraphUIFixture.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/AnimGraphStateMachine.h>
  26. namespace EMotionFX
  27. {
  28. TEST_F(UIFixture, CanSetEntryState)
  29. {
  30. // This test checks that you can set the entry state of motion nodes in an animgraph
  31. RecordProperty("test_case_id", "C1559146");
  32. AnimGraph* animGraph = nullptr;
  33. EMStudio::AnimGraphPlugin* animGraphPlugin = nullptr;
  34. {
  35. // Set up an empty animgraph with two motion nodes
  36. const AZ::u32 animGraphId = 64;
  37. MCore::CommandGroup group;
  38. // Create empty anim graph, add a motion, entry, hub, and blend tree node.
  39. group.AddCommandString(AZStd::string::format("CreateAnimGraph -animGraphID %d", animGraphId));
  40. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 200 -yPos 200 -name motionNodeA",
  41. animGraphId, azrtti_typeid<AnimGraphMotionNode>().ToString<AZStd::string>().c_str()));
  42. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 0 -yPos 0 -name motionNodeB",
  43. animGraphId, azrtti_typeid<AnimGraphMotionNode>().ToString<AZStd::string>().c_str()));
  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. // Create Needed Objects
  54. const AnimGraphMotionNode* motionNodeA = static_cast<AnimGraphMotionNode*>(animGraph->RecursiveFindNodeByName("motionNodeA"));
  55. const AnimGraphMotionNode* motionNodeB = static_cast<AnimGraphMotionNode*>(animGraph->RecursiveFindNodeByName("motionNodeB"));
  56. EMStudio::BlendGraphWidget* graphWidget = animGraphPlugin->GetGraphWidget();
  57. const EMStudio::GraphNode* graphNodeForMotionNodeB = graphWidget->GetActiveGraph()->FindGraphNode(motionNodeB);
  58. EMotionFX::AnimGraphStateMachine* stateMachine = static_cast<EMotionFX::AnimGraphStateMachine*>(motionNodeA->GetParentNode());
  59. // Ensure that motionNodeA is the entry node
  60. ASSERT_EQ(motionNodeA->GetId(), stateMachine->GetEntryStateId()) << "motionNodeA does not start as set entry state.";
  61. // Select motionNodeB
  62. EMStudio::NodeGraph* nodeGraph = graphWidget->GetActiveGraph();
  63. nodeGraph->FitGraphOnScreen(graphWidget->geometry().width(), graphWidget->geometry().height(), graphWidget->GetMousePos(), true);
  64. const QRect nodeRect = graphNodeForMotionNodeB->GetFinalRect();
  65. QTest::mouseClick(static_cast<QWidget*>(graphWidget), Qt::LeftButton, Qt::NoModifier, nodeRect.center());
  66. ASSERT_EQ(nodeGraph->GetSelectedAnimGraphNodes().size(), 1) << "No node is selected";
  67. ASSERT_EQ(nodeGraph->GetSelectedAnimGraphNodes()[0]->GetNameString(), "motionNodeB") << "Motion Node B was not selected";
  68. // Right click on motionNodeB
  69. const AZStd::vector<EMotionFX::AnimGraphNode*> selectedAnimGraphNodes = nodeGraph->GetSelectedAnimGraphNodes();
  70. graphWidget->OnContextMenuEvent(graphWidget, nodeRect.center(), graphWidget->LocalToGlobal(nodeRect.center()), animGraphPlugin, selectedAnimGraphNodes, true, false, animGraphPlugin->GetActionFilter());
  71. // Click "Set As Entry State" button
  72. QAction* setAsEntryPointButton = UIFixture::GetNamedAction(graphWidget, "Set As Entry State");
  73. ASSERT_TRUE(setAsEntryPointButton) << "Set as entry state button not found.";
  74. setAsEntryPointButton->trigger();
  75. // Verify motionNodeA is no longer the entry node
  76. ASSERT_NE(motionNodeA->GetId(), stateMachine->GetEntryStateId()) << "motionNodeA is still set as entry state.";
  77. // Verify motionNodeB is the entry node
  78. ASSERT_EQ(motionNodeB->GetId(), stateMachine->GetEntryStateId()) << "motionNodeB is not set as entry state.";
  79. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  80. }
  81. }