test_fixture.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "keycode.h"
  11. #include "test_driver.hpp"
  12. #include "test_logger.hpp"
  13. #include "test_matrix.h"
  14. #include "test_keymap_key.hpp"
  15. #include "timer.h"
  16. extern "C" {
  17. #include "action.h"
  18. #include "action_tapping.h"
  19. #include "action_util.h"
  20. #include "action_layer.h"
  21. #include "debug.h"
  22. #include "eeconfig.h"
  23. #include "keyboard.h"
  24. #include "keymap.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.raw);
  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. test_logger.info() << "tapping term is " << +GET_TAPPING_TERM(KC_TRANSPARENT, &(keyrecord_t){}) << "ms" << std::endl;
  52. }
  53. TestFixture::~TestFixture() {
  54. test_logger.info() << "test fixture clean-up start." << std::endl;
  55. TestDriver driver;
  56. /* Reset keyboard state. */
  57. clear_all_keys();
  58. clear_keyboard();
  59. clear_oneshot_mods();
  60. clear_oneshot_locked_mods();
  61. reset_oneshot_layer();
  62. layer_clear();
  63. #if defined(SWAP_HANDS_ENABLE)
  64. clear_oneshot_swaphands();
  65. #endif
  66. idle_for(TAPPING_TERM * 10);
  67. VERIFY_AND_CLEAR(driver);
  68. /* Verify that the matrix really is cleared */
  69. EXPECT_NO_REPORT(driver);
  70. idle_for(TAPPING_TERM * 10);
  71. VERIFY_AND_CLEAR(driver);
  72. m_this = nullptr;
  73. test_logger.info() << "test fixture clean-up end." << std::endl;
  74. print_test_log();
  75. }
  76. void TestFixture::add_key(KeymapKey key) {
  77. if (this->find_key(key.layer, key.position)) {
  78. FAIL() << "key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")";
  79. }
  80. this->keymap.push_back(key);
  81. }
  82. void TestFixture::tap_key(KeymapKey key, unsigned delay_ms) {
  83. key.press();
  84. idle_for(delay_ms);
  85. key.release();
  86. run_one_scan_loop();
  87. }
  88. void TestFixture::tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms) {
  89. for (KeymapKey key : chord_keys) { // Press each key.
  90. key.press();
  91. run_one_scan_loop();
  92. }
  93. if (delay_ms > 1) {
  94. idle_for(delay_ms - 1);
  95. }
  96. for (KeymapKey key : chord_keys) { // Release each key.
  97. key.release();
  98. run_one_scan_loop();
  99. }
  100. }
  101. void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) {
  102. this->keymap.clear();
  103. for (auto& key : keys) {
  104. add_key(key);
  105. }
  106. }
  107. const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const {
  108. auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; };
  109. auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate);
  110. if (result != std::end(this->keymap)) {
  111. return &(*result);
  112. }
  113. return nullptr;
  114. }
  115. void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const {
  116. bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS;
  117. if (key_is_out_of_bounds) {
  118. /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */
  119. auto msg = [&]() {
  120. std::stringstream msg;
  121. msg << "keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl;
  122. return msg.str();
  123. }();
  124. *result = KC_NO;
  125. test_logger.error() << msg;
  126. EXPECT_FALSE(key_is_out_of_bounds) << msg;
  127. return;
  128. }
  129. if (auto key = this->find_key(layer, position)) {
  130. *result = key->code;
  131. return;
  132. }
  133. FAIL() << "no key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")";
  134. }
  135. void TestFixture::run_one_scan_loop() {
  136. this->idle_for(1);
  137. }
  138. void TestFixture::idle_for(unsigned time) {
  139. test_logger.trace() << +time << " keyboard task " << (time > 1 ? "loops" : "loop") << std::endl;
  140. for (unsigned i = 0; i < time; i++) {
  141. keyboard_task();
  142. advance_time(1);
  143. }
  144. }
  145. void TestFixture::print_test_log() const {
  146. const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
  147. if (HasFailure()) {
  148. std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl;
  149. test_logger.print_header();
  150. test_logger.print_log();
  151. }
  152. test_logger.reset();
  153. }
  154. void TestFixture::expect_layer_state(layer_t layer_state) const {
  155. test_logger.trace() << "layer state: (" << +layer_state << ") highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl;
  156. EXPECT_TRUE(layer_state_is(layer_state));
  157. }