LCOV - code coverage report
Current view: top level - gio/tests - g-icon.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 399 399
Test Date: 2025-07-08 05:15:22 Functions: 100.0 % 9 9
Branches: - 0 0

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

Generated by: LCOV version 2.0-1