Files
anvil/libraries/button/src/button_mock.h
2026-02-22 07:47:51 -06:00

61 lines
1.4 KiB
C++

#ifndef BUTTON_MOCK_H
#define BUTTON_MOCK_H
#include "button.h"
/*
* Button -- Test mock with programmable state.
*
* Use this in tests to control exactly what button state your
* application sees, without any real hardware.
*
* Example:
* ButtonMock btn;
* btn.setPressed(true);
*
* ToggleApp app(&sim, &btn);
* app.update();
*
* EXPECT_EQ(sim.getPin(LED_PIN), HIGH); // LED should be on
*
* You can also track how many times the button was read:
* btn.resetReadCount();
* app.update();
* EXPECT_EQ(btn.readCount(), 1);
*/
class ButtonMock : public Button {
public:
/// Set whether the button appears pressed.
void setPressed(bool pressed) {
pressed_ = pressed;
}
/// Set the raw digital state returned by readState().
void setRaw(int value) {
raw_ = value;
}
bool isPressed() override {
++read_count_;
return pressed_;
}
int readState() override {
++read_count_;
return raw_;
}
/// How many times has the button been read?
int readCount() const { return read_count_; }
/// Reset the read counter.
void resetReadCount() { read_count_ = 0; }
private:
bool pressed_ = false; // Default: not pressed
int raw_ = HIGH; // Default: HIGH (active-low, not pressed)
int read_count_ = 0;
};
#endif // BUTTON_MOCK_H