logo

qmk_firmware

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

encoder_tests_split_role.cpp (3030B)


  1. /* Copyright 2021 Balz Guenat
  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 "gtest/gtest.h"
  17. #include "gmock/gmock.h"
  18. #include <vector>
  19. #include <algorithm>
  20. #include <stdio.h>
  21. extern "C" {
  22. #include "encoder.h"
  23. #include "keyboard.h"
  24. #include "encoder/tests/mock_split.h"
  25. }
  26. struct update {
  27. int8_t index;
  28. bool clockwise;
  29. };
  30. uint8_t num_updates = 0;
  31. bool isMaster;
  32. bool isLeftHand;
  33. bool is_keyboard_master(void) {
  34. return isMaster;
  35. }
  36. bool encoder_update_kb(uint8_t index, bool clockwise) {
  37. if (!isMaster) {
  38. ADD_FAILURE() << "We shouldn't get here.";
  39. }
  40. num_updates++;
  41. return true;
  42. }
  43. bool setAndRead(pin_t pin, bool val) {
  44. setPin(pin, val);
  45. return encoder_task();
  46. }
  47. class EncoderSplitTestRole : public ::testing::Test {
  48. protected:
  49. void SetUp() override {
  50. num_updates = 0;
  51. for (int i = 0; i < 32; i++) {
  52. pinIsInputHigh[i] = 0;
  53. pins[i] = 0;
  54. }
  55. }
  56. };
  57. TEST_F(EncoderSplitTestRole, TestPrimaryLeft) {
  58. isMaster = true;
  59. isLeftHand = true;
  60. encoder_init();
  61. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  62. setAndRead(0, false);
  63. setAndRead(1, false);
  64. setAndRead(0, true);
  65. setAndRead(1, true);
  66. EXPECT_EQ(num_updates, 1); // one update received
  67. }
  68. TEST_F(EncoderSplitTestRole, TestPrimaryRight) {
  69. isMaster = true;
  70. isLeftHand = false;
  71. encoder_init();
  72. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  73. setAndRead(6, false);
  74. setAndRead(7, false);
  75. setAndRead(6, true);
  76. setAndRead(7, true);
  77. EXPECT_EQ(num_updates, 1); // one update received
  78. }
  79. TEST_F(EncoderSplitTestRole, TestNotPrimaryLeft) {
  80. isMaster = false;
  81. isLeftHand = true;
  82. encoder_init();
  83. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  84. setAndRead(0, false);
  85. setAndRead(1, false);
  86. setAndRead(0, true);
  87. setAndRead(1, true);
  88. EXPECT_EQ(num_updates, 0); // zero updates received
  89. }
  90. TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
  91. isMaster = false;
  92. isLeftHand = false;
  93. encoder_init();
  94. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  95. setAndRead(6, false);
  96. setAndRead(7, false);
  97. setAndRead(6, true);
  98. setAndRead(7, true);
  99. EXPECT_EQ(num_updates, 0); // zero updates received
  100. }