diff options
-rw-r--r-- | configure.ac | 10 | ||||
-rw-r--r-- | devkit-power-gobject/dkp-history-obj.c | 5 | ||||
-rw-r--r-- | devkit-power-gobject/dkp-history-obj.h | 2 | ||||
-rw-r--r-- | src/.gitignore | 1 | ||||
-rw-r--r-- | src/Makefile.am | 42 | ||||
-rw-r--r-- | src/dkp-backend.h | 2 | ||||
-rw-r--r-- | src/dkp-daemon.c | 27 | ||||
-rw-r--r-- | src/dkp-daemon.h | 1 | ||||
-rw-r--r-- | src/dkp-device-list.c | 56 | ||||
-rw-r--r-- | src/dkp-device-list.h | 2 | ||||
-rw-r--r-- | src/dkp-device.c | 39 | ||||
-rw-r--r-- | src/dkp-device.h | 2 | ||||
-rw-r--r-- | src/dkp-history.c | 52 | ||||
-rw-r--r-- | src/dkp-history.h | 2 | ||||
-rw-r--r-- | src/dkp-native.h | 1 | ||||
-rw-r--r-- | src/dkp-polkit.c | 27 | ||||
-rw-r--r-- | src/dkp-polkit.h | 2 | ||||
-rw-r--r-- | src/dkp-qos.c | 46 | ||||
-rw-r--r-- | src/dkp-qos.h | 2 | ||||
-rw-r--r-- | src/dkp-self-test.c | 60 | ||||
-rw-r--r-- | src/dkp-wakeups.c | 27 | ||||
-rw-r--r-- | src/dkp-wakeups.h | 2 | ||||
-rw-r--r-- | src/dummy/.gitignore | 2 | ||||
-rw-r--r-- | src/dummy/Makefile.am | 3 | ||||
-rw-r--r-- | src/dummy/dkp-backend.c | 27 | ||||
-rw-r--r-- | src/dummy/dkp-native.c | 24 | ||||
-rw-r--r-- | src/egg-test.c | 357 | ||||
-rw-r--r-- | src/egg-test.h | 48 |
28 files changed, 832 insertions, 39 deletions
diff --git a/configure.ac b/configure.ac index 0d521e3..b66a73a 100644 --- a/configure.ac +++ b/configure.ac @@ -177,6 +177,16 @@ fi AM_CONDITIONAL(BACKEND_TYPE_DUMMY, [test x$with_backend = xdummy]) AM_CONDITIONAL(BACKEND_TYPE_LINUX, [test x$with_backend = xlinux]) +dnl --------------------------------------------------------------------------- +dnl - Build self tests +dnl --------------------------------------------------------------------------- +AC_ARG_ENABLE(tests, AS_HELP_STRING([--enable-tests],[enable unit test code]), + enable_tests=$enableval,enable_tests=yes) +AM_CONDITIONAL(EGG_BUILD_TESTS, test x$enable_tests = xyes) +if test x$enable_tests = xyes; then + AC_DEFINE(EGG_BUILD_TESTS,1,[Build test code]) +fi + if test "x$GCC" = "xyes"; then LDFLAGS="-Wl,--as-needed $LDFLAGS" fi diff --git a/devkit-power-gobject/dkp-history-obj.c b/devkit-power-gobject/dkp-history-obj.c index 33a56a7..021377e 100644 --- a/devkit-power-gobject/dkp-history-obj.c +++ b/devkit-power-gobject/dkp-history-obj.c @@ -102,13 +102,12 @@ dkp_history_obj_clear (DkpHistoryObj *obj) /** * dkp_history_obj_free: **/ -gboolean +void dkp_history_obj_free (DkpHistoryObj *obj) { if (obj == NULL) - return FALSE; + return; g_free (obj); - return TRUE; } /** diff --git a/devkit-power-gobject/dkp-history-obj.h b/devkit-power-gobject/dkp-history-obj.h index d1dce89..a031f0c 100644 --- a/devkit-power-gobject/dkp-history-obj.h +++ b/devkit-power-gobject/dkp-history-obj.h @@ -39,7 +39,7 @@ typedef struct DkpHistoryObj *dkp_history_obj_new (void); gboolean dkp_history_obj_clear (DkpHistoryObj *obj); -gboolean dkp_history_obj_free (DkpHistoryObj *obj); +void dkp_history_obj_free (DkpHistoryObj *obj); DkpHistoryObj *dkp_history_obj_copy (const DkpHistoryObj *cobj); gboolean dkp_history_obj_print (const DkpHistoryObj *obj); DkpHistoryObj *dkp_history_obj_create (gdouble value, diff --git a/src/.gitignore b/src/.gitignore index 7d8db05..75c1242 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,5 +1,6 @@ .deps devkit-power-daemon +dkp-self-test *.o *-glue.h *-marshal.c diff --git a/src/Makefile.am b/src/Makefile.am index d03ce1b..0c5900c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -115,6 +115,48 @@ devkit_power_daemon_CFLAGS = \ $(WARNINGFLAGS_C) \ $(NULL) +if EGG_BUILD_TESTS +check_PROGRAMS = \ + dkp-self-test + +dkp_self_test_SOURCES = \ + egg-test.h \ + egg-test.c \ + egg-debug.c \ + egg-debug.h \ + dkp-self-test.c \ + dkp-polkit.h \ + dkp-polkit.c \ + dkp-daemon.h \ + dkp-daemon.c \ + dkp-device.h \ + dkp-device.c \ + dkp-device-list.h \ + dkp-device-list.c \ + dkp-qos.h \ + dkp-qos.c \ + dkp-wakeups.h \ + dkp-wakeups.c \ + dkp-history.h \ + dkp-history.c \ + dkp-backend.h \ + dkp-native.h \ + $(BUILT_SOURCES) + +dkp_self_test_LDADD = \ + -lm \ + dummy/libdkpshared.la \ + $(GLIB_LIBS) \ + $(GIO_CFLAGS) \ + $(SELFTEST_LIBS) \ + $(DEVKIT_POWER_LIBS) \ + $(POLKIT_LIBS) + +dkp_self_test_CFLAGS = -DEGG_TEST $(AM_CFLAGS) $(WARNINGFLAGS_C) + +TESTS = dkp-self-test +endif + servicedir = $(datadir)/dbus-1/system-services service_in_files = org.freedesktop.DeviceKit.Power.service.in service_DATA = $(service_in_files:.service.in=.service) diff --git a/src/dkp-backend.h b/src/dkp-backend.h index a866693..c03bac9 100644 --- a/src/dkp-backend.h +++ b/src/dkp-backend.h @@ -63,6 +63,8 @@ typedef struct GType dkp_backend_get_type (void); DkpBackend *dkp_backend_new (void); +void dkp_backend_test (gpointer user_data); + gboolean dkp_backend_coldplug (DkpBackend *backend, DkpDaemon *daemon); diff --git a/src/dkp-daemon.c b/src/dkp-daemon.c index 5e713e1..7c91b6c 100644 --- a/src/dkp-daemon.c +++ b/src/dkp-daemon.c @@ -989,3 +989,30 @@ dkp_daemon_new (void) return daemon; } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_daemon_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpDaemon *daemon; + + if (!egg_test_start (test, "DkpDaemon")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + daemon = dkp_daemon_new (); + egg_test_assert (test, daemon != NULL); + + /* unref */ + g_object_unref (daemon); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-daemon.h b/src/dkp-daemon.h index fa371fa..0e70194 100644 --- a/src/dkp-daemon.h +++ b/src/dkp-daemon.h @@ -66,6 +66,7 @@ GType dkp_daemon_error_get_type (void); GQuark dkp_daemon_error_quark (void); GType dkp_daemon_get_type (void); DkpDaemon *dkp_daemon_new (void); +void dkp_daemon_test (gpointer user_data); /* private */ guint dkp_daemon_get_number_devices_of_type (DkpDaemon *daemon, diff --git a/src/dkp-device-list.c b/src/dkp-device-list.c index eefd25c..c8b9ab0 100644 --- a/src/dkp-device-list.c +++ b/src/dkp-device-list.c @@ -84,6 +84,10 @@ dkp_device_list_insert (DkpDeviceList *list, GObject *native, GObject *device) g_return_val_if_fail (device != NULL, FALSE); native_path = dkp_native_get_native_path (native); + if (native_path == NULL) { + egg_warning ("failed to get native path"); + return FALSE; + } g_hash_table_insert (list->priv->map_native_path_to_device, g_strdup (native_path), g_object_ref (device)); g_ptr_array_add (list->priv->array, g_object_ref (device)); @@ -190,3 +194,55 @@ dkp_device_list_new (void) return DKP_DEVICE_LIST (list); } + +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_device_list_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpDeviceList *list; + GObject *native; + GObject *device; + GObject *found; + gboolean ret; + + if (!egg_test_start (test, "DkpDeviceList")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + list = dkp_device_list_new (); + egg_test_assert (test, list != NULL); + + /************************************************************/ + egg_test_title (test, "add device"); + native = g_object_new (G_TYPE_OBJECT, NULL); + device = g_object_new (G_TYPE_OBJECT, NULL); + ret = dkp_device_list_insert (list, native, device); + egg_test_assert (test, ret); + + /************************************************************/ + egg_test_title (test, "find device"); + found = dkp_device_list_lookup (list, native); + egg_test_assert (test, (found != NULL)); + g_object_unref (found); + + /************************************************************/ + egg_test_title (test, "remove device"); + ret = dkp_device_list_remove (list, device); + egg_test_assert (test, ret); + + /* unref */ + g_object_unref (native); + g_object_unref (device); + g_object_unref (list); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-device-list.h b/src/dkp-device-list.h index d496bdc..aac2a6c 100644 --- a/src/dkp-device-list.h +++ b/src/dkp-device-list.h @@ -51,6 +51,8 @@ typedef struct GType dkp_device_list_get_type (void); DkpDeviceList *dkp_device_list_new (void); +void dkp_device_list_test (gpointer user_data); + GObject *dkp_device_list_lookup (DkpDeviceList *list, GObject *native); gboolean dkp_device_list_insert (DkpDeviceList *list, diff --git a/src/dkp-device.c b/src/dkp-device.c index 254724a..1eff284 100644 --- a/src/dkp-device.c +++ b/src/dkp-device.c @@ -636,10 +636,8 @@ dkp_device_get_statistics (DkpDevice *device, const gchar *type, DBusGMethodInvo dbus_g_method_return (context, complex); out: - if (array != NULL) { - g_ptr_array_foreach (array, (GFunc) dkp_stats_obj_free, NULL); - g_ptr_array_free (array, TRUE); - } + if (array != NULL) + g_ptr_array_unref (array); return TRUE; } @@ -894,8 +892,10 @@ dkp_device_finalize (GObject *object) device = DKP_DEVICE (object); g_return_if_fail (device->priv != NULL); - g_object_unref (device->priv->native); - g_object_unref (device->priv->daemon); + if (device->priv->native != NULL) + g_object_unref (device->priv->native); + if (device->priv->daemon != NULL) + g_object_unref (device->priv->daemon); g_object_unref (device->priv->history); g_free (device->priv->object_path); g_free (device->priv->vendor); @@ -1185,3 +1185,30 @@ dkp_device_new (void) return device; } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_device_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpDevice *device; + + if (!egg_test_start (test, "DkpDevice")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + device = dkp_device_new (); + egg_test_assert (test, device != NULL); + + /* unref */ + g_object_unref (device); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-device.h b/src/dkp-device.h index 0e03f0f..1688fa5 100644 --- a/src/dkp-device.h +++ b/src/dkp-device.h @@ -74,6 +74,8 @@ GQuark dkp_device_error_quark (void); GType dkp_device_error_get_type (void); GType dkp_device_get_type (void); DkpDevice *dkp_device_new (void); +void dkp_device_test (gpointer user_data); + gboolean dkp_device_coldplug (DkpDevice *device, DkpDaemon *daemon, GObject *native); diff --git a/src/dkp-history.c b/src/dkp-history.c index ad72d1a..23bf4c2 100644 --- a/src/dkp-history.c +++ b/src/dkp-history.c @@ -121,7 +121,7 @@ dkp_history_array_limit_resolution (GPtrArray *array, guint max_num) guint step = 1; gfloat preset; - new = g_ptr_array_new (); + new = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free); egg_debug ("length of array (before) %i", array->len); /* check length */ @@ -250,8 +250,7 @@ dkp_history_get_data (DkpHistory *history, DkpHistoryType type, guint timespan, /* only add a certain number of points */ array_resolution = dkp_history_array_limit_resolution (array, resolution); - g_ptr_array_foreach (array, (GFunc) dkp_history_obj_free, NULL); - g_ptr_array_free (array, TRUE); + g_ptr_array_unref (array); return array_resolution; } @@ -814,10 +813,10 @@ dkp_history_init (DkpHistory *history) history->priv->rate_last = 0; history->priv->percentage_last = 0; history->priv->state = DKP_DEVICE_STATE_UNKNOWN; - history->priv->data_rate = g_ptr_array_new (); - history->priv->data_charge = g_ptr_array_new (); - history->priv->data_time_full = g_ptr_array_new (); - history->priv->data_time_empty = g_ptr_array_new (); + history->priv->data_rate = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free); + history->priv->data_charge = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free); + history->priv->data_time_full = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free); + history->priv->data_time_empty = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free); history->priv->save_id = 0; } @@ -840,14 +839,10 @@ dkp_history_finalize (GObject *object) if (history->priv->id != NULL) dkp_history_save_data (history); - g_ptr_array_foreach (history->priv->data_rate, (GFunc) dkp_history_obj_free, NULL); - g_ptr_array_free (history->priv->data_rate, TRUE); - g_ptr_array_foreach (history->priv->data_charge, (GFunc) dkp_history_obj_free, NULL); - g_ptr_array_free (history->priv->data_charge, TRUE); - g_ptr_array_foreach (history->priv->data_time_full, (GFunc) dkp_history_obj_free, NULL); - g_ptr_array_free (history->priv->data_time_full, TRUE); - g_ptr_array_foreach (history->priv->data_time_empty, (GFunc) dkp_history_obj_free, NULL); - g_ptr_array_free (history->priv->data_time_empty, TRUE); + g_ptr_array_unref (history->priv->data_rate); + g_ptr_array_unref (history->priv->data_charge); + g_ptr_array_unref (history->priv->data_time_full); + g_ptr_array_unref (history->priv->data_time_empty); g_free (history->priv->id); @@ -869,3 +864,30 @@ dkp_history_new (void) return DKP_HISTORY (history); } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_history_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpHistory *history; + + if (!egg_test_start (test, "DkpHistory")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + history = dkp_history_new (); + egg_test_assert (test, history != NULL); + + /* unref */ + g_object_unref (history); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-history.h b/src/dkp-history.h index d1d3a77..afabaca 100644 --- a/src/dkp-history.h +++ b/src/dkp-history.h @@ -60,6 +60,8 @@ typedef enum { GType dkp_history_get_type (void); DkpHistory *dkp_history_new (void); +void dkp_history_test (gpointer user_data); + GPtrArray *dkp_history_get_data (DkpHistory *history, DkpHistoryType type, guint timespan, diff --git a/src/dkp-native.h b/src/dkp-native.h index 53a9673..ea729e9 100644 --- a/src/dkp-native.h +++ b/src/dkp-native.h @@ -26,6 +26,7 @@ G_BEGIN_DECLS const gchar *dkp_native_get_native_path (GObject *object); +void dkp_native_test (gpointer user_data); G_END_DECLS diff --git a/src/dkp-polkit.c b/src/dkp-polkit.c index ca00936..4245d95 100644 --- a/src/dkp-polkit.c +++ b/src/dkp-polkit.c @@ -232,3 +232,30 @@ dkp_polkit_new (void) return DKP_POLKIT (dkp_polkit_object); } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_polkit_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpPolkit *polkit; + + if (!egg_test_start (test, "DkpPolkit")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + polkit = dkp_polkit_new (); + egg_test_assert (test, polkit != NULL); + + /* unref */ + g_object_unref (polkit); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-polkit.h b/src/dkp-polkit.h index 56ed205..e628890 100644 --- a/src/dkp-polkit.h +++ b/src/dkp-polkit.h @@ -49,6 +49,8 @@ typedef struct GType dkp_polkit_get_type (void); DkpPolkit *dkp_polkit_new (void); +void dkp_polkit_test (gpointer user_data); + PolkitSubject *dkp_polkit_get_subject (DkpPolkit *polkit, DBusGMethodInvocation *context); gboolean dkp_polkit_check_auth (DkpPolkit *polkit, diff --git a/src/dkp-qos.c b/src/dkp-qos.c index 407e369..2a453d7 100644 --- a/src/dkp-qos.c +++ b/src/dkp-qos.c @@ -59,9 +59,9 @@ static void dkp_qos_finalize (GObject *object); struct DkpQosPrivate { GPtrArray *data; - gint fd[DKP_QOS_TYPE_UNKNOWN]; - gint last[DKP_QOS_TYPE_UNKNOWN]; - gint minimum[DKP_QOS_TYPE_UNKNOWN]; + gint fd[DKP_QOS_TYPE_LAST]; + gint last[DKP_QOS_TYPE_LAST]; + gint minimum[DKP_QOS_TYPE_LAST]; DkpPolkit *polkit; DBusGConnection *connection; DBusGProxy *proxy; @@ -392,8 +392,6 @@ dkp_qos_cancel_request (DkpQos *qos, guint cookie, DBusGMethodInvocation *contex /* TODO: if persistent remove from datadase */ - dkp_qos_free_data_obj (obj); - g_signal_emit (qos, signals [REQUESTS_CHANGED], 0); out: if (subject != NULL) @@ -505,7 +503,6 @@ dkp_qos_remove_dbus (DkpQos *qos, const gchar *sender) egg_debug ("Auto-revoked idle qos on %s", sender); g_ptr_array_remove (qos->priv->data, obj); dkp_qos_latency_perhaps_changed (qos, obj->type); - dkp_qos_free_data_obj (obj); } } } @@ -559,8 +556,7 @@ dkp_qos_init (DkpQos *qos) qos->priv = DKP_QOS_GET_PRIVATE (qos); qos->priv->polkit = dkp_polkit_new (); - qos->priv->data = g_ptr_array_new (); - + qos->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_qos_free_data_obj); /* TODO: need to load persistent values */ /* setup lowest */ @@ -617,11 +613,10 @@ dkp_qos_finalize (GObject *object) if (qos->priv->fd[i] > 0) close (qos->priv->fd[i]); } - g_ptr_array_foreach (qos->priv->data, (GFunc) dkp_qos_free_data_obj, NULL); - g_ptr_array_free (qos->priv->data, TRUE); + g_ptr_array_unref (qos->priv->data); g_object_unref (qos->priv->proxy); -//FIXME: causes bt -// g_object_unref (qos->priv->polkit); + + g_object_unref (qos->priv->polkit); G_OBJECT_CLASS (dkp_qos_parent_class)->finalize (object); } @@ -637,3 +632,30 @@ dkp_qos_new (void) return DKP_QOS (qos); } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_qos_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpQos *qos; + + if (!egg_test_start (test, "DkpQos")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + qos = dkp_qos_new (); + egg_test_assert (test, qos != NULL); + + /* unref */ + g_object_unref (qos); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-qos.h b/src/dkp-qos.h index ae002a9..68f4251 100644 --- a/src/dkp-qos.h +++ b/src/dkp-qos.h @@ -53,6 +53,8 @@ typedef struct DkpQos *dkp_qos_new (void); GType dkp_qos_get_type (void); +void dkp_qos_test (gpointer user_data); + void dkp_qos_request_latency (DkpQos *qos, const gchar *type, gint value, diff --git a/src/dkp-self-test.c b/src/dkp-self-test.c new file mode 100644 index 0000000..e274be9 --- /dev/null +++ b/src/dkp-self-test.c @@ -0,0 +1,60 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2009 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include <glib-object.h> +#include "egg-test.h" +#include "egg-debug.h" + +#include "dkp-backend.h" +#include "dkp-daemon.h" +#include "dkp-device.h" +#include "dkp-device-list.h" +#include "dkp-history.h" +#include "dkp-native.h" +#include "dkp-polkit.h" +#include "dkp-qos.h" +#include "dkp-wakeups.h" + +int +main (int argc, char **argv) +{ + EggTest *test; + + g_type_init (); + test = egg_test_init (); + egg_debug_init (TRUE); + + /* tests go here */ + dkp_backend_test (test); + dkp_device_test (test); + dkp_device_list_test (test); + dkp_history_test (test); + dkp_native_test (test); + dkp_polkit_test (test); + dkp_qos_test (test); + dkp_wakeups_test (test); + dkp_daemon_test (test); + + return (egg_test_finish (test)); +} + diff --git a/src/dkp-wakeups.c b/src/dkp-wakeups.c index fb8918e..397fd84 100644 --- a/src/dkp-wakeups.c +++ b/src/dkp-wakeups.c @@ -779,3 +779,30 @@ dkp_wakeups_new (void) return DKP_WAKEUPS (wakeups); } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_wakeups_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpWakeups *wakeups; + + if (!egg_test_start (test, "DkpWakeups")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + wakeups = dkp_wakeups_new (); + egg_test_assert (test, wakeups != NULL); + + /* unref */ + g_object_unref (wakeups); + + egg_test_end (test); +} +#endif + diff --git a/src/dkp-wakeups.h b/src/dkp-wakeups.h index 0f3e890..d66aacd 100644 --- a/src/dkp-wakeups.h +++ b/src/dkp-wakeups.h @@ -51,6 +51,8 @@ typedef struct } DkpWakeupsClass; DkpWakeups *dkp_wakeups_new (void); +void dkp_wakeups_test (gpointer user_data); + GType dkp_wakeups_get_type (void); gboolean dkp_wakeups_get_total (DkpWakeups *wakeups, guint *value, diff --git a/src/dummy/.gitignore b/src/dummy/.gitignore index ed89e2f..412ad37 100644 --- a/src/dummy/.gitignore +++ b/src/dummy/.gitignore @@ -2,4 +2,6 @@ .libs *.o *.a +*.lo +*.la diff --git a/src/dummy/Makefile.am b/src/dummy/Makefile.am index 804358a..25afad6 100644 --- a/src/dummy/Makefile.am +++ b/src/dummy/Makefile.am @@ -7,14 +7,13 @@ INCLUDES = \ -DEGG_LOGGING="\"DKP_LOGGING\"" \ -DEGG_CONSOLE="\"DKP_CONSOLE\"" \ -DDKP_COMPILATION \ + -DEGG_TEST \ -I$(top_srcdir)/devkit-power-gobject \ $(DBUS_GLIB_CFLAGS) \ $(POLKIT_CFLAGS) \ $(GLIB_CFLAGS) -if BACKEND_TYPE_DUMMY noinst_LTLIBRARIES = libdkpshared.la -endif libdkpshared_la_SOURCES = \ dkp-backend.c \ diff --git a/src/dummy/dkp-backend.c b/src/dummy/dkp-backend.c index 8db6277..1ffd508 100644 --- a/src/dummy/dkp-backend.c +++ b/src/dummy/dkp-backend.c @@ -219,3 +219,30 @@ dkp_backend_new (void) return DKP_BACKEND (backend); } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_backend_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + DkpBackend *backend; + + if (!egg_test_start (test, "DkpBackend")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + backend = dkp_backend_new (); + egg_test_assert (test, backend != NULL); + + /* unref */ + g_object_unref (backend); + + egg_test_end (test); +} +#endif + diff --git a/src/dummy/dkp-native.c b/src/dummy/dkp-native.c index 1dea65c..4388a41 100644 --- a/src/dummy/dkp-native.c +++ b/src/dummy/dkp-native.c @@ -38,3 +38,27 @@ dkp_native_get_native_path (GObject *object) return "/sys/dummy"; } +/*************************************************************************** + *** MAKE CHECK TESTS *** + ***************************************************************************/ +#ifdef EGG_TEST +#include "egg-test.h" + +void +dkp_native_test (gpointer user_data) +{ + EggTest *test = (EggTest *) user_data; + const gchar *path; + + if (!egg_test_start (test, "DkpNative")) + return; + + /************************************************************/ + egg_test_title (test, "get instance"); + path = dkp_native_get_native_path (NULL); + egg_test_assert (test, (g_strcmp0 (path, "/sys/dummy") == 0)); + + egg_test_end (test); +} +#endif + diff --git a/src/egg-test.c b/src/egg-test.c new file mode 100644 index 0000000..ef4eef3 --- /dev/null +++ b/src/egg-test.c @@ -0,0 +1,357 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include <glib.h> +#include <string.h> +#include <glib/gprintf.h> + +#include "egg-test.h" + +struct EggTest { + guint total; + guint succeeded; + gboolean started; + gboolean titled; + gchar *type; + GTimer *timer; + GMainLoop *loop; + guint hang_loop_id; + gpointer user_data; +}; + +/** + * egg_test_init: + **/ +EggTest * +egg_test_init () +{ + EggTest *test; + test = g_new (EggTest, 1); + test->total = 0; + test->succeeded = 0; + test->type = NULL; + test->started = FALSE; + test->titled = FALSE; + test->timer = g_timer_new (); + test->loop = g_main_loop_new (NULL, FALSE); + test->hang_loop_id = 0; + return test; +} + +/** + * egg_test_loop_quit: + **/ +void +egg_test_loop_quit (EggTest *test) +{ + /* disable the loop watch */ + if (test->hang_loop_id != 0) { + g_source_remove (test->hang_loop_id); + test->hang_loop_id = 0; + } + g_main_loop_quit (test->loop); +} + +/** + * egg_test_hang_check: + **/ +static gboolean +egg_test_hang_check (gpointer data) +{ + EggTest *test = (EggTest *) data; + g_main_loop_quit (test->loop); + return FALSE; +} + +/** + * egg_test_loop_wait: + **/ +void +egg_test_loop_wait (EggTest *test, guint timeout) +{ + test->hang_loop_id = g_timeout_add (timeout, egg_test_hang_check, test); + g_main_loop_run (test->loop); +} + +/** + * egg_test_loop_check: + **/ +void +egg_test_loop_check (EggTest *test) +{ + guint elapsed = egg_test_elapsed (test); + egg_test_title (test, "did we timeout out of the loop"); + if (test->hang_loop_id == 0) { + egg_test_success (test, "loop blocked for %ims", elapsed); + } else { + egg_test_failed (test, "hangcheck saved us after %ims", elapsed); + } +} + +/** + * egg_test_set_user_data: + **/ +void +egg_test_set_user_data (EggTest *test, gpointer user_data) +{ + test->user_data = user_data; +} + +/** + * egg_test_get_user_data: + **/ +gpointer +egg_test_get_user_data (EggTest *test) +{ + return test->user_data; +} + +/** + * egg_test_finish: + **/ +gint +egg_test_finish (EggTest *test) +{ + gint retval; + g_print ("test passes (%u/%u) : ", test->succeeded, test->total); + if (test->succeeded == test->total) { + g_print ("ALL OKAY\n"); + retval = 0; + } else { + g_print ("%u FAILURE(S)\n", test->total - test->succeeded); + retval = 1; + } + + g_timer_destroy (test->timer); + g_main_loop_unref (test->loop); + g_free (test); + + return retval; +} + +/** + * egg_test_elapsed: + * + * Returns: time in ms + **/ +guint +egg_test_elapsed (EggTest *test) +{ + gdouble time_s; + time_s = g_timer_elapsed (test->timer, NULL); + return (guint) (time_s * 1000.0f); +} + +/** + * egg_test_start: + **/ +gboolean +egg_test_start (EggTest *test, const gchar *name) +{ + if (test->started) { + g_print ("Not ended test! Cannot start!\n"); + exit (1); + } + test->type = g_strdup (name); + test->started = TRUE; + return TRUE; +} + +/** + * egg_test_end: + **/ +void +egg_test_end (EggTest *test) +{ + if (test->started == FALSE) { + g_print ("Not started test! Cannot finish!\n"); + exit (1); + } + + /* disable hang check */ + if (test->hang_loop_id != 0) { + g_source_remove (test->hang_loop_id); + test->hang_loop_id = 0; + } + + /* remove all the test callbacks */ + while (g_source_remove_by_user_data (test)) + g_print ("WARNING: removed callback for test module"); + + /* check we don't have any pending iterations */ + if (g_main_context_pending (NULL)) { + g_print ("WARNING: Pending event in context! Running to completion... "); + while (g_main_context_pending (NULL)) + g_main_context_iteration (NULL, TRUE); + g_print ("Done!\n"); + } + + test->started = FALSE; + g_free (test->type); +} + +/** + * egg_test_title: + **/ +void +egg_test_title (EggTest *test, const gchar *format, ...) +{ + va_list args; + gchar *va_args_buffer = NULL; + + /* already titled? */ + if (test->titled) { + g_print ("Already titled!\n"); + exit (1); + } + + /* reset the value egg_test_elapsed replies with */ + g_timer_reset (test->timer); + + va_start (args, format); + g_vasprintf (&va_args_buffer, format, args); + va_end (args); + g_print ("> check #%u\t%s: \t%s...", test->total+1, test->type, va_args_buffer); + g_free (va_args_buffer); + + test->titled = TRUE; + test->total++; +} + +/** + * egg_test_success: + **/ +void +egg_test_success (EggTest *test, const gchar *format, ...) +{ + va_list args; + gchar *va_args_buffer = NULL; + + /* not titled? */ + if (!test->titled) { + g_print ("Not titled!\n"); + exit (1); + } + if (format == NULL) { + g_print ("...OK\n"); + goto finish; + } + va_start (args, format); + g_vasprintf (&va_args_buffer, format, args); + va_end (args); + g_print ("...OK [%s]\n", va_args_buffer); + g_free (va_args_buffer); +finish: + test->titled = FALSE; + test->succeeded++; +} + +/** + * egg_test_failed: + **/ +void +egg_test_failed (EggTest *test, const gchar *format, ...) +{ + va_list args; + gchar *va_args_buffer = NULL; + + /* not titled? */ + if (!test->titled) { + g_print ("Not titled!\n"); + exit (1); + } + if (format == NULL) { + g_print ("FAILED\n"); + goto failed; + } + va_start (args, format); + g_vasprintf (&va_args_buffer, format, args); + va_end (args); + g_print ("FAILED [%s]\n", va_args_buffer); + g_free (va_args_buffer); +failed: + exit (1); +} + +/** + * egg_test_assert: + **/ +void +egg_test_assert (EggTest *test, gboolean value) +{ + if (value) + egg_test_success (test, NULL); + else + egg_test_failed (test, NULL); +} + +/** + * egg_test_title_assert: + **/ +void +egg_test_title_assert (EggTest *test, const gchar *text, gboolean value) +{ + egg_test_title (test, "%s", text); + if (value) + egg_test_success (test, NULL); + else + egg_test_failed (test, NULL); +} + +/** + * egg_test_get_data_file: + **/ +gchar * +egg_test_get_data_file (const gchar *filename) +{ + gboolean ret; + gchar *full; + + /* check to see if we are being run in the build root */ + full = g_build_filename ("..", "data", "tests", filename, NULL); + ret = g_file_test (full, G_FILE_TEST_EXISTS); + if (ret) + return full; + g_free (full); + + /* check to see if we are being run in the build root */ + full = g_build_filename ("..", "..", "data", "tests", filename, NULL); + ret = g_file_test (full, G_FILE_TEST_EXISTS); + if (ret) + return full; + g_free (full); + + /* check to see if we are being run in make check */ + full = g_build_filename ("..", "..", "data", "tests", filename, NULL); + ret = g_file_test (full, G_FILE_TEST_EXISTS); + if (ret) + return full; + g_free (full); + full = g_build_filename ("..", "..", "..", "data", "tests", filename, NULL); + ret = g_file_test (full, G_FILE_TEST_EXISTS); + if (ret) + return full; + g_print ("[WARN] failed to find '%s'\n", full); + g_free (full); + return NULL; +} + diff --git a/src/egg-test.h b/src/egg-test.h new file mode 100644 index 0000000..ea4b94b --- /dev/null +++ b/src/egg-test.h @@ -0,0 +1,48 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_TEST_H +#define __EGG_TEST_H + +#include <glib.h> +#include <string.h> + +typedef struct EggTest EggTest; + +gboolean egg_test_start (EggTest *test, const gchar *name); +void egg_test_end (EggTest *test); +void egg_test_title (EggTest *test, const gchar *format, ...); +void egg_test_title_assert (EggTest *test, const gchar *text, gboolean value); +void egg_test_assert (EggTest *test, gboolean value); +void egg_test_success (EggTest *test, const gchar *format, ...); +void egg_test_failed (EggTest *test, const gchar *format, ...) G_GNUC_NORETURN; +EggTest *egg_test_init (void); +gint egg_test_finish (EggTest *test); +guint egg_test_elapsed (EggTest *test); +void egg_test_loop_quit (EggTest *test); +void egg_test_loop_wait (EggTest *test, guint timeout); +void egg_test_loop_check (EggTest *test); +void egg_test_set_user_data (EggTest *test, gpointer user_data); +gpointer egg_test_get_user_data (EggTest *test); +gchar *egg_test_get_data_file (const gchar *filename); + +#endif /* __EGG_TEST_H */ + |