]> git.proxmox.com Git - libxdgmime-perl.git/blob - src/test-mime.c
Squashed 'xdgmime-source/' content from commit 28b70c4
[libxdgmime-perl.git] / src / test-mime.c
1 /*
2 * Copyright (C) 2003,2004 Red Hat, Inc.
3 * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
4 *
5 * Licensed under the Academic Free License version 2.0
6 * Or under the following terms:
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23 #include "xdgmime.h"
24 #include "xdgmimeglob.h"
25 #include <string.h>
26 #include <stdio.h>
27
28
29 static void
30 test_individual_glob (const char *glob,
31 XdgGlobType expected_type)
32 {
33 XdgGlobType test_type;
34
35 test_type = _xdg_glob_determine_type (glob);
36 if (test_type != expected_type)
37 {
38 printf ("Test Failed: %s is of type %s, but %s is expected\n",
39 glob,
40 ((test_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
41 ((test_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_FULL")),
42 ((expected_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
43 ((expected_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_COMPLEX")));
44 exit (1);
45 }
46 }
47
48 static void
49 test_glob_type (void)
50 {
51 test_individual_glob ("*.gif", XDG_GLOB_SIMPLE);
52 test_individual_glob ("Foo*.gif", XDG_GLOB_FULL);
53 test_individual_glob ("*[4].gif", XDG_GLOB_FULL);
54 test_individual_glob ("Makefile", XDG_GLOB_LITERAL);
55 test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL);
56 test_individual_glob ("tree.[ch]", XDG_GLOB_FULL);
57 }
58
59 static void
60 test_alias (const char *mime_a,
61 const char *mime_b,
62 int expected)
63 {
64 int actual;
65
66 actual = xdg_mime_mime_type_equal (mime_a, mime_b);
67
68 if (actual != expected)
69 {
70 printf ("Test Failed: %s is %s to %s\n",
71 mime_a, actual ? "equal" : "not equal", mime_b);
72 exit (1);
73 }
74 }
75
76 static void
77 test_aliasing (void)
78 {
79 test_alias ("application/wordperfect", "application/vnd.wordperfect", 1);
80 test_alias ("application/x-gnome-app-info", "application/x-desktop", 1);
81 test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
82 test_alias ("application/x-wordperfect", "audio/x-midi", 0);
83 test_alias ("/", "vnd/vnd", 0);
84 test_alias ("application/octet-stream", "text/plain", 0);
85 test_alias ("text/plain", "text/*", 0);
86 }
87
88 static void
89 test_subclass (const char *mime_a,
90 const char *mime_b,
91 int expected)
92 {
93 int actual;
94
95 actual = xdg_mime_mime_type_subclass (mime_a, mime_b);
96
97 if (actual != expected)
98 {
99 printf ("Test Failed: %s is %s of %s\n",
100 mime_a, actual ? "subclass" : "not subclass", mime_b);
101 exit (1);
102 }
103 }
104
105 static void
106 test_subclassing (void)
107 {
108 test_subclass ("application/rtf", "text/plain", 1);
109 test_subclass ("message/news", "text/plain", 1);
110 test_subclass ("message/news", "message/*", 1);
111 test_subclass ("message/news", "text/*", 1);
112 test_subclass ("message/news", "application/octet-stream", 1);
113 test_subclass ("application/rtf", "application/octet-stream", 1);
114 test_subclass ("application/x-gnome-app-info", "text/plain", 1);
115 test_subclass ("image/x-djvu", "image/vnd.djvu", 1);
116 test_subclass ("image/vnd.djvu", "image/x-djvu", 1);
117 test_subclass ("image/vnd.djvu", "text/plain", 0);
118 test_subclass ("image/vnd.djvu", "text/*", 0);
119 test_subclass ("text/*", "text/plain", 1);
120 }
121
122 static void
123 test_one_match (const char *filename, const char *expected)
124 {
125 const char *actual;
126
127 actual = xdg_mime_get_mime_type_from_file_name (filename);
128
129 if (strcasecmp (actual, expected) != 0)
130 {
131 printf ("Test Failed: mime type of %s is %s, expected %s\n",
132 filename, actual, expected);
133 exit (1);
134 }
135 }
136
137 static void
138 test_matches (void)
139 {
140 test_one_match ("foo.bar.epub", "application/epub+zip");
141 test_one_match ("core", "application/x-core");
142 test_one_match ("README.in", "text/x-readme");
143 test_one_match ("README.gz", "application/gzip");
144 test_one_match ("blabla.cs", "text/x-csharp");
145 test_one_match ("blabla.f90", "text/x-fortran");
146 test_one_match ("blabla.F95", "text/x-fortran");
147 test_one_match ("tarball.tar.gz", "application/x-compressed-tar");
148 test_one_match ("file.gz", "application/gzip");
149 test_one_match ("file.tar.lzo", "application/x-tzo");
150 test_one_match ("file.lzo", "application/x-lzop");
151 }
152
153 static void
154 test_one_icon (const char *mimetype, const char *expected)
155 {
156 const char *actual;
157
158 actual = xdg_mime_get_generic_icon (mimetype);
159
160 if (actual != expected && (actual == NULL || strcmp (actual, expected) != 0))
161 {
162 printf ("Test Failed: icon of %s is %s, expected %s\n",
163 mimetype, actual, expected);
164 exit (1);
165 }
166 }
167
168 static void
169 test_icons (void)
170 {
171 test_one_icon ("application/x-font-ttx", "font-x-generic");
172 test_one_icon ("application/mathematica", "x-office-document");
173 test_one_icon ("text/plain", NULL);
174 }
175
176 int
177 main (int argc, char *argv[])
178 {
179 const char *result;
180 const char *file_name;
181 int i;
182
183 test_glob_type ();
184 test_aliasing ();
185 test_subclassing ();
186 test_matches ();
187 test_icons ();
188
189 for (i = 1; i < argc; i++)
190 {
191 file_name = argv[i];
192 result = xdg_mime_get_mime_type_for_file (file_name, NULL);
193 printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
194 }
195
196 #if 0
197 xdg_mime_dump ();
198 #endif
199 return 0;
200 }
201