1. apply systemd
[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 static int update_running_list()
366 {
367         aul_running_list_update(gargv[2], gargv[3], gargv[4]);
368
369         return 0;
370 }
371
372
373 /*
374 static int set_pkg_func()
375 {
376         char* pkgname;
377         char* apppath;
378         char* appname;
379         char query[QUERY_LEN];
380
381         pkgname = gargv[2];
382         apppath = gargv[3];
383         
384         appname = strrchr(apppath,'/')+1;
385         snprintf(ai.app_icon_path, PATH_LEN, "aul_test_icon_path/%d",getpid());
386         snprintf(ai.desktop_path, PATH_LEN, 
387                 "aul_test_desktop_path/%d",getpid());
388
389         snprintf (query, sizeof(query), "insert into "TABLE_MENU"(\
390         pkg_name,\
391         app_path,\
392         app_name,\
393         app_icon_path,\
394         desktop_path)\
395         values ('%s', '%s', '%s', '%s', '%s')",
396         pkgname,
397         apppath,
398         appname,
399         record->app_icon_path,
400         record->desktop_path,
401         );
402
403         // TODO: record_add is not supported anymore; use AIL
404         if (record_add(ADD_ICON, &ai)){
405                 printf("set pkg success\n");
406                 return 0;
407         }
408         else{
409                 printf("set pkg fail\n");
410                 return -1;
411         }
412 }
413
414 static int del_pkg_func()
415 {
416         app_info ai;
417
418         memset(&ai, 0, sizeof(app_info));
419         snprintf(ai.pkg_name, NAME_LEN, "%s", gargv[2]);
420
421         // TODO: record_add is not supported anymore; use AIL
422         if(record_delete(DELETE_MENU, &ai)){
423                 printf("del pkg success\n");
424                 return 0;
425         }
426         else {
427                 printf("del pkg fail\n");
428                 return -1;
429         }
430 }
431 */
432
433 static int test_regex()
434 {
435         char *token;
436         char mime[MAX_LOCAL_BUFSZ];
437         char *saveptr;
438
439         INIT_PERF(NULL);
440
441         printf("=======================\n");
442
443         token = strtok_r(gargv[2], " \t\n,.()", &saveptr);
444         if (aul_get_mime_from_content(token, mime, sizeof(mime)) == AUL_R_OK)
445                 printf("found %s %s\n", mime, token);
446
447         while (token) {
448                 token = strtok_r(NULL, " \t\n,()", &saveptr);
449                 if (aul_get_mime_from_content(token, mime, sizeof(mime)) ==
450                     AUL_R_OK)
451                         printf("found %s %s\n", mime, token);
452         }
453
454         PERF("======= parse end =====\n");
455         return 0;
456 }
457
458 int open_svc_test()
459 {
460         static int num = 0;
461         int ret;
462
463         bundle *kb = NULL;
464         kb = create_internal_bundle(3);
465         if (kb == NULL) {
466                 printf("bundle creation fail\n");
467                 return -1;
468         }
469         printf("[aul_open_service %d test] %s \n", num++, gargv[2]);
470         ret = aul_open_service(gargv[2], kb, NULL, NULL);
471         if (ret >= 0) {
472                 printf("open service success\n");
473                 if (kb) {
474                         bundle_free(kb);
475                         kb = NULL;
476                 }
477                 return 0;
478         } else {
479                 printf("open service fail\n");
480                 if (kb) {
481                         bundle_free(kb);
482                         kb = NULL;
483                 }
484                 return -1;
485         }
486 }
487
488 int open_svc_res_test()
489 {
490         static int num = 0;
491         int ret;
492
493         bundle *kb = NULL;
494         kb = create_internal_bundle(3);
495         if (kb == NULL) {       /* Prevent Fix: ID: 21027,21581 */
496                 printf("bundle creation fail\n");
497                 return -1;
498         }
499
500         printf("[aul_open_service(wait result) %d test] %s \n", num++,
501                gargv[2]);
502         ret = aul_open_service(gargv[2], kb, cb_func, (void *)num);
503         if (ret >= 0) {
504                 printf("open service(wait result) success\n");
505                 if (kb) {
506                         bundle_free(kb);
507                         kb = NULL;
508                 }
509                 return 0;
510         } else {
511                 printf("open service(wait result) fail\n");
512                 if (kb) {
513                         bundle_free(kb);
514                         kb = NULL;
515                 }
516                 return -1;
517         }
518 }
519
520 int get_defapp_svc_test()
521 {
522         static int num = 0;
523         int ret;
524         char buf[MAX_LOCAL_BUFSZ];
525         printf("[aul_get_defapp_from_svc %d test] %s \n", num++, gargv[2]);
526         ret = aul_get_defapp_for_service(gargv[2], buf, sizeof(buf));
527         if (ret >= 0)
528                 printf("==> defapp name = %s\n", buf);
529         return ret;
530 }
531
532 int set_defapp_svc_test()
533 {
534         static int num = 0;
535         int ret;
536         printf("[aul_set_defapp_with_svc %d test] %s %s\n", num++, gargv[2],
537                gargv[3]);
538         ret = aul_set_defapp_for_service(gargv[2], gargv[3]);
539         return ret;
540 }
541
542 static test_func_t test_func[] = {
543         {"launch",launch_test,"aul_launch_app test",
544                 "[usage] launch <pkgname> <key1> <val1> <key2> <val2> ..."},
545         {"open",open_test,"aul_open_app test",
546                 "[usage] open <pkgname>" },
547         {"resume",resume_test,"aul_resume_app test",
548                 "[usage] resume <pkgname>" },
549         {"resume_pid",resume_pid_test,"aul_resume_pid test",
550                 "[usage] resume_pid <pid>" },
551         {"term_pid", term_pid_test,"aul_terminate_pid test",
552                 "[usage] term_pid <pid>" },
553         {"dbuslaunch", dbus_launch_test,"launch by dbus auto activation",
554                 "[usage] term_pid <pid>" },
555         {"all",all_test,"test based on predefine scenario",
556                 "[usage] all <pkgname>"},
557
558         {"is_run", is_run_test,"aul_is_running test",
559                 "[usage] is_run <pkgname>"},
560         {"getallpkg", get_allpkg_test, "aul_app_get_running_app_info test",
561                 "[usage] getallpkg all"},
562         {"getpkgpid", get_pkgpid_test, "aul_app_get_appid_bypid test",
563                 "[usage] getpkgpid <pid>"},
564         
565         {"open_file", open_file_test, "aul_open_file test",
566                 "[usage] open_file <filename>"},
567         {"open_content", open_content_test, "aul_open_content test",
568                 "[usage] open_content <content>"},
569         {"get_defapp_mime", get_defapp_test, "aul_get_defapp_from_mime test",
570                 "[usage] get_defapp_mime <mime_type>"},
571         {"set_defapp_mime", set_defapp_test, "aul_set_defapp_with_mime test",
572                 "[usage] set_defapp_mime <mime_type> <defapp to be set>"},
573         {"get_mime_file", get_mime_file_test, "aul_get_mime_from_file test",
574                 "[usage] get_mime_file <filename>"},
575         {"get_mime_content", get_mime_content_test, "aul_get_mime_from_content",
576                 "[usage] get_mime_content <content>"},
577
578         {"get_mime_icon", aul_get_mime_icon_test, "aul_get_mime_icon test",
579                 "[usage] get_mime_icon <mimetype>"},
580         {"get_mime_desc", aul_get_mime_description_test, "aul_get_mime_description test",
581                 "[usage] get_mime_desc <mimetype>"},
582         {"get_mime_ext", aul_get_mime_extension_test, "aul_get_mime_extension test",
583                 "[usage] get_mime_ext <mimetype>"},
584
585         {"test_regex", test_regex, "regular expression parser test",
586                 "[usage] test_regex <full text>"},
587
588         {"open_svc", open_svc_test, "aul_open_service test"
589                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
590         {"open_svc_res", open_svc_res_test, "aul_open_service (wait result) test"
591                 "[usage] open_svc <svcname> <key1> <val1> <key2> <val2> ..."},
592         {"set_defapp_svc", set_defapp_svc_test, "aul_set_defapp_with_svc test"
593                 "[usage] set_defapp_svc <svcname> <defapp to be set>"},
594         {"get_defapp_svc", get_defapp_svc_test, "aul_get_defapp_from_svc test"
595                 "[usage] get_defapp_svc <svcname>"},
596         
597         {"getpkg", get_pkg_func, "get package",
598                 "[usage] getpkg <pkgname>"},
599         {"update_list", update_running_list, "update running list",
600                 "[usage] update_list <appid> <app_path> <pid>"},
601 /*
602         {"setpkg", set_pkg_func, "set package",
603                 "[usage] setpkg <pkgname> <apppath>"},
604         {"delpkg", del_pkg_func, "del package",
605                 "[usage] getpkg <pkgname>"},
606 */
607 };
608
609 int callfunc(char *testname)
610 {
611         test_func_t *tmp;
612         int res;
613         int i;
614
615         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
616                 tmp = &test_func[i];
617                 if (strcmp(testname, tmp->name) == 0) {
618                         res = tmp->func();
619                         if (strcmp(testname, "all")) {
620                                 if (res < 0)
621                                         printf("... test failed\n");
622                                 else
623                                         printf("... test successs ret = %d\n",
624                                                res);
625                         }
626                 }
627         }
628         return 0;
629 }
630
631 int dead_tracker(int pid, void *data)
632 {
633         printf("[DEAD] pid = %d dead\n", pid);
634         return 0;
635 }
636
637 void print_usage(char *progname)
638 {
639         test_func_t *tmp;
640         int i;
641
642         printf("[usage] %s <cmd> ...\n", progname);
643         printf(" - available cmd list\n");
644
645         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
646                 tmp = &test_func[i];
647                 printf("\t%s : %s\n", tmp->name, tmp->desc);
648                 printf("\t\t%s\n", tmp->usage);
649         }
650
651         printf("[note] getpkg/setpkg/delpkg/init_defapp_mime "
652                 "cmd is internal purpose\n");
653 }
654
655 static Eina_Bool run_func(void *data)
656 {
657         callfunc(cmd);
658
659         if (strcmp(cmd, "launch_res") == 0 || strcmp(cmd, "all") == 0
660             || strcmp(cmd, "dbuslaunch") == 0
661             || strcmp(cmd, "open_svc_res") == 0)
662                 return 0;
663         else
664                 ecore_main_loop_quit();
665
666         return 0;
667 }
668
669 int main(int argc, char **argv)
670 {
671         if (argc < 3) {
672                 print_usage(argv[0]);
673                 exit(0);
674         }
675
676         ecore_init();
677
678         cmd = argv[1];
679         gargc = argc;
680         gargv = argv;
681         apn_pid = atoi(argv[2]);
682
683         aul_launch_init(NULL, NULL);
684
685         /*aul_listen_app_dead_signal(dead_tracker,NULL); */
686         /*aul_listen_app_dead_signal(NULL,NULL); */
687
688         ecore_idler_add(run_func, NULL);
689
690         ecore_main_loop_begin();
691
692         return 0;
693 }
694
695 /* vi: set ts=8 sts=8 sw=8: */
696