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

Generated by: LCOV version 2.0-1