AceButton  1.0.0
An Adjustable Compact Event-driven (ACE) button library for Arduino.
TestHelper.h
1 /*
2  Copyright 2018 Brian T. Park
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef TEST_HELPER_H
18 #define TEST_HELPER_H
19 
20 #include <AceButton.h>
21 #include <testing/TestableButtonConfig.h>
22 #include <testing/EventTracker.h>
23 
24 namespace ace_button {
25 namespace testing {
26 
32 class TestHelper {
33  public:
34  TestHelper(
35  TestableButtonConfig* testableConfig,
36  AceButton* button,
37  EventTracker* eventTracker):
38  mTestableConfig(testableConfig),
39  mButton(button),
40  mEventTracker(eventTracker) {}
41 
43  void init(uint8_t pin, uint8_t defaultReleasedState, uint8_t id) {
44  mPin = pin;
45  mDefaultReleasedState = defaultReleasedState;
46  mId = id;
47  mButton->init(mPin, mDefaultReleasedState, mId);
48  mTestableConfig->init();
49  mTestableConfig->setButtonState(defaultReleasedState);
50  }
51 
57  void pressButton(unsigned long time) {
58  uint8_t targetState = (HIGH == mDefaultReleasedState) ? LOW : HIGH;
59  mTestableConfig->setClock(time);
60  mTestableConfig->setButtonState(targetState);
61  mEventTracker->clear();
62  mButton->check();
63  }
64 
68  void releaseButton(unsigned long time) {
69  uint8_t targetState = (HIGH == mDefaultReleasedState) ? HIGH : LOW;
70  mTestableConfig->setClock(time);
71  mTestableConfig->setButtonState(targetState);
72  mEventTracker->clear();
73  mButton->check();
74  }
75 
79  void checkTime(unsigned long time) {
80  mTestableConfig->setClock(time);
81  mEventTracker->clear();
82  mButton->check();
83  }
84 
85  private:
86  // Disable copy-constructor and assignment operator
87  TestHelper(const TestHelper&) = delete;
88  TestHelper& operator=(const TestHelper&) = delete;
89 
90  TestableButtonConfig* mTestableConfig;
91  AceButton* mButton;
92  EventTracker* mEventTracker;
93 
94  uint8_t mPin;
95  uint8_t mDefaultReleasedState;
96  uint8_t mId;
97 };
98 
99 }
100 }
101 #endif
void releaseButton(unsigned long time)
Simulate a release of the button and run the button.check() processing.
Definition: TestHelper.h:68
A subclass of ButtonConfig which overrides getClock() and readButton() so that their values can be co...
Definition: TestableButtonConfig.h:30
A wrapper class that sends emulated button presses and released to the the underlying AceButton class...
Definition: TestHelper.h:32
void setClock(unsigned long millis)
Set the time of the fake clock.
Definition: TestableButtonConfig.h:56
void checkTime(unsigned long time)
Simply move the time forward and check the button.
Definition: TestHelper.h:79
Class that can receive and remember multiple calls to the eventHandler from AceButton.
Definition: EventTracker.h:57
Definition: AceButton.h:23
void setButtonState(int buttonState)
Set the state of the fake physical button.
Definition: TestableButtonConfig.h:59
virtual void init() override
Initialize to its pristine state.
Definition: TestableButtonConfig.h:43
void init(uint8_t pin=0, uint8_t defaultReleasedState=HIGH, uint8_t id=0)
Reset the button to the initial constructed state.
Definition: AceButton.cpp:35
void check()
Check state of button and trigger event processing.
Definition: AceButton.cpp:59
void init(uint8_t pin, uint8_t defaultReleasedState, uint8_t id)
Reinitilize to its pristine state.
Definition: TestHelper.h:43
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:42
void pressButton(unsigned long time)
Simulate a press of the button and run the button.check() processing.
Definition: TestHelper.h:57