71888944d4f82d2843f3a38017e0ea5ce71c2ea0
[profile/ivi/glib2.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   res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", -1, &uncertain);
59   expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
60   g_assert (g_content_type_equals (expected, res));
61   g_assert (uncertain);
62   g_free (res);
63   g_free (expected);
64
65   res = g_content_type_guess ("test.otf", (guchar *)"OTTO", -1, &uncertain);
66   expected = g_content_type_from_mime_type ("application/x-font-otf");
67   g_assert (g_content_type_equals (expected, res));
68   g_assert (!uncertain);
69   g_free (res);
70   g_free (expected);
71 }
72
73 static void
74 test_unknown (void)
75 {
76   gchar *unknown;
77   gchar *str;
78
79   unknown = g_content_type_from_mime_type ("application/octet-stream");
80   g_assert (g_content_type_is_unknown (unknown));
81   str = g_content_type_get_mime_type (unknown);
82   g_assert_cmpstr (str, ==, "application/octet-stream");
83   g_free (str);
84   g_free (unknown);
85 }
86
87 static void
88 test_subtype (void)
89 {
90   gchar *plain;
91   gchar *xml;
92
93   plain = g_content_type_from_mime_type ("text/plain");
94   xml = g_content_type_from_mime_type ("application/xml");
95
96   g_assert (g_content_type_is_a (xml, plain));
97
98   g_free (plain);
99   g_free (xml);
100 }
101
102 static gint
103 find_mime (gconstpointer a, gconstpointer b)
104 {
105   if (g_content_type_equals (a, b))
106     return 0;
107   return 1;
108 }
109
110 static void
111 test_list (void)
112 {
113   GList *types;
114   gchar *plain;
115   gchar *xml;
116
117   plain = g_content_type_from_mime_type ("text/plain");
118   xml = g_content_type_from_mime_type ("application/xml");
119
120   types = g_content_types_get_registered ();
121
122   g_assert (g_list_length (types) > 1);
123
124   /* just check that some types are in the list */
125   g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
126   g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
127
128   g_list_free_full (types, g_free);
129
130   g_free (plain);
131   g_free (xml);
132 }
133
134 static void
135 test_executable (void)
136 {
137   gchar *type;
138
139   type = g_content_type_from_mime_type ("application/x-executable");
140   g_assert (g_content_type_can_be_executable (type));
141   g_free (type);
142
143   type = g_content_type_from_mime_type ("text/plain");
144   g_assert (g_content_type_can_be_executable (type));
145   g_free (type);
146
147   type = g_content_type_from_mime_type ("image/png");
148   g_assert (!g_content_type_can_be_executable (type));
149   g_free (type);
150 }
151
152 static void
153 test_description (void)
154 {
155   gchar *type;
156   gchar *desc;
157
158   type = g_content_type_from_mime_type ("text/plain");
159   desc = g_content_type_get_description (type);
160   g_assert (desc != NULL);
161
162   g_free (desc);
163   g_free (type);
164 }
165
166 static void
167 test_icon (void)
168 {
169   gchar *type;
170   GIcon *icon;
171
172   type = g_content_type_from_mime_type ("text/plain");
173   icon = g_content_type_get_icon (type);
174   g_assert (G_IS_ICON (icon));
175   g_object_unref (icon);
176   g_free (type);
177
178   type = g_content_type_from_mime_type ("application/rtf");
179   icon = g_content_type_get_icon (type);
180   g_assert (G_IS_ICON (icon));
181   g_object_unref (icon);
182   g_free (type);
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   g_type_init ();
190
191   g_test_init (&argc, &argv, NULL);
192
193   g_test_add_func ("/contenttype/guess", test_guess);
194   g_test_add_func ("/contenttype/unknown", test_unknown);
195   g_test_add_func ("/contenttype/subtype", test_subtype);
196   g_test_add_func ("/contenttype/list", test_list);
197   g_test_add_func ("/contenttype/executable", test_executable);
198   g_test_add_func ("/contenttype/description", test_description);
199   g_test_add_func ("/contenttype/icon", test_icon);
200
201   return g_test_run ();
202 }