AddParameter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <Tests/UI/UIFixture.h>
  10. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  11. #include <EMotionFX/Source/Actor.h>
  12. #include <EMotionFX/Source/AnimGraphManager.h>
  13. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  14. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  15. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/ParameterCreateEditWidget.h>
  16. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/ParameterWindow.h>
  17. #include <QApplication>
  18. #include <QComboBox>
  19. #include <QtTest>
  20. #include "qtestsystem.h"
  21. namespace EMotionFX
  22. {
  23. class AddParametersFixture
  24. : public UIFixture
  25. , public ::testing::WithParamInterface</*ParameterTypeIndex=*/int>
  26. {
  27. public:
  28. void SetUp() override
  29. {
  30. UIFixture::SetUp();
  31. AZStd::string commandResult;
  32. // Create empty anim graph and select it.
  33. const AZStd::string command = AZStd::string::format("CreateAnimGraph -animGraphID %d", m_animGraphId);
  34. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(command, commandResult)) << commandResult.c_str();
  35. m_animGraph = GetAnimGraphManager().FindAnimGraphByID(m_animGraphId);
  36. EXPECT_NE(m_animGraph, nullptr) << "Cannot find newly created anim graph.";
  37. }
  38. void TearDown() override
  39. {
  40. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  41. delete m_animGraph;
  42. UIFixture::TearDown();
  43. }
  44. public:
  45. const AZ::u32 m_animGraphId = 64;
  46. AnimGraph* m_animGraph = nullptr;
  47. };
  48. TEST_P(AddParametersFixture, AddParameters)
  49. {
  50. RecordProperty("test_case_id", "C1559138");
  51. auto animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  52. ASSERT_TRUE(animGraphPlugin) << "Anim graph plugin not found.";
  53. EMStudio::ParameterWindow* parameterWindow = animGraphPlugin->GetParameterWindow();
  54. ASSERT_TRUE(parameterWindow) << "Anim graph parameter window is invalid.";
  55. // Normally users press the + button and a context menu appears with the options to either add a parameter or a group.
  56. // We are bypassing the context menu and directly call the add parameter slot.
  57. parameterWindow->OnAddParameter();
  58. // Create parameter window.
  59. QWidget* parameterCreate = FindTopLevelWidget("ParameterCreateEditWidget");
  60. ASSERT_NE(parameterCreate, nullptr) << "Cannot find anim graph parameter create/edit widget. Is the anim graph selected?";
  61. auto parameterCreateWidget = qobject_cast<EMStudio::ParameterCreateEditWidget*>(parameterCreate);
  62. // Set the parameter type using the combo box.
  63. QComboBox* valueTypeComboBox = parameterCreateWidget->GetValueTypeComboBox();
  64. const int row = GetParam();
  65. valueTypeComboBox->setCurrentIndex(row);
  66. EXPECT_EQ(row, valueTypeComboBox->currentIndex()) << "Changing the value type failed. Out of bounds?";
  67. // Verify if the type ids match.
  68. const QByteArray parameterTypeIdString = valueTypeComboBox->itemData(row, Qt::UserRole).toString().toUtf8();
  69. const AZ::TypeId parameterTypeId = AZ::TypeId::CreateString(parameterTypeIdString.data(), parameterTypeIdString.size());
  70. EXPECT_FALSE(parameterTypeId.IsNull()) << "Selected parameter type is invalid.";
  71. EXPECT_EQ(parameterTypeId, EMotionFX::ParameterFactory::GetValueParameterTypes()[row])
  72. << "The parameter type id from the combo box do not match the type ids from the parameter factory.";
  73. // Accept the dialog (this creates the actual parameter object in the anim graph).
  74. parameterCreateWidget->accept();
  75. // Verify the parameter in the anim graph.
  76. EXPECT_EQ(m_animGraph->GetNumParameters(), 1) << "Parameter creation failed. We should end up with exactly one parameter.";
  77. const Parameter* parameter = m_animGraph->FindParameter(0);
  78. EXPECT_NE(parameter, nullptr) << "The parameter we created should be valid.";
  79. EXPECT_EQ(parameter->RTTI_GetType(), parameterTypeId)
  80. << "The type of the created parameter does not match the selected type in the dialog.";
  81. }
  82. std::vector<int> GetValueParameterTypeIndices()
  83. {
  84. // We cannot call EMotionFX::ParameterFactory::GetValueParameterTypes().size() here as the Az is not initialized yet.
  85. const int numValueParameterTypes = 13;
  86. std::vector<int> result(numValueParameterTypes);
  87. std::iota (std::begin(result), std::end(result), 0);
  88. return result;
  89. }
  90. INSTANTIATE_TEST_CASE_P(AddParameters,
  91. AddParametersFixture,
  92. ::testing::ValuesIn(GetValueParameterTypeIndices()));
  93. } // namespace EMotionFX