Removed mainloop dependency and updated with
tester framework.
---
unit/test-hfp.c | 84 +++++++++++++++++++++++++++------------------------------
1 file changed, 40 insertions(+), 44 deletions(-)
diff --git a/unit/test-hfp.c b/unit/test-hfp.c
index 66966ce..509aaab 100644
--- a/unit/test-hfp.c
+++ b/unit/test-hfp.c
@@ -29,9 +29,9 @@
#include <glib.h>
#include "src/shared/hfp.h"
+#include "src/shared/tester.h"
struct context {
- GMainLoop *main_loop;
guint watch_id;
int fd_server;
int fd_client;
@@ -97,7 +97,7 @@ struct test_data {
data.test_name = g_strdup(name); \
data.pdu_list = g_memdup(pdus, sizeof(pdus)); \
data.result_func = result_function; \
- g_test_add_data_func(name, &data, function); \
+ tester_add(name, &data, NULL, function, NULL); \
data.test_handler = test_handler; \
} while (0)
@@ -112,15 +112,10 @@ struct test_data {
data.pdu_list = g_memdup(pdus, sizeof(pdus)); \
data.hf_result_func = result_func; \
data.response_func = response_function; \
- g_test_add_data_func(name, &data, function); \
+ tester_add(name, &data, NULL, function, NULL); \
data.test_handler = test_hf_handler; \
} while (0)
-static void context_quit(struct context *context)
-{
- g_main_loop_quit(context->main_loop);
-}
-
static void test_free(gconstpointer user_data)
{
const struct test_data *data = user_data;
@@ -129,6 +124,34 @@ static void test_free(gconstpointer user_data)
g_free(data->pdu_list);
}
+static void destroy_context(struct context *context)
+{
+ if (context->watch_id)
+ g_source_remove(context->watch_id);
+
+ test_free(context->data);
+
+ if (context->hfp)
+ hfp_gw_unref(context->hfp);
+
+ if (context->hfp_hf)
+ hfp_hf_unref(context->hfp_hf);
+
+ g_free(context);
+}
+
+static gboolean context_quit(gpointer user_data)
+{
+ struct context *context = user_data;
+
+ if(context == NULL)
+ return FALSE;
+
+ destroy_context(context);
+ tester_test_passed();
+ return FALSE;
+}
+
static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
@@ -224,9 +247,6 @@ static struct context *create_context(gconstpointer data)
int err, sv[2];
const struct test_data *d = data;
- context->main_loop = g_main_loop_new(NULL, FALSE);
- g_assert(context->main_loop);
-
err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
g_assert(err == 0);
@@ -251,26 +271,6 @@ static struct context *create_context(gconstpointer data)
return context;
}
-static void execute_context(struct context *context)
-{
- g_main_loop_run(context->main_loop);
-
- if (context->watch_id)
- g_source_remove(context->watch_id);
-
- g_main_loop_unref(context->main_loop);
-
- test_free(context->data);
-
- if (context->hfp)
- hfp_gw_unref(context->hfp);
-
- if (context->hfp_hf)
- hfp_hf_unref(context->hfp_hf);
-
- g_free(context);
-}
-
static void test_init(gconstpointer data)
{
struct context *context = create_context(data);
@@ -283,7 +283,7 @@ static void test_init(gconstpointer data)
hfp_gw_unref(context->hfp);
context->hfp = NULL;
- execute_context(context);
+ context_quit(context);
}
static void test_command_handler(gconstpointer data)
@@ -308,7 +308,7 @@ static void test_command_handler(gconstpointer data)
len = write(context->fd_server, pdu->data, pdu->size);
g_assert_cmpint(len, ==, pdu->size);
- execute_context(context);
+ context_quit(context);
}
static void test_register(gconstpointer data)
@@ -337,7 +337,7 @@ static void test_register(gconstpointer data)
len = write(context->fd_server, pdu->data, pdu->size);
g_assert_cmpint(len, ==, pdu->size);
- execute_context(context);
+ context_quit(context);
}
static void test_fragmented(gconstpointer data)
@@ -352,8 +352,6 @@ static void test_fragmented(gconstpointer data)
g_assert(ret);
g_idle_add(send_pdu, context);
-
- execute_context(context);
}
static void test_send_and_close(gconstpointer data)
@@ -372,7 +370,7 @@ static void test_send_and_close(gconstpointer data)
hfp_gw_unref(context->hfp);
context->hfp = NULL;
- execute_context(context);
+ context_quit(context);
}
static void check_ustring_1(struct hfp_context *result,
@@ -494,7 +492,7 @@ static void test_hf_init(gconstpointer data)
hfp_hf_unref(context->hfp_hf);
context->hfp_hf = NULL;
- execute_context(context);
+ context_quit(context);
}
static bool unsolicited_resp = false;
@@ -567,7 +565,7 @@ static void test_hf_send_command(gconstpointer data)
g_assert(ret);
}
- execute_context(context);
+ context_quit(context);
}
static void hf_chld_result_handler(struct hfp_context *hf_context,
void *user_data)
@@ -667,8 +665,6 @@ static void test_hf_unsolicited(gconstpointer data)
}
send_pdu(context);
-
- execute_context(context);
}
static void test_hf_robustness(gconstpointer data)
@@ -687,12 +683,12 @@ static void test_hf_robustness(gconstpointer data)
hfp_hf_unref(context->hfp_hf);
context->hfp_hf = NULL;
- execute_context(context);
+ context_quit(context);
}
int main(int argc, char *argv[])
{
- g_test_init(&argc, &argv, NULL);
+ tester_init(&argc, &argv);
define_test("/hfp/test_init", test_init, NULL, data_end());
define_test("/hfp/test_cmd_handler_1", test_command_handler, NULL,
@@ -860,5 +856,5 @@ int main(int argc, char *argv[])
frg_pdu('1', ',', '2', 'x', '\r', '\n'),
data_end());
- return g_test_run();
+ return tester_run();
}
--
1.9.1
Removed mainloop dependency and updated with
tester framework.
---
unit/test-uhid.c | 62 ++++++++++++++++++++++++--------------------------------
1 file changed, 26 insertions(+), 36 deletions(-)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index b48e0fa..ea91a38 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -38,6 +38,8 @@
#include "src/shared/uhid.h"
#include "src/shared/util.h"
+#include "src/shared/tester.h"
+
struct test_pdu {
bool valid;
const uint8_t *data;
@@ -50,7 +52,6 @@ struct test_data {
};
struct context {
- GMainLoop *main_loop;
struct bt_uhid *uhid;
guint source;
guint process;
@@ -74,14 +75,14 @@ struct context {
static struct test_data data; \
data.test_name = g_strdup(name); \
data.pdu_list = g_memdup(pdus, sizeof(pdus)); \
- g_test_add_data_func(name, &data, function); \
+ tester_add(name, &data, NULL, function, NULL); \
} while (0)
static void test_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
- g_print("%s%s\n", prefix, str);
+ tester_debug("%s%s\n", prefix, str);
}
static void test_free(gconstpointer user_data)
@@ -92,14 +93,29 @@ static void test_free(gconstpointer user_data)
g_free(data->pdu_list);
}
+static void destroy_context(struct context *context)
+{
+ if (context->source > 0)
+ g_source_remove(context->source);
+
+ bt_uhid_unref(context->uhid);
+
+ test_free(context->data);
+ g_free(context);
+}
+
static gboolean context_quit(gpointer user_data)
{
struct context *context = user_data;
+ if(context == NULL)
+ return FALSE;
+
if (context->process > 0)
g_source_remove(context->process);
- g_main_loop_quit(context->main_loop);
+ destroy_context(context);
+ tester_test_passed();
return FALSE;
}
@@ -114,8 +130,8 @@ static gboolean send_pdu(gpointer user_data)
len = write(context->fd, pdu->data, pdu->size);
- if (g_test_verbose())
- util_hexdump('<', pdu->data, len, test_debug, "uHID: ");
+
+ util_hexdump('<', pdu->data, len, test_debug, "uHID: ");
g_assert_cmpint(len, ==, pdu->size);
@@ -156,8 +172,7 @@ static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
g_assert(len > 0);
- if (g_test_verbose())
- util_hexdump('>', buf, len, test_debug, "uHID: ");
+ util_hexdump('>', buf, len, test_debug, "uHID: ");
g_assert_cmpint(len, ==, pdu->size);
@@ -174,9 +189,6 @@ static struct context *create_context(gconstpointer data)
GIOChannel *channel;
int err, sv[2];
- context->main_loop = g_main_loop_new(NULL, FALSE);
- g_assert(context->main_loop);
-
err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
g_assert(err == 0);
@@ -202,26 +214,6 @@ static struct context *create_context(gconstpointer data)
return context;
}
-static void destroy_context(struct context *context)
-{
- if (context->source > 0)
- g_source_remove(context->source);
-
- bt_uhid_unref(context->uhid);
-
- g_main_loop_unref(context->main_loop);
-
- test_free(context->data);
- g_free(context);
-}
-
-static void execute_context(struct context *context)
-{
- g_main_loop_run(context->main_loop);
-
- destroy_context(context);
-}
-
static const struct uhid_event ev_create = {
.type = UHID_CREATE,
};
@@ -263,7 +255,7 @@ static void test_client(gconstpointer data)
if (g_str_equal(context->data->test_name, "/uhid/command/input"))
bt_uhid_send(context->uhid, &ev_input);
- execute_context(context);
+ context_quit(context);
}
static void handle_output(struct uhid_event *ev, void *user_data)
@@ -288,13 +280,11 @@ static void test_server(gconstpointer data)
bt_uhid_register(context->uhid, UHID_FEATURE, handle_feature, context);
g_idle_add(send_pdu, context);
-
- execute_context(context);
}
int main(int argc, char *argv[])
{
- g_test_init(&argc, &argv, NULL);
+ tester_init(&argc, &argv);
define_test("/uhid/command/create", test_client, event(&ev_create));
define_test("/uhid/command/destroy", test_client, event(&ev_destroy));
@@ -305,5 +295,5 @@ int main(int argc, char *argv[])
define_test("/uhid/event/output", test_server, event(&ev_output));
define_test("/uhid/event/feature", test_server, event(&ev_feature));
- return g_test_run();
+ return tester_run();
}
--
1.9.1