From def72e6dde2ea97da3f1621d681d614298bda05b Mon Sep 17 00:00:00 2001 From: Eric Paniagua Date: Fri, 4 Sep 2020 20:27:38 -0700 Subject: [PATCH] Remove test dependency on change to KeyboardioHID/src/HIDReportObserver.h. Signed-off-by: Eric Paniagua --- testing/common/State.cpp | 38 ++++++++++---------- testing/common/State.h | 21 ++++++----- testing/common/VirtualDeviceTest.cpp | 12 +++++-- testing/common/VirtualDeviceTest.h | 6 ++-- testing/kaleidoscope/test/simulator_test.cpp | 3 +- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/testing/common/State.cpp b/testing/common/State.cpp index 4f0cca39..1232213e 100644 --- a/testing/common/State.cpp +++ b/testing/common/State.cpp @@ -25,22 +25,9 @@ namespace kaleidoscope { namespace testing { -State::State() { - HIDReportObserver::resetHook( - [this](uint8_t id, const void* data, int len, int result) { - this->ProcessHidReport(id, data, len, result); - }); -} - -State::~State() { - HIDReportObserver::resetHook(&State::DefaultProcessHidReport); -} +std::vector State::keyboard_reports; // static -void State::DefaultProcessHidReport(uint8_t, const void*, int, int) { - // TODO: Log that the report was dropped. -} - void State::ProcessHidReport( uint8_t id, const void* data, int len, int result) { switch (id) { @@ -72,6 +59,18 @@ void State::ProcessHidReport( } } +// 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); + + Clear(); // Clear global state. + return state; +} + const std::vector& State::KeyboardReports() const { return keyboard_reports_; } @@ -80,13 +79,14 @@ const KeyboardReport& State::KeyboardReports(size_t i) const { return keyboard_reports_.at(i); } -void State::ProcessKeyboardReport(const KeyboardReport& report) { - keyboard_reports_.push_back(report); +// static +void State::Clear() { + keyboard_reports.clear(); } -void State::Snapshot() { - // TODO: Grab a copy of current instantaneous state, like: - // key states, layer stack, led states +// static +void State::ProcessKeyboardReport(const KeyboardReport& report) { + keyboard_reports.push_back(report); } } // namespace testing diff --git a/testing/common/State.h b/testing/common/State.h index 18eb9203..d9365a82 100644 --- a/testing/common/State.h +++ b/testing/common/State.h @@ -21,31 +21,30 @@ #include #include "HID-Settings.h" -#include "HIDReportObserver.h" #include "testing/common/KeyboardReport.h" +// Out of order due to macro conflicts. +#include "testing/common/fix-macros.h" +#include + namespace kaleidoscope { namespace testing { class State { public: - ~State(); - - static void DefaultProcessHidReport(uint8_t, const void*, int, int); + static void ProcessHidReport( + uint8_t id, const void* data, int len, int result); - 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; private: - friend class VirtualDeviceTest; - - State(); - - void ProcessKeyboardReport(const KeyboardReport& report); + static std::vector keyboard_reports; - void Snapshot(); + static void Clear(); + static void ProcessKeyboardReport(const KeyboardReport& report); std::vector keyboard_reports_; }; diff --git a/testing/common/VirtualDeviceTest.cpp b/testing/common/VirtualDeviceTest.cpp index 5fe82afc..5877fee9 100644 --- a/testing/common/VirtualDeviceTest.cpp +++ b/testing/common/VirtualDeviceTest.cpp @@ -14,6 +14,10 @@ * 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 @@ -21,11 +25,13 @@ namespace kaleidoscope { namespace testing { +void VirtualDeviceTest::SetUp() { + HIDReportObserver::resetHook(&State::ProcessHidReport); +} + std::unique_ptr VirtualDeviceTest::RunCycle() { - auto state = std::unique_ptr(new State()); sim_.RunCycle(); - state->Snapshot(); - return state; + return State::Snapshot(); } } // namespace testing diff --git a/testing/common/VirtualDeviceTest.h b/testing/common/VirtualDeviceTest.h index 171ab7cb..8af68813 100644 --- a/testing/common/VirtualDeviceTest.h +++ b/testing/common/VirtualDeviceTest.h @@ -21,16 +21,18 @@ #include "testing/common/SimHarness.h" #include "testing/common/State.h" -// Out of order because `fix-macros.h` clears the preprocessor environment for -// gtest and gmock. +// Out of order due to macro conflicts. #include "testing/common/fix-macros.h" #include "gtest/gtest.h" +#include namespace kaleidoscope { namespace testing { class VirtualDeviceTest : public ::testing::Test { protected: + void SetUp(); + std::unique_ptr RunCycle(); SimHarness sim_; diff --git a/testing/kaleidoscope/test/simulator_test.cpp b/testing/kaleidoscope/test/simulator_test.cpp index f8f13171..a577382f 100644 --- a/testing/kaleidoscope/test/simulator_test.cpp +++ b/testing/kaleidoscope/test/simulator_test.cpp @@ -54,8 +54,9 @@ TEST_F(KeyboardReports, KeysActiveWhenPressed) { state->KeyboardReports(0).ActiveKeycodes(), IsEmpty()); - state = RunCycle(); // 2 cycles later + state = RunCycle(); + // 2 cycles after releasing A EXPECT_EQ(state->KeyboardReports().size(), 0); }