test_fixture.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. extern "C" {
  16. #include "action.h"
  17. #include "action_tapping.h"
  18. #include "action_util.h"
  19. #include "action_layer.h"
  20. #include "debug.h"
  21. #include "eeconfig.h"
  22. #include "keyboard.h"
  23. #include "keymap.h"
  24. void set_time(uint32_t t);
  25. void advance_time(uint32_t ms);
  26. }
  27. using testing::_;
  28. /* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */
  29. TestFixture* TestFixture::m_this = nullptr;
  30. /* Override weak QMK function to allow the usage of isolated per-test keymaps in unit-tests.
  31. * The actual call is dynamicaly dispatched to the current active test fixture, which in turn has it's own keymap. */
  32. extern "C" uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t position) {
  33. uint16_t keycode;
  34. TestFixture::m_this->get_keycode(layer, position, &keycode);
  35. return keycode;
  36. }
  37. void TestFixture::SetUpTestCase() {
  38. test_logger.info() << "TestFixture setup-up start." << std::endl;
  39. // The following is enough to bootstrap the values set in main
  40. eeconfig_init_quantum();
  41. eeconfig_update_debug(debug_config.raw);
  42. TestDriver driver;
  43. EXPECT_CALL(driver, send_keyboard_mock(_));
  44. keyboard_init();
  45. test_logger.info() << "TestFixture setup-up end." << std::endl;
  46. }
  47. void TestFixture::TearDownTestCase() {}
  48. TestFixture::TestFixture() { m_this = this; }
  49. TestFixture::~TestFixture() {
  50. test_logger.info() << "TestFixture clean-up start." << std::endl;
  51. TestDriver driver;
  52. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
  53. /* Reset keyboard state. */
  54. clear_all_keys();
  55. clear_keyboard();
  56. clear_oneshot_mods();
  57. clear_oneshot_locked_mods();
  58. reset_oneshot_layer();
  59. layer_clear();
  60. #if defined(SWAP_HANDS_ENABLE)
  61. clear_oneshot_swaphands();
  62. #endif
  63. idle_for(TAPPING_TERM * 10);
  64. testing::Mock::VerifyAndClearExpectations(&driver);
  65. /* Verify that the matrix really is cleared */
  66. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  67. idle_for(TAPPING_TERM * 10);
  68. testing::Mock::VerifyAndClearExpectations(&driver);
  69. m_this = nullptr;
  70. test_logger.info() << "TestFixture clean-up end." << std::endl;
  71. print_test_log();
  72. }
  73. void TestFixture::add_key(KeymapKey key) {
  74. if (this->find_key(key.layer, key.position)) {
  75. FAIL() << "Key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")";
  76. }
  77. this->keymap.push_back(key);
  78. }
  79. void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) {
  80. this->keymap.clear();
  81. for (auto& key : keys) {
  82. add_key(key);
  83. }
  84. }
  85. const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const {
  86. auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; };
  87. auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate);
  88. if (result != std::end(this->keymap)) {
  89. return &(*result);
  90. }
  91. return nullptr;
  92. }
  93. void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const {
  94. bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS;
  95. if (key_is_out_of_bounds) {
  96. /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */
  97. auto msg = [&]() {
  98. std::stringstream msg;
  99. msg << "Keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl;
  100. return msg.str();
  101. }();
  102. *result = KC_NO;
  103. test_logger.error() << msg;
  104. EXPECT_FALSE(key_is_out_of_bounds) << msg;
  105. return;
  106. }
  107. if (auto key = this->find_key(layer, position)) {
  108. *result = key->code;
  109. return;
  110. }
  111. FAIL() << "No key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")";
  112. }
  113. void TestFixture::run_one_scan_loop() {
  114. keyboard_task();
  115. advance_time(1);
  116. }
  117. void TestFixture::idle_for(unsigned time) {
  118. for (unsigned i = 0; i < time; i++) {
  119. run_one_scan_loop();
  120. }
  121. }
  122. void TestFixture::print_test_log() const {
  123. const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
  124. if (HasFailure()) {
  125. std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl;
  126. test_logger.print_log();
  127. }
  128. test_logger.reset();
  129. }
  130. void TestFixture::expect_layer_state(layer_t layer_state) const {
  131. test_logger.trace() << "Layer state: (" << +layer_state << ") Highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl;
  132. EXPECT_TRUE(layer_state_is(layer_state));
  133. }