Remove test dependency on change to KeyboardioHID/src/HIDReportObserver.h.

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

@ -25,22 +25,9 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
State::State() { std::vector<KeyboardReport> State::keyboard_reports;
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);
}
// static // static
void State::DefaultProcessHidReport(uint8_t, const void*, int, int) {
// TODO: Log that the report was dropped.
}
void State::ProcessHidReport( void State::ProcessHidReport(
uint8_t id, const void* data, int len, int result) { uint8_t id, const void* data, int len, int result) {
switch (id) { switch (id) {
@ -72,6 +59,18 @@ void State::ProcessHidReport(
} }
} }
// static
std::unique_ptr<State> State::Snapshot() {
auto state = std::make_unique<State>();
// 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<KeyboardReport>& State::KeyboardReports() const { const std::vector<KeyboardReport>& State::KeyboardReports() const {
return keyboard_reports_; return keyboard_reports_;
} }
@ -80,13 +79,14 @@ const KeyboardReport& State::KeyboardReports(size_t i) const {
return keyboard_reports_.at(i); return keyboard_reports_.at(i);
} }
void State::ProcessKeyboardReport(const KeyboardReport& report) { // static
keyboard_reports_.push_back(report); void State::Clear() {
keyboard_reports.clear();
} }
void State::Snapshot() { // static
// TODO: Grab a copy of current instantaneous state, like: void State::ProcessKeyboardReport(const KeyboardReport& report) {
// key states, layer stack, led states keyboard_reports.push_back(report);
} }
} // namespace testing } // namespace testing

@ -21,31 +21,30 @@
#include <vector> #include <vector>
#include "HID-Settings.h" #include "HID-Settings.h"
#include "HIDReportObserver.h"
#include "testing/common/KeyboardReport.h" #include "testing/common/KeyboardReport.h"
// Out of order due to macro conflicts.
#include "testing/common/fix-macros.h"
#include <memory>
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
class State { class State {
public: public:
~State(); static void ProcessHidReport(
uint8_t id, const void* data, int len, int result);
static void DefaultProcessHidReport(uint8_t, const void*, int, int);
void ProcessHidReport(uint8_t id, const void* data, int len, int result); static std::unique_ptr<State> Snapshot();
const std::vector<KeyboardReport>& KeyboardReports() const; const std::vector<KeyboardReport>& KeyboardReports() const;
const KeyboardReport& KeyboardReports(size_t i) const; const KeyboardReport& KeyboardReports(size_t i) const;
private: private:
friend class VirtualDeviceTest; static std::vector<KeyboardReport> keyboard_reports;
State();
void ProcessKeyboardReport(const KeyboardReport& report);
void Snapshot(); static void Clear();
static void ProcessKeyboardReport(const KeyboardReport& report);
std::vector<KeyboardReport> keyboard_reports_; std::vector<KeyboardReport> keyboard_reports_;
}; };

@ -14,6 +14,10 @@
* 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 <memory>
@ -21,11 +25,13 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
void VirtualDeviceTest::SetUp() {
HIDReportObserver::resetHook(&State::ProcessHidReport);
}
std::unique_ptr<State> VirtualDeviceTest::RunCycle() { std::unique_ptr<State> VirtualDeviceTest::RunCycle() {
auto state = std::unique_ptr<State>(new State());
sim_.RunCycle(); sim_.RunCycle();
state->Snapshot(); return State::Snapshot();
return state;
} }
} // namespace testing } // namespace testing

@ -21,16 +21,18 @@
#include "testing/common/SimHarness.h" #include "testing/common/SimHarness.h"
#include "testing/common/State.h" #include "testing/common/State.h"
// Out of order because `fix-macros.h` clears the preprocessor environment for // Out of order due to macro conflicts.
// gtest and gmock.
#include "testing/common/fix-macros.h" #include "testing/common/fix-macros.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <memory>
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
class VirtualDeviceTest : public ::testing::Test { class VirtualDeviceTest : public ::testing::Test {
protected: protected:
void SetUp();
std::unique_ptr<State> RunCycle(); std::unique_ptr<State> RunCycle();
SimHarness sim_; SimHarness sim_;

@ -54,8 +54,9 @@ TEST_F(KeyboardReports, KeysActiveWhenPressed) {
state->KeyboardReports(0).ActiveKeycodes(), state->KeyboardReports(0).ActiveKeycodes(),
IsEmpty()); IsEmpty());
state = RunCycle(); // 2 cycles later state = RunCycle();
// 2 cycles after releasing A
EXPECT_EQ(state->KeyboardReports().size(), 0); EXPECT_EQ(state->KeyboardReports().size(), 0);
} }

Loading…
Cancel
Save