Home | History | Annotate | Download | only in tests
      1 /* GLib testing framework examples and tests
      2  *
      3  * Copyright (C) 2008 Red Hat, Inc.
      4  *
      5  * This work is provided "as is"; redistribution and modification
      6  * in whole or in part, in any medium, physical or electronic is
      7  * permitted without restriction.
      8  *
      9  * This work is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12  *
     13  * In no event shall the authors or contributors be liable for any
     14  * direct, indirect, incidental, special, exemplary, or consequential
     15  * damages (including, but not limited to, procurement of substitute
     16  * goods or services; loss of use, data, or profits; or business
     17  * interruption) however caused and on any theory of liability, whether
     18  * in contract, strict liability, or tort (including negligence or
     19  * otherwise) arising in any way out of the use of this software, even
     20  * if advised of the possibility of such damage.
     21  *
     22  * Authors: David Zeuthen <davidz (at) redhat.com>
     23  */
     24 
     25 #include <glib/glib.h>
     26 #include <gio/gio.h>
     27 #include <stdlib.h>
     28 #include <string.h>
     29 
     30 static void
     31 test_g_icon_serialize (void)
     32 {
     33   GIcon *icon;
     34   GIcon *icon2;
     35   GIcon *icon3;
     36   GIcon *icon4;
     37   GIcon *icon5;
     38   GEmblem *emblem1;
     39   GEmblem *emblem2;
     40   const char *uri;
     41   GFile *location;
     42   char *data;
     43   GError *error;
     44 
     45   error = NULL;
     46 
     47   /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
     48 
     49   uri = "file:///some/native/path/to/an/icon.png";
     50   location = g_file_new_for_uri (uri);
     51   icon = g_file_icon_new (location);
     52   data = g_icon_to_string (icon);
     53   g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon.png");
     54   icon2 = g_icon_new_for_string (data, &error);
     55   g_assert_no_error (error);
     56   g_assert (g_icon_equal (icon, icon2));
     57   g_free (data);
     58   g_object_unref (icon);
     59   g_object_unref (icon2);
     60   g_object_unref (location);
     61 
     62   uri = "file:///some/native/path/to/an/icon with spaces.png";
     63   location = g_file_new_for_uri (uri);
     64   icon = g_file_icon_new (location);
     65   data = g_icon_to_string (icon);
     66   g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon with spaces.png");
     67   icon2 = g_icon_new_for_string (data, &error);
     68   g_assert_no_error (error);
     69   g_assert (g_icon_equal (icon, icon2));
     70   g_free (data);
     71   g_object_unref (icon);
     72   g_object_unref (icon2);
     73   g_object_unref (location);
     74 
     75   uri = "sftp:///some/non-native/path/to/an/icon.png";
     76   location = g_file_new_for_uri (uri);
     77   icon = g_file_icon_new (location);
     78   data = g_icon_to_string (icon);
     79   g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png");
     80   icon2 = g_icon_new_for_string (data, &error);
     81   g_assert_no_error (error);
     82   g_assert (g_icon_equal (icon, icon2));
     83   g_free (data);
     84   g_object_unref (icon);
     85   g_object_unref (icon2);
     86   g_object_unref (location);
     87 
     88 #if 0
     89   uri = "sftp:///some/non-native/path/to/an/icon with spaces.png";
     90   location = g_file_new_for_uri (uri);
     91   icon = g_file_icon_new (location);
     92   data = g_icon_to_string (icon);
     93   g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
     94   icon2 = g_icon_new_for_string (data, &error);
     95   g_assert_no_error (error);
     96   g_assert (g_icon_equal (icon, icon2));
     97   g_free (data);
     98   g_object_unref (icon);
     99   g_object_unref (icon2);
    100   g_object_unref (location);
    101 #endif
    102 
    103   icon = g_themed_icon_new ("network-server");
    104   data = g_icon_to_string (icon);
    105   g_assert_cmpstr (data, ==, "network-server");
    106   icon2 = g_icon_new_for_string (data, &error);
    107   g_assert_no_error (error);
    108   g_assert (g_icon_equal (icon, icon2));
    109   g_free (data);
    110   g_object_unref (icon);
    111   g_object_unref (icon2);
    112 
    113   /* Check that we can serialize from well-known specified formats */
    114   icon = g_icon_new_for_string ("network-server%", &error);
    115   g_assert_no_error (error);
    116   icon2 = g_themed_icon_new ("network-server%");
    117   g_assert (g_icon_equal (icon, icon2));
    118   g_object_unref (icon);
    119   g_object_unref (icon2);
    120 
    121   icon = g_icon_new_for_string ("/path/to/somewhere.png", &error);
    122   g_assert_no_error (error);
    123   location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
    124   icon2 = g_file_icon_new (location);
    125   g_assert (g_icon_equal (icon, icon2));
    126   g_object_unref (icon);
    127   g_object_unref (icon2);
    128   g_object_unref (location);
    129 
    130   icon = g_icon_new_for_string ("/path/to/somewhere with whitespace.png", &error);
    131   g_assert_no_error (error);
    132   data = g_icon_to_string (icon);
    133   g_assert_cmpstr (data, ==, "/path/to/somewhere with whitespace.png");
    134   g_free (data);
    135   location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
    136   icon2 = g_file_icon_new (location);
    137   g_assert (g_icon_equal (icon, icon2));
    138   g_object_unref (location);
    139   g_object_unref (icon2);
    140   location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
    141   icon2 = g_file_icon_new (location);
    142   g_assert (!g_icon_equal (icon, icon2));
    143   g_object_unref (location);
    144   g_object_unref (icon2);
    145   g_object_unref (icon);
    146 
    147   icon = g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error);
    148   g_assert_no_error (error);
    149   data = g_icon_to_string (icon);
    150   g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere.png");
    151   g_free (data);
    152   location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
    153   icon2 = g_file_icon_new (location);
    154   g_assert (g_icon_equal (icon, icon2));
    155   g_object_unref (icon);
    156   g_object_unref (icon2);
    157   g_object_unref (location);
    158 
    159 #if 0
    160   icon = g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png", &error);
    161   g_assert_no_error (error);
    162   data = g_icon_to_string (icon);
    163   g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere%20with%20whitespace.png");
    164   g_free (data);
    165   location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png");
    166   icon2 = g_file_icon_new (location);
    167   g_assert (g_icon_equal (icon, icon2));
    168   g_object_unref (location);
    169   g_object_unref (icon2);
    170   location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png");
    171   icon2 = g_file_icon_new (location);
    172   g_assert (g_icon_equal (icon, icon2));
    173   g_object_unref (location);
    174   g_object_unref (icon2);
    175   g_object_unref (icon);
    176 #endif
    177 
    178   /* Check that GThemedIcon serialization works */
    179 
    180   icon = g_themed_icon_new ("network-server");
    181   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
    182   data = g_icon_to_string (icon);
    183   icon2 = g_icon_new_for_string (data, &error);
    184   g_assert_no_error (error);
    185   g_assert (g_icon_equal (icon, icon2));
    186   g_free (data);
    187   g_object_unref (icon);
    188   g_object_unref (icon2);
    189 
    190   icon = g_themed_icon_new ("icon name with whitespace");
    191   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
    192   data = g_icon_to_string (icon);
    193   icon2 = g_icon_new_for_string (data, &error);
    194   g_assert_no_error (error);
    195   g_assert (g_icon_equal (icon, icon2));
    196   g_free (data);
    197   g_object_unref (icon);
    198   g_object_unref (icon2);
    199 
    200   icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
    201   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
    202   data = g_icon_to_string (icon);
    203   icon2 = g_icon_new_for_string (data, &error);
    204   g_assert_no_error (error);
    205   g_assert (g_icon_equal (icon, icon2));
    206   g_free (data);
    207   g_object_unref (icon);
    208   g_object_unref (icon2);
    209 
    210   /* Check that GEmblemedIcon serialization works */
    211 
    212   icon = g_themed_icon_new ("face-smirk");
    213   icon2 = g_themed_icon_new ("emblem-important");
    214   g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
    215   location = g_file_new_for_uri ("file:///some/path/somewhere.png");
    216   icon3 = g_file_icon_new (location);
    217   g_object_unref (location);
    218   emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
    219   emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
    220   icon4 = g_emblemed_icon_new (icon, emblem1);
    221   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
    222   data = g_icon_to_string (icon4);
    223   icon5 = g_icon_new_for_string (data, &error);
    224   g_assert_no_error (error);
    225   g_assert (g_icon_equal (icon4, icon5));
    226   g_object_unref (emblem1);
    227   g_object_unref (emblem2);
    228   g_object_unref (icon);
    229   g_object_unref (icon2);
    230   g_object_unref (icon3);
    231   g_object_unref (icon4);
    232   g_object_unref (icon5);
    233 }
    234 
    235 int
    236 main (int   argc,
    237       char *argv[])
    238 {
    239   g_type_init ();
    240   g_test_init (&argc, &argv, NULL);
    241 
    242   g_test_add_func ("/g-icon/serialize", test_g_icon_serialize);
    243 
    244   return g_test_run();
    245 }
    246