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
test_individual_glob(const char * glob,XdgGlobType expected_type)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 }
45 }
46
47 static void
test_glob_type(void)48 test_glob_type (void)
49 {
50 test_individual_glob ("*.gif", XDG_GLOB_SIMPLE);
51 test_individual_glob ("Foo*.gif", XDG_GLOB_FULL);
52 test_individual_glob ("*[4].gif", XDG_GLOB_FULL);
53 test_individual_glob ("Makefile", XDG_GLOB_LITERAL);
54 test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL);
55 test_individual_glob ("tree.[ch]", XDG_GLOB_FULL);
56 }
57
58 static void
test_alias(const char * mime_a,const char * mime_b,int expected)59 test_alias (const char *mime_a,
60 const char *mime_b,
61 int expected)
62 {
63 int actual;
64
65 actual = xdg_mime_mime_type_equal (mime_a, mime_b);
66
67 if (actual != expected)
68 {
69 printf ("Test Failed: %s is %s to %s\n",
70 mime_a, actual ? "equal" : "not equal", mime_b);
71 }
72 }
73
74 static void
test_aliasing(void)75 test_aliasing (void)
76 {
77 test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
78 test_alias ("application/x-gnome-app-info", "application/x-desktop", 1);
79 test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
80 test_alias ("application/x-wordperfect", "audio/x-midi", 0);
81 test_alias ("/", "vnd/vnd", 0);
82 test_alias ("application/octet-stream", "text/plain", 0);
83 test_alias ("text/plain", "text/*", 0);
84 }
85
86 static void
test_subclass(const char * mime_a,const char * mime_b,int expected)87 test_subclass (const char *mime_a,
88 const char *mime_b,
89 int expected)
90 {
91 int actual;
92
93 actual = xdg_mime_mime_type_subclass (mime_a, mime_b);
94
95 if (actual != expected)
96 {
97 printf ("Test Failed: %s is %s of %s\n",
98 mime_a, actual ? "subclass" : "not subclass", mime_b);
99 }
100 }
101
102 static void
test_subclassing(void)103 test_subclassing (void)
104 {
105 test_subclass ("application/rtf", "text/plain", 1);
106 test_subclass ("message/news", "text/plain", 1);
107 test_subclass ("message/news", "message/*", 1);
108 test_subclass ("message/news", "text/*", 1);
109 test_subclass ("message/news", "application/octet-stream", 1);
110 test_subclass ("application/rtf", "application/octet-stream", 1);
111 test_subclass ("application/x-gnome-app-info", "text/plain", 1);
112 test_subclass ("image/x-djvu", "image/vnd.djvu", 1);
113 test_subclass ("image/vnd.djvu", "image/x-djvu", 1);
114 test_subclass ("image/vnd.djvu", "text/plain", 0);
115 test_subclass ("image/vnd.djvu", "text/*", 0);
116 test_subclass ("text/*", "text/plain", 1);
117 }
118
119 static void
test_one_match(const char * filename,const char * expected)120 test_one_match (const char *filename, const char *expected)
121 {
122 const char *actual;
123
124 actual = xdg_mime_get_mime_type_from_file_name (filename);
125
126 if (strcmp (actual, expected) != 0)
127 {
128 printf ("Test Failed: mime type of %s is %s, expected %s\n",
129 filename, actual, expected);
130 }
131 }
132
133 static void
test_matches(void)134 test_matches (void)
135 {
136 test_one_match ("foo.bar.epub", "application/epub+zip");
137 test_one_match ("core", "application/x-core");
138 test_one_match ("README.in", "text/x-readme");
139 test_one_match ("README.gz", "application/x-gzip");
140 test_one_match ("blabla.cs", "text/x-csharp");
141 test_one_match ("blabla.f90", "text/x-fortran");
142 test_one_match ("blabla.F95", "text/x-fortran");
143 test_one_match ("tarball.tar.gz", "application/x-compressed-tar");
144 test_one_match ("file.gz", "application/x-gzip");
145 test_one_match ("file.tar.lzo", "application/x-tzo");
146 test_one_match ("file.lzo", "application/x-lzop");
147 }
148
149 static void
test_one_icon(const char * mimetype,const char * expected)150 test_one_icon (const char *mimetype, const char *expected)
151 {
152 const char *actual;
153
154 actual = xdg_mime_get_generic_icon (mimetype);
155
156 if (actual != expected && strcmp (actual, expected) != 0)
157 {
158 printf ("Test Failed: icon of %s is %s, expected %s\n",
159 mimetype, actual, expected);
160 }
161 }
162
163 static void
test_icons(void)164 test_icons (void)
165 {
166 test_one_icon ("application/x-font-ttx", "font-x-generic");
167 test_one_icon ("application/mathematica", "x-office-document");
168 test_one_icon ("text/plain", NULL);
169 }
170
171 int
main(int argc,char * argv[])172 main (int argc, char *argv[])
173 {
174 const char *result;
175 const char *file_name;
176 int i;
177
178 test_glob_type ();
179 test_aliasing ();
180 test_subclassing ();
181 test_matches ();
182 test_icons ();
183
184 for (i = 1; i < argc; i++)
185 {
186 file_name = argv[i];
187 result = xdg_mime_get_mime_type_for_file (file_name, NULL);
188 printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
189 }
190
191 #if 0
192 xdg_mime_dump ();
193 #endif
194 return 0;
195 }
196
197