test: add click test for reset event

This commit is contained in:
Tilen
2026-09-12 14:58:28 +02:00
parent d2ff69fc11
commit 8bdf769403
4 changed files with 140 additions and 3 deletions
+12 -3
View File
@@ -1,18 +1,27 @@
/**
* \file test.h
* \author Tilen MAJERLE <tilen@majerle.eu>
* \brief
* \brief
* \version 0.1
* \date 2025-03-21
*
*
* @copyright Copyright (c) 2025
*
*
*/
#ifndef TEST_HDR_H
#define TEST_HDR_H
#include "lwbtn/lwbtn.h"
/* Test assert information */
#define TEST_ASSERT(condition) \
do { \
if (!(condition)) { \
printf("Assert failed on the line %d\r\n", __LINE__); \
rescode = -1; \
} \
} while (0)
int test_run(void);
#endif /* TEST_HDR_H */
+9
View File
@@ -0,0 +1,9 @@
# CMake include file
# Add more sources
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/test_lwbtn_reset.c
)
# Options file
set(LWBTN_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/lwbtn_opts.h)
+44
View File
@@ -0,0 +1,44 @@
/**
* \file lwbtn_opts.h
* \brief lwbtn configuration file
*/
/*
* Copyright (c) 2024 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwBTN - Lightweight button manager.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.3.2
*/
#ifndef LWBTN_HDR_OPTS_H
#define LWBTN_HDR_OPTS_H
/* Disable debounce completely, to keep event timing simple and deterministic */
#define LWBTN_CFG_TIME_DEBOUNCE_PRESS 0
#define LWBTN_CFG_TIME_DEBOUNCE_RELEASE 0
/* Drive input state manually from the test, instead of through a get-state callback */
#define LWBTN_CFG_GET_STATE_MODE LWBTN_GET_STATE_MODE_MANUAL
#endif /* LWBTN_HDR_OPTS_H */
+75
View File
@@ -0,0 +1,75 @@
#include <stdio.h>
#include "lwbtn/lwbtn.h"
#include "test.h"
/* Check the conditions for the test */
#if LWBTN_CFG_TIME_DEBOUNCE_PRESS > 0
#error "LWBTN_CFG_TIME_DEBOUNCE_PRESS must be 0 for this test"
#endif /* LWBTN_CFG_TIME_DEBOUNCE_PRESS > 0 */
#if LWBTN_CFG_TIME_DEBOUNCE_RELEASE > 0
#error "LWBTN_CFG_TIME_DEBOUNCE_RELEASE must be 0 for this test"
#endif /* LWBTN_CFG_TIME_DEBOUNCE_RELEASE > 0 */
#if !LWBTN_CFG_USE_CLICK
#error "LWBTN_CFG_USE_CLICK must be enabled for this test"
#endif /* !LWBTN_CFG_USE_CLICK */
/* Single button, driven manually (no get-state callback) */
static lwbtn_btn_t btn;
/* Counts how many ONCLICK events actually reach the application */
static uint16_t click_cnt;
/* Event callback -> just counts click events, no assertions here */
static void
prv_btn_event(struct lwbtn* lw, struct lwbtn_btn* b, lwbtn_evt_t evt) {
(void)lw;
(void)b;
if (evt == LWBTN_EVT_ONCLICK) {
++click_cnt;
}
}
/**
* \brief Test function
*
* Regression test for `lwbtn_reset()`: a click that is still "pending"
* (on-release already seen, but the multi-click timeout has not expired yet)
* must not be reported to the application once the button has been reset.
*/
int
test_run(void) {
int rescode = 0;
lwbtn_time_t mstime = 0;
lwbtn_init_ex(NULL, &btn, 1, NULL, prv_btn_event);
/* Start with the default state */
lwbtn_set_btn_state(&btn, 0);
lwbtn_process(mstime++);
/* Press and release within valid click timing -> click is now pending (cnt == 1) */
lwbtn_set_btn_state(&btn, 1);
lwbtn_process(mstime++);
mstime += LWBTN_CFG_TIME_CLICK_MIN;
lwbtn_set_btn_state(&btn, 0);
lwbtn_process(mstime++);
printf("Pending click count before reset: %u (expected 1)\r\n", lwbtn_click_get_count(&btn));
TEST_ASSERT(lwbtn_click_get_count(&btn) == 1);
/* Reset the button right in the middle of the pending click sequence */
lwbtn_reset(NULL, &btn);
printf("Pending click count right after reset: %u (expected 0)\r\n", lwbtn_click_get_count(&btn));
TEST_ASSERT(lwbtn_click_get_count(&btn) == 0);
/* Advance well past the multi-click timeout, button left idle -> no ONCLICK must ever fire */
for (lwbtn_time_t i = 0; i < (lwbtn_time_t)LWBTN_CFG_TIME_CLICK_MULTI_MAX + 10; ++i) {
lwbtn_process(mstime++);
}
printf("ONCLICK events fired after reset: %u (expected 0)\r\n", click_cnt);
TEST_ASSERT(click_cnt == 0);
return rescode;
}