ca5dfe8c047f9636efc94a18553d6148ee061642
[profile/ivi/glib2.git] / gio / tests / contenttype.c
1 #include <gio/gio.h>
2
3 static void
4 test_guess (void)
5 {
6   gchar *res;
7   gchar *expected;
8   gboolean uncertain;
9
10   res = g_content_type_guess ("/etc/", NULL, 0, &uncertain);
11   expected = g_content_type_from_mime_type ("inode/directory");
12   g_assert (g_content_type_equals (expected, res));
13   g_free (res);
14   g_free (expected);
15
16   res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
17   expected = g_content_type_from_mime_type ("text/plain");
18   g_assert (g_content_type_equals (expected, res));
19   g_free (res);
20   g_free (expected);
21 }
22
23 static void
24 test_unknown (void)
25 {
26   gchar *unknown;
27   gchar *str;
28
29   unknown = g_content_type_from_mime_type ("application/octet-stream");
30   g_assert (g_content_type_is_unknown (unknown));
31   str = g_content_type_get_mime_type (unknown);
32   g_assert_cmpstr (str, ==, "application/octet-stream");
33   g_free (str);
34   g_free (unknown);
35 }
36
37 static void
38 test_subtype (void)
39 {
40   gchar *plain;
41   gchar *xml;
42
43   plain = g_content_type_from_mime_type ("text/plain");
44   xml = g_content_type_from_mime_type ("application/xml");
45
46   g_assert (g_content_type_is_a (xml, plain));
47
48   g_free (plain);
49   g_free (xml);
50 }
51
52 static gint
53 find_mime (gconstpointer a, gconstpointer b)
54 {
55   if (g_content_type_equals (a, b))
56     return 0;
57   return 1;
58 }
59
60 static void
61 test_list (void)
62 {
63   GList *types;
64   gchar *plain;
65   gchar *xml;
66
67   plain = g_content_type_from_mime_type ("text/plain");
68   xml = g_content_type_from_mime_type ("application/xml");
69
70   types = g_content_types_get_registered ();
71
72   g_assert (g_list_length (types) > 1);
73
74   /* just check that some types are in the list */
75   g_assert (g_list_find_custom (types, plain, find_mime) != NULL);
76   g_assert (g_list_find_custom (types, xml, find_mime) != NULL);
77
78   g_list_free_full (types, g_free);
79
80   g_free (plain);
81   g_free (xml);
82 }
83
84 static void
85 test_executable (void)
86 {
87   gchar *type;
88
89   type = g_content_type_from_mime_type ("application/x-executable");
90   g_assert (g_content_type_can_be_executable (type));
91   g_free (type);
92
93   type = g_content_type_from_mime_type ("text/plain");
94   g_assert (g_content_type_can_be_executable (type));
95   g_free (type);
96
97   type = g_content_type_from_mime_type ("image/png");
98   g_assert (!g_content_type_can_be_executable (type));
99   g_free (type);
100 }
101
102 static void
103 test_description (void)
104 {
105   gchar *type;
106   gchar *desc;
107
108   type = g_content_type_from_mime_type ("text/plain");
109   desc = g_content_type_get_description (type);
110   g_assert (desc != NULL);
111
112   g_free (desc);
113   g_free (type);
114 }
115
116 int
117 main (int argc, char *argv[])
118 {
119   g_type_init ();
120
121   g_test_init (&argc, &argv, NULL);
122
123   g_test_add_func ("/contenttype/guess", test_guess);
124   g_test_add_func ("/contenttype/unknown", test_unknown);
125   g_test_add_func ("/contenttype/subtype", test_subtype);
126   g_test_add_func ("/contenttype/list", test_list);
127   g_test_add_func ("/contenttype/executable", test_executable);
128   g_test_add_func ("/contenttype/description", test_description);
129
130   return g_test_run ();
131 }