test_driver.hpp 5.9 KB

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