Make RunCycle return std::unique_ptr<State>.

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

@ -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

@ -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<KeyboardReport> keyboard_reports_;
};

@ -16,22 +16,16 @@
#include "testing/common/VirtualDeviceTest.h"
#include <memory>
namespace kaleidoscope {
namespace testing {
void VirtualDeviceTest::RunCycle() {
state_.Clear();
std::unique_ptr<State> VirtualDeviceTest::RunCycle() {
auto state = std::unique_ptr<State>(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

@ -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<State> 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:

@ -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

Loading…
Cancel
Save