test_driver.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <iomanip>
  18. TestDriver* TestDriver::m_this = nullptr;
  19. namespace {
  20. // Given a hex digit between 0 and 15, returns the corresponding keycode.
  21. uint8_t hex_digit_to_keycode(uint8_t digit) {
  22. // clang-format off
  23. static const uint8_t hex_keycodes[] = {
  24. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  25. KC_8, KC_9, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F
  26. };
  27. // clang-format on
  28. return hex_keycodes[digit];
  29. }
  30. } // namespace
  31. TestDriver::TestDriver() : m_driver{&TestDriver::keyboard_leds, &TestDriver::send_keyboard, &TestDriver::send_nkro, &TestDriver::send_mouse, &TestDriver::send_extra} {
  32. host_set_driver(&m_driver);
  33. m_this = this;
  34. }
  35. TestDriver::~TestDriver() {
  36. m_this = nullptr;
  37. }
  38. uint8_t TestDriver::keyboard_leds(void) {
  39. return m_this->m_leds;
  40. }
  41. void TestDriver::send_keyboard(report_keyboard_t* report) {
  42. test_logger.trace() << *report;
  43. m_this->send_keyboard_mock(*report);
  44. }
  45. void TestDriver::send_nkro(report_nkro_t* report) {
  46. m_this->send_nkro_mock(*report);
  47. }
  48. void TestDriver::send_mouse(report_mouse_t* report) {
  49. test_logger.trace() << std::setw(10) << std::left << "send_mouse: (X:" << (int)report->x << ", Y:" << (int)report->y << ", H:" << (int)report->h << ", V:" << (int)report->v << ", B:" << (int)report->buttons << ")" << std::endl;
  50. m_this->send_mouse_mock(*report);
  51. }
  52. void TestDriver::send_extra(report_extra_t* report) {
  53. m_this->send_extra_mock(*report);
  54. }
  55. namespace internal {
  56. void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) {
  57. testing::InSequence seq;
  58. EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT));
  59. EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT, KC_U));
  60. EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT));
  61. EXPECT_EMPTY_REPORT(driver);
  62. bool print_zero = false;
  63. for (int i = 7; i >= 0; --i) {
  64. if (i <= 3) {
  65. print_zero = true;
  66. }
  67. const uint8_t digit = (code_point >> (i * 4)) & 0xf;
  68. if (digit || print_zero) {
  69. EXPECT_REPORT(driver, (hex_digit_to_keycode(digit)));
  70. EXPECT_EMPTY_REPORT(driver);
  71. print_zero = true;
  72. }
  73. }
  74. EXPECT_REPORT(driver, (KC_SPACE));
  75. EXPECT_EMPTY_REPORT(driver);
  76. }
  77. } // namespace internal