From ca3c4de85c8055f039396b6270ac476e3aa5df61 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Wed, 22 Aug 2012 00:03:48 +0200 Subject: [PATCH] WIP Retain C test cases from libzeitgeist1 diff --git a/Makefile.am b/Makefile.am index 7b5731a..7b28551 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,3 +52,6 @@ test-dbus: all test-direct: all cd ./test/direct/ && make run; + +test-c: all + cd ./test/c/ && make run; diff --git a/test/Makefile.am b/test/Makefile.am index a4da622..687cc7a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,4 +3,5 @@ NULL = SUBDIRS = \ dbus \ direct \ + c \ $(NULL) diff --git a/test/c/Makefile.am b/test/c/Makefile.am new file mode 100644 index 0000000..04c20ca --- /dev/null +++ b/test/c/Makefile.am @@ -0,0 +1,68 @@ +include $(top_srcdir)/Makefile.decl + +INCLUDES = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(GIO_UNIX_CFLAGS) \ + $(SQLITE_CFLAGS) \ + $(ZEITGEIST_CFLAGS) + +AM_CFLAGS = \ + -Wall \ + -g \ + -DZEITGEIST_COMPILATION \ + -DTEST_DIR=\"$(top_srcdir)/tests\" + +zeitgeist_libs = $(top_builddir)/src/libzeitgeist-2.0.la $(ZEITGEIST_LIBS) + +EXTRA_DIST += \ + test.desktop + +SRC_FILES = \ + $(top_srcdir)/src/mimetype.c \ + $(top_srcdir)/src/mimetype.h \ + test.desktop + +noinst_PROGRAMS = $(TEST_PROGS) + +test-monitor: test-monitor.c $(SRC_FILES) + $(CC) $(CFLAGS) -o $@ $^ + +TESTS = \ + test-monitor \ + $(NULL) + + +clean-local: + rm -f *.~[0-9]~ + +CLEANFILES = \ + datamodel-test \ + datasource-test \ + event-test \ + log-test \ + marshalling-test \ + mimetype-test \ + query-operators-test \ + table-lookup-test \ + where-clause-test \ + $(NULL) + + +# HEADLESS CHECKS +# Start up a new Zeitgeist on a private bus using only a +# memory backed log database. We also need a fake X server +# because dbus-launch requires an X server... +check-headless: + export ZEITGEIST_DATABASE_PATH=":memory:"; \ + export DISPLAY=":1"; \ + Xvfb :1 -screen 0 1024x768x8 & \ + dbus-launch > _sessionbus.sh; \ + source _sessionbus.sh; \ + cat _sessionbus.sh; \ + zeitgeist-daemon --quit; \ + zeitgeist-daemon --no-datahub & \ + make check; \ + zeitgeist-daemon --quit; \ + kill `grep DBUS_SESSION_BUS_PID _sessionbus.sh | grep -oE '[0-9]+'`; \ + pkill Xvfb; \ + rm _sessionbus.sh diff --git a/test/c/test-datasource.c b/test/c/test-datasource.c new file mode 100644 index 0000000..a301500 --- /dev/null +++ b/test/c/test-datasource.c @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include + +#include "zeitgeist-event.h" +#include "zeitgeist-data-source.h" +#include "zeitgeist-ontology-interpretations.h" +#include "zeitgeist-ontology-manifestations.h" +#include "zeitgeist-timestamp.h" + + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static const gchar *old_xdg_data_dirs = NULL; + +static void +setup (Fixture *fix, gconstpointer data) +{ + if (old_xdg_data_dirs != NULL) + old_xdg_data_dirs = g_getenv ("XDG_DATA_DIRS"); + g_setenv ("XDG_DATA_DIRS", TEST_DIR, TRUE); +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + g_setenv ("XDG_DATA_DIRS", old_xdg_data_dirs, TRUE); +} + +static void +test_create_empty (Fixture *fix, gconstpointer data) +{ + ZeitgeistDataSource *src; + + src = zeitgeist_data_source_new (); + + g_assert_cmpstr (NULL, ==, zeitgeist_data_source_get_unique_id (src)); + g_assert_cmpstr (NULL, ==, zeitgeist_data_source_get_name (src)); + g_assert_cmpstr (NULL, ==, zeitgeist_data_source_get_description (src)); + g_assert (NULL == zeitgeist_data_source_get_event_templates (src)); + g_assert_cmpint (0, ==, zeitgeist_data_source_is_running (src)); + g_assert (0 == zeitgeist_data_source_get_timestamp (src)); + g_assert_cmpint (1, ==, zeitgeist_data_source_is_enabled (src)); + + g_object_unref (src); +} + +static void +test_create_full (Fixture *fix, gconstpointer data) +{ + ZeitgeistDataSource *src; + GPtrArray *event_templates; + gint64 now; + + src = zeitgeist_data_source_new_full ("my-id", "my-name", + "my description", NULL); + + g_assert_cmpstr ("my-id", ==, zeitgeist_data_source_get_unique_id (src)); + g_assert_cmpstr ("my-name", ==, zeitgeist_data_source_get_name (src)); + g_assert_cmpstr ("my description", ==, zeitgeist_data_source_get_description (src)); + g_assert (NULL == zeitgeist_data_source_get_event_templates (src)); + g_assert_cmpint (0, ==, zeitgeist_data_source_is_running (src)); + g_assert (0 == zeitgeist_data_source_get_timestamp (src)); + g_assert_cmpint (1, ==, zeitgeist_data_source_is_enabled (src)); + + now = zeitgeist_timestamp_for_now (); + zeitgeist_data_source_set_running (src, TRUE); + zeitgeist_data_source_set_timestamp (src, now); + zeitgeist_data_source_set_enabled (src, FALSE); + + g_assert_cmpint (1, ==, zeitgeist_data_source_is_running (src)); + g_assert (now == zeitgeist_data_source_get_timestamp (src)); + g_assert_cmpint (0, ==, zeitgeist_data_source_is_enabled (src)); + + event_templates = g_ptr_array_new (); + g_ptr_array_add (event_templates, zeitgeist_event_new ()); + zeitgeist_data_source_set_event_templates (src, event_templates); // own ref + + g_assert (event_templates == zeitgeist_data_source_get_event_templates (src)); + + g_object_unref (src); +} + +static void +test_to_from_variant (Fixture *fix, gconstpointer data) +{ + ZeitgeistDataSource *orig, *src; + GPtrArray *event_templates; + gint64 now; + + /* Build the data source to serialize */ + orig = zeitgeist_data_source_new_full ("my-id", "my-name", + "my description", NULL); + + now = zeitgeist_timestamp_for_now (); + zeitgeist_data_source_set_timestamp (orig, now); + + event_templates = g_ptr_array_new (); + g_ptr_array_add (event_templates, zeitgeist_event_new ()); + zeitgeist_data_source_set_event_templates (orig, event_templates); // own ref + + /* Serialize + unserialize */ + src = zeitgeist_data_source_new_from_variant ( + zeitgeist_data_source_to_variant_full (orig) /* own ref */); + + g_assert_cmpstr ("my-id", ==, zeitgeist_data_source_get_unique_id (src)); + g_assert_cmpstr ("my-name", ==, zeitgeist_data_source_get_name (src)); + g_assert_cmpstr ("my description", ==, zeitgeist_data_source_get_description (src)); + g_assert (NULL != zeitgeist_data_source_get_event_templates (src)); + g_assert_cmpint (0, ==, zeitgeist_data_source_is_running (src)); + g_assert (now == zeitgeist_data_source_get_timestamp (src)); + g_assert_cmpint (1, ==, zeitgeist_data_source_is_enabled (src)); + + event_templates = zeitgeist_data_source_get_event_templates (src); + g_assert_cmpint (1, ==, event_templates->len); + g_assert (ZEITGEIST_IS_EVENT (g_ptr_array_index (event_templates, 0))); + + g_object_unref (src); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/DataSource/CreateEmpty", Fixture, NULL, + setup, test_create_empty, teardown); + g_test_add ("/Zeitgeist/DataSource/CreateFull", Fixture, NULL, + setup, test_create_full, teardown); + g_test_add ("/Zeitgeist/DataSource/ToFromVariant", Fixture, NULL, + setup, test_to_from_variant, teardown); + + return g_test_run(); +} diff --git a/test/c/test-event.c b/test/c/test-event.c new file mode 100644 index 0000000..169cf40 --- /dev/null +++ b/test/c/test-event.c @@ -0,0 +1,438 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include + +#include "zeitgeist-event.h" +#include "zeitgeist-ontology-interpretations.h" +#include "zeitgeist-ontology-manifestations.h" + + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static const gchar *old_xdg_data_dirs = NULL; + +static void +setup (Fixture *fix, gconstpointer data) +{ + if (old_xdg_data_dirs != NULL) + old_xdg_data_dirs = g_getenv ("XDG_DATA_DIRS"); + g_setenv ("XDG_DATA_DIRS", TEST_DIR, TRUE); +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + g_setenv ("XDG_DATA_DIRS", old_xdg_data_dirs, TRUE); +} + +static void +test_create_empty (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *ev; + + ev = zeitgeist_event_new (); + + g_assert_cmpint (0, ==, zeitgeist_event_get_id (ev)); + g_assert_cmpint (0, ==, zeitgeist_event_get_timestamp (ev)); + g_assert_cmpstr (NULL, ==, zeitgeist_event_get_interpretation (ev)); + g_assert_cmpstr (NULL, ==, zeitgeist_event_get_manifestation (ev)); + g_assert_cmpstr (NULL, ==, zeitgeist_event_get_actor (ev)); + g_assert_cmpstr (NULL, ==, zeitgeist_event_get_origin (ev)); + g_assert_cmpint (0, ==, zeitgeist_event_num_subjects (ev)); + g_assert (zeitgeist_event_get_payload (ev) == NULL); + + g_object_unref (ev); +} + +static void +test_create_full (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *ev; + ZeitgeistSubject *su; + + /* Test with two subjects, one of them empty */ + ev = zeitgeist_event_new_full ( + ZEITGEIST_ZG_ACCESS_EVENT, + ZEITGEIST_ZG_USER_ACTIVITY, + "application://firefox.desktop", + zeitgeist_subject_new_full ("http://example.com", + ZEITGEIST_NFO_WEBSITE, + ZEITGEIST_NFO_REMOTE_DATA_OBJECT, + "text/html", + "http://example.com", + "example.com", + "net"), + zeitgeist_subject_new (), + NULL); + + g_assert_cmpint (0, ==, zeitgeist_event_get_id (ev)); + g_assert_cmpint (0, ==, zeitgeist_event_get_timestamp (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_ACCESS_EVENT,==, zeitgeist_event_get_interpretation (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_USER_ACTIVITY, ==, zeitgeist_event_get_manifestation (ev)); + g_assert_cmpstr ("application://firefox.desktop", ==, zeitgeist_event_get_actor (ev)); + g_assert (zeitgeist_event_get_origin (ev) == NULL); + g_assert_cmpint (2, ==, zeitgeist_event_num_subjects (ev)); + g_assert (zeitgeist_event_get_payload (ev) == NULL); + + su = zeitgeist_event_get_subject (ev, 0); + g_assert_cmpstr ("http://example.com", ==, zeitgeist_subject_get_uri(su)); + g_assert_cmpstr (ZEITGEIST_NFO_WEBSITE, ==, zeitgeist_subject_get_interpretation (su)); + g_assert_cmpstr (ZEITGEIST_NFO_REMOTE_DATA_OBJECT, ==, zeitgeist_subject_get_manifestation (su)); + g_assert_cmpstr ("text/html", ==, zeitgeist_subject_get_mimetype (su)); + g_assert_cmpstr ("http://example.com", ==, zeitgeist_subject_get_origin (su)); + g_assert_cmpstr ("example.com", ==, zeitgeist_subject_get_text (su)); + g_assert_cmpstr ("net", ==, zeitgeist_subject_get_storage (su)); + g_assert (zeitgeist_subject_get_current_uri (su) == NULL); + + su = zeitgeist_event_get_subject (ev, 1); + g_assert (zeitgeist_subject_get_uri(su) == NULL); + g_assert (zeitgeist_subject_get_interpretation (su) == NULL); + g_assert (zeitgeist_subject_get_manifestation (su) == NULL); + g_assert (zeitgeist_subject_get_mimetype (su) == NULL); + g_assert (zeitgeist_subject_get_origin (su) == NULL); + g_assert (zeitgeist_subject_get_text (su) == NULL); + g_assert (zeitgeist_subject_get_storage (su) == NULL); + g_assert (zeitgeist_subject_get_current_uri (su) == NULL); + + g_object_unref (ev); +} + +static void +test_actor_from_app_info (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *ev; + GAppInfo *appinfo; + + appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (TEST_DIR"/test.desktop")); + g_assert (G_IS_APP_INFO (appinfo)); + + ev = zeitgeist_event_new (); + zeitgeist_event_set_actor_from_app_info (ev, appinfo); + + g_assert_cmpstr ("application://test.desktop", ==, zeitgeist_event_get_actor (ev)); +} + +static void +test_from_variant (Fixture *fix, gconstpointer data) +{ + GVariant *var; + GVariantBuilder b; + ZeitgeistEvent *ev; + ZeitgeistSubject *su; + GByteArray *payload; + + g_variant_builder_init (&b, ZEITGEIST_EVENT_VARIANT_TYPE); + + /* Build event data */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("as")); + g_variant_builder_add (&b, "s", "27"); + g_variant_builder_add (&b, "s", "68"); + g_variant_builder_add (&b, "s", ZEITGEIST_ZG_ACCESS_EVENT); + g_variant_builder_add (&b, "s", ZEITGEIST_ZG_USER_ACTIVITY); + g_variant_builder_add (&b, "s", "application://foo.desktop"); + g_variant_builder_close (&b); + + /* Build subjects */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("aas")); + { + g_variant_builder_open (&b, G_VARIANT_TYPE ("as")); + g_variant_builder_add (&b, "s", "file:///tmp/foo.txt"); + g_variant_builder_add (&b, "s", ZEITGEIST_NFO_DOCUMENT); + g_variant_builder_add (&b, "s", ZEITGEIST_NFO_FILE_DATA_OBJECT); + g_variant_builder_add (&b, "s", "file:///tmp"); + g_variant_builder_add (&b, "s", "text/plain"); + g_variant_builder_add (&b, "s", "foo.txt"); + g_variant_builder_add (&b, "s", "36e5604e-7e1b-4ebd-bb6a-184c6ea99627"); + g_variant_builder_close (&b); + } + g_variant_builder_close (&b); + + /* Build payload */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("ay")); + g_variant_builder_add (&b, "y", 1); + g_variant_builder_add (&b, "y", 2); + g_variant_builder_add (&b, "y", 3); + g_variant_builder_close (&b); + + var = g_variant_builder_end (&b); + ev = zeitgeist_event_new_from_variant (var); // var freed + + g_assert_cmpint (27, ==, zeitgeist_event_get_id (ev)); + g_assert_cmpint (68, ==, zeitgeist_event_get_timestamp (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_ACCESS_EVENT,==, zeitgeist_event_get_interpretation (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_USER_ACTIVITY, ==, zeitgeist_event_get_manifestation (ev)); + g_assert_cmpstr ("application://foo.desktop", ==, zeitgeist_event_get_actor (ev)); + g_assert (zeitgeist_event_get_origin (ev) == NULL); + g_assert_cmpint (1, ==, zeitgeist_event_num_subjects (ev)); + + su = zeitgeist_event_get_subject (ev, 0); + g_assert_cmpstr ("file:///tmp/foo.txt", ==, zeitgeist_subject_get_uri(su)); + g_assert_cmpstr (ZEITGEIST_NFO_DOCUMENT, ==, zeitgeist_subject_get_interpretation (su)); + g_assert_cmpstr (ZEITGEIST_NFO_FILE_DATA_OBJECT, ==, zeitgeist_subject_get_manifestation (su)); + g_assert_cmpstr ("text/plain", ==, zeitgeist_subject_get_mimetype (su)); + g_assert_cmpstr ("file:///tmp", ==, zeitgeist_subject_get_origin (su)); + g_assert_cmpstr ("foo.txt", ==, zeitgeist_subject_get_text (su)); + g_assert_cmpstr ("36e5604e-7e1b-4ebd-bb6a-184c6ea99627", ==, zeitgeist_subject_get_storage (su)); + g_assert (zeitgeist_subject_get_current_uri (su) == NULL); + + payload = zeitgeist_event_get_payload (ev); + g_assert (payload != NULL); + g_assert_cmpint (3, ==, payload->len); + g_assert_cmpint (1, ==, payload->data[0]); + g_assert_cmpint (2, ==, payload->data[1]); + g_assert_cmpint (3, ==, payload->data[2]); + + g_object_unref (ev); +} + +static void +test_from_variant_with_new_fields (Fixture *fix, gconstpointer data) +{ + GVariant *var; + GVariantBuilder b; + ZeitgeistEvent *ev; + ZeitgeistSubject *su; + GByteArray *payload; + + g_variant_builder_init (&b, ZEITGEIST_EVENT_VARIANT_TYPE); + + /* Build event data */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("as")); + g_variant_builder_add (&b, "s", "27"); + g_variant_builder_add (&b, "s", "68"); + g_variant_builder_add (&b, "s", ZEITGEIST_ZG_ACCESS_EVENT); + g_variant_builder_add (&b, "s", ZEITGEIST_ZG_USER_ACTIVITY); + g_variant_builder_add (&b, "s", "application://foo.desktop"); + g_variant_builder_add (&b, "s", "origin"); + g_variant_builder_close (&b); + + /* Build subjects */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("aas")); + { + g_variant_builder_open (&b, G_VARIANT_TYPE ("as")); + g_variant_builder_add (&b, "s", "file:///tmp/foo.txt"); + g_variant_builder_add (&b, "s", ZEITGEIST_NFO_DOCUMENT); + g_variant_builder_add (&b, "s", ZEITGEIST_NFO_FILE_DATA_OBJECT); + g_variant_builder_add (&b, "s", "file:///tmp"); + g_variant_builder_add (&b, "s", "text/plain"); + g_variant_builder_add (&b, "s", "foo.txt"); + g_variant_builder_add (&b, "s", "36e5604e-7e1b-4ebd-bb6a-184c6ea99627"); + g_variant_builder_add (&b, "s", "file:///tmp/current.txt"); + g_variant_builder_close (&b); + } + g_variant_builder_close (&b); + + /* Build payload */ + g_variant_builder_open (&b, G_VARIANT_TYPE ("ay")); + g_variant_builder_add (&b, "y", 1); + g_variant_builder_add (&b, "y", 2); + g_variant_builder_add (&b, "y", 3); + g_variant_builder_close (&b); + + var = g_variant_builder_end (&b); + ev = zeitgeist_event_new_from_variant (var); // var freed + + g_assert_cmpint (27, ==, zeitgeist_event_get_id (ev)); + g_assert_cmpint (68, ==, zeitgeist_event_get_timestamp (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_ACCESS_EVENT,==, zeitgeist_event_get_interpretation (ev)); + g_assert_cmpstr (ZEITGEIST_ZG_USER_ACTIVITY, ==, zeitgeist_event_get_manifestation (ev)); + g_assert_cmpstr ("application://foo.desktop", ==, zeitgeist_event_get_actor (ev)); + g_assert_cmpstr ("origin", ==, zeitgeist_event_get_origin (ev)); + g_assert_cmpint (1, ==, zeitgeist_event_num_subjects (ev)); + + su = zeitgeist_event_get_subject (ev, 0); + g_assert_cmpstr ("file:///tmp/foo.txt", ==, zeitgeist_subject_get_uri(su)); + g_assert_cmpstr (ZEITGEIST_NFO_DOCUMENT, ==, zeitgeist_subject_get_interpretation (su)); + g_assert_cmpstr (ZEITGEIST_NFO_FILE_DATA_OBJECT, ==, zeitgeist_subject_get_manifestation (su)); + g_assert_cmpstr ("text/plain", ==, zeitgeist_subject_get_mimetype (su)); + g_assert_cmpstr ("file:///tmp", ==, zeitgeist_subject_get_origin (su)); + g_assert_cmpstr ("foo.txt", ==, zeitgeist_subject_get_text (su)); + g_assert_cmpstr ("36e5604e-7e1b-4ebd-bb6a-184c6ea99627", ==, zeitgeist_subject_get_storage (su)); + g_assert_cmpstr ("file:///tmp/current.txt", ==, zeitgeist_subject_get_current_uri (su)); + + payload = zeitgeist_event_get_payload (ev); + g_assert (payload != NULL); + g_assert_cmpint (3, ==, payload->len); + g_assert_cmpint (1, ==, payload->data[0]); + g_assert_cmpint (2, ==, payload->data[1]); + g_assert_cmpint (3, ==, payload->data[2]); + + g_object_unref (ev); +} + +static void +test_empty_to_from_variant (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *orig, *marshalled; + + + orig = zeitgeist_event_new (); + marshalled = zeitgeist_event_new_from_variant (zeitgeist_event_to_variant (orig)); + + g_assert_cmpint (0, ==, zeitgeist_event_get_id (marshalled)); + g_assert_cmpint (0, ==, zeitgeist_event_get_timestamp (marshalled)); + g_assert (zeitgeist_event_get_interpretation (marshalled) == NULL); + g_assert (zeitgeist_event_get_manifestation (marshalled) == NULL); + g_assert (zeitgeist_event_get_actor (marshalled) == NULL); + g_assert (zeitgeist_event_get_origin (marshalled) == NULL); + g_assert_cmpint (0, ==, zeitgeist_event_num_subjects (marshalled)); + g_assert (zeitgeist_event_get_payload (marshalled) == NULL); + + g_object_unref (orig); + g_object_unref (marshalled); +} + +static void +test_with_one_subject_to_from_variant (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *orig, *marshalled; + ZeitgeistSubject *su; + GByteArray *payload; + guint8 byte; + + orig = zeitgeist_event_new_full ( + ZEITGEIST_ZG_ACCESS_EVENT, + ZEITGEIST_ZG_USER_ACTIVITY, + "application://firefox.desktop", + zeitgeist_subject_new_full ("http://example.com", + ZEITGEIST_NFO_WEBSITE, + ZEITGEIST_NFO_REMOTE_DATA_OBJECT, + "text/html", + "http://example.com", + "example.com", + "net"), + NULL); + + // Set event origin and current_uri + zeitgeist_event_set_origin (orig, "origin"); + zeitgeist_subject_set_current_uri ( + zeitgeist_event_get_subject (orig, 0), "http://current-example.com"); + + payload = g_byte_array_new (); + byte = 255; + g_byte_array_append (payload, &byte, 1); + zeitgeist_event_set_payload (orig, payload); // steals payload + + marshalled = zeitgeist_event_new_from_variant (zeitgeist_event_to_variant (orig)); + + g_assert_cmpint (0, ==, zeitgeist_event_get_id (marshalled)); + g_assert_cmpint (0, ==, zeitgeist_event_get_timestamp (marshalled)); + g_assert_cmpstr (ZEITGEIST_ZG_ACCESS_EVENT,==, zeitgeist_event_get_interpretation (marshalled)); + g_assert_cmpstr (ZEITGEIST_ZG_USER_ACTIVITY, ==, zeitgeist_event_get_manifestation (marshalled)); + g_assert_cmpstr ("application://firefox.desktop", ==, zeitgeist_event_get_actor (marshalled)); + g_assert_cmpstr ("origin", ==, zeitgeist_event_get_origin (marshalled)); + g_assert_cmpint (1, ==, zeitgeist_event_num_subjects (marshalled)); + + payload = zeitgeist_event_get_payload (marshalled); + g_assert (payload != NULL); + g_assert_cmpint (1, ==, payload->len); + g_assert_cmpint (255, ==, payload->data[0]); + + su = zeitgeist_event_get_subject (marshalled, 0); + g_assert_cmpstr ("http://example.com", ==, zeitgeist_subject_get_uri(su)); + g_assert_cmpstr (ZEITGEIST_NFO_WEBSITE, ==, zeitgeist_subject_get_interpretation (su)); + g_assert_cmpstr (ZEITGEIST_NFO_REMOTE_DATA_OBJECT, ==, zeitgeist_subject_get_manifestation (su)); + g_assert_cmpstr ("text/html", ==, zeitgeist_subject_get_mimetype (su)); + g_assert_cmpstr ("http://example.com", ==, zeitgeist_subject_get_origin (su)); + g_assert_cmpstr ("example.com", ==, zeitgeist_subject_get_text (su)); + g_assert_cmpstr ("net", ==, zeitgeist_subject_get_storage (su)); + g_assert_cmpstr ("http://current-example.com", ==, zeitgeist_subject_get_current_uri (su)); + + g_object_unref (orig); + g_object_unref (marshalled); +} + +static void +test_3_events_to_from_variant (Fixture *fix, gconstpointer data) +{ + GPtrArray *events; + GVariant *vevents; + + events = g_ptr_array_sized_new (3); + + g_ptr_array_add (events, zeitgeist_event_new ()); + g_ptr_array_add (events, zeitgeist_event_new ()); + g_ptr_array_add (events, zeitgeist_event_new ()); + + vevents = zeitgeist_events_to_variant (events); // events ref stolen + g_assert_cmpint (3, ==, g_variant_n_children (vevents)); + + events = zeitgeist_events_from_variant (vevents); // vevents ref stolen + g_assert_cmpint (3, ==, events->len); + g_assert (ZEITGEIST_IS_EVENT (g_ptr_array_index (events, 0))); + g_assert (ZEITGEIST_IS_EVENT (g_ptr_array_index (events, 1))); + g_assert (ZEITGEIST_IS_EVENT (g_ptr_array_index (events, 2))); + + g_ptr_array_unref (events); +} + +static void +test_0_events_to_from_variant (Fixture *fix, gconstpointer data) +{ + GPtrArray *events; + GVariant *vevents; + + events = g_ptr_array_new (); + + vevents = zeitgeist_events_to_variant (events); // events ref stolen + g_assert_cmpint (0, ==, g_variant_n_children (vevents)); + + events = zeitgeist_events_from_variant (vevents); // vevents ref stolen + g_assert_cmpint (0, ==, events->len); + + g_ptr_array_unref (events); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Event/CreateEmpty", Fixture, NULL, + setup, test_create_empty, teardown); + g_test_add ("/Zeitgeist/Event/CreateFull", Fixture, NULL, + setup, test_create_full, teardown); + g_test_add ("/Zeitgeist/Event/ActorFromAppInfo", Fixture, NULL, + setup, test_actor_from_app_info, teardown); + g_test_add ("/Zeitgeist/Event/FromVariant", Fixture, NULL, + setup, test_from_variant, teardown); + g_test_add ("/Zeitgeist/Event/FromVariantWithNewFields", Fixture, NULL, + setup, test_from_variant_with_new_fields, teardown); + g_test_add ("/Zeitgeist/Event/EmptyToFromVariant", Fixture, NULL, + setup, test_empty_to_from_variant, teardown); + g_test_add ("/Zeitgeist/Event/WithOneSubjectToFromVariant", Fixture, NULL, + setup, test_with_one_subject_to_from_variant, teardown); + g_test_add ("/Zeitgeist/Event/3EventsToFromVariant", Fixture, NULL, + setup, test_3_events_to_from_variant, teardown); + g_test_add ("/Zeitgeist/Event/0EventsToFromVariant", Fixture, NULL, + setup, test_0_events_to_from_variant, teardown); + + return g_test_run(); +} diff --git a/test/c/test-log.c b/test/c/test-log.c new file mode 100644 index 0000000..fb18c97 --- /dev/null +++ b/test/c/test-log.c @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include "zeitgeist-log.h" +#include "zeitgeist-event.h" +#include "zeitgeist-subject.h" + +typedef struct +{ + ZeitgeistLog *log; + GMainLoop *mainloop; +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + fix->log = zeitgeist_log_new (); + fix->mainloop = g_main_loop_new (NULL, FALSE); +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + g_object_unref (fix->log); + g_main_loop_unref (fix->mainloop); +} + +static gboolean +_quit_main_loop (GMainLoop *mainloop) +{ + g_main_loop_quit (mainloop); + return FALSE; +} + +static void +_on_events_deleted (ZeitgeistLog *log, + GAsyncResult *res, + GPtrArray *expected_events) +{ + GError *error; + + error = NULL; + zeitgeist_log_delete_events_finish (log, res, &error); + if (error) + { + g_critical ("Failed to get events: %s", error->message); + g_error_free (error); + return; + } + + g_ptr_array_unref (expected_events); +} + +static void +_on_events_received (ZeitgeistLog *log, + GAsyncResult *res, + GPtrArray *expected_events) +{ + ZeitgeistResultSet *events; + GArray *event_ids; + GError *error; + ZeitgeistEvent *ev, *_ev; + gint i; + guint32 event_id; + + error = NULL; + events = zeitgeist_log_get_events_finish (log, res, &error); + if (error) + { + g_critical ("Failed to get events: %s", error->message); + g_error_free (error); + return; + } + + /* Assert that we got what we expected, and collect the event ids, + * so we can delete the events */ + g_assert_cmpint (expected_events->len, ==, zeitgeist_result_set_size (events)); + g_assert_cmpint (expected_events->len, ==, zeitgeist_result_set_estimated_matches (events)); + event_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint32), + zeitgeist_result_set_size(events)); + i = 0; + while (zeitgeist_result_set_has_next (events)) + { + g_assert_cmpint (i, ==, zeitgeist_result_set_tell (events)); + ev = zeitgeist_result_set_next (events); + _ev = ZEITGEIST_EVENT (g_ptr_array_index (expected_events, i)); + g_assert_cmpstr (zeitgeist_event_get_interpretation (ev), ==, + zeitgeist_event_get_interpretation (_ev)); + g_assert_cmpstr (zeitgeist_event_get_manifestation (ev), ==, + zeitgeist_event_get_manifestation (_ev)); + g_assert_cmpstr (zeitgeist_event_get_actor (ev), ==, + zeitgeist_event_get_actor (_ev)); + g_assert_cmpint (zeitgeist_event_num_subjects (ev), ==, + zeitgeist_event_num_subjects (_ev)); + // TODO: compare subjects + + event_id = zeitgeist_event_get_id (ev); + g_array_append_val (event_ids, event_id); + i++; + } + + /* Assert that the end is still what we expect */ + g_assert_cmpint (expected_events->len, ==, zeitgeist_result_set_size (events)); + g_assert_cmpint (expected_events->len, ==, zeitgeist_result_set_estimated_matches (events)); + g_assert_cmpint (i, ==, zeitgeist_result_set_tell (events)); + g_assert_cmpint (i, ==, zeitgeist_result_set_size (events)); + + /* This method call now owns event_ids */ + zeitgeist_log_delete_events (log, event_ids, NULL, + (GAsyncReadyCallback) _on_events_deleted, + expected_events); + + g_object_unref (events); +} + +static void +_on_events_inserted (ZeitgeistLog *log, + GAsyncResult *res, + GPtrArray *expected_events) +{ + GArray *event_ids; + GError *error; + + error = NULL; + event_ids = zeitgeist_log_insert_events_finish (log, res, &error); + if (error) + { + g_critical ("Failed to insert events: %s", error->message); + g_error_free (error); + return; + } + + g_assert_cmpint (expected_events->len, ==, event_ids->len); + + /* This method call now owns event_ids */ + zeitgeist_log_get_events (log, event_ids, NULL, + (GAsyncReadyCallback) _on_events_received, + expected_events); +} + +static void +test_insert_get_delete (Fixture *fix, gconstpointer data) +{ + ZeitgeistEvent *ev; + ZeitgeistSubject *su; + GPtrArray *expected_events; + + expected_events = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + ev = zeitgeist_event_new (); + su = zeitgeist_subject_new (); + zeitgeist_event_add_subject (ev, su); + g_ptr_array_add (expected_events, g_object_ref (ev)); + /* ^^ Regarding the extra ref on ev above, it's not normally + * needed, but we need to keep them alive so we can compare the results + * against the expected values */ + + zeitgeist_event_set_interpretation (ev, "foo://Interp"); + zeitgeist_event_set_manifestation (ev, "foo://Manif"); + zeitgeist_event_set_actor (ev, "app://firefox.desktop"); + + zeitgeist_subject_set_uri (su, "file:///tmp/bar.txt"); + zeitgeist_subject_set_interpretation (su, "foo://TextDoc"); + zeitgeist_subject_set_manifestation (su, "foo://File"); + zeitgeist_subject_set_mimetype (su, "text/plain"); + zeitgeist_subject_set_origin (su, "file:///tmp"); + zeitgeist_subject_set_text (su, "bar.txt"); + zeitgeist_subject_set_storage (su, "bfb486f6-f5f8-4296-8871-0cc749cf8ef7"); + + /* This method call now owns all events, subjects, and the events array */ + zeitgeist_log_insert_events (fix->log, NULL, + (GAsyncReadyCallback) _on_events_inserted, + expected_events, + ev, NULL); + g_assert_cmpint (expected_events->len, ==, 1); + + g_timeout_add_seconds (1, (GSourceFunc) _quit_main_loop, fix->mainloop); + g_main_loop_run (fix->mainloop); +} + +static void +test_get_default (Fixture *fix, gconstpointer data) { + ZeitgeistLog *zeitgeist_log1, *zeitgeist_log2; + zeitgeist_log1 = zeitgeist_log_get_default (); + zeitgeist_log2 = zeitgeist_log_get_default (); + g_assert (zeitgeist_log1 == zeitgeist_log2); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Log/InsertGetDelete", Fixture, NULL, + setup, test_insert_get_delete, teardown); + g_test_add ("/Zeitgeist/Log/GetDefault", Fixture, NULL, + NULL, test_get_default, NULL); + + return g_test_run(); +} diff --git a/test/c/test-mimetypes.c b/test/c/test-mimetypes.c new file mode 100644 index 0000000..897c5a5 --- /dev/null +++ b/test/c/test-mimetypes.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include "zeitgeist-mimetypes.h" +#include "zeitgeist-ontology-interpretations.h" +#include "zeitgeist-ontology-manifestations.h" + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + +} + +static void +test_mime_textplain (Fixture *fix, gconstpointer data) +{ + g_assert_cmpstr (ZEITGEIST_NFO_TEXT_DOCUMENT, ==, + zeitgeist_interpretation_for_mimetype ("text/plain")); +} + +static void +test_mime_none (Fixture *fix, gconstpointer data) +{ + g_assert (zeitgeist_interpretation_for_mimetype ("asdfasdf") == NULL); +} + +static void +test_mime_regex (Fixture *fix, gconstpointer data) +{ + /* We should have a fallback for application/x-applix-* */ + g_assert_cmpstr (ZEITGEIST_NFO_DOCUMENT, ==, + zeitgeist_interpretation_for_mimetype ("application/x-applix-FOOOOBAR!")); + + /* Still application/x-applix-spreadsheet should be a spreadsheet */ + g_assert_cmpstr (ZEITGEIST_NFO_SPREADSHEET, ==, + zeitgeist_interpretation_for_mimetype ("application/x-applix-spreadsheet")); +} + +static void +test_scheme_file (Fixture *fix, gconstpointer data) +{ + g_assert_cmpstr (ZEITGEIST_NFO_FILE_DATA_OBJECT, ==, + zeitgeist_manifestation_for_uri ("file:///tmp/foo.txt")); +} + +static void +test_scheme_none (Fixture *fix, gconstpointer data) +{ + g_assert (zeitgeist_manifestation_for_uri ("asdf://asdfasdf") == NULL); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Mime/TextPlain", Fixture, NULL, + setup, test_mime_textplain, teardown); + g_test_add ("/Zeitgeist/Mime/None", Fixture, NULL, + setup, test_mime_none, teardown); + g_test_add ("/Zeitgeist/Mime/Regex", Fixture, NULL, + setup, test_mime_regex, teardown); + g_test_add ("/Zeitgeist/UriScheme/File", Fixture, NULL, + setup, test_scheme_file, teardown); + g_test_add ("/Zeitgeist/UriScheme/None", Fixture, NULL, + setup, test_scheme_none, teardown); + + return g_test_run(); +} \ No newline at end of file diff --git a/test/c/test-monitor.c b/test/c/test-monitor.c new file mode 100644 index 0000000..698deab --- /dev/null +++ b/test/c/test-monitor.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include "zeitgeist-monitor.h" +#include "zeitgeist-timerange.h" + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + +} + +static void +test_create (Fixture *fix, gconstpointer data) +{ + ZeitgeistMonitor *mon; + ZeitgeistTimeRange *tr; + GPtrArray *event_templates, *event_templates_; + + event_templates = g_ptr_array_new (); + mon = zeitgeist_monitor_new (zeitgeist_time_range_new (27, 68), + event_templates); + + g_object_get (mon, + "time-range", &tr, + "event-templates", &event_templates_, + NULL); + + g_assert_cmpint (27, ==, zeitgeist_time_range_get_start (tr)); + g_assert_cmpint (68, ==, zeitgeist_time_range_get_end (tr)); + + g_assert (event_templates == event_templates_); + + g_object_unref (tr); + g_ptr_array_unref (event_templates_); + g_object_unref (mon); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Monitor/Create", Fixture, NULL, + setup, test_create, teardown); + + return g_test_run(); +} \ No newline at end of file diff --git a/test/c/test-symbols.c b/test/c/test-symbols.c new file mode 100644 index 0000000..c584f09 --- /dev/null +++ b/test/c/test-symbols.c @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Michal Hruby + * + */ + +#include +#include +#include +#include "zeitgeist-symbols.h" +#include "zeitgeist-ontology-interpretations.h" + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + +} + +static void +test_null_symbols (Fixture *fix, gconstpointer data) +{ + // shouldn't crash + zeitgeist_symbol_is_a (NULL, NULL); +} + +static void +test_null_first (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (NULL, ZEITGEIST_NFO_MEDIA); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +test_null_second (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA, NULL); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +test_not_uri (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a ("first", "second"); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +test_not_uri_equal (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a ("something", "something"); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +test_uris_equal (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_AUDIO, + ZEITGEIST_NFO_AUDIO); + + g_assert_cmpint (res, ==, TRUE); +} + +static void +test_vector_image_media (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_VECTOR_IMAGE, + ZEITGEIST_NFO_MEDIA); + + g_assert_cmpint (res, ==, TRUE); +} + +static void +test_media_vector_image (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA, + ZEITGEIST_NFO_VECTOR_IMAGE); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +test_media_software (Fixture *fix, gconstpointer data) +{ + gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA, + ZEITGEIST_NFO_SOFTWARE); + + g_assert_cmpint (res, ==, FALSE); +} + +static void +is_uri_valid (gpointer data, gpointer unused) +{ + const gchar SEM_D_URI[] = "http://www.semanticdesktop.org/ontologies"; + gchar *uri = (gchar*) data; + g_assert (uri != NULL && g_str_has_prefix (uri, SEM_D_URI)); + gchar *str = g_strdup_printf ("%s", uri); + + g_free (str); +} + +static void +test_media_children (Fixture *fix, gconstpointer data) +{ + GList* children = zeitgeist_symbol_get_children (ZEITGEIST_NFO_MEDIA); + + g_assert_cmpint (g_list_length (children), >, 0); + g_list_foreach (children, is_uri_valid, NULL); + + g_list_free (children); +} + +static void +test_media_all_children (Fixture *fix, gconstpointer data) +{ + GList* children = zeitgeist_symbol_get_all_children (ZEITGEIST_NFO_MEDIA); + + g_assert_cmpint (g_list_length (children), >, 0); + g_list_foreach (children, is_uri_valid, NULL); + + g_list_free (children); +} + +static void +test_vector_image_parents (Fixture *fix, gconstpointer data) +{ + GList* parents = zeitgeist_symbol_get_parents (ZEITGEIST_NFO_VECTOR_IMAGE); + + g_assert_cmpint (g_list_length (parents), >, 0); + g_list_foreach (parents, is_uri_valid, NULL); + + g_list_free (parents); +} + +static void +test_media_complex (Fixture *fix, gconstpointer data) +{ + GList* iter; + GList* children = zeitgeist_symbol_get_children (ZEITGEIST_NFO_MEDIA); + GList* all_ch = zeitgeist_symbol_get_all_children (ZEITGEIST_NFO_MEDIA); + + g_assert_cmpint (g_list_length (children), >, 0); + g_assert_cmpint (g_list_length (all_ch), >, g_list_length (children)); + + for (iter = children; iter; iter = iter->next) + { + // check that it's also in all_children + g_assert (g_list_find_custom (all_ch, iter->data, (GCompareFunc) strcmp)); + } + + g_list_free (all_ch); + g_list_free (children); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Symbols/NullNull", Fixture, NULL, + setup, test_null_symbols, teardown); + g_test_add ("/Zeitgeist/Symbols/FirstNull", Fixture, NULL, + setup, test_null_first, teardown); + g_test_add ("/Zeitgeist/Symbols/SecondNull", Fixture, NULL, + setup, test_null_second, teardown); + g_test_add ("/Zeitgeist/Symbols/NotUris", Fixture, NULL, + setup, test_not_uri, teardown); + g_test_add ("/Zeitgeist/Symbols/NotUrisEqual", Fixture, NULL, + setup, test_not_uri_equal, teardown); + g_test_add ("/Zeitgeist/Symbols/EqualUris", Fixture, NULL, + setup, test_uris_equal, teardown); + g_test_add ("/Zeitgeist/Symbols/ValidParent", Fixture, NULL, + setup, test_vector_image_media, teardown); + g_test_add ("/Zeitgeist/Symbols/ValidChild", Fixture, NULL, + setup, test_media_vector_image, teardown); + g_test_add ("/Zeitgeist/Symbols/Unrelated", Fixture, NULL, + setup, test_media_software, teardown); + g_test_add ("/Zeitgeist/Symbols/GetChildren", Fixture, NULL, + setup, test_media_children, teardown); + g_test_add ("/Zeitgeist/Symbols/GetAllChildren", Fixture, NULL, + setup, test_media_all_children, teardown); + g_test_add ("/Zeitgeist/Symbols/GetParents", Fixture, NULL, + setup, test_vector_image_parents, teardown); + g_test_add ("/Zeitgeist/Symbols/SymbolInfo", Fixture, NULL, + setup, test_media_complex, teardown); + + return g_test_run(); +} diff --git a/test/c/test-timerange.c b/test/c/test-timerange.c new file mode 100644 index 0000000..34ed54e --- /dev/null +++ b/test/c/test-timerange.c @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include "zeitgeist-timerange.h" +#include "zeitgeist-timestamp.h" + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + +} + +static void +test_create (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + + tr = zeitgeist_time_range_new (0, 1); + + g_assert_cmpint (0, ==, zeitgeist_time_range_get_start (tr)); + g_assert_cmpint (1, ==, zeitgeist_time_range_get_end (tr)); + + g_object_unref (tr); +} + +static void +test_anytime (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + + tr = zeitgeist_time_range_new_anytime (); + + g_assert (0 == zeitgeist_time_range_get_start (tr)); + g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr)); + + g_object_unref (tr); +} + +static void +test_to_now (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + + tr = zeitgeist_time_range_new_to_now (); + + g_assert (0 == zeitgeist_time_range_get_start (tr)); + + /* Since system time is unreliable we simply assert that the end timestamp + * is after 2000. This assueres that we catch any uint/int32 overflow + * at least */ + g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_end (tr)); + + g_object_unref (tr); +} + +static void +test_from_now (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + + tr = zeitgeist_time_range_new_from_now (); + + /* Since system time is unreliable we simply assert that the start timestamp + * is after 2000. This assueres that we catch any uint/int32 overflow + * at least */ + g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_start (tr)); + g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr)); + + g_object_unref (tr); +} + +static void +test_from_variant (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + GVariant *v; + gint64 i,j; + + v = g_variant_new (ZEITGEIST_TIME_RANGE_VARIANT_SIGNATURE, + G_GINT64_CONSTANT(0), G_MAXINT64); + g_variant_get (v, ZEITGEIST_TIME_RANGE_VARIANT_SIGNATURE, &i, &j); + tr = zeitgeist_time_range_new_from_variant (v); // v freed + + g_assert (0 == zeitgeist_time_range_get_start (tr)); + g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr)); + + g_object_unref (tr); +} + +static void +test_to_variant (Fixture *fix, gconstpointer data) +{ + ZeitgeistTimeRange *tr; + GVariant *v; + gint64 i,j; + + tr = zeitgeist_time_range_new (0, G_MAXINT64); + v = zeitgeist_time_range_to_variant (tr); // tr freed + g_variant_get (v, ZEITGEIST_TIME_RANGE_VARIANT_SIGNATURE, + &i, &j); + + g_assert (0 == i); + g_assert (G_MAXINT64 == j); + + g_variant_unref (v); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/TimeRange/Create", Fixture, NULL, + setup, test_create, teardown); + g_test_add ("/Zeitgeist/TimeRange/Anytime", Fixture, NULL, + setup, test_anytime, teardown); + g_test_add ("/Zeitgeist/TimeRange/ToNow", Fixture, NULL, + setup, test_to_now, teardown); + g_test_add ("/Zeitgeist/TimeRange/FromNow", Fixture, NULL, + setup, test_from_now, teardown); + g_test_add ("/Zeitgeist/TimeRange/FromVariant", Fixture, NULL, + setup, test_from_variant, teardown); + g_test_add ("/Zeitgeist/TimeRange/ToVariant", Fixture, NULL, + setup, test_to_variant, teardown); + + return g_test_run(); +} diff --git a/test/c/test-timestamp.c b/test/c/test-timestamp.c new file mode 100644 index 0000000..4192559 --- /dev/null +++ b/test/c/test-timestamp.c @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2010 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * 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, see . + * + * Authored by Mikkel Kamstrup Erlandsen + * + */ + +#include +#include +#include "zeitgeist-timestamp.h" + +typedef struct +{ + +} Fixture; + +static void setup (Fixture *fix, gconstpointer data); +static void teardown (Fixture *fix, gconstpointer data); + +static void +setup (Fixture *fix, gconstpointer data) +{ + +} + +static void +teardown (Fixture *fix, gconstpointer data) +{ + +} + +static void +test_from_iso8601 (Fixture *fix, gconstpointer data) +{ + const gchar *orig = "2010-06-17T00:00:00Z"; + gint64 from_iso = zeitgeist_timestamp_from_iso8601 (orig); + gchar *d = zeitgeist_timestamp_to_iso8601 (from_iso); + + g_assert_cmpstr (orig, ==, d); + g_free (d); +} + +static void +test_inc_year (Fixture *fix, gconstpointer data) +{ + gchar *d = zeitgeist_timestamp_to_iso8601 (ZEITGEIST_TIMESTAMP_YEAR); + + // Since ZEITGEIST_TIMESTAMP_YEAR accounts for leap years we wont exactly + // match "1971-01-01T00:00:00Z" on the hour + g_assert (g_str_has_prefix (d, "1971-01-01T")); + g_free (d); +} + +static void +test_from_date (Fixture *fix, gconstpointer data) +{ + GDate date = { 0 }; + + g_date_set_dmy (&date, 25, G_DATE_JUNE, 2000); + gint64 from_date = zeitgeist_timestamp_from_date (&date); + + gchar *d = zeitgeist_timestamp_to_iso8601 (from_date); + + /* API guarantees that the timestamp is rounded to midnight */ + g_assert_cmpstr ("2000-06-25T00:00:00Z", ==, d); +} + +/* Assert that now() is after 2000. This will catch any overflow bugs + * for uints of int32 */ +static void +test_now (Fixture *fix, gconstpointer data) +{ + g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_timestamp_for_now ()); +} + +/* If a timestamp is divisible by 1000 we should have lossless conversion + * to and from GTimeVals */ +static void +test_timeval_conversion (Fixture *fix, gconstpointer data) +{ + GTimeVal tv = { 0 }; + gint64 ts, _ts; + + /* Check 0 */ + ts = 0; + zeitgeist_timestamp_to_timeval (ts, &tv); + _ts = zeitgeist_timestamp_from_timeval (&tv); + g_assert (ts == _ts); + + /* Check a low number */ + ts = 10000; + zeitgeist_timestamp_to_timeval (ts, &tv); + _ts = zeitgeist_timestamp_from_timeval (&tv); + g_assert (ts == _ts); + + /* Check 2010-06-18 */ + ts = G_GINT64_CONSTANT (1276849717119); + zeitgeist_timestamp_to_timeval (ts, &tv); + _ts = zeitgeist_timestamp_from_timeval (&tv); + g_assert (ts == _ts); + + /* Note : G_MAXINT64 wont work because GTimeVal uses glongs internally + * to track the numbers */ +} + +static void +test_prev_midnight (Fixture *fix, gconstpointer data) +{ + gint64 ts, midnight; + gchar *iso; + + /* Check 2010-06-23T11:19:07Z */ + ts = G_GINT64_CONSTANT (1277284743659); + + /* Now the actual test */ + midnight = zeitgeist_timestamp_prev_midnight (ts); + iso = zeitgeist_timestamp_to_iso8601(midnight); + g_assert(g_str_has_prefix (iso, "2010-06-23T00:00:00")); + g_free (iso); + + /* Pre midnight of 'midnight' should go one day back */ + midnight = zeitgeist_timestamp_prev_midnight (midnight); + iso = zeitgeist_timestamp_to_iso8601(midnight); + g_assert(g_str_has_prefix (iso, "2010-06-22T00:00:00")); + g_free (iso); +} + +static void +test_next_midnight (Fixture *fix, gconstpointer data) +{ + gint64 ts, midnight; + gchar *iso; + + /* Check 2010-06-23T11:19:07Z */ + ts = G_GINT64_CONSTANT (1277284743659); + + /* Now the actual test */ + midnight = zeitgeist_timestamp_next_midnight (ts); + iso = zeitgeist_timestamp_to_iso8601(midnight); + g_assert(g_str_has_prefix (iso, "2010-06-24T00:00:00")); + g_free (iso); + + /* Pre midnight of 'midnight' should go one day back */ + midnight = zeitgeist_timestamp_next_midnight (midnight); + iso = zeitgeist_timestamp_to_iso8601(midnight); + g_assert(g_str_has_prefix (iso, "2010-06-25T00:00:00")); + g_free (iso); +} + +int +main (int argc, + char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/Zeitgeist/Timestamp/FromISO8601", Fixture, NULL, + setup, test_from_iso8601, teardown); + g_test_add ("/Zeitgeist/Timestamp/IncrementYear", Fixture, NULL, + setup, test_inc_year, teardown); + g_test_add ("/Zeitgeist/Timestamp/FromDate", Fixture, NULL, + setup, test_from_date, teardown); + g_test_add ("/Zeitgeist/Timestamp/Now", Fixture, NULL, + setup, test_now, teardown); + g_test_add ("/Zeitgeist/Timestamp/TimeValConversion", Fixture, NULL, + setup, test_timeval_conversion, teardown); + g_test_add ("/Zeitgeist/Timestamp/PrevMidnight", Fixture, NULL, + setup, test_prev_midnight, teardown); + g_test_add ("/Zeitgeist/Timestamp/NextMidnight", Fixture, NULL, + setup, test_next_midnight, teardown); + + return g_test_run(); +} \ No newline at end of file diff --git a/test/c/test.desktop b/test/c/test.desktop new file mode 100644 index 0000000..b8180be --- /dev/null +++ b/test/c/test.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=test +GenericName=Test Desktop File +Comment=This is just a test file +Exec=echo %U +Terminal=false +Type=Application +StartupNotify=false +MimeType=text/plain; +Icon=accessories-text-editor -- 1.7.9.5