LCOV - code coverage report
Current view: top level - gio/tests - mimeapps.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 99.5 % 370 368
Test Date: 2025-11-11 05:25:45 Functions: 100.0 % 11 11
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include <glib/gstdio.h>
       2                 :             : #include <gio/gio.h>
       3                 :             : #include <gio/gdesktopappinfo.h>
       4                 :             : 
       5                 :             : static gboolean
       6                 :           8 : strv_equal (gchar **strv, ...)
       7                 :             : {
       8                 :             :   gsize count;
       9                 :             :   va_list list;
      10                 :             :   const gchar *str;
      11                 :             :   gboolean res;
      12                 :             : 
      13                 :           8 :   res = TRUE;
      14                 :           8 :   count = 0;
      15                 :           8 :   va_start (list, strv);
      16                 :             :   while (1)
      17                 :             :     {
      18                 :          22 :       str = va_arg (list, const gchar *);
      19                 :          22 :       if (str == NULL)
      20                 :           8 :         break;
      21                 :          14 :       if (g_strcmp0 (str, strv[count]) != 0)
      22                 :             :         {
      23                 :           0 :           res = FALSE;
      24                 :           0 :           break;
      25                 :             :         }
      26                 :          14 :       count++;
      27                 :             :     }
      28                 :           8 :   va_end (list);
      29                 :             : 
      30                 :           8 :   if (res)
      31                 :           8 :     res = g_strv_length (strv) == count;
      32                 :             : 
      33                 :           8 :   return res;
      34                 :             : }
      35                 :             : 
      36                 :             : const gchar *myapp_data =
      37                 :             :   "[Desktop Entry]\n"
      38                 :             :   "Encoding=UTF-8\n"
      39                 :             :   "Version=1.0\n"
      40                 :             :   "Type=Application\n"
      41                 :             :   "Exec=true %f\n"
      42                 :             :   "Name=my app\n";
      43                 :             : 
      44                 :             : const gchar *myapp2_data =
      45                 :             :   "[Desktop Entry]\n"
      46                 :             :   "Encoding=UTF-8\n"
      47                 :             :   "Version=1.0\n"
      48                 :             :   "Type=Application\n"
      49                 :             :   "Exec=sleep %f\n"
      50                 :             :   "Name=my app 2\n";
      51                 :             : 
      52                 :             : const gchar *myapp3_data =
      53                 :             :   "[Desktop Entry]\n"
      54                 :             :   "Encoding=UTF-8\n"
      55                 :             :   "Version=1.0\n"
      56                 :             :   "Type=Application\n"
      57                 :             :   "Exec=sleep 1\n"
      58                 :             :   "Name=my app 3\n"
      59                 :             :   "MimeType=image/png;";
      60                 :             : 
      61                 :             : const gchar *myapp4_data =
      62                 :             :   "[Desktop Entry]\n"
      63                 :             :   "Encoding=UTF-8\n"
      64                 :             :   "Version=1.0\n"
      65                 :             :   "Type=Application\n"
      66                 :             :   "Exec=echo %f\n"
      67                 :             :   "Name=my app 4\n"
      68                 :             :   "MimeType=image/bmp;";
      69                 :             : 
      70                 :             : const gchar *myapp5_data =
      71                 :             :   "[Desktop Entry]\n"
      72                 :             :   "Encoding=UTF-8\n"
      73                 :             :   "Version=1.0\n"
      74                 :             :   "Type=Application\n"
      75                 :             :   "Exec=true %f\n"
      76                 :             :   "Name=my app 5\n"
      77                 :             :   "MimeType=image/bmp;x-scheme-handler/ftp;";
      78                 :             : 
      79                 :             : const gchar *nosuchapp_data =
      80                 :             :   "[Desktop Entry]\n"
      81                 :             :   "Encoding=UTF-8\n"
      82                 :             :   "Version=1.0\n"
      83                 :             :   "Type=Application\n"
      84                 :             :   "Exec=no_such_application %f\n"
      85                 :             :   "Name=no such app\n";
      86                 :             : 
      87                 :             : const gchar *defaults_data =
      88                 :             :   "[Default Applications]\n"
      89                 :             :   "image/bmp=myapp4.desktop;\n"
      90                 :             :   "image/png=myapp3.desktop;\n"
      91                 :             :   "x-scheme-handler/ftp=myapp5.desktop;\n";
      92                 :             : 
      93                 :             : const gchar *mimecache_data =
      94                 :             :   "[MIME Cache]\n"
      95                 :             :   "image/bmp=myapp4.desktop;myapp5.desktop;\n"
      96                 :             :   "image/png=myapp3.desktop;\n";
      97                 :             : 
      98                 :             : typedef struct
      99                 :             : {
     100                 :             :   gchar *mimeapps_list_home;  /* (owned) */
     101                 :             : } Fixture;
     102                 :             : 
     103                 :             : /* Set up XDG_DATA_HOME and XDG_DATA_DIRS.
     104                 :             :  * XDG_DATA_DIRS/applications will contain mimeapps.list
     105                 :             :  * XDG_DATA_HOME/applications will contain myapp.desktop
     106                 :             :  * and myapp2.desktop, and no mimeapps.list
     107                 :             :  */
     108                 :             : static void
     109                 :          13 : setup (Fixture       *fixture,
     110                 :             :        gconstpointer  test_data)
     111                 :             : {
     112                 :             :   const gchar *xdgdatahome;
     113                 :             :   const gchar * const *xdgdatadirs;
     114                 :             :   gchar *appdir_name;
     115                 :             :   gchar *apphome;
     116                 :             :   gchar *mimeapps;
     117                 :             :   gchar *name;
     118                 :             :   gint res;
     119                 :          13 :   GError *error = NULL;
     120                 :             : 
     121                 :             :   /* These are already set to a temporary directory through our use of
     122                 :             :    * %G_TEST_OPTION_ISOLATE_DIRS below. */
     123                 :          13 :   xdgdatahome = g_get_user_data_dir ();
     124                 :          13 :   xdgdatadirs = g_get_system_data_dirs ();
     125                 :             : 
     126                 :          13 :   appdir_name = g_build_filename (xdgdatadirs[0], "applications", NULL);
     127                 :          13 :   g_test_message ("creating '%s'", appdir_name);
     128                 :          13 :   res = g_mkdir_with_parents (appdir_name, 0700);
     129                 :          13 :   g_assert_cmpint (res, ==, 0);
     130                 :             : 
     131                 :          13 :   if (!GPOINTER_TO_INT (test_data))
     132                 :             :     {
     133                 :           7 :       name = g_build_filename (appdir_name, "mimeapps.list", NULL);
     134                 :             :     }
     135                 :             :   else
     136                 :             :     {
     137                 :           6 :       GFile *file_a = NULL, *file_b = NULL, *appdir = NULL;
     138                 :             : 
     139                 :             :       /*
     140                 :             :        * Ensure mimeapps.list can be reachable via a symlink chain.
     141                 :             :        * See https://gitlab.gnome.org/GNOME/glib/-/issues/3579
     142                 :             :       */
     143                 :           6 :       appdir = g_file_new_for_path (appdir_name);
     144                 :           6 :       file_a = g_file_get_child (appdir, "mimeapps.list");
     145                 :           6 :       g_file_make_symbolic_link (file_a, "mimeapps.list.b", NULL, &error);
     146                 :           6 :       g_assert_no_error (error);
     147                 :           6 :       g_object_unref (file_a);
     148                 :             : 
     149                 :           6 :       file_b = g_file_get_child (appdir, "mimeapps.list.b");
     150                 :           6 :       g_file_make_symbolic_link (file_b, "mimeapps.list.c", NULL, &error);
     151                 :           6 :       g_assert_no_error (error);
     152                 :           6 :       g_object_unref (file_b);
     153                 :             : 
     154                 :           6 :       g_object_unref (appdir);
     155                 :           6 :       name = g_build_filename (appdir_name, "mimeapps.list.c", NULL);
     156                 :             :     }
     157                 :             : 
     158                 :          13 :   g_test_message ("creating '%s'", name);
     159                 :          13 :   g_file_set_contents (name, defaults_data, -1, &error);
     160                 :          13 :   g_assert_no_error (error);
     161                 :          13 :   g_free (name);
     162                 :             : 
     163                 :          13 :   apphome = g_build_filename (xdgdatahome, "applications", NULL);
     164                 :          13 :   g_test_message ("creating '%s'", apphome);
     165                 :          13 :   res = g_mkdir_with_parents (apphome, 0700);
     166                 :          13 :   g_assert_cmpint (res, ==, 0);
     167                 :             : 
     168                 :          13 :   name = g_build_filename (apphome, "myapp.desktop", NULL);
     169                 :          13 :   g_test_message ("creating '%s'", name);
     170                 :          13 :   g_file_set_contents (name, myapp_data, -1, &error);
     171                 :          13 :   g_assert_no_error (error);
     172                 :          13 :   g_free (name);
     173                 :             : 
     174                 :          13 :   name = g_build_filename (apphome, "myapp2.desktop", NULL);
     175                 :          13 :   g_test_message ("creating '%s'", name);
     176                 :          13 :   g_file_set_contents (name, myapp2_data, -1, &error);
     177                 :          13 :   g_assert_no_error (error);
     178                 :          13 :   g_free (name);
     179                 :             : 
     180                 :          13 :   name = g_build_filename (apphome, "myapp3.desktop", NULL);
     181                 :          13 :   g_test_message ("creating '%s'", name);
     182                 :          13 :   g_file_set_contents (name, myapp3_data, -1, &error);
     183                 :          13 :   g_assert_no_error (error);
     184                 :          13 :   g_free (name);
     185                 :             : 
     186                 :          13 :   name = g_build_filename (apphome, "myapp4.desktop", NULL);
     187                 :          13 :   g_test_message ("creating '%s'", name);
     188                 :          13 :   g_file_set_contents (name, myapp4_data, -1, &error);
     189                 :          13 :   g_assert_no_error (error);
     190                 :          13 :   g_free (name);
     191                 :             : 
     192                 :          13 :   name = g_build_filename (apphome, "myapp5.desktop", NULL);
     193                 :          13 :   g_test_message ("creating '%s'", name);
     194                 :          13 :   g_file_set_contents (name, myapp5_data, -1, &error);
     195                 :          13 :   g_assert_no_error (error);
     196                 :          13 :   g_free (name);
     197                 :             : 
     198                 :          13 :   name = g_build_filename (apphome, "nosuchapp.desktop", NULL);
     199                 :          13 :   g_test_message ("creating '%s'", name);
     200                 :          13 :   g_file_set_contents (name, nosuchapp_data, -1, &error);
     201                 :          13 :   g_assert_no_error (error);
     202                 :          13 :   g_free (name);
     203                 :             : 
     204                 :          13 :   mimeapps = g_build_filename (apphome, "mimeapps.list", NULL);
     205                 :          13 :   g_test_message ("removing '%s'", mimeapps);
     206                 :          13 :   g_remove (mimeapps);
     207                 :             : 
     208                 :          13 :   name = g_build_filename (apphome, "mimeinfo.cache", NULL);
     209                 :          13 :   g_test_message ("creating '%s'", name);
     210                 :          13 :   g_file_set_contents (name, mimecache_data, -1, &error);
     211                 :          13 :   g_assert_no_error (error);
     212                 :          13 :   g_free (name);
     213                 :             : 
     214                 :          13 :   g_free (apphome);
     215                 :          13 :   g_free (appdir_name);
     216                 :          13 :   g_free (mimeapps);
     217                 :             : 
     218                 :             :   /* Pointer to one of the temporary directories. */
     219                 :          13 :   fixture->mimeapps_list_home = g_build_filename (g_get_user_config_dir (), "mimeapps.list", NULL);
     220                 :          13 : }
     221                 :             : 
     222                 :             : static void
     223                 :          13 : teardown (Fixture       *fixture,
     224                 :             :           gconstpointer  test_data)
     225                 :             : {
     226                 :          13 :   g_free (fixture->mimeapps_list_home);
     227                 :          13 : }
     228                 :             : 
     229                 :             : static void
     230                 :           2 : test_mime_api (Fixture       *fixture,
     231                 :             :                gconstpointer  test_data)
     232                 :             : {
     233                 :             :   GAppInfo *appinfo;
     234                 :             :   GAppInfo *appinfo2;
     235                 :           2 :   GError *error = NULL;
     236                 :             :   GAppInfo *def;
     237                 :             :   GList *list;
     238                 :           2 :   const gchar *contenttype = "application/pdf";
     239                 :             : 
     240                 :             :   /* clear things out */
     241                 :           2 :   g_app_info_reset_type_associations (contenttype);
     242                 :             : 
     243                 :           2 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     244                 :           2 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     245                 :             : 
     246                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     247                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     248                 :           2 :   g_assert_null (def);
     249                 :           2 :   g_assert_null (list);
     250                 :             : 
     251                 :             :   /* 1. add a non-default association */
     252                 :           2 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     253                 :           2 :   g_assert_no_error (error);
     254                 :             : 
     255                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     256                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     257                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo));
     258                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 1);
     259                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     260                 :           2 :   g_object_unref (def);
     261                 :           2 :   g_list_free_full (list, g_object_unref);
     262                 :             : 
     263                 :             :   /* 2. add another non-default association */
     264                 :           2 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     265                 :           2 :   g_assert_no_error (error);
     266                 :             : 
     267                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     268                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     269                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo));
     270                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     271                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     272                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     273                 :           2 :   g_object_unref (def);
     274                 :           2 :   g_list_free_full (list, g_object_unref);
     275                 :             : 
     276                 :             :   /* 3. make the first app the default */
     277                 :           2 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     278                 :           2 :   g_assert_no_error (error);
     279                 :             : 
     280                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     281                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     282                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo));
     283                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     284                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     285                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     286                 :           2 :   g_object_unref (def);
     287                 :           2 :   g_list_free_full (list, g_object_unref);
     288                 :             : 
     289                 :             :   /* 4. make the second app the last used one */
     290                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
     291                 :           2 :   g_assert_no_error (error);
     292                 :             : 
     293                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     294                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     295                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo));
     296                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     297                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
     298                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
     299                 :           2 :   g_object_unref (def);
     300                 :           2 :   g_list_free_full (list, g_object_unref);
     301                 :             : 
     302                 :             :   /* 5. reset everything */
     303                 :           2 :   g_app_info_reset_type_associations (contenttype);
     304                 :             : 
     305                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     306                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     307                 :           2 :   g_assert_null (def);
     308                 :           2 :   g_assert_null (list);
     309                 :             : 
     310                 :           2 :   g_object_unref (appinfo);
     311                 :           2 :   g_object_unref (appinfo2);
     312                 :           2 : }
     313                 :             : 
     314                 :             : /* Repeat the same tests, this time checking that we handle
     315                 :             :  * mimeapps.list as expected. These tests are different from
     316                 :             :  * the ones in test_mime_api() in that we directly parse
     317                 :             :  * mimeapps.list to verify the results.
     318                 :             :  */
     319                 :             : static void
     320                 :           2 : test_mime_file (Fixture       *fixture,
     321                 :             :                 gconstpointer  test_data)
     322                 :             : {
     323                 :             :   gchar **assoc;
     324                 :             :   GAppInfo *appinfo;
     325                 :             :   GAppInfo *appinfo2;
     326                 :           2 :   GError *error = NULL;
     327                 :             :   GKeyFile *keyfile;
     328                 :             :   gchar *str;
     329                 :             :   gboolean res;
     330                 :             :   GAppInfo *def;
     331                 :             :   GList *list;
     332                 :           2 :   const gchar *contenttype = "application/pdf";
     333                 :             : 
     334                 :             :   /* clear things out */
     335                 :           2 :   g_app_info_reset_type_associations (contenttype);
     336                 :             : 
     337                 :           2 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     338                 :           2 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     339                 :             : 
     340                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     341                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     342                 :           2 :   g_assert_null (def);
     343                 :           2 :   g_assert_null (list);
     344                 :             : 
     345                 :             :   /* 1. add a non-default association */
     346                 :           2 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     347                 :           2 :   g_assert_no_error (error);
     348                 :             : 
     349                 :           2 :   keyfile = g_key_file_new ();
     350                 :           2 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     351                 :           2 :   g_assert_no_error (error);
     352                 :             : 
     353                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     354                 :           2 :   g_assert_no_error (error);
     355                 :           2 :   g_assert_true (strv_equal (assoc, "myapp.desktop", NULL));
     356                 :           2 :   g_strfreev (assoc);
     357                 :             : 
     358                 :             :   /* we've unset XDG_DATA_DIRS so there should be no default */
     359                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
     360                 :           2 :   g_assert_nonnull (error);
     361                 :           2 :   g_clear_error (&error);
     362                 :             : 
     363                 :           2 :   g_key_file_free (keyfile);
     364                 :             : 
     365                 :             :   /* 2. add another non-default association */
     366                 :           2 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     367                 :           2 :   g_assert_no_error (error);
     368                 :             : 
     369                 :           2 :   keyfile = g_key_file_new ();
     370                 :           2 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     371                 :           2 :   g_assert_no_error (error);
     372                 :             : 
     373                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     374                 :           2 :   g_assert_no_error (error);
     375                 :           2 :   g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
     376                 :           2 :   g_strfreev (assoc);
     377                 :             : 
     378                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
     379                 :           2 :   g_assert_nonnull (error);
     380                 :           2 :   g_clear_error (&error);
     381                 :             : 
     382                 :           2 :   g_key_file_free (keyfile);
     383                 :             : 
     384                 :             :   /* 3. make the first app the default */
     385                 :           2 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     386                 :           2 :   g_assert_no_error (error);
     387                 :             : 
     388                 :           2 :   keyfile = g_key_file_new ();
     389                 :           2 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     390                 :           2 :   g_assert_no_error (error);
     391                 :             : 
     392                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     393                 :           2 :   g_assert_no_error (error);
     394                 :           2 :   g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
     395                 :           2 :   g_strfreev (assoc);
     396                 :             : 
     397                 :           2 :   str = g_key_file_get_string (keyfile, "Default Applications", contenttype, &error);
     398                 :           2 :   g_assert_no_error (error);
     399                 :           2 :   g_assert_cmpstr (str, ==, "myapp.desktop");
     400                 :           2 :   g_free (str);
     401                 :             : 
     402                 :           2 :   g_key_file_free (keyfile);
     403                 :             : 
     404                 :             :   /* 4. make the second app the last used one */
     405                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
     406                 :           2 :   g_assert_no_error (error);
     407                 :             : 
     408                 :           2 :   keyfile = g_key_file_new ();
     409                 :           2 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     410                 :           2 :   g_assert_no_error (error);
     411                 :             : 
     412                 :           2 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     413                 :           2 :   g_assert_no_error (error);
     414                 :           2 :   g_assert_true (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
     415                 :           2 :   g_strfreev (assoc);
     416                 :             : 
     417                 :           2 :   g_key_file_free (keyfile);
     418                 :             : 
     419                 :             :   /* 5. reset everything */
     420                 :           2 :   g_app_info_reset_type_associations (contenttype);
     421                 :             : 
     422                 :           2 :   keyfile = g_key_file_new ();
     423                 :           2 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     424                 :           2 :   g_assert_no_error (error);
     425                 :             : 
     426                 :           2 :   res = g_key_file_has_key (keyfile, "Added Associations", contenttype, NULL);
     427                 :           2 :   g_assert_false (res);
     428                 :             : 
     429                 :           2 :   res = g_key_file_has_key (keyfile, "Default Applications", contenttype, NULL);
     430                 :           2 :   g_assert_false (res);
     431                 :             : 
     432                 :           2 :   g_key_file_free (keyfile);
     433                 :             : 
     434                 :           2 :   g_object_unref (appinfo);
     435                 :           2 :   g_object_unref (appinfo2);
     436                 :           2 : }
     437                 :             : 
     438                 :             : /* test interaction between mimeapps.list at different levels */
     439                 :             : static void
     440                 :           2 : test_mime_default (Fixture       *fixture,
     441                 :             :                    gconstpointer  test_data)
     442                 :             : {
     443                 :             :   GAppInfo *appinfo;
     444                 :             :   GAppInfo *appinfo2;
     445                 :             :   GAppInfo *appinfo3;
     446                 :           2 :   GError *error = NULL;
     447                 :             :   GAppInfo *def;
     448                 :             :   GList *list;
     449                 :           2 :   const gchar *contenttype = "image/png";
     450                 :             : 
     451                 :             :   /* clear things out */
     452                 :           2 :   g_app_info_reset_type_associations (contenttype);
     453                 :             : 
     454                 :           2 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     455                 :           2 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     456                 :           2 :   appinfo3 = (GAppInfo*)g_desktop_app_info_new ("myapp3.desktop");
     457                 :             : 
     458                 :             :   /* myapp3 is set as the default in defaults.list */
     459                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     460                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     461                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo3));
     462                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 1);
     463                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo3));
     464                 :           2 :   g_object_unref (def);
     465                 :           2 :   g_list_free_full (list, g_object_unref);
     466                 :             : 
     467                 :             :   /* 1. add a non-default association */
     468                 :           2 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     469                 :           2 :   g_assert_no_error (error);
     470                 :             : 
     471                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     472                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     473                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo3)); /* default is unaffected */
     474                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     475                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     476                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo3));
     477                 :           2 :   g_object_unref (def);
     478                 :           2 :   g_list_free_full (list, g_object_unref);
     479                 :             : 
     480                 :             :   /* 2. add another non-default association */
     481                 :           2 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     482                 :           2 :   g_assert_no_error (error);
     483                 :             : 
     484                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     485                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     486                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo3));
     487                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 3);
     488                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     489                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     490                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
     491                 :           2 :   g_object_unref (def);
     492                 :           2 :   g_list_free_full (list, g_object_unref);
     493                 :             : 
     494                 :             :   /* 3. make the first app the default */
     495                 :           2 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     496                 :           2 :   g_assert_no_error (error);
     497                 :             : 
     498                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     499                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     500                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo));
     501                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 3);
     502                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     503                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     504                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
     505                 :           2 :   g_object_unref (def);
     506                 :           2 :   g_list_free_full (list, g_object_unref);
     507                 :             : 
     508                 :           2 :   g_object_unref (appinfo);
     509                 :           2 :   g_object_unref (appinfo2);
     510                 :           2 :   g_object_unref (appinfo3);
     511                 :           2 : }
     512                 :             : 
     513                 :             : /* test interaction between mimeinfo.cache, defaults.list and mimeapps.list
     514                 :             :  * to ensure g_app_info_set_as_last_used_for_type doesn't incorrectly
     515                 :             :  * change the default
     516                 :             :  */
     517                 :             : static void
     518                 :           2 : test_mime_default_last_used (Fixture       *fixture,
     519                 :             :                              gconstpointer  test_data)
     520                 :             : {
     521                 :             :   GAppInfo *appinfo4;
     522                 :             :   GAppInfo *appinfo5;
     523                 :           2 :   GError *error = NULL;
     524                 :             :   GAppInfo *def;
     525                 :             :   GList *list;
     526                 :           2 :   const gchar *contenttype = "image/bmp";
     527                 :             : 
     528                 :             :   /* clear things out */
     529                 :           2 :   g_app_info_reset_type_associations (contenttype);
     530                 :             : 
     531                 :           2 :   appinfo4 = (GAppInfo*)g_desktop_app_info_new ("myapp4.desktop");
     532                 :           2 :   appinfo5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
     533                 :             : 
     534                 :             :   /* myapp4 is set as the default in defaults.list */
     535                 :             :   /* myapp4 and myapp5 can both handle image/bmp */
     536                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     537                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     538                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo4));
     539                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     540                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     541                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     542                 :           2 :   g_object_unref (def);
     543                 :           2 :   g_list_free_full (list, g_object_unref);
     544                 :             : 
     545                 :             :   /* 1. set default (myapp4) as last used */
     546                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
     547                 :           2 :   g_assert_no_error (error);
     548                 :             : 
     549                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     550                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     551                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo4)); /* default is unaffected */
     552                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     553                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     554                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     555                 :           2 :   g_object_unref (def);
     556                 :           2 :   g_list_free_full (list, g_object_unref);
     557                 :             : 
     558                 :             :   /* 2. set other (myapp5) as last used */
     559                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
     560                 :           2 :   g_assert_no_error (error);
     561                 :             : 
     562                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     563                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     564                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo4));
     565                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     566                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     567                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     568                 :           2 :   g_object_unref (def);
     569                 :           2 :   g_list_free_full (list, g_object_unref);
     570                 :             : 
     571                 :             :   /* 3. change the default to myapp5 */
     572                 :           2 :   g_app_info_set_as_default_for_type (appinfo5, contenttype, &error);
     573                 :           2 :   g_assert_no_error (error);
     574                 :             : 
     575                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     576                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     577                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo5));
     578                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     579                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     580                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     581                 :           2 :   g_object_unref (def);
     582                 :           2 :   g_list_free_full (list, g_object_unref);
     583                 :             : 
     584                 :             :   /* 4. set myapp4 as last used */
     585                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
     586                 :           2 :   g_assert_no_error (error);
     587                 :             : 
     588                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     589                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     590                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo5));
     591                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     592                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     593                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     594                 :           2 :   g_object_unref (def);
     595                 :           2 :   g_list_free_full (list, g_object_unref);
     596                 :             : 
     597                 :             :   /* 5. set myapp5 as last used again */
     598                 :           2 :   g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
     599                 :           2 :   g_assert_no_error (error);
     600                 :             : 
     601                 :           2 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     602                 :           2 :   list = g_app_info_get_recommended_for_type (contenttype);
     603                 :           2 :   g_assert_true (g_app_info_equal (def, appinfo5));
     604                 :           2 :   g_assert_cmpint (g_list_length (list), ==, 2);
     605                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     606                 :           2 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     607                 :           2 :   g_object_unref (def);
     608                 :           2 :   g_list_free_full (list, g_object_unref);
     609                 :             : 
     610                 :           2 :   g_object_unref (appinfo4);
     611                 :           2 :   g_object_unref (appinfo5);
     612                 :           2 : }
     613                 :             : 
     614                 :             : static void
     615                 :           2 : test_scheme_handler (Fixture       *fixture,
     616                 :             :                      gconstpointer  test_data)
     617                 :             : {
     618                 :             :   GAppInfo *info, *info5;
     619                 :             : 
     620                 :           2 :   info5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
     621                 :           2 :   info = g_app_info_get_default_for_uri_scheme ("ftp");
     622                 :           2 :   g_assert_true (g_app_info_equal (info, info5));
     623                 :             : 
     624                 :           2 :   g_object_unref (info);
     625                 :           2 :   g_object_unref (info5);
     626                 :           2 : }
     627                 :             : 
     628                 :             : /* test that g_app_info_* ignores desktop files with nonexisting executables
     629                 :             :  */
     630                 :             : static void
     631                 :           2 : test_mime_ignore_nonexisting (Fixture       *fixture,
     632                 :             :                               gconstpointer  test_data)
     633                 :             : {
     634                 :             :   GAppInfo *appinfo;
     635                 :             : 
     636                 :           2 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("nosuchapp.desktop");
     637                 :           2 :   g_assert_null (appinfo);
     638                 :           2 : }
     639                 :             : 
     640                 :             : static void
     641                 :           1 : test_all (Fixture       *fixture,
     642                 :             :           gconstpointer  test_data)
     643                 :             : {
     644                 :             :   GList *all, *l;
     645                 :             : 
     646                 :           1 :   all = g_app_info_get_all ();
     647                 :             : 
     648                 :           6 :   for (l = all; l; l = l->next)
     649                 :           5 :     g_assert_true (G_IS_APP_INFO (l->data));
     650                 :             : 
     651                 :           1 :   g_list_free_full (all, g_object_unref);
     652                 :           1 : }
     653                 :             : 
     654                 :             : int
     655                 :           1 : main (int argc, char *argv[])
     656                 :             : {
     657                 :           1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     658                 :             : 
     659                 :           1 :   g_test_add ("/appinfo/mime/api", Fixture, GINT_TO_POINTER (FALSE), setup,
     660                 :             :               test_mime_api, teardown);
     661                 :           1 :   g_test_add ("/appinfo/mime/default", Fixture, GINT_TO_POINTER (FALSE), setup,
     662                 :             :               test_mime_default, teardown);
     663                 :           1 :   g_test_add ("/appinfo/mime/file", Fixture, GINT_TO_POINTER (FALSE), setup,
     664                 :             :               test_mime_file, teardown);
     665                 :           1 :   g_test_add ("/appinfo/mime/scheme-handler", Fixture, GINT_TO_POINTER (FALSE), setup,
     666                 :             :               test_scheme_handler, teardown);
     667                 :           1 :   g_test_add ("/appinfo/mime/default-last-used", Fixture, GINT_TO_POINTER (FALSE), setup,
     668                 :             :               test_mime_default_last_used, teardown);
     669                 :           1 :   g_test_add ("/appinfo/mime/ignore-nonexisting", Fixture, GINT_TO_POINTER (FALSE), setup,
     670                 :             :               test_mime_ignore_nonexisting, teardown);
     671                 :             : 
     672                 :           1 :   g_test_add ("/appinfo/mime-symlinked/api", Fixture, GINT_TO_POINTER (TRUE), setup,
     673                 :             :               test_mime_api, teardown);
     674                 :           1 :   g_test_add ("/appinfo/mime-symlinked/default", Fixture, GINT_TO_POINTER (TRUE), setup,
     675                 :             :               test_mime_default, teardown);
     676                 :           1 :   g_test_add ("/appinfo/mime-symlinked/file", Fixture, GINT_TO_POINTER (TRUE), setup,
     677                 :             :               test_mime_file, teardown);
     678                 :           1 :   g_test_add ("/appinfo/mime-symlinked/scheme-handler", Fixture, GINT_TO_POINTER (TRUE), setup,
     679                 :             :               test_scheme_handler, teardown);
     680                 :           1 :   g_test_add ("/appinfo/mime-symlinked/default-last-used", Fixture, GINT_TO_POINTER (TRUE), setup,
     681                 :             :               test_mime_default_last_used, teardown);
     682                 :           1 :   g_test_add ("/appinfo/mime-symlinked/ignore-nonexisting", Fixture, GINT_TO_POINTER (TRUE), setup,
     683                 :             :               test_mime_ignore_nonexisting, teardown);
     684                 :             : 
     685                 :           1 :   g_test_add ("/appinfo/all", Fixture, NULL, setup, test_all, teardown);
     686                 :             : 
     687                 :           1 :   return g_test_run ();
     688                 :             : }
        

Generated by: LCOV version 2.0-1