logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

test_fixture.cpp (6020B)


  1. #include "test_fixture.hpp"
  2. #include <algorithm>
  3. #include <cstdint>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include "gmock/gmock-cardinalities.h"
  7. #include "gmock/gmock.h"
  8. #include "gtest/gtest.h"
  9. #include "keyboard_report_util.hpp"
  10. #include "mouse_report_util.hpp"
  11. #include "keycode.h"
  12. #include "test_driver.hpp"
  13. #include "test_logger.hpp"
  14. #include "test_matrix.h"
  15. #include "test_keymap_key.hpp"
  16. #include "timer.h"
  17. extern "C" {
  18. #include "action.h"
  19. #include "action_tapping.h"
  20. #include "action_util.h"
  21. #include "action_layer.h"
  22. #include "debug.h"
  23. #include "eeconfig.h"
  24. #include "keyboard.h"
  25. void set_time(uint32_t t);
  26. void advance_time(uint32_t ms);
  27. }
  28. using testing::_;
  29. /* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */
  30. TestFixture* TestFixture::m_this = nullptr;
  31. /* Override weak QMK function to allow the usage of isolated per-test keymaps in unit-tests.
  32. * The actual call is dynamicaly dispatched to the current active test fixture, which in turn has it's own keymap. */
  33. extern "C" uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t position) {
  34. uint16_t keycode;
  35. TestFixture::m_this->get_keycode(layer, position, &keycode);
  36. return keycode;
  37. }
  38. void TestFixture::SetUpTestCase() {
  39. test_logger.info() << "test fixture setup-up start." << std::endl;
  40. // The following is enough to bootstrap the values set in main
  41. eeconfig_init_quantum();
  42. eeconfig_update_debug(&debug_config);
  43. TestDriver driver;
  44. keyboard_init();
  45. test_logger.info() << "test fixture setup-up end." << std::endl;
  46. }
  47. void TestFixture::TearDownTestCase() {}
  48. TestFixture::TestFixture() {
  49. m_this = this;
  50. timer_clear();
  51. keyrecord_t empty_keyrecord = {0};
  52. test_logger.info() << "tapping term is " << +GET_TAPPING_TERM(KC_TRANSPARENT, &empty_keyrecord) << "ms" << std::endl;
  53. }
  54. TestFixture::~TestFixture() {
  55. test_logger.info() << "test fixture clean-up start." << std::endl;
  56. TestDriver driver;
  57. /* Reset keyboard state. */
  58. clear_all_keys();
  59. #ifdef MOUSEKEY_ENABLE
  60. EXPECT_EMPTY_MOUSE_REPORT(driver);
  61. #endif
  62. clear_keyboard();
  63. clear_oneshot_mods();
  64. clear_oneshot_locked_mods();
  65. reset_oneshot_layer();
  66. layer_clear();
  67. #if defined(SWAP_HANDS_ENABLE)
  68. clear_oneshot_swaphands();
  69. #endif
  70. idle_for(TAPPING_TERM * 10);
  71. VERIFY_AND_CLEAR(driver);
  72. /* Verify that the matrix really is cleared */
  73. EXPECT_NO_REPORT(driver);
  74. idle_for(TAPPING_TERM * 10);
  75. VERIFY_AND_CLEAR(driver);
  76. m_this = nullptr;
  77. test_logger.info() << "test fixture clean-up end." << std::endl;
  78. print_test_log();
  79. }
  80. void TestFixture::add_key(KeymapKey key) {
  81. if (this->find_key(key.layer, key.position)) {
  82. FAIL() << "key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")";
  83. }
  84. this->keymap.push_back(key);
  85. }
  86. void TestFixture::tap_key(KeymapKey key, unsigned delay_ms) {
  87. key.press();
  88. idle_for(delay_ms);
  89. key.release();
  90. run_one_scan_loop();
  91. }
  92. void TestFixture::tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms) {
  93. for (KeymapKey key : chord_keys) { // Press each key.
  94. key.press();
  95. run_one_scan_loop();
  96. }
  97. if (delay_ms > 1) {
  98. idle_for(delay_ms - 1);
  99. }
  100. for (KeymapKey key : chord_keys) { // Release each key.
  101. key.release();
  102. run_one_scan_loop();
  103. }
  104. }
  105. void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) {
  106. this->keymap.clear();
  107. for (auto& key : keys) {
  108. add_key(key);
  109. }
  110. }
  111. const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const {
  112. auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; };
  113. auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate);
  114. if (result != std::end(this->keymap)) {
  115. return &(*result);
  116. }
  117. return nullptr;
  118. }
  119. void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const {
  120. bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS;
  121. if (key_is_out_of_bounds) {
  122. /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */
  123. auto msg = [&]() {
  124. std::stringstream msg;
  125. msg << "keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl;
  126. return msg.str();
  127. }();
  128. *result = KC_NO;
  129. test_logger.error() << msg;
  130. EXPECT_FALSE(key_is_out_of_bounds) << msg;
  131. return;
  132. }
  133. if (auto key = this->find_key(layer, position)) {
  134. *result = key->code;
  135. return;
  136. }
  137. FAIL() << "no key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")";
  138. }
  139. void TestFixture::run_one_scan_loop() {
  140. this->idle_for(1);
  141. }
  142. void TestFixture::idle_for(unsigned time) {
  143. test_logger.trace() << +time << " keyboard task " << (time > 1 ? "loops" : "loop") << std::endl;
  144. for (unsigned i = 0; i < time; i++) {
  145. keyboard_task();
  146. housekeeping_task();
  147. advance_time(1);
  148. }
  149. }
  150. void TestFixture::print_test_log() const {
  151. const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
  152. if (HasFailure()) {
  153. std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl;
  154. test_logger.print_header();
  155. test_logger.print_log();
  156. }
  157. test_logger.reset();
  158. }
  159. void TestFixture::expect_layer_state(layer_t layer_state) const {
  160. test_logger.trace() << "layer state: (" << +layer_state << ") highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl;
  161. EXPECT_TRUE(layer_state_is(layer_state));
  162. }