test_driver.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "test_driver.hpp"
  17. TestDriver* TestDriver::m_this = nullptr;
  18. namespace {
  19. // Given a hex digit between 0 and 15, returns the corresponding keycode.
  20. uint8_t hex_digit_to_keycode(uint8_t digit) {
  21. // clang-format off
  22. static const uint8_t hex_keycodes[] = {
  23. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  24. KC_8, KC_9, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F
  25. };
  26. // clang-format on
  27. return hex_keycodes[digit];
  28. }
  29. } // namespace
  30. TestDriver::TestDriver() : m_driver{&TestDriver::keyboard_leds, &TestDriver::send_keyboard, &TestDriver::send_mouse, &TestDriver::send_system, &TestDriver::send_consumer} {
  31. host_set_driver(&m_driver);
  32. m_this = this;
  33. }
  34. TestDriver::~TestDriver() {
  35. m_this = nullptr;
  36. }
  37. uint8_t TestDriver::keyboard_leds(void) {
  38. return m_this->m_leds;
  39. }
  40. void TestDriver::send_keyboard(report_keyboard_t* report) {
  41. test_logger.trace() << *report;
  42. m_this->send_keyboard_mock(*report);
  43. }
  44. void TestDriver::send_mouse(report_mouse_t* report) {
  45. m_this->send_mouse_mock(*report);
  46. }
  47. void TestDriver::send_system(uint16_t data) {
  48. m_this->send_system_mock(data);
  49. }
  50. void TestDriver::send_consumer(uint16_t data) {
  51. m_this->send_consumer(data);
  52. }
  53. namespace internal {
  54. void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) {
  55. testing::InSequence seq;
  56. EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_U));
  57. bool print_zero = false;
  58. for (int i = 7; i >= 0; --i) {
  59. if (i <= 3) {
  60. print_zero = true;
  61. }
  62. const uint8_t digit = (code_point >> (i * 4)) & 0xf;
  63. if (digit || print_zero) {
  64. EXPECT_REPORT(driver, (hex_digit_to_keycode(digit)));
  65. print_zero = true;
  66. }
  67. }
  68. EXPECT_REPORT(driver, (KC_SPC));
  69. }
  70. } // namespace internal