[tracker] Add tracker-miner-manager unit test.



commit 51f52c6191014a0d53e4fd0a0f9ddb592bcbe716
Author: Ivan Frade <ivan frade nokia com>
Date:   Tue Mar 16 22:55:31 2010 +0200

    Add tracker-miner-manager unit test.

 tests/libtracker-miner/Makefile.am                 |   37 +++-
 tests/libtracker-miner/miners-mock.c               |  245 ++++++++++++++++++++
 tests/libtracker-miner/miners-mock.h               |   23 ++
 .../libtracker-miner/tracker-miner-manager-test.c  |  171 ++++++++++++++
 tests/libtracker-miner/tracker-miner-mock.vala     |   48 ++++
 5 files changed, 521 insertions(+), 3 deletions(-)
---
diff --git a/tests/libtracker-miner/Makefile.am b/tests/libtracker-miner/Makefile.am
index b0f8760..e86a8f6 100644
--- a/tests/libtracker-miner/Makefile.am
+++ b/tests/libtracker-miner/Makefile.am
@@ -2,7 +2,7 @@ include $(top_srcdir)/Makefile.decl
 
 noinst_PROGRAMS = $(TEST_PROGS)
 
-TEST_PROGS += tracker-crawler tracker-thumbnailer-test
+TEST_PROGS += tracker-crawler tracker-thumbnailer-test tracker-miner-manager
 
 INCLUDES = 									\
 	-DTEST									\
@@ -23,6 +23,8 @@ INCLUDES = 									\
 	$(PANGO_CFLAGS)								\
 	$(DBUS_CFLAGS)
 
+BUILT_SOURCES = libtracker-miner-testing.vala.stamp
+
 tracker_crawler_SOURCES = 							\
 	tracker-crawler-test.c
 
@@ -50,9 +52,38 @@ tracker_thumbnailer_test_LDADD = 						\
 	$(GTHREAD_LIBS)								\
 	$(GLIB2_LIBS)
 
-EXTRA_DIST = \
+tracker_miner_manager_VALASOURCES =						\
+	tracker-miner-mock.vala
+
+tracker_miner_manager_SOURCES = 						\
+	tracker-miner-manager-test.c						\
+	miners-mock.c								\
+	$(tracker_miner_manager_VALASOURCES)					\
+	empty-gobject.c
+
+tracker_miner_manager_LDADD = 							\
+	$(top_builddir)/src/libtracker-miner/libtracker-miner-0.7.la 		\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la 		\
+	$(DBUS_LIBS)								\
+	$(GCOV_LIBS)								\
+	$(GMODULE_LIBS)								\
+	$(GTHREAD_LIBS)								\
+	$(GLIB2_LIBS)
+
+libtracker-miner-testing.vala.stamp: $(tracker_miner_manager_VALASOURCES)
+	$(AM_V_GEN)$(VALAC) $(GCOV_VALAFLAGS) --pkg glib-2.0 -C $(VALAFLAGS) -H $(<:.vala=.h) $<
+	touch $@
+
+MAINTAINERCLEANFILES =					\
+	libtracker-miner-testing.vala.stamp		\
+	$(tracker_miner_manager_VALASOURCES:.vala=.c)	
+
+EXTRA_DIST = $(tracker_miner_manager_VALASOURCES) \
+	libtracker-miner-testing.vala.stamp  \
 	data/empty-dir/.hidden \
 	data/file1 \
 	data/dir/empty-dir/.hidden \
 	data/dir/file1 \
-	data/dir/file2
+	data/dir/file2 \
+	mock-miners/mock-miner-1.desktop \
+	mock-miners/mock-miner-2.desktop
diff --git a/tests/libtracker-miner/miners-mock.c b/tests/libtracker-miner/miners-mock.c
new file mode 100644
index 0000000..63d7427
--- /dev/null
+++ b/tests/libtracker-miner/miners-mock.c
@@ -0,0 +1,245 @@
+#include <glib-object.h>
+#include <gobject/gvaluecollector.h>
+#include <dbus/dbus-glib.h>
+#include "empty-gobject.h"
+#include "miners-mock.h"
+#include "tracker-miner-mock.h"
+
+#include <string.h>
+
+GHashTable *miners = NULL;
+
+void
+miners_mock_init ()
+{
+        TrackerMinerMock *miner;
+
+        miners = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+
+        miner = tracker_miner_mock_new (MOCK_MINER_1);
+        tracker_miner_mock_set_paused (miner, FALSE);
+        g_hash_table_insert (miners, MOCK_MINER_1, miner);
+
+        miner = tracker_miner_mock_new (MOCK_MINER_2);
+        tracker_miner_mock_set_paused (miner, TRUE);
+        g_hash_table_insert (miners, MOCK_MINER_2, miner);
+}
+
+/*
+ * DBus overrides
+ */
+DBusGConnection * 	
+dbus_g_bus_get (DBusBusType type, GError **error) 
+{
+        return (DBusGConnection *) empty_object_new ();
+}
+
+DBusGProxy *
+dbus_g_proxy_new_for_name (DBusGConnection *connection,
+                           const gchar *service,
+                           const gchar *path,
+                           const gchar *interface )
+{
+        TrackerMinerMock *miner;
+
+        miner = (TrackerMinerMock *)g_hash_table_lookup (miners, service);
+        if (!miner) {
+                return (DBusGProxy *) empty_object_new ();
+        }
+        return (DBusGProxy *) miner;
+}
+
+void 	
+dbus_g_proxy_add_signal (DBusGProxy *proxy, const char *signal_name, GType first_type,...)
+{
+}
+
+void 	
+dbus_g_proxy_connect_signal (DBusGProxy *proxy, 
+                             const char *signal_name, 
+                             GCallback handler, 
+                             void *data, 
+                             GClosureNotify free_data_func)
+{
+        TrackerMinerMock *miner = (TrackerMinerMock *)proxy;
+        
+        if (g_strcmp0 (signal_name, "NameOwnerChanged") == 0) {
+                return;
+        }
+
+        g_signal_connect (miner, g_utf8_strdown (signal_name, -1), handler, data);
+        
+}
+
+/*
+ * Two mock miners available but only 1 running
+ */
+gboolean
+dbus_g_proxy_call (DBusGProxy *proxy,
+                   const gchar *function_name,
+                   GError  **error,
+                   GType first_arg_type, ...)
+{
+        va_list args;
+        GType   arg_type;
+        GError *local_error = NULL;
+        const gchar *running_services[] = { "org.gnome.Tomboy",
+                                            "org.gnome.GConf",
+                                            MOCK_MINER_1, 
+                                            "org.gnome.SessionManager",
+                                            NULL};
+
+        va_start (args, first_arg_type);
+                
+        if (g_strcmp0 (function_name, "ListNames") == 0) {
+                /* 
+                 *  G_TYPE_INVALID,
+                 *  G_TYPE_STRV, &result,
+                 *  G_TYPE_INVALID
+                 */
+                GValue value = { 0, };
+
+                arg_type = va_arg (args, GType);
+
+                g_assert (arg_type == G_TYPE_STRV);
+                g_value_init (&value, arg_type);
+                g_value_set_boxed (&value, running_services);
+                G_VALUE_LCOPY (&value, 
+                               args, 0, 
+                               &local_error);
+                g_value_unset (&value);
+
+        } else if (g_strcmp0 (function_name, "NameHasOwner") == 0) {
+                /*
+                 * G_TYPE_STRING, miner,
+                 * G_TYPE_INVALID,
+                 * G_TYPE_BOOLEAN, &active,
+                 *  G_TYPE_INVALID)) {
+                 */ 
+                GValue value = { 0, };
+                const gchar *miner_name;
+                TrackerMinerMock *miner;
+                gboolean     active;
+
+                g_value_init (&value, G_TYPE_STRING);
+                G_VALUE_COLLECT (&value, args, 0, &local_error);
+                miner_name = g_value_get_string (&value);
+
+                miner = (TrackerMinerMock *)g_hash_table_lookup (miners, miner_name);
+                active = !tracker_miner_mock_get_paused (miner);
+                g_value_unset (&value);
+
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_INVALID);
+
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_BOOLEAN);
+                g_value_init (&value, arg_type);
+                g_value_set_boolean (&value, active);
+                G_VALUE_LCOPY (&value, 
+                               args, 0, 
+                               &local_error);
+                g_value_unset (&value);
+                
+        } else if (g_strcmp0 (function_name, "GetPauseDetails") == 0) {
+                /* 
+                 *  G_TYPE_INVALID,
+                 *  G_TYPE_STRV, &apps,
+                 *  G_TYPE_STRV, &reasons,
+                 *  G_TYPE_INVALID
+                 */
+                GValue value = { 0, };
+                gint   amount;
+                gchar **apps, **reasons;
+                TrackerMinerMock *miner = (TrackerMinerMock *)proxy;
+                
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_STRV);
+                g_value_init (&value, arg_type);
+                apps = tracker_miner_mock_get_apps (miner, &amount);
+                if (apps == NULL || amount == 0) {
+                        apps = g_new0 (gchar *, 1);
+                }
+                g_value_set_boxed (&value, apps);
+                G_VALUE_LCOPY (&value, 
+                               args, 0, 
+                               &local_error);
+                g_value_unset (&value);
+
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_STRV);
+                g_value_init (&value, arg_type);
+                reasons = tracker_miner_mock_get_reasons (miner, &amount);
+                if (reasons == NULL || amount == 0) {
+                        reasons = g_new0 (gchar *, 1);
+                }
+                g_value_set_boxed (&value, reasons);
+                G_VALUE_LCOPY (&value, 
+                               args, 0, 
+                               &local_error);
+                g_value_unset (&value);
+
+        } else if (g_strcmp0 (function_name, "Pause") == 0) {
+                /* 
+                 *  G_TYPE_STRING, &app,
+                 *  G_TYPE_STRING, &reason,
+                 *  G_TYPE_INVALID,
+                 *  G_TYPE_INT, &cookie,
+                 *  G_TYPE_INVALID
+                 */
+                GValue value_app = { 0, };
+                GValue value_reason = {0, };
+                const gchar *app;
+                const gchar *reason;
+                TrackerMinerMock *miner = (TrackerMinerMock *)proxy;
+
+                g_value_init (&value_app, G_TYPE_STRING);
+                G_VALUE_COLLECT (&value_app, args, 0, &local_error);
+                app = g_value_get_string (&value_app);
+
+                arg_type = va_arg (args, GType);
+                g_value_init (&value_reason, G_TYPE_STRING);
+                G_VALUE_COLLECT (&value_reason, args, 0, &local_error);
+                reason = g_value_get_string (&value_reason);
+
+                tracker_miner_mock_pause (miner, app, reason);
+
+        } else if (g_strcmp0 (function_name, "Resume") == 0) {
+                /*
+                 * G_TYPE_INT, &cookie
+                 * G_TYPE_INVALID
+                 */
+                TrackerMinerMock *miner = (TrackerMinerMock *)proxy;
+                tracker_miner_mock_resume (miner);
+
+        } else if (g_strcmp0 (function_name, "IgnoreNextUpdate") == 0) {
+                /* Well, ok... */
+        } else if (g_strcmp0 (function_name, "GetProgress") == 0) {
+                /* Whatever */
+        } else if (g_strcmp0 (function_name, "GetStatus") == 0) {
+                /* Whatever */
+        } else {
+                g_critical ("dbus_g_proxy_call '%s' unsupported", function_name);
+        }
+        
+        va_end (args);
+        *error == NULL;
+        return TRUE;
+}
+
+
+void              
+dbus_g_proxy_call_no_reply (DBusGProxy        *proxy,
+                            const char        *method,
+                            GType              first_arg_type,
+                            ...)
+{
+}
+
+
+void
+dbus_g_connection_unref (DBusGConnection *conn) 
+{
+        /* It is an EmptyGObject */
+        g_object_unref (conn);
+}
diff --git a/tests/libtracker-miner/miners-mock.h b/tests/libtracker-miner/miners-mock.h
new file mode 100644
index 0000000..cb46149
--- /dev/null
+++ b/tests/libtracker-miner/miners-mock.h
@@ -0,0 +1,23 @@
+#ifndef __MINERS_MOCK_H__
+#define __MINERS_MOCK_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define MOCK_MINER_1 "org.freedesktop.Tracker1.Miner.Mock1"
+#define MOCK_MINER_2 "org.freedesktop.Tracker1.Miner.Mock2"
+
+/*
+ * Assumptions:
+ *
+ *  There are this two miners, 
+ *  Initial state: Mock1 is running, Mock2 is paused
+ *
+ */
+void    miners_mock_init (void);
+
+G_END_DECLS
+
+
+#endif
diff --git a/tests/libtracker-miner/tracker-miner-manager-test.c b/tests/libtracker-miner/tracker-miner-manager-test.c
new file mode 100644
index 0000000..1cec295
--- /dev/null
+++ b/tests/libtracker-miner/tracker-miner-manager-test.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2010, Nokia (urho konttori nokia com)
+ *
+ * This library 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 library 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 library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+#include <glib.h>
+#include <glib-object.h>
+#include <libtracker-miner/tracker-miner-manager.h>
+#include "miners-mock.h"
+
+
+/*
+ * miners-mock.c implements the DBus magic and has hardcoded
+ *   some test data.
+ */
+
+
+TrackerMinerManager *manager = NULL;
+
+static void
+test_miner_manager_get_running () 
+{
+        GSList *running_miners;
+
+        running_miners = tracker_miner_manager_get_running (manager);
+
+        g_assert_cmpint (g_slist_length (running_miners), ==, 1);
+        g_assert_cmpstr (running_miners->data, ==, MOCK_MINER_1);
+}
+
+static void
+test_miner_manager_get_available () 
+{
+
+        GSList *available_miners;
+        GSList *item;
+
+        available_miners = tracker_miner_manager_get_available (manager);
+
+        g_assert_cmpint (g_slist_length (available_miners), ==, 2);
+        
+        item = g_slist_find_custom (available_miners, 
+                                    MOCK_MINER_1,
+                                    (GCompareFunc) g_strcmp0);
+        g_assert (item != NULL);
+
+        item = g_slist_find_custom (available_miners, 
+                                    MOCK_MINER_2,
+                                    (GCompareFunc) g_strcmp0);
+        g_assert (item != NULL);
+}
+
+static void
+test_miner_manager_is_active () 
+{
+        g_assert (tracker_miner_manager_is_active (manager, MOCK_MINER_1));
+        g_assert (!tracker_miner_manager_is_active (manager, MOCK_MINER_2));
+}
+
+static void
+test_miner_manager_pause_resume ()
+{
+        GStrv   *apps = NULL, *reasons = NULL;
+        guint32 cookie;
+
+        g_assert (tracker_miner_manager_is_active (manager, MOCK_MINER_1));
+        g_assert (!tracker_miner_manager_is_paused (manager, MOCK_MINER_1, apps, reasons));
+
+        tracker_miner_manager_pause (manager, MOCK_MINER_1, "Testing pause", &cookie);
+
+        g_assert (!tracker_miner_manager_is_active (manager, MOCK_MINER_1));
+        g_assert (tracker_miner_manager_is_paused (manager, MOCK_MINER_1, apps, reasons));
+
+        tracker_miner_manager_resume (manager, MOCK_MINER_1, cookie);
+
+        g_assert (tracker_miner_manager_is_active (manager, MOCK_MINER_1));
+        g_assert (!tracker_miner_manager_is_paused (manager, MOCK_MINER_1, apps, reasons));
+}
+
+static void
+test_miner_manager_getters ()
+{
+        const gchar *result;
+
+        result = tracker_miner_manager_get_display_name (manager, MOCK_MINER_1);
+        g_assert_cmpstr (result, ==, "Mock miner for testing");
+
+        result = tracker_miner_manager_get_description  (manager, MOCK_MINER_1);
+        g_assert_cmpstr (result, ==, "Comment in the mock miner");
+
+        result = tracker_miner_manager_get_display_name (manager, MOCK_MINER_2);
+        g_assert_cmpstr (result, ==, "Yet another mock miner");
+
+        result = tracker_miner_manager_get_description  (manager, MOCK_MINER_2);
+        g_assert_cmpstr (result, ==, "Stupid and tedious test for the comment");
+}
+
+static void
+test_miner_manager_ignore_next_update ()
+{
+        const gchar *urls[] = { "url1", "url2", "url3", NULL };
+
+        /* Lame, this doesn't test nothing... but improves coverage numbers */
+        g_assert (tracker_miner_manager_ignore_next_update (manager, MOCK_MINER_1, urls));
+}
+
+static void
+test_miner_manager_get_status ()
+{
+        gchar   *status;
+        gdouble  progress;
+        g_assert (tracker_miner_manager_get_status (manager, MOCK_MINER_1, &status, &progress));
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+        gint result;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+        
+	g_test_message ("Testing miner manager");
+
+        miners_mock_init ();
+        g_setenv ("TRACKER_MINERS_DIR", "./mock-miners", TRUE);
+        manager = tracker_miner_manager_new ();
+
+	g_test_add_func ("/libtracker-miner/tracker-miner-manager/get_running",
+                         test_miner_manager_get_running);
+
+	g_test_add_func ("/libtracker-miner/tracker-miner-manager/get_available",
+                         test_miner_manager_get_available);
+
+        g_test_add_func ("/libtracker-miner/tracker-miner-manager/is_active",
+                         test_miner_manager_is_active);
+
+        g_test_add_func ("/libtracker-miner/tracker-miner-manager/pause_resume",
+                         test_miner_manager_pause_resume);
+
+        g_test_add_func ("/libtracker-miner/tracker-miner-manager/getters",
+                         test_miner_manager_getters);
+
+        g_test_add_func ("/libtracker-miner/tracker-miner-manager/ignore_next_update",
+                         test_miner_manager_ignore_next_update);
+
+        g_test_add_func ("/libtracker-miner/tracker-miner-manager/status",
+                         test_miner_manager_get_status);
+
+        result = g_test_run ();
+        
+        g_object_unref (manager);
+        g_unsetenv ("TRACKER_MINERS_DIR");
+
+	return result;
+}
diff --git a/tests/libtracker-miner/tracker-miner-mock.vala b/tests/libtracker-miner/tracker-miner-mock.vala
new file mode 100644
index 0000000..39f666e
--- /dev/null
+++ b/tests/libtracker-miner/tracker-miner-mock.vala
@@ -0,0 +1,48 @@
+using GLib;
+
+public class TrackerMinerMock : GLib.Object {
+
+    public bool is_paused ;
+    public string pause_reason { get; set; default = ""; }
+    public string name { get; set; default = ""; } 
+    public string[] apps {get; set; }
+    public string[] reasons {get; set; }
+
+    public signal void progress (string miner, string status, double progress);
+    public signal void paused ();
+    public signal void resumed ();
+
+    public TrackerMinerMock (string name) {
+        this.name = name;
+        this.apps = {};
+        this.reasons = {};
+    }
+
+    public void set_paused (bool paused) { this.is_paused = paused; }
+    public bool get_paused () { return this.is_paused ; }
+
+    public void pause (string app, string reason) {
+
+        if (this.apps == null) {
+            this.apps = { app };
+        } else {
+            //this.apps += app;
+        }
+
+        if (this.reasons == null) {
+            this.reasons = { reason };
+        } else {
+            //this.reasons += reason;
+        }
+        this.is_paused = true;
+        this.paused ();
+    }
+
+    public void resume () {
+        this.apps = null;
+        this.reasons = null;
+        this.is_paused = false;
+        this.resumed ();
+    }
+
+}
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]