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.

240 lines
8.5 KiB

  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. #include "test_common.hpp"
  17. using testing::_;
  18. using testing::InSequence;
  19. using testing::Return;
  20. class KeyPress : public TestFixture {};
  21. TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) {
  22. TestDriver driver;
  23. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  24. keyboard_task();
  25. }
  26. TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) {
  27. TestDriver driver;
  28. press_key(0, 0);
  29. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
  30. keyboard_task();
  31. release_key(0, 0);
  32. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  33. keyboard_task();
  34. }
  35. TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) {
  36. TestDriver driver;
  37. press_key(1, 0);
  38. press_key(0, 3);
  39. // Note that QMK only processes one key at a time
  40. // See issue #1476 for more information
  41. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B)));
  42. keyboard_task();
  43. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B, KC_C)));
  44. keyboard_task();
  45. release_key(1, 0);
  46. release_key(0, 3);
  47. // Note that the first key released is the first one in the matrix order
  48. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_C)));
  49. keyboard_task();
  50. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  51. keyboard_task();
  52. }
  53. TEST_F(KeyPress, ANonMappedKeyDoesNothing) {
  54. TestDriver driver;
  55. press_key(2, 0);
  56. EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
  57. keyboard_task();
  58. keyboard_task();
  59. }
  60. TEST_F(KeyPress, LeftShiftIsReportedCorrectly) {
  61. TestDriver driver;
  62. press_key(3, 0);
  63. press_key(0, 0);
  64. // Unfortunately modifiers are also processed in the wrong order
  65. // See issue #1476 for more information
  66. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
  67. keyboard_task();
  68. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A, KC_LSFT)));
  69. keyboard_task();
  70. release_key(0, 0);
  71. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  72. keyboard_task();
  73. release_key(3, 0);
  74. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  75. keyboard_task();
  76. }
  77. TEST_F(KeyPress, PressLeftShiftAndControl) {
  78. TestDriver driver;
  79. press_key(3, 0);
  80. press_key(5, 0);
  81. // Unfortunately modifiers are also processed in the wrong order
  82. // See issue #1476 for more information
  83. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  84. keyboard_task();
  85. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_LCTRL)));
  86. keyboard_task();
  87. }
  88. TEST_F(KeyPress, LeftAndRightShiftCanBePressedAtTheSameTime) {
  89. TestDriver driver;
  90. press_key(3, 0);
  91. press_key(4, 0);
  92. // Unfortunately modifiers are also processed in the wrong order
  93. // See issue #1476 for more information
  94. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  95. keyboard_task();
  96. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_RSFT)));
  97. keyboard_task();
  98. }
  99. TEST_F(KeyPress, RightShiftLeftControlAndCharWithTheSameKey) {
  100. TestDriver driver;
  101. press_key(6, 0);
  102. // BUG: The press is split into two reports
  103. // BUG: It reports RSFT instead of LSFT
  104. // See issue #524 for more information
  105. // The underlying cause is that we use only one bit to represent the right hand
  106. // modifiers.
  107. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL)));
  108. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL, KC_O)));
  109. keyboard_task();
  110. release_key(6, 0);
  111. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL)));
  112. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  113. keyboard_task();
  114. }
  115. TEST_F(KeyPress, PressPlusEqualReleaseBeforePress) {
  116. TestDriver driver;
  117. InSequence s;
  118. press_key(1, 1); // KC_PLUS
  119. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  120. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
  121. run_one_scan_loop();
  122. testing::Mock::VerifyAndClearExpectations(&driver);
  123. release_key(1, 1); // KC_PLUS
  124. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  125. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  126. run_one_scan_loop();
  127. testing::Mock::VerifyAndClearExpectations(&driver);
  128. press_key(0, 1); // KC_EQL
  129. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
  130. run_one_scan_loop();
  131. testing::Mock::VerifyAndClearExpectations(&driver);
  132. release_key(0, 1); // KC_EQL
  133. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  134. run_one_scan_loop();
  135. testing::Mock::VerifyAndClearExpectations(&driver);
  136. }
  137. TEST_F(KeyPress, PressPlusEqualDontReleaseBeforePress) {
  138. TestDriver driver;
  139. InSequence s;
  140. press_key(1, 1); // KC_PLUS
  141. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  142. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
  143. run_one_scan_loop();
  144. testing::Mock::VerifyAndClearExpectations(&driver);
  145. press_key(0, 1); // KC_EQL
  146. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  147. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
  148. run_one_scan_loop();
  149. testing::Mock::VerifyAndClearExpectations(&driver);
  150. release_key(1, 1); // KC_PLS
  151. // BUG: Should really still return KC_EQL, but this is fine too
  152. // It's also called twice for some reason
  153. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
  154. run_one_scan_loop();
  155. testing::Mock::VerifyAndClearExpectations(&driver);
  156. release_key(0, 1); // KC_EQL
  157. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  158. run_one_scan_loop();
  159. testing::Mock::VerifyAndClearExpectations(&driver);
  160. }
  161. TEST_F(KeyPress, PressEqualPlusReleaseBeforePress) {
  162. TestDriver driver;
  163. InSequence s;
  164. press_key(0, 1); // KC_EQL
  165. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
  166. run_one_scan_loop();
  167. testing::Mock::VerifyAndClearExpectations(&driver);
  168. release_key(0, 1); // KQ_EQL
  169. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  170. run_one_scan_loop();
  171. testing::Mock::VerifyAndClearExpectations(&driver);
  172. press_key(1, 1); // KC_PLUS
  173. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  174. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
  175. run_one_scan_loop();
  176. testing::Mock::VerifyAndClearExpectations(&driver);
  177. release_key(1, 1); // KC_PLUS
  178. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  179. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  180. run_one_scan_loop();
  181. testing::Mock::VerifyAndClearExpectations(&driver);
  182. }
  183. TEST_F(KeyPress, PressEqualPlusDontReleaseBeforePress) {
  184. TestDriver driver;
  185. InSequence s;
  186. press_key(0, 1); // KC_EQL
  187. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
  188. run_one_scan_loop();
  189. testing::Mock::VerifyAndClearExpectations(&driver);
  190. press_key(1, 1); // KC_PLUS
  191. // BUG: The sequence is a bit strange, but it works, the end result is that
  192. // KC_PLUS is sent
  193. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
  194. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  195. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
  196. run_one_scan_loop();
  197. testing::Mock::VerifyAndClearExpectations(&driver);
  198. release_key(0, 1); // KC_EQL
  199. // I guess it's fine to still report shift here
  200. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  201. run_one_scan_loop();
  202. testing::Mock::VerifyAndClearExpectations(&driver);
  203. release_key(1, 1); // KC_PLUS
  204. // This report is not needed
  205. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
  206. EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
  207. run_one_scan_loop();
  208. testing::Mock::VerifyAndClearExpectations(&driver);
  209. }