blob: 2bfe1b3f93cacd2994cb2d0327a70d229e6929e5 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2014-2018 FIRST. 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#include <cstdlib>
9
10#include <hal/HAL.h>
11#include <wpi/raw_ostream.h>
12
13#include "frc/DriverStation.h"
14#include "frc/Timer.h"
15#include "frc/livewindow/LiveWindow.h"
16#include "gtest/gtest.h"
17#include "mockds/MockDS.h"
18
19using namespace frc;
20
21class TestEnvironment : public testing::Environment {
22 bool m_alreadySetUp = false;
23 MockDS m_mockDS;
24
25 public:
26 void SetUp() override {
27 /* Only set up once. This allows gtest_repeat to be used to
28 automatically repeat tests. */
29 if (m_alreadySetUp) return;
30 m_alreadySetUp = true;
31
32 if (!HAL_Initialize(500, 0)) {
33 wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
34 std::exit(-1);
35 }
36
37 m_mockDS.start();
38
39 /* This sets up the network communications library to enable the driver
40 station. After starting network coms, it will loop until the driver
41 station returns that the robot is enabled, to ensure that tests
42 will be able to run on the hardware. */
43 HAL_ObserveUserProgramStarting();
44 LiveWindow::GetInstance()->SetEnabled(false);
45
46 wpi::outs() << "Started coms\n";
47
48 int enableCounter = 0;
49 while (!DriverStation::GetInstance().IsEnabled()) {
50 if (enableCounter > 50) {
51 // Robot did not enable properly after 5 seconds.
52 // Force exit
53 wpi::errs() << " Failed to enable. Aborting\n";
54 std::terminate();
55 }
56
57 Wait(0.1);
58
59 wpi::outs() << "Waiting for enable: " << enableCounter++ << "\n";
60 }
61 }
62
63 void TearDown() override { m_mockDS.stop(); }
64};
65
66testing::Environment* const environment =
67 testing::AddGlobalTestEnvironment(new TestEnvironment);