4 #define g_assert_content_type_equals(s1, s2) \
6 const char *__s1 = (s1), *__s2 = (s2); \
7 if (g_content_type_equals (__s1, __s2)) ; \
9 g_assertion_message_cmpstr (G_LOG_DOMAIN, \
13 __s1, " == ", __s2); \
21 gchar *existing_directory;
27 "Exec=./appinfo-test --option\n";
30 existing_directory = (gchar *) g_getenv ("SYSTEMROOT");
32 if (existing_directory)
33 existing_directory = g_strdup_printf ("%s" G_DIR_SEPARATOR_S, existing_directory);
35 existing_directory = g_strdup ("/etc/");
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_true (uncertain);
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);
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_false (uncertain);
59 /* Sadly win32 & OSX just don't have as large and robust of a mime type database as Linux */
62 res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
63 expected = g_content_type_from_mime_type ("text/plain");
64 g_assert_content_type_equals (expected, res);
65 g_assert_false (uncertain);
69 res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
70 expected = g_content_type_from_mime_type ("application/x-desktop");
71 g_assert_content_type_equals (expected, res);
72 g_assert_false (uncertain);
76 res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
77 expected = g_content_type_from_mime_type ("application/x-desktop");
78 g_assert_content_type_equals (expected, res);
79 g_assert_false (uncertain);
83 /* this is potentially ambiguous: it does not match the PO template format,
84 * but looks like text so it can't be Powerpoint */
85 res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
86 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
87 g_assert_content_type_equals (expected, res);
88 g_assert_false (uncertain);
92 res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
93 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
94 g_assert_content_type_equals (expected, res);
95 g_assert_false (uncertain);
99 res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
100 expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
101 g_assert_content_type_equals (expected, res);
102 /* we cannot reliably detect binary powerpoint files as long as there is no
103 * defined MIME magic, so do not check uncertain here
108 res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
109 expected = g_content_type_from_mime_type ("application/x-font-otf");
110 g_assert_content_type_equals (expected, res);
111 g_assert_false (uncertain);
114 #endif /* __APPLE__ */
116 res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
117 expected = g_content_type_from_mime_type ("image/x-eps");
118 g_assert_content_type_equals (expected, res);
119 g_assert_false (uncertain);
123 /* The data below would be detected as a valid content type, but shouldn’t be read at all. */
124 res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 0, &uncertain);
125 expected = g_content_type_from_mime_type ("application/x-zerosize");
126 g_assert_content_type_equals (expected, res);
127 g_assert_false (uncertain);
130 #endif /* G_OS_WIN32 */
139 unknown = g_content_type_from_mime_type ("application/octet-stream");
140 g_assert_true (g_content_type_is_unknown (unknown));
141 str = g_content_type_get_mime_type (unknown);
142 g_assert_cmpstr (str, ==, "application/octet-stream");
153 plain = g_content_type_from_mime_type ("text/plain");
154 xml = g_content_type_from_mime_type ("application/xml");
156 g_assert_true (g_content_type_is_a (xml, plain));
157 g_assert_true (g_content_type_is_mime_type (xml, "text/plain"));
164 find_mime (gconstpointer a, gconstpointer b)
166 if (g_content_type_equals (a, b))
179 g_test_skip ("The OSX backend does not implement g_content_types_get_registered()");
183 plain = g_content_type_from_mime_type ("text/plain");
184 xml = g_content_type_from_mime_type ("application/xml");
186 types = g_content_types_get_registered ();
188 g_assert_cmpuint (g_list_length (types), >, 1);
190 /* just check that some types are in the list */
191 g_assert_nonnull (g_list_find_custom (types, plain, find_mime));
192 g_assert_nonnull (g_list_find_custom (types, xml, find_mime));
194 g_list_free_full (types, g_free);
201 test_executable (void)
206 type = g_content_type_from_mime_type ("application/vnd.microsoft.portable-executable");
207 /* FIXME: the MIME is not in the default `MIME\Database\Content Type` registry.
208 * g_assert_true (g_content_type_can_be_executable (type));
212 type = g_content_type_from_mime_type ("application/x-executable");
213 g_assert_true (g_content_type_can_be_executable (type));
216 type = g_content_type_from_mime_type ("text/plain");
217 g_assert_true (g_content_type_can_be_executable (type));
220 type = g_content_type_from_mime_type ("image/png");
221 g_assert_false (g_content_type_can_be_executable (type));
226 test_description (void)
231 type = g_content_type_from_mime_type ("text/plain");
232 desc = g_content_type_get_description (type);
233 g_assert_nonnull (desc);
245 type = g_content_type_from_mime_type ("text/plain");
246 icon = g_content_type_get_icon (type);
247 g_assert_true (G_IS_ICON (icon));
248 if (G_IS_THEMED_ICON (icon))
250 const gchar *const *names;
252 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
254 g_assert_true (g_strv_contains (names, "text-*"));
255 #elif defined(G_OS_WIN32)
256 g_assert_cmpuint (g_strv_length ((GStrv) names), >, 0);
258 g_assert_true (g_strv_contains (names, "text-plain"));
259 g_assert_true (g_strv_contains (names, "text-x-generic"));
262 g_object_unref (icon);
265 type = g_content_type_from_mime_type ("application/rtf");
266 icon = g_content_type_get_icon (type);
267 g_assert_true (G_IS_ICON (icon));
268 if (G_IS_THEMED_ICON (icon))
270 const gchar *const *names;
272 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
274 g_assert_true (g_strv_contains (names, "text-x-generic"));
276 g_assert_true (g_strv_contains (names, "application-rtf"));
278 g_assert_true (g_strv_contains (names, "x-office-document"));
282 g_object_unref (icon);
287 test_symbolic_icon (void)
293 type = g_content_type_from_mime_type ("text/plain");
294 icon = g_content_type_get_symbolic_icon (type);
295 g_assert_true (G_IS_ICON (icon));
296 if (G_IS_THEMED_ICON (icon))
298 const gchar *const *names;
300 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
302 g_assert_true (g_strv_contains (names, "text-*-symbolic"));
303 g_assert_true (g_strv_contains (names, "text-*"));
305 g_assert_true (g_strv_contains (names, "text-plain-symbolic"));
306 g_assert_true (g_strv_contains (names, "text-x-generic-symbolic"));
307 g_assert_true (g_strv_contains (names, "text-plain"));
308 g_assert_true (g_strv_contains (names, "text-x-generic"));
311 g_object_unref (icon);
314 type = g_content_type_from_mime_type ("application/rtf");
315 icon = g_content_type_get_symbolic_icon (type);
316 g_assert_true (G_IS_ICON (icon));
317 if (G_IS_THEMED_ICON (icon))
319 const gchar *const *names;
321 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
322 g_assert_true (g_strv_contains (names, "application-rtf-symbolic"));
323 g_assert_true (g_strv_contains (names, "application-rtf"));
325 g_assert_true (g_strv_contains (names, "x-office-document-symbolic"));
326 g_assert_true (g_strv_contains (names, "x-office-document"));
329 g_object_unref (icon);
337 const gchar *tests[] = {
338 "x-content/image-dcf",
339 "x-content/unix-software",
340 "x-content/win32-software"
347 #if defined(__APPLE__) || defined(G_OS_WIN32)
348 g_test_skip ("The OSX & Windows backends do not implement g_content_type_guess_for_tree()");
352 for (i = 0; i < G_N_ELEMENTS (tests); i++)
354 path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
355 file = g_file_new_for_path (path);
356 types = g_content_type_guess_for_tree (file);
357 g_assert_content_type_equals (types[0], tests[i]);
359 g_object_unref (file);
364 test_type_is_a_special_case (void)
368 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=782311");
370 /* Everything but the inode type is application/octet-stream */
371 res = g_content_type_is_a ("inode/directory", "application/octet-stream");
372 g_assert_false (res);
373 #if !defined(__APPLE__) && !defined(G_OS_WIN32)
374 res = g_content_type_is_a ("anything", "application/octet-stream");
380 test_guess_svg_from_data (void)
382 const gchar svgfilecontent[] = "<svg xmlns=\"http://www.w3.org/2000/svg\"\
383 xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\
384 <rect x=\"10\" y=\"10\" height=\"100\" width=\"100\"\n\
385 style=\"stroke:#ff0000; fill: #0000ff\"/>\n\
388 gboolean uncertain = TRUE;
389 gchar *res = g_content_type_guess (NULL, (guchar *)svgfilecontent,
390 sizeof (svgfilecontent) - 1, &uncertain);
392 g_assert_cmpstr (res, ==, "public.svg-image");
393 #elif defined(G_OS_WIN32)
394 g_test_skip ("svg type detection from content is not implemented on WIN32");
396 g_assert_cmpstr (res, ==, "image/svg+xml");
398 g_assert_false (uncertain);
403 test_mime_from_content (void)
407 mime_type = g_content_type_get_mime_type ("com.microsoft.bmp");
408 g_assert_cmpstr (mime_type, ==, "image/bmp");
410 mime_type = g_content_type_get_mime_type ("com.compuserve.gif");
411 g_assert_cmpstr (mime_type, ==, "image/gif");
413 mime_type = g_content_type_get_mime_type ("public.png");
414 g_assert_cmpstr (mime_type, ==, "image/png");
416 mime_type = g_content_type_get_mime_type ("public.text");
417 g_assert_cmpstr (mime_type, ==, "text/*");
419 mime_type = g_content_type_get_mime_type ("public.svg-image");
420 g_assert_cmpstr (mime_type, ==, "image/svg+xml");
422 #elif defined(G_OS_WIN32)
423 g_test_skip ("mime from content type test not implemented on WIN32");
425 g_test_skip ("mime from content type test not implemented on UNIX");
430 main (int argc, char *argv[])
432 g_test_init (&argc, &argv, NULL);
434 g_test_add_func ("/contenttype/guess", test_guess);
435 g_test_add_func ("/contenttype/guess_svg_from_data", test_guess_svg_from_data);
436 g_test_add_func ("/contenttype/mime_from_content", test_mime_from_content);
437 g_test_add_func ("/contenttype/unknown", test_unknown);
438 g_test_add_func ("/contenttype/subtype", test_subtype);
439 g_test_add_func ("/contenttype/list", test_list);
440 g_test_add_func ("/contenttype/executable", test_executable);
441 g_test_add_func ("/contenttype/description", test_description);
442 g_test_add_func ("/contenttype/icon", test_icon);
443 g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
444 g_test_add_func ("/contenttype/tree", test_tree);
445 g_test_add_func ("/contenttype/test_type_is_a_special_case",
446 test_type_is_a_special_case);
448 return g_test_run ();