encoder_tests_split_role.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 is_keyboard_left(void) {
  37. return isLeftHand;
  38. }
  39. bool encoder_update_kb(uint8_t index, bool clockwise) {
  40. if (!isMaster) {
  41. ADD_FAILURE() << "We shouldn't get here.";
  42. }
  43. num_updates++;
  44. return true;
  45. }
  46. bool setAndRead(pin_t pin, bool val) {
  47. setPin(pin, val);
  48. return encoder_task();
  49. }
  50. class EncoderSplitTestRole : public ::testing::Test {
  51. protected:
  52. void SetUp() override {
  53. num_updates = 0;
  54. for (int i = 0; i < 32; i++) {
  55. pinIsInputHigh[i] = 0;
  56. pins[i] = 0;
  57. }
  58. }
  59. };
  60. TEST_F(EncoderSplitTestRole, TestPrimaryLeft) {
  61. isMaster = true;
  62. isLeftHand = true;
  63. encoder_init();
  64. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  65. setAndRead(0, false);
  66. setAndRead(1, false);
  67. setAndRead(0, true);
  68. setAndRead(1, true);
  69. EXPECT_EQ(num_updates, 1); // one update received
  70. }
  71. TEST_F(EncoderSplitTestRole, TestPrimaryRight) {
  72. isMaster = true;
  73. isLeftHand = false;
  74. encoder_init();
  75. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  76. setAndRead(6, false);
  77. setAndRead(7, false);
  78. setAndRead(6, true);
  79. setAndRead(7, true);
  80. EXPECT_EQ(num_updates, 1); // one update received
  81. }
  82. TEST_F(EncoderSplitTestRole, TestNotPrimaryLeft) {
  83. isMaster = false;
  84. isLeftHand = true;
  85. encoder_init();
  86. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  87. setAndRead(0, false);
  88. setAndRead(1, false);
  89. setAndRead(0, true);
  90. setAndRead(1, true);
  91. EXPECT_EQ(num_updates, 0); // zero updates received
  92. }
  93. TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
  94. isMaster = false;
  95. isLeftHand = false;
  96. encoder_init();
  97. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  98. setAndRead(6, false);
  99. setAndRead(7, false);
  100. setAndRead(6, true);
  101. setAndRead(7, true);
  102. EXPECT_EQ(num_updates, 0); // zero updates received
  103. }