Skip to content

Commit

Permalink
fix: short press is triggered on up after long press
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 23, 2023
1 parent 16ebfff commit bdaa92f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 5 additions & 1 deletion lib/Domain/Contactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void Contactor::checkForLongPress(long now) {
if ((downStartTime > 0) && ((now - downStartTime) >= LONG_PRESS)) {
downStartTime = 0;
onLongPress();
inALongPress = true;
}
}
void Contactor::onDown(long now) {
Expand All @@ -30,8 +31,11 @@ void Contactor::onDown(long now) {

void Contactor::onUp(long now) {
currentState = State::Up;
onShortPress();
checkForLongPress(now);
if (!inALongPress) {
onShortPress();
}
inALongPress = false;
downStartTime = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Domain/Contactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class Contactor {

static const int LONG_PRESS = 2000;
long downStartTime = 0;
bool inALongPress = false;

public:

enum Event { NoEvent, Pressed, Released };
enum State { Up, Down };


/**
* @brief Event can be detected in an interrupt, and processed later
*
Expand Down
35 changes: 26 additions & 9 deletions test/noarch/test_contactor/contactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@ void expect_short_press_to_be_evaluated_on_contactor_up() {

void expect_long_press_to_be_evaluated_on_contactor_up() {

Spy(Method(spy, onShortPress), Method(spy, onLongPress), Method(spy, checkForLongPress));
Spy(Method(spy, onShortPress), Method(spy, onLongPress),
Method(spy, checkForLongPress));
Contactor &sut = spy.get();

long now = 1;

sut.onDown(now);
sut.onUp(now + 2002);

Verify(Method(spy, onShortPress)).Once();
Verify(Method(spy, checkForLongPress)).Once();
Verify(Method(spy, onLongPress)).Once();
}

void expect_long_press_to_not_be_called_on_up_in_a_too_short_time() {

Spy(Method(spy, onShortPress), Method(spy, onLongPress), Method(spy, checkForLongPress));
Spy(Method(spy, onShortPress), Method(spy, onLongPress),
Method(spy, checkForLongPress));
Contactor &sut = spy.get();

long now = 1;
Expand All @@ -71,21 +72,35 @@ void expect_long_press_to_not_be_called_on_up_in_a_too_short_time() {
Verify(Method(spy, onLongPress)).Exactly(0);
}

void expect_long_press_to_not_be_called_on_up_in_a_sufficient_time() {
void expect_long_press_to_be_called_on_up_in_a_sufficient_time() {

Spy(Method(spy, onShortPress), Method(spy, onLongPress), Method(spy, checkForLongPress));
Spy(Method(spy, onShortPress), Method(spy, onLongPress),
Method(spy, checkForLongPress));
Contactor &sut = spy.get();

long now = 1;

sut.onDown(now);
sut.onUp(now + 2500);

Verify(Method(spy, onShortPress)).Once();
Verify(Method(spy, checkForLongPress)).Once();
Verify(Method(spy, onLongPress)).Once();
}

void expect_long_press_to_not_trigger_short_press_on_release() {

Spy(Method(spy, onShortPress), Method(spy, onLongPress),
Method(spy, checkForLongPress));
Contactor &sut = spy.get();

long now = 1;

sut.onDown(now);
sut.onUp(now + 2500);

Verify(Method(spy, onShortPress)).Exactly(0);
}

void expect_long_press_to_not_evaluate_if_button_is_not_down() {

Spy(Method(spy, onShortPress), Method(spy, onLongPress));
Expand All @@ -94,7 +109,7 @@ void expect_long_press_to_not_evaluate_if_button_is_not_down() {
long now = 1;

sut.onDown(now);
sut.onUp(now+100);
sut.onUp(now + 100);

sut.checkForLongPress(now + 3000);

Expand All @@ -116,15 +131,17 @@ void expect_long_press_to_be_evaluatable_while_contactor_is_down() {
Verify(Method(spy, onLongPress)).Once();
}



int main(int, char **) {
UNITY_BEGIN();
RUN_TEST(expect_short_press_to_be_evaluated_on_contactor_up);
RUN_TEST(expect_long_press_to_be_evaluated_on_contactor_up);
RUN_TEST(expect_long_press_to_not_be_called_on_up_in_a_too_short_time);
RUN_TEST(expect_long_press_to_not_be_called_on_up_in_a_sufficient_time);
RUN_TEST(expect_long_press_to_be_called_on_up_in_a_sufficient_time);
RUN_TEST(expect_long_press_to_not_trigger_short_press_on_release);
RUN_TEST(expect_long_press_to_not_evaluate_if_button_is_not_down);
RUN_TEST(expect_long_press_to_be_evaluatable_while_contactor_is_down);
//RUN_TEST(expect_short_press_to_not_trigger);

return UNITY_END();
}

0 comments on commit bdaa92f

Please sign in to comment.