test_driver.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_extra} {
  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_extra(uint8_t report_id, uint16_t data) {
  48. m_this->send_extra_mock(report_id, data);
  49. }
  50. namespace internal {
  51. void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) {
  52. testing::InSequence seq;
  53. EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_U));
  54. bool print_zero = false;
  55. for (int i = 7; i >= 0; --i) {
  56. if (i <= 3) {
  57. print_zero = true;
  58. }
  59. const uint8_t digit = (code_point >> (i * 4)) & 0xf;
  60. if (digit || print_zero) {
  61. EXPECT_REPORT(driver, (hex_digit_to_keycode(digit)));
  62. print_zero = true;
  63. }
  64. }
  65. EXPECT_REPORT(driver, (KC_SPC));
  66. }
  67. } // namespace internal