2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#include "CommandTestBase.h"
|
2022-11-28 02:23:56 +02:00
|
|
|
#include "CompositionTestBase.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
#include "frc2/command/InstantCommand.h"
|
|
|
|
|
#include "frc2/command/SequentialCommandGroup.h"
|
|
|
|
|
#include "frc2/command/WaitUntilCommand.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
class SequentialCommandGroupTest : public CommandTestBase {};
|
|
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(SequentialCommandGroupTest, SequentialGroupSchedule) {
|
2019-08-25 23:55:59 -04:00
|
|
|
CommandScheduler scheduler = GetScheduler();
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<MockCommand> command1Holder = std::make_unique<MockCommand>();
|
|
|
|
|
std::unique_ptr<MockCommand> command2Holder = std::make_unique<MockCommand>();
|
|
|
|
|
std::unique_ptr<MockCommand> command3Holder = std::make_unique<MockCommand>();
|
|
|
|
|
|
|
|
|
|
MockCommand* command1 = command1Holder.get();
|
|
|
|
|
MockCommand* command2 = command2Holder.get();
|
|
|
|
|
MockCommand* command3 = command3Holder.get();
|
|
|
|
|
|
2023-11-12 20:23:34 -08:00
|
|
|
SequentialCommandGroup group{make_vector<std::unique_ptr<Command>>(
|
2019-08-25 23:55:59 -04:00
|
|
|
std::move(command1Holder), std::move(command2Holder),
|
|
|
|
|
std::move(command3Holder))};
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command1, Initialize());
|
|
|
|
|
EXPECT_CALL(*command1, Execute()).Times(1);
|
|
|
|
|
EXPECT_CALL(*command1, End(false));
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command2, Initialize());
|
|
|
|
|
EXPECT_CALL(*command2, Execute()).Times(1);
|
|
|
|
|
EXPECT_CALL(*command2, End(false));
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command3, Initialize());
|
|
|
|
|
EXPECT_CALL(*command3, Execute()).Times(1);
|
|
|
|
|
EXPECT_CALL(*command3, End(false));
|
|
|
|
|
|
|
|
|
|
scheduler.Schedule(&group);
|
|
|
|
|
|
|
|
|
|
command1->SetFinished(true);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
command2->SetFinished(true);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
command3->SetFinished(true);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(scheduler.IsScheduled(&group));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(SequentialCommandGroupTest, SequentialGroupInterrupt) {
|
2019-08-25 23:55:59 -04:00
|
|
|
CommandScheduler scheduler = GetScheduler();
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<MockCommand> command1Holder = std::make_unique<MockCommand>();
|
|
|
|
|
std::unique_ptr<MockCommand> command2Holder = std::make_unique<MockCommand>();
|
|
|
|
|
std::unique_ptr<MockCommand> command3Holder = std::make_unique<MockCommand>();
|
|
|
|
|
|
|
|
|
|
MockCommand* command1 = command1Holder.get();
|
|
|
|
|
MockCommand* command2 = command2Holder.get();
|
|
|
|
|
MockCommand* command3 = command3Holder.get();
|
|
|
|
|
|
2023-11-12 20:23:34 -08:00
|
|
|
SequentialCommandGroup group{make_vector<std::unique_ptr<Command>>(
|
2019-08-25 23:55:59 -04:00
|
|
|
std::move(command1Holder), std::move(command2Holder),
|
|
|
|
|
std::move(command3Holder))};
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command1, Initialize());
|
|
|
|
|
EXPECT_CALL(*command1, Execute()).Times(1);
|
|
|
|
|
EXPECT_CALL(*command1, End(false));
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command2, Initialize());
|
|
|
|
|
EXPECT_CALL(*command2, Execute()).Times(0);
|
|
|
|
|
EXPECT_CALL(*command2, End(false)).Times(0);
|
|
|
|
|
EXPECT_CALL(*command2, End(true));
|
|
|
|
|
|
|
|
|
|
EXPECT_CALL(*command3, Initialize()).Times(0);
|
|
|
|
|
EXPECT_CALL(*command3, Execute()).Times(0);
|
|
|
|
|
EXPECT_CALL(*command3, End(false)).Times(0);
|
|
|
|
|
EXPECT_CALL(*command3, End(true)).Times(0);
|
|
|
|
|
|
|
|
|
|
scheduler.Schedule(&group);
|
|
|
|
|
|
|
|
|
|
command1->SetFinished(true);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
scheduler.Cancel(&group);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(scheduler.IsScheduled(&group));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(SequentialCommandGroupTest, SequentialGroupNotScheduledCancel) {
|
2019-08-25 23:55:59 -04:00
|
|
|
CommandScheduler scheduler = GetScheduler();
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup group{InstantCommand(), InstantCommand()};
|
|
|
|
|
|
|
|
|
|
EXPECT_NO_FATAL_FAILURE(scheduler.Cancel(&group));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(SequentialCommandGroupTest, SequentialGroupCopy) {
|
2019-08-25 23:55:59 -04:00
|
|
|
CommandScheduler scheduler = GetScheduler();
|
|
|
|
|
|
|
|
|
|
bool finished = false;
|
|
|
|
|
|
|
|
|
|
WaitUntilCommand command([&finished] { return finished; });
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup group(command);
|
|
|
|
|
scheduler.Schedule(&group);
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
EXPECT_TRUE(scheduler.IsScheduled(&group));
|
|
|
|
|
finished = true;
|
|
|
|
|
scheduler.Run();
|
|
|
|
|
EXPECT_FALSE(scheduler.IsScheduled(&group));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(SequentialCommandGroupTest, SequentialGroupRequirement) {
|
2019-08-25 23:55:59 -04:00
|
|
|
CommandScheduler scheduler = GetScheduler();
|
|
|
|
|
|
|
|
|
|
TestSubsystem requirement1;
|
|
|
|
|
TestSubsystem requirement2;
|
|
|
|
|
TestSubsystem requirement3;
|
|
|
|
|
TestSubsystem requirement4;
|
|
|
|
|
|
|
|
|
|
InstantCommand command1([] {}, {&requirement1, &requirement2});
|
|
|
|
|
InstantCommand command2([] {}, {&requirement3});
|
|
|
|
|
InstantCommand command3([] {}, {&requirement3, &requirement4});
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup group(std::move(command1), std::move(command2));
|
|
|
|
|
|
|
|
|
|
scheduler.Schedule(&group);
|
|
|
|
|
scheduler.Schedule(&command3);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(scheduler.IsScheduled(&command3));
|
|
|
|
|
EXPECT_FALSE(scheduler.IsScheduled(&group));
|
|
|
|
|
}
|
2022-11-28 02:23:56 +02:00
|
|
|
|
|
|
|
|
INSTANTIATE_MULTI_COMMAND_COMPOSITION_TEST_SUITE(SequentialCommandGroupTest,
|
|
|
|
|
SequentialCommandGroup);
|