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