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

TestableButtonConfig.h (2729B)

      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_TESTABLE_BUTTON_CONFIG_H
     26 #define ACE_BUTTON_TESTABLE_BUTTON_CONFIG_H
     27 
     28 #include "../ButtonConfig.h"
     29 
     30 namespace ace_button {
     31 namespace testing {
     32 
     33 /**
     34  * A subclass of ButtonConfig which overrides getClock() and readButton() so
     35  * that their values can be controlled manually. This is intended to be used for
     36  * unit testing.
     37  */
     38 class TestableButtonConfig: public ButtonConfig {
     39   public:
     40     TestableButtonConfig():
     41         mMillis(0),
     42         mButtonState(HIGH) {}
     43 
     44     /**
     45      * Initialize to its pristine state. This method is needed because
     46      * ArduinoUnit does not create a new instance of the Test class for each
     47      * test case, so we have to reuse objects between test cases, so we need a
     48      * way to reinitialize this object to its pristine state just after
     49      * construction.
     50      */
     51     void init() override {
     52       ButtonConfig::init();
     53       mMillis = 0;
     54       mButtonState = HIGH;
     55     }
     56 
     57     /** Read the time of the fake clock. */
     58     unsigned long getClock() override { return mMillis; }
     59 
     60     /** Read the fake physical button. */
     61     int readButton(uint8_t /* pin */) override { return mButtonState; }
     62 
     63     /** Set the time of the fake clock. */
     64     void setClock(unsigned long millis) { mMillis = millis; }
     65 
     66     /** Set the state of the fake physical button. */
     67     void setButtonState(int buttonState) { mButtonState = buttonState; }
     68 
     69   private:
     70     // Disable copy-constructor and assignment operator
     71     TestableButtonConfig(const TestableButtonConfig&) = delete;
     72     TestableButtonConfig& operator=(const TestableButtonConfig&) = delete;
     73 
     74     unsigned long mMillis;
     75     int mButtonState;
     76 };
     77 
     78 }
     79 }
     80 #endif