Skip to content

Commit ee71587

Browse files
author
James Hagborg
committed
Unit test release command
1 parent b9b99ec commit ee71587

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
// We only need the desktop version right now, since this build file is only used for local testing.
2424
compile group: 'edu.wpi.first.wpilib.networktables.java', name: 'NetworkTables', version: '3.+', classifier: 'desktop'
2525
testCompile group: 'junit', name: 'junit', version: '4.+'
26+
testCompile group: 'com.google.guava', name: 'guava', version: '19.0'
2627
}
2728

2829
task sourcesJar(type: Jar, dependsOn: classes) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
7+
8+
package edu.wpi.first.wpilibj;
9+
10+
/**
11+
* A concrete implementation of the robot state interface that can be used in UnitTests.
12+
*/
13+
public class MockRobotStateInterface implements RobotState.Interface {
14+
@Override
15+
public boolean isDisabled() {
16+
return false;
17+
}
18+
19+
@Override
20+
public boolean isEnabled() {
21+
return true;
22+
}
23+
24+
@Override
25+
public boolean isOperatorControl() {
26+
return false;
27+
}
28+
29+
@Override
30+
public boolean isAutonomous() {
31+
return false;
32+
}
33+
34+
@Override
35+
public boolean isTest() {
36+
return true;
37+
}
38+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
7+
8+
package edu.wpi.first.wpilibj;
9+
10+
import com.google.common.base.Stopwatch;
11+
12+
import java.util.concurrent.TimeUnit;
13+
14+
import edu.wpi.first.wpilibj.util.BaseSystemNotInitializedException;
15+
16+
/**
17+
* Utility class for configuring unit tests.
18+
*/
19+
public final class UnitTestUtility {
20+
private UnitTestUtility() {
21+
/* no-op */
22+
}
23+
24+
/**
25+
* Sets up the base system WPILib so that it does not rely on hardware.
26+
*/
27+
public static void setupMockBase() {
28+
try {
29+
// Check to see if this has been setup
30+
Timer.getFPGATimestamp();
31+
} catch (BaseSystemNotInitializedException ex) {
32+
// If it hasn't been then do this setup
33+
34+
HLUsageReporting.SetImplementation(new HLUsageReporting.Null());
35+
RobotState.SetImplementation(new MockRobotStateInterface());
36+
Timer.SetImplementation(new Timer.StaticInterface() {
37+
38+
@Override
39+
public double getFPGATimestamp() {
40+
return System.currentTimeMillis() / 1000.0;
41+
}
42+
43+
@Override
44+
public double getMatchTime() {
45+
return 0;
46+
}
47+
48+
@Override
49+
public void delay(double seconds) {
50+
try {
51+
Thread.sleep((long) (seconds * 1e3));
52+
} catch (InterruptedException ex) {
53+
Thread.currentThread().interrupt();
54+
throw new RuntimeException("Thread was interrupted", ex);
55+
}
56+
}
57+
58+
@Override
59+
public Timer.Interface newTimer() {
60+
return new Timer.Interface() {
61+
private final Stopwatch m_stopwatch = Stopwatch.createUnstarted();
62+
63+
@Override
64+
public double get() {
65+
return m_stopwatch.elapsed(TimeUnit.SECONDS);
66+
}
67+
68+
@Override
69+
public void reset() {
70+
m_stopwatch.reset();
71+
}
72+
73+
@Override
74+
public void start() {
75+
m_stopwatch.start();
76+
}
77+
78+
@Override
79+
public void stop() {
80+
m_stopwatch.stop();
81+
}
82+
83+
@Override
84+
public boolean hasPeriodPassed(double period) {
85+
if (get() > period) {
86+
// Advance the start time by the period.
87+
// Don't set it to the current time... we want to avoid drift.
88+
m_stopwatch.reset().start();
89+
return true;
90+
}
91+
return false;
92+
}
93+
};
94+
}
95+
});
96+
}
97+
}
98+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import org.usfirst.frc.team69.util.QuickCommand;
9+
10+
import edu.wpi.first.wpilibj.UnitTestUtility;
11+
import edu.wpi.first.wpilibj.command.Command;
12+
import edu.wpi.first.wpilibj.command.Scheduler;
13+
import edu.wpi.first.wpilibj.command.Subsystem;
14+
15+
public class TestReleaseCommand {
16+
17+
private Command runningCommand;
18+
private Command defaultCommand;
19+
private Command releaseCommand;
20+
private Subsystem subsystem;
21+
private Scheduler scheduler;
22+
23+
@BeforeClass
24+
public static void setUpBeforeClass() throws Exception {
25+
UnitTestUtility.setupMockBase();
26+
}
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
subsystem = new MySubsystem();
31+
defaultCommand = QuickCommand.continuous(subsystem, () -> {});
32+
runningCommand = QuickCommand.continuous(subsystem, () -> {});
33+
releaseCommand = QuickCommand.release(subsystem);
34+
35+
scheduler = Scheduler.getInstance();
36+
scheduler.removeAll();
37+
}
38+
39+
private class MySubsystem extends Subsystem {
40+
@Override
41+
protected void initDefaultCommand() {
42+
setDefaultCommand(defaultCommand);
43+
}
44+
}
45+
46+
@Test
47+
public void testRequiresSubsystem() {
48+
assertTrue(releaseCommand.doesRequire(subsystem));
49+
}
50+
51+
@Test
52+
public void testKillsRunningAndStartsDefault() {
53+
runningCommand.start();
54+
scheduler.run();
55+
assertTrue(runningCommand.isRunning());
56+
57+
releaseCommand.start();
58+
scheduler.run();
59+
assertTrue(releaseCommand.isRunning());
60+
assertFalse(runningCommand.isRunning());
61+
assertFalse(defaultCommand.isRunning());
62+
63+
scheduler.run();
64+
assertTrue(defaultCommand.isRunning());
65+
assertFalse(releaseCommand.isRunning());
66+
assertFalse(runningCommand.isRunning());
67+
}
68+
69+
}

0 commit comments

Comments
 (0)