Git init
[framework/appfw/aul-1.git] / test / aul_test.c
1 /*
2 Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
3 PROPRIETARY/CONFIDENTIAL
4 This software is the confidential and proprietary information of 
5 SAMSUNG ELECTRONICS ("Confidential Information"). You agree and acknowledge that 
6 this software is owned by Samsung and you 
7 shall not disclose such Confidential Information and shall 
8 use it only in accordance with the terms of the license agreement 
9 you entered into with SAMSUNG ELECTRONICS.  SAMSUNG make no 
10 representations or warranties about the suitability 
11 of the software, either express or implied, including but not 
12 limited to the implied warranties of merchantability, fitness for 
13 a particular purpose, or non-infringement. 
14 SAMSUNG shall not be liable for any damages suffered by licensee arising out of or 
15 related to this software.
16 */
17
18 #include <poll.h>
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include <Ecore.h>
23
24 #include "menu_db_util.h"
25 #include "aul.h"
26 #define PERF_ACTIVATE
27 #include "perf.h"
28
29 #define MAX_LOCAL_BUFSZ 128
30 #define QUERY_LEN       10240
31
32 static char **gargv;
33 static int gargc;
34 static char *cmd;
35 static int apn_pid;
36
37 typedef struct _test_func_t {
38         char *name;
39         int (*func) ();
40         char *desc;
41         char *usage;
42 } test_func_t;
43
44 static bundle *create_internal_bundle(int start)
45 {
46         bundle *kb;
47         int i;
48
49         kb = bundle_create();
50         for (i = start; i < gargc - 1; i++) {
51                 if ((i + 1) > gargc - 1)
52                         bundle_add(kb, gargv[i], " ");
53                 else
54                         bundle_add(kb, gargv[i], gargv[i + 1]);
55         }
56
57         return kb;
58 }
59
60 int launch_test()
61 {
62         static int num = 0;
63         int ret = 0;
64         bundle *kb = NULL;
65
66         kb = create_internal_bundle(3);
67         if (NULL == kb) {
68                 return -1;
69         }
70         printf("[aul_launch_app %d test] %s \n", num++, gargv[2]);
71
72         ret = aul_launch_app(gargv[2], kb);
73
74         if (kb) {
75                 bundle_free(kb);
76                 kb = NULL;
77         }
78         return ret;
79 }
80
81 int dbus_launch_test()
82 {
83         bundle *kb = NULL;
84         int ret = 0;
85
86         kb = create_internal_bundle(3);
87
88         if (NULL == kb) {
89                 return -1;
90         }
91
92         ret = aul_launch_app(gargv[2], kb);
93
94         if (kb) {
95                 bundle_free(kb);
96                 kb = NULL;
97         }
98
99         return ret;
100 }
101
102 static void prt_recvd_bundle(const char *key, const char *value, void *d)
103 {
104         printf("recvd - key: %s, value: %s\n", key, value);
105 }
106
107 static void cb_func(bundle *kb, int is_cancel, void *data)
108 {
109         int num;
110         num = (int)data;
111
112         if (is_cancel) {
113                 printf("==== %d : canceled(preemptted) my request ===\n", num);
114         } else {
115                 printf("==== %d : result packet ===\n", num);
116                 bundle_iterate(kb, prt_recvd_bundle, NULL);
117         }
118
119         if ((strcmp(cmd, "launch_res") == 0)
120             || (strcmp(cmd, "open_svc_res") == 0))
121                 ecore_main_loop_quit();
122 }
123
124 int resume_test()
125 {
126         static int num = 0;
127
128         printf("[aul_open_app %d test] %s \n", num++, gargv[2]);
129         return aul_open_app(gargv[2]);
130 }
131
132 int resume_pid_test()
133 {
134         static int num = 0;
135         printf("[aul_resume_pid %d test] %d \n", num++, apn_pid);
136         return aul_resume_pid(apn_pid);
137 }
138
139 int term_pid_test()
140 {
141         static int num = 0;
142         printf("[aul_term_pid %d test] %d \n", num++, apn_pid);
143         return aul_terminate_pid(apn_pid);
144 }
145
146 static test_func_t scn_func[] = {
147         {"n", launch_test, "launch_test", ""},
148         {"n", launch_test, "launch_test", ""},
149         {"n", resume_test, "open_test", ""},
150         {"n", resume_test, "open_test", ""},
151         {"p", resume_pid_test, "resume_pid_test", ""},
152         {"p", resume_pid_test, "resume_pid_test", ""},
153         {"p", term_pid_test, "term_pid_test", ""},
154         {"n", resume_test, "open_test", ""},
155         {"n", launch_test, "launch_test", ""}
156 };
157
158 static Eina_Bool run_all_test(void *data)
159 {
160         static int pos = 0;
161         int ret;
162
163         if (pos > sizeof(scn_func) / sizeof(test_func_t) - 1) {
164                 printf("all internal test done\n");
165                 ecore_main_loop_quit();
166                 return 0;
167         }
168
169         if (strncmp(scn_func[pos].name, "n", 1) == 0) {
170                 printf("[test %d] %s , pkgname = %s\n", pos, scn_func[pos].desc,
171                        gargv[2]);
172                 apn_pid = scn_func[pos].func();
173                 printf("... return pid = %d\n", apn_pid);
174         } else {
175                 printf("[test %d] %s , pid = %d\n", pos, scn_func[pos].desc,
176                        apn_pid);
177                 ret = scn_func[pos].func();
178                 printf("... return res = %d\n", ret);
179         }
180         pos++;
181
182         return 1;
183 }
184
185 int all_test()
186 {
187         ecore_timer_add(2, run_all_test, NULL);
188         return 0;
189 }
190
191 int is_run_test()
192 {
193         if (aul_app_is_running(gargv[2]))
194                 printf("... %s is running\n", gargv[2]);
195         else
196                 printf("... %s is not running\n", gargv[2]);
197
198         return 0;
199 }
200
201 int iterfunc(const aul_app_info *info, void *data)
202 {
203         printf("\t==========================\n");
204         printf("\t pkg_name: %s\n", info->pkg_name);
205         printf("\t app_path: %s\n", info->app_path);
206         printf("\t running pid: %d\n", info->pid);
207         printf("\t==========================\n");
208
209         return 0;
210 }
211
212 int get_allpkg_test()
213 {
214         static int num = 0;
215         printf("[aul_app_get_ruuning_app_info %d test] \n", num++);
216         return aul_app_get_running_app_info(iterfunc, NULL);
217 }
218
219 int get_pkgpid_test()
220 {
221         int pid = 0;
222         static int num = 0;
223         char buf[MAX_LOCAL_BUFSZ];
224
225         printf("[aul_app_get_pkgname_bypid %d test] \n", num++);
226         pid = atoi(gargv[2]);
227
228         if (aul_app_get_pkgname_bypid(pid, buf, sizeof(buf)) < 0)
229                 printf("no such pkg by %d\n", pid);
230         else
231                 printf("pkgname = %s, pid = %d\n", buf, pid);
232
233         return 0;
234 }
235
236 int open_file_test()
237 {
238         static int num = 0;
239         printf("[aul_open_file %d test] %s \n", num++, gargv[2]);
240         return aul_open_file(gargv[2]);
241 }
242
243 int open_content_test()
244 {
245         static int num = 0;
246         printf("[aul_open_content %d test] %s \n", num++, gargv[2]);
247         return aul_open_content(gargv[2]);
248 }
249
250 int get_defapp_test()
251 {
252         static int num = 0;
253         int ret;
254         char buf[MAX_LOCAL_BUFSZ];
255         printf("[aul_get_defapp_from_mime %d test] %s \n", num++, gargv[2]);
256         ret = aul_get_defapp_from_mime(gargv[2], buf, sizeof(buf));
257         if (ret >= 0)
258                 printf("==> defapp name = %s\n", buf);
259         return ret;
260 }
261
262 int set_defapp_test()
263 {
264         static int num = 0;
265         int ret;
266         printf("[aul_set_defapp_with_mime %d test] %s %s\n", num++, gargv[2],
267                gargv[3]);
268         ret = aul_set_defapp_with_mime(gargv[2], gargv[3]);
269         return ret;
270 }
271
272 int get_mime_file_test()
273 {
274         static int num = 0;
275         int ret;
276         char buf[MAX_LOCAL_BUFSZ];
277         printf("[aul_get_mime_from_file %d test] %s \n", num++, gargv[2]);
278         ret = aul_get_mime_from_file(gargv[2], buf, sizeof(buf));
279         if (ret >= 0)
280                 printf("==> mime type = %s\n", buf);
281         return ret;
282 }
283
284 int get_mime_content_test()
285 {
286         static int num = 0;
287         int ret;
288         char buf[MAX_LOCAL_BUFSZ];
289         printf("[aul_get_mime_from_content %d test] %s \n", num++, gargv[2]);
290         ret = aul_get_mime_from_content(gargv[2], buf, sizeof(buf));
291         if (ret >= 0)
292                 printf("==> mime type = %s\n", buf);
293         return ret;
294 }
295
296 int aul_get_mime_icon_test()
297 {
298         int ret;
299         char buf[MAX_LOCAL_BUFSZ];
300         ret = aul_get_mime_icon(gargv[2], buf, sizeof(buf));
301         if (ret >= 0)
302                 printf("==> mimetype %s : iconname = %s\n", gargv[2], buf);
303         return ret;
304 }
305
306 int aul_get_mime_description_test()
307 {
308         int ret;
309         char buf[MAX_LOCAL_BUFSZ];
310         ret = aul_get_mime_description(gargv[2], buf, sizeof(buf));
311         if (ret >= 0)
312                 printf("==> mimetype %s : description = %s\n", gargv[2], buf);
313         return ret;
314 }
315
316 int aul_get_mime_extension_test()
317 {
318         int ret;
319         char buf[MAX_LOCAL_BUFSZ];
320         ret = aul_get_mime_extension(gargv[2], buf, sizeof(buf));
321         if (ret >= 0)
322                 printf("==> mimetype %s : extension = %s\n", gargv[2], buf);
323         return ret;
324 }
325
326 static void print_menu_db_info(const app_info_from_db *info)
327 {
328         if (info == NULL) {
329                 printf("pkg %s no found\n", gargv[2]);
330                 return;
331         }
332
333         printf("\t==========================\n");
334         printf("\t pkg_name: %s\n", info->pkg_name);
335         printf("\t app_path: %s\n", info->app_path);
336         printf("\t w: %d\n", info->width);
337         printf("\t h: %d\n", info->height);
338         printf("\t is_minst: %d\n", 0);
339         printf("\t==========================\n");
340 }
341
342 static int get_pkg_func()
343 {
344         app_info_from_db *info;
345
346         info = _get_app_info_from_db_by_pkgname(gargv[2]);
347         print_menu_db_info(info);
348         if (info)
349                 _free_app_info_from_db(info);
350
351         return 0;
352 }
353
354 /*
355 static int set_pkg_func()
356 {
357         char* pkgname;
358         char* apppath;
359         char* appname;
360         char query[QUERY_LEN];
361
362         pkgname = gargv[2];
363         apppath = gargv[3];
364         
365         appname = strrchr(apppath,'/')+1;
366         snprintf(ai.app_icon_path, PATH_LEN, "aul_test_icon_path/%d",getpid());
367         snprintf(ai.desktop_path, PATH_LEN, 
368                 "aul_test_desktop_path/%d",getpid());
369
370         snprintf (query, sizeof(query), "insert into "TABLE_MENU"(\
371         pkg_name,\
372         app_path,\
373         app_name,\
374         app_icon_path,\
375         desktop_path)\
376         values ('%s', '%s', '%s', '%s', '%s')",
377         pkgname,
378         apppath,
379         appname,
380         record->app_icon_path,
381         record->desktop_path,
382         );
383
384         // TODO: record_add is not supported anymore; use AIL
385         if (record_add(ADD_ICON, &ai)){
386                 printf("set pkg success\n");
387                 return 0;
388         }
389         else{
390                 printf("set pkg fail\n");
391                 return -1;
392         }
393 }
394
395 static int del_pkg_func()
396 {
397         app_info ai;
398
399         memset(&ai, 0, sizeof(app_info));
400         snprintf(ai.pkg_name, NAME_LEN, "%s", gargv[2]);
401
402         // TODO: record_add is not supported anymore; use AIL
403         if(record_delete(DELETE_MENU, &ai)){
404                 printf("del pkg success\n");
405                 return 0;
406         }
407         else {
408                 printf("del pkg fail\n");
409                 return -1;
410         }
411 }
412 */
413
414 static int test_regex()
415 {
416         char *token;
417         char mime[MAX_LOCAL_BUFSZ];
418         char *saveptr;
419
420         INIT_PERF(NULL);
421
422         printf("=======================\n");
423
424         token = strtok_r(gargv[2], " \t\n,.()", &saveptr);
425         if (aul_get_mime_from_content(token, mime, sizeof(mime)) == AUL_R_OK)
426                 printf("found %s %s\n", mime, token);
427
428         while (token) {
429                 token = strtok_r(NULL, " \t\n,()", &saveptr);
430                 if (aul_get_mime_from_content(token, mime, sizeof(mime)) ==
431                     AUL_R_OK)
432                         printf("found %s %s\n", mime, token);
433         }
434
435         PERF("======= parse end =====\n");
436         return 0;
437 }
438
439 int open_svc_test()
440 {
441         static int num = 0;
442         int ret;
443
444         bundle *kb = NULL;
445         kb = create_internal_bundle(3);
446         if (kb == NULL) {
447                 printf("bundle creation fail\n");
448                 return -1;
449         }
450         printf("[aul_open_service %d test] %s \n", num++, gargv[2]);
451         ret = aul_open_service(gargv[2], kb, NULL, NULL);
452         if (ret >= 0) {
453                 printf("open service success\n");
454                 if (kb) {
455                         bundle_free(kb);
456                         kb = NULL;
457                 }
458                 return 0;
459         } else {
460                 printf("open service fail\n");
461                 if (kb) {
462                         bundle_free(kb);
463                         kb = NULL;
464                 }
465                 return -1;
466         }
467 }
468
469 int open_svc_res_test()
470 {
471         static int num = 0;
472         int ret;
473
474         bundle *kb = NULL;
475         kb = create_internal_bundle(3);
476         if (kb == NULL) {       /* Prevent Fix: ID: 21027,21581 */
477                 printf("bundle creation fail\n");
478                 return -1;
479         }
480
481         printf("[aul_open_service(wait result) %d test] %s \n", num++,
482                gargv[2]);
483         ret = aul_open_service(gargv[2], kb, cb_func, (void *)num);
484         if (ret >= 0) {
485                 printf("open service(wait result) success\n");
486                 if (kb) {
487                         bundle_free(kb);
488                         kb = NULL;
489                 }
490                 return 0;
491         } else {
492                 printf("open service(wait result) fail\n");
493                 if (kb) {
494                         bundle_free(kb);
495                         kb = NULL;
496                 }
497                 return -1;
498         }
499 }
500
501 int get_defapp_svc_test()
502 {
503         static int num = 0;
504         int ret;
505         char buf[MAX_LOCAL_BUFSZ];
506         printf("[aul_get_defapp_from_svc %d test] %s \n", num++, gargv[2]);
507         ret = aul_get_defapp_for_service(gargv[2], buf, sizeof(buf));
508         if (ret >= 0)
509                 printf("==> defapp name = %s\n", buf);
510         return ret;
511 }
512
513 int set_defapp_svc_test()
514 {
515         static int num = 0;
516         int ret;
517         printf("[aul_set_defapp_with_svc %d test] %s %s\n", num++, gargv[2],
518                gargv[3]);
519         ret = aul_set_defapp_for_service(gargv[2], gargv[3]);
520         return ret;
521 }
522
523 static test_func_t test_func[] = {
524         {"launch",launch_test,"aul_launch_app test",
525                 "[usage] launch <pkgname> <key1> <val1> <key2> <val2> ..."},
526         {"open",resume_test,"aul_open_app test",
527                 "[usage] open <pkgname>" },
528         {"resume_pid",resume_pid_test,"aul_resume_pid test",
529                 "[usage] resume_pid <pid>" },
530         {"term_pid", term_pid_test,"aul_terminate_pid test",
531                 "[usage] term_pid <pid>" },
532         {"dbuslaunch", dbus_launch_test,"launch by dbus auto activation",
533                 "[usage] term_pid <pid>" },
534         {"all",all_test,"test based on predefine scenario",
535                 "[usage] all <pkgname>"},
536
537         {"is_run", is_run_test,"aul_is_running test",
538                 "[usage] is_run_test <pkgname>"},
539         {"getallpkg", get_allpkg_test, "aul_app_get_running_app_info test",
540                 "[usage] getallpkg all"},
541         {"getpkgpid", get_pkgpid_test, "aul_app_get_pkgname_bypid test",
542                 "[usage] getpkgpid <pid>"},
543         
544         {"open_file", open_file_test, "aul_open_file test",
545                 "[usage] open_file <filename>"},
546         {"open_content", open_content_test, "aul_open_content test",
547                 "[usage] open_content <content>"},
548         {"get_defapp_mime", get_defapp_test, "aul_get_defapp_from_mime test",
549                 "[usage] get_defapp_mime <mime_type>"},
550         {"set_defapp_mime", set_defapp_test, "aul_set_defapp_with_mime test",
551                 "[usage] set_defapp_mime <mime_type> <defapp to be set>"},
552         {"get_mime_file", get_mime_file_test, "aul_get_mime_from_file test",
553                 "[usage] get_mime_file <filename>"},
554         {"get_mime_content", get_mime_content_test, "aul_get_mime_from_content",
555                 "[usage] get_mime_content <content>"},
556
557         {"get_mime_icon", aul_get_mime_icon_test, "aul_get_mime_icon test",
558                 "[usage] get_mime_icon <mimetype>"},
559         {"get_mime_desc", aul_get_mime_description_test, "aul_get_mime_description test",
560                 "[usage] get_mime_desc <mimetype>"},
561         {"get_mime_ext", aul_get_mime_extension_test, "aul_get_mime_extension test",
562                 "[usage] get_mime_ext <mimetype>"},
563
564         {"test_regex", test_regex, "regular expression parser test",
565                 "[usage] test_regex <full text>"},
566
567         {"open_svc", open_svc_test, "aul_open_service test"
568                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
569         {"open_svc_res", open_svc_res_test, "aul_open_service (wait result) test"
570                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
571         {"set_defapp_svc", set_defapp_svc_test, "aul_set_defapp_with_svc test"
572                 "[usage] set_defapp_svc <svcname> <defapp to be set>"},
573         {"get_defapp_svc", get_defapp_svc_test, "aul_get_defapp_from_svc test"
574                 "[usage] get_defapp_svc <svcname>"},
575         
576         {"getpkg", get_pkg_func, "get package",
577                 "[usage] getpkg <pkgname>"},
578 /*
579         {"setpkg", set_pkg_func, "set package",
580                 "[usage] setpkg <pkgname> <apppath>"},
581         {"delpkg", del_pkg_func, "del package",
582                 "[usage] getpkg <pkgname>"},
583 */
584 };
585
586 int callfunc(char *testname)
587 {
588         test_func_t *tmp;
589         int res;
590         int i;
591
592         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
593                 tmp = &test_func[i];
594                 if (strcmp(testname, tmp->name) == 0) {
595                         res = tmp->func();
596                         if (strcmp(testname, "all")) {
597                                 if (res < 0)
598                                         printf("... test failed\n");
599                                 else
600                                         printf("... test successs ret = %d\n",
601                                                res);
602                         }
603                 }
604         }
605         return 0;
606 }
607
608 int dead_tracker(int pid, void *data)
609 {
610         printf("[DEAD] pid = %d dead\n", pid);
611         return 0;
612 }
613
614 void print_usage(char *progname)
615 {
616         test_func_t *tmp;
617         int i;
618
619         printf("[usage] %s <cmd> ...\n", progname);
620         printf(" - available cmd list\n");
621
622         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
623                 tmp = &test_func[i];
624                 printf("\t%s : %s\n", tmp->name, tmp->desc);
625                 printf("\t\t%s\n", tmp->usage);
626         }
627
628         printf("[note] getpkg/setpkg/delpkg/init_defapp_mime "
629                 "cmd is internal purpose\n");
630 }
631
632 static Eina_Bool run_func(void *data)
633 {
634         callfunc(cmd);
635
636         if (strcmp(cmd, "launch_res") == 0 || strcmp(cmd, "all") == 0
637             || strcmp(cmd, "dbuslaunch") == 0
638             || strcmp(cmd, "open_svc_res") == 0)
639                 return 0;
640         else
641                 ecore_main_loop_quit();
642
643         return 0;
644 }
645
646 int main(int argc, char **argv)
647 {
648         if (argc < 3) {
649                 print_usage(argv[0]);
650                 exit(0);
651         }
652
653         ecore_init();
654
655         cmd = argv[1];
656         gargc = argc;
657         gargv = argv;
658         apn_pid = atoi(argv[2]);
659
660         aul_launch_init(NULL, NULL);
661
662         /*aul_listen_app_dead_signal(dead_tracker,NULL); */
663         /*aul_listen_app_dead_signal(NULL,NULL); */
664
665         ecore_idler_add(run_func, NULL);
666
667         ecore_main_loop_begin();
668
669         return 0;
670 }
671
672 /* vi: set ts=8 sts=8 sw=8: */
673