From 6c4c1b3d95ee87a6ae658500006baefc2bcd025d Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 26 Nov 2017 16:51:59 +0100 Subject: [PATCH] Unit test for widget_set_timer() --- unittest/mgos_mock.c | 8 +++++++- unittest/test_screen.c | 5 +++++ unittest/test_widget.c | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/unittest/mgos_mock.c b/unittest/mgos_mock.c index 9f91dbf..c07b361 100644 --- a/unittest/mgos_mock.c +++ b/unittest/mgos_mock.c @@ -3,6 +3,8 @@ #include "mgos_mock.h" +int _mgos_timers = 0; + int log_print_prefix(enum cs_log_level l, const char *func, const char *file) { char ll_str[6]; @@ -30,15 +32,19 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) { } mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_arg) { + _mgos_timers++; + LOG(LL_INFO, ("Installing timer -- %d timers currently installed)", _mgos_timers)); (void) msecs; (void) flags; (void) cb; (void) cb_arg; - return 0; + return _mgos_timers; } void mgos_clear_timer(mgos_timer_id id) { + _mgos_timers--; + LOG(LL_INFO, ("Clearing timer -- %d timers currently installed)", _mgos_timers)); (void) id; return; diff --git a/unittest/test_screen.c b/unittest/test_screen.c index 59aaf30..d015ba0 100644 --- a/unittest/test_screen.c +++ b/unittest/test_screen.c @@ -2,6 +2,8 @@ #include "screen.h" #include "widget.h" +extern int _mgos_timers; + static void test_screen_widget_add_and_remove(struct screen_t *s, const char *fn) { struct widget_t *w1; @@ -115,5 +117,8 @@ int test_screen() { screen_destroy(&s); ASSERT(!s, "Could not destroy screen"); + LOG(LL_INFO, ("ensure we have no timers")); + ASSERT(_mgos_timers==0, "timers found"); + return 0; } diff --git a/unittest/test_widget.c b/unittest/test_widget.c index a19dabf..e786ff5 100644 --- a/unittest/test_widget.c +++ b/unittest/test_widget.c @@ -1,6 +1,8 @@ #include "test.h" #include "widget.h" +extern int _mgos_timers; + static int test_widget_create_from_file(void) { struct widget_t *w; @@ -13,6 +15,14 @@ static int test_widget_create_from_file(void) { ASSERT(w->w == 48, "'x' field is invalid"); ASSERT(w->h == 48, "'x' field is invalid"); + LOG(LL_INFO, ("widget_set_timer()")); + widget_set_timer(w, 1000); + ASSERT(_mgos_timers==1, "timer not found"); + + LOG(LL_INFO, ("widget_delete_timer()")); + widget_delete_timer(w); + ASSERT(_mgos_timers==0, "timers found"); + fn = "data/TestWidget-invalid.json"; LOG(LL_INFO, ("widget_create_from_file(%s)", fn)); w = widget_create_from_file(fn); @@ -22,5 +32,9 @@ static int test_widget_create_from_file(void) { int test_widget() { test_widget_create_from_file(); + + LOG(LL_INFO, ("ensure we have no timers")); + ASSERT(_mgos_timers==0, "timers found"); + return 0; }