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.
|
2020-11-13 14:11:10 -05:00
|
|
|
|
2020-11-14 15:00:56 -05:00
|
|
|
#include <string>
|
2020-11-13 14:11:10 -05:00
|
|
|
#include <tuple>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/driverstation/Joystick.hpp"
|
2026-04-18 19:56:45 -07:00
|
|
|
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/simulation/DriverStationSim.hpp"
|
|
|
|
|
#include "wpi/simulation/SimHooks.hpp"
|
2020-11-13 14:11:10 -05:00
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
class IsJoystickConnectedParametersTest
|
2020-11-13 14:11:10 -05:00
|
|
|
: public ::testing::TestWithParam<std::tuple<int, int, int, bool>> {};
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST_P(IsJoystickConnectedParametersTest, IsJoystickConnected) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::DriverStationSim::SetJoystickAxesMaximumIndex(
|
2025-10-25 23:03:50 -07:00
|
|
|
1, std::get<0>(GetParam()));
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::DriverStationSim::SetJoystickButtonsMaximumIndex(
|
2025-10-25 23:03:50 -07:00
|
|
|
1, std::get<1>(GetParam()));
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::DriverStationSim::SetJoystickPOVsMaximumIndex(
|
2025-10-25 23:03:50 -07:00
|
|
|
1, std::get<2>(GetParam()));
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::DriverStationSim::NotifyNewData();
|
2020-11-13 14:11:10 -05:00
|
|
|
|
|
|
|
|
ASSERT_EQ(std::get<3>(GetParam()),
|
2026-04-18 19:56:45 -07:00
|
|
|
wpi::internal::DriverStationBackend::IsJoystickConnected(1));
|
2020-11-13 14:11:10 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
INSTANTIATE_TEST_SUITE_P(IsConnectedTests, IsJoystickConnectedParametersTest,
|
2020-11-13 14:11:10 -05:00
|
|
|
::testing::Values(std::make_tuple(0, 0, 0, false),
|
|
|
|
|
std::make_tuple(1, 0, 0, true),
|
|
|
|
|
std::make_tuple(0, 1, 0, true),
|
|
|
|
|
std::make_tuple(0, 0, 1, true),
|
|
|
|
|
std::make_tuple(1, 1, 1, true),
|
|
|
|
|
std::make_tuple(4, 10, 1, true)));
|
2021-09-21 06:12:50 -07:00
|
|
|
class JoystickConnectionWarningTest
|
2020-11-14 15:00:56 -05:00
|
|
|
: public ::testing::TestWithParam<
|
|
|
|
|
std::tuple<bool, bool, bool, std::string>> {};
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST_P(JoystickConnectionWarningTest, JoystickConnectionWarnings) {
|
2020-11-14 15:00:56 -05:00
|
|
|
// Capture all output to stderr.
|
|
|
|
|
::testing::internal::CaptureStderr();
|
|
|
|
|
|
|
|
|
|
// Set FMS and Silence settings
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::DriverStationSim::SetFmsAttached(std::get<0>(GetParam()));
|
|
|
|
|
wpi::sim::DriverStationSim::NotifyNewData();
|
2026-04-18 19:56:45 -07:00
|
|
|
wpi::internal::DriverStationBackend::SilenceJoystickConnectionWarning(
|
|
|
|
|
std::get<1>(GetParam()));
|
2020-11-14 15:00:56 -05:00
|
|
|
|
|
|
|
|
// Create joystick and attempt to retrieve button.
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::Joystick joystick(0);
|
2020-11-14 15:00:56 -05:00
|
|
|
joystick.GetRawButton(1);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::sim::StepTiming(1_s);
|
2026-04-18 19:56:45 -07:00
|
|
|
EXPECT_EQ(wpi::internal::DriverStationBackend::
|
|
|
|
|
IsJoystickConnectionWarningSilenced(),
|
2021-06-15 23:06:03 -07:00
|
|
|
std::get<2>(GetParam()));
|
2021-05-24 23:36:26 -07:00
|
|
|
EXPECT_EQ(::testing::internal::GetCapturedStderr().substr(
|
|
|
|
|
0, std::get<3>(GetParam()).size()),
|
|
|
|
|
std::get<3>(GetParam()));
|
2020-11-14 15:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2021-09-21 06:12:50 -07:00
|
|
|
DriverStationTests, JoystickConnectionWarningTest,
|
2021-05-24 23:36:26 -07:00
|
|
|
::testing::Values(
|
|
|
|
|
std::make_tuple(false, true, true, ""),
|
2026-02-17 23:22:02 -05:00
|
|
|
std::make_tuple(false, false, false,
|
|
|
|
|
"Warning: Joystick on port 0 not available, check if "
|
|
|
|
|
"all controllers are plugged in\n"),
|
|
|
|
|
std::make_tuple(true, true, false,
|
|
|
|
|
"Warning: Joystick on port 0 not available, check if "
|
|
|
|
|
"all controllers are plugged in\n"),
|
|
|
|
|
std::make_tuple(true, false, false,
|
|
|
|
|
"Warning: Joystick on port 0 not available, check if "
|
|
|
|
|
"all controllers are plugged in\n")));
|