blob: 91f27b91a8f4ddd25cd01ceb9714e8db28370d60 [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.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include <cstdlib>
Austin Schuh812d0d12021-11-04 20:16:48 -07006#include <thread>
Brian Silverman8fce7482020-01-05 13:18:21 -08007
Austin Schuh812d0d12021-11-04 20:16:48 -07008#include <fmt/core.h>
James Kuszmaulb13e13f2023-11-22 20:44:04 -08009#include <gtest/gtest.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080010#include <hal/HAL.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080011
12#include "frc/DriverStation.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include "frc/livewindow/LiveWindow.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014#include "mockds/MockDS.h"
15
Austin Schuh812d0d12021-11-04 20:16:48 -070016using namespace std::chrono_literals;
Brian Silverman8fce7482020-01-05 13:18:21 -080017
18class TestEnvironment : public testing::Environment {
19 bool m_alreadySetUp = false;
20 MockDS m_mockDS;
21
22 public:
Austin Schuh812d0d12021-11-04 20:16:48 -070023 TestEnvironment() {
24 // Only set up once. This allows gtest_repeat to be used to automatically
25 // repeat tests.
26 if (m_alreadySetUp) {
27 return;
28 }
Brian Silverman8fce7482020-01-05 13:18:21 -080029 m_alreadySetUp = true;
30
31 if (!HAL_Initialize(500, 0)) {
Austin Schuh812d0d12021-11-04 20:16:48 -070032 fmt::print(stderr, "FATAL ERROR: HAL could not be initialized\n");
Brian Silverman8fce7482020-01-05 13:18:21 -080033 std::exit(-1);
34 }
35
Austin Schuh812d0d12021-11-04 20:16:48 -070036 m_mockDS.Start();
Brian Silverman8fce7482020-01-05 13:18:21 -080037
Austin Schuh812d0d12021-11-04 20:16:48 -070038 // This sets up the network communications library to enable the driver
39 // station. After starting network coms, it will loop until the driver
40 // station returns that the robot is enabled, to ensure that tests will be
41 // able to run on the hardware.
Brian Silverman8fce7482020-01-05 13:18:21 -080042 HAL_ObserveUserProgramStarting();
Austin Schuh812d0d12021-11-04 20:16:48 -070043 frc::LiveWindow::SetEnabled(false);
Brian Silverman8fce7482020-01-05 13:18:21 -080044
Austin Schuh812d0d12021-11-04 20:16:48 -070045 fmt::print("Started coms\n");
Brian Silverman8fce7482020-01-05 13:18:21 -080046
47 int enableCounter = 0;
James Kuszmaulcf324122023-01-14 14:07:17 -080048 frc::DriverStation::RefreshData();
Austin Schuh812d0d12021-11-04 20:16:48 -070049 while (!frc::DriverStation::IsEnabled()) {
Brian Silverman8fce7482020-01-05 13:18:21 -080050 if (enableCounter > 50) {
51 // Robot did not enable properly after 5 seconds.
52 // Force exit
Austin Schuh812d0d12021-11-04 20:16:48 -070053 fmt::print(stderr, " Failed to enable. Aborting\n");
Brian Silverman8fce7482020-01-05 13:18:21 -080054 std::terminate();
55 }
56
Austin Schuh812d0d12021-11-04 20:16:48 -070057 std::this_thread::sleep_for(100ms);
Brian Silverman8fce7482020-01-05 13:18:21 -080058
Austin Schuh812d0d12021-11-04 20:16:48 -070059 fmt::print("Waiting for enable: {}\n", enableCounter++);
James Kuszmaulcf324122023-01-14 14:07:17 -080060 frc::DriverStation::RefreshData();
Brian Silverman8fce7482020-01-05 13:18:21 -080061 }
62 }
63
Austin Schuh812d0d12021-11-04 20:16:48 -070064 ~TestEnvironment() override { m_mockDS.Stop(); }
Brian Silverman8fce7482020-01-05 13:18:21 -080065};
66
67testing::Environment* const environment =
68 testing::AddGlobalTestEnvironment(new TestEnvironment);