1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 <http://www.gnu.org/licenses/>.
*
* Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
*
*/
#include <glib.h>
#include <glib-object.h>
#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();
}
|