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.

123 lines
3.1 KiB

  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_read();
  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. uint8_t slave_state[32] = {0};
  78. encoder_state_raw(slave_state);
  79. EXPECT_EQ(num_updates, 1); // one update received
  80. }
  81. TEST_F(EncoderSplitTestRole, TestNotPrimaryLeft) {
  82. isMaster = false;
  83. isLeftHand = true;
  84. encoder_init();
  85. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  86. setAndRead(0, false);
  87. setAndRead(1, false);
  88. setAndRead(0, true);
  89. setAndRead(1, true);
  90. EXPECT_EQ(num_updates, 0); // zero updates received
  91. }
  92. TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
  93. isMaster = false;
  94. isLeftHand = false;
  95. encoder_init();
  96. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  97. setAndRead(6, false);
  98. setAndRead(7, false);
  99. setAndRead(6, true);
  100. setAndRead(7, true);
  101. uint8_t slave_state[32] = {0};
  102. encoder_state_raw(slave_state);
  103. EXPECT_EQ(num_updates, 0); // zero updates received
  104. }