diff options
author | Daniel Wagner <daniel.wagner@bmw-carit.de> | 2013-07-16 13:12:45 +0200 |
---|---|---|
committer | Daniel Wagner <daniel.wagner@bmw-carit.de> | 2013-07-23 09:03:34 +0200 |
commit | c190e462eba30b57586d37a4e30605ca205f0b1d (patch) | |
tree | 385917fb62f14523f7061e03e181ec8fef01da6e /tools | |
parent | adce56ffbb2580b55680e93a45d38d23dff652ce (diff) | |
download | connman-c190e462eba30b57586d37a4e30605ca205f0b1d.tar.gz |
session-test: Pass in struct test_fix into tests
Instead of using GSourceFunc signature test function signature
we struct test_fix directly. This is done by wrapping
the setup, test and teardown function inside session-utils.c.
That allows us also to get rid of the g_main_loop_run() dance during
setup and teardown. This is all done in session-utils.c.
In preperation to get rid of the gbooleans.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/session-test.c | 86 | ||||
-rw-r--r-- | tools/session-test.h | 43 | ||||
-rw-r--r-- | tools/session-utils.c | 121 |
3 files changed, 120 insertions, 130 deletions
diff --git a/tools/session-test.c b/tools/session-test.c index ac918947..04389b90 100644 --- a/tools/session-test.c +++ b/tools/session-test.c @@ -58,9 +58,8 @@ static struct test_session *get_session(struct test_session *session, return &session->fix->session[index]; } -static gboolean test_session_create_no_notify(gpointer data) +static void test_session_create_no_notify(struct test_fix *fix) { - struct test_fix *fix = data; DBusMessage *msg; util_session_create(fix, 1); @@ -73,13 +72,10 @@ static gboolean test_session_create_no_notify(gpointer data) dbus_message_unref(msg); util_idle_call(fix, util_quit_loop, util_session_destroy); - - return FALSE; } -static gboolean test_session_destroy_no_notify(gpointer data) +static void test_session_destroy_no_notify(struct test_fix *fix) { - struct test_fix *fix = data; DBusMessage *msg; util_session_create(fix, 1); @@ -88,8 +84,6 @@ static gboolean test_session_destroy_no_notify(gpointer data) g_assert(msg == NULL); util_idle_call(fix, util_quit_loop, util_session_destroy); - - return FALSE; } static void test_session_create_notify(struct test_session *session) @@ -99,9 +93,8 @@ static void test_session_create_notify(struct test_session *session) util_idle_call(session->fix, util_quit_loop, util_session_destroy); } -static gboolean test_session_create(gpointer data) +static void test_session_create(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; DBusMessage *msg; int err; @@ -122,13 +115,10 @@ static gboolean test_session_create(gpointer data) g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); dbus_message_unref(msg); - - return FALSE; } -static gboolean test_session_create_destroy(gpointer data) +static void test_session_create_destroy(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; util_session_create(fix, 1); @@ -140,13 +130,10 @@ static gboolean test_session_create_destroy(gpointer data) util_session_cleanup(fix->session); util_idle_call(fix, util_quit_loop, util_session_destroy); - - return FALSE; } -static gboolean test_session_create_already_exists(gpointer data) +static void test_session_create_already_exists(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session0, *session1; DBusMessage *msg; @@ -167,8 +154,6 @@ static gboolean test_session_create_already_exists(gpointer data) util_session_cleanup(session0); util_idle_call(fix, util_quit_loop, util_session_destroy); - - return FALSE; } static void test_session_create_many_notify(struct test_session *session) @@ -187,9 +172,8 @@ static void test_session_create_many_notify(struct test_session *session) util_idle_call(session->fix, util_quit_loop, util_session_destroy); } -static gboolean test_session_create_many(gpointer data) +static void test_session_create_many(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; unsigned int i, max; @@ -207,8 +191,6 @@ static gboolean test_session_create_many(gpointer data) util_session_init(session); } - - return FALSE; } static void set_session_mode(struct test_fix *fix, @@ -235,9 +217,8 @@ static void test_session_connect_notify(struct test_session *session) util_idle_call(session->fix, util_quit_loop, util_session_destroy); } -static gboolean test_session_connect(gpointer data) +static void test_session_connect(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; DBusMessage *msg; @@ -253,8 +234,6 @@ static gboolean test_session_connect(gpointer data) g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); dbus_message_unref(msg); - - return FALSE; } static void test_session_disconnect_notify(struct test_session *session) @@ -269,9 +248,8 @@ static void test_session_disconnect_notify(struct test_session *session) util_idle_call(session->fix, util_quit_loop, util_session_destroy); } -static gboolean test_session_disconnect(gpointer data) +static void test_session_disconnect(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; DBusMessage *msg; @@ -285,8 +263,6 @@ static gboolean test_session_disconnect(gpointer data) msg = session_disconnect(session->connection, session); g_assert(msg != NULL); dbus_message_unref(msg); - - return FALSE; } static void test_session_connect_disconnect_notify(struct test_session *session) @@ -342,9 +318,8 @@ static void test_session_connect_disconnect_notify(struct test_session *session) } } -static gboolean test_session_connect_disconnect(gpointer data) +static void test_session_connect_disconnect(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; /* @@ -374,8 +349,6 @@ static gboolean test_session_connect_disconnect(gpointer data) util_session_init(session); set_session_state(session, TEST_SESSION_STATE_0); - - return FALSE; } static void test_session_connect_free_ride_notify(struct test_session *session) @@ -454,9 +427,8 @@ static void test_session_connect_free_ride_notify(struct test_session *session) } } -static gboolean test_session_connect_free_ride(gpointer data) +static void test_session_connect_free_ride(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session0, *session1; /* @@ -496,8 +468,6 @@ static gboolean test_session_connect_free_ride(gpointer data) util_session_init(session1); set_session_state(session0, TEST_SESSION_STATE_0); - - return FALSE; } static void policy_save(GKeyFile *keyfile, char *pathname) @@ -605,9 +575,8 @@ static void test_session_policy_notify(struct test_session *session) } } -static gboolean test_session_policy(gpointer data) +static void test_session_policy(struct test_fix *fix) { - struct test_fix *fix = data; struct test_session *session; /* @@ -639,8 +608,6 @@ static gboolean test_session_policy(gpointer data) util_session_init(session); set_session_state(session, TEST_SESSION_STATE_0); - - return FALSE; } static connman_bool_t is_online(struct test_fix *fix) @@ -651,59 +618,38 @@ static connman_bool_t is_online(struct test_fix *fix) return FALSE; } -static gboolean enable_session_mode(gpointer data) +static void enable_session_mode(struct test_fix *fix) { - struct test_fix *fix = data; - set_session_mode(fix, TRUE); if (is_online(fix) == FALSE) util_idle_call(fix, util_quit_loop, NULL); - - return FALSE; } -static gboolean manager_state_changed(gpointer data) +static void manager_state_changed(struct test_fix *fix) { - struct test_fix *fix = data; - if (is_online(fix) == FALSE) { fix->manager_changed = NULL; util_idle_call(fix, util_quit_loop, NULL); } - - return FALSE; } -static gboolean disable_session_mode(gpointer data) +static void disable_session_mode(struct test_fix *fix) { - struct test_fix *fix = data; - set_session_mode(fix, FALSE); - - return FALSE; } -static void setup_cb(struct test_fix *fix, gconstpointer data) +static void setup_cb(struct test_fix *fix) { fix->manager_changed = manager_state_changed; - util_setup(fix, data); util_call(fix, enable_session_mode, NULL); - - g_main_loop_run(fix->main_loop); - - fix->manager_changed = NULL; } -static void teardown_cb(struct test_fix *fix, gconstpointer data) +static void teardown_cb(struct test_fix *fix) { util_call(fix, disable_session_mode, NULL); util_idle_call(fix, util_quit_loop, NULL); - - g_main_loop_run(fix->main_loop); - - util_teardown(fix, data); } int main(int argc, char *argv[]) diff --git a/tools/session-test.h b/tools/session-test.h index a21f5269..be313f33 100644 --- a/tools/session-test.h +++ b/tools/session-test.h @@ -26,50 +26,47 @@ #include "../src/connman.h" struct test_session; +struct test_fix; struct test_manager { char *state; }; +typedef void (* util_test_func_t) (struct test_fix *fix); + struct test_fix { gpointer user_data; GMainLoop *main_loop; DBusConnection *main_connection; - guint watch; - guint manager_watch; + unsigned int watch; + unsigned int manager_watch; struct test_manager manager; - GSourceFunc manager_changed; + util_test_func_t manager_changed; /* session test cases */ unsigned int max_sessions; struct test_session *session; }; -/* utils.c */ -typedef void (* util_test_setup_cb) (struct test_fix *fix, - gconstpointer data); -typedef void (* util_test_teardown_cb) (struct test_fix *fix, - gconstpointer data); - -gboolean util_quit_loop(gpointer fix); -guint util_idle_call(struct test_fix *fix, GSourceFunc func, - GDestroyNotify notify); -guint util_call(struct test_fix *fix, GSourceFunc func, - GDestroyNotify notify); -void util_test_add(const char *test_name, GSourceFunc test_func, - util_test_setup_cb setup_cb, - util_test_teardown_cb teardown_cb); -void util_setup(struct test_fix *fix, gconstpointer data); -void util_teardown(struct test_fix *fix, gconstpointer data); +void util_quit_loop(struct test_fix *fix); +void util_idle_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy); +void util_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy); +void util_test_add(const char *test_name, util_test_func_t test, + util_test_func_t setup, + util_test_func_t teardown); +void util_setup(struct test_fix *fix); +void util_teardown(struct test_fix *fix); void util_session_create(struct test_fix *fix, unsigned int max_sessions); -void util_session_destroy(gpointer fix); +void util_session_destroy(struct test_fix *fix); void util_session_init(struct test_session *session); void util_session_cleanup(struct test_session *session); -typedef void (* notify_cb) (struct test_session *session); +typedef void (* notify_func_t) (struct test_session *session); enum connman_session_state { CONNMAN_SESSION_STATE_DISCONNECTED = 0, @@ -88,14 +85,14 @@ struct test_session_info { }; struct test_session { - gpointer user_data; + void *user_data; struct test_fix *fix; DBusConnection *connection; char *session_path; char *notify_path; - notify_cb notify; + notify_func_t notify; struct test_session_info *info; }; diff --git a/tools/session-utils.c b/tools/session-utils.c index 6a3d9918..1866acb8 100644 --- a/tools/session-utils.c +++ b/tools/session-utils.c @@ -27,32 +27,66 @@ #include <gdbus.h> +#include "../src/shared/util.h" #include "session-test.h" #define ENABLE_WRAPPER 1 #define PROPERTY_CHANGED "PropertyChanged" -gboolean util_quit_loop(gpointer data) +void util_quit_loop(struct test_fix *fix) { - struct test_fix *fix = data; - g_main_loop_quit(fix->main_loop); +} + +static gboolean func_cb(gpointer data) +{ + struct cb_data *cbd = data; + util_test_func_t cb = cbd->cb; + struct test_fix *fix = cbd->user_data; + + (*cb)(fix); return FALSE; } -guint util_idle_call(struct test_fix *fix, GSourceFunc func, - GDestroyNotify notify) +static void destroy_cb(gpointer data) { + struct cb_data *cbd = data; + util_test_func_t cb = cbd->data; + struct test_fix *fix = cbd->user_data; + + if (cb != NULL) + (*cb)(fix); + + g_free(cbd); +} + +void util_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy) +{ + struct cb_data *cbd = cb_data_new(func, fix); GSource *source; - guint id; - source = g_idle_source_new(); - g_source_set_callback(source, func, fix, notify); - id = g_source_attach(source, g_main_loop_get_context(fix->main_loop)); + cbd->data = destroy; + + source = g_timeout_source_new(0); + g_source_set_callback(source, func_cb, cbd, destroy_cb); + g_source_attach(source, g_main_loop_get_context(fix->main_loop)); g_source_unref(source); +} + +void util_idle_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy) +{ + struct cb_data *cbd = cb_data_new(func, fix); + GSource *source; + + cbd->data = destroy; - return id; + source = g_idle_source_new(); + g_source_set_callback(source, func_cb, cbd, destroy_cb); + g_source_attach(source, g_main_loop_get_context(fix->main_loop)); + g_source_unref(source); } static void connman_died(DBusConnection *connection, void *user_data) @@ -110,24 +144,13 @@ static gboolean handle_manager_changed(DBusConnection *connection, return TRUE; } -guint util_call(struct test_fix *fix, GSourceFunc func, - GDestroyNotify notify) -{ - GSource *source; - guint id; - - source = g_timeout_source_new(0); - g_source_set_callback(source, func, fix, notify); - id = g_source_attach(source, g_main_loop_get_context(fix->main_loop)); - g_source_unref(source); - - return id; -} - -void util_setup(struct test_fix *fix, gconstpointer data) +static struct test_fix *create_fix(void) { + struct test_fix *fix; DBusMessage *msg; + fix = g_new0(struct test_fix, 1); + fix->main_loop = g_main_loop_new(NULL, FALSE); fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL); @@ -146,9 +169,11 @@ void util_setup(struct test_fix *fix, gconstpointer data) msg = manager_get_properties(fix->main_connection); manager_parse_properties(msg, &fix->manager); dbus_message_unref(msg); + + return fix; } -void util_teardown(struct test_fix *fix, gconstpointer data) +static void cleanup_fix(struct test_fix *fix) { g_dbus_remove_watch(fix->main_connection, fix->watch); g_dbus_remove_watch(fix->main_connection, fix->manager_watch); @@ -156,14 +181,29 @@ void util_teardown(struct test_fix *fix, gconstpointer data) dbus_connection_unref(fix->main_connection); g_main_loop_unref(fix->main_loop); + + g_free(fix); } -static void util_wrapper(struct test_fix *fix, gconstpointer data) +struct test_data_cb { + util_test_func_t func; + util_test_func_t setup; + util_test_func_t teardown; +}; + +static void run_test_cb(gconstpointer data) { - GSourceFunc func = data; + const struct test_data_cb *cbd = data; + struct test_fix *fix; + + fix = create_fix(); + + util_call(fix, cbd->setup, NULL); + g_main_loop_run(fix->main_loop); + #if ENABLE_WRAPPER if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) { - util_call(fix, func, NULL); + util_call(fix, cbd->func, NULL); g_main_loop_run(fix->main_loop); exit(0); } @@ -173,14 +213,23 @@ static void util_wrapper(struct test_fix *fix, gconstpointer data) util_call(fix, func, NULL); g_main_loop_run(fix->main_loop); #endif + + util_call(fix, cbd->teardown, NULL); + g_main_loop_run(fix->main_loop); + + cleanup_fix(fix); } -void util_test_add(const char *test_name, GSourceFunc test_func, - util_test_setup_cb setup_cb, - util_test_teardown_cb teardown_cb) +void util_test_add(const char *test_name, util_test_func_t test_func, + util_test_func_t setup, util_test_func_t teardown) { - g_test_add(test_name, struct test_fix, test_func, - setup_cb, util_wrapper, teardown_cb); + struct test_data_cb *cbd = g_new0(struct test_data_cb, 1); + + cbd->func = test_func; + cbd->setup = setup; + cbd->teardown = teardown; + + g_test_add_data_func_full(test_name, cbd, run_test_cb, g_free); } void util_session_create(struct test_fix *fix, unsigned int max_sessions) @@ -198,10 +247,8 @@ void util_session_create(struct test_fix *fix, unsigned int max_sessions) } } -void util_session_destroy(gpointer data) +void util_session_destroy(struct test_fix *fix) { - struct test_fix *fix = data; - unsigned int i; for (i = 0; i < fix->max_sessions; i++) { |