contenttype tests: better assertions
[platform/upstream/glib.git] / gio / tests / contenttype.c
1 #include <gio/gio.h>
2 #include <string.h>
3
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)
15
16 static void
17 test_guess (void)
18 {
19   gchar *res;
20   gchar *expected;
21   gboolean uncertain;
22   guchar data[] =
23     "[Desktop Entry]\n"
24     "Type=Application\n"
25     "Name=appinfo-test\n"
26     "Exec=./appinfo-test --option\n";
27
28   res = g_content_type_guess ("/etc/", NULL, 0, &uncertain);
29   expected = g_content_type_from_mime_type ("inode/directory");
30   g_assert_content_type_equals (expected, res);
31   g_assert (uncertain);
32   g_free (res);
33   g_free (expected);
34
35   res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
36   expected = g_content_type_from_mime_type ("text/plain");
37   g_assert_content_type_equals (expected, res);
38   g_free (res);
39   g_free (expected);
40
41   res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
42   expected = g_content_type_from_mime_type ("application/x-desktop");
43   g_assert_content_type_equals (expected, res);
44   g_assert (!uncertain);
45   g_free (res);
46   g_free (expected);
47
48   res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain);
49   expected = g_content_type_from_mime_type ("text/plain");
50   g_assert_content_type_equals (expected, res);
51   g_assert (!uncertain);
52   g_free (res);
53   g_free (expected);
54
55   res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
56   expected = g_content_type_from_mime_type ("text/plain");
57   g_assert_content_type_equals (expected, res);
58   g_assert (!uncertain);
59   g_free (res);
60   g_free (expected);
61
62   res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
63   expected = g_content_type_from_mime_type ("application/x-desktop");
64   g_assert_content_type_equals (expected, res);
65   g_assert (!uncertain);
66   g_free (res);
67   g_free (expected);
68
69   /* this is potentially ambiguous: it does not match the PO template format,
70    * but looks like text so it can't be Powerpoint */
71   res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
72   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
73   g_assert_content_type_equals (expected, res);
74   g_assert (!uncertain);
75   g_free (res);
76   g_free (expected);
77
78   res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
79   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
80   g_assert_content_type_equals (expected, res);
81   g_assert (!uncertain);
82   g_free (res);
83   g_free (expected);
84
85   res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
86   expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
87   g_assert_content_type_equals (expected, res);
88   /* we cannot reliably detect binary powerpoint files as long as there is no
89    * defined MIME magic, so do not check uncertain here
90    */
91   g_free (res);
92   g_free (expected);
93
94   res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
95   expected = g_content_type_from_mime_type ("application/x-font-otf");
96   g_assert_content_type_equals (expected, res);
97   g_assert (!uncertain);
98   g_free (res);
99   g_free (expected);
100 }
101
102 static void
103 test_unknown (void)
104 {
105   gchar *unknown;
106   gchar *str;
107
108   unknown = g_content_type_from_mime_type ("application/octet-stream");
109   g_assert (g_content_type_is_unknown (unknown));
110   str = g_content_type_get_mime_type (unknown);
111   g_assert_cmpstr (str, ==, "application/octet-stream");
112   g_free (str);
113   g_free (unknown);
114 }
115
116 static void
117 test_subtype (void)
118 {
119   gchar *plain;
120   gchar *xml;
121
122   plain = g_content_type_from_mime_type ("text/plain");
123   xml = g_content_type_from_mime_type ("application/xml");
124
125   g_assert (g_content_type_is_a (xml, plain));
126
127   g_free (plain);
128   g_free (xml);
129 }
130
131 static gint
132 find_mime (gconstpointer a, gconstpointer b)
133 {
134   if (g_content_type_equals (a, b))
135     return 0;
136   return 1;
137 }
138
139 static void
140 test_list (void)
141 {
142   GList *types;
143   gchar *plain;
144   gchar *xml;
145
146   plain = g_content_type_from_mime_type ("text/plain");
147   xml = g_content_type_from_mime_type ("application/xml");
148
149   types = g_content_types_get_registered ();
150
151   g_assert (g_list_length (types) > 1);
152
153   /* just check that some types are in the list */
154   g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
155   g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
156
157   g_list_free_full (types, g_free);
158
159   g_free (plain);
160   g_free (xml);
161 }
162
163 static void
164 test_executable (void)
165 {
166   gchar *type;
167
168   type = g_content_type_from_mime_type ("application/x-executable");
169   g_assert (g_content_type_can_be_executable (type));
170   g_free (type);
171
172   type = g_content_type_from_mime_type ("text/plain");
173   g_assert (g_content_type_can_be_executable (type));
174   g_free (type);
175
176   type = g_content_type_from_mime_type ("image/png");
177   g_assert (!g_content_type_can_be_executable (type));
178   g_free (type);
179 }
180
181 static void
182 test_description (void)
183 {
184   gchar *type;
185   gchar *desc;
186
187   type = g_content_type_from_mime_type ("text/plain");
188   desc = g_content_type_get_description (type);
189   g_assert (desc != NULL);
190
191   g_free (desc);
192   g_free (type);
193 }
194
195 static void
196 test_icon (void)
197 {
198   gchar *type;
199   GIcon *icon;
200
201   type = g_content_type_from_mime_type ("text/plain");
202   icon = g_content_type_get_icon (type);
203   g_assert (G_IS_ICON (icon));
204   g_object_unref (icon);
205   g_free (type);
206
207   type = g_content_type_from_mime_type ("application/rtf");
208   icon = g_content_type_get_icon (type);
209   g_assert (G_IS_ICON (icon));
210   g_object_unref (icon);
211   g_free (type);
212 }
213
214
215 int
216 main (int argc, char *argv[])
217 {
218   g_test_init (&argc, &argv, NULL);
219
220   g_test_add_func ("/contenttype/guess", test_guess);
221   g_test_add_func ("/contenttype/unknown", test_unknown);
222   g_test_add_func ("/contenttype/subtype", test_subtype);
223   g_test_add_func ("/contenttype/list", test_list);
224   g_test_add_func ("/contenttype/executable", test_executable);
225   g_test_add_func ("/contenttype/description", test_description);
226   g_test_add_func ("/contenttype/icon", test_icon);
227
228   return g_test_run ();
229 }