qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

test_driver.hpp (5989B)


      1 /* Copyright 2017 Fred Sundvik
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 2 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 
     17 #pragma once
     18 
     19 #include "gmock/gmock.h"
     20 #include <stdint.h>
     21 #include "host.h"
     22 #include "keyboard_report_util.hpp"
     23 extern "C" {
     24 #include "keycode_string.h"
     25 }
     26 #include "test_logger.hpp"
     27 
     28 class TestDriver {
     29    public:
     30     TestDriver();
     31     ~TestDriver();
     32     void set_leds(uint8_t leds) {
     33         m_leds = leds;
     34     }
     35 
     36     MOCK_METHOD1(send_keyboard_mock, void(report_keyboard_t&));
     37     MOCK_METHOD1(send_nkro_mock, void(report_nkro_t&));
     38     MOCK_METHOD1(send_mouse_mock, void(report_mouse_t&));
     39     MOCK_METHOD1(send_extra_mock, void(report_extra_t&));
     40 
     41    private:
     42     static uint8_t     keyboard_leds(void);
     43     static void        send_keyboard(report_keyboard_t* report);
     44     static void        send_nkro(report_nkro_t* report);
     45     static void        send_mouse(report_mouse_t* report);
     46     static void        send_extra(report_extra_t* report);
     47     host_driver_t      m_driver;
     48     uint8_t            m_leds = 0;
     49     static TestDriver* m_this;
     50 };
     51 
     52 /**
     53  * @brief Sets gmock expectation that a keyboard report of `report` keys will be sent.
     54  * For this macro to parse correctly, the `report` arg must be surrounded by
     55  * parentheses ( ). For instance,
     56  *
     57  *   // Expect that a report of "KC_LSFT + KC_A" is sent to the host.
     58  *   EXPECT_REPORT(driver, (KC_LSFT, KC_A));
     59  *
     60  * is shorthand for
     61  *
     62  *   EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
     63  *
     64  * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
     65  * allow only single report to be sent:
     66  *
     67  *   EXPECT_REPORT(driver, (KC_LSFT, KC_A)).Times(1);
     68  */
     69 #define EXPECT_REPORT(driver, report) EXPECT_CALL((driver), send_keyboard_mock(KeyboardReport report))
     70 
     71 /**
     72  * @brief Sets gmock expectation that a mouse report of `report` will be sent.
     73  * For this macro to parse correctly, the `report` arg must be surrounded by
     74  * parentheses ( ). For instance,
     75  *
     76  *   // Expect that a report of "X:-10 Y:0 H:0 V:10 BTN:1 " is sent to the host.
     77  *   EXPECT_REPORT(driver, (-10, 0, 0, 0, 1));
     78  *
     79  * is shorthand for
     80  *
     81  *   EXPECT_CALL(driver, send_mouse_mock(MouseReport(-10, 0, 0, 0, 1)));
     82  *
     83  * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
     84  * allow only single report to be sent:
     85  *
     86  *    EXPECT_REPORT(driver, (-10, 0, 0, 0, 1)).Times(1);
     87  */
     88 #define EXPECT_MOUSE_REPORT(driver, report) EXPECT_CALL((driver), send_mouse_mock(MouseReport report))
     89 
     90 /**
     91  * @brief Sets gmock expectation that Unicode `code_point` is sent with UNICODE_MODE_LINUX input
     92  * mode. For instance for U+2013,
     93  *
     94  *   EXPECT_UNICODE(driver, 0x2013);
     95  *
     96  * expects the sequence of keys:
     97  *
     98  *   "Ctrl+Shift+U, 2, 0, 1, 3, space".
     99  */
    100 #define EXPECT_UNICODE(driver, code_point) internal::expect_unicode_code_point((driver), (code_point))
    101 
    102 /**
    103  * @brief Sets gmock expectation that a empty keyboard report will be sent.
    104  * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_REPORT, for instance,
    105  * allow any number of empty reports with:
    106  *
    107  *   EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
    108  */
    109 #define EXPECT_EMPTY_REPORT(driver) EXPECT_REPORT(driver, ())
    110 
    111 /**
    112  * @brief Sets gmock expectation that a empty keyboard report will be sent.
    113  * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_MOUSE_REPORT, for instance,
    114  * allow any number of empty reports with:
    115  *
    116  *   EXPECT_EMPTY_MOUSE_REPORT(driver).Times(AnyNumber());
    117  */
    118 #define EXPECT_EMPTY_MOUSE_REPORT(driver) EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 0))
    119 
    120 /**
    121  * @brief Sets gmock expectation that a keyboard report will be sent, without matching its content.
    122  * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_REPORT, for instance,
    123  * allow a single arbitrary report with:
    124  *
    125  *   EXPECT_ANY_REPORT(driver).Times(1);
    126  */
    127 #define EXPECT_ANY_REPORT(driver) EXPECT_CALL((driver), send_keyboard_mock(_))
    128 
    129 /**
    130  * @brief Sets gmock expectation that a mouse report will be sent, without matching its content.
    131  * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_MOUSE_REPORT, for instance,
    132  * allow a single arbitrary report with:
    133  *
    134  *   EXPECT_ANY_MOUSE_REPORT(driver).Times(1);
    135  */
    136 #define EXPECT_ANY_MOUSE_REPORT(driver) EXPECT_CALL((driver), send_mouse_mock(_))
    137 
    138 /**
    139  * @brief Sets gmock expectation that no keyboard report will be sent at all.
    140  */
    141 #define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0)
    142 
    143 /**
    144  * @brief Sets gmock expectation that no keyboard report will be sent at all.
    145  */
    146 #define EXPECT_NO_MOUSE_REPORT(driver) EXPECT_ANY_MOUSE_REPORT(driver).Times(0)
    147 
    148 /** @brief Tests whether keycode `actual` is equal to `expected`. */
    149 #define EXPECT_KEYCODE_EQ(actual, expected) EXPECT_THAT((actual), KeycodeEq((expected)))
    150 
    151 MATCHER_P(KeycodeEq, expected_keycode, "is equal to " + testing::PrintToString(expected_keycode) + ", keycode " + get_keycode_string(expected_keycode)) {
    152     if (arg == expected_keycode) {
    153         return true;
    154     }
    155     *result_listener << "keycode " << get_keycode_string(arg);
    156     return false;
    157 }
    158 
    159 /**
    160  * @brief Verify and clear all gmock expectations that have been setup until
    161  * this point.
    162  */
    163 #define VERIFY_AND_CLEAR(driver) testing::Mock::VerifyAndClearExpectations(&driver)
    164 
    165 namespace internal {
    166 void expect_unicode_code_point(TestDriver& driver, uint32_t code_point);
    167 } // namespace internal