From 36f820be7e80bc8f1f5489373708356cf142c269 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 18 Jun 2017 23:49:38 +0300 Subject: Add a test fixture It only initializes QMK once, and clears the matrix after each test. --- tests/test_common/test_fixture.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/test_common/test_fixture.cpp (limited to 'tests/test_common/test_fixture.cpp') diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp new file mode 100644 index 000000000..aff518d21 --- /dev/null +++ b/tests/test_common/test_fixture.cpp @@ -0,0 +1,38 @@ +#include "test_fixture.h" +#include "gmock/gmock.h" +#include "test_driver.h" +#include "test_matrix.h" +#include "keyboard.h" + +using testing::_; +using testing::AnyNumber; +using testing::Return; +using testing::Between; + +void TestFixture::SetUpTestCase() { + TestDriver driver; + EXPECT_CALL(driver, send_keyboard_mock(_)); + keyboard_init(); +} + +void TestFixture::TearDownTestCase() { +} + +TestFixture::TestFixture() { +} + +TestFixture::~TestFixture() { + TestDriver driver; + clear_all_keys(); + // Run for a while to make sure all keys are completely released + // Should probably wait until tapping term etc, has timed out + EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + for (int i=0; i<100; i++) { + keyboard_task(); + } + testing::Mock::VerifyAndClearExpectations(&driver); + // Verify that the matrix really is cleared + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1)); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 43d8fa5bf1248ce5c1ce5f9cb0d238d794b4475d Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Mon, 19 Jun 2017 00:19:09 +0300 Subject: More natural interface for setting keyboard leds --- tests/basic/test.cpp | 3 --- tests/test_common/test_driver.cpp | 3 +-- tests/test_common/test_driver.h | 4 +++- tests/test_common/test_fixture.cpp | 2 -- 4 files changed, 4 insertions(+), 8 deletions(-) (limited to 'tests/test_common/test_fixture.cpp') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index 26a09585c..1bd5c2762 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -37,7 +37,6 @@ class KeyPress : public TestFixture {}; TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TestDriver driver; - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); keyboard_task(); } @@ -45,7 +44,6 @@ TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) { TestDriver driver; press_key(0, 0); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); keyboard_task(); } @@ -54,7 +52,6 @@ TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { TestDriver driver; press_key(1, 0); press_key(0, 1); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); //Note that QMK only processes one key at a time EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))); keyboard_task(); diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp index 9e618aa97..feb80563a 100644 --- a/tests/test_common/test_driver.cpp +++ b/tests/test_common/test_driver.cpp @@ -27,7 +27,6 @@ TestDriver::TestDriver() &TestDriver::send_consumer } { - host_set_driver(&m_driver); m_this = this; } @@ -37,7 +36,7 @@ TestDriver::~TestDriver() { } uint8_t TestDriver::keyboard_leds(void) { - return m_this->keyboard_leds_mock(); + return m_this->m_leds; } void TestDriver::send_keyboard(report_keyboard_t* report) { diff --git a/tests/test_common/test_driver.h b/tests/test_common/test_driver.h index b1b95fbcc..0123fd539 100644 --- a/tests/test_common/test_driver.h +++ b/tests/test_common/test_driver.h @@ -27,7 +27,8 @@ class TestDriver { public: TestDriver(); ~TestDriver(); - MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); + void set_leds(uint8_t leds) { m_leds = leds; } + MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); MOCK_METHOD1(send_system_mock, void (uint16_t)); @@ -39,6 +40,7 @@ private: static void send_system(uint16_t data); static void send_consumer(uint16_t data); host_driver_t m_driver; + uint8_t m_leds = 0; static TestDriver* m_this; }; diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index aff518d21..eef9b854b 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp @@ -27,12 +27,10 @@ TestFixture::~TestFixture() { // Run for a while to make sure all keys are completely released // Should probably wait until tapping term etc, has timed out EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); for (int i=0; i<100; i++) { keyboard_task(); } testing::Mock::VerifyAndClearExpectations(&driver); // Verify that the matrix really is cleared EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1)); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); } \ No newline at end of file -- cgit v1.2.3-70-g09d2