SimulatedObjectModelTests.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <Editor/SimulatedObjectHelpers.h>
  9. #include "EMotionFX/CommandSystem/Source/CommandManager.h"
  10. #include "EMotionFX/CommandSystem/Source/SimulatedObjectCommands.h"
  11. #include "EMotionFX/Source/Actor.h"
  12. #include "EMotionFX/Source/Node.h"
  13. #include "EMotionFX/Source/Skeleton.h"
  14. #include "EMotionStudio/EMStudioSDK/Source/EMStudioManager.h"
  15. #include "Editor/Plugins/SimulatedObject/SimulatedObjectWidget.h"
  16. #include <QtWidgets/QApplication>
  17. #include <Editor/SimulatedObjectModel.h>
  18. #include <AzTest/AzTest.h>
  19. #include <Tests/UI/UIFixture.h>
  20. #include <Tests/TestAssetCode/SimpleActors.h>
  21. #include <Tests/TestAssetCode/ActorFactory.h>
  22. #include <Tests/TestAssetCode/TestActorAssets.h>
  23. namespace EMotionFX
  24. {
  25. using SimulatedObjectModelTestsFixture = UIFixture;
  26. TEST_F(SimulatedObjectModelTestsFixture, CanUndoAddSimulatedObjectAndSimulatedJointWithChildren)
  27. {
  28. AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  29. AZ::Data::Asset<Integration::ActorAsset> actorAsset =
  30. TestActorAssets::CreateActorAssetAndRegister<SimpleJointChainActor>(actorAssetId, 3, "simulatedObjectModelTestActor");
  31. const Actor* actor = actorAsset->GetActor();
  32. EMotionFX::SimulatedObjectWidget* simulatedObjectWidget = static_cast<EMotionFX::SimulatedObjectWidget*>(EMStudio::GetPluginManager()->FindActivePlugin(EMotionFX::SimulatedObjectWidget::CLASS_ID));
  33. ASSERT_TRUE(simulatedObjectWidget) << "Simulated Object plugin not loaded";
  34. simulatedObjectWidget->ActorSelectionChanged(actorAsset->GetActor());
  35. SimulatedObjectModel* model = simulatedObjectWidget->GetSimulatedObjectModel();
  36. EXPECT_EQ(model->rowCount(), 0) << "Failed to add the simulated object to the model";
  37. AZStd::string result;
  38. // Add one simulated object with no joints
  39. MCore::CommandGroup commandGroup(AZStd::string::format("Add simulated object"));
  40. EMotionFX::SimulatedObjectHelpers::AddSimulatedObject(actor->GetID(), "testSimulatedObject", &commandGroup);
  41. EMotionFX::SimulatedObjectHelpers::AddSimulatedJoints({}, actor->GetSimulatedObjectSetup()->GetNumSimulatedObjects(), false, &commandGroup);
  42. ASSERT_TRUE(EMStudio::GetCommandManager()->ExecuteCommandGroup(commandGroup, result)) << result.c_str();
  43. EXPECT_EQ(model->rowCount(), 1) << "Failed to add the simulated object to the model";
  44. EXPECT_STREQ(model->index(0, 0).data().toString().toUtf8().data(), "testSimulatedObject");
  45. // Add another simulated object with 3 joints
  46. commandGroup = MCore::CommandGroup(AZStd::string::format("Add simulated object and joints"));
  47. EMotionFX::SimulatedObjectHelpers::AddSimulatedObject(actor->GetID(), "testSimulatedObject2", &commandGroup);
  48. CommandSimulatedObjectHelpers::AddSimulatedJoints(actor->GetID(), {0}, actor->GetSimulatedObjectSetup()->GetNumSimulatedObjects(), true, &commandGroup);
  49. ASSERT_TRUE(EMStudio::GetCommandManager()->ExecuteCommandGroup(commandGroup, result)) << result.c_str();
  50. EXPECT_EQ(model->rowCount(), 2) << "Failed to add the simulated object to the model";
  51. EXPECT_STREQ(model->index(1, 0).data().toString().toUtf8().data(), "testSimulatedObject2");
  52. // Undo the second add
  53. ASSERT_TRUE(CommandSystem::GetCommandManager()->Undo(result)) << result.c_str();
  54. EXPECT_EQ(model->rowCount(), 1) << "Failed to remove the second simulated object from the model";
  55. // Undo the first add
  56. ASSERT_TRUE(CommandSystem::GetCommandManager()->Undo(result)) << result.c_str();
  57. EXPECT_EQ(model->rowCount(), 0) << "Failed to remove the first simulated object from the model";
  58. model->SetActor(nullptr); // Reset the model as otherwise when we destroy the plugin it will still try to use the actor that isn't valid anymore.
  59. }
  60. } // namespace EMotionFX