qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

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