72ed4fd87c6149567c880366689a967cd082cc6d
[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 launch_test_async()
87 {
88         static int num = 0;
89         int ret = 0;
90         bundle *kb = NULL;
91
92         kb = create_internal_bundle(3);
93         if (NULL == kb) {
94                 return -1;
95         }
96         printf("[aul_launch_app %d test] %s \n", num++, gargv[2]);
97
98         ret = aul_launch_app_async(gargv[2], kb);
99
100         if (kb) {
101                 bundle_free(kb);
102                 kb = NULL;
103         }
104         return ret;
105 }
106
107 int dbus_launch_test()
108 {
109         bundle *kb = NULL;
110         int ret = 0;
111
112         kb = create_internal_bundle(3);
113
114         if (NULL == kb) {
115                 return -1;
116         }
117
118         ret = aul_launch_app(gargv[2], kb);
119
120         if (kb) {
121                 bundle_free(kb);
122                 kb = NULL;
123         }
124
125         return ret;
126 }
127
128 static void prt_recvd_bundle(const char *key, const char *value, void *d)
129 {
130         printf("recvd - key: %s, value: %s\n", key, value);
131 }
132
133 static void cb_func(bundle *kb, int is_cancel, void *data)
134 {
135         int num;
136         num = (int)data;
137
138         if (is_cancel) {
139                 printf("==== %d : canceled(preemptted) my request ===\n", num);
140         } else {
141                 printf("==== %d : result packet ===\n", num);
142                 bundle_iterate(kb, prt_recvd_bundle, NULL);
143         }
144
145         if ((strcmp(cmd, "launch_res") == 0)
146             || (strcmp(cmd, "open_svc_res") == 0))
147                 ecore_main_loop_quit();
148 }
149
150 int open_test()
151 {
152         static int num = 0;
153
154         printf("[aul_open_app %d test] %s \n", num++, gargv[2]);
155         return aul_open_app(gargv[2]);
156 }
157
158 int resume_test()
159 {
160         static int num = 0;
161
162         printf("[aul_open_app %d test] %s \n", num++, gargv[2]);
163         return aul_resume_app(gargv[2]);
164 }
165
166 int resume_pid_test()
167 {
168         static int num = 0;
169         printf("[aul_resume_pid %d test] %d \n", num++, apn_pid);
170         return aul_resume_pid(apn_pid);
171 }
172
173 int term_pid_test()
174 {
175         static int num = 0;
176         printf("[aul_term_pid %d test] %d \n", num++, apn_pid);
177         return aul_terminate_pid(apn_pid);
178 }
179
180 int term_pid_without_restart_test()
181 {
182         static int num = 0;
183         printf("[aul_term_pid_without_restart %d test] %d \n", num++, apn_pid);
184         return aul_terminate_pid_without_restart(apn_pid);
185 }
186
187 int term_req_pid_test()
188 {
189         static int num = 0;
190         printf("[aul_subapp_terminate_request_pid %d test] %d \n", num++, apn_pid);
191         return aul_subapp_terminate_request_pid(apn_pid);
192 }
193
194 static test_func_t scn_func[] = {
195         {"n", launch_test, "launch_test", ""},
196         {"n", launch_test, "launch_test", ""},
197         {"n", resume_test, "open_test", ""},
198         {"n", resume_test, "open_test", ""},
199         {"p", resume_pid_test, "resume_pid_test", ""},
200         {"p", resume_pid_test, "resume_pid_test", ""},
201         {"p", term_pid_test, "term_pid_test", ""},
202         {"n", resume_test, "open_test", ""},
203         {"n", launch_test, "launch_test", ""}
204 };
205
206 static Eina_Bool run_all_test(void *data)
207 {
208         static int pos = 0;
209         int ret;
210
211         if (pos > sizeof(scn_func) / sizeof(test_func_t) - 1) {
212                 printf("all internal test done\n");
213                 ecore_main_loop_quit();
214                 return 0;
215         }
216
217         if (strncmp(scn_func[pos].name, "n", 1) == 0) {
218                 printf("[test %d] %s , pkgname = %s\n", pos, scn_func[pos].desc,
219                        gargv[2]);
220                 apn_pid = scn_func[pos].func();
221                 printf("... return pid = %d\n", apn_pid);
222         } else {
223                 printf("[test %d] %s , pid = %d\n", pos, scn_func[pos].desc,
224                        apn_pid);
225                 ret = scn_func[pos].func();
226                 printf("... return res = %d\n", ret);
227         }
228         pos++;
229
230         return 1;
231 }
232
233 int all_test()
234 {
235         ecore_timer_add(2, run_all_test, NULL);
236         return 0;
237 }
238
239 int is_run_test()
240 {
241         if (aul_app_is_running(gargv[2]))
242                 printf("... %s is running\n", gargv[2]);
243         else
244                 printf("... %s is not running\n", gargv[2]);
245
246         return 0;
247 }
248
249 int iterfunc(const aul_app_info *info, void *data)
250 {
251         printf("\t==========================\n");
252         printf("\t pkg_name: %s\n", info->appid);
253         printf("\t app_path: %s\n", info->app_path);
254         printf("\t running pid: %d\n", info->pid);
255         printf("\t==========================\n");
256
257         return 0;
258 }
259
260 int get_allpkg_test()
261 {
262         static int num = 0;
263         printf("[aul_app_get_ruuning_app_info %d test] \n", num++);
264         return aul_app_get_running_app_info(iterfunc, NULL);
265 }
266
267 int get_pkgpid_test()
268 {
269         int pid = 0;
270         static int num = 0;
271         char buf[MAX_LOCAL_BUFSZ];
272
273         printf("[aul_app_get_appid_bypid %d test] \n", num++);
274         pid = atoi(gargv[2]);
275
276         if (aul_app_get_appid_bypid(pid, buf, sizeof(buf)) < 0)
277                 printf("no such pkg by %d\n", pid);
278         else
279                 printf("pkgname = %s, pid = %d\n", buf, pid);
280
281         return 0;
282 }
283
284 int get_mime_file_test()
285 {
286         static int num = 0;
287         int ret;
288         char buf[MAX_LOCAL_BUFSZ];
289         printf("[aul_get_mime_from_file %d test] %s \n", num++, gargv[2]);
290         ret = aul_get_mime_from_file(gargv[2], buf, sizeof(buf));
291         if (ret >= 0)
292                 printf("==> mime type = %s\n", buf);
293         return ret;
294 }
295
296 int get_mime_content_test()
297 {
298         static int num = 0;
299         int ret;
300         char buf[MAX_LOCAL_BUFSZ];
301         printf("[aul_get_mime_from_content %d test] %s \n", num++, gargv[2]);
302         ret = aul_get_mime_from_content(gargv[2], buf, sizeof(buf));
303         if (ret >= 0)
304                 printf("==> mime type = %s\n", buf);
305         return ret;
306 }
307
308 int aul_get_mime_icon_test()
309 {
310         int ret;
311         char buf[MAX_LOCAL_BUFSZ];
312         ret = aul_get_mime_icon(gargv[2], buf, sizeof(buf));
313         if (ret >= 0)
314                 printf("==> mimetype %s : iconname = %s\n", gargv[2], buf);
315         return ret;
316 }
317
318 int aul_get_mime_description_test()
319 {
320         int ret;
321         char buf[MAX_LOCAL_BUFSZ];
322         ret = aul_get_mime_description(gargv[2], buf, sizeof(buf));
323         if (ret >= 0)
324                 printf("==> mimetype %s : description = %s\n", gargv[2], buf);
325         return ret;
326 }
327
328 int aul_get_mime_extension_test()
329 {
330         int ret;
331         char buf[MAX_LOCAL_BUFSZ];
332         ret = aul_get_mime_extension(gargv[2], buf, sizeof(buf));
333         if (ret >= 0)
334                 printf("==> mimetype %s : extension = %s\n", gargv[2], buf);
335         return ret;
336 }
337
338 static void print_menu_db_info(const app_info_from_db *info)
339 {
340         if (info == NULL) {
341                 printf("pkg %s no found\n", gargv[2]);
342                 return;
343         }
344
345         printf("\t==========================\n");
346         printf("\t pkg_name: %s\n", info->pkg_name);
347         printf("\t app_path: %s\n", info->app_path);
348         printf("\t is_minst: %d\n", 0);
349         printf("\t==========================\n");
350 }
351
352 static int get_pkg_func()
353 {
354         app_info_from_db *info;
355
356         info = _get_app_info_from_db_by_pkgname(gargv[2]);
357         print_menu_db_info(info);
358         if (info)
359                 _free_app_info_from_db(info);
360
361         return 0;
362 }
363
364 static int update_running_list()
365 {
366         aul_running_list_update(gargv[2], gargv[3], gargv[4]);
367
368         return 0;
369 }
370
371
372 /*
373 static int set_pkg_func()
374 {
375         char* pkgname;
376         char* apppath;
377         char* appname;
378         char query[QUERY_LEN];
379
380         pkgname = gargv[2];
381         apppath = gargv[3];
382
383         appname = strrchr(apppath,'/')+1;
384         snprintf(ai.app_icon_path, PATH_LEN, "aul_test_icon_path/%d",getpid());
385         snprintf(ai.desktop_path, PATH_LEN,
386                 "aul_test_desktop_path/%d",getpid());
387
388         snprintf (query, sizeof(query), "insert into "TABLE_MENU"(\
389         pkg_name,\
390         app_path,\
391         app_name,\
392         app_icon_path,\
393         desktop_path)\
394         values ('%s', '%s', '%s', '%s', '%s')",
395         pkgname,
396         apppath,
397         appname,
398         record->app_icon_path,
399         record->desktop_path,
400         );
401
402         // TODO: record_add is not supported anymore; use AIL
403         if (record_add(ADD_ICON, &ai)){
404                 printf("set pkg success\n");
405                 return 0;
406         }
407         else{
408                 printf("set pkg fail\n");
409                 return -1;
410         }
411 }
412
413 static int del_pkg_func()
414 {
415         app_info ai;
416
417         memset(&ai, 0, sizeof(app_info));
418         snprintf(ai.pkg_name, NAME_LEN, "%s", gargv[2]);
419
420         // TODO: record_add is not supported anymore; use AIL
421         if(record_delete(DELETE_MENU, &ai)){
422                 printf("del pkg success\n");
423                 return 0;
424         }
425         else {
426                 printf("del pkg fail\n");
427                 return -1;
428         }
429 }
430 */
431
432 static int test_regex()
433 {
434         char *token;
435         char mime[MAX_LOCAL_BUFSZ];
436         char *saveptr;
437
438         INIT_PERF(NULL);
439
440         printf("=======================\n");
441
442         token = strtok_r(gargv[2], " \t\n,.()", &saveptr);
443         if (aul_get_mime_from_content(token, mime, sizeof(mime)) == AUL_R_OK)
444                 printf("found %s %s\n", mime, token);
445
446         while (token) {
447                 token = strtok_r(NULL, " \t\n,()", &saveptr);
448                 if (aul_get_mime_from_content(token, mime, sizeof(mime)) ==
449                     AUL_R_OK)
450                         printf("found %s %s\n", mime, token);
451         }
452
453         PERF("======= parse end =====\n");
454         return 0;
455 }
456
457 static test_func_t test_func[] = {
458         {"launch",launch_test,"aul_launch_app test",
459                 "[usage] launch <pkgname> <key1> <val1> <key2> <val2> ..."},
460         {"launch_async",launch_test_async,"aul_launch_app test",
461                 "[usage] launch_async <pkgname> <key1> <val1> <key2> <val2> ..."},
462         {"open",open_test,"aul_open_app test",
463                 "[usage] open <pkgname>" },
464         {"resume",resume_test,"aul_resume_app test",
465                 "[usage] resume <pkgname>" },
466         {"resume_pid",resume_pid_test,"aul_resume_pid test",
467                 "[usage] resume_pid <pid>" },
468         {"term_pid", term_pid_test,"aul_terminate_pid test",
469                 "[usage] term_pid <pid>" },
470         {"term_pid_without_restart", term_pid_without_restart_test,"aul_terminate_pid_without_restart test",
471                 "[usage] term_pid_without_restart <pid>" },
472         {"term_req_pid", term_req_pid_test,"aul_subapp_terminate_request_pid test",
473                 "[usage] term_req_pid <pid>" },
474         {"dbuslaunch", dbus_launch_test,"launch by dbus auto activation",
475                 "[usage] term_pid <pid>" },
476         {"all",all_test,"test based on predefine scenario",
477                 "[usage] all <pkgname>"},
478
479         {"is_run", is_run_test,"aul_is_running test",
480                 "[usage] is_run <pkgname>"},
481         {"getallpkg", get_allpkg_test, "aul_app_get_running_app_info test",
482                 "[usage] getallpkg all"},
483         {"getpkgpid", get_pkgpid_test, "aul_app_get_appid_bypid test",
484                 "[usage] getpkgpid <pid>"},
485
486         {"get_mime_file", get_mime_file_test, "aul_get_mime_from_file test",
487                 "[usage] get_mime_file <filename>"},
488         {"get_mime_content", get_mime_content_test, "aul_get_mime_from_content",
489                 "[usage] get_mime_content <content>"},
490
491         {"get_mime_icon", aul_get_mime_icon_test, "aul_get_mime_icon test",
492                 "[usage] get_mime_icon <mimetype>"},
493         {"get_mime_desc", aul_get_mime_description_test, "aul_get_mime_description test",
494                 "[usage] get_mime_desc <mimetype>"},
495         {"get_mime_ext", aul_get_mime_extension_test, "aul_get_mime_extension test",
496                 "[usage] get_mime_ext <mimetype>"},
497
498         {"test_regex", test_regex, "regular expression parser test",
499                 "[usage] test_regex <full text>"},
500
501         {"getpkg", get_pkg_func, "get package",
502                 "[usage] getpkg <pkgname>"},
503         {"update_list", update_running_list, "update running list",
504                 "[usage] update_list <appid> <app_path> <pid>"},
505 /*
506         {"setpkg", set_pkg_func, "set package",
507                 "[usage] setpkg <pkgname> <apppath>"},
508         {"delpkg", del_pkg_func, "del package",
509                 "[usage] getpkg <pkgname>"},
510 */
511 };
512
513 int callfunc(char *testname)
514 {
515         test_func_t *tmp;
516         int res;
517         int i;
518
519         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
520                 tmp = &test_func[i];
521                 if (strcmp(testname, tmp->name) == 0) {
522                         res = tmp->func();
523                         if (strcmp(testname, "all")) {
524                                 if (res < 0)
525                                         printf("... test failed\n");
526                                 else
527                                         printf("... test successs ret = %d\n",
528                                                res);
529                         }
530                 }
531         }
532         return 0;
533 }
534
535 int dead_tracker(int pid, void *data)
536 {
537         printf("[DEAD] pid = %d dead\n", pid);
538         return 0;
539 }
540
541 void print_usage(char *progname)
542 {
543         test_func_t *tmp;
544         int i;
545
546         printf("[usage] %s <cmd> ...\n", progname);
547         printf(" - available cmd list\n");
548
549         for (i = 0; i < sizeof(test_func) / sizeof(test_func_t); i++) {
550                 tmp = &test_func[i];
551                 printf("\t%s : %s\n", tmp->name, tmp->desc);
552                 printf("\t\t%s\n", tmp->usage);
553         }
554
555         printf("[note] getpkg/setpkg/delpkg/init_defapp_mime "
556                 "cmd is internal purpose\n");
557 }
558
559 static Eina_Bool run_func(void *data)
560 {
561         callfunc(cmd);
562
563         if (strcmp(cmd, "launch_res") == 0 || strcmp(cmd, "all") == 0
564             || strcmp(cmd, "dbuslaunch") == 0
565             || strcmp(cmd, "open_svc_res") == 0)
566                 return 0;
567         else
568                 ecore_main_loop_quit();
569
570         return 0;
571 }
572
573 int main(int argc, char **argv)
574 {
575         if (argc < 3) {
576                 print_usage(argv[0]);
577                 exit(0);
578         }
579
580         ecore_init();
581
582         cmd = argv[1];
583         gargc = argc;
584         gargv = argv;
585         apn_pid = atoi(argv[2]);
586
587         aul_launch_init(NULL, NULL);
588
589         /*aul_listen_app_dead_signal(dead_tracker,NULL); */
590         /*aul_listen_app_dead_signal(NULL,NULL); */
591
592         ecore_idler_add(run_func, NULL);
593
594         ecore_main_loop_begin();
595
596         return 0;
597 }
598
599 /* vi: set ts=8 sts=8 sw=8: */
600