From: Matthias Clasen Date: Mon, 12 Sep 2011 05:46:48 +0000 (-0400) Subject: Expand mimeapps test X-Git-Tag: 2.31.0~524 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=73c427fab9c61f523c57dff2107c959cb5d86751;p=platform%2Fupstream%2Fglib.git Expand mimeapps test This adds a test involving both defaults.list and mimeapps.list. --- diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am index 1df90c7..f1689b5 100644 --- a/gio/tests/Makefile.am +++ b/gio/tests/Makefile.am @@ -533,4 +533,4 @@ DISTCLEANFILES = \ gschemas.compiled distclean-local: - rm -r xdgdatahome + rm -r xdgdatahome xdgdatadir diff --git a/gio/tests/mimeapps.c b/gio/tests/mimeapps.c index e1cb21f..b364947 100644 --- a/gio/tests/mimeapps.c +++ b/gio/tests/mimeapps.c @@ -33,7 +33,6 @@ strv_equal (gchar **strv, ...) return res; } - const gchar *myapp_data = "[Desktop Entry]\n" "Encoding=UTF-8\n" @@ -50,171 +49,276 @@ const gchar *myapp2_data = "Exec=my_app2 %f\n" "Name=my app 2\n"; -/* Test that we handle mimeapps.list as expected. - * These tests are different from the ones in appinfo.c - * in that we directly parse mimeapps.list here - * to verify the results. - * - * We need to keep this test in a separate binary, since - * g_get_user_data_dir() doesn't get updated at runtime. +const gchar *myapp3_data = + "[Desktop Entry]\n" + "Encoding=UTF-8\n" + "Version=1.0\n" + "Type=Application\n" + "Exec=my_app3 %f\n" + "Name=my app 3\n"; + +const gchar *defaults_data = + "[Default Applications]\n" + "image/png=myapp3.desktop;\n"; + +/* Set up XDG_DATA_HOME and XDG_DATA_DIRS. + * XDG_DATA_DIRS/applications will contain defaults.list + * XDG_DATA_HOME/applications will contain myapp.desktop + * and myapp2.desktop, and no mimeapps.list */ static void -test_mimeapps (void) +setup (void) { gchar *dir; - gchar *xdgdir; + gchar *xdgdatahome; + gchar *xdgdatadir; gchar *appdir; + gchar *apphome; gchar *mimeapps; gchar *name; - gchar **assoc; - GAppInfo *appinfo; - GAppInfo *appinfo2; - GError *error = NULL; - GKeyFile *keyfile; - gchar *str; gboolean res; - GAppInfo *def; - GList *list; + GError *error = NULL; dir = g_get_current_dir (); - xdgdir = g_build_filename (dir, "xdgdatahome", NULL); - g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdir); - g_setenv ("XDG_DATA_HOME", xdgdir, TRUE); - g_setenv ("XDG_DATA_DIRS", " ", TRUE); - - appdir = g_build_filename (xdgdir, "applications", NULL); + xdgdatahome = g_build_filename (dir, "xdgdatahome", NULL); + xdgdatadir = g_build_filename (dir, "xdgdatadir", NULL); + g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdatahome); + g_setenv ("XDG_DATA_HOME", xdgdatahome, TRUE); + g_test_message ("setting XDG_DATA_DIRS to '%s'\n", xdgdatadir); + g_setenv ("XDG_DATA_DIRS", xdgdatadir, TRUE); + + appdir = g_build_filename (xdgdatadir, "applications", NULL); g_test_message ("creating '%s'\n", appdir); res = g_mkdir_with_parents (appdir, 0700); g_assert (res == 0); - name = g_build_filename (appdir, "myapp.desktop", NULL); + name = g_build_filename (appdir, "defaults.list", NULL); + g_test_message ("creating '%s'\n", name); + g_file_set_contents (name, defaults_data, -1, &error); + g_assert_no_error (error); + g_free (name); + + apphome = g_build_filename (xdgdatahome, "applications", NULL); + g_test_message ("creating '%s'\n", apphome); + res = g_mkdir_with_parents (apphome, 0700); + g_assert (res == 0); + + name = g_build_filename (apphome, "myapp.desktop", NULL); g_test_message ("creating '%s'\n", name); g_file_set_contents (name, myapp_data, -1, &error); g_assert_no_error (error); g_free (name); - name = g_build_filename (appdir, "myapp2.desktop", NULL); + name = g_build_filename (apphome, "myapp2.desktop", NULL); g_test_message ("creating '%s'\n", name); g_file_set_contents (name, myapp2_data, -1, &error); g_assert_no_error (error); g_free (name); - mimeapps = g_build_filename (appdir, "mimeapps.list", NULL); + name = g_build_filename (apphome, "myapp3.desktop", NULL); + g_test_message ("creating '%s'\n", name); + g_file_set_contents (name, myapp3_data, -1, &error); + g_assert_no_error (error); + g_free (name); + + mimeapps = g_build_filename (apphome, "mimeapps.list", NULL); g_test_message ("removing '%s'\n", mimeapps); g_remove (mimeapps); - /* 1. add a non-default association */ + g_free (dir); + g_free (xdgdatahome); + g_free (xdgdatadir); + g_free (apphome); + g_free (appdir); + g_free (mimeapps); +} + +static void +test_mime_api (void) +{ + GAppInfo *appinfo; + GAppInfo *appinfo2; + GError *error = NULL; + GAppInfo *def; + GList *list; + const gchar *contenttype = "application/pdf"; + + /* clear things out */ + g_app_info_reset_type_associations (contenttype); + appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop"); - g_app_info_add_supports_type (appinfo, "application/pdf", &error); + appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop"); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (def == NULL); + g_assert (list == NULL); + + /* 1. add a non-default association */ + g_app_info_add_supports_type (appinfo, contenttype, &error); g_assert_no_error (error); - /* check api results */ - def = g_app_info_get_default_for_type ("application/pdf", FALSE); - list = g_app_info_get_recommended_for_type ("application/pdf"); + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); g_assert (g_app_info_equal (def, appinfo)); g_assert_cmpint (g_list_length (list), ==, 1); g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); g_object_unref (def); g_list_free_full (list, g_object_unref); - /* check mimeapps.list */ + /* 2. add another non-default association */ + g_app_info_add_supports_type (appinfo2, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo)); + g_assert_cmpint (g_list_length (list), ==, 2); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 3. make the first app the default */ + g_app_info_set_as_default_for_type (appinfo, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo)); + g_assert_cmpint (g_list_length (list), ==, 2); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 4. make the second app the last used one */ + g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo)); + g_assert_cmpint (g_list_length (list), ==, 2); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo2)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 5. reset everything */ + g_app_info_reset_type_associations (contenttype); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (def == NULL); + g_assert (list == NULL); + + g_object_unref (appinfo); + g_object_unref (appinfo2); +} + +/* Repeat the same tests, this time checking that we handle + * mimeapps.list as expected. These tests are different from + * the ones in test_mime_api() in that we directly parse + * mimeapps.list to verify the results. + */ +static void +test_mime_file (void) +{ + gchar **assoc; + GAppInfo *appinfo; + GAppInfo *appinfo2; + GError *error = NULL; + GKeyFile *keyfile; + gchar *str; + gboolean res; + GAppInfo *def; + GList *list; + gchar *mimeapps; + gchar *dir; + const gchar *contenttype = "application/pdf"; + + dir = g_get_current_dir (); + mimeapps = g_build_filename (dir, "xdgdatahome", "applications", "mimeapps.list", NULL); + + /* clear things out */ + g_app_info_reset_type_associations (contenttype); + + appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop"); + appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop"); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (def == NULL); + g_assert (list == NULL); + + /* 1. add a non-default association */ + g_app_info_add_supports_type (appinfo, contenttype, &error); + g_assert_no_error (error); + keyfile = g_key_file_new (); g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error); g_assert_no_error (error); - assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error); + assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error); g_assert_no_error (error); g_assert (strv_equal (assoc, "myapp.desktop", NULL)); g_strfreev (assoc); /* we've unset XDG_DATA_DIRS so there should be no default */ - assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error); - g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND); + assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error); + g_assert (error != NULL); g_clear_error (&error); g_key_file_free (keyfile); /* 2. add another non-default association */ - appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop"); - g_app_info_add_supports_type (appinfo2, "application/pdf", &error); + g_app_info_add_supports_type (appinfo2, contenttype, &error); g_assert_no_error (error); - /* check api results */ - def = g_app_info_get_default_for_type ("application/pdf", FALSE); - list = g_app_info_get_recommended_for_type ("application/pdf"); - g_assert (g_app_info_equal (def, appinfo)); - g_assert_cmpint (g_list_length (list), ==, 2); - g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); - g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); - g_object_unref (def); - g_list_free_full (list, g_object_unref); - - /* check mimeapps.list */ keyfile = g_key_file_new (); g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error); g_assert_no_error (error); - assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error); + assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error); g_assert_no_error (error); g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL)); g_strfreev (assoc); - assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error); - g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND); + assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error); + g_assert (error != NULL); g_clear_error (&error); g_key_file_free (keyfile); /* 3. make the first app the default */ - g_app_info_set_as_default_for_type (appinfo, "application/pdf", &error); + g_app_info_set_as_default_for_type (appinfo, contenttype, &error); g_assert_no_error (error); - /* check api results */ - def = g_app_info_get_default_for_type ("application/pdf", FALSE); - list = g_app_info_get_recommended_for_type ("application/pdf"); - g_assert (g_app_info_equal (def, appinfo)); - g_assert_cmpint (g_list_length (list), ==, 2); - g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); - g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); - g_object_unref (def); - g_list_free_full (list, g_object_unref); - - /* check mimeapps.list */ keyfile = g_key_file_new (); g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error); g_assert_no_error (error); - assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error); + assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error); g_assert_no_error (error); g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL)); g_strfreev (assoc); - str = g_key_file_get_string (keyfile, "Default Applications", "application/pdf", &error); + str = g_key_file_get_string (keyfile, "Default Applications", contenttype, &error); g_assert_no_error (error); g_assert_cmpstr (str, ==, "myapp.desktop"); g_key_file_free (keyfile); /* 4. make the second app the last used one */ - g_app_info_set_as_last_used_for_type (appinfo2, "application/pdf", &error); + g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error); g_assert_no_error (error); - /* check api results */ - def = g_app_info_get_default_for_type ("application/pdf", FALSE); - list = g_app_info_get_recommended_for_type ("application/pdf"); - g_assert (g_app_info_equal (def, appinfo)); - g_assert_cmpint (g_list_length (list), ==, 2); - g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo2)); - g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo)); - g_object_unref (def); - g_list_free_full (list, g_object_unref); - - /* check mimeapps.list */ keyfile = g_key_file_new (); g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error); g_assert_no_error (error); - assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error); + assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error); g_assert_no_error (error); g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL)); g_strfreev (assoc); @@ -222,23 +326,16 @@ test_mimeapps (void) g_key_file_free (keyfile); /* 5. reset everything */ - g_app_info_reset_type_associations ("application/pdf"); + g_app_info_reset_type_associations (contenttype); - /* check api results */ - def = g_app_info_get_default_for_type ("application/pdf", FALSE); - list = g_app_info_get_recommended_for_type ("application/pdf"); - g_assert (def == NULL); - g_assert (list == NULL); - - /* check mimeapps.list */ keyfile = g_key_file_new (); g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error); g_assert_no_error (error); - res = g_key_file_has_key (keyfile, "Added Associations", "application/pdf", NULL); + res = g_key_file_has_key (keyfile, "Added Associations", contenttype, NULL); g_assert (!res); - res = g_key_file_has_key (keyfile, "Default Applications", "application/pdf", NULL); + res = g_key_file_has_key (keyfile, "Default Applications", contenttype, NULL); g_assert (!res); g_key_file_free (keyfile); @@ -246,10 +343,82 @@ test_mimeapps (void) g_object_unref (appinfo); g_object_unref (appinfo2); - g_free (dir); - g_free (xdgdir); g_free (mimeapps); - g_free (appdir); + g_free (dir); +} + +/* test interaction between defaults.list and mimeapps.list */ +static void +test_mime_default (void) +{ + GAppInfo *appinfo; + GAppInfo *appinfo2; + GAppInfo *appinfo3; + GError *error = NULL; + GAppInfo *def; + GList *list; + const gchar *contenttype = "image/png"; + + /* clear things out */ + g_app_info_reset_type_associations (contenttype); + + appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop"); + appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop"); + appinfo3 = (GAppInfo*)g_desktop_app_info_new ("myapp3.desktop"); + + /* myapp3 is set as the default in defaults.list */ + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo3)); + g_assert_cmpint (g_list_length (list), ==, 1); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo3)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 1. add a non-default association */ + g_app_info_add_supports_type (appinfo, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo3)); /* default is unaffected */ + g_assert_cmpint (g_list_length (list), ==, 2); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo3)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 2. add another non-default association */ + g_app_info_add_supports_type (appinfo2, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo3)); + g_assert_cmpint (g_list_length (list), ==, 3); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + /* 3. make the first app the default */ + g_app_info_set_as_default_for_type (appinfo, contenttype, &error); + g_assert_no_error (error); + + def = g_app_info_get_default_for_type (contenttype, FALSE); + list = g_app_info_get_recommended_for_type (contenttype); + g_assert (g_app_info_equal (def, appinfo)); + g_assert_cmpint (g_list_length (list), ==, 3); + g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2)); + g_assert (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3)); + g_object_unref (def); + g_list_free_full (list, g_object_unref); + + g_object_unref (appinfo); + g_object_unref (appinfo2); + g_object_unref (appinfo3); } int @@ -258,7 +427,11 @@ main (int argc, char *argv[]) g_type_init (); g_test_init (&argc, &argv, NULL); - g_test_add_func ("/appinfo/mimeapps", test_mimeapps); + setup (); + + g_test_add_func ("/appinfo/mime/api", test_mime_api); + g_test_add_func ("/appinfo/mime/default", test_mime_default); + g_test_add_func ("/appinfo/mime/file", test_mime_file); return g_test_run (); }