Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include <cstdlib> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 6 | #include <thread> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 7 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 8 | #include <fmt/core.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include <hal/HAL.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | |
| 11 | #include "frc/DriverStation.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include "frc/livewindow/LiveWindow.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | #include "mockds/MockDS.h" |
| 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 16 | using namespace std::chrono_literals; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | class TestEnvironment : public testing::Environment { |
| 19 | bool m_alreadySetUp = false; |
| 20 | MockDS m_mockDS; |
| 21 | |
| 22 | public: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 23 | 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | m_alreadySetUp = true; |
| 30 | |
| 31 | if (!HAL_Initialize(500, 0)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | fmt::print(stderr, "FATAL ERROR: HAL could not be initialized\n"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | std::exit(-1); |
| 34 | } |
| 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 36 | m_mockDS.Start(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 38 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 42 | HAL_ObserveUserProgramStarting(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | frc::LiveWindow::SetEnabled(false); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 45 | fmt::print("Started coms\n"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | |
| 47 | int enableCounter = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 48 | while (!frc::DriverStation::IsEnabled()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | if (enableCounter > 50) { |
| 50 | // Robot did not enable properly after 5 seconds. |
| 51 | // Force exit |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | fmt::print(stderr, " Failed to enable. Aborting\n"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | std::terminate(); |
| 54 | } |
| 55 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | std::this_thread::sleep_for(100ms); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | fmt::print("Waiting for enable: {}\n", enableCounter++); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 62 | ~TestEnvironment() override { m_mockDS.Stop(); } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | testing::Environment* const environment = |
| 66 | testing::AddGlobalTestEnvironment(new TestEnvironment); |