acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

TestHelper.h (3509B)

      1 /*
      2 MIT License
      3 
      4 Copyright (c) 2018 Brian T. Park
      5 
      6 Permission is hereby granted, free of charge, to any person obtaining a copy
      7 of this software and associated documentation files (the "Software"), to deal
      8 in the Software without restriction, including without limitation the rights
      9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10 copies of the Software, and to permit persons to whom the Software is
     11 furnished to do so, subject to the following conditions:
     12 
     13 The above copyright notice and this permission notice shall be included in all
     14 copies or substantial portions of the Software.
     15 
     16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22 SOFTWARE.
     23 */
     24 
     25 #ifndef ACE_BUTTON_TEST_HELPER_H
     26 #define ACE_BUTTON_TEST_HELPER_H
     27 
     28 #include <AceButton.h>
     29 #include "TestableButtonConfig.h"
     30 #include "EventTracker.h"
     31 
     32 namespace ace_button {
     33 namespace testing {
     34 
     35 /**
     36  * A wrapper class that sends emulated button presses and released to the the
     37  * underlying AceButton class, and captures the resulting events in the
     38  * provided EventTracker.
     39  */
     40 class TestHelper {
     41   public:
     42     TestHelper(
     43         TestableButtonConfig* testableConfig,
     44         AceButton* button,
     45         EventTracker* eventTracker):
     46       mTestableConfig(testableConfig),
     47       mButton(button),
     48       mEventTracker(eventTracker) {}
     49 
     50     /** Reinitilize to its pristine state. */
     51     void init(uint8_t pin, uint8_t defaultReleasedState, uint8_t id) {
     52       mPin = pin;
     53       mDefaultReleasedState = defaultReleasedState;
     54       mId = id;
     55       mButton->init(mPin, mDefaultReleasedState, mId);
     56       mTestableConfig->init();
     57       mTestableConfig->setButtonState(defaultReleasedState);
     58     }
     59 
     60     /**
     61      * Simulate a press of the button and run the button.check() processing. The
     62      * defaultReleasedState is determined by whether the button has a pullup
     63      * (HIGH) or pulldown (LOW) resistor.
     64      */
     65     void pressButton(unsigned long time) {
     66       uint8_t targetState = (HIGH == mDefaultReleasedState) ? LOW : HIGH;
     67       mTestableConfig->setClock(time);
     68       mTestableConfig->setButtonState(targetState);
     69       mEventTracker->clear();
     70       mButton->check();
     71     }
     72 
     73     /**
     74      * Simulate a release of the button and run the button.check() processing.
     75      */
     76     void releaseButton(unsigned long time) {
     77       uint8_t targetState = (HIGH == mDefaultReleasedState) ? HIGH : LOW;
     78       mTestableConfig->setClock(time);
     79       mTestableConfig->setButtonState(targetState);
     80       mEventTracker->clear();
     81       mButton->check();
     82     }
     83 
     84     /**
     85      * Simply move the time forward and check the button. No changes to button.
     86      */
     87     void checkTime(unsigned long time) {
     88       mTestableConfig->setClock(time);
     89       mEventTracker->clear();
     90       mButton->check();
     91     }
     92 
     93   private:
     94     // Disable copy-constructor and assignment operator
     95     TestHelper(const TestHelper&) = delete;
     96     TestHelper& operator=(const TestHelper&) = delete;
     97 
     98     TestableButtonConfig* mTestableConfig;
     99     AceButton* mButton;
    100     EventTracker* mEventTracker;
    101 
    102     uint8_t mPin;
    103     uint8_t mDefaultReleasedState;
    104     uint8_t mId;
    105 };
    106 
    107 }
    108 }
    109 #endif