Add some tests for mimeapps.list handling
[platform/upstream/glib.git] / gio / tests / mimeapps.c
1 #include <glib/gstdio.h>
2 #include <gio/gio.h>
3 #include <gio/gdesktopappinfo.h>
4
5 static gboolean
6 strv_equal (gchar **strv, ...)
7 {
8   gint count;
9   va_list list;
10   const gchar *str;
11   gboolean res;
12
13   res = TRUE;
14   count = 0;
15   va_start (list, strv);
16   while (1)
17     {
18       str = va_arg (list, const gchar *);
19       if (str == NULL)
20         break;
21       if (g_strcmp0 (str, strv[count]) != 0)
22         {
23           res = FALSE;
24           break;
25         }
26       count++;
27     }
28   va_end (list);
29
30   if (res)
31     res = g_strv_length (strv) == count;
32
33   return res;
34 }
35
36
37 const gchar *myapp_data =
38   "[Desktop Entry]\n"
39   "Encoding=UTF-8\n"
40   "Version=1.0\n"
41   "Type=Application\n"
42   "Exec=my_app %f\n"
43   "Name=my app\n";
44
45 const gchar *myapp2_data =
46   "[Desktop Entry]\n"
47   "Encoding=UTF-8\n"
48   "Version=1.0\n"
49   "Type=Application\n"
50   "Exec=my_app2 %f\n"
51   "Name=my app 2\n";
52
53 /* Test that we handle mimeapps.list as expected.
54  * These tests are different from the ones in appinfo.c
55  * in that we directly parse mimeapps.list here
56  * to verify the results.
57  *
58  * We need to keep this test in a separate binary, since
59  * g_get_user_data_dir() doesn't get updated at runtime.
60  */
61 static void
62 test_mimeapps (void)
63 {
64   gchar *dir;
65   gchar *xdgdir;
66   gchar *appdir;
67   gchar *mimeapps;
68   gchar *name;
69   gchar **assoc;
70   GAppInfo *appinfo;
71   GAppInfo *appinfo2;
72   GError *error = NULL;
73   GKeyFile *keyfile;
74   gchar *str;
75   gboolean res;
76
77   dir = g_get_current_dir ();
78   xdgdir = g_build_filename (dir, "xdgdatahome", NULL);
79   g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdir);
80   g_setenv ("XDG_DATA_HOME", xdgdir, TRUE);
81   g_setenv ("XDG_DATA_DIRS", " ", TRUE);
82
83   appdir = g_build_filename (xdgdir, "applications", NULL);
84   g_test_message ("creating '%s'\n", appdir);
85   res = g_mkdir_with_parents (appdir, 0700);
86   g_assert (res == 0);
87
88   name = g_build_filename (appdir, "myapp.desktop", NULL);
89   g_test_message ("creating '%s'\n", name);
90   g_file_set_contents (name, myapp_data, -1, &error);
91   g_assert_no_error (error);
92   g_free (name);
93
94   name = g_build_filename (appdir, "myapp2.desktop", NULL);
95   g_test_message ("creating '%s'\n", name);
96   g_file_set_contents (name, myapp2_data, -1, &error);
97   g_assert_no_error (error);
98   g_free (name);
99
100   mimeapps = g_build_filename (appdir, "mimeapps.list", NULL);
101   g_test_message ("removing '%s'\n", mimeapps);
102   g_remove (mimeapps);
103
104   /* 1. add a non-default association */
105   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
106   g_app_info_add_supports_type (appinfo, "application/pdf", &error);
107   g_assert_no_error (error);
108
109   keyfile = g_key_file_new ();
110   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
111   g_assert_no_error (error);
112
113   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
114   g_assert_no_error (error);
115   g_assert (strv_equal (assoc, "myapp.desktop", NULL));
116   g_strfreev (assoc);
117
118   /* we've unset XDG_DATA_DIRS so there should be no default */
119   assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
120   g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
121   g_clear_error (&error);
122
123   g_key_file_free (keyfile);
124
125   /* 2. add another non-default association */
126   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
127   g_app_info_add_supports_type (appinfo2, "application/pdf", &error);
128   g_assert_no_error (error);
129
130   keyfile = g_key_file_new ();
131   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
132   g_assert_no_error (error);
133
134   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
135   g_assert_no_error (error);
136   g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
137   g_strfreev (assoc);
138
139   assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
140   g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
141   g_clear_error (&error);
142
143   g_key_file_free (keyfile);
144
145   /* 3. make the first app the default */
146   g_app_info_set_as_default_for_type (appinfo, "application/pdf", &error);
147   g_assert_no_error (error);
148
149   keyfile = g_key_file_new ();
150   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
151   g_assert_no_error (error);
152
153   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
154   g_assert_no_error (error);
155   g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
156   g_strfreev (assoc);
157
158   str = g_key_file_get_string (keyfile, "Default Applications", "application/pdf", &error);
159   g_assert_no_error (error);
160   g_assert_cmpstr (str, ==, "myapp.desktop");
161
162   g_key_file_free (keyfile);
163
164   /* 4. make the second app the last used one */
165   g_app_info_set_as_last_used_for_type (appinfo2, "application/pdf", &error);
166   g_assert_no_error (error);
167
168   keyfile = g_key_file_new ();
169   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
170   g_assert_no_error (error);
171
172   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
173   g_assert_no_error (error);
174   g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
175   g_strfreev (assoc);
176
177   g_key_file_free (keyfile);
178
179   /* 5. reset everything */
180   g_app_info_reset_type_associations ("application/pdf");
181
182   keyfile = g_key_file_new ();
183   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
184   g_assert_no_error (error);
185
186   res = g_key_file_has_key (keyfile, "Added Associations", "application/pdf", NULL);
187   g_assert (!res);
188
189   res = g_key_file_has_key (keyfile, "Default Applications", "application/pdf", NULL);
190   g_assert (!res);
191
192   g_key_file_free (keyfile);
193
194   g_object_unref (appinfo);
195   g_object_unref (appinfo2);
196
197   g_free (dir);
198   g_free (xdgdir);
199   g_free (mimeapps);
200   g_free (appdir);
201 }
202
203 int
204 main (int argc, char *argv[])
205 {
206   g_type_init ();
207   g_test_init (&argc, &argv, NULL);
208
209   g_test_add_func ("/appinfo/mimeapps", test_mimeapps);
210
211   return g_test_run ();
212 }