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