logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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. #pragma once
  17. #include "gmock/gmock.h"
  18. #include <stdint.h>
  19. #include "host.h"
  20. #include "keyboard_report_util.hpp"
  21. extern "C" {
  22. #include "keycode_string.h"
  23. }
  24. #include "test_logger.hpp"
  25. class TestDriver {
  26. public:
  27. TestDriver();
  28. ~TestDriver();
  29. void set_leds(uint8_t leds) {
  30. m_leds = leds;
  31. }
  32. MOCK_METHOD1(send_keyboard_mock, void(report_keyboard_t&));
  33. MOCK_METHOD1(send_nkro_mock, void(report_nkro_t&));
  34. MOCK_METHOD1(send_mouse_mock, void(report_mouse_t&));
  35. MOCK_METHOD1(send_extra_mock, void(report_extra_t&));
  36. private:
  37. static uint8_t keyboard_leds(void);
  38. static void send_keyboard(report_keyboard_t* report);
  39. static void send_nkro(report_nkro_t* report);
  40. static void send_mouse(report_mouse_t* report);
  41. static void send_extra(report_extra_t* report);
  42. host_driver_t m_driver;
  43. uint8_t m_leds = 0;
  44. static TestDriver* m_this;
  45. };
  46. /**
  47. * @brief Sets gmock expectation that a keyboard report of `report` keys will be sent.
  48. * For this macro to parse correctly, the `report` arg must be surrounded by
  49. * parentheses ( ). For instance,
  50. *
  51. * // Expect that a report of "KC_LSFT + KC_A" is sent to the host.
  52. * EXPECT_REPORT(driver, (KC_LSFT, KC_A));
  53. *
  54. * is shorthand for
  55. *
  56. * EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
  57. *
  58. * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
  59. * allow only single report to be sent:
  60. *
  61. * EXPECT_REPORT(driver, (KC_LSFT, KC_A)).Times(1);
  62. */
  63. #define EXPECT_REPORT(driver, report) EXPECT_CALL((driver), send_keyboard_mock(KeyboardReport report))
  64. /**
  65. * @brief Sets gmock expectation that a mouse report of `report` will be sent.
  66. * For this macro to parse correctly, the `report` arg must be surrounded by
  67. * parentheses ( ). For instance,
  68. *
  69. * // Expect that a report of "X:-10 Y:0 H:0 V:10 BTN:1 " is sent to the host.
  70. * EXPECT_REPORT(driver, (-10, 0, 0, 0, 1));
  71. *
  72. * is shorthand for
  73. *
  74. * EXPECT_CALL(driver, send_mouse_mock(MouseReport(-10, 0, 0, 0, 1)));
  75. *
  76. * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
  77. * allow only single report to be sent:
  78. *
  79. * EXPECT_REPORT(driver, (-10, 0, 0, 0, 1)).Times(1);
  80. */
  81. #define EXPECT_MOUSE_REPORT(driver, report) EXPECT_CALL((driver), send_mouse_mock(MouseReport report))
  82. /**
  83. * @brief Sets gmock expectation that Unicode `code_point` is sent with UNICODE_MODE_LINUX input
  84. * mode. For instance for U+2013,
  85. *
  86. * EXPECT_UNICODE(driver, 0x2013);
  87. *
  88. * expects the sequence of keys:
  89. *
  90. * "Ctrl+Shift+U, 2, 0, 1, 3, space".
  91. */
  92. #define EXPECT_UNICODE(driver, code_point) internal::expect_unicode_code_point((driver), (code_point))
  93. /**
  94. * @brief Sets gmock expectation that a empty keyboard report will be sent.
  95. * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_REPORT, for instance,
  96. * allow any number of empty reports with:
  97. *
  98. * EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
  99. */
  100. #define EXPECT_EMPTY_REPORT(driver) EXPECT_REPORT(driver, ())
  101. /**
  102. * @brief Sets gmock expectation that a empty keyboard report will be sent.
  103. * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_MOUSE_REPORT, for instance,
  104. * allow any number of empty reports with:
  105. *
  106. * EXPECT_EMPTY_MOUSE_REPORT(driver).Times(AnyNumber());
  107. */
  108. #define EXPECT_EMPTY_MOUSE_REPORT(driver) EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 0))
  109. /**
  110. * @brief Sets gmock expectation that a keyboard report will be sent, without matching its content.
  111. * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_REPORT, for instance,
  112. * allow a single arbitrary report with:
  113. *
  114. * EXPECT_ANY_REPORT(driver).Times(1);
  115. */
  116. #define EXPECT_ANY_REPORT(driver) EXPECT_CALL((driver), send_keyboard_mock(_))
  117. /**
  118. * @brief Sets gmock expectation that a mouse report will be sent, without matching its content.
  119. * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_MOUSE_REPORT, for instance,
  120. * allow a single arbitrary report with:
  121. *
  122. * EXPECT_ANY_MOUSE_REPORT(driver).Times(1);
  123. */
  124. #define EXPECT_ANY_MOUSE_REPORT(driver) EXPECT_CALL((driver), send_mouse_mock(_))
  125. /**
  126. * @brief Sets gmock expectation that no keyboard report will be sent at all.
  127. */
  128. #define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0)
  129. /**
  130. * @brief Sets gmock expectation that no keyboard report will be sent at all.
  131. */
  132. #define EXPECT_NO_MOUSE_REPORT(driver) EXPECT_ANY_MOUSE_REPORT(driver).Times(0)
  133. /** @brief Tests whether keycode `actual` is equal to `expected`. */
  134. #define EXPECT_KEYCODE_EQ(actual, expected) EXPECT_THAT((actual), KeycodeEq((expected)))
  135. MATCHER_P(KeycodeEq, expected_keycode, "is equal to " + testing::PrintToString(expected_keycode) + ", keycode " + get_keycode_string(expected_keycode)) {
  136. if (arg == expected_keycode) {
  137. return true;
  138. }
  139. *result_listener << "keycode " << get_keycode_string(arg);
  140. return false;
  141. }
  142. /**
  143. * @brief Verify and clear all gmock expectations that have been setup until
  144. * this point.
  145. */
  146. #define VERIFY_AND_CLEAR(driver) testing::Mock::VerifyAndClearExpectations(&driver)
  147. namespace internal {
  148. void expect_unicode_code_point(TestDriver& driver, uint32_t code_point);
  149. } // namespace internal