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