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.
|
2014-06-07 17:37:51 -04:00
|
|
|
|
2016-09-21 23:48:54 -07:00
|
|
|
#include <cstdlib>
|
2021-05-31 10:21:34 -07:00
|
|
|
#include <thread>
|
2016-09-21 23:48:54 -07:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
#include <fmt/core.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
|
|
|
|
#include "frc/livewindow/LiveWindow.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
#include "gtest/gtest.h"
|
2017-11-28 19:12:05 -08:00
|
|
|
#include "mockds/MockDS.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
using namespace std::chrono_literals;
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
class TestEnvironment : public testing::Environment {
|
2015-06-24 01:06:29 -07:00
|
|
|
bool m_alreadySetUp = false;
|
2017-11-28 19:12:05 -08:00
|
|
|
MockDS m_mockDS;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
public:
|
2021-05-31 10:21:34 -07:00
|
|
|
TestEnvironment() {
|
|
|
|
|
// Only set up once. This allows gtest_repeat to be used to automatically
|
|
|
|
|
// repeat tests.
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_alreadySetUp) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
m_alreadySetUp = true;
|
|
|
|
|
|
2017-07-01 00:43:06 -07:00
|
|
|
if (!HAL_Initialize(500, 0)) {
|
2021-05-31 10:21:34 -07:00
|
|
|
fmt::print(stderr, "FATAL ERROR: HAL could not be initialized\n");
|
2016-09-21 23:48:54 -07:00
|
|
|
std::exit(-1);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
m_mockDS.Start();
|
2017-11-28 19:12:05 -08:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// This sets up the network communications library to enable the driver
|
|
|
|
|
// station. After starting network coms, it will loop until the driver
|
|
|
|
|
// station returns that the robot is enabled, to ensure that tests will be
|
|
|
|
|
// able to run on the hardware.
|
2016-07-10 16:24:57 -07:00
|
|
|
HAL_ObserveUserProgramStarting();
|
2021-05-31 10:21:34 -07:00
|
|
|
frc::LiveWindow::GetInstance()->SetEnabled(false);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
fmt::print("Started coms\n");
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-12-04 20:24:26 -08:00
|
|
|
int enableCounter = 0;
|
2021-05-31 10:21:34 -07:00
|
|
|
while (!frc::DriverStation::GetInstance().IsEnabled()) {
|
2017-12-04 20:24:26 -08:00
|
|
|
if (enableCounter > 50) {
|
|
|
|
|
// Robot did not enable properly after 5 seconds.
|
|
|
|
|
// Force exit
|
2021-05-31 10:21:34 -07:00
|
|
|
fmt::print(stderr, " Failed to enable. Aborting\n");
|
2017-12-04 20:24:26 -08:00
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2017-12-04 20:24:26 -08:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
fmt::print("Waiting for enable: {}\n", enableCounter++);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
~TestEnvironment() { m_mockDS.Stop(); }
|
2014-06-07 17:37:51 -04:00
|
|
|
};
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
testing::Environment* const environment =
|
2015-06-25 15:07:55 -04:00
|
|
|
testing::AddGlobalTestEnvironment(new TestEnvironment);
|