Blame gio/tests/mimeapps.c

Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gio/gdesktopappinfo.h>
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
strv_equal (gchar **strv, ...)
Packit ae235b
{
Packit ae235b
  gint count;
Packit ae235b
  va_list list;
Packit ae235b
  const gchar *str;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  count = 0;
Packit ae235b
  va_start (list, strv);
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      str = va_arg (list, const gchar *);
Packit ae235b
      if (str == NULL)
Packit ae235b
        break;
Packit ae235b
      if (g_strcmp0 (str, strv[count]) != 0)
Packit ae235b
        {
Packit ae235b
          res = FALSE;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      count++;
Packit ae235b
    }
Packit ae235b
  va_end (list);
Packit ae235b
Packit ae235b
  if (res)
Packit ae235b
    res = g_strv_length (strv) == count;
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
const gchar *myapp_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=true %f\n"
Packit ae235b
  "Name=my app\n";
Packit ae235b
Packit ae235b
const gchar *myapp2_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=sleep %f\n"
Packit ae235b
  "Name=my app 2\n";
Packit ae235b
Packit ae235b
const gchar *myapp3_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=sleep 1\n"
Packit ae235b
  "Name=my app 3\n"
Packit ae235b
  "MimeType=image/png;";
Packit ae235b
Packit ae235b
const gchar *myapp4_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=echo %f\n"
Packit ae235b
  "Name=my app 4\n"
Packit ae235b
  "MimeType=image/bmp;";
Packit ae235b
Packit ae235b
const gchar *myapp5_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=true %f\n"
Packit ae235b
  "Name=my app 5\n"
Packit ae235b
  "MimeType=image/bmp;x-scheme-handler/ftp;";
Packit ae235b
Packit ae235b
const gchar *nosuchapp_data =
Packit ae235b
  "[Desktop Entry]\n"
Packit ae235b
  "Encoding=UTF-8\n"
Packit ae235b
  "Version=1.0\n"
Packit ae235b
  "Type=Application\n"
Packit ae235b
  "Exec=no_such_application %f\n"
Packit ae235b
  "Name=no such app\n";
Packit ae235b
Packit ae235b
const gchar *defaults_data =
Packit ae235b
  "[Default Applications]\n"
Packit ae235b
  "image/bmp=myapp4.desktop;\n"
Packit ae235b
  "image/png=myapp3.desktop;\n"
Packit ae235b
  "x-scheme-handler/ftp=myapp5.desktop;\n";
Packit ae235b
Packit ae235b
const gchar *mimecache_data =
Packit ae235b
  "[MIME Cache]\n"
Packit ae235b
  "image/bmp=myapp4.desktop;myapp5.desktop;\n"
Packit ae235b
  "image/png=myapp3.desktop;\n";
Packit ae235b
Packit ae235b
/* Set up XDG_DATA_HOME and XDG_DATA_DIRS.
Packit ae235b
 * XDG_DATA_DIRS/applications will contain mimeapps.list
Packit ae235b
 * XDG_DATA_HOME/applications will contain myapp.desktop
Packit ae235b
 * and myapp2.desktop, and no mimeapps.list
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
setup (void)
Packit ae235b
{
Packit ae235b
  gchar *dir;
Packit ae235b
  gchar *xdgconfighome;
Packit ae235b
  gchar *xdgdatahome;
Packit ae235b
  gchar *xdgdatadir;
Packit ae235b
  gchar *appdir;
Packit ae235b
  gchar *apphome;
Packit ae235b
  gchar *mimeapps;
Packit ae235b
  gchar *name;
Packit ae235b
  gboolean res;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  dir = g_get_current_dir ();
Packit ae235b
  xdgconfighome = g_build_filename (dir, "xdgconfighome", NULL);
Packit ae235b
  xdgdatahome = g_build_filename (dir, "xdgdatahome", NULL);
Packit ae235b
  xdgdatadir = g_build_filename (dir, "xdgdatadir", NULL);
Packit ae235b
  g_test_message ("setting XDG_CONFIG_HOME to '%s'\n", xdgconfighome);
Packit ae235b
  g_setenv ("XDG_CONFIG_HOME", xdgconfighome, TRUE);
Packit ae235b
  g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdatahome);
Packit ae235b
  g_setenv ("XDG_DATA_HOME", xdgdatahome, TRUE);
Packit ae235b
  g_test_message ("setting XDG_DATA_DIRS to '%s'\n", xdgdatadir);
Packit ae235b
  g_setenv ("XDG_DATA_DIRS", xdgdatadir, TRUE);
Packit ae235b
Packit ae235b
  appdir = g_build_filename (xdgdatadir, "applications", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", appdir);
Packit ae235b
  res = g_mkdir_with_parents (appdir, 0700);
Packit ae235b
  g_assert (res == 0);
Packit ae235b
Packit ae235b
  name = g_build_filename (appdir, "mimeapps.list", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, defaults_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  apphome = g_build_filename (xdgdatahome, "applications", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", apphome);
Packit ae235b
  res = g_mkdir_with_parents (apphome, 0700);
Packit ae235b
  g_assert (res == 0);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "myapp.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, myapp_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "myapp2.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, myapp2_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "myapp3.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, myapp3_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "myapp4.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, myapp4_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "myapp5.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, myapp5_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "nosuchapp.desktop", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, nosuchapp_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  mimeapps = g_build_filename (apphome, "mimeapps.list", NULL);
Packit ae235b
  g_test_message ("removing '%s'\n", mimeapps);
Packit ae235b
  g_remove (mimeapps);
Packit ae235b
Packit ae235b
  name = g_build_filename (apphome, "mimeinfo.cache", NULL);
Packit ae235b
  g_test_message ("creating '%s'\n", name);
Packit ae235b
  g_file_set_contents (name, mimecache_data, -1, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  g_free (dir);
Packit ae235b
  g_free (xdgconfighome);
Packit ae235b
  g_free (xdgdatahome);
Packit ae235b
  g_free (xdgdatadir);
Packit ae235b
  g_free (apphome);
Packit ae235b
  g_free (appdir);
Packit ae235b
  g_free (mimeapps);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_mime_api (void)
Packit ae235b
{
Packit ae235b
  GAppInfo *appinfo;
Packit ae235b
  GAppInfo *appinfo2;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GAppInfo *def;
Packit ae235b
  GList *list;
Packit ae235b
  const gchar *contenttype = "application/pdf";
Packit ae235b
Packit ae235b
  /* clear things out */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
Packit ae235b
  appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (def == NULL);
Packit ae235b
  g_assert (list == NULL);
Packit ae235b
Packit ae235b
  /* 1. add a non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 1);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 2. add another non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo2, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 3. make the first app the default */
Packit ae235b
  g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 4. make the second app the last used one */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 5. reset everything */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (def == NULL);
Packit ae235b
  g_assert (list == NULL);
Packit ae235b
Packit ae235b
  g_object_unref (appinfo);
Packit ae235b
  g_object_unref (appinfo2);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Repeat the same tests, this time checking that we handle
Packit ae235b
 * mimeapps.list as expected. These tests are different from
Packit ae235b
 * the ones in test_mime_api() in that we directly parse
Packit ae235b
 * mimeapps.list to verify the results.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
test_mime_file (void)
Packit ae235b
{
Packit ae235b
  gchar **assoc;
Packit ae235b
  GAppInfo *appinfo;
Packit ae235b
  GAppInfo *appinfo2;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GKeyFile *keyfile;
Packit ae235b
  gchar *str;
Packit ae235b
  gboolean res;
Packit ae235b
  GAppInfo *def;
Packit ae235b
  GList *list;
Packit ae235b
  gchar *mimeapps;
Packit ae235b
  gchar *dir;
Packit ae235b
  const gchar *contenttype = "application/pdf";
Packit ae235b
Packit ae235b
  dir = g_get_current_dir ();
Packit ae235b
  mimeapps = g_build_filename (dir, "xdgconfighome", "mimeapps.list", NULL);
Packit ae235b
Packit ae235b
  /* clear things out */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
Packit ae235b
  appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (def == NULL);
Packit ae235b
  g_assert (list == NULL);
Packit ae235b
Packit ae235b
  /* 1. add a non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  keyfile = g_key_file_new ();
Packit ae235b
  g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (strv_equal (assoc, "myapp.desktop", NULL));
Packit ae235b
  g_strfreev (assoc);
Packit ae235b
Packit ae235b
  /* we've unset XDG_DATA_DIRS so there should be no default */
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_clear_error (&error);
Packit ae235b
Packit ae235b
  g_key_file_free (keyfile);
Packit ae235b
Packit ae235b
  /* 2. add another non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo2, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  keyfile = g_key_file_new ();
Packit ae235b
  g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
Packit ae235b
  g_strfreev (assoc);
Packit ae235b
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
Packit ae235b
  g_assert (error != NULL);
Packit ae235b
  g_clear_error (&error);
Packit ae235b
Packit ae235b
  g_key_file_free (keyfile);
Packit ae235b
Packit ae235b
  /* 3. make the first app the default */
Packit ae235b
  g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  keyfile = g_key_file_new ();
Packit ae235b
  g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
Packit ae235b
  g_strfreev (assoc);
Packit ae235b
Packit ae235b
  str = g_key_file_get_string (keyfile, "Default Applications", contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpstr (str, ==, "myapp.desktop");
Packit ae235b
  g_free (str);
Packit ae235b
Packit ae235b
  g_key_file_free (keyfile);
Packit ae235b
Packit ae235b
  /* 4. make the second app the last used one */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  keyfile = g_key_file_new ();
Packit ae235b
  g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
Packit ae235b
  g_strfreev (assoc);
Packit ae235b
Packit ae235b
  g_key_file_free (keyfile);
Packit ae235b
Packit ae235b
  /* 5. reset everything */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  keyfile = g_key_file_new ();
Packit ae235b
  g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  res = g_key_file_has_key (keyfile, "Added Associations", contenttype, NULL);
Packit ae235b
  g_assert (!res);
Packit ae235b
Packit ae235b
  res = g_key_file_has_key (keyfile, "Default Applications", contenttype, NULL);
Packit ae235b
  g_assert (!res);
Packit ae235b
Packit ae235b
  g_key_file_free (keyfile);
Packit ae235b
Packit ae235b
  g_object_unref (appinfo);
Packit ae235b
  g_object_unref (appinfo2);
Packit ae235b
Packit ae235b
  g_free (mimeapps);
Packit ae235b
  g_free (dir);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* test interaction between mimeapps.list at different levels */
Packit ae235b
static void
Packit ae235b
test_mime_default (void)
Packit ae235b
{
Packit ae235b
  GAppInfo *appinfo;
Packit ae235b
  GAppInfo *appinfo2;
Packit ae235b
  GAppInfo *appinfo3;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GAppInfo *def;
Packit ae235b
  GList *list;
Packit ae235b
  const gchar *contenttype = "image/png";
Packit ae235b
Packit ae235b
  /* clear things out */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
Packit ae235b
  appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
Packit ae235b
  appinfo3 = (GAppInfo*)g_desktop_app_info_new ("myapp3.desktop");
Packit ae235b
Packit ae235b
  /* myapp3 is set as the default in defaults.list */
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo3));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 1);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo3));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 1. add a non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo3)); /* default is unaffected */
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo3));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 2. add another non-default association */
Packit ae235b
  g_app_info_add_supports_type (appinfo2, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo3));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 3);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 3. make the first app the default */
Packit ae235b
  g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 3);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  g_object_unref (appinfo);
Packit ae235b
  g_object_unref (appinfo2);
Packit ae235b
  g_object_unref (appinfo3);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* test interaction between mimeinfo.cache, defaults.list and mimeapps.list
Packit ae235b
 * to ensure g_app_info_set_as_last_used_for_type doesn't incorrectly
Packit ae235b
 * change the default
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
test_mime_default_last_used (void)
Packit ae235b
{
Packit ae235b
  GAppInfo *appinfo4;
Packit ae235b
  GAppInfo *appinfo5;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GAppInfo *def;
Packit ae235b
  GList *list;
Packit ae235b
  const gchar *contenttype = "image/bmp";
Packit ae235b
Packit ae235b
  /* clear things out */
Packit ae235b
  g_app_info_reset_type_associations (contenttype);
Packit ae235b
Packit ae235b
  appinfo4 = (GAppInfo*)g_desktop_app_info_new ("myapp4.desktop");
Packit ae235b
  appinfo5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
Packit ae235b
Packit ae235b
  /* myapp4 is set as the default in defaults.list */
Packit ae235b
  /* myapp4 and myapp5 can both handle image/bmp */
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo4));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 1. set default (myapp4) as last used */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo4)); /* default is unaffected */
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 2. set other (myapp5) as last used */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo4));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 3. change the default to myapp5 */
Packit ae235b
  g_app_info_set_as_default_for_type (appinfo5, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo5));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 4. set myapp4 as last used */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo5));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  /* 5. set myapp5 as last used again */
Packit ae235b
  g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  def = g_app_info_get_default_for_type (contenttype, FALSE);
Packit ae235b
  list = g_app_info_get_recommended_for_type (contenttype);
Packit ae235b
  g_assert (g_app_info_equal (def, appinfo5));
Packit ae235b
  g_assert_cmpint (g_list_length (list), ==, 2);
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
Packit ae235b
  g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
Packit ae235b
  g_object_unref (def);
Packit ae235b
  g_list_free_full (list, g_object_unref);
Packit ae235b
Packit ae235b
  g_object_unref (appinfo4);
Packit ae235b
  g_object_unref (appinfo5);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_scheme_handler (void)
Packit ae235b
{
Packit ae235b
  GAppInfo *info, *info5;
Packit ae235b
Packit ae235b
  info5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
Packit ae235b
  info = g_app_info_get_default_for_uri_scheme ("ftp");
Packit ae235b
  g_assert (g_app_info_equal (info, info5));
Packit ae235b
Packit ae235b
  g_object_unref (info);
Packit ae235b
  g_object_unref (info5);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* test that g_app_info_* ignores desktop files with nonexisting executables
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
test_mime_ignore_nonexisting (void)
Packit ae235b
{
Packit ae235b
  GAppInfo *appinfo;
Packit ae235b
Packit ae235b
  appinfo = (GAppInfo*)g_desktop_app_info_new ("nosuchapp.desktop");
Packit ae235b
  g_assert (appinfo == NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_all (void)
Packit ae235b
{
Packit ae235b
  GList *all, *l;
Packit ae235b
Packit ae235b
  all = g_app_info_get_all ();
Packit ae235b
Packit ae235b
  for (l = all; l; l = l->next)
Packit ae235b
    g_assert (G_IS_APP_INFO (l->data));
Packit ae235b
Packit ae235b
  g_list_free_full (all, g_object_unref);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  setup ();
Packit ae235b
Packit ae235b
  g_test_add_func ("/appinfo/mime/api", test_mime_api);
Packit ae235b
  g_test_add_func ("/appinfo/mime/default", test_mime_default);
Packit ae235b
  g_test_add_func ("/appinfo/mime/file", test_mime_file);
Packit ae235b
  g_test_add_func ("/appinfo/mime/scheme-handler", test_scheme_handler);
Packit ae235b
  g_test_add_func ("/appinfo/mime/default-last-used", test_mime_default_last_used);
Packit ae235b
  g_test_add_func ("/appinfo/mime/ignore-nonexisting", test_mime_ignore_nonexisting);
Packit ae235b
  g_test_add_func ("/appinfo/all", test_all);
Packit ae235b
Packit ae235b
  return g_test_run ();
Packit ae235b
}