tests: Silence compiler warnings.
[platform/upstream/evolution-data-server.git] / tests / libedataserver / e-source-test.c
1 /*
2  * e-source-test.c
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
16  *
17  */
18
19 #include <string.h>
20 #include <gio/gunixoutputstream.h>
21
22 #include <libedataserver/libedataserver.h>
23
24 #define SIMPLE_KEY_FILE \
25         "[Data Source]\n" \
26         "DisplayName=Simple Test Case\n" \
27         "Parent=local\n"
28
29 typedef struct _TestESource TestESource;
30 typedef struct _TestFixture TestFixture;
31
32 struct _TestESource {
33         GFile *file;
34         ESource *source;
35         gboolean changed;
36 };
37
38 struct _TestFixture {
39         TestESource test;
40         TestESource same_file;
41         TestESource same_content;
42         gboolean changed;
43 };
44
45 #if 0  /* ACCOUNT_MGMT */
46 static void
47 source_changed_cb (ESource *source,
48                    TestESource *test)
49 {
50         test->changed = TRUE;
51 }
52
53 static void
54 setup_test_source (TestESource *test,
55                    const gchar *content)
56 {
57         GOutputStream *stream;
58         gchar *filename;
59         gint fd;
60         GError *error = NULL;
61
62         /* Create a new temporary key file. */
63         fd = g_file_open_tmp ("test-source-XXXXXX", &filename, &error);
64         g_assert_no_error (error);
65
66         /* Write the given content to the temporary key file. */
67         stream = g_unix_output_stream_new (fd, TRUE);
68         g_output_stream_write_all (
69                 stream, content, strlen (content), NULL, NULL, &error);
70         g_object_unref (stream);
71         g_assert_no_error (error);
72
73         /* Create a GFile that points to the temporary key file. */
74         test->file = g_file_new_for_path (filename);
75
76         /* Create an ESource from the GFile and load the key file. */
77         test->source = e_source_new (test->file, &error);
78         g_assert_no_error (error);
79
80         g_signal_connect (
81                 test->source, "changed",
82                 G_CALLBACK (source_changed_cb), test);
83
84         g_free (filename);
85 }
86
87 static void
88 teardown_test_source (TestESource *test)
89 {
90         GError *error = NULL;
91
92         /* Delete the temporary key file. */
93         g_file_delete (test->file, NULL, &error);
94         g_assert_no_error (error);
95
96         g_object_unref (test->file);
97         g_object_unref (test->source);
98 }
99 #endif /* ACCOUNT_MGMT */
100
101 static void
102 test_fixture_setup_key_file (TestFixture *fixture,
103                              gconstpointer test_data)
104 {
105 #if 0  /* ACCOUNT_MGMT */
106         GError *error = NULL;
107
108         /* The primary ESource for testing. */
109         setup_test_source (&fixture->test, SIMPLE_KEY_FILE);
110
111         /* Secondary ESource: different file with identical content. */
112         setup_test_source (&fixture->same_content, SIMPLE_KEY_FILE);
113
114         /* Secondary ESource: a clone of the primary ESource. */
115         fixture->same_file.file =
116                 g_file_dup (fixture->test.file);
117         fixture->same_file.source =
118                 e_source_new (fixture->same_file.file, &error);
119         g_assert_no_error (error);
120 #endif /* ACCOUNT_MGMT */
121 }
122
123 static void
124 test_fixture_teardown_key_file (TestFixture *fixture,
125                                 gconstpointer test_data)
126 {
127 #if 0  /* ACCOUNT_MGMT */
128         teardown_test_source (&fixture->test);
129 #endif
130 }
131
132 static void
133 test_single_source (TestFixture *fixture,
134                     gconstpointer test_data)
135 {
136 #if 0  /* ACCOUNT_MGMT */
137         GFile *file;
138         ESourceExtension *extension;
139         const gchar *extension_name;
140         const gchar *backend_name;
141         const gchar *display_name;
142         const gchar *parent;
143         const gchar *uid;
144         gchar *basename;
145         guint hash;
146         GError *error = NULL;
147
148         /* Extract a bunch of data from the primary ESource. */
149         backend_name = e_source_get_backend_name (fixture->test.source);
150         display_name = e_source_get_display_name (fixture->test.source);
151         parent = e_source_get_parent (fixture->test.source);
152         file = e_source_get_file (fixture->test.source);
153         uid = e_source_get_uid (fixture->test.source);
154         hash = e_source_hash (fixture->test.source);
155
156         /* An ESource's GFile should compare equal to the GFile
157          * used to create it. */
158         g_assert (g_file_equal (file, fixture->test.file));
159
160         /* An ESource's UID should match the key file's basename. */
161         basename = g_file_get_basename (fixture->test.file);
162         g_assert_cmpstr (uid, ==, basename);
163         g_free (basename);
164
165         /* An ESource's hash value is based on its UID. */
166         g_assert_cmpuint (hash, ==, g_str_hash (uid));
167
168         /* Check the other fields specified in this key file. */
169         g_assert_cmpstr (display_name, ==, "Simple Test Case");
170         g_assert_cmpstr (parent, ==, "local");
171
172         /* The backend name was omitted, so it should be NULL. */
173         g_assert_cmpstr (backend_name, ==, NULL);
174
175         /* ESource equality is based solely on UIDs. */
176         g_assert (e_source_equal (
177                 fixture->test.source, fixture->test.source));
178         g_assert (e_source_equal (
179                 fixture->test.source, fixture->same_file.source));
180         g_assert (!e_source_equal (
181                 fixture->test.source, fixture->same_content.source));
182
183         /* Nothing done so far should have triggered ESource::changed. */
184         g_assert (!fixture->test.changed);
185
186         /* Check on-demand extension loading. */
187         E_TYPE_SOURCE_AUTHENTICATION;  /* register the type */
188         extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
189         g_assert (!e_source_has_extension (
190                 fixture->test.source, extension_name));
191         extension = e_source_get_extension (
192                 fixture->test.source, extension_name);
193         g_assert (e_source_has_extension (
194                 fixture->test.source, extension_name));
195         g_assert (E_IS_SOURCE_AUTHENTICATION (extension));
196
197         /* Loading an extension should trigger ESource::changed. */
198         g_assert (fixture->test.changed);
199
200         fixture->test.changed = FALSE;
201
202         /* The ESource::changed signal is wired to its own "notify" and
203          * the "notify" signals of its extensions.  Passing any property
204          * to g_object_notify() should trigger it. */
205         g_object_notify (G_OBJECT (fixture->test.source), "display-name");
206         g_assert (fixture->test.changed);
207
208         fixture->test.changed = FALSE;
209
210         /* Now try the extension we loaded. */
211         g_object_notify (G_OBJECT (extension), "host");
212         g_assert (fixture->test.changed);
213
214         /* The primary ESource and cloned ESource both handle the same
215          * key file.  If we change a property in one ESource and sync it
216          * to disk, then reload the other ESource and examine the same
217          * property, the values should match. */
218         display_name = "Modified Test Case";
219         e_source_set_display_name (fixture->test.source, display_name);
220         e_source_sync (fixture->test.source, &error);
221         g_assert_no_error (error);
222         e_source_reload (fixture->same_file.source, &error);
223         g_assert_no_error (error);
224         display_name = e_source_get_display_name (fixture->same_file.source);
225         g_assert_cmpstr (display_name, ==, "Modified Test Case");
226 #endif /* ACOCUNT_MGMT */
227 }
228
229 gint
230 main (gint argc,
231       gchar **argv)
232 {
233         g_type_init ();
234
235         g_test_init (&argc, &argv, NULL);
236
237         g_test_add (
238                 "/e-source-test/SingleSource",
239                 TestFixture, NULL,
240                 test_fixture_setup_key_file,
241                 test_single_source,
242                 test_fixture_teardown_key_file);
243
244         return g_test_run ();
245 }