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