blob: ee8c2b1b2469ba3fcac905c2cdcf356dafe30977 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include <cstdlib>
6#include <thread>
7
8#include <fmt/core.h>
9#include <hal/HAL.h>
10
11#include "gtest/gtest.h"
12#include "mockds/MockDS.h"
13
14using namespace std::chrono_literals;
15
16class TestEnvironment : public testing::Environment {
17 bool m_alreadySetUp = false;
18 MockDS m_mockDS;
19
20 public:
21 TestEnvironment() {
22 // Only set up once. This allows gtest_repeat to be used to automatically
23 // repeat tests.
24 if (m_alreadySetUp) {
25 return;
26 }
27 m_alreadySetUp = true;
28
29 if (!HAL_Initialize(500, 0)) {
30 fmt::print(stderr, "FATAL ERROR: HAL could not be initialized\n");
31 std::exit(-1);
32 }
33
34 m_mockDS.Start();
35
36 // This sets up the network communications library to enable the driver
37 // station. After starting network coms, it will loop until the driver
38 // station returns that the robot is enabled, to ensure that tests will be
39 // able to run on the hardware.
40 HAL_ObserveUserProgramStarting();
41
42 fmt::print("Started coms\n");
43
44 int enableCounter = 0;
45
46 auto checkEnabled = []() {
47 HAL_ControlWord controlWord;
48 std::memset(&controlWord, 0, sizeof(controlWord));
49 HAL_GetControlWord(&controlWord);
50 return controlWord.enabled && controlWord.dsAttached;
51 };
52 while (!checkEnabled()) {
53 if (enableCounter > 50) {
54 // Robot did not enable properly after 5 seconds.
55 // Force exit
56 fmt::print(stderr, " Failed to enable. Aborting\n");
57 std::terminate();
58 }
59
60 std::this_thread::sleep_for(100ms);
61
62 fmt::print("Waiting for enable: {}\n", enableCounter++);
63 }
64 std::this_thread::sleep_for(500ms);
65 }
66
67 ~TestEnvironment() override { m_mockDS.Stop(); }
68};
69
70testing::Environment* const environment =
71 testing::AddGlobalTestEnvironment(new TestEnvironment);