You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.7 KiB

  1. /* Copyright 2021 Stefan Kerkmann
  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 "keyboard_report_util.hpp"
  17. #include "keycode.h"
  18. #include "test_common.hpp"
  19. #include "action_tapping.h"
  20. #include "test_fixture.hpp"
  21. #include "test_keymap_key.hpp"
  22. using testing::_;
  23. using testing::InSequence;
  24. class TappingForceHold : public TestFixture {};
  25. TEST_F(TappingForceHold, tap_regular_key_while_mod_tap_key_is_held) {
  26. TestDriver driver;
  27. InSequence s;
  28. auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P));
  29. auto regular_key = KeymapKey(0, 2, 0, KC_A);
  30. set_keymap({mod_tap_hold_key, regular_key});
  31. /* Press mod-tap-hold key. */
  32. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  33. mod_tap_hold_key.press();
  34. run_one_scan_loop();
  35. testing::Mock::VerifyAndClearExpectations(&driver);
  36. /* Press regular key. */
  37. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  38. regular_key.press();
  39. run_one_scan_loop();
  40. testing::Mock::VerifyAndClearExpectations(&driver);
  41. /* Release regular key. */
  42. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  43. regular_key.release();
  44. run_one_scan_loop();
  45. testing::Mock::VerifyAndClearExpectations(&driver);
  46. /* Release mod-tap-hold key. */
  47. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  48. mod_tap_hold_key.release();
  49. run_one_scan_loop();
  50. testing::Mock::VerifyAndClearExpectations(&driver);
  51. /* Idle for tapping term of mod tap hold key. */
  52. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  53. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
  54. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  55. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  56. idle_for(TAPPING_TERM - 3);
  57. testing::Mock::VerifyAndClearExpectations(&driver);
  58. }
  59. TEST_F(TappingForceHold, tap_mod_tap_key_while_mod_tap_key_is_held) {
  60. TestDriver driver;
  61. InSequence s;
  62. auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P));
  63. auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A));
  64. set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key});
  65. /* Press first mod-tap-hold key */
  66. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  67. first_mod_tap_hold_key.press();
  68. run_one_scan_loop();
  69. testing::Mock::VerifyAndClearExpectations(&driver);
  70. /* Press second tap-hold key */
  71. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  72. second_mod_tap_hold_key.press();
  73. run_one_scan_loop();
  74. testing::Mock::VerifyAndClearExpectations(&driver);
  75. /* Release second tap-hold key */
  76. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  77. second_mod_tap_hold_key.release();
  78. run_one_scan_loop();
  79. testing::Mock::VerifyAndClearExpectations(&driver);
  80. /* Release first mod-tap-hold key */
  81. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  82. first_mod_tap_hold_key.release();
  83. run_one_scan_loop();
  84. testing::Mock::VerifyAndClearExpectations(&driver);
  85. /* Idle for tapping term of first mod tap hold key. */
  86. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  87. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
  88. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  89. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  90. idle_for(TAPPING_TERM - 3);
  91. testing::Mock::VerifyAndClearExpectations(&driver);
  92. }
  93. TEST_F(TappingForceHold, tap_regular_key_while_layer_tap_key_is_held) {
  94. TestDriver driver;
  95. InSequence s;
  96. auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P));
  97. auto regular_key = KeymapKey(0, 2, 0, KC_A);
  98. auto layer_key = KeymapKey(1, 2, 0, KC_B);
  99. set_keymap({layer_tap_hold_key, regular_key, layer_key});
  100. /* Press layer-tap-hold key */
  101. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  102. layer_tap_hold_key.press();
  103. run_one_scan_loop();
  104. testing::Mock::VerifyAndClearExpectations(&driver);
  105. /* Press regular key */
  106. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  107. regular_key.press();
  108. run_one_scan_loop();
  109. testing::Mock::VerifyAndClearExpectations(&driver);
  110. /* Release regular key */
  111. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  112. regular_key.release();
  113. run_one_scan_loop();
  114. testing::Mock::VerifyAndClearExpectations(&driver);
  115. /* Release layer-tap-hold key */
  116. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
  117. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A, KC_P)));
  118. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
  119. EXPECT_CALL(driver, send_keyboard_mock(_));
  120. layer_tap_hold_key.release();
  121. run_one_scan_loop();
  122. testing::Mock::VerifyAndClearExpectations(&driver);
  123. }
  124. TEST_F(TappingForceHold, tap_mod_tap_hold_key_two_times) {
  125. TestDriver driver;
  126. InSequence s;
  127. auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P));
  128. set_keymap({mod_tap_hold_key});
  129. /* Press mod-tap-hold key. */
  130. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  131. mod_tap_hold_key.press();
  132. run_one_scan_loop();
  133. testing::Mock::VerifyAndClearExpectations(&driver);
  134. /* Release mod-tap-hold key. */
  135. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
  136. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  137. mod_tap_hold_key.release();
  138. run_one_scan_loop();
  139. testing::Mock::VerifyAndClearExpectations(&driver);
  140. /* Press mod-tap-hold key again. */
  141. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  142. mod_tap_hold_key.press();
  143. run_one_scan_loop();
  144. testing::Mock::VerifyAndClearExpectations(&driver);
  145. /* Release mod-tap-hold key. */
  146. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
  147. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  148. mod_tap_hold_key.release();
  149. run_one_scan_loop();
  150. testing::Mock::VerifyAndClearExpectations(&driver);
  151. }
  152. TEST_F(TappingForceHold, tap_mod_tap_hold_key_twice_and_hold_on_second_time) {
  153. TestDriver driver;
  154. InSequence s;
  155. auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P));
  156. set_keymap({mod_tap_hold_key});
  157. /* Press mod-tap-hold key. */
  158. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  159. mod_tap_hold_key.press();
  160. run_one_scan_loop();
  161. testing::Mock::VerifyAndClearExpectations(&driver);
  162. /* Release mod-tap-hold key. */
  163. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
  164. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  165. mod_tap_hold_key.release();
  166. run_one_scan_loop();
  167. testing::Mock::VerifyAndClearExpectations(&driver);
  168. /* Press mod-tap-hold key again. */
  169. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  170. mod_tap_hold_key.press();
  171. idle_for(TAPPING_TERM);
  172. testing::Mock::VerifyAndClearExpectations(&driver);
  173. /* Release mod-tap-hold key. */
  174. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSHIFT)));
  175. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  176. mod_tap_hold_key.release();
  177. run_one_scan_loop();
  178. testing::Mock::VerifyAndClearExpectations(&driver);
  179. }