AnimGraphActivateTests.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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>
  11. #include <qtoolbar.h>
  12. #include <QWidget>
  13. #include <QComboBox>
  14. #include <Tests/UI/UIFixture.h>
  15. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  16. #include <EMotionFX/Source/AnimGraphEntryNode.h>
  17. #include <EMotionFX/Source/AnimGraphHubNode.h>
  18. #include <EMotionFX/Source/AnimGraphManager.h>
  19. #include <EMotionFX/Source/ActorManager.h>
  20. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  21. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  22. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphViewWidget.h>
  23. #include <Tests/TestAssetCode/ActorFactory.h>
  24. #include <Tests/TestAssetCode/SimpleActors.h>
  25. #include <Tests/TestAssetCode/TestActorAssets.h>
  26. #include <EMotionFX/Source/MotionManager.h>
  27. namespace EMotionFX
  28. {
  29. TEST_F(UIFixture, CanNotActiveEmptyGraph)
  30. {
  31. // This test checks that activating an empty anim graph does nothing.
  32. EMStudio::GetMainWindow()->ApplicationModeChanged("AnimGraph");
  33. auto animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  34. ASSERT_TRUE(animGraphPlugin) << "Anim graph plugin not found.";
  35. ASSERT_FALSE(animGraphPlugin->GetActiveAnimGraph()) << "No anim graph should be activated.";
  36. ASSERT_EQ(0, EMotionFX::GetAnimGraphManager().GetNumAnimGraphs()) << "Anim graph manager should contain 0 anim graphs.";
  37. auto toolBar = animGraphPlugin->GetViewWidget()->findChild<QToolBar*>("EMFX.BlendGraphViewWidget.TopToolBar");
  38. QWidget* activateButton = UIFixture::GetWidgetFromToolbar(toolBar, "Activate Animgraph/State");
  39. ASSERT_TRUE(activateButton) << "Activate anim graph button not found.";
  40. QTest::mouseClick(activateButton, Qt::LeftButton);
  41. ASSERT_FALSE(animGraphPlugin->GetActiveAnimGraph()) << "No anim graph should be activated after click the activate button.";
  42. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  43. }
  44. class PopulatedAnimGraphFixture
  45. : public UIFixture
  46. {
  47. public:
  48. void SetUp() override
  49. {
  50. UIFixture::SetUp();
  51. AZStd::string commandResult;
  52. MCore::CommandGroup group;
  53. // Create empty anim graph, add a motion, entry, hub, and blend tree node.
  54. group.AddCommandString(AZStd::string::format("CreateAnimGraph -animGraphID %d", m_animGraphId));
  55. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 100 -yPos 100 -name %s",
  56. m_animGraphId, azrtti_typeid<AnimGraphMotionNode>().ToString<AZStd::string>().c_str(), m_motionNodeName.c_str()));
  57. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 200 -yPos 100 -name %s",
  58. m_animGraphId, azrtti_typeid<BlendTree>().ToString<AZStd::string>().c_str(), m_blendTreeName.c_str()));
  59. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 200 -yPos 100 -name %s",
  60. m_animGraphId, azrtti_typeid<AnimGraphHubNode>().ToString<AZStd::string>().c_str(), m_hubNodeName.c_str()));
  61. group.AddCommandString(AZStd::string::format("AnimGraphCreateNode -animGraphID %d -type %s -parentName Root -xPos 200 -yPos 100 -name %s",
  62. m_animGraphId, azrtti_typeid<AnimGraphEntryNode>().ToString<AZStd::string>().c_str(), m_entryNodeName.c_str()));
  63. // Create new motion set
  64. group.AddCommandString(AZStd::string::format("CreateMotionSet -name motionSet0 -setID %d", m_motionSetID));
  65. // Run Commands
  66. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommandGroup(group, commandResult)) << commandResult.c_str();
  67. // Create temp Actor
  68. AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  69. m_actorAsset = TestActorAssets::CreateActorAssetAndRegister<SimpleJointChainActor>(actorAssetId, 1, "tempActor");
  70. // Cache some local poitners.
  71. m_animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  72. ASSERT_NE(m_animGraphPlugin, nullptr) << "Anim graph plugin not found.";
  73. m_animGraph = GetAnimGraphManager().FindAnimGraphByID(m_animGraphId);
  74. EXPECT_NE(m_animGraph, nullptr) << "Cannot find newly created anim graph.";
  75. }
  76. void TearDown() override
  77. {
  78. GetEMotionFX().GetActorManager()->UnregisterAllActors();
  79. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  80. delete m_animGraph;
  81. m_actorAsset.Reset();
  82. UIFixture::TearDown();
  83. }
  84. public:
  85. const AZ::u32 m_animGraphId = 64;
  86. const AZ::u32 m_motionSetID = 32;
  87. AZStd::string m_motionNodeName = "testMotion";
  88. AZStd::string m_blendTreeName = "testBlendTree";
  89. AZStd::string m_hubNodeName = "testHub";
  90. AZStd::string m_entryNodeName = "testEntry";
  91. AnimGraph* m_animGraph = nullptr;
  92. EMStudio::AnimGraphPlugin* m_animGraphPlugin = nullptr;
  93. AZ::Data::Asset<Integration::ActorAsset> m_actorAsset;
  94. };
  95. TEST_F(PopulatedAnimGraphFixture, CanActivateValidGraph)
  96. {
  97. // This test checks that activating a filled anim graph will not crash and create an animgraph instance
  98. RecordProperty("test_case_id", "C1559131");
  99. // Find QComboBox that indicates preview of motionSet
  100. QComboBox* motionSetPreviewSelector = qobject_cast<QComboBox*>(PopulatedAnimGraphFixture::FindTopLevelWidget("EMFX.AttributesWindowWidget.AnimGraph.MotionSetComboBox"));
  101. // Set Preview motionset as created MotionSet
  102. motionSetPreviewSelector->setCurrentIndex(1);
  103. ASSERT_EQ(motionSetPreviewSelector->currentText(), "motionSet0") << "Preview Motionset could not be set";
  104. // Find activate animigraph button
  105. auto toolBar = m_animGraphPlugin->GetViewWidget()->findChild<QToolBar*>("EMFX.BlendGraphViewWidget.TopToolBar");
  106. QWidget* activateButton = PopulatedAnimGraphFixture::GetWidgetFromToolbar(toolBar, "Activate Animgraph/State");
  107. // Click activate animigraph button
  108. ASSERT_TRUE(activateButton) << "Activate anim graph button not found.";
  109. QTest::mouseClick(activateButton, Qt::LeftButton);
  110. // Confirm that the animigraph instance was created and is active
  111. ASSERT_TRUE(m_animGraphPlugin->GetActiveAnimGraph()) << "Anim graph should be activated.";
  112. ASSERT_EQ(1, EMotionFX::GetAnimGraphManager().GetNumAnimGraphs()) << "Anim graph manager should contain 1 anim graphs.";
  113. }
  114. } // namespace EMotionFX