LCOV - code coverage report
Current view: top level - glib/gio/tests - mimeapps.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 349 351 99.4 %
Date: 2024-04-23 05:16:05 Functions: 11 11 100.0 %
Branches: 6 8 75.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                 :          4 : strv_equal (gchar **strv, ...)
       7                 :            : {
       8                 :            :   gsize count;
       9                 :            :   va_list list;
      10                 :            :   const gchar *str;
      11                 :            :   gboolean res;
      12                 :            : 
      13                 :          4 :   res = TRUE;
      14                 :          4 :   count = 0;
      15                 :          4 :   va_start (list, strv);
      16                 :            :   while (1)
      17                 :            :     {
      18                 :         11 :       str = va_arg (list, const gchar *);
      19         [ +  + ]:         11 :       if (str == NULL)
      20                 :          4 :         break;
      21         [ -  + ]:          7 :       if (g_strcmp0 (str, strv[count]) != 0)
      22                 :            :         {
      23                 :          0 :           res = FALSE;
      24                 :          0 :           break;
      25                 :            :         }
      26                 :          7 :       count++;
      27                 :            :     }
      28                 :          4 :   va_end (list);
      29                 :            : 
      30         [ +  - ]:          4 :   if (res)
      31                 :          4 :     res = g_strv_length (strv) == count;
      32                 :            : 
      33                 :          4 :   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                 :          7 : setup (Fixture       *fixture,
     110                 :            :        gconstpointer  test_data)
     111                 :            : {
     112                 :            :   const gchar *xdgdatahome;
     113                 :            :   const gchar * const *xdgdatadirs;
     114                 :            :   gchar *appdir;
     115                 :            :   gchar *apphome;
     116                 :            :   gchar *mimeapps;
     117                 :            :   gchar *name;
     118                 :            :   gint res;
     119                 :          7 :   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                 :          7 :   xdgdatahome = g_get_user_data_dir ();
     124                 :          7 :   xdgdatadirs = g_get_system_data_dirs ();
     125                 :            : 
     126                 :          7 :   appdir = g_build_filename (xdgdatadirs[0], "applications", NULL);
     127                 :          7 :   g_test_message ("creating '%s'", appdir);
     128                 :          7 :   res = g_mkdir_with_parents (appdir, 0700);
     129                 :          7 :   g_assert_cmpint (res, ==, 0);
     130                 :            : 
     131                 :          7 :   name = g_build_filename (appdir, "mimeapps.list", NULL);
     132                 :          7 :   g_test_message ("creating '%s'", name);
     133                 :          7 :   g_file_set_contents (name, defaults_data, -1, &error);
     134                 :          7 :   g_assert_no_error (error);
     135                 :          7 :   g_free (name);
     136                 :            : 
     137                 :          7 :   apphome = g_build_filename (xdgdatahome, "applications", NULL);
     138                 :          7 :   g_test_message ("creating '%s'", apphome);
     139                 :          7 :   res = g_mkdir_with_parents (apphome, 0700);
     140                 :          7 :   g_assert_cmpint (res, ==, 0);
     141                 :            : 
     142                 :          7 :   name = g_build_filename (apphome, "myapp.desktop", NULL);
     143                 :          7 :   g_test_message ("creating '%s'", name);
     144                 :          7 :   g_file_set_contents (name, myapp_data, -1, &error);
     145                 :          7 :   g_assert_no_error (error);
     146                 :          7 :   g_free (name);
     147                 :            : 
     148                 :          7 :   name = g_build_filename (apphome, "myapp2.desktop", NULL);
     149                 :          7 :   g_test_message ("creating '%s'", name);
     150                 :          7 :   g_file_set_contents (name, myapp2_data, -1, &error);
     151                 :          7 :   g_assert_no_error (error);
     152                 :          7 :   g_free (name);
     153                 :            : 
     154                 :          7 :   name = g_build_filename (apphome, "myapp3.desktop", NULL);
     155                 :          7 :   g_test_message ("creating '%s'", name);
     156                 :          7 :   g_file_set_contents (name, myapp3_data, -1, &error);
     157                 :          7 :   g_assert_no_error (error);
     158                 :          7 :   g_free (name);
     159                 :            : 
     160                 :          7 :   name = g_build_filename (apphome, "myapp4.desktop", NULL);
     161                 :          7 :   g_test_message ("creating '%s'", name);
     162                 :          7 :   g_file_set_contents (name, myapp4_data, -1, &error);
     163                 :          7 :   g_assert_no_error (error);
     164                 :          7 :   g_free (name);
     165                 :            : 
     166                 :          7 :   name = g_build_filename (apphome, "myapp5.desktop", NULL);
     167                 :          7 :   g_test_message ("creating '%s'", name);
     168                 :          7 :   g_file_set_contents (name, myapp5_data, -1, &error);
     169                 :          7 :   g_assert_no_error (error);
     170                 :          7 :   g_free (name);
     171                 :            : 
     172                 :          7 :   name = g_build_filename (apphome, "nosuchapp.desktop", NULL);
     173                 :          7 :   g_test_message ("creating '%s'", name);
     174                 :          7 :   g_file_set_contents (name, nosuchapp_data, -1, &error);
     175                 :          7 :   g_assert_no_error (error);
     176                 :          7 :   g_free (name);
     177                 :            : 
     178                 :          7 :   mimeapps = g_build_filename (apphome, "mimeapps.list", NULL);
     179                 :          7 :   g_test_message ("removing '%s'", mimeapps);
     180                 :          7 :   g_remove (mimeapps);
     181                 :            : 
     182                 :          7 :   name = g_build_filename (apphome, "mimeinfo.cache", NULL);
     183                 :          7 :   g_test_message ("creating '%s'", name);
     184                 :          7 :   g_file_set_contents (name, mimecache_data, -1, &error);
     185                 :          7 :   g_assert_no_error (error);
     186                 :          7 :   g_free (name);
     187                 :            : 
     188                 :          7 :   g_free (apphome);
     189                 :          7 :   g_free (appdir);
     190                 :          7 :   g_free (mimeapps);
     191                 :            : 
     192                 :            :   /* Pointer to one of the temporary directories. */
     193                 :          7 :   fixture->mimeapps_list_home = g_build_filename (g_get_user_config_dir (), "mimeapps.list", NULL);
     194                 :          7 : }
     195                 :            : 
     196                 :            : static void
     197                 :          7 : teardown (Fixture       *fixture,
     198                 :            :           gconstpointer  test_data)
     199                 :            : {
     200                 :          7 :   g_free (fixture->mimeapps_list_home);
     201                 :          7 : }
     202                 :            : 
     203                 :            : static void
     204                 :          1 : test_mime_api (Fixture       *fixture,
     205                 :            :                gconstpointer  test_data)
     206                 :            : {
     207                 :            :   GAppInfo *appinfo;
     208                 :            :   GAppInfo *appinfo2;
     209                 :          1 :   GError *error = NULL;
     210                 :            :   GAppInfo *def;
     211                 :            :   GList *list;
     212                 :          1 :   const gchar *contenttype = "application/pdf";
     213                 :            : 
     214                 :            :   /* clear things out */
     215                 :          1 :   g_app_info_reset_type_associations (contenttype);
     216                 :            : 
     217                 :          1 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     218                 :          1 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     219                 :            : 
     220                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     221                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     222                 :          1 :   g_assert_null (def);
     223                 :          1 :   g_assert_null (list);
     224                 :            : 
     225                 :            :   /* 1. add a non-default association */
     226                 :          1 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     227                 :          1 :   g_assert_no_error (error);
     228                 :            : 
     229                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     230                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     231                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo));
     232                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 1);
     233                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     234                 :          1 :   g_object_unref (def);
     235                 :          1 :   g_list_free_full (list, g_object_unref);
     236                 :            : 
     237                 :            :   /* 2. add another non-default association */
     238                 :          1 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     239                 :          1 :   g_assert_no_error (error);
     240                 :            : 
     241                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     242                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     243                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo));
     244                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     245                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     246                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     247                 :          1 :   g_object_unref (def);
     248                 :          1 :   g_list_free_full (list, g_object_unref);
     249                 :            : 
     250                 :            :   /* 3. make the first app the default */
     251                 :          1 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     252                 :          1 :   g_assert_no_error (error);
     253                 :            : 
     254                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     255                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     256                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo));
     257                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     258                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     259                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     260                 :          1 :   g_object_unref (def);
     261                 :          1 :   g_list_free_full (list, g_object_unref);
     262                 :            : 
     263                 :            :   /* 4. make the second app the last used one */
     264                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
     265                 :          1 :   g_assert_no_error (error);
     266                 :            : 
     267                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     268                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     269                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo));
     270                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     271                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
     272                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
     273                 :          1 :   g_object_unref (def);
     274                 :          1 :   g_list_free_full (list, g_object_unref);
     275                 :            : 
     276                 :            :   /* 5. reset everything */
     277                 :          1 :   g_app_info_reset_type_associations (contenttype);
     278                 :            : 
     279                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     280                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     281                 :          1 :   g_assert_null (def);
     282                 :          1 :   g_assert_null (list);
     283                 :            : 
     284                 :          1 :   g_object_unref (appinfo);
     285                 :          1 :   g_object_unref (appinfo2);
     286                 :          1 : }
     287                 :            : 
     288                 :            : /* Repeat the same tests, this time checking that we handle
     289                 :            :  * mimeapps.list as expected. These tests are different from
     290                 :            :  * the ones in test_mime_api() in that we directly parse
     291                 :            :  * mimeapps.list to verify the results.
     292                 :            :  */
     293                 :            : static void
     294                 :          1 : test_mime_file (Fixture       *fixture,
     295                 :            :                 gconstpointer  test_data)
     296                 :            : {
     297                 :            :   gchar **assoc;
     298                 :            :   GAppInfo *appinfo;
     299                 :            :   GAppInfo *appinfo2;
     300                 :          1 :   GError *error = NULL;
     301                 :            :   GKeyFile *keyfile;
     302                 :            :   gchar *str;
     303                 :            :   gboolean res;
     304                 :            :   GAppInfo *def;
     305                 :            :   GList *list;
     306                 :          1 :   const gchar *contenttype = "application/pdf";
     307                 :            : 
     308                 :            :   /* clear things out */
     309                 :          1 :   g_app_info_reset_type_associations (contenttype);
     310                 :            : 
     311                 :          1 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     312                 :          1 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     313                 :            : 
     314                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     315                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     316                 :          1 :   g_assert_null (def);
     317                 :          1 :   g_assert_null (list);
     318                 :            : 
     319                 :            :   /* 1. add a non-default association */
     320                 :          1 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     321                 :          1 :   g_assert_no_error (error);
     322                 :            : 
     323                 :          1 :   keyfile = g_key_file_new ();
     324                 :          1 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     325                 :          1 :   g_assert_no_error (error);
     326                 :            : 
     327                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     328                 :          1 :   g_assert_no_error (error);
     329                 :          1 :   g_assert_true (strv_equal (assoc, "myapp.desktop", NULL));
     330                 :          1 :   g_strfreev (assoc);
     331                 :            : 
     332                 :            :   /* we've unset XDG_DATA_DIRS so there should be no default */
     333                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
     334                 :          1 :   g_assert_nonnull (error);
     335                 :          1 :   g_clear_error (&error);
     336                 :            : 
     337                 :          1 :   g_key_file_free (keyfile);
     338                 :            : 
     339                 :            :   /* 2. add another non-default association */
     340                 :          1 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     341                 :          1 :   g_assert_no_error (error);
     342                 :            : 
     343                 :          1 :   keyfile = g_key_file_new ();
     344                 :          1 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     345                 :          1 :   g_assert_no_error (error);
     346                 :            : 
     347                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     348                 :          1 :   g_assert_no_error (error);
     349                 :          1 :   g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
     350                 :          1 :   g_strfreev (assoc);
     351                 :            : 
     352                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
     353                 :          1 :   g_assert_nonnull (error);
     354                 :          1 :   g_clear_error (&error);
     355                 :            : 
     356                 :          1 :   g_key_file_free (keyfile);
     357                 :            : 
     358                 :            :   /* 3. make the first app the default */
     359                 :          1 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     360                 :          1 :   g_assert_no_error (error);
     361                 :            : 
     362                 :          1 :   keyfile = g_key_file_new ();
     363                 :          1 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     364                 :          1 :   g_assert_no_error (error);
     365                 :            : 
     366                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     367                 :          1 :   g_assert_no_error (error);
     368                 :          1 :   g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
     369                 :          1 :   g_strfreev (assoc);
     370                 :            : 
     371                 :          1 :   str = g_key_file_get_string (keyfile, "Default Applications", contenttype, &error);
     372                 :          1 :   g_assert_no_error (error);
     373                 :          1 :   g_assert_cmpstr (str, ==, "myapp.desktop");
     374                 :          1 :   g_free (str);
     375                 :            : 
     376                 :          1 :   g_key_file_free (keyfile);
     377                 :            : 
     378                 :            :   /* 4. make the second app the last used one */
     379                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
     380                 :          1 :   g_assert_no_error (error);
     381                 :            : 
     382                 :          1 :   keyfile = g_key_file_new ();
     383                 :          1 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     384                 :          1 :   g_assert_no_error (error);
     385                 :            : 
     386                 :          1 :   assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
     387                 :          1 :   g_assert_no_error (error);
     388                 :          1 :   g_assert_true (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
     389                 :          1 :   g_strfreev (assoc);
     390                 :            : 
     391                 :          1 :   g_key_file_free (keyfile);
     392                 :            : 
     393                 :            :   /* 5. reset everything */
     394                 :          1 :   g_app_info_reset_type_associations (contenttype);
     395                 :            : 
     396                 :          1 :   keyfile = g_key_file_new ();
     397                 :          1 :   g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
     398                 :          1 :   g_assert_no_error (error);
     399                 :            : 
     400                 :          1 :   res = g_key_file_has_key (keyfile, "Added Associations", contenttype, NULL);
     401                 :          1 :   g_assert_false (res);
     402                 :            : 
     403                 :          1 :   res = g_key_file_has_key (keyfile, "Default Applications", contenttype, NULL);
     404                 :          1 :   g_assert_false (res);
     405                 :            : 
     406                 :          1 :   g_key_file_free (keyfile);
     407                 :            : 
     408                 :          1 :   g_object_unref (appinfo);
     409                 :          1 :   g_object_unref (appinfo2);
     410                 :          1 : }
     411                 :            : 
     412                 :            : /* test interaction between mimeapps.list at different levels */
     413                 :            : static void
     414                 :          1 : test_mime_default (Fixture       *fixture,
     415                 :            :                    gconstpointer  test_data)
     416                 :            : {
     417                 :            :   GAppInfo *appinfo;
     418                 :            :   GAppInfo *appinfo2;
     419                 :            :   GAppInfo *appinfo3;
     420                 :          1 :   GError *error = NULL;
     421                 :            :   GAppInfo *def;
     422                 :            :   GList *list;
     423                 :          1 :   const gchar *contenttype = "image/png";
     424                 :            : 
     425                 :            :   /* clear things out */
     426                 :          1 :   g_app_info_reset_type_associations (contenttype);
     427                 :            : 
     428                 :          1 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
     429                 :          1 :   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
     430                 :          1 :   appinfo3 = (GAppInfo*)g_desktop_app_info_new ("myapp3.desktop");
     431                 :            : 
     432                 :            :   /* myapp3 is set as the default in defaults.list */
     433                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     434                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     435                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo3));
     436                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 1);
     437                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo3));
     438                 :          1 :   g_object_unref (def);
     439                 :          1 :   g_list_free_full (list, g_object_unref);
     440                 :            : 
     441                 :            :   /* 1. add a non-default association */
     442                 :          1 :   g_app_info_add_supports_type (appinfo, contenttype, &error);
     443                 :          1 :   g_assert_no_error (error);
     444                 :            : 
     445                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     446                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     447                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo3)); /* default is unaffected */
     448                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     449                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     450                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo3));
     451                 :          1 :   g_object_unref (def);
     452                 :          1 :   g_list_free_full (list, g_object_unref);
     453                 :            : 
     454                 :            :   /* 2. add another non-default association */
     455                 :          1 :   g_app_info_add_supports_type (appinfo2, contenttype, &error);
     456                 :          1 :   g_assert_no_error (error);
     457                 :            : 
     458                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     459                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     460                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo3));
     461                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 3);
     462                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     463                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     464                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
     465                 :          1 :   g_object_unref (def);
     466                 :          1 :   g_list_free_full (list, g_object_unref);
     467                 :            : 
     468                 :            :   /* 3. make the first app the default */
     469                 :          1 :   g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
     470                 :          1 :   g_assert_no_error (error);
     471                 :            : 
     472                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     473                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     474                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo));
     475                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 3);
     476                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
     477                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
     478                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
     479                 :          1 :   g_object_unref (def);
     480                 :          1 :   g_list_free_full (list, g_object_unref);
     481                 :            : 
     482                 :          1 :   g_object_unref (appinfo);
     483                 :          1 :   g_object_unref (appinfo2);
     484                 :          1 :   g_object_unref (appinfo3);
     485                 :          1 : }
     486                 :            : 
     487                 :            : /* test interaction between mimeinfo.cache, defaults.list and mimeapps.list
     488                 :            :  * to ensure g_app_info_set_as_last_used_for_type doesn't incorrectly
     489                 :            :  * change the default
     490                 :            :  */
     491                 :            : static void
     492                 :          1 : test_mime_default_last_used (Fixture       *fixture,
     493                 :            :                              gconstpointer  test_data)
     494                 :            : {
     495                 :            :   GAppInfo *appinfo4;
     496                 :            :   GAppInfo *appinfo5;
     497                 :          1 :   GError *error = NULL;
     498                 :            :   GAppInfo *def;
     499                 :            :   GList *list;
     500                 :          1 :   const gchar *contenttype = "image/bmp";
     501                 :            : 
     502                 :            :   /* clear things out */
     503                 :          1 :   g_app_info_reset_type_associations (contenttype);
     504                 :            : 
     505                 :          1 :   appinfo4 = (GAppInfo*)g_desktop_app_info_new ("myapp4.desktop");
     506                 :          1 :   appinfo5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
     507                 :            : 
     508                 :            :   /* myapp4 is set as the default in defaults.list */
     509                 :            :   /* myapp4 and myapp5 can both handle image/bmp */
     510                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     511                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     512                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo4));
     513                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     514                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     515                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     516                 :          1 :   g_object_unref (def);
     517                 :          1 :   g_list_free_full (list, g_object_unref);
     518                 :            : 
     519                 :            :   /* 1. set default (myapp4) as last used */
     520                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
     521                 :          1 :   g_assert_no_error (error);
     522                 :            : 
     523                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     524                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     525                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo4)); /* default is unaffected */
     526                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     527                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     528                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     529                 :          1 :   g_object_unref (def);
     530                 :          1 :   g_list_free_full (list, g_object_unref);
     531                 :            : 
     532                 :            :   /* 2. set other (myapp5) as last used */
     533                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
     534                 :          1 :   g_assert_no_error (error);
     535                 :            : 
     536                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     537                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     538                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo4));
     539                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     540                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     541                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     542                 :          1 :   g_object_unref (def);
     543                 :          1 :   g_list_free_full (list, g_object_unref);
     544                 :            : 
     545                 :            :   /* 3. change the default to myapp5 */
     546                 :          1 :   g_app_info_set_as_default_for_type (appinfo5, contenttype, &error);
     547                 :          1 :   g_assert_no_error (error);
     548                 :            : 
     549                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     550                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     551                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo5));
     552                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     553                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     554                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     555                 :          1 :   g_object_unref (def);
     556                 :          1 :   g_list_free_full (list, g_object_unref);
     557                 :            : 
     558                 :            :   /* 4. set myapp4 as last used */
     559                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
     560                 :          1 :   g_assert_no_error (error);
     561                 :            : 
     562                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     563                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     564                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo5));
     565                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     566                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
     567                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
     568                 :          1 :   g_object_unref (def);
     569                 :          1 :   g_list_free_full (list, g_object_unref);
     570                 :            : 
     571                 :            :   /* 5. set myapp5 as last used again */
     572                 :          1 :   g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
     573                 :          1 :   g_assert_no_error (error);
     574                 :            : 
     575                 :          1 :   def = g_app_info_get_default_for_type (contenttype, FALSE);
     576                 :          1 :   list = g_app_info_get_recommended_for_type (contenttype);
     577                 :          1 :   g_assert_true (g_app_info_equal (def, appinfo5));
     578                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     579                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
     580                 :          1 :   g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
     581                 :          1 :   g_object_unref (def);
     582                 :          1 :   g_list_free_full (list, g_object_unref);
     583                 :            : 
     584                 :          1 :   g_object_unref (appinfo4);
     585                 :          1 :   g_object_unref (appinfo5);
     586                 :          1 : }
     587                 :            : 
     588                 :            : static void
     589                 :          1 : test_scheme_handler (Fixture       *fixture,
     590                 :            :                      gconstpointer  test_data)
     591                 :            : {
     592                 :            :   GAppInfo *info, *info5;
     593                 :            : 
     594                 :          1 :   info5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
     595                 :          1 :   info = g_app_info_get_default_for_uri_scheme ("ftp");
     596                 :          1 :   g_assert_true (g_app_info_equal (info, info5));
     597                 :            : 
     598                 :          1 :   g_object_unref (info);
     599                 :          1 :   g_object_unref (info5);
     600                 :          1 : }
     601                 :            : 
     602                 :            : /* test that g_app_info_* ignores desktop files with nonexisting executables
     603                 :            :  */
     604                 :            : static void
     605                 :          1 : test_mime_ignore_nonexisting (Fixture       *fixture,
     606                 :            :                               gconstpointer  test_data)
     607                 :            : {
     608                 :            :   GAppInfo *appinfo;
     609                 :            : 
     610                 :          1 :   appinfo = (GAppInfo*)g_desktop_app_info_new ("nosuchapp.desktop");
     611                 :          1 :   g_assert_null (appinfo);
     612                 :          1 : }
     613                 :            : 
     614                 :            : static void
     615                 :          1 : test_all (Fixture       *fixture,
     616                 :            :           gconstpointer  test_data)
     617                 :            : {
     618                 :            :   GList *all, *l;
     619                 :            : 
     620                 :          1 :   all = g_app_info_get_all ();
     621                 :            : 
     622         [ +  + ]:          6 :   for (l = all; l; l = l->next)
     623                 :          5 :     g_assert_true (G_IS_APP_INFO (l->data));
     624                 :            : 
     625                 :          1 :   g_list_free_full (all, g_object_unref);
     626                 :          1 : }
     627                 :            : 
     628                 :            : int
     629                 :          1 : main (int argc, char *argv[])
     630                 :            : {
     631                 :          1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     632                 :            : 
     633                 :          1 :   g_test_add ("/appinfo/mime/api", Fixture, NULL, setup,
     634                 :            :               test_mime_api, teardown);
     635                 :          1 :   g_test_add ("/appinfo/mime/default", Fixture, NULL, setup,
     636                 :            :               test_mime_default, teardown);
     637                 :          1 :   g_test_add ("/appinfo/mime/file", Fixture, NULL, setup,
     638                 :            :               test_mime_file, teardown);
     639                 :          1 :   g_test_add ("/appinfo/mime/scheme-handler", Fixture, NULL, setup,
     640                 :            :               test_scheme_handler, teardown);
     641                 :          1 :   g_test_add ("/appinfo/mime/default-last-used", Fixture, NULL, setup,
     642                 :            :               test_mime_default_last_used, teardown);
     643                 :          1 :   g_test_add ("/appinfo/mime/ignore-nonexisting", Fixture, NULL, setup,
     644                 :            :               test_mime_ignore_nonexisting, teardown);
     645                 :          1 :   g_test_add ("/appinfo/all", Fixture, NULL, setup, test_all, teardown);
     646                 :            : 
     647                 :          1 :   return g_test_run ();
     648                 :            : }

Generated by: LCOV version 1.14