Add some more cases to the app-id unit tests
[glib.git] / gio / tests / contenttype.c
blob8c08da69c78d599db37ee84129049b70bc554322
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.desktop", data, sizeof (data) - 1, &uncertain);
53 expected = g_content_type_from_mime_type ("application/x-desktop");
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.txt", 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 res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
67 expected = g_content_type_from_mime_type ("text/plain");
68 g_assert_content_type_equals (expected, res);
69 g_assert (!uncertain);
70 g_free (res);
71 g_free (expected);
73 res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
74 expected = g_content_type_from_mime_type ("application/x-desktop");
75 g_assert_content_type_equals (expected, res);
76 g_assert (!uncertain);
77 g_free (res);
78 g_free (expected);
80 /* this is potentially ambiguous: it does not match the PO template format,
81 * but looks like text so it can't be Powerpoint */
82 res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
83 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
84 g_assert_content_type_equals (expected, res);
85 g_assert (!uncertain);
86 g_free (res);
87 g_free (expected);
89 res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
90 expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
91 g_assert_content_type_equals (expected, res);
92 g_assert (!uncertain);
93 g_free (res);
94 g_free (expected);
96 res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
97 expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
98 g_assert_content_type_equals (expected, res);
99 /* we cannot reliably detect binary powerpoint files as long as there is no
100 * defined MIME magic, so do not check uncertain here
102 g_free (res);
103 g_free (expected);
105 res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
106 expected = g_content_type_from_mime_type ("application/x-font-otf");
107 g_assert_content_type_equals (expected, res);
108 g_assert (!uncertain);
109 g_free (res);
110 g_free (expected);
112 res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
113 expected = g_content_type_from_mime_type ("image/x-eps");
114 g_assert_content_type_equals (expected, res);
115 g_assert (!uncertain);
116 g_free (res);
117 g_free (expected);
120 static void
121 test_unknown (void)
123 gchar *unknown;
124 gchar *str;
126 unknown = g_content_type_from_mime_type ("application/octet-stream");
127 g_assert (g_content_type_is_unknown (unknown));
128 str = g_content_type_get_mime_type (unknown);
129 g_assert_cmpstr (str, ==, "application/octet-stream");
130 g_free (str);
131 g_free (unknown);
134 static void
135 test_subtype (void)
137 gchar *plain;
138 gchar *xml;
140 plain = g_content_type_from_mime_type ("text/plain");
141 xml = g_content_type_from_mime_type ("application/xml");
143 g_assert (g_content_type_is_a (xml, plain));
145 g_free (plain);
146 g_free (xml);
149 static gint
150 find_mime (gconstpointer a, gconstpointer b)
152 if (g_content_type_equals (a, b))
153 return 0;
154 return 1;
157 static void
158 test_list (void)
160 GList *types;
161 gchar *plain;
162 gchar *xml;
164 plain = g_content_type_from_mime_type ("text/plain");
165 xml = g_content_type_from_mime_type ("application/xml");
167 types = g_content_types_get_registered ();
169 g_assert (g_list_length (types) > 1);
171 /* just check that some types are in the list */
172 g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
173 g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
175 g_list_free_full (types, g_free);
177 g_free (plain);
178 g_free (xml);
181 static void
182 test_executable (void)
184 gchar *type;
186 type = g_content_type_from_mime_type ("application/x-executable");
187 g_assert (g_content_type_can_be_executable (type));
188 g_free (type);
190 type = g_content_type_from_mime_type ("text/plain");
191 g_assert (g_content_type_can_be_executable (type));
192 g_free (type);
194 type = g_content_type_from_mime_type ("image/png");
195 g_assert (!g_content_type_can_be_executable (type));
196 g_free (type);
199 static void
200 test_description (void)
202 gchar *type;
203 gchar *desc;
205 type = g_content_type_from_mime_type ("text/plain");
206 desc = g_content_type_get_description (type);
207 g_assert (desc != NULL);
209 g_free (desc);
210 g_free (type);
213 static void
214 test_icon (void)
216 gchar *type;
217 GIcon *icon;
219 type = g_content_type_from_mime_type ("text/plain");
220 icon = g_content_type_get_icon (type);
221 g_assert (G_IS_ICON (icon));
222 if (G_IS_THEMED_ICON (icon))
224 const gchar *const *names;
226 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
227 g_assert (g_strv_contains (names, "text-plain"));
228 g_assert (g_strv_contains (names, "text-x-generic"));
230 g_object_unref (icon);
231 g_free (type);
233 type = g_content_type_from_mime_type ("application/rtf");
234 icon = g_content_type_get_icon (type);
235 g_assert (G_IS_ICON (icon));
236 if (G_IS_THEMED_ICON (icon))
238 const gchar *const *names;
240 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
241 g_assert (g_strv_contains (names, "application-rtf"));
242 g_assert (g_strv_contains (names, "x-office-document"));
244 g_object_unref (icon);
245 g_free (type);
248 static void
249 test_symbolic_icon (void)
251 #ifndef G_OS_WIN32
252 gchar *type;
253 GIcon *icon;
255 type = g_content_type_from_mime_type ("text/plain");
256 icon = g_content_type_get_symbolic_icon (type);
257 g_assert (G_IS_ICON (icon));
258 if (G_IS_THEMED_ICON (icon))
260 const gchar *const *names;
262 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
263 g_assert (g_strv_contains (names, "text-plain-symbolic"));
264 g_assert (g_strv_contains (names, "text-x-generic-symbolic"));
265 g_assert (g_strv_contains (names, "text-plain"));
266 g_assert (g_strv_contains (names, "text-x-generic"));
268 g_object_unref (icon);
269 g_free (type);
271 type = g_content_type_from_mime_type ("application/rtf");
272 icon = g_content_type_get_symbolic_icon (type);
273 g_assert (G_IS_ICON (icon));
274 if (G_IS_THEMED_ICON (icon))
276 const gchar *const *names;
278 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
279 g_assert (g_strv_contains (names, "application-rtf-symbolic"));
280 g_assert (g_strv_contains (names, "x-office-document-symbolic"));
281 g_assert (g_strv_contains (names, "application-rtf"));
282 g_assert (g_strv_contains (names, "x-office-document"));
284 g_object_unref (icon);
285 g_free (type);
286 #endif
289 static void
290 test_tree (void)
292 const gchar *tests[] = {
293 "x-content/image-dcf",
294 "x-content/unix-software",
295 "x-content/win32-software"
297 const gchar *path;
298 GFile *file;
299 gchar **types;
300 gint i;
302 for (i = 0; i < G_N_ELEMENTS (tests); i++)
304 path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
305 file = g_file_new_for_path (path);
306 types = g_content_type_guess_for_tree (file);
307 g_assert_content_type_equals (types[0], tests[i]);
308 g_strfreev (types);
309 g_object_unref (file);
314 main (int argc, char *argv[])
316 g_test_init (&argc, &argv, NULL);
318 g_test_add_func ("/contenttype/guess", test_guess);
319 g_test_add_func ("/contenttype/unknown", test_unknown);
320 g_test_add_func ("/contenttype/subtype", test_subtype);
321 g_test_add_func ("/contenttype/list", test_list);
322 g_test_add_func ("/contenttype/executable", test_executable);
323 g_test_add_func ("/contenttype/description", test_description);
324 g_test_add_func ("/contenttype/icon", test_icon);
325 g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
326 g_test_add_func ("/contenttype/tree", test_tree);
328 return g_test_run ();