Files
o3de/Gems/EMotionFX/Code/Tests/SimulatedObjectModelTests.cpp
T
2021-03-08 14:30:57 -08:00

76 lines
4.0 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <Editor/SimulatedObjectHelpers.h>
#include "EMotionFX/CommandSystem/Source/CommandManager.h"
#include "EMotionFX/CommandSystem/Source/SimulatedObjectCommands.h"
#include "EMotionFX/Source/Actor.h"
#include "EMotionFX/Source/Node.h"
#include "EMotionFX/Source/Skeleton.h"
#include "EMotionStudio/EMStudioSDK/Source/EMStudioManager.h"
#include "Editor/Plugins/SimulatedObject/SimulatedObjectWidget.h"
#include <QtWidgets/QApplication>
#include <Editor/SimulatedObjectModel.h>
#include <AzTest/AzTest.h>
#include <Tests/UI/UIFixture.h>
#include <Tests/TestAssetCode/SimpleActors.h>
#include <Tests/TestAssetCode/ActorFactory.h>
namespace EMotionFX
{
using SimulatedObjectModelTestsFixture = UIFixture;
TEST_F(SimulatedObjectModelTestsFixture, CanUndoAddSimulatedObjectAndSimulatedJointWithChildren)
{
AutoRegisteredActor actor = ActorFactory::CreateAndInit<SimpleJointChainActor>(3, "simulatedObjectModelTestActor");
EMotionFX::SimulatedObjectWidget* simulatedObjectWidget = static_cast<EMotionFX::SimulatedObjectWidget*>(EMStudio::GetPluginManager()->FindActivePlugin(EMotionFX::SimulatedObjectWidget::CLASS_ID));
ASSERT_TRUE(simulatedObjectWidget) << "Simulated Object plugin not loaded";
simulatedObjectWidget->ActorSelectionChanged(actor.get());
SimulatedObjectModel* model = simulatedObjectWidget->GetSimulatedObjectModel();
EXPECT_EQ(model->rowCount(), 0) << "Failed to add the simulated object to the model";
AZStd::string result;
// Add one simulated object with no joints
MCore::CommandGroup commandGroup(AZStd::string::format("Add simulated object"));
EMotionFX::SimulatedObjectHelpers::AddSimulatedObject(actor->GetID(), "testSimulatedObject", &commandGroup);
EMotionFX::SimulatedObjectHelpers::AddSimulatedJoints({}, actor->GetSimulatedObjectSetup()->GetNumSimulatedObjects(), false, &commandGroup);
ASSERT_TRUE(EMStudio::GetCommandManager()->ExecuteCommandGroup(commandGroup, result)) << result.c_str();
EXPECT_EQ(model->rowCount(), 1) << "Failed to add the simulated object to the model";
EXPECT_STREQ(model->index(0, 0).data().toString().toUtf8().data(), "testSimulatedObject");
// Add another simulated object with 3 joints
commandGroup = MCore::CommandGroup(AZStd::string::format("Add simulated object and joints"));
EMotionFX::SimulatedObjectHelpers::AddSimulatedObject(actor->GetID(), "testSimulatedObject2", &commandGroup);
CommandSimulatedObjectHelpers::AddSimulatedJoints(actor->GetID(), {0}, actor->GetSimulatedObjectSetup()->GetNumSimulatedObjects(), true, &commandGroup);
ASSERT_TRUE(EMStudio::GetCommandManager()->ExecuteCommandGroup(commandGroup, result)) << result.c_str();
EXPECT_EQ(model->rowCount(), 2) << "Failed to add the simulated object to the model";
EXPECT_STREQ(model->index(1, 0).data().toString().toUtf8().data(), "testSimulatedObject2");
// Undo the second add
ASSERT_TRUE(CommandSystem::GetCommandManager()->Undo(result)) << result.c_str();
EXPECT_EQ(model->rowCount(), 1) << "Failed to remove the second simulated object from the model";
// Undo the first add
ASSERT_TRUE(CommandSystem::GetCommandManager()->Undo(result)) << result.c_str();
EXPECT_EQ(model->rowCount(), 0) << "Failed to remove the first simulated object from the model";
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.
}
} // namespace EMotionFX