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() { State::~State() {
keyboard_reports_.clear(); HIDReportObserver::resetHook(&State::DefaultProcessHidReport);
}
// static
void State::DefaultProcessHidReport(uint8_t, const void*, int, int) {
// TODO: Log that the report was dropped.
} }
void State::ProcessHidReport( void State::ProcessHidReport(
@ -79,5 +84,10 @@ void State::ProcessKeyboardReport(const KeyboardReport& report) {
keyboard_reports_.push_back(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 testing
} // namespace kaleidoscope } // namespace kaleidoscope

@ -29,9 +29,9 @@ namespace testing {
class State { class State {
public: 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); 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; const KeyboardReport& KeyboardReports(size_t i) const;
private: private:
friend class VirtualDeviceTest;
State();
void ProcessKeyboardReport(const KeyboardReport& report); void ProcessKeyboardReport(const KeyboardReport& report);
void Snapshot();
std::vector<KeyboardReport> keyboard_reports_; std::vector<KeyboardReport> keyboard_reports_;
}; };

@ -16,22 +16,16 @@
#include "testing/common/VirtualDeviceTest.h" #include "testing/common/VirtualDeviceTest.h"
#include <memory>
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
void VirtualDeviceTest::RunCycle() { std::unique_ptr<State> VirtualDeviceTest::RunCycle() {
state_.Clear(); auto state = std::unique_ptr<State>(new State());
sim_.RunCycle(); sim_.RunCycle();
} state->Snapshot();
return state;
void VirtualDeviceTest::RunCycles(size_t n) {
if (n == 0) return;
state_.Clear();
sim_.RunCycles(n);
}
const State& VirtualDeviceTest::Result() const {
return state_;
} }
} // namespace testing } // namespace testing

@ -31,11 +31,11 @@ namespace testing {
class VirtualDeviceTest : public ::testing::Test { class VirtualDeviceTest : public ::testing::Test {
protected: protected:
void RunCycle(); std::unique_ptr<State> RunCycle();
void RunCycles(size_t n);
const State& Result() const;
// 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_; ::kaleidoscope::testing::Simulator sim_;
private: private:

@ -39,20 +39,24 @@ class KeyboardReports : public VirtualDeviceTest {};
TEST_F(KeyboardReports, KeysActiveWhenPressed) { TEST_F(KeyboardReports, KeysActiveWhenPressed) {
sim_.Press(2, 1); // A sim_.Press(2, 1); // A
RunCycle(); auto state = RunCycle();
EXPECT_EQ(Result().KeyboardReports().size(), 1); ASSERT_EQ(state->KeyboardReports().size(), 1);
EXPECT_THAT( EXPECT_THAT(
Result().KeyboardReports(0).ActiveKeycodes(), state->KeyboardReports(0).ActiveKeycodes(),
ContainsKey(Key_A)); ContainsKey(Key_A));
sim_.Release(2, 1); // A sim_.Release(2, 1); // A
RunCycles(2); state = RunCycle();
EXPECT_EQ(Result().KeyboardReports().size(), 1); ASSERT_EQ(state->KeyboardReports().size(), 1);
EXPECT_THAT( EXPECT_THAT(
Result().KeyboardReports(0).ActiveKeycodes(), state->KeyboardReports(0).ActiveKeycodes(),
IsEmpty()); IsEmpty());
state = RunCycle(); // 2 cycles later
EXPECT_EQ(state->KeyboardReports().size(), 0);
} }
} // namespace } // namespace

Loading…
Cancel
Save