diff --git a/testing/common/HIDState.cpp b/testing/common/HIDState.cpp new file mode 100644 index 00000000..dec0d88a --- /dev/null +++ b/testing/common/HIDState.cpp @@ -0,0 +1,119 @@ +/* -*- mode: c++ -*- + * Copyright (C) 2020 Eric Paniagua (epaniagua@google.com) + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "testing/common/HIDState.h" + +#include "HID-Settings.h" + +#include "testing/common/fix-macros.h" + +// TODO(epan): Add proper logging. +#include +#define LOG(x) std::cout + +namespace kaleidoscope { +namespace testing { + +const std::vector& HIDState::Keyboard() const { + return keyboard_reports_; +} + +const KeyboardReport& HIDState::Keyboard(size_t i) const { + return keyboard_reports_.at(i); +} + +const std::vector& HIDState::SystemControl() const { + return system_control_reports_; +} + +const SystemControlReport& HIDState::SystemControl(size_t i) const { + return system_control_reports_.at(i); +} + +namespace internal { + +// static +void HIDStateBuilder::ProcessHidReport( + uint8_t id, const void* data, int len, int result) { + switch (id) { + case HID_REPORTID_MOUSE: { + LOG(ERROR) << "Dropped MouseReport: unimplemented"; + break; + } + case HID_REPORTID_KEYBOARD: { + LOG(ERROR) << "Dropped BootKeyboardReport: unimplemented"; + break; + } + case HID_REPORTID_GAMEPAD: + case HID_REPORTID_CONSUMERCONTROL: { + // TODO: React appropriately to these. + LOG(INFO) << "Ignoring HID report with id = " << id; + break; + } + case HID_REPORTID_SYSTEMCONTROL: { + ProcessSystemControlReport(SystemControlReport{data}); + break; + } + case HID_REPORTID_MOUSE_ABSOLUTE: { + LOG(ERROR) << "Dropped AbsoluteMouseReport: unimplemented"; + break; + } + case HID_REPORTID_NKRO_KEYBOARD: { + ProcessKeyboardReport(KeyboardReport{data}); + break; + } + default: + LOG(ERROR) << "Encountered unknown HID report with id = " << id; + } +} + +// static +std::unique_ptr HIDStateBuilder::Snapshot() { + auto hid_state = std::make_unique(); + // Populate state. + // TODO: Grab a copy of current instantaneous state, like: + // key states, layer stack, led states + hid_state->keyboard_reports_ = std::move(keyboard_reports_); + hid_state->system_control_reports_ = std::move(system_control_reports_); + + Clear(); // Clear global state. + return hid_state; +} + +// static +void HIDStateBuilder::Clear() { + keyboard_reports_.clear(); + system_control_reports_.clear(); +} + +// static +void HIDStateBuilder::ProcessKeyboardReport(const KeyboardReport& report) { + keyboard_reports_.push_back(report); +} + +// static +void HIDStateBuilder::ProcessSystemControlReport(const SystemControlReport& report) { + system_control_reports_.push_back(report); +} + +// static +std::vector HIDStateBuilder::keyboard_reports_; +// static +std::vector HIDStateBuilder::system_control_reports_; + +} // namesapce internal +} // namespace testing +} // namespace kaleidoscope diff --git a/testing/common/HIDState.h b/testing/common/HIDState.h new file mode 100644 index 00000000..4ae0e193 --- /dev/null +++ b/testing/common/HIDState.h @@ -0,0 +1,65 @@ +/* -*- mode: c++ -*- + * Copyright (C) 2020 Eric Paniagua (epaniagua@google.com) + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#pragma once + +#include "testing/common/KeyboardReport.h" +#include "testing/common/SystemControlReport.h" + +// Out of order due to macro conflicts. +#include "testing/common/fix-macros.h" +#include + +namespace kaleidoscope { +namespace testing { +namespace internal { class HIDStateBuilder; } + +class HIDState { + public: + const std::vector& Keyboard() const; + const KeyboardReport& Keyboard(size_t i) const; + + const std::vector& SystemControl() const; + const SystemControlReport& SystemControl(size_t i) const; + + private: + friend class internal::HIDStateBuilder; + + std::vector keyboard_reports_; + std::vector system_control_reports_; +}; + +namespace internal { + +class HIDStateBuilder { + public: + static void ProcessHidReport( + uint8_t id, const void* data, int len, int result); + + static std::unique_ptr Snapshot(); + + private: + static void Clear(); + static void ProcessKeyboardReport(const KeyboardReport& report); + static void ProcessSystemControlReport(const SystemControlReport& report); + + static std::vector keyboard_reports_; + static std::vector system_control_reports_; +}; + +} // namespace internal +} // namespace testing +} // namespace kaleidoscope diff --git a/testing/common/State.cpp b/testing/common/State.cpp index ad45da29..29d58c29 100644 --- a/testing/common/State.cpp +++ b/testing/common/State.cpp @@ -16,96 +16,18 @@ #include "testing/common/State.h" -#include "testing/common/fix-macros.h" - -// TODO(epan): Add proper logging. -#include -#define LOG(x) std::cout - namespace kaleidoscope { namespace testing { -std::vector State::keyboard_reports; -std::vector State::system_control_reports; - -// static -void State::ProcessHidReport( - uint8_t id, const void* data, int len, int result) { - switch (id) { - case HID_REPORTID_MOUSE: { - LOG(ERROR) << "Dropped MouseReport: unimplemented"; - break; - } - case HID_REPORTID_KEYBOARD: { - LOG(ERROR) << "Dropped BootKeyboardReport: unimplemented"; - break; - } - case HID_REPORTID_GAMEPAD: - case HID_REPORTID_CONSUMERCONTROL: { - // TODO: React appropriately to these. - LOG(INFO) << "Ignoring HID report with id = " << id; - break; - } - case HID_REPORTID_SYSTEMCONTROL: { - ProcessSystemControlReport(SystemControlReport{data}); - break; - } - case HID_REPORTID_MOUSE_ABSOLUTE: { - LOG(ERROR) << "Dropped AbsoluteMouseReport: unimplemented"; - break; - } - case HID_REPORTID_NKRO_KEYBOARD: { - ProcessKeyboardReport(KeyboardReport{data}); - break; - } - default: - LOG(ERROR) << "Encountered unknown HID report with id = " << id; - } -} - // static std::unique_ptr State::Snapshot() { auto state = std::make_unique(); - // Populate state. - // TODO: Grab a copy of current instantaneous state, like: - // key states, layer stack, led states - state->keyboard_reports_ = std::move(keyboard_reports); - state->system_control_reports_ = std::move(system_control_reports); - - Clear(); // Clear global state. + state->hid_state_ = internal::HIDStateBuilder::Snapshot(); return state; } -const std::vector& State::KeyboardReports() const { - return keyboard_reports_; -} - -const KeyboardReport& State::KeyboardReports(size_t i) const { - return keyboard_reports_.at(i); -} - -const std::vector& State::SystemControlReports() const { - return system_control_reports_; -} - -const SystemControlReport& State::SystemControlReports(size_t i) const { - return system_control_reports_.at(i); -} - -// static -void State::Clear() { - keyboard_reports.clear(); - system_control_reports.clear(); -} - -// static -void State::ProcessKeyboardReport(const KeyboardReport& report) { - keyboard_reports.push_back(report); -} - -// static -void State::ProcessSystemControlReport(const SystemControlReport& report) { - system_control_reports.push_back(report); +const HIDState* State::HIDReports() const { + return hid_state_.get(); } } // namespace testing diff --git a/testing/common/State.h b/testing/common/State.h index 9682566d..b1b4b0d6 100644 --- a/testing/common/State.h +++ b/testing/common/State.h @@ -20,9 +20,7 @@ #include #include -#include "HID-Settings.h" -#include "testing/common/KeyboardReport.h" -#include "testing/common/SystemControlReport.h" +#include "testing/common/HIDState.h" // Out of order due to macro conflicts. #include "testing/common/fix-macros.h" @@ -33,27 +31,12 @@ namespace testing { class State { public: - static void ProcessHidReport( - uint8_t id, const void* data, int len, int result); - static std::unique_ptr Snapshot(); - const std::vector& KeyboardReports() const; - const KeyboardReport& KeyboardReports(size_t i) const; - - const std::vector& SystemControlReports() const; - const SystemControlReport& SystemControlReports(size_t i) const; + const HIDState* HIDReports() const; private: - static std::vector keyboard_reports; - static std::vector system_control_reports; - - static void Clear(); - static void ProcessKeyboardReport(const KeyboardReport& report); - static void ProcessSystemControlReport(const SystemControlReport& report); - - std::vector keyboard_reports_; - std::vector system_control_reports_; + std::unique_ptr hid_state_; }; } // namespace testing diff --git a/testing/common/VirtualDeviceTest.cpp b/testing/common/VirtualDeviceTest.cpp index 5877fee9..dcaf6696 100644 --- a/testing/common/VirtualDeviceTest.cpp +++ b/testing/common/VirtualDeviceTest.cpp @@ -14,19 +14,16 @@ * this program. If not, see . */ -// Out of order due to macro conflicts. -#include "HIDReportObserver.h" - -#include "testing/common/fix-macros.h" #include "testing/common/VirtualDeviceTest.h" -#include +#include "HIDReportObserver.h" +#include "testing/common/HIDState.h" namespace kaleidoscope { namespace testing { void VirtualDeviceTest::SetUp() { - HIDReportObserver::resetHook(&State::ProcessHidReport); + HIDReportObserver::resetHook(&internal::HIDStateBuilder::ProcessHidReport); } std::unique_ptr VirtualDeviceTest::RunCycle() { diff --git a/testing/common/VirtualDeviceTest.h b/testing/common/VirtualDeviceTest.h index 8af68813..32f1c2ec 100644 --- a/testing/common/VirtualDeviceTest.h +++ b/testing/common/VirtualDeviceTest.h @@ -36,9 +36,6 @@ class VirtualDeviceTest : public ::testing::Test { std::unique_ptr RunCycle(); SimHarness sim_; - - private: - State state_; }; } // namespace testing diff --git a/testing/issue_840/test/simulator_test.cpp b/testing/issue_840/test/simulator_test.cpp index 9e06bbb4..321c1cc1 100644 --- a/testing/issue_840/test/simulator_test.cpp +++ b/testing/issue_840/test/simulator_test.cpp @@ -41,29 +41,29 @@ TEST_F(Issue840, HasNotRegressed) { sim_.Press(2, 1); // Press System_PowerDown auto state = RunCycle(); - ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown)); + ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1); + EXPECT_THAT(state->HIDReports()->SystemControl(0), Contains(System_PowerDown)); sim_.Press(3, 5); // Press System_Sleep state = RunCycle(); - ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep)); + ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1); + EXPECT_THAT(state->HIDReports()->SystemControl(0), Contains(System_Sleep)); sim_.Release(2, 1); // Release System_PowerDown state = RunCycle(); - EXPECT_EQ(state->SystemControlReports().size(), 0); + EXPECT_EQ(state->HIDReports()->SystemControl().size(), 0); sim_.Release(3, 5); // Release System_Sleep state = RunCycle(); - ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_THAT(state->SystemControlReports(0), IsEmpty()); + ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1); + EXPECT_THAT(state->HIDReports()->SystemControl(0), IsEmpty()); state = RunCycle(); - EXPECT_EQ(state->SystemControlReports().size(), 0); + EXPECT_EQ(state->HIDReports()->SystemControl().size(), 0); } // TEST_F(Issue840, Reproduces) { diff --git a/testing/kaleidoscope/test/simulator_test.cpp b/testing/kaleidoscope/test/simulator_test.cpp index b91e009d..95e8774a 100644 --- a/testing/kaleidoscope/test/simulator_test.cpp +++ b/testing/kaleidoscope/test/simulator_test.cpp @@ -41,23 +41,23 @@ TEST_F(KeyboardReports, KeysActiveWhenPressed) { sim_.Press(2, 1); // A auto state = RunCycle(); - ASSERT_EQ(state->KeyboardReports().size(), 1); + ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); EXPECT_THAT( - state->KeyboardReports(0).ActiveKeycodes(), + state->HIDReports()->Keyboard(0).ActiveKeycodes(), Contains(Key_A)); sim_.Release(2, 1); // A state = RunCycle(); - ASSERT_EQ(state->KeyboardReports().size(), 1); + ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); EXPECT_THAT( - state->KeyboardReports(0).ActiveKeycodes(), + state->HIDReports()->Keyboard(0).ActiveKeycodes(), IsEmpty()); state = RunCycle(); // 2 cycles after releasing A - EXPECT_EQ(state->KeyboardReports().size(), 0); + EXPECT_EQ(state->HIDReports()->Keyboard().size(), 0); } } // namespace