Add kaleidoscope::testing::HIDState.

Signed-off-by: Eric Paniagua <epaniagua@google.com>
pull/898/head
Eric Paniagua 4 years ago
parent 5cf3f65030
commit ccc8542329

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "testing/common/HIDState.h"
#include "HID-Settings.h"
#include "testing/common/fix-macros.h"
// TODO(epan): Add proper logging.
#include <iostream>
#define LOG(x) std::cout
namespace kaleidoscope {
namespace testing {
const std::vector<KeyboardReport>& HIDState::Keyboard() const {
return keyboard_reports_;
}
const KeyboardReport& HIDState::Keyboard(size_t i) const {
return keyboard_reports_.at(i);
}
const std::vector<SystemControlReport>& 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<HIDState> HIDStateBuilder::Snapshot() {
auto hid_state = std::make_unique<HIDState>();
// 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<KeyboardReport> HIDStateBuilder::keyboard_reports_;
// static
std::vector<SystemControlReport> HIDStateBuilder::system_control_reports_;
} // namesapce internal
} // namespace testing
} // namespace kaleidoscope

@ -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 <http://www.gnu.org/licenses/>.
*/
#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 <memory>
namespace kaleidoscope {
namespace testing {
namespace internal { class HIDStateBuilder; }
class HIDState {
public:
const std::vector<KeyboardReport>& Keyboard() const;
const KeyboardReport& Keyboard(size_t i) const;
const std::vector<SystemControlReport>& SystemControl() const;
const SystemControlReport& SystemControl(size_t i) const;
private:
friend class internal::HIDStateBuilder;
std::vector<KeyboardReport> keyboard_reports_;
std::vector<SystemControlReport> 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<HIDState> Snapshot();
private:
static void Clear();
static void ProcessKeyboardReport(const KeyboardReport& report);
static void ProcessSystemControlReport(const SystemControlReport& report);
static std::vector<KeyboardReport> keyboard_reports_;
static std::vector<SystemControlReport> system_control_reports_;
};
} // namespace internal
} // namespace testing
} // namespace kaleidoscope

@ -16,96 +16,18 @@
#include "testing/common/State.h" #include "testing/common/State.h"
#include "testing/common/fix-macros.h"
// TODO(epan): Add proper logging.
#include <iostream>
#define LOG(x) std::cout
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
std::vector<KeyboardReport> State::keyboard_reports;
std::vector<SystemControlReport> 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 // static
std::unique_ptr<State> State::Snapshot() { std::unique_ptr<State> State::Snapshot() {
auto state = std::make_unique<State>(); auto state = std::make_unique<State>();
// Populate state. state->hid_state_ = internal::HIDStateBuilder::Snapshot();
// 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.
return state; return state;
} }
const std::vector<KeyboardReport>& State::KeyboardReports() const { const HIDState* State::HIDReports() const {
return keyboard_reports_; return hid_state_.get();
}
const KeyboardReport& State::KeyboardReports(size_t i) const {
return keyboard_reports_.at(i);
}
const std::vector<SystemControlReport>& 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);
} }
} // namespace testing } // namespace testing

@ -20,9 +20,7 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "HID-Settings.h" #include "testing/common/HIDState.h"
#include "testing/common/KeyboardReport.h"
#include "testing/common/SystemControlReport.h"
// Out of order due to macro conflicts. // Out of order due to macro conflicts.
#include "testing/common/fix-macros.h" #include "testing/common/fix-macros.h"
@ -33,27 +31,12 @@ namespace testing {
class State { class State {
public: public:
static void ProcessHidReport(
uint8_t id, const void* data, int len, int result);
static std::unique_ptr<State> Snapshot(); static std::unique_ptr<State> Snapshot();
const std::vector<KeyboardReport>& KeyboardReports() const; const HIDState* HIDReports() const;
const KeyboardReport& KeyboardReports(size_t i) const;
const std::vector<SystemControlReport>& SystemControlReports() const;
const SystemControlReport& SystemControlReports(size_t i) const;
private: private:
static std::vector<KeyboardReport> keyboard_reports; std::unique_ptr<HIDState> hid_state_;
static std::vector<SystemControlReport> system_control_reports;
static void Clear();
static void ProcessKeyboardReport(const KeyboardReport& report);
static void ProcessSystemControlReport(const SystemControlReport& report);
std::vector<KeyboardReport> keyboard_reports_;
std::vector<SystemControlReport> system_control_reports_;
}; };
} // namespace testing } // namespace testing

@ -14,19 +14,16 @@
* this program. If not, see <http://www.gnu.org/licenses/>. * this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// Out of order due to macro conflicts.
#include "HIDReportObserver.h"
#include "testing/common/fix-macros.h"
#include "testing/common/VirtualDeviceTest.h" #include "testing/common/VirtualDeviceTest.h"
#include <memory> #include "HIDReportObserver.h"
#include "testing/common/HIDState.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
void VirtualDeviceTest::SetUp() { void VirtualDeviceTest::SetUp() {
HIDReportObserver::resetHook(&State::ProcessHidReport); HIDReportObserver::resetHook(&internal::HIDStateBuilder::ProcessHidReport);
} }
std::unique_ptr<State> VirtualDeviceTest::RunCycle() { std::unique_ptr<State> VirtualDeviceTest::RunCycle() {

@ -36,9 +36,6 @@ class VirtualDeviceTest : public ::testing::Test {
std::unique_ptr<State> RunCycle(); std::unique_ptr<State> RunCycle();
SimHarness sim_; SimHarness sim_;
private:
State state_;
}; };
} // namespace testing } // namespace testing

@ -41,29 +41,29 @@ TEST_F(Issue840, HasNotRegressed) {
sim_.Press(2, 1); // Press System_PowerDown sim_.Press(2, 1); // Press System_PowerDown
auto state = RunCycle(); auto state = RunCycle();
ASSERT_EQ(state->SystemControlReports().size(), 1); ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1);
EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown)); EXPECT_THAT(state->HIDReports()->SystemControl(0), Contains(System_PowerDown));
sim_.Press(3, 5); // Press System_Sleep sim_.Press(3, 5); // Press System_Sleep
state = RunCycle(); state = RunCycle();
ASSERT_EQ(state->SystemControlReports().size(), 1); ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1);
EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep)); EXPECT_THAT(state->HIDReports()->SystemControl(0), Contains(System_Sleep));
sim_.Release(2, 1); // Release System_PowerDown sim_.Release(2, 1); // Release System_PowerDown
state = RunCycle(); state = RunCycle();
EXPECT_EQ(state->SystemControlReports().size(), 0); EXPECT_EQ(state->HIDReports()->SystemControl().size(), 0);
sim_.Release(3, 5); // Release System_Sleep sim_.Release(3, 5); // Release System_Sleep
state = RunCycle(); state = RunCycle();
ASSERT_EQ(state->SystemControlReports().size(), 1); ASSERT_EQ(state->HIDReports()->SystemControl().size(), 1);
EXPECT_THAT(state->SystemControlReports(0), IsEmpty()); EXPECT_THAT(state->HIDReports()->SystemControl(0), IsEmpty());
state = RunCycle(); state = RunCycle();
EXPECT_EQ(state->SystemControlReports().size(), 0); EXPECT_EQ(state->HIDReports()->SystemControl().size(), 0);
} }
// TEST_F(Issue840, Reproduces) { // TEST_F(Issue840, Reproduces) {

@ -41,23 +41,23 @@ TEST_F(KeyboardReports, KeysActiveWhenPressed) {
sim_.Press(2, 1); // A sim_.Press(2, 1); // A
auto state = RunCycle(); auto state = RunCycle();
ASSERT_EQ(state->KeyboardReports().size(), 1); ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT( EXPECT_THAT(
state->KeyboardReports(0).ActiveKeycodes(), state->HIDReports()->Keyboard(0).ActiveKeycodes(),
Contains(Key_A)); Contains(Key_A));
sim_.Release(2, 1); // A sim_.Release(2, 1); // A
state = RunCycle(); state = RunCycle();
ASSERT_EQ(state->KeyboardReports().size(), 1); ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT( EXPECT_THAT(
state->KeyboardReports(0).ActiveKeycodes(), state->HIDReports()->Keyboard(0).ActiveKeycodes(),
IsEmpty()); IsEmpty());
state = RunCycle(); state = RunCycle();
// 2 cycles after releasing A // 2 cycles after releasing A
EXPECT_EQ(state->KeyboardReports().size(), 0); EXPECT_EQ(state->HIDReports()->Keyboard().size(), 0);
} }
} // namespace } // namespace

Loading…
Cancel
Save