summaryrefslogtreecommitdiff
path: root/tests/libedataserver/e-source-test.c
blob: d6b6da060c8b9df4bf02d8b8edb7ace9ab5b42be (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * e-source-test.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#include <string.h>
#include <gio/gunixoutputstream.h>

#include <libedataserver/libedataserver.h>

#define SIMPLE_KEY_FILE \
	"[Data Source]\n" \
	"DisplayName=Simple Test Case\n" \
	"Parent=local\n"

typedef struct _TestESource TestESource;
typedef struct _TestFixture TestFixture;

struct _TestESource {
	GFile *file;
	ESource *source;
	gboolean changed;
};

struct _TestFixture {
	TestESource test;
	TestESource same_file;
	TestESource same_content;
	gboolean changed;
};

static void
source_changed_cb (ESource *source,
                   TestESource *test)
{
	test->changed = TRUE;
}

static void
setup_test_source (TestESource *test,
                   const gchar *content)
{
	GOutputStream *stream;
	gchar *filename;
	gint fd;
	GError *error = NULL;

	/* Create a new temporary key file. */
	fd = g_file_open_tmp ("test-source-XXXXXX", &filename, &error);
	g_assert_no_error (error);

	/* Write the given content to the temporary key file. */
	stream = g_unix_output_stream_new (fd, TRUE);
	g_output_stream_write_all (
		stream, content, strlen (content), NULL, NULL, &error);
	g_object_unref (stream);
	g_assert_no_error (error);

	/* Create a GFile that points to the temporary key file. */
	test->file = g_file_new_for_path (filename);

	/* Create an ESource from the GFile and load the key file. */
#if 0  /* ACCOUNT_MGMT */
	test->source = e_source_new (test->file, &error);
	g_assert_no_error (error);
#endif /* ACCOUNT_MGMT */

	g_signal_connect (
		test->source, "changed",
		G_CALLBACK (source_changed_cb), test);

	g_free (filename);
}

static void
teardown_test_source (TestESource *test)
{
	GError *error = NULL;

	/* Delete the temporary key file. */
	g_file_delete (test->file, NULL, &error);
	g_assert_no_error (error);

	g_object_unref (test->file);
	g_object_unref (test->source);
}

static void
test_fixture_setup_key_file (TestFixture *fixture,
                             gconstpointer test_data)
{
#if 0  /* ACCOUNT_MGMT */
	GError *error = NULL;

	/* The primary ESource for testing. */
	setup_test_source (&fixture->test, SIMPLE_KEY_FILE);

	/* Secondary ESource: different file with identical content. */
	setup_test_source (&fixture->same_content, SIMPLE_KEY_FILE);

	/* Secondary ESource: a clone of the primary ESource. */
	fixture->same_file.file =
		g_file_dup (fixture->test.file);
	fixture->same_file.source =
		e_source_new (fixture->same_file.file, &error);
	g_assert_no_error (error);
#endif /* ACCOUNT_MGMT */
}

static void
test_fixture_teardown_key_file (TestFixture *fixture,
                                gconstpointer test_data)
{
#if 0  /* ACCOUNT_MGMT */
	teardown_test_source (&fixture->test);
#endif
}

static void
test_single_source (TestFixture *fixture,
                    gconstpointer test_data)
{
#if 0  /* ACCOUNT_MGMT */
	GFile *file;
	ESourceExtension *extension;
	const gchar *extension_name;
	const gchar *backend_name;
	const gchar *display_name;
	const gchar *parent;
	const gchar *uid;
	gchar *basename;
	guint hash;
	GError *error = NULL;

	/* Extract a bunch of data from the primary ESource. */
	backend_name = e_source_get_backend_name (fixture->test.source);
	display_name = e_source_get_display_name (fixture->test.source);
	parent = e_source_get_parent (fixture->test.source);
	file = e_source_get_file (fixture->test.source);
	uid = e_source_get_uid (fixture->test.source);
	hash = e_source_hash (fixture->test.source);

	/* An ESource's GFile should compare equal to the GFile
	 * used to create it. */
	g_assert (g_file_equal (file, fixture->test.file));

	/* An ESource's UID should match the key file's basename. */
	basename = g_file_get_basename (fixture->test.file);
	g_assert_cmpstr (uid, ==, basename);
	g_free (basename);

	/* An ESource's hash value is based on its UID. */
	g_assert_cmpuint (hash, ==, g_str_hash (uid));

	/* Check the other fields specified in this key file. */
	g_assert_cmpstr (display_name, ==, "Simple Test Case");
	g_assert_cmpstr (parent, ==, "local");

	/* The backend name was omitted, so it should be NULL. */
	g_assert_cmpstr (backend_name, ==, NULL);

	/* ESource equality is based solely on UIDs. */
	g_assert (e_source_equal (
		fixture->test.source, fixture->test.source));
	g_assert (e_source_equal (
		fixture->test.source, fixture->same_file.source));
	g_assert (!e_source_equal (
		fixture->test.source, fixture->same_content.source));

	/* Nothing done so far should have triggered ESource::changed. */
	g_assert (!fixture->test.changed);

	/* Check on-demand extension loading. */
	E_TYPE_SOURCE_AUTHENTICATION;  /* register the type */
	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
	g_assert (!e_source_has_extension (
		fixture->test.source, extension_name));
	extension = e_source_get_extension (
		fixture->test.source, extension_name);
	g_assert (e_source_has_extension (
		fixture->test.source, extension_name));
	g_assert (E_IS_SOURCE_AUTHENTICATION (extension));

	/* Loading an extension should trigger ESource::changed. */
	g_assert (fixture->test.changed);

	fixture->test.changed = FALSE;

	/* The ESource::changed signal is wired to its own "notify" and
	 * the "notify" signals of its extensions.  Passing any property
	 * to g_object_notify() should trigger it. */
	g_object_notify (G_OBJECT (fixture->test.source), "display-name");
	g_assert (fixture->test.changed);

	fixture->test.changed = FALSE;

	/* Now try the extension we loaded. */
	g_object_notify (G_OBJECT (extension), "host");
	g_assert (fixture->test.changed);

	/* The primary ESource and cloned ESource both handle the same
	 * key file.  If we change a property in one ESource and sync it
	 * to disk, then reload the other ESource and examine the same
	 * property, the values should match. */
	display_name = "Modified Test Case";
	e_source_set_display_name (fixture->test.source, display_name);
	e_source_sync (fixture->test.source, &error);
	g_assert_no_error (error);
	e_source_reload (fixture->same_file.source, &error);
	g_assert_no_error (error);
	display_name = e_source_get_display_name (fixture->same_file.source);
	g_assert_cmpstr (display_name, ==, "Modified Test Case");
#endif /* ACOCUNT_MGMT */
}

gint
main (gint argc,
      gchar **argv)
{
	g_type_init ();

	g_test_init (&argc, &argv, NULL);

	g_test_add (
		"/e-source-test/SingleSource",
		TestFixture, NULL,
		test_fixture_setup_key_file,
		test_single_source,
		test_fixture_teardown_key_file);

	return g_test_run ();
}