diff --git a/fake-gtest/src/setup-googletest.h b/fake-gtest/src/setup-googletest.h index 3ee6624a..69eaa5c3 100644 --- a/fake-gtest/src/setup-googletest.h +++ b/fake-gtest/src/setup-googletest.h @@ -22,8 +22,10 @@ #undef max #undef T #undef U +#undef TEST #include "gtest/gtest.h" +#include "gmock/gmock.h" #define SETUP_GOOGLETEST() \ void executeTestFunction() { \ diff --git a/src/kaleidoscope/sim/Sim.cpp b/src/kaleidoscope/sim/Sim.cpp index 8e78aa51..158a7425 100644 --- a/src/kaleidoscope/sim/Sim.cpp +++ b/src/kaleidoscope/sim/Sim.cpp @@ -15,9 +15,24 @@ */ #include "kaleidoscope/sim/Sim.h" +#include "Kaleidoscope.h" namespace kaleidoscope { namespace sim { +void Sim::RunCycle() { + Kaleidoscope.loop(); +} + +void Sim::RunCycles(size_t n) { + for (size_t i = 0; i < n; ++i) RunCycle(); +} + +void Sim::Press(uint8_t row, uint8_t, col) { + Kaleidoscope.device().keyScanner().setKeystate( + KeyAddr{row, col}, + Kaleidoscope::Device::Props::keyScanner::KeyState::Pressed); +} + } // namespace sim } // namespace kaleidoscope diff --git a/src/kaleidoscope/sim/Sim.h b/src/kaleidoscope/sim/Sim.h index 74cd31b0..a16cf42c 100644 --- a/src/kaleidoscope/sim/Sim.h +++ b/src/kaleidoscope/sim/Sim.h @@ -23,7 +23,8 @@ namespace sim { class Sim { public: - + void RunCycle(); + void Press(uint8_t row, uint8_t col); }; } // namespace sim diff --git a/testing/common/observer/Observer.h b/src/kaleidoscope/testing/observer/Observer.h similarity index 68% rename from testing/common/observer/Observer.h rename to src/kaleidoscope/testing/observer/Observer.h index 3d67f0eb..cc389a82 100644 --- a/testing/common/observer/Observer.h +++ b/src/kaleidoscope/testing/observer/Observer.h @@ -18,15 +18,31 @@ #pragma once -#include "./Logging.h" +//#include "./Logging.h" +#include +#define LOG(x) std::cout #include "HID-Settings.h" +#include "HIDReportObserver.h" +#include "kaleidoscope/testing/reports/KeyboardReport.h" namespace kaleidoscope { namespace testing { class Observer { public: + Observer() { + HIDReportObserver::resetHook( + std::bind(&Observer::ProcessHidReport, this)); + } + + void Clear() { + mouse_reports_.clear(); + boot_keyboard_reports_.clear(); + absolute_mouse_reports_.clear(); + keyboard_reports_.clear(); + } + void ProcessHidReport(uint8_t id, const void* data, int len, int result) { switch (id) { case HID_REPORTID_MOUSE: { @@ -57,34 +73,32 @@ class Observer { } } - template - void RecordReport(const R& report); - - template <> - void RecordReport(const MouseReport& report) { - mouse_reports_.push_back(report); + const std::vector& KeyboardReports() const { + return keyboard_reports_; } - template <> - void RecordReport(const BootKeyboardReport& report) { - boot_keyboard_reports_.push_back(report); + const KeyboardReport& KeyboardReports(size_t i) const { + return keyboard_reports_.at(i); } - template <> - void RecordReport(const AbsoluteMouseReport& report) { - absolute_mouse_reports_.push_back(report); + private: + template + void ProcessReport(const R& report) { + observer_.RecordReport(report); } - template <> - void RecordReport(const KeyboardReport& report) { - keyboard_reports_.push_back(report); - } + template + void RecordReport(const R& report); - std::vector mouse_reports_; - std::vector boot_keyboard_reports_; - std::vector absolute_mouse_reports_; std::vector keyboard_reports_; }; +template <> +void Observer::RecordReport(const KeyboardReport& report) { + keyboard_reports_.push_back(report); +} + } // namespace testing } // namespace kaleidoscope + +#endif // KALEIDOSCOPE_VIRTUAL_BUILD diff --git a/src/kaleidoscope/testing/reports/KeyboardReport.cpp b/src/kaleidoscope/testing/reports/KeyboardReport.cpp new file mode 100644 index 00000000..5781e404 --- /dev/null +++ b/src/kaleidoscope/testing/reports/KeyboardReport.cpp @@ -0,0 +1,43 @@ +/* -*- 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 . + */ + +#ifdef KALEIDOSCOPE_VIRTUAL_BUILD + +#include + +namespace kaleidoscope { +namespace testing { + +KeyboardReport::KeyboardReport(const void* data) { + const ReportData& report_data = + *static_cast(data); + memcpy(&report_data_, &report_data, sizeof(report_data_)); +} + +std::vector KeyboardReport::ActiveKeycodes() const { + std::vector active_keys; + for (uint8_t i = 0; i <= HID_LAST_KEY; ++k) { + uint8_t bit = 1 << (uint8_t(k) % 8); + uint8_t key_code = report_data_.keys[k/8] & bit; + if (key_code) active_keys.push_back(i); + } + return active_keys; +} + +} // namespace testing +} // namespace kaleidoscope + +#endif // KALEIDOSCOPE_VIRTUAL_BUILD diff --git a/testing/common/reports/KeyboardReport.h b/src/kaleidoscope/testing/reports/KeyboardReport.h similarity index 64% rename from testing/common/reports/KeyboardReport.h rename to src/kaleidoscope/testing/reports/KeyboardReport.h index e67f99b1..3341c5a3 100644 --- a/testing/common/reports/KeyboardReport.h +++ b/src/kaleidoscope/testing/reports/KeyboardReport.h @@ -18,4 +18,26 @@ #pragma once +#include "MultiReport/Keyboard.h" + +namespace kaleidoscope { +namespace testing { + +class KeyboardReport { + public: + typedef HID_KeyboardReport_Data_t ReportData; + + static constexpr uint8_t kHidReportType = HID_REPORTID_NKRO_KEYBOARD; + + KeyboardReport(const void* data); + + std::vector ActiveKeycodes() const; + + private: + ReportData report_data_; +}; + +} // namespace testing +} // namespace kaleidoscope + #endif // KALEIDOSCOPE_VIRTUAL_BUILD diff --git a/testing/common/VirtualDeviceTest.h b/testing/common/VirtualDeviceTest.h index 45453c77..f92f9449 100644 --- a/testing/common/VirtualDeviceTest.h +++ b/testing/common/VirtualDeviceTest.h @@ -18,58 +18,32 @@ #pragma once -#include "./Logging.h" +#include "setup-googletest.h" #include "HID-Settings.h" +#include "kaleidoscope/sim/Sim.h" +#include "kaleidoscope/testing/observer/Observer.h" namespace kaleidoscope { namespace testing { class VirtualDeviceTest : public ::testing::Test { protected: - void SetUp() { - HIDReportObserver + void RunCycle() { + observer_.Clear(); + sim_.RunCycle(); } - - void Press(uint8_t row, uint8_t, col) { - Kaleidoscope.device().keyScanner().setKeystate( - KeyAddr{row, col}, - Kaleidoscope::Device::Props::keyScanner::KeyState::Pressed); - } - - private: - template - void ProcessReport(const R& report) { - observer_RecordReport(report); + void RunCycles(size_t n) { + if (n == 0) return; + observer_.Clear(); + sim_.RunCycles(n); } + Observer& Result() { return observer_; } - template - void RecordReport(const R& report); + Sim sim_; - template <> - void RecordReport(const MouseReport& report) { - mouse_reports_.push_back(report); - } - - template <> - void RecordReport(const BootKeyboardReport& report) { - boot_keyboard_reports_.push_back(report); - } - - template <> - void RecordReport(const AbsoluteMouseReport& report) { - absolute_mouse_reports_.push_back(report); - } - - template <> - void RecordReport(const KeyboardReport& report) { - keyboard_reports_.push_back(report); - } - - std::vector mouse_reports_; - std::vector boot_keyboard_reports_; - std::vector absolute_mouse_reports_; - std::vector keyboard_reports_; + private: + Observer observer_; }; } // namespace testing diff --git a/testing/kaleidoscope/kaleidoscope_test.h b/testing/common/matchers.h similarity index 83% rename from testing/kaleidoscope/kaleidoscope_test.h rename to testing/common/matchers.h index 0c63f401..179fbf33 100644 --- a/testing/kaleidoscope/kaleidoscope_test.h +++ b/testing/common/matchers.h @@ -20,19 +20,11 @@ #include "setup-googletest.h" -SETUP_GOOGLETEST(); - namespace kaleidoscope { namespace testing { -namespace { - -class KeyboardReports : public VirtualDeviceTest {}; -TEST_F(KeyboardReports, KeysActiveWhenPressed) { - Press(2, 1); // A -} +auto ContainsKey(Key key) { return ::testing::Contains(key.getKeyCode()); } -} // namespace } // namespace testing } // namespace kaleidoscope diff --git a/testing/kaleidoscope/tests.h b/testing/kaleidoscope/tests.h index fb86bc07..b928b68d 100644 --- a/testing/kaleidoscope/tests.h +++ b/testing/kaleidoscope/tests.h @@ -1 +1,56 @@ -#include "./kaleidoscope_test.h" +/* -*- 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 . + */ + +#ifdef KALEIDOSCOPE_VIRTUAL_BUILD + +#pragma once + +#include "setup-googletest.h" + +#include "kaleidoscope/key_defs_keyboard.h" +#include "../common/matchers.h" +#include "../common/VirtualDeviceTest.h" + +SETUP_GOOGLETEST(); + +namespace kaleidoscope { +namespace testing { +namespace { + +using ::testing::IsEmpty; + +class KeyboardReports : public VirtualDeviceTest {}; + +TEST_F(KeyboardReports, KeysActiveWhenPressed) { + sim_.Press(2, 1); // A + RunCycle(); + + EXPECT_EQ(Result().KeyboardReports().size(), 1); + EXPECT_THAT( + Result().KeyboardReports(0).ActiveKeyCodes(), + ContainsKey(Key_A)); + + sim_.Release(2, 1); // A + RunCycles(2); + + EXPECT_THAT(Result().KeyboardReports().size(), IsEmpty()); +} + +} // namespace +} // namespace testing +} // namespace kaleidoscope + +#endif // KALEIDOSCOPE_VIRTUAL_BUILD