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