[tracker] Test tracker-thumbnailer mocking the dbus functions



commit dbbf1c4ac2153b203b93bea7e0e0c5b8a2d94ee8
Author: Ivan Frade <ivan frade nokia com>
Date:   Thu Mar 11 18:57:34 2010 +0200

    Test tracker-thumbnailer mocking the dbus functions

 tests/libtracker-miner/Makefile.am                |   16 ++-
 tests/libtracker-miner/empty-gobject.c            |  140 +++++++++++++++++++
 tests/libtracker-miner/empty-gobject.h            |   43 ++++++
 tests/libtracker-miner/thumbnailer-mock.c         |  112 ++++++++++++++++
 tests/libtracker-miner/thumbnailer-mock.h         |   14 ++
 tests/libtracker-miner/tracker-thumbnailer-test.c |  148 +++++++++++++++++++++
 6 files changed, 472 insertions(+), 1 deletions(-)
---
diff --git a/tests/libtracker-miner/Makefile.am b/tests/libtracker-miner/Makefile.am
index 84606d7..d85d65e 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
+TEST_PROGS += tracker-crawler tracker-thumbnailer-test
 
 INCLUDES = 									\
 	-DTEST									\
@@ -34,6 +34,20 @@ tracker_crawler_LDADD = 							\
 	$(GTHREAD_LIBS)								\
 	$(GLIB2_LIBS)
 
+tracker_thumbnailer_test_SOURCES = 						\
+	tracker-thumbnailer-test.c						\
+	thumbnailer-mock.c							\
+	empty-gobject.c
+
+tracker_thumbnailer_test_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)
+
 EXTRA_DIST = \
 	data/empty-dir/.hidden \
 	data/file1 \
diff --git a/tests/libtracker-miner/empty-gobject.c b/tests/libtracker-miner/empty-gobject.c
new file mode 100644
index 0000000..4c0bbad
--- /dev/null
+++ b/tests/libtracker-miner/empty-gobject.c
@@ -0,0 +1,140 @@
+/* empty-gobject.c generated by valac, the Vala compiler
+ * generated from empty-gobject.vala, do not modify */
+
+
+#include <glib.h>
+#include <glib-object.h>
+
+
+#define TYPE_EMPTY_OBJECT (empty_object_get_type ())
+#define EMPTY_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_EMPTY_OBJECT, EmptyObject))
+#define EMPTY_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_EMPTY_OBJECT, EmptyObjectClass))
+#define IS_EMPTY_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_EMPTY_OBJECT))
+#define IS_EMPTY_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_EMPTY_OBJECT))
+#define EMPTY_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_EMPTY_OBJECT, EmptyObjectClass))
+
+typedef struct _EmptyObject EmptyObject;
+typedef struct _EmptyObjectClass EmptyObjectClass;
+typedef struct _EmptyObjectPrivate EmptyObjectPrivate;
+
+struct _EmptyObject {
+	GObject parent_instance;
+	EmptyObjectPrivate * priv;
+};
+
+struct _EmptyObjectClass {
+	GObjectClass parent_class;
+};
+
+struct _EmptyObjectPrivate {
+	gint _id;
+};
+
+
+static gpointer empty_object_parent_class = NULL;
+
+GType empty_object_get_type (void);
+#define EMPTY_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_EMPTY_OBJECT, EmptyObjectPrivate))
+enum  {
+	EMPTY_OBJECT_DUMMY_PROPERTY,
+	EMPTY_OBJECT_ID
+};
+EmptyObject* empty_object_new (void);
+EmptyObject* empty_object_construct (GType object_type);
+gint empty_object_get_id (EmptyObject* self);
+void empty_object_set_id (EmptyObject* self, gint value);
+static void empty_object_finalize (GObject* obj);
+static void empty_object_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
+static void empty_object_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);
+
+
+
+EmptyObject* empty_object_construct (GType object_type) {
+	EmptyObject * self;
+	self = (EmptyObject*) g_object_new (object_type, NULL);
+	return self;
+}
+
+
+EmptyObject* empty_object_new (void) {
+	return empty_object_construct (TYPE_EMPTY_OBJECT);
+}
+
+
+gint empty_object_get_id (EmptyObject* self) {
+	gint result;
+	g_return_val_if_fail (self != NULL, 0);
+	result = self->priv->_id;
+	return result;
+}
+
+
+void empty_object_set_id (EmptyObject* self, gint value) {
+	g_return_if_fail (self != NULL);
+	self->priv->_id = value;
+	g_object_notify ((GObject *) self, "id");
+}
+
+
+static void empty_object_class_init (EmptyObjectClass * klass) {
+	empty_object_parent_class = g_type_class_peek_parent (klass);
+	g_type_class_add_private (klass, sizeof (EmptyObjectPrivate));
+	G_OBJECT_CLASS (klass)->get_property = empty_object_get_property;
+	G_OBJECT_CLASS (klass)->set_property = empty_object_set_property;
+	G_OBJECT_CLASS (klass)->finalize = empty_object_finalize;
+	g_object_class_install_property (G_OBJECT_CLASS (klass), EMPTY_OBJECT_ID, g_param_spec_int ("id", "id", "id", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
+}
+
+
+static void empty_object_instance_init (EmptyObject * self) {
+	self->priv = EMPTY_OBJECT_GET_PRIVATE (self);
+}
+
+
+static void empty_object_finalize (GObject* obj) {
+	EmptyObject * self;
+	self = EMPTY_OBJECT (obj);
+	G_OBJECT_CLASS (empty_object_parent_class)->finalize (obj);
+}
+
+
+GType empty_object_get_type (void) {
+	static GType empty_object_type_id = 0;
+	if (empty_object_type_id == 0) {
+		static const GTypeInfo g_define_type_info = { sizeof (EmptyObjectClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) empty_object_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (EmptyObject), 0, (GInstanceInitFunc) empty_object_instance_init, NULL };
+		empty_object_type_id = g_type_register_static (G_TYPE_OBJECT, "EmptyObject", &g_define_type_info, 0);
+	}
+	return empty_object_type_id;
+}
+
+
+static void empty_object_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
+	EmptyObject * self;
+	self = EMPTY_OBJECT (object);
+	switch (property_id) {
+		case EMPTY_OBJECT_ID:
+		g_value_set_int (value, empty_object_get_id (self));
+		break;
+		default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+
+static void empty_object_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
+	EmptyObject * self;
+	self = EMPTY_OBJECT (object);
+	switch (property_id) {
+		case EMPTY_OBJECT_ID:
+		empty_object_set_id (self, g_value_get_int (value));
+		break;
+		default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+
+
+
diff --git a/tests/libtracker-miner/empty-gobject.h b/tests/libtracker-miner/empty-gobject.h
new file mode 100644
index 0000000..c760ac7
--- /dev/null
+++ b/tests/libtracker-miner/empty-gobject.h
@@ -0,0 +1,43 @@
+/* empty-gobject.h generated by valac, the Vala compiler, do not modify */
+
+
+#ifndef __EMPTY_GOBJECT_H__
+#define __EMPTY_GOBJECT_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+
+#define TYPE_EMPTY_OBJECT (empty_object_get_type ())
+#define EMPTY_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_EMPTY_OBJECT, EmptyObject))
+#define EMPTY_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_EMPTY_OBJECT, EmptyObjectClass))
+#define IS_EMPTY_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_EMPTY_OBJECT))
+#define IS_EMPTY_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_EMPTY_OBJECT))
+#define EMPTY_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_EMPTY_OBJECT, EmptyObjectClass))
+
+typedef struct _EmptyObject EmptyObject;
+typedef struct _EmptyObjectClass EmptyObjectClass;
+typedef struct _EmptyObjectPrivate EmptyObjectPrivate;
+
+struct _EmptyObject {
+	GObject parent_instance;
+	EmptyObjectPrivate * priv;
+};
+
+struct _EmptyObjectClass {
+	GObjectClass parent_class;
+};
+
+
+GType empty_object_get_type (void);
+EmptyObject* empty_object_new (void);
+EmptyObject* empty_object_construct (GType object_type);
+gint empty_object_get_id (EmptyObject* self);
+void empty_object_set_id (EmptyObject* self, gint value);
+
+
+G_END_DECLS
+
+#endif
diff --git a/tests/libtracker-miner/thumbnailer-mock.c b/tests/libtracker-miner/thumbnailer-mock.c
new file mode 100644
index 0000000..65141cb
--- /dev/null
+++ b/tests/libtracker-miner/thumbnailer-mock.c
@@ -0,0 +1,112 @@
+#include <glib-object.h>
+#include <gobject/gvaluecollector.h>
+#include <dbus/dbus-glib.h>
+#include "empty-gobject.h"
+#include "thumbnailer-mock.h"
+
+static GList *calls = NULL;
+
+void 
+dbus_mock_call_log_reset () 
+{
+        if (calls) {
+                g_list_foreach (calls, (GFunc)g_free, NULL);
+                g_list_free (calls);
+                calls = NULL;
+        }
+}
+
+GList *
+dbus_mock_call_log_get ()
+{
+        return calls;
+}
+
+static void
+dbus_mock_call_log_append (const gchar *function_name)
+{
+        calls = g_list_append (calls, g_strdup (function_name));
+}
+
+
+
+/*
+ * 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 )
+{
+        return (DBusGProxy *) empty_object_new ();
+}
+
+gboolean
+dbus_g_proxy_call (DBusGProxy *proxy,
+                   const gchar *function_name,
+                   GError  **error,
+                   GType first_arg_type, ...)
+{
+        va_list args;
+        GType arg_type;
+        const gchar *supported_mimes[] = { "mock/one", "mock/two", NULL};
+        GError *local_error = NULL;
+
+        g_assert (g_strcmp0 (function_name, "GetSupported") == 0);
+        
+        /* 
+	   G_TYPE_INVALID,
+	   G_TYPE_STRV, &uri_schemes,
+	   G_TYPE_STRV, &mime_types,
+	   G_TYPE_INVALID);
+
+           Set the mock values in the second parameter :)
+        */
+
+        va_start (args, first_arg_type);
+        arg_type = va_arg (args, GType);
+
+        int counter = 1;
+        while (arg_type != G_TYPE_INVALID) {
+                
+                if (arg_type == G_TYPE_STRV && counter == 2) {
+                        GValue value = { 0, };
+                        g_value_init (&value, arg_type);
+                        g_value_set_boxed (&value, supported_mimes);
+                        G_VALUE_LCOPY (&value, 
+                                       args, 0, 
+                                       &local_error);
+                        g_value_unset (&value);
+                } else {
+                        gpointer *out_param;
+                        out_param = va_arg (args, gpointer *);
+                }
+                arg_type = va_arg (args, GType);
+                counter += 1;
+        }
+
+        va_end (args);
+
+        *error == NULL;
+        return TRUE;
+}
+
+
+void              
+dbus_g_proxy_call_no_reply (DBusGProxy        *proxy,
+                            const char        *method,
+                            GType              first_arg_type,
+                            ...)
+{
+        dbus_mock_call_log_append (method);
+}
+
+
diff --git a/tests/libtracker-miner/thumbnailer-mock.h b/tests/libtracker-miner/thumbnailer-mock.h
new file mode 100644
index 0000000..6334620
--- /dev/null
+++ b/tests/libtracker-miner/thumbnailer-mock.h
@@ -0,0 +1,14 @@
+#ifndef __THUMBNAILER_MOCK_H__
+#define __THUMBNAILER_MOCK_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void    dbus_mock_call_log_reset (void);
+GList * dbus_mock_call_log_get   (void);
+
+G_END_DECLS
+
+
+#endif
diff --git a/tests/libtracker-miner/tracker-thumbnailer-test.c b/tests/libtracker-miner/tracker-thumbnailer-test.c
new file mode 100644
index 0000000..612cef3
--- /dev/null
+++ b/tests/libtracker-miner/tracker-thumbnailer-test.c
@@ -0,0 +1,148 @@
+/*
+ * 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-thumbnailer.h>
+#include "thumbnailer-mock.h"
+
+
+static void
+test_thumbnailer_init ()
+{
+        g_assert (tracker_thumbnailer_init ());
+
+        tracker_thumbnailer_shutdown ();
+}
+
+static void
+test_thumbnailer_send_empty () 
+{
+        GList *dbus_calls = NULL;
+
+        dbus_mock_call_log_reset ();
+
+        tracker_thumbnailer_init ();
+        tracker_thumbnailer_send ();
+
+        dbus_calls = dbus_mock_call_log_get ();
+        g_assert (dbus_calls == NULL);
+
+        tracker_thumbnailer_shutdown ();
+}
+
+static void
+test_thumbnailer_send_moves ()
+{
+        GList *dbus_calls = NULL;
+        
+        dbus_mock_call_log_reset ();
+
+        tracker_thumbnailer_init ();
+
+        /* Returns TRUE, but there is no dbus call */
+        g_assert (tracker_thumbnailer_move_add ("file://a.jpeg", "mock/one", "file://b.jpeg"));
+        g_assert (dbus_mock_call_log_get () == NULL);
+
+        /* Returns FALSE, unsupported mime */
+        g_assert (!tracker_thumbnailer_move_add ("file://a.jpeg", "unsupported", "file://b.jpeg"));
+        g_assert (dbus_mock_call_log_get () == NULL);
+
+        tracker_thumbnailer_send ();
+
+        /* One call to "move" method */
+        dbus_calls = dbus_mock_call_log_get ();
+        g_assert_cmpint (g_list_length (dbus_calls), ==, 1);
+        g_assert_cmpstr (dbus_calls->data, ==, "Move");
+
+        tracker_thumbnailer_shutdown ();
+        dbus_mock_call_log_reset ();
+}
+
+static void
+test_thumbnailer_send_removes ()
+{
+        GList *dbus_calls = NULL;
+        
+        dbus_mock_call_log_reset ();
+
+        tracker_thumbnailer_init ();
+
+        /* Returns TRUE, but there is no dbus call */
+        g_assert (tracker_thumbnailer_remove_add ("file://a.jpeg", "mock/one"));
+        g_assert (dbus_mock_call_log_get () == NULL);
+
+        /* Returns FALSE, unsupported mime */
+        g_assert (!tracker_thumbnailer_remove_add ("file://a.jpeg", "unsupported"));
+        g_assert (dbus_mock_call_log_get () == NULL);
+
+        tracker_thumbnailer_send ();
+
+        /* One call to "Delete" method */
+        dbus_calls = dbus_mock_call_log_get ();
+        g_assert_cmpint (g_list_length (dbus_calls), ==, 1);
+        g_assert_cmpstr (dbus_calls->data, ==, "Delete");
+
+        tracker_thumbnailer_shutdown ();
+        dbus_mock_call_log_reset ();
+}
+
+static void
+test_thumbnailer_send_cleanup ()
+{
+        GList *dbus_calls = NULL;
+        
+        dbus_mock_call_log_reset ();
+
+        tracker_thumbnailer_init ();
+
+        /* Returns TRUE, and there is a dbus call */
+        g_assert (tracker_thumbnailer_cleanup ("file://tri/lu/ri"));
+
+        /* One call to "Clean" method */
+        dbus_calls = dbus_mock_call_log_get ();
+        g_assert_cmpint (g_list_length (dbus_calls), ==, 1);
+        g_assert_cmpstr (dbus_calls->data, ==, "Cleanup");
+
+        tracker_thumbnailer_shutdown ();
+        dbus_mock_call_log_reset ();
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	g_test_message ("Testing filesystem crawler");
+
+	g_test_add_func ("/libtracker-miner/tracker-thumbnailer/init",
+	                 test_thumbnailer_init);
+	g_test_add_func ("/libtracker-miner/tracker-thumbnailer/send_empty",
+	                 test_thumbnailer_send_empty);
+        g_test_add_func ("/libtracker-minter/tracker-thumbnailer/send_moves",
+                         test_thumbnailer_send_moves);
+        g_test_add_func ("/libtracker-minter/tracker-thumbnailer/send_removes",
+                         test_thumbnailer_send_removes);
+        g_test_add_func ("/libtracker-minter/tracker-thumbnailer/send_cleanup",
+                         test_thumbnailer_send_cleanup);
+
+	return g_test_run ();
+}



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