Upload Tizen2.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 open_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_test()
138 {
139         static int num = 0;
140
141         printf("[aul_open_app %d test] %s \n", num++, gargv[2]);
142         return aul_resume_app(gargv[2]);
143 }
144
145 int resume_pid_test()
146 {
147         static int num = 0;
148         printf("[aul_resume_pid %d test] %d \n", num++, apn_pid);
149         return aul_resume_pid(apn_pid);
150 }
151
152 int term_pid_test()
153 {
154         static int num = 0;
155         printf("[aul_term_pid %d test] %d \n", num++, apn_pid);
156         return aul_terminate_pid(apn_pid);
157 }
158
159 static test_func_t scn_func[] = {
160         {"n", launch_test, "launch_test", ""},
161         {"n", launch_test, "launch_test", ""},
162         {"n", resume_test, "open_test", ""},
163         {"n", resume_test, "open_test", ""},
164         {"p", resume_pid_test, "resume_pid_test", ""},
165         {"p", resume_pid_test, "resume_pid_test", ""},
166         {"p", term_pid_test, "term_pid_test", ""},
167         {"n", resume_test, "open_test", ""},
168         {"n", launch_test, "launch_test", ""}
169 };
170
171 static Eina_Bool run_all_test(void *data)
172 {
173         static int pos = 0;
174         int ret;
175
176         if (pos > sizeof(scn_func) / sizeof(test_func_t) - 1) {
177                 printf("all internal test done\n");
178                 ecore_main_loop_quit();
179                 return 0;
180         }
181
182         if (strncmp(scn_func[pos].name, "n", 1) == 0) {
183                 printf("[test %d] %s , pkgname = %s\n", pos, scn_func[pos].desc,
184                        gargv[2]);
185                 apn_pid = scn_func[pos].func();
186                 printf("... return pid = %d\n", apn_pid);
187         } else {
188                 printf("[test %d] %s , pid = %d\n", pos, scn_func[pos].desc,
189                        apn_pid);
190                 ret = scn_func[pos].func();
191                 printf("... return res = %d\n", ret);
192         }
193         pos++;
194
195         return 1;
196 }
197
198 int all_test()
199 {
200         ecore_timer_add(2, run_all_test, NULL);
201         return 0;
202 }
203
204 int is_run_test()
205 {
206         if (aul_app_is_running(gargv[2]))
207                 printf("... %s is running\n", gargv[2]);
208         else
209                 printf("... %s is not running\n", gargv[2]);
210
211         return 0;
212 }
213
214 int iterfunc(const aul_app_info *info, void *data)
215 {
216         printf("\t==========================\n");
217         printf("\t pkg_name: %s\n", info->appid);
218         printf("\t app_path: %s\n", info->app_path);
219         printf("\t running pid: %d\n", info->pid);
220         printf("\t==========================\n");
221
222         return 0;
223 }
224
225 int get_allpkg_test()
226 {
227         static int num = 0;
228         printf("[aul_app_get_ruuning_app_info %d test] \n", num++);
229         return aul_app_get_running_app_info(iterfunc, NULL);
230 }
231
232 int get_pkgpid_test()
233 {
234         int pid = 0;
235         static int num = 0;
236         char buf[MAX_LOCAL_BUFSZ];
237
238         printf("[aul_app_get_appid_bypid %d test] \n", num++);
239         pid = atoi(gargv[2]);
240
241         if (aul_app_get_appid_bypid(pid, buf, sizeof(buf)) < 0)
242                 printf("no such pkg by %d\n", pid);
243         else
244                 printf("pkgname = %s, pid = %d\n", buf, pid);
245
246         return 0;
247 }
248
249 int open_file_test()
250 {
251         static int num = 0;
252         printf("[aul_open_file %d test] %s \n", num++, gargv[2]);
253         return aul_open_file(gargv[2]);
254 }
255
256 int open_content_test()
257 {
258         static int num = 0;
259         printf("[aul_open_content %d test] %s \n", num++, gargv[2]);
260         return aul_open_content(gargv[2]);
261 }
262
263 int get_defapp_test()
264 {
265         static int num = 0;
266         int ret;
267         char buf[MAX_LOCAL_BUFSZ];
268         printf("[aul_get_defapp_from_mime %d test] %s \n", num++, gargv[2]);
269         ret = aul_get_defapp_from_mime(gargv[2], buf, sizeof(buf));
270         if (ret >= 0)
271                 printf("==> defapp name = %s\n", buf);
272         return ret;
273 }
274
275 int set_defapp_test()
276 {
277         static int num = 0;
278         int ret;
279         printf("[aul_set_defapp_with_mime %d test] %s %s\n", num++, gargv[2],
280                gargv[3]);
281         ret = aul_set_defapp_with_mime(gargv[2], gargv[3]);
282         return ret;
283 }
284
285 int get_mime_file_test()
286 {
287         static int num = 0;
288         int ret;
289         char buf[MAX_LOCAL_BUFSZ];
290         printf("[aul_get_mime_from_file %d test] %s \n", num++, gargv[2]);
291         ret = aul_get_mime_from_file(gargv[2], buf, sizeof(buf));
292         if (ret >= 0)
293                 printf("==> mime type = %s\n", buf);
294         return ret;
295 }
296
297 int get_mime_content_test()
298 {
299         static int num = 0;
300         int ret;
301         char buf[MAX_LOCAL_BUFSZ];
302         printf("[aul_get_mime_from_content %d test] %s \n", num++, gargv[2]);
303         ret = aul_get_mime_from_content(gargv[2], buf, sizeof(buf));
304         if (ret >= 0)
305                 printf("==> mime type = %s\n", buf);
306         return ret;
307 }
308
309 int aul_get_mime_icon_test()
310 {
311         int ret;
312         char buf[MAX_LOCAL_BUFSZ];
313         ret = aul_get_mime_icon(gargv[2], buf, sizeof(buf));
314         if (ret >= 0)
315                 printf("==> mimetype %s : iconname = %s\n", gargv[2], buf);
316         return ret;
317 }
318
319 int aul_get_mime_description_test()
320 {
321         int ret;
322         char buf[MAX_LOCAL_BUFSZ];
323         ret = aul_get_mime_description(gargv[2], buf, sizeof(buf));
324         if (ret >= 0)
325                 printf("==> mimetype %s : description = %s\n", gargv[2], buf);
326         return ret;
327 }
328
329 int aul_get_mime_extension_test()
330 {
331         int ret;
332         char buf[MAX_LOCAL_BUFSZ];
333         ret = aul_get_mime_extension(gargv[2], buf, sizeof(buf));
334         if (ret >= 0)
335                 printf("==> mimetype %s : extension = %s\n", gargv[2], buf);
336         return ret;
337 }
338
339 static void print_menu_db_info(const app_info_from_db *info)
340 {
341         if (info == NULL) {
342                 printf("pkg %s no found\n", gargv[2]);
343                 return;
344         }
345
346         printf("\t==========================\n");
347         printf("\t pkg_name: %s\n", info->pkg_name);
348         printf("\t app_path: %s\n", info->app_path);
349         printf("\t is_minst: %d\n", 0);
350         printf("\t==========================\n");
351 }
352
353 static int get_pkg_func()
354 {
355         app_info_from_db *info;
356
357         info = _get_app_info_from_db_by_pkgname(gargv[2]);
358         print_menu_db_info(info);
359         if (info)
360                 _free_app_info_from_db(info);
361
362         return 0;
363 }
364
365 /*
366 static int set_pkg_func()
367 {
368         char* pkgname;
369         char* apppath;
370         char* appname;
371         char query[QUERY_LEN];
372
373         pkgname = gargv[2];
374         apppath = gargv[3];
375         
376         appname = strrchr(apppath,'/')+1;
377         snprintf(ai.app_icon_path, PATH_LEN, "aul_test_icon_path/%d",getpid());
378         snprintf(ai.desktop_path, PATH_LEN, 
379                 "aul_test_desktop_path/%d",getpid());
380
381         snprintf (query, sizeof(query), "insert into "TABLE_MENU"(\
382         pkg_name,\
383         app_path,\
384         app_name,\
385         app_icon_path,\
386         desktop_path)\
387         values ('%s', '%s', '%s', '%s', '%s')",
388         pkgname,
389         apppath,
390         appname,
391         record->app_icon_path,
392         record->desktop_path,
393         );
394
395         // TODO: record_add is not supported anymore; use AIL
396         if (record_add(ADD_ICON, &ai)){
397                 printf("set pkg success\n");
398                 return 0;
399         }
400         else{
401                 printf("set pkg fail\n");
402                 return -1;
403         }
404 }
405
406 static int del_pkg_func()
407 {
408         app_info ai;
409
410         memset(&ai, 0, sizeof(app_info));
411         snprintf(ai.pkg_name, NAME_LEN, "%s", gargv[2]);
412
413         // TODO: record_add is not supported anymore; use AIL
414         if(record_delete(DELETE_MENU, &ai)){
415                 printf("del pkg success\n");
416                 return 0;
417         }
418         else {
419                 printf("del pkg fail\n");
420                 return -1;
421         }
422 }
423 */
424
425 static int test_regex()
426 {
427         char *token;
428         char mime[MAX_LOCAL_BUFSZ];
429         char *saveptr;
430
431         INIT_PERF(NULL);
432
433         printf("=======================\n");
434
435         token = strtok_r(gargv[2], " \t\n,.()", &saveptr);
436         if (aul_get_mime_from_content(token, mime, sizeof(mime)) == AUL_R_OK)
437                 printf("found %s %s\n", mime, token);
438
439         while (token) {
440                 token = strtok_r(NULL, " \t\n,()", &saveptr);
441                 if (aul_get_mime_from_content(token, mime, sizeof(mime)) ==
442                     AUL_R_OK)
443                         printf("found %s %s\n", mime, token);
444         }
445
446         PERF("======= parse end =====\n");
447         return 0;
448 }
449
450 int open_svc_test()
451 {
452         static int num = 0;
453         int ret;
454
455         bundle *kb = NULL;
456         kb = create_internal_bundle(3);
457         if (kb == NULL) {
458                 printf("bundle creation fail\n");
459                 return -1;
460         }
461         printf("[aul_open_service %d test] %s \n", num++, gargv[2]);
462         ret = aul_open_service(gargv[2], kb, NULL, NULL);
463         if (ret >= 0) {
464                 printf("open service success\n");
465                 if (kb) {
466                         bundle_free(kb);
467                         kb = NULL;
468                 }
469                 return 0;
470         } else {
471                 printf("open service fail\n");
472                 if (kb) {
473                         bundle_free(kb);
474                         kb = NULL;
475                 }
476                 return -1;
477         }
478 }
479
480 int open_svc_res_test()
481 {
482         static int num = 0;
483         int ret;
484
485         bundle *kb = NULL;
486         kb = create_internal_bundle(3);
487         if (kb == NULL) {       /* Prevent Fix: ID: 21027,21581 */
488                 printf("bundle creation fail\n");
489                 return -1;
490         }
491
492         printf("[aul_open_service(wait result) %d test] %s \n", num++,
493                gargv[2]);
494         ret = aul_open_service(gargv[2], kb, cb_func, (void *)num);
495         if (ret >= 0) {
496                 printf("open service(wait result) success\n");
497                 if (kb) {
498                         bundle_free(kb);
499                         kb = NULL;
500                 }
501                 return 0;
502         } else {
503                 printf("open service(wait result) fail\n");
504                 if (kb) {
505                         bundle_free(kb);
506                         kb = NULL;
507                 }
508                 return -1;
509         }
510 }
511
512 int get_defapp_svc_test()
513 {
514         static int num = 0;
515         int ret;
516         char buf[MAX_LOCAL_BUFSZ];
517         printf("[aul_get_defapp_from_svc %d test] %s \n", num++, gargv[2]);
518         ret = aul_get_defapp_for_service(gargv[2], buf, sizeof(buf));
519         if (ret >= 0)
520                 printf("==> defapp name = %s\n", buf);
521         return ret;
522 }
523
524 int set_defapp_svc_test()
525 {
526         static int num = 0;
527         int ret;
528         printf("[aul_set_defapp_with_svc %d test] %s %s\n", num++, gargv[2],
529                gargv[3]);
530         ret = aul_set_defapp_for_service(gargv[2], gargv[3]);
531         return ret;
532 }
533
534 static test_func_t test_func[] = {
535         {"launch",launch_test,"aul_launch_app test",
536                 "[usage] launch <pkgname> <key1> <val1> <key2> <val2> ..."},
537         {"open",open_test,"aul_open_app test",
538                 "[usage] open <pkgname>" },
539         {"resume",resume_test,"aul_resume_app test",
540                 "[usage] resume <pkgname>" },
541         {"resume_pid",resume_pid_test,"aul_resume_pid test",
542                 "[usage] resume_pid <pid>" },
543         {"term_pid", term_pid_test,"aul_terminate_pid test",
544                 "[usage] term_pid <pid>" },
545         {"dbuslaunch", dbus_launch_test,"launch by dbus auto activation",
546                 "[usage] term_pid <pid>" },
547         {"all",all_test,"test based on predefine scenario",
548                 "[usage] all <pkgname>"},
549
550         {"is_run", is_run_test,"aul_is_running test",
551                 "[usage] is_run <pkgname>"},
552         {"getallpkg", get_allpkg_test, "aul_app_get_running_app_info test",
553                 "[usage] getallpkg all"},
554         {"getpkgpid", get_pkgpid_test, "aul_app_get_appid_bypid test",
555                 "[usage] getpkgpid <pid>"},
556         
557         {"open_file", open_file_test, "aul_open_file test",
558                 "[usage] open_file <filename>"},
559         {"open_content", open_content_test, "aul_open_content test",
560                 "[usage] open_content <content>"},
561         {"get_defapp_mime", get_defapp_test, "aul_get_defapp_from_mime test",
562                 "[usage] get_defapp_mime <mime_type>"},
563         {"set_defapp_mime", set_defapp_test, "aul_set_defapp_with_mime test",
564                 "[usage] set_defapp_mime <mime_type> <defapp to be set>"},
565         {"get_mime_file", get_mime_file_test, "aul_get_mime_from_file test",
566                 "[usage] get_mime_file <filename>"},
567         {"get_mime_content", get_mime_content_test, "aul_get_mime_from_content",
568                 "[usage] get_mime_content <content>"},
569
570         {"get_mime_icon", aul_get_mime_icon_test, "aul_get_mime_icon test",
571                 "[usage] get_mime_icon <mimetype>"},
572         {"get_mime_desc", aul_get_mime_description_test, "aul_get_mime_description test",
573                 "[usage] get_mime_desc <mimetype>"},
574         {"get_mime_ext", aul_get_mime_extension_test, "aul_get_mime_extension test",
575                 "[usage] get_mime_ext <mimetype>"},
576
577         {"test_regex", test_regex, "regular expression parser test",
578                 "[usage] test_regex <full text>"},
579
580         {"open_svc", open_svc_test, "aul_open_service test"
581                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
582         {"open_svc_res", open_svc_res_test, "aul_open_service (wait result) test"
583                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
584         {"set_defapp_svc", set_defapp_svc_test, "aul_set_defapp_with_svc test"
585                 "[usage] set_defapp_svc <svcname> <defapp to be set>"},
586         {"get_defapp_svc", get_defapp_svc_test, "aul_get_defapp_from_svc test"
587                 "[usage] get_defapp_svc <svcname>"},
588         
589         {"getpkg", get_pkg_func, "get package",
590                 "[usage] getpkg <pkgname>"},
591 /*
592         {"setpkg", set_pkg_func, "set package",
593                 "[usage] setpkg <pkgname> <apppath>"},
594         {"delpkg", del_pkg_func, "del package",
595                 "[usage] getpkg <pkgname>"},
596 */
597 };
598
599 int callfunc(char *testname)
600 {
601         test_func_t *tmp;
602         int res;
603         int i;
604
605         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
606                 tmp = &test_func[i];
607                 if (strcmp(testname, tmp->name) == 0) {
608                         res = tmp->func();
609                         if (strcmp(testname, "all")) {
610                                 if (res < 0)
611                                         printf("... test failed\n");
612                                 else
613                                         printf("... test successs ret = %d\n",
614                                                res);
615                         }
616                 }
617         }
618         return 0;
619 }
620
621 int dead_tracker(int pid, void *data)
622 {
623         printf("[DEAD] pid = %d dead\n", pid);
624         return 0;
625 }
626
627 void print_usage(char *progname)
628 {
629         test_func_t *tmp;
630         int i;
631
632         printf("[usage] %s <cmd> ...\n", progname);
633         printf(" - available cmd list\n");
634
635         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
636                 tmp = &test_func[i];
637                 printf("\t%s : %s\n", tmp->name, tmp->desc);
638                 printf("\t\t%s\n", tmp->usage);
639         }
640
641         printf("[note] getpkg/setpkg/delpkg/init_defapp_mime "
642                 "cmd is internal purpose\n");
643 }
644
645 static Eina_Bool run_func(void *data)
646 {
647         callfunc(cmd);
648
649         if (strcmp(cmd, "launch_res") == 0 || strcmp(cmd, "all") == 0
650             || strcmp(cmd, "dbuslaunch") == 0
651             || strcmp(cmd, "open_svc_res") == 0)
652                 return 0;
653         else
654                 ecore_main_loop_quit();
655
656         return 0;
657 }
658
659 int main(int argc, char **argv)
660 {
661         if (argc < 3) {
662                 print_usage(argv[0]);
663                 exit(0);
664         }
665
666         ecore_init();
667
668         cmd = argv[1];
669         gargc = argc;
670         gargv = argv;
671         apn_pid = atoi(argv[2]);
672
673         aul_launch_init(NULL, NULL);
674
675         /*aul_listen_app_dead_signal(dead_tracker,NULL); */
676         /*aul_listen_app_dead_signal(NULL,NULL); */
677
678         ecore_idler_add(run_func, NULL);
679
680         ecore_main_loop_begin();
681
682         return 0;
683 }
684
685 /* vi: set ts=8 sts=8 sw=8: */
686