Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / contenttype.c
blob2424b8e5f470ddfcfb97ad614395e3cf3bc60738
1 #include <gio/gio.h>
2 #include <string.h>
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)
16 static void
17 test_guess (void)
19 gchar *res;
20 gchar *expected;
21 gchar *existing_directory;
22 gboolean uncertain;
23 guchar data[] =
24 "[Desktop Entry]\n"
25 "Type=Application\n"
26 "Name=appinfo-test\n"
27 "Exec=./appinfo-test --option\n";
29 #ifdef G_OS_WIN32
30 existing_directory = (gchar *) g_getenv ("SYSTEMROOT");
32 if (existing_directory)
33 existing_directory = g_strdup_printf ("%s/", existing_directory);
34 #else
35 existing_directory = g_strdup ("/etc/");
36 #endif
38 res = g_content_type_guess (existing_directory, NULL, 0, &uncertain);
39 g_free (existing_directory);
40 expected = g_content_type_from_mime_type ("inode/directory");
41 g_assert_content_type_equals (expected, res);
42 g_assert (uncertain);
43 g_free (res);
44 g_free (expected);
46 res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
47 expected = g_content_type_from_mime_type ("text/plain");
48 g_assert_content_type_equals (expected, res);
49 g_free (res);
50 g_free (expected);
52 res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain);
53 expected = g_content_type_from_mime_type ("text/plain");
54 g_assert_content_type_equals (expected, res);
55 g_assert (!uncertain);
56 g_free (res);
57 g_free (expected);
59 /* Sadly OSX just doesn't have as large and robust of a mime type database as Linux */
60 #ifndef __APPLE__
61 res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
62 expected = g_content_type_from_mime_type ("text/plain");
63 g_assert_content_type_equals (expected, res);
64 g_assert (!uncertain);
65 g_free (res);
66 g_free (expected);
68 res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
69 expected = g_content_type_from_mime_type ("application/x-desktop");
70 g_assert_content_type_equals (expected, res);
71 g_assert (!uncertain);
72 g_free (res);
73 g_free (expected);
75 res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
76 expected = g_content_type_from_mime_type ("application/x-desktop");
77 g_assert_content_type_equals (expected, res);
78 g_assert (!uncertain);
79 g_free (res);
80 g_free (expected);
82 /* this is potentially ambiguous: it does not match the PO template format,
83 * but looks like text so it can't be Powerpoint */
84 res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
85 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
86 g_assert_content_type_equals (expected, res);
87 g_assert (!uncertain);
88 g_free (res);
89 g_free (expected);
91 res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
92 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
93 g_assert_content_type_equals (expected, res);
94 g_assert (!uncertain);
95 g_free (res);
96 g_free (expected);
98 res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
99 expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
100 g_assert_content_type_equals (expected, res);
101 /* we cannot reliably detect binary powerpoint files as long as there is no
102 * defined MIME magic, so do not check uncertain here
104 g_free (res);
105 g_free (expected);
107 res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
108 expected = g_content_type_from_mime_type ("application/x-font-otf");
109 g_assert_content_type_equals (expected, res);
110 g_assert (!uncertain);
111 g_free (res);
112 g_free (expected);
113 #endif
115 res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
116 expected = g_content_type_from_mime_type ("image/x-eps");
117 g_assert_content_type_equals (expected, res);
118 g_assert (!uncertain);
119 g_free (res);
120 g_free (expected);
123 static void
124 test_unknown (void)
126 gchar *unknown;
127 gchar *str;
129 unknown = g_content_type_from_mime_type ("application/octet-stream");
130 g_assert (g_content_type_is_unknown (unknown));
131 str = g_content_type_get_mime_type (unknown);
132 g_assert_cmpstr (str, ==, "application/octet-stream");
133 g_free (str);
134 g_free (unknown);
137 static void
138 test_subtype (void)
140 gchar *plain;
141 gchar *xml;
143 plain = g_content_type_from_mime_type ("text/plain");
144 xml = g_content_type_from_mime_type ("application/xml");
146 g_assert (g_content_type_is_a (xml, plain));
147 g_assert (g_content_type_is_mime_type (xml, "text/plain"));
149 g_free (plain);
150 g_free (xml);
153 static gint
154 find_mime (gconstpointer a, gconstpointer b)
156 if (g_content_type_equals (a, b))
157 return 0;
158 return 1;
161 static void
162 test_list (void)
164 GList *types;
165 gchar *plain;
166 gchar *xml;
168 #ifdef __APPLE__
169 g_test_skip ("The OSX backend does not implement g_content_types_get_registered()");
170 return;
171 #endif
173 plain = g_content_type_from_mime_type ("text/plain");
174 xml = g_content_type_from_mime_type ("application/xml");
176 types = g_content_types_get_registered ();
178 g_assert (g_list_length (types) > 1);
180 /* just check that some types are in the list */
181 g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
182 g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
184 g_list_free_full (types, g_free);
186 g_free (plain);
187 g_free (xml);
190 static void
191 test_executable (void)
193 gchar *type;
195 type = g_content_type_from_mime_type ("application/x-executable");
196 g_assert (g_content_type_can_be_executable (type));
197 g_free (type);
199 type = g_content_type_from_mime_type ("text/plain");
200 g_assert (g_content_type_can_be_executable (type));
201 g_free (type);
203 type = g_content_type_from_mime_type ("image/png");
204 g_assert (!g_content_type_can_be_executable (type));
205 g_free (type);
208 static void
209 test_description (void)
211 gchar *type;
212 gchar *desc;
214 type = g_content_type_from_mime_type ("text/plain");
215 desc = g_content_type_get_description (type);
216 g_assert (desc != NULL);
218 g_free (desc);
219 g_free (type);
222 static void
223 test_icon (void)
225 gchar *type;
226 GIcon *icon;
228 type = g_content_type_from_mime_type ("text/plain");
229 icon = g_content_type_get_icon (type);
230 g_assert (G_IS_ICON (icon));
231 if (G_IS_THEMED_ICON (icon))
233 const gchar *const *names;
235 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
236 #ifdef __APPLE__
237 g_assert (g_strv_contains (names, "text-*"));
238 #else
239 g_assert (g_strv_contains (names, "text-plain"));
240 g_assert (g_strv_contains (names, "text-x-generic"));
241 #endif
243 g_object_unref (icon);
244 g_free (type);
246 type = g_content_type_from_mime_type ("application/rtf");
247 icon = g_content_type_get_icon (type);
248 g_assert (G_IS_ICON (icon));
249 if (G_IS_THEMED_ICON (icon))
251 const gchar *const *names;
253 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
254 g_assert (g_strv_contains (names, "application-rtf"));
255 #ifndef __APPLE__
256 g_assert (g_strv_contains (names, "x-office-document"));
257 #endif
259 g_object_unref (icon);
260 g_free (type);
263 static void
264 test_symbolic_icon (void)
266 #ifndef G_OS_WIN32
267 gchar *type;
268 GIcon *icon;
270 type = g_content_type_from_mime_type ("text/plain");
271 icon = g_content_type_get_symbolic_icon (type);
272 g_assert (G_IS_ICON (icon));
273 if (G_IS_THEMED_ICON (icon))
275 const gchar *const *names;
277 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
278 #ifdef __APPLE__
279 g_assert (g_strv_contains (names, "text-*-symbolic"));
280 g_assert (g_strv_contains (names, "text-*"));
281 #else
282 g_assert (g_strv_contains (names, "text-plain-symbolic"));
283 g_assert (g_strv_contains (names, "text-x-generic-symbolic"));
284 g_assert (g_strv_contains (names, "text-plain"));
285 g_assert (g_strv_contains (names, "text-x-generic"));
286 #endif
288 g_object_unref (icon);
289 g_free (type);
291 type = g_content_type_from_mime_type ("application/rtf");
292 icon = g_content_type_get_symbolic_icon (type);
293 g_assert (G_IS_ICON (icon));
294 if (G_IS_THEMED_ICON (icon))
296 const gchar *const *names;
298 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
299 g_assert (g_strv_contains (names, "application-rtf-symbolic"));
300 g_assert (g_strv_contains (names, "application-rtf"));
301 #ifndef __APPLE__
302 g_assert (g_strv_contains (names, "x-office-document-symbolic"));
303 g_assert (g_strv_contains (names, "x-office-document"));
304 #endif
306 g_object_unref (icon);
307 g_free (type);
308 #endif
311 static void
312 test_tree (void)
314 const gchar *tests[] = {
315 "x-content/image-dcf",
316 "x-content/unix-software",
317 "x-content/win32-software"
319 const gchar *path;
320 GFile *file;
321 gchar **types;
322 gint i;
324 #ifdef __APPLE__
325 g_test_skip ("The OSX backend does not implement g_content_type_guess_for_tree()");
326 return;
327 #endif
329 for (i = 0; i < G_N_ELEMENTS (tests); i++)
331 path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
332 file = g_file_new_for_path (path);
333 types = g_content_type_guess_for_tree (file);
334 g_assert_content_type_equals (types[0], tests[i]);
335 g_strfreev (types);
336 g_object_unref (file);
340 static void
341 test_type_is_a_special_case (void)
343 gboolean res;
345 g_test_bug ("782311");
347 /* Everything but the inode type is application/octet-stream */
348 res = g_content_type_is_a ("inode/directory", "application/octet-stream");
349 g_assert_false (res);
350 #ifndef __APPLE__
351 res = g_content_type_is_a ("anything", "application/octet-stream");
352 g_assert_true (res);
353 #endif
356 static void
357 test_guess_svg_from_data (void)
359 const gchar svgfilecontent[] = "<svg xmlns=\"http://www.w3.org/2000/svg\"\
360 xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\
361 <rect x=\"10\" y=\"10\" height=\"100\" width=\"100\"\n\
362 style=\"stroke:#ff0000; fill: #0000ff\"/>\n\
363 </svg>\n";
365 gboolean uncertain = TRUE;
366 gchar *res = g_content_type_guess (NULL, (guchar *)svgfilecontent,
367 sizeof (svgfilecontent) - 1, &uncertain);
368 #ifdef __APPLE__
369 g_assert_cmpstr (res, ==, "public.svg-image");
370 #elif defined(G_OS_WIN32)
371 g_test_skip ("svg type detection from content is not implemented on WIN32");
372 #else
373 g_assert_cmpstr (res, ==, "image/svg+xml");
374 #endif
375 g_assert_false (uncertain);
376 g_free (res);
379 static void
380 test_mime_from_content (void)
382 #ifdef __APPLE__
383 gchar *mime_type;
384 mime_type = g_content_type_get_mime_type ("com.microsoft.bmp");
385 g_assert_cmpstr (mime_type, ==, "image/bmp");
386 g_free (mime_type);
387 mime_type = g_content_type_get_mime_type ("com.compuserve.gif");
388 g_assert_cmpstr (mime_type, ==, "image/gif");
389 g_free (mime_type);
390 mime_type = g_content_type_get_mime_type ("public.png");
391 g_assert_cmpstr (mime_type, ==, "image/png");
392 g_free (mime_type);
393 mime_type = g_content_type_get_mime_type ("public.text");
394 g_assert_cmpstr (mime_type, ==, "text/*");
395 g_free (mime_type);
396 mime_type = g_content_type_get_mime_type ("public.svg-image");
397 g_assert_cmpstr (mime_type, ==, "image/svg+xml");
398 g_free (mime_type);
399 #elif defined(G_OS_WIN32)
400 g_test_skip ("mime from content type test not implemented on WIN32");
401 #else
402 g_test_skip ("mime from content type test not implemented on UNIX");
403 #endif
407 main (int argc, char *argv[])
409 g_test_init (&argc, &argv, NULL);
411 g_test_bug_base ("http://bugzilla.gnome.org/");
413 g_test_add_func ("/contenttype/guess", test_guess);
414 g_test_add_func ("/contenttype/guess_svg_from_data", test_guess_svg_from_data);
415 g_test_add_func ("/contenttype/mime_from_content", test_mime_from_content);
416 g_test_add_func ("/contenttype/unknown", test_unknown);
417 g_test_add_func ("/contenttype/subtype", test_subtype);
418 g_test_add_func ("/contenttype/list", test_list);
419 g_test_add_func ("/contenttype/executable", test_executable);
420 g_test_add_func ("/contenttype/description", test_description);
421 g_test_add_func ("/contenttype/icon", test_icon);
422 g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
423 g_test_add_func ("/contenttype/tree", test_tree);
424 g_test_add_func ("/contenttype/test_type_is_a_special_case",
425 test_type_is_a_special_case);
427 return g_test_run ();