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/", 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
);
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 (!uncertain
);
59 /* Sadly OSX just doesn't have as large and robust of a mime type database as Linux */
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
);
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
);
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
);
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
);
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
);
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
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
);
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
);
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");
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"));
154 find_mime (gconstpointer a
, gconstpointer b
)
156 if (g_content_type_equals (a
, b
))
169 g_test_skip ("The OSX backend does not implement g_content_types_get_registered()");
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
);
191 test_executable (void)
195 type
= g_content_type_from_mime_type ("application/x-executable");
196 g_assert (g_content_type_can_be_executable (type
));
199 type
= g_content_type_from_mime_type ("text/plain");
200 g_assert (g_content_type_can_be_executable (type
));
203 type
= g_content_type_from_mime_type ("image/png");
204 g_assert (!g_content_type_can_be_executable (type
));
209 test_description (void)
214 type
= g_content_type_from_mime_type ("text/plain");
215 desc
= g_content_type_get_description (type
);
216 g_assert (desc
!= NULL
);
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
));
237 g_assert (g_strv_contains (names
, "text-*"));
239 g_assert (g_strv_contains (names
, "text-plain"));
240 g_assert (g_strv_contains (names
, "text-x-generic"));
243 g_object_unref (icon
);
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"));
256 g_assert (g_strv_contains (names
, "x-office-document"));
259 g_object_unref (icon
);
264 test_symbolic_icon (void)
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
));
279 g_assert (g_strv_contains (names
, "text-*-symbolic"));
280 g_assert (g_strv_contains (names
, "text-*"));
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"));
288 g_object_unref (icon
);
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"));
302 g_assert (g_strv_contains (names
, "x-office-document-symbolic"));
303 g_assert (g_strv_contains (names
, "x-office-document"));
306 g_object_unref (icon
);
314 const gchar
*tests
[] = {
315 "x-content/image-dcf",
316 "x-content/unix-software",
317 "x-content/win32-software"
325 g_test_skip ("The OSX backend does not implement g_content_type_guess_for_tree()");
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
]);
336 g_object_unref (file
);
341 test_type_is_a_special_case (void)
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
);
351 res
= g_content_type_is_a ("anything", "application/octet-stream");
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\
365 gboolean uncertain
= TRUE
;
366 gchar
*res
= g_content_type_guess (NULL
, (guchar
*)svgfilecontent
,
367 sizeof (svgfilecontent
) - 1, &uncertain
);
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");
373 g_assert_cmpstr (res
, ==, "image/svg+xml");
375 g_assert_false (uncertain
);
380 test_mime_from_content (void)
384 mime_type
= g_content_type_get_mime_type ("com.microsoft.bmp");
385 g_assert_cmpstr (mime_type
, ==, "image/bmp");
387 mime_type
= g_content_type_get_mime_type ("com.compuserve.gif");
388 g_assert_cmpstr (mime_type
, ==, "image/gif");
390 mime_type
= g_content_type_get_mime_type ("public.png");
391 g_assert_cmpstr (mime_type
, ==, "image/png");
393 mime_type
= g_content_type_get_mime_type ("public.text");
394 g_assert_cmpstr (mime_type
, ==, "text/*");
396 mime_type
= g_content_type_get_mime_type ("public.svg-image");
397 g_assert_cmpstr (mime_type
, ==, "image/svg+xml");
399 #elif defined(G_OS_WIN32)
400 g_test_skip ("mime from content type test not implemented on WIN32");
402 g_test_skip ("mime from content type test not implemented on UNIX");
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 ();