1 | /* GLib testing framework examples and tests |
2 | * |
3 | * Copyright (C) 2008 Red Hat, Inc. |
4 | * |
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. |
8 | * |
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. |
12 | * |
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. |
21 | * |
22 | * Authors: David Zeuthen <davidz@redhat.com> |
23 | */ |
24 | |
25 | #include <glib/glib.h> |
26 | #include <gio/gio.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | |
30 | static void |
31 | test_g_icon_to_string (void) |
32 | { |
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; |
46 | GFile *file; |
47 | |
48 | error = NULL; |
49 | |
50 | /* check that GFileIcon and GThemedIcon serialize to the encoding specified */ |
51 | |
52 | uri = "file:///some/native/path/to/an/icon.png" ; |
53 | location = g_file_new_for_uri (uri); |
54 | icon = g_file_icon_new (file: location); |
55 | |
56 | g_object_get (object: icon, first_property_name: "file" , &file, NULL); |
57 | g_assert (file == location); |
58 | g_object_unref (object: file); |
59 | |
60 | data = g_icon_to_string (icon); |
61 | g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S "native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon.png" ); |
62 | icon2 = g_icon_new_for_string (str: data, error: &error); |
63 | g_assert_no_error (error); |
64 | g_assert (g_icon_equal (icon, icon2)); |
65 | g_free (mem: data); |
66 | g_object_unref (object: icon); |
67 | g_object_unref (object: icon2); |
68 | g_object_unref (object: location); |
69 | |
70 | uri = "file:///some/native/path/to/an/icon with spaces.png" ; |
71 | location = g_file_new_for_uri (uri); |
72 | icon = g_file_icon_new (file: location); |
73 | data = g_icon_to_string (icon); |
74 | g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S "native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon with spaces.png" ); |
75 | icon2 = g_icon_new_for_string (str: data, error: &error); |
76 | g_assert_no_error (error); |
77 | g_assert (g_icon_equal (icon, icon2)); |
78 | g_free (mem: data); |
79 | g_object_unref (object: icon); |
80 | g_object_unref (object: icon2); |
81 | g_object_unref (object: location); |
82 | |
83 | uri = "sftp:///some/non-native/path/to/an/icon.png" ; |
84 | location = g_file_new_for_uri (uri); |
85 | icon = g_file_icon_new (file: location); |
86 | data = g_icon_to_string (icon); |
87 | g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png" ); |
88 | icon2 = g_icon_new_for_string (str: data, error: &error); |
89 | g_assert_no_error (error); |
90 | g_assert (g_icon_equal (icon, icon2)); |
91 | g_free (mem: data); |
92 | g_object_unref (object: icon); |
93 | g_object_unref (object: icon2); |
94 | g_object_unref (object: location); |
95 | |
96 | #if 0 |
97 | uri = "sftp:///some/non-native/path/to/an/icon with spaces.png" ; |
98 | location = g_file_new_for_uri (uri); |
99 | icon = g_file_icon_new (location); |
100 | data = g_icon_to_string (icon); |
101 | g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png" ); |
102 | icon2 = g_icon_new_for_string (data, &error); |
103 | g_assert_no_error (error); |
104 | g_assert (g_icon_equal (icon, icon2)); |
105 | g_free (data); |
106 | g_object_unref (icon); |
107 | g_object_unref (icon2); |
108 | g_object_unref (location); |
109 | #endif |
110 | |
111 | icon = g_themed_icon_new_with_default_fallbacks (iconname: "some-icon-symbolic" ); |
112 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "some-other-icon" ); |
113 | data = g_icon_to_string (icon); |
114 | g_assert_cmpstr (data, ==, ". GThemedIcon " |
115 | "some-icon-symbolic some-symbolic some-other-icon some-other some " |
116 | "some-icon some-other-icon-symbolic some-other-symbolic" ); |
117 | g_free (mem: data); |
118 | g_object_unref (object: icon); |
119 | |
120 | icon = g_themed_icon_new (iconname: "network-server" ); |
121 | data = g_icon_to_string (icon); |
122 | g_assert_cmpstr (data, ==, "network-server" ); |
123 | icon2 = g_icon_new_for_string (str: data, error: &error); |
124 | g_assert_no_error (error); |
125 | g_assert (g_icon_equal (icon, icon2)); |
126 | g_free (mem: data); |
127 | g_object_unref (object: icon); |
128 | g_object_unref (object: icon2); |
129 | |
130 | icon = g_themed_icon_new_with_default_fallbacks (iconname: "network-server" ); |
131 | data = g_icon_to_string (icon); |
132 | g_assert_cmpstr (data, ==, ". GThemedIcon network-server network network-server-symbolic network-symbolic" ); |
133 | icon2 = g_icon_new_for_string (str: data, error: &error); |
134 | g_assert_no_error (error); |
135 | g_assert (g_icon_equal (icon, icon2)); |
136 | g_free (mem: data); |
137 | g_object_unref (object: icon); |
138 | g_object_unref (object: icon2); |
139 | |
140 | /* Check that we can serialize from well-known specified formats */ |
141 | icon = g_icon_new_for_string (str: "network-server%" , error: &error); |
142 | g_assert_no_error (error); |
143 | icon2 = g_themed_icon_new (iconname: "network-server%" ); |
144 | g_assert (g_icon_equal (icon, icon2)); |
145 | g_object_unref (object: icon); |
146 | g_object_unref (object: icon2); |
147 | |
148 | icon = g_icon_new_for_string (str: "/path/to/somewhere.png" , error: &error); |
149 | g_assert_no_error (error); |
150 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere.png" ); |
151 | icon2 = g_file_icon_new (file: location); |
152 | g_assert (g_icon_equal (icon, icon2)); |
153 | g_object_unref (object: icon); |
154 | g_object_unref (object: icon2); |
155 | g_object_unref (object: location); |
156 | |
157 | icon = g_icon_new_for_string (str: "/path/to/somewhere with whitespace.png" , error: &error); |
158 | g_assert_no_error (error); |
159 | data = g_icon_to_string (icon); |
160 | g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "somewhere with whitespace.png" ); |
161 | g_free (mem: data); |
162 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere with whitespace.png" ); |
163 | icon2 = g_file_icon_new (file: location); |
164 | g_assert (g_icon_equal (icon, icon2)); |
165 | g_object_unref (object: location); |
166 | g_object_unref (object: icon2); |
167 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere%20with%20whitespace.png" ); |
168 | icon2 = g_file_icon_new (file: location); |
169 | g_assert (!g_icon_equal (icon, icon2)); |
170 | g_object_unref (object: location); |
171 | g_object_unref (object: icon2); |
172 | g_object_unref (object: icon); |
173 | |
174 | icon = g_icon_new_for_string (str: "sftp:///path/to/somewhere.png" , error: &error); |
175 | g_assert_no_error (error); |
176 | data = g_icon_to_string (icon); |
177 | g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere.png" ); |
178 | g_free (mem: data); |
179 | location = g_file_new_for_commandline_arg (arg: "sftp:///path/to/somewhere.png" ); |
180 | icon2 = g_file_icon_new (file: location); |
181 | g_assert (g_icon_equal (icon, icon2)); |
182 | g_object_unref (object: icon); |
183 | g_object_unref (object: icon2); |
184 | g_object_unref (object: location); |
185 | |
186 | #if 0 |
187 | icon = g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png" , &error); |
188 | g_assert_no_error (error); |
189 | data = g_icon_to_string (icon); |
190 | g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere%20with%20whitespace.png" ); |
191 | g_free (data); |
192 | location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png" ); |
193 | icon2 = g_file_icon_new (location); |
194 | g_assert (g_icon_equal (icon, icon2)); |
195 | g_object_unref (location); |
196 | g_object_unref (icon2); |
197 | location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png" ); |
198 | icon2 = g_file_icon_new (location); |
199 | g_assert (g_icon_equal (icon, icon2)); |
200 | g_object_unref (location); |
201 | g_object_unref (icon2); |
202 | g_object_unref (icon); |
203 | #endif |
204 | |
205 | /* Check that GThemedIcon serialization works */ |
206 | |
207 | icon = g_themed_icon_new (iconname: "network-server" ); |
208 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
209 | data = g_icon_to_string (icon); |
210 | icon2 = g_icon_new_for_string (str: data, error: &error); |
211 | g_assert_no_error (error); |
212 | g_assert (g_icon_equal (icon, icon2)); |
213 | g_free (mem: data); |
214 | g_object_unref (object: icon); |
215 | g_object_unref (object: icon2); |
216 | |
217 | icon = g_themed_icon_new (iconname: "icon name with whitespace" ); |
218 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
219 | data = g_icon_to_string (icon); |
220 | icon2 = g_icon_new_for_string (str: data, error: &error); |
221 | g_assert_no_error (error); |
222 | g_assert (g_icon_equal (icon, icon2)); |
223 | g_free (mem: data); |
224 | g_object_unref (object: icon); |
225 | g_object_unref (object: icon2); |
226 | |
227 | icon = g_themed_icon_new_with_default_fallbacks (iconname: "network-server-xyz" ); |
228 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
229 | data = g_icon_to_string (icon); |
230 | icon2 = g_icon_new_for_string (str: data, error: &error); |
231 | g_assert_no_error (error); |
232 | g_assert (g_icon_equal (icon, icon2)); |
233 | g_free (mem: data); |
234 | g_object_unref (object: icon); |
235 | g_object_unref (object: icon2); |
236 | |
237 | /* Check that GEmblemedIcon serialization works */ |
238 | |
239 | icon = g_themed_icon_new (iconname: "face-smirk" ); |
240 | icon2 = g_themed_icon_new (iconname: "emblem-important" ); |
241 | g_themed_icon_append_name (G_THEMED_ICON (icon2), iconname: "emblem-shared" ); |
242 | location = g_file_new_for_uri (uri: "file:///some/path/somewhere.png" ); |
243 | icon3 = g_file_icon_new (file: location); |
244 | g_object_unref (object: location); |
245 | emblem1 = g_emblem_new_with_origin (icon: icon2, origin: G_EMBLEM_ORIGIN_DEVICE); |
246 | emblem2 = g_emblem_new_with_origin (icon: icon3, origin: G_EMBLEM_ORIGIN_LIVEMETADATA); |
247 | icon4 = g_emblemed_icon_new (icon, emblem: emblem1); |
248 | g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem: emblem2); |
249 | data = g_icon_to_string (icon: icon4); |
250 | icon5 = g_icon_new_for_string (str: data, error: &error); |
251 | g_assert_no_error (error); |
252 | g_assert (g_icon_equal (icon4, icon5)); |
253 | |
254 | g_object_get (object: emblem1, first_property_name: "origin" , &origin, "icon" , &i, NULL); |
255 | g_assert (origin == G_EMBLEM_ORIGIN_DEVICE); |
256 | g_assert (i == icon2); |
257 | g_object_unref (object: i); |
258 | |
259 | g_object_unref (object: emblem1); |
260 | g_object_unref (object: emblem2); |
261 | g_object_unref (object: icon); |
262 | g_object_unref (object: icon2); |
263 | g_object_unref (object: icon3); |
264 | g_object_unref (object: icon4); |
265 | g_object_unref (object: icon5); |
266 | g_free (mem: data); |
267 | } |
268 | |
269 | static void |
270 | test_g_icon_serialize (void) |
271 | { |
272 | GIcon *icon; |
273 | GIcon *icon2; |
274 | GIcon *icon3; |
275 | GIcon *icon4; |
276 | GIcon *icon5; |
277 | GEmblem *emblem1; |
278 | GEmblem *emblem2; |
279 | GFile *location; |
280 | GVariant *data; |
281 | gint origin; |
282 | GIcon *i; |
283 | |
284 | /* Check that we can deserialize from well-known specified formats */ |
285 | data = g_variant_new_string (string: "network-server%" ); |
286 | icon = g_icon_deserialize (value: g_variant_ref_sink (value: data)); |
287 | g_variant_unref (value: data); |
288 | icon2 = g_themed_icon_new (iconname: "network-server%" ); |
289 | g_assert (g_icon_equal (icon, icon2)); |
290 | g_object_unref (object: icon); |
291 | g_object_unref (object: icon2); |
292 | |
293 | data = g_variant_new_string (string: "/path/to/somewhere.png" ); |
294 | icon = g_icon_deserialize (value: g_variant_ref_sink (value: data)); |
295 | g_variant_unref (value: data); |
296 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere.png" ); |
297 | icon2 = g_file_icon_new (file: location); |
298 | g_assert (g_icon_equal (icon, icon2)); |
299 | g_object_unref (object: icon); |
300 | g_object_unref (object: icon2); |
301 | g_object_unref (object: location); |
302 | |
303 | data = g_variant_new_string (string: "/path/to/somewhere with whitespace.png" ); |
304 | icon = g_icon_deserialize (value: g_variant_ref_sink (value: data)); |
305 | g_variant_unref (value: data); |
306 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere with whitespace.png" ); |
307 | icon2 = g_file_icon_new (file: location); |
308 | g_assert (g_icon_equal (icon, icon2)); |
309 | g_object_unref (object: location); |
310 | g_object_unref (object: icon2); |
311 | location = g_file_new_for_commandline_arg (arg: "/path/to/somewhere%20with%20whitespace.png" ); |
312 | icon2 = g_file_icon_new (file: location); |
313 | g_assert (!g_icon_equal (icon, icon2)); |
314 | g_object_unref (object: location); |
315 | g_object_unref (object: icon2); |
316 | g_object_unref (object: icon); |
317 | |
318 | data = g_variant_new_string (string: "sftp:///path/to/somewhere.png" ); |
319 | icon = g_icon_deserialize (value: g_variant_ref_sink (value: data)); |
320 | g_variant_unref (value: data); |
321 | location = g_file_new_for_commandline_arg (arg: "sftp:///path/to/somewhere.png" ); |
322 | icon2 = g_file_icon_new (file: location); |
323 | g_assert (g_icon_equal (icon, icon2)); |
324 | g_object_unref (object: icon); |
325 | g_object_unref (object: icon2); |
326 | g_object_unref (object: location); |
327 | |
328 | /* Check that GThemedIcon serialization works */ |
329 | |
330 | icon = g_themed_icon_new (iconname: "network-server" ); |
331 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
332 | data = g_icon_serialize (icon); |
333 | icon2 = g_icon_deserialize (value: data); |
334 | g_assert (g_icon_equal (icon, icon2)); |
335 | g_variant_unref (value: data); |
336 | g_object_unref (object: icon); |
337 | g_object_unref (object: icon2); |
338 | |
339 | icon = g_themed_icon_new (iconname: "icon name with whitespace" ); |
340 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
341 | data = g_icon_serialize (icon); |
342 | icon2 = g_icon_deserialize (value: data); |
343 | g_assert (g_icon_equal (icon, icon2)); |
344 | g_variant_unref (value: data); |
345 | g_object_unref (object: icon); |
346 | g_object_unref (object: icon2); |
347 | |
348 | icon = g_themed_icon_new_with_default_fallbacks (iconname: "network-server-xyz" ); |
349 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "computer" ); |
350 | data = g_icon_serialize (icon); |
351 | icon2 = g_icon_deserialize (value: data); |
352 | g_assert (g_icon_equal (icon, icon2)); |
353 | g_variant_unref (value: data); |
354 | g_object_unref (object: icon); |
355 | g_object_unref (object: icon2); |
356 | |
357 | /* Check that GEmblemedIcon serialization works */ |
358 | |
359 | icon = g_themed_icon_new (iconname: "face-smirk" ); |
360 | icon2 = g_themed_icon_new (iconname: "emblem-important" ); |
361 | g_themed_icon_append_name (G_THEMED_ICON (icon2), iconname: "emblem-shared" ); |
362 | location = g_file_new_for_uri (uri: "file:///some/path/somewhere.png" ); |
363 | icon3 = g_file_icon_new (file: location); |
364 | g_object_unref (object: location); |
365 | emblem1 = g_emblem_new_with_origin (icon: icon2, origin: G_EMBLEM_ORIGIN_DEVICE); |
366 | emblem2 = g_emblem_new_with_origin (icon: icon3, origin: G_EMBLEM_ORIGIN_LIVEMETADATA); |
367 | icon4 = g_emblemed_icon_new (icon, emblem: emblem1); |
368 | g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem: emblem2); |
369 | data = g_icon_serialize (icon: icon4); |
370 | icon5 = g_icon_deserialize (value: data); |
371 | g_assert (g_icon_equal (icon4, icon5)); |
372 | |
373 | g_object_get (object: emblem1, first_property_name: "origin" , &origin, "icon" , &i, NULL); |
374 | g_assert (origin == G_EMBLEM_ORIGIN_DEVICE); |
375 | g_assert (i == icon2); |
376 | g_object_unref (object: i); |
377 | |
378 | g_object_unref (object: emblem1); |
379 | g_object_unref (object: emblem2); |
380 | g_object_unref (object: icon); |
381 | g_object_unref (object: icon2); |
382 | g_object_unref (object: icon3); |
383 | g_object_unref (object: icon4); |
384 | g_object_unref (object: icon5); |
385 | g_variant_unref (value: data); |
386 | } |
387 | |
388 | static void |
389 | test_themed_icon (void) |
390 | { |
391 | GIcon *icon1, *icon2, *icon3, *icon4; |
392 | const gchar *const *names; |
393 | const gchar *names2[] = { "first-symbolic" , "testicon" , "last" , NULL }; |
394 | gchar *str; |
395 | gboolean fallbacks; |
396 | GVariant *variant; |
397 | |
398 | icon1 = g_themed_icon_new (iconname: "testicon" ); |
399 | |
400 | g_object_get (object: icon1, first_property_name: "use-default-fallbacks" , &fallbacks, NULL); |
401 | g_assert (!fallbacks); |
402 | |
403 | names = g_themed_icon_get_names (G_THEMED_ICON (icon1)); |
404 | g_assert_cmpint (g_strv_length ((gchar **)names), ==, 2); |
405 | g_assert_cmpstr (names[0], ==, "testicon" ); |
406 | g_assert_cmpstr (names[1], ==, "testicon-symbolic" ); |
407 | |
408 | g_themed_icon_prepend_name (G_THEMED_ICON (icon1), iconname: "first-symbolic" ); |
409 | g_themed_icon_append_name (G_THEMED_ICON (icon1), iconname: "last" ); |
410 | names = g_themed_icon_get_names (G_THEMED_ICON (icon1)); |
411 | g_assert_cmpint (g_strv_length ((gchar **)names), ==, 6); |
412 | g_assert_cmpstr (names[0], ==, "first-symbolic" ); |
413 | g_assert_cmpstr (names[1], ==, "testicon" ); |
414 | g_assert_cmpstr (names[2], ==, "last" ); |
415 | g_assert_cmpstr (names[3], ==, "first" ); |
416 | g_assert_cmpstr (names[4], ==, "testicon-symbolic" ); |
417 | g_assert_cmpstr (names[5], ==, "last-symbolic" ); |
418 | g_assert_cmpuint (g_icon_hash (icon1), ==, 1812785139); |
419 | |
420 | icon2 = g_themed_icon_new_from_names (iconnames: (gchar**)names2, len: -1); |
421 | g_assert (g_icon_equal (icon1, icon2)); |
422 | |
423 | str = g_icon_to_string (icon: icon2); |
424 | icon3 = g_icon_new_for_string (str, NULL); |
425 | g_assert (g_icon_equal (icon2, icon3)); |
426 | g_free (mem: str); |
427 | |
428 | variant = g_icon_serialize (icon: icon3); |
429 | icon4 = g_icon_deserialize (value: variant); |
430 | g_assert (g_icon_equal (icon3, icon4)); |
431 | g_assert (g_icon_hash (icon3) == g_icon_hash (icon4)); |
432 | g_variant_unref (value: variant); |
433 | |
434 | g_object_unref (object: icon1); |
435 | g_object_unref (object: icon2); |
436 | g_object_unref (object: icon3); |
437 | g_object_unref (object: icon4); |
438 | } |
439 | |
440 | static void |
441 | test_emblemed_icon (void) |
442 | { |
443 | GIcon *icon; |
444 | GIcon *icon1, *icon2, *icon3, *icon4, *icon5; |
445 | GEmblem *emblem, *emblem1, *emblem2; |
446 | GList *emblems; |
447 | GVariant *variant; |
448 | |
449 | icon1 = g_themed_icon_new (iconname: "testicon" ); |
450 | icon2 = g_themed_icon_new (iconname: "testemblem" ); |
451 | emblem1 = g_emblem_new (icon: icon2); |
452 | emblem2 = g_emblem_new_with_origin (icon: icon2, origin: G_EMBLEM_ORIGIN_TAG); |
453 | |
454 | icon3 = g_emblemed_icon_new (icon: icon1, emblem: emblem1); |
455 | emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3)); |
456 | g_assert_cmpint (g_list_length (emblems), ==, 1); |
457 | g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1); |
458 | |
459 | icon4 = g_emblemed_icon_new (icon: icon1, emblem: emblem1); |
460 | g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem: emblem2); |
461 | emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4)); |
462 | g_assert_cmpint (g_list_length (emblems), ==, 2); |
463 | |
464 | g_assert (!g_icon_equal (icon3, icon4)); |
465 | |
466 | variant = g_icon_serialize (icon: icon4); |
467 | icon5 = g_icon_deserialize (value: variant); |
468 | g_assert (g_icon_equal (icon4, icon5)); |
469 | g_assert (g_icon_hash (icon4) == g_icon_hash (icon5)); |
470 | g_variant_unref (value: variant); |
471 | |
472 | emblem = emblems->data; |
473 | g_assert (g_emblem_get_icon (emblem) == icon2); |
474 | g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN); |
475 | |
476 | emblem = emblems->next->data; |
477 | g_assert (g_emblem_get_icon (emblem) == icon2); |
478 | g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG); |
479 | |
480 | g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (icon4)); |
481 | g_assert (g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4)) == NULL); |
482 | |
483 | g_assert (g_icon_hash (icon4) != g_icon_hash (icon2)); |
484 | g_object_get (object: icon4, first_property_name: "gicon" , &icon, NULL); |
485 | g_assert (icon == icon1); |
486 | g_object_unref (object: icon); |
487 | |
488 | g_object_unref (object: icon1); |
489 | g_object_unref (object: icon2); |
490 | g_object_unref (object: icon3); |
491 | g_object_unref (object: icon4); |
492 | g_object_unref (object: icon5); |
493 | |
494 | g_object_unref (object: emblem1); |
495 | g_object_unref (object: emblem2); |
496 | } |
497 | |
498 | static void |
499 | load_cb (GObject *source_object, |
500 | GAsyncResult *res, |
501 | gpointer data) |
502 | { |
503 | GLoadableIcon *icon = G_LOADABLE_ICON (source_object); |
504 | GMainLoop *loop = data; |
505 | GError *error = NULL; |
506 | GInputStream *stream; |
507 | |
508 | stream = g_loadable_icon_load_finish (icon, res, NULL, error: &error); |
509 | g_assert_no_error (error); |
510 | g_assert (G_IS_INPUT_STREAM (stream)); |
511 | g_object_unref (object: stream); |
512 | g_main_loop_quit (loop); |
513 | } |
514 | |
515 | static void |
516 | loadable_icon_tests (GLoadableIcon *icon) |
517 | { |
518 | GError *error = NULL; |
519 | GInputStream *stream; |
520 | GMainLoop *loop; |
521 | |
522 | stream = g_loadable_icon_load (icon, size: 20, NULL, NULL, error: &error); |
523 | g_assert_no_error (error); |
524 | g_assert (G_IS_INPUT_STREAM (stream)); |
525 | g_object_unref (object: stream); |
526 | |
527 | loop = g_main_loop_new (NULL, FALSE); |
528 | g_loadable_icon_load_async (icon, size: 20, NULL, callback: load_cb, user_data: loop); |
529 | g_main_loop_run (loop); |
530 | g_main_loop_unref (loop); |
531 | } |
532 | |
533 | static void |
534 | test_file_icon (void) |
535 | { |
536 | GFile *file; |
537 | GIcon *icon; |
538 | GIcon *icon2; |
539 | GIcon *icon3; |
540 | GIcon *icon4; |
541 | gchar *str; |
542 | GVariant *variant; |
543 | |
544 | file = g_file_new_for_path (path: g_test_get_filename (file_type: G_TEST_DIST, first_path: "g-icon.c" , NULL)); |
545 | icon = g_file_icon_new (file); |
546 | g_object_unref (object: file); |
547 | |
548 | loadable_icon_tests (G_LOADABLE_ICON (icon)); |
549 | |
550 | str = g_icon_to_string (icon); |
551 | icon2 = g_icon_new_for_string (str, NULL); |
552 | g_assert (g_icon_equal (icon, icon2)); |
553 | g_free (mem: str); |
554 | |
555 | file = g_file_new_for_path (path: "/\1\2\3/\244" ); |
556 | icon4 = g_file_icon_new (file); |
557 | |
558 | variant = g_icon_serialize (icon: icon4); |
559 | icon3 = g_icon_deserialize (value: variant); |
560 | g_assert (g_icon_equal (icon4, icon3)); |
561 | g_assert (g_icon_hash (icon4) == g_icon_hash (icon3)); |
562 | g_variant_unref (value: variant); |
563 | |
564 | g_object_unref (object: icon); |
565 | g_object_unref (object: icon2); |
566 | g_object_unref (object: icon3); |
567 | g_object_unref (object: icon4); |
568 | g_object_unref (object: file); |
569 | } |
570 | |
571 | static void |
572 | test_bytes_icon (void) |
573 | { |
574 | GBytes *bytes; |
575 | GBytes *bytes2; |
576 | GIcon *icon; |
577 | GIcon *icon2; |
578 | GIcon *icon3; |
579 | GVariant *variant; |
580 | const gchar *data = "1234567890987654321" ; |
581 | |
582 | bytes = g_bytes_new_static (data, size: strlen (s: data)); |
583 | icon = g_bytes_icon_new (bytes); |
584 | icon2 = g_bytes_icon_new (bytes); |
585 | |
586 | g_assert (g_bytes_icon_get_bytes (G_BYTES_ICON (icon)) == bytes); |
587 | g_assert (g_icon_equal (icon, icon2)); |
588 | g_assert (g_icon_hash (icon) == g_icon_hash (icon2)); |
589 | |
590 | g_object_get (object: icon, first_property_name: "bytes" , &bytes2, NULL); |
591 | g_assert (bytes == bytes2); |
592 | g_bytes_unref (bytes: bytes2); |
593 | |
594 | variant = g_icon_serialize (icon); |
595 | icon3 = g_icon_deserialize (value: variant); |
596 | g_assert (g_icon_equal (icon, icon3)); |
597 | g_assert (g_icon_hash (icon) == g_icon_hash (icon3)); |
598 | |
599 | loadable_icon_tests (G_LOADABLE_ICON (icon)); |
600 | |
601 | g_variant_unref (value: variant); |
602 | g_object_unref (object: icon); |
603 | g_object_unref (object: icon2); |
604 | g_object_unref (object: icon3); |
605 | g_bytes_unref (bytes); |
606 | } |
607 | |
608 | int |
609 | main (int argc, |
610 | char *argv[]) |
611 | { |
612 | g_test_init (argc: &argc, argv: &argv, NULL); |
613 | |
614 | g_test_add_func (testpath: "/icons/to-string" , test_func: test_g_icon_to_string); |
615 | g_test_add_func (testpath: "/icons/serialize" , test_func: test_g_icon_serialize); |
616 | g_test_add_func (testpath: "/icons/themed" , test_func: test_themed_icon); |
617 | g_test_add_func (testpath: "/icons/emblemed" , test_func: test_emblemed_icon); |
618 | g_test_add_func (testpath: "/icons/file" , test_func: test_file_icon); |
619 | g_test_add_func (testpath: "/icons/bytes" , test_func: test_bytes_icon); |
620 | |
621 | return g_test_run(); |
622 | } |
623 | |