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 17:47:07 -04:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj2.command;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
|
2023-07-24 00:34:49 -04:00
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
|
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2020-12-29 22:45:16 -08:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
class CommandScheduleTest extends CommandTestBase {
|
|
|
|
|
@Test
|
|
|
|
|
void instantScheduleTest() {
|
2020-07-24 13:07:11 -07:00
|
|
|
try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
|
|
|
MockCommandHolder holder = new MockCommandHolder(true);
|
|
|
|
|
holder.setFinished(true);
|
|
|
|
|
Command mockCommand = holder.getMock();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.schedule(mockCommand);
|
|
|
|
|
assertTrue(scheduler.isScheduled(mockCommand));
|
|
|
|
|
verify(mockCommand).initialize();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.run();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
verify(mockCommand).execute();
|
|
|
|
|
verify(mockCommand).end(false);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
assertFalse(scheduler.isScheduled(mockCommand));
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void singleIterationScheduleTest() {
|
2020-07-24 13:07:11 -07:00
|
|
|
try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
|
|
|
MockCommandHolder holder = new MockCommandHolder(true);
|
|
|
|
|
Command mockCommand = holder.getMock();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.schedule(mockCommand);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
assertTrue(scheduler.isScheduled(mockCommand));
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.run();
|
|
|
|
|
holder.setFinished(true);
|
|
|
|
|
scheduler.run();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
verify(mockCommand).initialize();
|
|
|
|
|
verify(mockCommand, times(2)).execute();
|
|
|
|
|
verify(mockCommand).end(false);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
assertFalse(scheduler.isScheduled(mockCommand));
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void multiScheduleTest() {
|
2020-07-24 13:07:11 -07:00
|
|
|
try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
|
|
|
MockCommandHolder command1Holder = new MockCommandHolder(true);
|
|
|
|
|
Command command1 = command1Holder.getMock();
|
|
|
|
|
MockCommandHolder command2Holder = new MockCommandHolder(true);
|
|
|
|
|
Command command2 = command2Holder.getMock();
|
|
|
|
|
MockCommandHolder command3Holder = new MockCommandHolder(true);
|
|
|
|
|
Command command3 = command3Holder.getMock();
|
|
|
|
|
|
2022-08-30 07:53:47 +03:00
|
|
|
scheduler.schedule(command1, command2, command3);
|
2020-07-24 13:07:11 -07:00
|
|
|
assertTrue(scheduler.isScheduled(command1, command2, command3));
|
|
|
|
|
scheduler.run();
|
|
|
|
|
assertTrue(scheduler.isScheduled(command1, command2, command3));
|
|
|
|
|
|
|
|
|
|
command1Holder.setFinished(true);
|
|
|
|
|
scheduler.run();
|
|
|
|
|
assertTrue(scheduler.isScheduled(command2, command3));
|
|
|
|
|
assertFalse(scheduler.isScheduled(command1));
|
|
|
|
|
|
|
|
|
|
command2Holder.setFinished(true);
|
|
|
|
|
scheduler.run();
|
|
|
|
|
assertTrue(scheduler.isScheduled(command3));
|
|
|
|
|
assertFalse(scheduler.isScheduled(command1, command2));
|
|
|
|
|
|
|
|
|
|
command3Holder.setFinished(true);
|
|
|
|
|
scheduler.run();
|
|
|
|
|
assertFalse(scheduler.isScheduled(command1, command2, command3));
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void schedulerCancelTest() {
|
2020-07-24 13:07:11 -07:00
|
|
|
try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
|
|
|
MockCommandHolder holder = new MockCommandHolder(true);
|
|
|
|
|
Command mockCommand = holder.getMock();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.schedule(mockCommand);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
scheduler.run();
|
|
|
|
|
scheduler.cancel(mockCommand);
|
|
|
|
|
scheduler.run();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
verify(mockCommand).execute();
|
|
|
|
|
verify(mockCommand).end(true);
|
|
|
|
|
verify(mockCommand, never()).end(false);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
assertFalse(scheduler.isScheduled(mockCommand));
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void notScheduledCancelTest() {
|
2020-07-24 13:07:11 -07:00
|
|
|
try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
|
|
|
MockCommandHolder holder = new MockCommandHolder(true);
|
|
|
|
|
Command mockCommand = holder.getMock();
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
assertDoesNotThrow(() -> scheduler.cancel(mockCommand));
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
2023-07-24 00:34:49 -04:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void smartDashboardCancelTest() {
|
|
|
|
|
try (CommandScheduler scheduler = new CommandScheduler();
|
|
|
|
|
var inst = NetworkTableInstance.create()) {
|
|
|
|
|
SmartDashboard.setNetworkTableInstance(inst);
|
|
|
|
|
SmartDashboard.putData("Scheduler", scheduler);
|
|
|
|
|
SmartDashboard.updateValues();
|
|
|
|
|
|
|
|
|
|
MockCommandHolder holder = new MockCommandHolder(true);
|
|
|
|
|
Command mockCommand = holder.getMock();
|
|
|
|
|
scheduler.schedule(mockCommand);
|
|
|
|
|
scheduler.run();
|
|
|
|
|
SmartDashboard.updateValues();
|
|
|
|
|
assertTrue(scheduler.isScheduled(mockCommand));
|
|
|
|
|
|
|
|
|
|
var table = inst.getTable("SmartDashboard");
|
|
|
|
|
table.getEntry("Scheduler/Cancel").setIntegerArray(new long[] {mockCommand.hashCode()});
|
|
|
|
|
SmartDashboard.updateValues();
|
|
|
|
|
scheduler.run();
|
|
|
|
|
assertFalse(scheduler.isScheduled(mockCommand));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|