Fix main function initialization (#1176)

I don't have a good way to ensure this always works, so this is going to be a documentation issue.
But initializeHardwareConfiguration is now reentrant, so we can just have all tests call it.
This commit is contained in:
Thad House
2018-07-08 15:41:31 -07:00
committed by Peter Johnson
parent f5b1028b5a
commit 89d15f061b
27 changed files with 86 additions and 729 deletions

View File

@@ -7,16 +7,12 @@
package edu.wpi.first.wpilibj;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.util.BaseSystemNotInitializedException;
import edu.wpi.first.wpilibj.sim.DriverStationSim;
public final class MockHardwareExtension implements BeforeAllCallback {
private static ExtensionContext getRoot(ExtensionContext context) {
@@ -33,72 +29,12 @@ public final class MockHardwareExtension implements BeforeAllCallback {
private void initializeHardware() {
HAL.initialize(500, 0);
try {
// Check to see if this has been setup
Timer.getFPGATimestamp();
} catch (BaseSystemNotInitializedException ex) {
// If it hasn't been then do this setup
DriverStationSim dsSim = new DriverStationSim();
dsSim.setDsAttached(true);
dsSim.setAutonomous(false);
dsSim.setEnabled(true);
dsSim.setTest(true);
HLUsageReporting.SetImplementation(new HLUsageReporting.Null());
RobotState.SetImplementation(new MockRobotStateInterface());
Timer.SetImplementation(new Timer.StaticInterface() {
@Override
public double getFPGATimestamp() {
return System.currentTimeMillis() / 1000.0;
}
@Override
public double getMatchTime() {
return 0;
}
@Override
public void delay(double seconds) {
try {
Thread.sleep((long) (seconds * 1e3));
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@Override
public Timer.Interface newTimer() {
return new Timer.Interface() {
private final Stopwatch m_stopwatch = Stopwatch.createUnstarted();
@Override
public double get() {
return m_stopwatch.elapsed(TimeUnit.SECONDS);
}
@Override
public void reset() {
m_stopwatch.reset();
}
@Override
public void start() {
m_stopwatch.start();
}
@Override
public void stop() {
m_stopwatch.stop();
}
@Override
public boolean hasPeriodPassed(double period) {
if (get() > period) {
// Advance the start time by the period.
// Don't set it to the current time... we want to avoid drift.
m_stopwatch.reset().start();
return true;
}
return false;
}
};
}
});
}
}
}

View File

@@ -1,38 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-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;
/**
* A concrete implementation of the robot state interface that can be used in UnitTests.
*/
public class MockRobotStateInterface implements RobotState.Interface {
@Override
public boolean isDisabled() {
return false;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean isOperatorControl() {
return false;
}
@Override
public boolean isAutonomous() {
return false;
}
@Override
public boolean isTest() {
return true;
}
}