encoder_tests_split_role.cpp 3.1 KB

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