From a89c95cc580807267909e148e37c61b1abcf644c Mon Sep 17 00:00:00 2001 From: Eric Paniagua Date: Fri, 4 Sep 2020 19:03:12 -0700 Subject: [PATCH] Make RunCycle return std::unique_ptr. Signed-off-by: Eric Paniagua --- testing/common/State.cpp | 14 ++++++++++++-- testing/common/State.h | 10 ++++++++-- testing/common/VirtualDeviceTest.cpp | 18 ++++++------------ testing/common/VirtualDeviceTest.h | 8 ++++---- testing/kaleidoscope/test/simulator_test.cpp | 16 ++++++++++------ 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/testing/common/State.cpp b/testing/common/State.cpp index f4cbe69d..4f0cca39 100644 --- a/testing/common/State.cpp +++ b/testing/common/State.cpp @@ -32,8 +32,13 @@ State::State() { }); } -void State::Clear() { - keyboard_reports_.clear(); +State::~State() { + HIDReportObserver::resetHook(&State::DefaultProcessHidReport); +} + +// static +void State::DefaultProcessHidReport(uint8_t, const void*, int, int) { + // TODO: Log that the report was dropped. } void State::ProcessHidReport( @@ -79,5 +84,10 @@ void State::ProcessKeyboardReport(const KeyboardReport& report) { keyboard_reports_.push_back(report); } +void State::Snapshot() { + // TODO: Grab a copy of current instantaneous state, like: + // key states, layer stack, led states +} + } // namespace testing } // namespace kaleidoscope diff --git a/testing/common/State.h b/testing/common/State.h index 2581f044..18eb9203 100644 --- a/testing/common/State.h +++ b/testing/common/State.h @@ -29,9 +29,9 @@ namespace testing { class State { public: - State(); + ~State(); - void Clear(); + static void DefaultProcessHidReport(uint8_t, const void*, int, int); void ProcessHidReport(uint8_t id, const void* data, int len, int result); @@ -39,8 +39,14 @@ class State { const KeyboardReport& KeyboardReports(size_t i) const; private: + friend class VirtualDeviceTest; + + State(); + void ProcessKeyboardReport(const KeyboardReport& report); + void Snapshot(); + std::vector keyboard_reports_; }; diff --git a/testing/common/VirtualDeviceTest.cpp b/testing/common/VirtualDeviceTest.cpp index 267ebf7d..5fe82afc 100644 --- a/testing/common/VirtualDeviceTest.cpp +++ b/testing/common/VirtualDeviceTest.cpp @@ -16,22 +16,16 @@ #include "testing/common/VirtualDeviceTest.h" +#include + namespace kaleidoscope { namespace testing { -void VirtualDeviceTest::RunCycle() { - state_.Clear(); +std::unique_ptr VirtualDeviceTest::RunCycle() { + auto state = std::unique_ptr(new State()); sim_.RunCycle(); -} - -void VirtualDeviceTest::RunCycles(size_t n) { - if (n == 0) return; - state_.Clear(); - sim_.RunCycles(n); -} - -const State& VirtualDeviceTest::Result() const { - return state_; + state->Snapshot(); + return state; } } // namespace testing diff --git a/testing/common/VirtualDeviceTest.h b/testing/common/VirtualDeviceTest.h index 6c15eace..747e00c5 100644 --- a/testing/common/VirtualDeviceTest.h +++ b/testing/common/VirtualDeviceTest.h @@ -31,11 +31,11 @@ namespace testing { class VirtualDeviceTest : public ::testing::Test { protected: - void RunCycle(); - void RunCycles(size_t n); - - const State& Result() const; + std::unique_ptr RunCycle(); + // DO NOT REMOVE: This namespace qualification is required. Otherwise the + // linker decides this symbol is really ::kaleidoscope::simulator::Simulator. + // No idea why. ::kaleidoscope::testing::Simulator sim_; private: diff --git a/testing/kaleidoscope/test/simulator_test.cpp b/testing/kaleidoscope/test/simulator_test.cpp index ad7775e8..f8f13171 100644 --- a/testing/kaleidoscope/test/simulator_test.cpp +++ b/testing/kaleidoscope/test/simulator_test.cpp @@ -39,20 +39,24 @@ class KeyboardReports : public VirtualDeviceTest {}; TEST_F(KeyboardReports, KeysActiveWhenPressed) { sim_.Press(2, 1); // A - RunCycle(); + auto state = RunCycle(); - EXPECT_EQ(Result().KeyboardReports().size(), 1); + ASSERT_EQ(state->KeyboardReports().size(), 1); EXPECT_THAT( - Result().KeyboardReports(0).ActiveKeycodes(), + state->KeyboardReports(0).ActiveKeycodes(), ContainsKey(Key_A)); sim_.Release(2, 1); // A - RunCycles(2); + state = RunCycle(); - EXPECT_EQ(Result().KeyboardReports().size(), 1); + ASSERT_EQ(state->KeyboardReports().size(), 1); EXPECT_THAT( - Result().KeyboardReports(0).ActiveKeycodes(), + state->KeyboardReports(0).ActiveKeycodes(), IsEmpty()); + + state = RunCycle(); // 2 cycles later + + EXPECT_EQ(state->KeyboardReports().size(), 0); } } // namespace