Treat SystemControlReport like a container in tests.

Signed-off-by: Eric Paniagua <epaniagua@google.com>
pull/898/head
Eric Paniagua 4 years ago
parent 54f2dfb070
commit 0cab1494b5

@ -25,6 +25,7 @@ SystemControlReport::SystemControlReport(const void* data) {
const ReportData& report_data = const ReportData& report_data =
*static_cast<const ReportData*>(data); *static_cast<const ReportData*>(data);
memcpy(&report_data_, &report_data, sizeof(report_data_)); memcpy(&report_data_, &report_data, sizeof(report_data_));
this->push_back(report_data_.key);
} }
uint8_t SystemControlReport::Key() const { uint8_t SystemControlReport::Key() const {

@ -24,7 +24,7 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
class SystemControlReport { class SystemControlReport : public std::vector<uint8_t> {
public: public:
typedef HID_SystemControlReport_Data_t ReportData; typedef HID_SystemControlReport_Data_t ReportData;

@ -17,6 +17,7 @@
#pragma once #pragma once
#include "kaleidoscope/key_defs.h" #include "kaleidoscope/key_defs.h"
#include "testing/common/SystemControlReport.h"
// Out of order because `fix-macros.h` clears the preprocessor environment for // Out of order because `fix-macros.h` clears the preprocessor environment for
// gtest and gmock. // gtest and gmock.
@ -26,6 +27,31 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace testing { namespace testing {
MATCHER_P(Contains, key, negation ? "does not contain" : "contains") {
return arg.Key() == key.getKeyCode();
}
class SystemControlReportIsNullMatcher
: public ::testing::MatcherInterface<SystemControlReport> {
public:
bool MatchAndExplain(SystemControlReport report,
::testing::MatchResultListener* /* ignored */) const override {
return report.Key() == 0;
}
void DescribeTo(std::ostream* os) const override {
*os << "is empty";
}
void DescribeNegationTo(std::ostream* os) const override {
*os << "is not empty";
}
};
::testing::Matcher<SystemControlReport> IsNull() {
return ::testing::MakeMatcher(new SystemControlReportIsNullMatcher);
}
auto Contains(Key key) { return ::testing::Contains(key.getKeyCode()); } auto Contains(Key key) { return ::testing::Contains(key.getKeyCode()); }
} // namespace testing } // namespace testing

@ -42,30 +42,24 @@ TEST_F(Issue840, HasNotRegressed) {
auto state = RunCycle(); auto state = RunCycle();
ASSERT_EQ(state->SystemControlReports().size(), 1); ASSERT_EQ(state->SystemControlReports().size(), 1);
EXPECT_EQ( EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown));
state->SystemControlReports(0).Key(),
System_PowerDown.getKeyCode());
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->SystemControlReports().size(), 1);
EXPECT_EQ( EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep));
state->SystemControlReports(0).Key(),
System_Sleep.getKeyCode());
sim_.Release(2, 1); // Release System_PowerDown sim_.Release(2, 1); // Release System_PowerDown
state = RunCycle(); state = RunCycle();
ASSERT_EQ(state->SystemControlReports().size(), 0); EXPECT_EQ(state->SystemControlReports().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->SystemControlReports().size(), 1);
EXPECT_EQ( EXPECT_THAT(state->SystemControlReports(0), IsNull());
state->SystemControlReports(0).Key(),
0 /* null report */);
state = RunCycle(); state = RunCycle();
@ -77,33 +71,25 @@ TEST_F(Issue840, HasNotRegressed) {
// auto state = RunCycle(); // auto state = RunCycle();
// //
// ASSERT_EQ(state->SystemControlReports().size(), 1); // ASSERT_EQ(state->SystemControlReports().size(), 1);
// EXPECT_EQ( // EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown));
// state->SystemControlReports(0).Key(),
// System_PowerDown.getKeyCode());
// //
// 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->SystemControlReports().size(), 1);
// EXPECT_EQ( // EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep));
// state->SystemControlReports(0).Key(),
// System_Sleep.getKeyCode());
// //
// sim_.Release(2, 1); // Release System_PowerDown // sim_.Release(2, 1); // Release System_PowerDown
// state = RunCycle(); // state = RunCycle();
// //
// ASSERT_EQ(state->SystemControlReports().size(), 1); // ASSERT_EQ(state->SystemControlReports().size(), 1);
// EXPECT_EQ( // EXPECT_THAT(state->SystemControlReports(0), IsNull());
// state->SystemControlReports(0).Key(),
// 0 /* null report */);
// //
// 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->SystemControlReports().size(), 1);
// EXPECT_EQ( // EXPECT_THAT(state->SystemControlReports(0), IsNull());
// state->SystemControlReports(0).Key(),
// 0 /* null report */);
// //
// state = RunCycle(); // state = RunCycle();
// //

Loading…
Cancel
Save