gio: Annotate GDBusObjectManagerClient signal appropriately
[glib.git] / gio / tests / contenttype.c
blob1acbd776f3216f64c281fee46f347c852ba69d6d
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 res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
60 expected = g_content_type_from_mime_type ("text/plain");
61 g_assert_content_type_equals (expected, res);
62 g_assert (!uncertain);
63 g_free (res);
64 g_free (expected);
66 /* Sadly OSX just doesn't have as large and robust of a mime type database as Linux */
67 #ifndef __APPLE__
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);
114 res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
115 expected = g_content_type_from_mime_type ("image/x-eps");
116 g_assert_content_type_equals (expected, res);
117 g_assert (!uncertain);
118 g_free (res);
119 g_free (expected);
120 #endif
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 g_assert (g_strv_contains (names, "text-plain"));
237 g_assert (g_strv_contains (names, "text-x-generic"));
239 g_object_unref (icon);
240 g_free (type);
242 type = g_content_type_from_mime_type ("application/rtf");
243 icon = g_content_type_get_icon (type);
244 g_assert (G_IS_ICON (icon));
245 if (G_IS_THEMED_ICON (icon))
247 const gchar *const *names;
249 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
250 g_assert (g_strv_contains (names, "application-rtf"));
251 g_assert (g_strv_contains (names, "x-office-document"));
253 g_object_unref (icon);
254 g_free (type);
257 static void
258 test_symbolic_icon (void)
260 #ifndef G_OS_WIN32
261 gchar *type;
262 GIcon *icon;
264 type = g_content_type_from_mime_type ("text/plain");
265 icon = g_content_type_get_symbolic_icon (type);
266 g_assert (G_IS_ICON (icon));
267 if (G_IS_THEMED_ICON (icon))
269 const gchar *const *names;
271 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
272 g_assert (g_strv_contains (names, "text-plain-symbolic"));
273 g_assert (g_strv_contains (names, "text-x-generic-symbolic"));
274 g_assert (g_strv_contains (names, "text-plain"));
275 g_assert (g_strv_contains (names, "text-x-generic"));
277 g_object_unref (icon);
278 g_free (type);
280 type = g_content_type_from_mime_type ("application/rtf");
281 icon = g_content_type_get_symbolic_icon (type);
282 g_assert (G_IS_ICON (icon));
283 if (G_IS_THEMED_ICON (icon))
285 const gchar *const *names;
287 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
288 g_assert (g_strv_contains (names, "application-rtf-symbolic"));
289 g_assert (g_strv_contains (names, "x-office-document-symbolic"));
290 g_assert (g_strv_contains (names, "application-rtf"));
291 g_assert (g_strv_contains (names, "x-office-document"));
293 g_object_unref (icon);
294 g_free (type);
295 #endif
298 static void
299 test_tree (void)
301 const gchar *tests[] = {
302 "x-content/image-dcf",
303 "x-content/unix-software",
304 "x-content/win32-software"
306 const gchar *path;
307 GFile *file;
308 gchar **types;
309 gint i;
311 #ifdef __APPLE__
312 g_test_skip ("The OSX backend does not implement g_content_type_guess_for_tree()");
313 return;
314 #endif
316 for (i = 0; i < G_N_ELEMENTS (tests); i++)
318 path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
319 file = g_file_new_for_path (path);
320 types = g_content_type_guess_for_tree (file);
321 g_assert_content_type_equals (types[0], tests[i]);
322 g_strfreev (types);
323 g_object_unref (file);
327 static void
328 test_type_is_a_special_case (void)
330 gboolean res;
332 g_test_bug ("782311");
334 /* Everything but the inode type is application/octet-stream */
335 res = g_content_type_is_a ("inode/directory", "application/octet-stream");
336 g_assert_false (res);
337 res = g_content_type_is_a ("anything", "application/octet-stream");
338 g_assert_true (res);
342 main (int argc, char *argv[])
344 g_test_init (&argc, &argv, NULL);
346 g_test_bug_base ("http://bugzilla.gnome.org/");
348 g_test_add_func ("/contenttype/guess", test_guess);
349 g_test_add_func ("/contenttype/unknown", test_unknown);
350 g_test_add_func ("/contenttype/subtype", test_subtype);
351 g_test_add_func ("/contenttype/list", test_list);
352 g_test_add_func ("/contenttype/executable", test_executable);
353 g_test_add_func ("/contenttype/description", test_description);
354 g_test_add_func ("/contenttype/icon", test_icon);
355 g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
356 g_test_add_func ("/contenttype/tree", test_tree);
357 g_test_add_func ("/contenttype/test_type_is_a_special_case",
358 test_type_is_a_special_case);
360 return g_test_run ();