Also check api results
[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   GAppInfo *def;
77   GList *list;
78
79   dir = g_get_current_dir ();
80   xdgdir = g_build_filename (dir, "xdgdatahome", NULL);
81   g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdir);
82   g_setenv ("XDG_DATA_HOME", xdgdir, TRUE);
83   g_setenv ("XDG_DATA_DIRS", " ", TRUE);
84
85   appdir = g_build_filename (xdgdir, "applications", NULL);
86   g_test_message ("creating '%s'\n", appdir);
87   res = g_mkdir_with_parents (appdir, 0700);
88   g_assert (res == 0);
89
90   name = g_build_filename (appdir, "myapp.desktop", NULL);
91   g_test_message ("creating '%s'\n", name);
92   g_file_set_contents (name, myapp_data, -1, &error);
93   g_assert_no_error (error);
94   g_free (name);
95
96   name = g_build_filename (appdir, "myapp2.desktop", NULL);
97   g_test_message ("creating '%s'\n", name);
98   g_file_set_contents (name, myapp2_data, -1, &error);
99   g_assert_no_error (error);
100   g_free (name);
101
102   mimeapps = g_build_filename (appdir, "mimeapps.list", NULL);
103   g_test_message ("removing '%s'\n", mimeapps);
104   g_remove (mimeapps);
105
106   /* 1. add a non-default association */
107   appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
108   g_app_info_add_supports_type (appinfo, "application/pdf", &error);
109   g_assert_no_error (error);
110
111   /* check api results */
112   def = g_app_info_get_default_for_type ("application/pdf", FALSE);
113   list = g_app_info_get_recommended_for_type ("application/pdf");
114   g_assert (g_app_info_equal (def, appinfo));
115   g_assert_cmpint (g_list_length (list), ==, 1);
116   g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
117   g_object_unref (def);
118   g_list_free_full (list, g_object_unref);
119
120   /* check mimeapps.list */
121   keyfile = g_key_file_new ();
122   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
123   g_assert_no_error (error);
124
125   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
126   g_assert_no_error (error);
127   g_assert (strv_equal (assoc, "myapp.desktop", NULL));
128   g_strfreev (assoc);
129
130   /* we've unset XDG_DATA_DIRS so there should be no default */
131   assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
132   g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
133   g_clear_error (&error);
134
135   g_key_file_free (keyfile);
136
137   /* 2. add another non-default association */
138   appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
139   g_app_info_add_supports_type (appinfo2, "application/pdf", &error);
140   g_assert_no_error (error);
141
142   /* check api results */
143   def = g_app_info_get_default_for_type ("application/pdf", FALSE);
144   list = g_app_info_get_recommended_for_type ("application/pdf");
145   g_assert (g_app_info_equal (def, appinfo));
146   g_assert_cmpint (g_list_length (list), ==, 2);
147   g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
148   g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
149   g_object_unref (def);
150   g_list_free_full (list, g_object_unref);
151
152   /* check mimeapps.list */
153   keyfile = g_key_file_new ();
154   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
155   g_assert_no_error (error);
156
157   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
158   g_assert_no_error (error);
159   g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
160   g_strfreev (assoc);
161
162   assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
163   g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
164   g_clear_error (&error);
165
166   g_key_file_free (keyfile);
167
168   /* 3. make the first app the default */
169   g_app_info_set_as_default_for_type (appinfo, "application/pdf", &error);
170   g_assert_no_error (error);
171
172   /* check api results */
173   def = g_app_info_get_default_for_type ("application/pdf", FALSE);
174   list = g_app_info_get_recommended_for_type ("application/pdf");
175   g_assert (g_app_info_equal (def, appinfo));
176   g_assert_cmpint (g_list_length (list), ==, 2);
177   g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
178   g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
179   g_object_unref (def);
180   g_list_free_full (list, g_object_unref);
181
182   /* check mimeapps.list */
183   keyfile = g_key_file_new ();
184   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
185   g_assert_no_error (error);
186
187   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
188   g_assert_no_error (error);
189   g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
190   g_strfreev (assoc);
191
192   str = g_key_file_get_string (keyfile, "Default Applications", "application/pdf", &error);
193   g_assert_no_error (error);
194   g_assert_cmpstr (str, ==, "myapp.desktop");
195
196   g_key_file_free (keyfile);
197
198   /* 4. make the second app the last used one */
199   g_app_info_set_as_last_used_for_type (appinfo2, "application/pdf", &error);
200   g_assert_no_error (error);
201
202   /* check api results */
203   def = g_app_info_get_default_for_type ("application/pdf", FALSE);
204   list = g_app_info_get_recommended_for_type ("application/pdf");
205   g_assert (g_app_info_equal (def, appinfo));
206   g_assert_cmpint (g_list_length (list), ==, 2);
207   g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
208   g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
209   g_object_unref (def);
210   g_list_free_full (list, g_object_unref);
211
212   /* check mimeapps.list */
213   keyfile = g_key_file_new ();
214   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
215   g_assert_no_error (error);
216
217   assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
218   g_assert_no_error (error);
219   g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
220   g_strfreev (assoc);
221
222   g_key_file_free (keyfile);
223
224   /* 5. reset everything */
225   g_app_info_reset_type_associations ("application/pdf");
226
227   /* check api results */
228   def = g_app_info_get_default_for_type ("application/pdf", FALSE);
229   list = g_app_info_get_recommended_for_type ("application/pdf");
230   g_assert (def == NULL);
231   g_assert (list == NULL);
232
233   /* check mimeapps.list */
234   keyfile = g_key_file_new ();
235   g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
236   g_assert_no_error (error);
237
238   res = g_key_file_has_key (keyfile, "Added Associations", "application/pdf", NULL);
239   g_assert (!res);
240
241   res = g_key_file_has_key (keyfile, "Default Applications", "application/pdf", NULL);
242   g_assert (!res);
243
244   g_key_file_free (keyfile);
245
246   g_object_unref (appinfo);
247   g_object_unref (appinfo2);
248
249   g_free (dir);
250   g_free (xdgdir);
251   g_free (mimeapps);
252   g_free (appdir);
253 }
254
255 int
256 main (int argc, char *argv[])
257 {
258   g_type_init ();
259   g_test_init (&argc, &argv, NULL);
260
261   g_test_add_func ("/appinfo/mimeapps", test_mimeapps);
262
263   return g_test_run ();
264 }