// // Created by erki on 17.04.21. // #include #include "peripherals_button.hpp" namespace { struct HAL { HAL() { millis = 1; } ~HAL() { millis = 1; } static std::uint32_t millis; static std::uint32_t getMillis() { return millis; } }; std::uint32_t HAL::millis = 1; struct Gpio { static bool is_set; Gpio() { is_set = false; } bool read() const { return is_set; } void set(const bool value) {} void toggle() {} }; bool Gpio::is_set = false; }// namespace using Button = Peripherals::Button; TEST_CASE("Button reads no press as NOT_PRESSED.", "[peripherals],[button]") { Gpio g; HAL hal; Button button{g}; button.update(); HAL::millis = Button::TIMEOUT_LONG_PRESS + 10; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED); } TEST_CASE("Button reads presses properly.", "[peripherals],[button]") { Gpio g; HAL hal; Button button{g}; Gpio::is_set = true; button.update(); SECTION("Too short of a press is debounced.") { HAL::millis = Button::TIMEOUT_SHORT_PRESS - 1; Gpio::is_set = false; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED); } SECTION("Short press is identified properly.") { HAL::millis = Button::TIMEOUT_SHORT_PRESS + 2; Gpio::is_set = false; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::SHORT_PRESS); SECTION("Next state is NOT_PRESSED.") { HAL::millis += 1; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED); } } SECTION("Long press is identified properly.") { HAL::millis = Button::TIMEOUT_LONG_PRESS + 2; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::LONG_PRESS); SECTION("Next state is NOT_PRESSED.") { HAL::millis += 1; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED); SECTION("Releasing keeps NOT_PRESSED state.") { HAL::millis += 1; Gpio::is_set = false; button.update(); REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED); } } } }