Use GTestDBus in all GDBus unit tests
[glib.git] / gio / tests / g-icon.c
bloba360c28be299518a15439f64199d787ef6ebe54a
1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008 Red Hat, Inc.
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work 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.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
22 * Authors: David Zeuthen <davidz@redhat.com>
25 #include <glib/glib.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 static void
31 test_g_icon_serialize (void)
33 GIcon *icon;
34 GIcon *icon2;
35 GIcon *icon3;
36 GIcon *icon4;
37 GIcon *icon5;
38 GEmblem *emblem1;
39 GEmblem *emblem2;
40 const char *uri;
41 GFile *location;
42 char *data;
43 GError *error;
44 gint origin;
45 GIcon *i;
47 error = NULL;
49 /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
51 uri = "file:///some/native/path/to/an/icon.png";
52 location = g_file_new_for_uri (uri);
53 icon = g_file_icon_new (location);
54 data = g_icon_to_string (icon);
55 g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon.png");
56 icon2 = g_icon_new_for_string (data, &error);
57 g_assert_no_error (error);
58 g_assert (g_icon_equal (icon, icon2));
59 g_free (data);
60 g_object_unref (icon);
61 g_object_unref (icon2);
62 g_object_unref (location);
64 uri = "file:///some/native/path/to/an/icon with spaces.png";
65 location = g_file_new_for_uri (uri);
66 icon = g_file_icon_new (location);
67 data = g_icon_to_string (icon);
68 g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon with spaces.png");
69 icon2 = g_icon_new_for_string (data, &error);
70 g_assert_no_error (error);
71 g_assert (g_icon_equal (icon, icon2));
72 g_free (data);
73 g_object_unref (icon);
74 g_object_unref (icon2);
75 g_object_unref (location);
77 uri = "sftp:///some/non-native/path/to/an/icon.png";
78 location = g_file_new_for_uri (uri);
79 icon = g_file_icon_new (location);
80 data = g_icon_to_string (icon);
81 g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png");
82 icon2 = g_icon_new_for_string (data, &error);
83 g_assert_no_error (error);
84 g_assert (g_icon_equal (icon, icon2));
85 g_free (data);
86 g_object_unref (icon);
87 g_object_unref (icon2);
88 g_object_unref (location);
90 #if 0
91 uri = "sftp:///some/non-native/path/to/an/icon with spaces.png";
92 location = g_file_new_for_uri (uri);
93 icon = g_file_icon_new (location);
94 data = g_icon_to_string (icon);
95 g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
96 icon2 = g_icon_new_for_string (data, &error);
97 g_assert_no_error (error);
98 g_assert (g_icon_equal (icon, icon2));
99 g_free (data);
100 g_object_unref (icon);
101 g_object_unref (icon2);
102 g_object_unref (location);
103 #endif
105 icon = g_themed_icon_new ("network-server");
106 data = g_icon_to_string (icon);
107 g_assert_cmpstr (data, ==, "network-server");
108 icon2 = g_icon_new_for_string (data, &error);
109 g_assert_no_error (error);
110 g_assert (g_icon_equal (icon, icon2));
111 g_free (data);
112 g_object_unref (icon);
113 g_object_unref (icon2);
115 /* Check that we can serialize from well-known specified formats */
116 icon = g_icon_new_for_string ("network-server%", &error);
117 g_assert_no_error (error);
118 icon2 = g_themed_icon_new ("network-server%");
119 g_assert (g_icon_equal (icon, icon2));
120 g_object_unref (icon);
121 g_object_unref (icon2);
123 icon = g_icon_new_for_string ("/path/to/somewhere.png", &error);
124 g_assert_no_error (error);
125 location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
126 icon2 = g_file_icon_new (location);
127 g_assert (g_icon_equal (icon, icon2));
128 g_object_unref (icon);
129 g_object_unref (icon2);
130 g_object_unref (location);
132 icon = g_icon_new_for_string ("/path/to/somewhere with whitespace.png", &error);
133 g_assert_no_error (error);
134 data = g_icon_to_string (icon);
135 g_assert_cmpstr (data, ==, "/path/to/somewhere with whitespace.png");
136 g_free (data);
137 location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
138 icon2 = g_file_icon_new (location);
139 g_assert (g_icon_equal (icon, icon2));
140 g_object_unref (location);
141 g_object_unref (icon2);
142 location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
143 icon2 = g_file_icon_new (location);
144 g_assert (!g_icon_equal (icon, icon2));
145 g_object_unref (location);
146 g_object_unref (icon2);
147 g_object_unref (icon);
149 icon = g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error);
150 g_assert_no_error (error);
151 data = g_icon_to_string (icon);
152 g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere.png");
153 g_free (data);
154 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
155 icon2 = g_file_icon_new (location);
156 g_assert (g_icon_equal (icon, icon2));
157 g_object_unref (icon);
158 g_object_unref (icon2);
159 g_object_unref (location);
161 #if 0
162 icon = g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png", &error);
163 g_assert_no_error (error);
164 data = g_icon_to_string (icon);
165 g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere%20with%20whitespace.png");
166 g_free (data);
167 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png");
168 icon2 = g_file_icon_new (location);
169 g_assert (g_icon_equal (icon, icon2));
170 g_object_unref (location);
171 g_object_unref (icon2);
172 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png");
173 icon2 = g_file_icon_new (location);
174 g_assert (g_icon_equal (icon, icon2));
175 g_object_unref (location);
176 g_object_unref (icon2);
177 g_object_unref (icon);
178 #endif
180 /* Check that GThemedIcon serialization works */
182 icon = g_themed_icon_new ("network-server");
183 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
184 data = g_icon_to_string (icon);
185 icon2 = g_icon_new_for_string (data, &error);
186 g_assert_no_error (error);
187 g_assert (g_icon_equal (icon, icon2));
188 g_free (data);
189 g_object_unref (icon);
190 g_object_unref (icon2);
192 icon = g_themed_icon_new ("icon name with whitespace");
193 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
194 data = g_icon_to_string (icon);
195 icon2 = g_icon_new_for_string (data, &error);
196 g_assert_no_error (error);
197 g_assert (g_icon_equal (icon, icon2));
198 g_free (data);
199 g_object_unref (icon);
200 g_object_unref (icon2);
202 icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
203 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
204 data = g_icon_to_string (icon);
205 icon2 = g_icon_new_for_string (data, &error);
206 g_assert_no_error (error);
207 g_assert (g_icon_equal (icon, icon2));
208 g_free (data);
209 g_object_unref (icon);
210 g_object_unref (icon2);
212 /* Check that GEmblemedIcon serialization works */
214 icon = g_themed_icon_new ("face-smirk");
215 icon2 = g_themed_icon_new ("emblem-important");
216 g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
217 location = g_file_new_for_uri ("file:///some/path/somewhere.png");
218 icon3 = g_file_icon_new (location);
219 g_object_unref (location);
220 emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
221 emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
222 icon4 = g_emblemed_icon_new (icon, emblem1);
223 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
224 data = g_icon_to_string (icon4);
225 icon5 = g_icon_new_for_string (data, &error);
226 g_assert_no_error (error);
227 g_assert (g_icon_equal (icon4, icon5));
229 g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
230 g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
231 g_assert (i == icon2);
232 g_object_unref (i);
234 g_object_unref (emblem1);
235 g_object_unref (emblem2);
236 g_object_unref (icon);
237 g_object_unref (icon2);
238 g_object_unref (icon3);
239 g_object_unref (icon4);
240 g_object_unref (icon5);
241 g_free (data);
244 static void
245 test_themed_icon (void)
247 GIcon *icon1, *icon2, *icon3;
248 const gchar *const *names;
249 const gchar *names2[] = { "first", "testicon", "last", NULL };
250 gchar *str;
251 gboolean fallbacks;
253 icon1 = g_themed_icon_new ("testicon");
255 g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
256 g_assert (!fallbacks);
258 names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
259 g_assert_cmpint (g_strv_length ((gchar **)names), ==, 1);
260 g_assert_cmpstr (names[0], ==, "testicon");
262 g_themed_icon_prepend_name (G_THEMED_ICON (icon1), "first");
263 g_themed_icon_append_name (G_THEMED_ICON (icon1), "last");
264 names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
265 g_assert_cmpint (g_strv_length ((gchar **)names), ==, 3);
266 g_assert_cmpstr (names[0], ==, "first");
267 g_assert_cmpstr (names[1], ==, "testicon");
268 g_assert_cmpstr (names[2], ==, "last");
269 g_assert_cmpuint (g_icon_hash (icon1), ==, 2400773466U);
271 icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
272 g_assert (g_icon_equal (icon1, icon2));
274 str = g_icon_to_string (icon2);
275 icon3 = g_icon_new_for_string (str, NULL);
276 g_assert (g_icon_equal (icon2, icon3));
277 g_free (str);
279 g_object_unref (icon1);
280 g_object_unref (icon2);
281 g_object_unref (icon3);
284 static void
285 test_emblemed_icon (void)
287 GIcon *icon1, *icon2, *icon3, *icon4;
288 GEmblem *emblem, *emblem1, *emblem2;
289 GList *emblems;
291 icon1 = g_themed_icon_new ("testicon");
292 icon2 = g_themed_icon_new ("testemblem");
293 emblem1 = g_emblem_new (icon2);
294 emblem2 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_TAG);
296 icon3 = g_emblemed_icon_new (icon1, emblem1);
297 emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3));
298 g_assert_cmpint (g_list_length (emblems), ==, 1);
299 g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1);
301 icon4 = g_emblemed_icon_new (icon1, emblem1);
302 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
303 emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4));
304 g_assert_cmpint (g_list_length (emblems), ==, 2);
306 g_assert (!g_icon_equal (icon3, icon4));
308 emblem = emblems->data;
309 g_assert (g_emblem_get_icon (emblem) == icon2);
310 g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG);
312 emblem = emblems->next->data;
313 g_assert (g_emblem_get_icon (emblem) == icon2);
314 g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN);
316 g_object_unref (icon1);
317 g_object_unref (icon2);
318 g_object_unref (icon3);
319 g_object_unref (icon4);
321 g_object_unref (emblem1);
322 g_object_unref (emblem2);
325 static void
326 test_file_icon (void)
328 GFile *file;
329 GIcon *icon;
330 GIcon *icon2;
331 GError *error;
332 GInputStream *stream;
333 gchar *str;
335 file = g_file_new_for_path (SRCDIR "/g-icon.c");
336 icon = g_file_icon_new (file);
337 g_object_unref (file);
339 error = NULL;
340 stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 20, NULL, NULL, &error);
341 g_assert (stream != NULL);
342 g_assert_no_error (error);
344 g_object_unref (stream);
346 str = g_icon_to_string (icon);
347 icon2 = g_icon_new_for_string (str, NULL);
348 g_assert (g_icon_equal (icon, icon2));
349 g_free (str);
351 g_object_unref (icon);
352 g_object_unref (icon2);
356 main (int argc,
357 char *argv[])
359 g_type_init ();
360 g_test_init (&argc, &argv, NULL);
362 g_test_add_func ("/icons/serialize", test_g_icon_serialize);
363 g_test_add_func ("/icons/themed", test_themed_icon);
364 g_test_add_func ("/icons/emblemed", test_emblemed_icon);
365 g_test_add_func ("/icons/file", test_file_icon);
367 return g_test_run();