|
| 1 | +package org.usfirst.frc.team69.util.oi.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import edu.wpi.first.wpilibj.MockCommand; |
| 10 | +import edu.wpi.first.wpilibj.UnitTestUtility; |
| 11 | +import edu.wpi.first.wpilibj.command.Command; |
| 12 | +import edu.wpi.first.wpilibj.command.InstantCommand; |
| 13 | +import edu.wpi.first.wpilibj.command.Scheduler; |
| 14 | +import edu.wpi.first.wpilibj.command.Subsystem; |
| 15 | +import edu.wpi.first.wpilibj.command.WhileCommand; |
| 16 | + |
| 17 | +public class WhileCommandTest { |
| 18 | + |
| 19 | + private MockCondition condition; |
| 20 | + private MockCommand body; |
| 21 | + private WhileCommand whileCommand; |
| 22 | + |
| 23 | + @BeforeClass |
| 24 | + public static void setUpBeforeClass() throws Exception { |
| 25 | + UnitTestUtility.setupMockBase(); |
| 26 | + } |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() throws Exception { |
| 30 | + Scheduler.getInstance().removeAll(); |
| 31 | + |
| 32 | + condition = new MockCondition(true); |
| 33 | + body = new MockCommand(); |
| 34 | + whileCommand = new WhileCommand(condition, body); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testStartup() { |
| 39 | + // Nothing is running at first |
| 40 | + whileCommand.start(); |
| 41 | + assertFalse(whileCommand.isRunning()); |
| 42 | + assertFalse(body.isRunning()); |
| 43 | + |
| 44 | + // On the first tick, the while command starts up |
| 45 | + Scheduler.getInstance().run(); |
| 46 | + assertTrue(whileCommand.isRunning()); |
| 47 | + assertFalse(body.isRunning()); |
| 48 | + |
| 49 | + // On the next tick, the body starts up |
| 50 | + Scheduler.getInstance().run(); |
| 51 | + assertTrue(whileCommand.isRunning()); |
| 52 | + assertTrue(body.isRunning()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testWhileCommandRuns() { |
| 57 | + // Run the body 10 times, with each iteration taking 5 ticks |
| 58 | + whileCommand.start(); |
| 59 | + Scheduler.getInstance().run(); // queue the command |
| 60 | + Scheduler.getInstance().run(); // actually run it |
| 61 | + |
| 62 | + for (int iter = 0; iter < 10; iter++) { |
| 63 | + // run 4 ticks normally |
| 64 | + for (int tick = 0; tick < 4; tick++) { |
| 65 | + Scheduler.getInstance().run(); |
| 66 | + } |
| 67 | + |
| 68 | + // on the 5th tick it finishes, this takes 2 ticks to happen |
| 69 | + body.setHasFinished(true); |
| 70 | + Scheduler.getInstance().run(); // re-queue the body |
| 71 | + body.setHasFinished(false); |
| 72 | + Scheduler.getInstance().run(); // initialize the body |
| 73 | + } |
| 74 | + |
| 75 | + assertEquals(10, body.getInitializeCount()); |
| 76 | + assertEquals(50, body.getExecuteCount()); |
| 77 | + assertEquals(50, body.getIsFinishedCount()); |
| 78 | + assertEquals(10, body.getEndCount()); |
| 79 | + assertEquals(0, body.getInterruptedCount()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testTransferRequirements() { |
| 84 | + Subsystem req = new Subsystem() { |
| 85 | + @Override protected void initDefaultCommand() {} |
| 86 | + }; |
| 87 | + |
| 88 | + Command bodyWithReqs = new InstantCommand() { |
| 89 | + { requires(req); } |
| 90 | + }; |
| 91 | + |
| 92 | + WhileCommand whileCommandWithReqs = new WhileCommand(condition, bodyWithReqs); |
| 93 | + assertTrue(whileCommandWithReqs.doesRequire(req)); |
| 94 | + assertFalse(bodyWithReqs.doesRequire(req)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testDoesNotRunIfFalse() { |
| 99 | + whileCommand.start(); |
| 100 | + condition.set(false); |
| 101 | + |
| 102 | + Scheduler.getInstance().run(); // queue the whileCommand |
| 103 | + Scheduler.getInstance().run(); // should check and stop |
| 104 | + |
| 105 | + assertFalse(whileCommand.isRunning()); |
| 106 | + assertFalse(body.isRunning()); |
| 107 | + assertEquals(0, body.getInitializeCount()); |
| 108 | + assertEquals(0, body.getExecuteCount()); |
| 109 | + assertEquals(0, body.getIsFinishedCount()); |
| 110 | + assertEquals(0, body.getEndCount()); |
| 111 | + assertEquals(0, body.getInterruptedCount()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testEndsWhenFalse() { |
| 116 | + whileCommand.start(); |
| 117 | + body.setHasFinished(true); |
| 118 | + |
| 119 | + Scheduler.getInstance().run(); // queue the whileCommand |
| 120 | + Scheduler.getInstance().run(); // queue the body |
| 121 | + Scheduler.getInstance().run(); // run the body once |
| 122 | + |
| 123 | + condition.set(false); |
| 124 | + Scheduler.getInstance().run(); // about to re-queue, but condition is false |
| 125 | + |
| 126 | + assertFalse(whileCommand.isRunning()); |
| 127 | + assertFalse(body.isRunning()); |
| 128 | + assertEquals(1, body.getInitializeCount()); |
| 129 | + assertEquals(1, body.getExecuteCount()); |
| 130 | + assertEquals(1, body.getIsFinishedCount()); |
| 131 | + assertEquals(1, body.getEndCount()); |
| 132 | + assertEquals(0, body.getInterruptedCount()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testCancelWhileCommand() { |
| 137 | + whileCommand.start(); |
| 138 | + Scheduler.getInstance().run(); // queue the whileCommand |
| 139 | + Scheduler.getInstance().run(); // queue the body |
| 140 | + Scheduler.getInstance().run(); // queue run the body once |
| 141 | + |
| 142 | + whileCommand.cancel(); |
| 143 | + Scheduler.getInstance().run(); // make the cancellation take effect |
| 144 | + |
| 145 | + assertFalse(whileCommand.isRunning()); |
| 146 | + assertFalse(body.isRunning()); |
| 147 | + assertEquals(1, body.getInitializeCount()); |
| 148 | + assertEquals(1, body.getExecuteCount()); |
| 149 | + assertEquals(1, body.getIsFinishedCount()); |
| 150 | + assertEquals(0, body.getEndCount()); |
| 151 | + assertEquals(1, body.getInterruptedCount()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testCancelBodyCommand() { |
| 156 | + whileCommand.start(); |
| 157 | + Scheduler.getInstance().run(); // queue the whileCommand |
| 158 | + Scheduler.getInstance().run(); // queue the body |
| 159 | + Scheduler.getInstance().run(); // queue run the body once |
| 160 | + |
| 161 | + body.cancel(); |
| 162 | + Scheduler.getInstance().run(); // make the cancellation take effect |
| 163 | + Scheduler.getInstance().run(); // Let the command re-queue |
| 164 | + Scheduler.getInstance().run(); // Let the command start up again |
| 165 | + |
| 166 | + assertTrue(whileCommand.isRunning()); |
| 167 | + assertTrue(body.isRunning()); |
| 168 | + // don't bother checking execute count, that's dependant on |
| 169 | + // implementation details of the scheduler (what order it runs the |
| 170 | + // two commands in) |
| 171 | + assertEquals(2, body.getInitializeCount()); |
| 172 | + assertEquals(0, body.getEndCount()); |
| 173 | + assertEquals(1, body.getInterruptedCount()); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments