Fix cancel of inner commands in ConditionalCommands (#858)

This commit is contained in:
sciencewhiz
2018-01-18 20:04:33 -08:00
committed by Peter Johnson
parent 0e8ff4663d
commit e4e1eab413
15 changed files with 901 additions and 30 deletions

View File

@@ -7,22 +7,38 @@
package edu.wpi.first.wpilibj.command;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
//import org.junit.Ignore;
import org.junit.Test;
public class ConditionalCommandTest extends AbstractCommandTest {
MockConditionalCommand m_command;
MockConditionalCommand m_commandNull;
MockCommand m_onTrue;
MockCommand m_onFalse;
MockSubsystem m_subsys;
Boolean m_condition;
@Before
public void initCommands() {
m_onTrue = new MockCommand();
m_onFalse = new MockCommand();
m_subsys = new MockSubsystem();
m_onTrue = new MockCommand(m_subsys);
m_onFalse = new MockCommand(m_subsys);
m_command = new MockConditionalCommand(m_onTrue, m_onFalse);
m_commandNull = new MockConditionalCommand(m_onTrue, null);
}
protected void assertConditionalCommandState(MockConditionalCommand command, int initialize,
int execute, int isFinished, int end,
int interrupted) {
assertEquals(initialize, command.getInitializeCount());
assertEquals(execute, command.getExecuteCount());
assertEquals(isFinished, command.getIsFinishedCount());
assertEquals(end, command.getEndCount());
assertEquals(interrupted, command.getInterruptedCount());
}
@Test
@@ -31,14 +47,33 @@ public class ConditionalCommandTest extends AbstractCommandTest {
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 2, 0, 0);
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 4, 0, 0);
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
assertTrue("Did not initialize the true command", m_onTrue.getInitializeCount() > 0);
assertTrue("Initialized the false command", m_onFalse.getInitializeCount() == 0);
@@ -50,16 +85,261 @@ public class ConditionalCommandTest extends AbstractCommandTest {
Scheduler.getInstance().add(m_command);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onFalse
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onFalse
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 1, 2, 0, 0);
assertCommandState(m_onFalse, 1, 1, 1, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 2, 4, 0, 0);
assertCommandState(m_onFalse, 1, 2, 2, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onFalse.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 3, 3, 1, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 3, 3, 1, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
assertTrue("Did not initialize the false command", m_onFalse.getInitializeCount() > 0);
assertTrue("Initialized the true command", m_onTrue.getInitializeCount() == 0);
}
@Test
public void testCancelSubCommand() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.cancel();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
}
@Test
public void testCancelRequires() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onFalse.start();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 1);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 0, 1);
assertCommandState(m_onFalse, 1, 1, 1, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 1);
}
@Test
public void testCancelCondCommand() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_command.cancel();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 1);
}
@Test
public void testOnTrueTwice() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
m_onTrue.resetCounters();
m_command.resetCounters();
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
}
@Test
public void testOnTrueInstant() {
m_command.setCondition(true);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 1, 0);
}
@Test
public void testOnFalseNull() {
m_commandNull.setCondition(false);
Scheduler.getInstance().add(m_commandNull);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onFalse
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onFalse
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 1, 1, 1, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 1, 1, 1, 1, 0);
}
}

View File

@@ -8,7 +8,7 @@
package edu.wpi.first.wpilibj.command;
/**
* A class to simulate a simple command The command keeps track of how many times each method was
* A class to simulate a simple command. The command keeps track of how many times each method was
* called.
*/
public class MockCommand extends Command {
@@ -19,6 +19,15 @@ public class MockCommand extends Command {
private int m_endCount = 0;
private int m_interruptedCount = 0;
public MockCommand(Subsystem subsys) {
super();
requires(subsys);
}
public MockCommand() {
super();
}
protected void initialize() {
++m_initializeCount;
}
@@ -115,4 +124,16 @@ public class MockCommand extends Command {
return getInterruptedCount() > 0;
}
/**
* Reset internal counters.
*/
public void resetCounters() {
m_initializeCount = 0;
m_executeCount = 0;
m_isFinishedCount = 0;
m_hasFinished = false;
m_endCount = 0;
m_interruptedCount = 0;
}
}

View File

@@ -9,6 +9,11 @@ package edu.wpi.first.wpilibj.command;
public class MockConditionalCommand extends ConditionalCommand {
private boolean m_condition = false;
private int m_initializeCount = 0;
private int m_executeCount = 0;
private int m_isFinishedCount = 0;
private int m_endCount = 0;
private int m_interruptedCount = 0;
public MockConditionalCommand(MockCommand onTrue, MockCommand onFalse) {
super(onTrue, onFalse);
@@ -22,4 +27,94 @@ public class MockConditionalCommand extends ConditionalCommand {
public void setCondition(boolean condition) {
this.m_condition = condition;
}
protected void initialize() {
++m_initializeCount;
}
protected void execute() {
++m_executeCount;
}
protected boolean isFinished() {
++m_isFinishedCount;
return super.isFinished();
}
protected void end() {
++m_endCount;
}
protected void interrupted() {
++m_interruptedCount;
}
/**
* How many times the initialize method has been called.
*/
public int getInitializeCount() {
return m_initializeCount;
}
/**
* If the initialize method has been called at least once.
*/
public boolean hasInitialized() {
return getInitializeCount() > 0;
}
/**
* How many time the execute method has been called.
*/
public int getExecuteCount() {
return m_executeCount;
}
/**
* How many times the isFinished method has been called.
*/
public int getIsFinishedCount() {
return m_isFinishedCount;
}
/**
* How many times the end method has been called.
*/
public int getEndCount() {
return m_endCount;
}
/**
* If the end method has been called at least once.
*/
public boolean hasEnd() {
return getEndCount() > 0;
}
/**
* How many times the interrupted method has been called.
*/
public int getInterruptedCount() {
return m_interruptedCount;
}
/**
* If the interrupted method has been called at least once.
*/
public boolean hasInterrupted() {
return getInterruptedCount() > 0;
}
/**
* Reset internal counters.
*/
public void resetCounters() {
m_condition = false;
m_initializeCount = 0;
m_executeCount = 0;
m_isFinishedCount = 0;
m_endCount = 0;
m_interruptedCount = 0;
}
}

View File

@@ -0,0 +1,15 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
/**
* A class to simulate a simple subsystem.
*/
public class MockSubsystem extends Subsystem {
protected void initDefaultCommand() {}
}