EFL 1.7 svn doobies
[profile/ivi/efreet.git] / src / tests / ef_desktop.c
1 #include "Efreet.h"
2 #include "config.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include "ef_test.h"
9
10 static void *_cb_command(void *data, Efreet_Desktop *desktop, char *exec, int remaining);
11
12
13 int
14 ef_cb_desktop_parse(void)
15 {
16     Efreet_Desktop *desktop;
17     Eina_List *l;
18     int ret = 1;
19
20     desktop = efreet_desktop_get(PKG_DATA_DIR"/test/test.desktop");
21     if (!desktop)
22     {
23         printf("No desktop found.\n");
24         return 0;
25     }
26
27     if (!desktop->name || strcmp(desktop->name, "Efreet Test Application"))
28     {
29         printf("Invalid Name\n");
30         ret = 0;
31     }
32
33     if (!desktop->generic_name ||
34         strcmp(desktop->generic_name, "Test Application"))
35     {
36         printf("Incorrent GenericName\n");
37         ret = 0;
38     }
39
40     if (!desktop->exec || strcmp(desktop->exec, "efreet_test %F %i"))
41     {
42         printf("Incorrect Exec (%s)\n", (desktop->exec ? desktop->exec : "(null)"));
43         ret = 0;
44     }
45
46     if (desktop->categories)
47     {
48         const char *categories[] = {"Test", "Enlightenment"};
49         const char *cat;
50         int num_categories = 2, i = 0;
51
52         EINA_LIST_FOREACH(desktop->categories, l, cat)
53         {
54             if (i >= num_categories)
55             {
56                 printf("Too many categories found.\n");
57                 ret = 0;
58                 break;
59             }
60
61             if (!cat || !categories[i] || strcmp(cat, categories[i]))
62             {
63                 printf("Expected category %s, found %s\n", categories[i], cat);
64                 ret = 0;
65             }
66             i++;
67         }
68     }
69     else ret = 0;
70
71     efreet_desktop_free(desktop);
72
73     return ret;
74 }
75
76 #if 0
77 int
78 ef_cb_desktop_file_id(void)
79 {
80     Efreet_Desktop *desktop;
81     int ret = 1;
82
83     desktop = efreet_desktop_get(PKG_DATA_DIR"/test/test.desktop");
84     if (desktop)
85     {
86         const char *id;
87         int i = 0;
88
89         struct {
90             char *dir;
91             int legacy;
92             char *prefix;
93             char *expected;
94         } tests[] = {
95             {PKG_DATA_DIR"/test/", 0, NULL, "test.desktop"},
96             {PKG_DATA_DIR"/", 0, NULL, "test-test.desktop"},
97             {PKG_DATA_DIR"/", 1, NULL, "test.desktop"},
98             {PKG_DATA_DIR"/", 1, "prefix", "prefix-test.desktop"},
99             {NULL, 0, NULL, NULL}
100         };
101
102         for (i = 0; tests[i].dir != NULL; i++)
103         {
104             id = efreet_desktop_id_get(desktop,
105                                        tests[i].dir,
106                                        tests[i].legacy,
107                                        tests[i].prefix);
108             if (!id || strcmp(id, tests[i].expected))
109             {
110                 printf("Expecting id: %s, got: %s\n", tests[i].expected, id);
111                 ret = 0;
112             }
113             if (id) eina_stringshare_del(id);
114         }
115     }
116     else
117         ret = 0;
118
119     return ret;
120 }
121 #endif
122
123 int
124 ef_cb_desktop_save(void)
125 {
126     Efreet_Desktop *desktop;
127
128     printf("\n");
129     desktop = efreet_desktop_get(PKG_DATA_DIR"/test/test.desktop");
130     if (!desktop)
131     {
132         printf("Failed to get Desktop file\n");
133         return 0;
134     }
135
136     printf("save data: %d\n", efreet_desktop_save(desktop));
137     efreet_desktop_free(desktop);
138
139     desktop = efreet_desktop_empty_new("/tmp/test.desktop");
140     desktop->name = strdup("Efreet Test Application");
141     desktop->type = EFREET_DESKTOP_TYPE_APPLICATION;
142     desktop->generic_name = strdup("Test Application");
143     desktop->exec = strdup("efreet_test");
144     desktop->categories = NULL;
145     desktop->categories = eina_list_append(desktop->categories, eina_stringshare_add("Test"));
146     desktop->categories = eina_list_append(desktop->categories, eina_stringshare_add("Enlightenment"));
147     printf("save test: %d\n", efreet_desktop_save(desktop));
148     unlink("/tmp/test.desktop");
149     efreet_desktop_free(desktop);
150
151     return 1;
152 }
153
154 typedef struct
155 {
156   Eina_List *expected;
157   int error;
158   char type;
159 } Test_Info;
160
161 int
162 ef_cb_desktop_command_get(void)
163 {
164     Efreet_Desktop *desktop;
165     Eina_List *files, *expected;
166     char olddir[PATH_MAX];
167     Test_Info *info;
168     int ret;
169
170     if (getcwd(olddir, PATH_MAX) != 0) ret = 0;
171     if (chdir("/") != 0) ret = 0;
172
173     printf("\n");
174     desktop = efreet_desktop_empty_new("test.desktop");
175
176     desktop->name = strdup("App Name");
177     desktop->icon = strdup("icon.png");
178
179     files = NULL;
180     files = eina_list_append(files, "/tmp/absolute_path");
181     files = eina_list_append(files, "relative_path");
182     files = eina_list_append(files, "file:///tmp/absolute_uri");
183     files = eina_list_append(files, "file:relative_uri");
184
185     info = NEW(Test_Info, 1);
186     expected = NULL;
187     info->error = 0;
188
189     /* test single full path */
190     info->type = 'f';
191     IF_FREE(desktop->exec);
192     desktop->exec = strdup("app %f");
193     expected = eina_list_append(expected, "app '/tmp/absolute_path'");
194     expected = eina_list_append(expected, "app '/relative_path'");
195     expected = eina_list_append(expected, "app '/tmp/absolute_uri'");
196     expected = eina_list_append(expected, "app '/relative_uri'");
197
198     info->expected = expected;
199     efreet_desktop_command_get(desktop, files, _cb_command, info);
200     expected = eina_list_free(expected);
201
202     /* test single uri */
203     info->type = 'u';
204     IF_FREE(desktop->exec);
205     desktop->exec = strdup("app %u");
206     expected = eina_list_append(expected, "app 'file:///tmp/absolute_path'");
207     expected = eina_list_append(expected, "app 'file:///relative_path'");
208     expected = eina_list_append(expected, "app 'file:///tmp/absolute_uri'");
209     expected = eina_list_append(expected, "app 'file:///relative_uri'");
210
211     info->expected = expected;
212     efreet_desktop_command_get(desktop, files, _cb_command, info);
213     expected = eina_list_free(expected);
214
215     /* test single dir */
216 #if 0
217     info->type = 'd';
218     IF_FREE(desktop->exec);
219     desktop->exec = strdup("app %d");
220     expected = eina_list_append(expected, "app '/tmp'");
221     expected = eina_list_append(expected, "app '/'");
222     expected = eina_list_append(expected, "app '/tmp'");
223     expected = eina_list_append(expected, "app '/'");
224
225     info->expected = expected;
226     efreet_desktop_command_get(desktop, files, _cb_command, info);
227     expected = eina_list_free(expected);
228 #endif
229
230
231     /* test single names */
232 #if 0
233     info->type = 'n';
234     IF_FREE(desktop->exec);
235     desktop->exec = strdup("app %n");
236     expected = eina_list_append(expected, "app 'absolute_path'");
237     expected = eina_list_append(expected, "app 'relative_path'");
238     expected = eina_list_append(expected, "app 'absolute_uri'");
239     expected = eina_list_append(expected, "app 'relative_uri'");
240
241     info->expected = expected;
242     efreet_desktop_command_get(desktop, files, _cb_command, info);
243     expected = eina_list_free(expected);
244 #endif
245
246     /* test multiple fullpaths */
247     info->type = 'F';
248     IF_FREE(desktop->exec);
249     desktop->exec = strdup("app %F");
250     expected = eina_list_append(expected, "app '/tmp/absolute_path' '/relative_path' '/tmp/absolute_uri' '/relative_uri'");
251
252     info->expected = expected;
253     efreet_desktop_command_get(desktop, files, _cb_command, info);
254     expected = eina_list_free(expected);
255
256     /* test multiple URIs */
257     info->type = 'U';
258     IF_FREE(desktop->exec);
259     desktop->exec = strdup("app %U");
260     expected = eina_list_append(expected, "app 'file:///tmp/absolute_path' 'file:///relative_path' 'file:///tmp/absolute_uri' 'file:///relative_uri'");
261
262     info->expected = expected;
263     efreet_desktop_command_get(desktop, files, _cb_command, info);
264     expected = eina_list_free(expected);
265
266     /* test multiple dirs */
267 #if 0
268     info->type = 'D';
269     IF_FREE(desktop->exec);
270     desktop->exec = strdup("app %D");
271     expected = eina_list_append(expected, "app '/tmp' '/' '/tmp' '/'");
272
273     info->expected = expected;
274     efreet_desktop_command_get(desktop, files, _cb_command, info);
275     expected = eina_list_free(expected);
276 #endif
277
278     /* test multiple names */
279 #if 0
280     info->type = 'N';
281     IF_FREE(desktop->exec);
282     desktop->exec = strdup("app %N");
283     expected = eina_list_append(expected, "app 'absolute_path' 'relative_path' 'absolute_uri' 'relative_uri'");
284
285     info->expected = expected;
286     efreet_desktop_command_get(desktop, files, _cb_command, info);
287     expected = eina_list_free(expected);
288 #endif
289
290     /* test icon appending */
291     info->type = 'i';
292     IF_FREE(desktop->exec);
293     desktop->exec = strdup("app %i");
294     expected = eina_list_append(expected, "app --icon 'icon.png'");
295
296     info->expected = expected;
297     efreet_desktop_command_get(desktop, NULL, _cb_command, info);
298     expected = eina_list_free(expected);
299
300     /* test app name */
301     info->type = 'c';
302     IF_FREE(desktop->exec);
303     desktop->exec = strdup("app %c");
304     expected = eina_list_append(expected, "app 'App Name'");
305
306     info->expected = expected;
307     efreet_desktop_command_get(desktop, NULL, _cb_command, info);
308     expected = eina_list_free(expected);
309
310     /* test desktop path */
311     info->type = 'k';
312     IF_FREE(desktop->exec);
313     desktop->exec = strdup("app %k");
314     expected = eina_list_append(expected, "app 'test.desktop'");
315
316     info->expected = expected;
317     efreet_desktop_command_get(desktop, NULL, _cb_command, info);
318     eina_list_free(expected);
319
320     /* clean up */
321     efreet_desktop_free(desktop);
322     eina_list_free(files);
323
324     if (chdir(olddir) != 0) ret = 0;
325    
326     ret = info->error > 0 ? 0 : 1;
327     free(info);
328
329     return ret;
330 }
331
332 static void *
333 _cb_command(void *data, Efreet_Desktop *desktop __UNUSED__,
334             char *exec, int remaining __UNUSED__)
335 {
336   Test_Info *info = data;
337   char *expected;
338
339   expected = eina_list_data_get(info->expected);
340   info->expected = eina_list_demote_list(info->expected, info->expected);
341   if (!expected)
342   {
343     printf("  ERROR: (%%%c) got \"%s\", expected nothing\n", info->type, exec);
344     info->error++;
345   }
346   else
347   {
348     if (strcmp(exec, expected))
349     {
350       printf("  ERROR: (%%%c) got \"%s\", expected \"%s\"\n", info->type, exec, expected);
351       info->error++;
352     }
353   }
354   free(exec);
355   return NULL;
356 }
357
358 static void *
359 cb_type_parse(Efreet_Desktop *desktop __UNUSED__, Efreet_Ini *ini)
360 {
361     const char *val;
362     val = efreet_ini_string_get(ini, "X-Test");
363     if (!val) return NULL;
364     return (void *)strdup(val);
365 }
366
367 int
368 ef_cb_desktop_type_parse(void)
369 {
370     Efreet_Desktop *desktop;
371     int my_type;
372     char *val;
373     int ret = 1;
374
375     /* add my custom desktop type to efreet */
376     my_type = efreet_desktop_type_add("My_Type", cb_type_parse, NULL,
377                                         (Efreet_Desktop_Type_Free_Cb)free);
378
379     desktop = efreet_desktop_get(PKG_DATA_DIR"/test/test_type.desktop");
380     if (!desktop)
381     {
382         printf("No desktop found.\n");
383         return 0;
384     }
385
386     if (desktop->type != my_type)
387     {
388         printf("Invalid type returned in desktop");
389         ret = 0;
390     }
391
392     val = (char *)efreet_desktop_type_data_get(desktop);
393     if (!val || strcmp(val, "Own key"))
394     {
395         printf("Invalid value of custom key (%s).\n", val);
396         ret = 0;
397     }
398
399     efreet_desktop_free(desktop);
400     return ret;
401 }