80ce6a24d804a797aa9a28ee68473e2841b98c91
[platform/upstream/glib.git] / gio / tests / contenttype.c
1 #include <gio/gio.h>
2 #include <string.h>
3
4 #define g_assert_content_type_equals(s1, s2)                    \
5   do {                                                          \
6     const char *__s1 = (s1), *__s2 = (s2);                      \
7     if (g_content_type_equals (__s1, __s2)) ;                   \
8     else                                                        \
9       g_assertion_message_cmpstr (G_LOG_DOMAIN,                 \
10                                   __FILE__, __LINE__,           \
11                                   G_STRFUNC,                    \
12                                   #s1 " == " #s2,               \
13                                   __s1, " == ", __s2);          \
14   } while (0)
15
16 static void
17 test_guess (void)
18 {
19   gchar *res;
20   gchar *expected;
21   gboolean uncertain;
22   guchar data[] =
23     "[Desktop Entry]\n"
24     "Type=Application\n"
25     "Name=appinfo-test\n"
26     "Exec=./appinfo-test --option\n";
27
28   res = g_content_type_guess ("/etc/", NULL, 0, &uncertain);
29   expected = g_content_type_from_mime_type ("inode/directory");
30   g_assert_content_type_equals (expected, res);
31   g_assert (uncertain);
32   g_free (res);
33   g_free (expected);
34
35   res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
36   expected = g_content_type_from_mime_type ("text/plain");
37   g_assert_content_type_equals (expected, res);
38   g_free (res);
39   g_free (expected);
40
41   res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
42   expected = g_content_type_from_mime_type ("application/x-desktop");
43   g_assert_content_type_equals (expected, res);
44   g_assert (!uncertain);
45   g_free (res);
46   g_free (expected);
47
48   res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain);
49   expected = g_content_type_from_mime_type ("text/plain");
50   g_assert_content_type_equals (expected, res);
51   g_assert (!uncertain);
52   g_free (res);
53   g_free (expected);
54
55   res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
56   expected = g_content_type_from_mime_type ("text/plain");
57   g_assert_content_type_equals (expected, res);
58   g_assert (!uncertain);
59   g_free (res);
60   g_free (expected);
61
62   res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
63   expected = g_content_type_from_mime_type ("application/x-desktop");
64   g_assert_content_type_equals (expected, res);
65   g_assert (!uncertain);
66   g_free (res);
67   g_free (expected);
68
69   /* this is potentially ambiguous: it does not match the PO template format,
70    * but looks like text so it can't be Powerpoint */
71   res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
72   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
73   g_assert_content_type_equals (expected, res);
74   g_assert (!uncertain);
75   g_free (res);
76   g_free (expected);
77
78   res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
79   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
80   g_assert_content_type_equals (expected, res);
81   g_assert (!uncertain);
82   g_free (res);
83   g_free (expected);
84
85   res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
86   expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
87   g_assert_content_type_equals (expected, res);
88   /* we cannot reliably detect binary powerpoint files as long as there is no
89    * defined MIME magic, so do not check uncertain here
90    */
91   g_free (res);
92   g_free (expected);
93
94   res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
95   expected = g_content_type_from_mime_type ("application/x-font-otf");
96   g_assert_content_type_equals (expected, res);
97   g_assert (!uncertain);
98   g_free (res);
99   g_free (expected);
100
101   res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
102   expected = g_content_type_from_mime_type ("image/x-eps");
103   g_assert_content_type_equals (expected, res);
104   g_assert (!uncertain);
105   g_free (res);
106   g_free (expected);
107 }
108
109 static void
110 test_unknown (void)
111 {
112   gchar *unknown;
113   gchar *str;
114
115   unknown = g_content_type_from_mime_type ("application/octet-stream");
116   g_assert (g_content_type_is_unknown (unknown));
117   str = g_content_type_get_mime_type (unknown);
118   g_assert_cmpstr (str, ==, "application/octet-stream");
119   g_free (str);
120   g_free (unknown);
121 }
122
123 static void
124 test_subtype (void)
125 {
126   gchar *plain;
127   gchar *xml;
128
129   plain = g_content_type_from_mime_type ("text/plain");
130   xml = g_content_type_from_mime_type ("application/xml");
131
132   g_assert (g_content_type_is_a (xml, plain));
133
134   g_free (plain);
135   g_free (xml);
136 }
137
138 static gint
139 find_mime (gconstpointer a, gconstpointer b)
140 {
141   if (g_content_type_equals (a, b))
142     return 0;
143   return 1;
144 }
145
146 static void
147 test_list (void)
148 {
149   GList *types;
150   gchar *plain;
151   gchar *xml;
152
153   plain = g_content_type_from_mime_type ("text/plain");
154   xml = g_content_type_from_mime_type ("application/xml");
155
156   types = g_content_types_get_registered ();
157
158   g_assert (g_list_length (types) > 1);
159
160   /* just check that some types are in the list */
161   g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
162   g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
163
164   g_list_free_full (types, g_free);
165
166   g_free (plain);
167   g_free (xml);
168 }
169
170 static void
171 test_executable (void)
172 {
173   gchar *type;
174
175   type = g_content_type_from_mime_type ("application/x-executable");
176   g_assert (g_content_type_can_be_executable (type));
177   g_free (type);
178
179   type = g_content_type_from_mime_type ("text/plain");
180   g_assert (g_content_type_can_be_executable (type));
181   g_free (type);
182
183   type = g_content_type_from_mime_type ("image/png");
184   g_assert (!g_content_type_can_be_executable (type));
185   g_free (type);
186 }
187
188 static void
189 test_description (void)
190 {
191   gchar *type;
192   gchar *desc;
193
194   type = g_content_type_from_mime_type ("text/plain");
195   desc = g_content_type_get_description (type);
196   g_assert (desc != NULL);
197
198   g_free (desc);
199   g_free (type);
200 }
201
202 static gboolean
203 strv_contains (const gchar * const *strv,
204                const gchar  *s)
205 {
206   gint i;
207
208   for (i = 0; strv[i]; i++)
209     {
210       if (g_strcmp0 (strv[i], s) == 0)
211         return TRUE;
212     }
213
214   return FALSE;
215 }
216
217 static void
218 test_icon (void)
219 {
220   gchar *type;
221   GIcon *icon;
222
223   type = g_content_type_from_mime_type ("text/plain");
224   icon = g_content_type_get_icon (type);
225   g_assert (G_IS_ICON (icon));
226   if (G_IS_THEMED_ICON (icon))
227     {
228       const gchar *const *names;
229
230       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
231       g_assert (strv_contains (names, "text-plain"));
232       g_assert (strv_contains (names, "text-x-generic"));
233     }
234   g_object_unref (icon);
235   g_free (type);
236
237   type = g_content_type_from_mime_type ("application/rtf");
238   icon = g_content_type_get_icon (type);
239   g_assert (G_IS_ICON (icon));
240   if (G_IS_THEMED_ICON (icon))
241     {
242       const gchar *const *names;
243
244       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
245       g_assert (strv_contains (names, "application-rtf"));
246       g_assert (strv_contains (names, "x-office-document"));
247     }
248   g_object_unref (icon);
249   g_free (type);
250 }
251
252 static void
253 test_symbolic_icon (void)
254 {
255   gchar *type;
256   GIcon *icon;
257
258   type = g_content_type_from_mime_type ("text/plain");
259   icon = g_content_type_get_symbolic_icon (type);
260   g_assert (G_IS_ICON (icon));
261   if (G_IS_THEMED_ICON (icon))
262     {
263       const gchar *const *names;
264
265       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
266       g_assert (strv_contains (names, "text-plain-symbolic"));
267       g_assert (strv_contains (names, "text-x-generic-symbolic"));
268       g_assert (strv_contains (names, "text-plain"));
269       g_assert (strv_contains (names, "text-x-generic"));
270     }
271   g_object_unref (icon);
272   g_free (type);
273
274   type = g_content_type_from_mime_type ("application/rtf");
275   icon = g_content_type_get_symbolic_icon (type);
276   g_assert (G_IS_ICON (icon));
277   if (G_IS_THEMED_ICON (icon))
278     {
279       const gchar *const *names;
280
281       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
282       g_assert (strv_contains (names, "application-rtf-symbolic"));
283       g_assert (strv_contains (names, "x-office-document-symbolic"));
284       g_assert (strv_contains (names, "application-rtf"));
285       g_assert (strv_contains (names, "x-office-document"));
286     }
287   g_object_unref (icon);
288   g_free (type);
289 }
290
291 static void
292 test_tree (void)
293 {
294   const gchar *tests[] = {
295     "x-content/image-dcf",
296     "x-content/unix-software",
297     "x-content/win32-software"
298   };
299   const gchar *path;
300   GFile *file;
301   gchar **types;
302   gint i;
303
304   for (i = 0; i < G_N_ELEMENTS (tests); i++)
305     {
306       path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
307       file = g_file_new_for_path (path);
308       types = g_content_type_guess_for_tree (file);
309       g_assert_content_type_equals (types[0], tests[i]);
310       g_strfreev (types);
311       g_object_unref (file);
312    }
313 }
314
315 int
316 main (int argc, char *argv[])
317 {
318   g_test_init (&argc, &argv, NULL);
319
320   g_test_add_func ("/contenttype/guess", test_guess);
321   g_test_add_func ("/contenttype/unknown", test_unknown);
322   g_test_add_func ("/contenttype/subtype", test_subtype);
323   g_test_add_func ("/contenttype/list", test_list);
324   g_test_add_func ("/contenttype/executable", test_executable);
325   g_test_add_func ("/contenttype/description", test_description);
326   g_test_add_func ("/contenttype/icon", test_icon);
327   g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
328   g_test_add_func ("/contenttype/tree", test_tree);
329
330   return g_test_run ();
331 }