Signed-off-by: Eric Paniagua <epaniagua@google.com>
epan/testing/readable
Eric Paniagua 4 years ago
parent 5c8a438cc6
commit c2a8c9f1eb

@ -22,8 +22,10 @@
#undef max #undef max
#undef T #undef T
#undef U #undef U
#undef TEST
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "gmock/gmock.h"
#define SETUP_GOOGLETEST() \ #define SETUP_GOOGLETEST() \
void executeTestFunction() { \ void executeTestFunction() { \

@ -15,9 +15,24 @@
*/ */
#include "kaleidoscope/sim/Sim.h" #include "kaleidoscope/sim/Sim.h"
#include "Kaleidoscope.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace sim { 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 sim
} // namespace kaleidoscope } // namespace kaleidoscope

@ -23,7 +23,8 @@ namespace sim {
class Sim { class Sim {
public: public:
void RunCycle();
void Press(uint8_t row, uint8_t col);
}; };
} // namespace sim } // namespace sim

@ -18,15 +18,31 @@
#pragma once #pragma once
#include "./Logging.h" //#include "./Logging.h"
#include <iostream>
#define LOG(x) std::cout
#include "HID-Settings.h" #include "HID-Settings.h"
#include "HIDReportObserver.h"
#include "kaleidoscope/testing/reports/KeyboardReport.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
class Observer { class Observer {
public: 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) { void ProcessHidReport(uint8_t id, const void* data, int len, int result) {
switch (id) { switch (id) {
case HID_REPORTID_MOUSE: { case HID_REPORTID_MOUSE: {
@ -57,34 +73,32 @@ class Observer {
} }
} }
template <class R> const std::vector<KeyboardReport>& KeyboardReports() const {
void RecordReport(const R& report); return keyboard_reports_;
template <>
void RecordReport<MouseReport>(const MouseReport& report) {
mouse_reports_.push_back(report);
} }
template <> const KeyboardReport& KeyboardReports(size_t i) const {
void RecordReport<BootKeyboardReport>(const BootKeyboardReport& report) { return keyboard_reports_.at(i);
boot_keyboard_reports_.push_back(report);
} }
template <> private:
void RecordReport<AbsoluteMouseReport>(const AbsoluteMouseReport& report) { template <class R>
absolute_mouse_reports_.push_back(report); void ProcessReport(const R& report) {
observer_.RecordReport(report);
} }
template <> template <class R>
void RecordReport<KeyboardReport>(const KeyboardReport& report) { void RecordReport(const R& report);
keyboard_reports_.push_back(report);
}
std::vector<MouseReport> mouse_reports_;
std::vector<BootKeyboardReport> boot_keyboard_reports_;
std::vector<AbsoluteMouseReport> absolute_mouse_reports_;
std::vector<KeyboardReport> keyboard_reports_; std::vector<KeyboardReport> keyboard_reports_;
}; };
template <>
void Observer::RecordReport<KeyboardReport>(const KeyboardReport& report) {
keyboard_reports_.push_back(report);
}
} // namespace testing } // namespace testing
} // namespace kaleidoscope } // namespace kaleidoscope
#endif // KALEIDOSCOPE_VIRTUAL_BUILD

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifdef KALEIDOSCOPE_VIRTUAL_BUILD
#include <cstring>
namespace kaleidoscope {
namespace testing {
KeyboardReport::KeyboardReport(const void* data) {
const ReportData& report_data =
*static_cast<const ReportData*>(data);
memcpy(&report_data_, &report_data, sizeof(report_data_));
}
std::vector<uint8_t> KeyboardReport::ActiveKeycodes() const {
std::vector<uint8_t> 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

@ -18,4 +18,26 @@
#pragma once #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<uint8_t> ActiveKeycodes() const;
private:
ReportData report_data_;
};
} // namespace testing
} // namespace kaleidoscope
#endif // KALEIDOSCOPE_VIRTUAL_BUILD #endif // KALEIDOSCOPE_VIRTUAL_BUILD

@ -18,58 +18,32 @@
#pragma once #pragma once
#include "./Logging.h" #include "setup-googletest.h"
#include "HID-Settings.h" #include "HID-Settings.h"
#include "kaleidoscope/sim/Sim.h"
#include "kaleidoscope/testing/observer/Observer.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
class VirtualDeviceTest : public ::testing::Test { class VirtualDeviceTest : public ::testing::Test {
protected: protected:
void SetUp() { void RunCycle() {
HIDReportObserver observer_.Clear();
sim_.RunCycle();
} }
void RunCycles(size_t n) {
void Press(uint8_t row, uint8_t, col) { if (n == 0) return;
Kaleidoscope.device().keyScanner().setKeystate( observer_.Clear();
KeyAddr{row, col}, sim_.RunCycles(n);
Kaleidoscope::Device::Props::keyScanner::KeyState::Pressed);
}
private:
template <class R>
void ProcessReport(const R& report) {
observer_RecordReport(report);
} }
Observer& Result() { return observer_; }
template <class R> Sim sim_;
void RecordReport(const R& report);
template <> private:
void RecordReport<MouseReport>(const MouseReport& report) { Observer observer_;
mouse_reports_.push_back(report);
}
template <>
void RecordReport<BootKeyboardReport>(const BootKeyboardReport& report) {
boot_keyboard_reports_.push_back(report);
}
template <>
void RecordReport<AbsoluteMouseReport>(const AbsoluteMouseReport& report) {
absolute_mouse_reports_.push_back(report);
}
template <>
void RecordReport<KeyboardReport>(const KeyboardReport& report) {
keyboard_reports_.push_back(report);
}
std::vector<MouseReport> mouse_reports_;
std::vector<BootKeyboardReport> boot_keyboard_reports_;
std::vector<AbsoluteMouseReport> absolute_mouse_reports_;
std::vector<KeyboardReport> keyboard_reports_;
}; };
} // namespace testing } // namespace testing

@ -20,19 +20,11 @@
#include "setup-googletest.h" #include "setup-googletest.h"
SETUP_GOOGLETEST();
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
namespace {
class KeyboardReports : public VirtualDeviceTest {};
TEST_F(KeyboardReports, KeysActiveWhenPressed) { auto ContainsKey(Key key) { return ::testing::Contains(key.getKeyCode()); }
Press(2, 1); // A
}
} // namespace
} // namespace testing } // namespace testing
} // namespace kaleidoscope } // namespace kaleidoscope

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

Loading…
Cancel
Save