1. support alias id
[platform/core/appfw/app-svc.git] / test / appsvc_test.c
1 /*
2  *  app-svc
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 #include <poll.h>
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <Ecore.h>
27 #include <aul.h>
28
29 #include "appsvc.h"
30
31
32 static char** gargv;
33 static int gargc;
34 static char* cmd;
35 static int apn_pid;
36
37
38 typedef struct _test_func_t{
39         char* name;
40         int(*func)();
41         char* desc;
42         char* usage;
43 }test_func_t;
44
45 void __set_bundle_from_args(bundle * kb)
46 {
47         int opt;
48         char *op = NULL;
49         char *mime = NULL;
50         char *uri = NULL;
51         char *package = NULL;
52         char *category = NULL;
53         char* key = NULL;
54         char* val_array[128];
55         
56         while( (opt = getopt(gargc,gargv,"d:o:m:u:p:c:")) != -1){
57                 switch(opt) {
58                         case 'o':
59                                 if(optarg)
60                                         op = strdup(optarg);
61                                 break;
62                         case 'm':
63                                 if(optarg)
64                                         mime = strdup(optarg);
65                                 break;
66                         case 'u':
67                                 if(optarg)
68                                         uri = strdup(optarg);
69                                 break;
70                         case 'p':
71                                 if(optarg)
72                                         package = strdup(optarg);
73                                 break;
74                         case 'c':
75                                 if(optarg)
76                                         category = strdup(optarg);
77                                 break;
78                         case 'd':
79                                 if(optarg){
80                                         int i = 0;
81                                         key = strtok(optarg,",");
82                                         while(1)
83                                         {
84                                                 val_array[i] = strtok(NULL,",");
85                                                 if(val_array[i] == NULL)
86                                                         break;
87                                                 i++;
88                                         }
89                                         if(i==1) 
90                                                 appsvc_add_data(kb, key, val_array[0]);
91                                         else if(i>1) 
92                                                 appsvc_add_data_array(kb, key, (const char **)val_array, i);
93                                 }
94                                 break;
95                 }
96         }               
97
98         if(op) {
99                 appsvc_set_operation(kb,op);
100                 free(op);
101         }
102         if(mime) {
103                 appsvc_set_mime(kb,mime);
104                 free(mime);
105         }
106         if(uri) {
107                 appsvc_set_uri(kb,uri);
108                 free(uri);
109         }
110         if(package) {
111                 appsvc_set_appid(kb,package);
112                 free(package);
113         }
114         if(category) {
115                 appsvc_set_category(kb,category);
116                 free(category);
117         }
118 }
119
120 int run_svc()
121 {
122         int ret;
123         bundle *kb=NULL;
124         kb = bundle_create();
125         if(kb == NULL)
126         {
127                 printf("bundle creation fail\n");
128                 return -1;
129         }
130         printf("[run_svc test]\n");
131
132         __set_bundle_from_args(kb);
133         
134         ret = appsvc_run_service(kb,0,NULL, NULL);
135         
136         if(ret >= 0){
137                 printf("open service success\n");
138                 if(kb)
139                 {
140                         bundle_free(kb); 
141                         kb=NULL;
142                 }
143                 return 0;
144         }
145         else{
146                 printf("open service fail\n");
147                 if(kb)
148                 {
149                         bundle_free(kb); 
150                         kb=NULL;
151                 }
152                 return -1;              
153         }
154 }
155
156 static void prt_recvd_bundle(const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
157 {
158         char **array_val;
159         int array_len;
160         size_t *array_item_size;
161
162         char *val;
163         size_t size;
164         int i;
165         
166         if(bundle_keyval_type_is_array((bundle_keyval_t *)kv) > 0) {
167                 bundle_keyval_get_array_val((bundle_keyval_t *)kv,
168                         (void ***)&array_val, (unsigned int *)&array_len,
169                         &array_item_size);
170                 
171                 for (i=0;i<array_len;i++)
172                 {
173                         printf("recvd - key: %s[%d], value: %s\n", key, i, array_val[i]);
174                 }
175                 
176         } else {
177                 bundle_keyval_get_basic_val((bundle_keyval_t *)kv, (void **)&val, &size);
178                 printf("recvd - key: %s, value: %s\n",key,val);
179         }       
180 }
181
182 static void cb_func(bundle *kb, int request_code, appsvc_result_val result, void *data)
183 {
184         int num;
185         num = (int)data;
186         
187         if(result == APPSVC_RES_CANCEL){
188                 printf("==== %d : canceled(preemptted) my request ===\n",num);
189         }
190         else{
191                 printf("==== %d : result packet === result %d\n",num, (int)result);
192                 //bundle_iterate(kb, prt_recvd_bundle, NULL);
193
194                 bundle_foreach(kb, prt_recvd_bundle, NULL);
195         }       
196
197
198         if(strcmp(cmd,"run_svc_res")==0){
199                 printf("==== end of appsvc_run() ====\n");
200                 ecore_main_loop_quit();
201         }
202 }
203
204 int run_svc_res()
205 {
206         static int num=0;
207         int ret;
208
209         bundle *kb=NULL;
210         kb = bundle_create();
211         if(kb == NULL)
212         {
213                 printf("bundle creation fail\n");
214                 return -1;
215         }
216         
217         printf("[run_svc_res test]\n");
218         
219         __set_bundle_from_args(kb);     
220         
221         ret = appsvc_run_service(kb, 0, cb_func, (void*)num);
222         
223         if(ret >= 0){
224                 printf("open service success\n");
225                 if(kb)
226                 {
227                         bundle_free(kb); 
228                         kb=NULL;
229                 }
230                 return 0;
231         }
232         else{
233                 printf("open service fail\n");
234                 if(kb)
235                 {
236                         bundle_free(kb); 
237                         kb=NULL;
238                 }
239                 return -1;              
240         }
241 }
242
243
244 static test_func_t test_func[] = {
245
246
247         {"run_svc", run_svc, "run_svc test",
248                 "[usage] run_svc -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
249         {"run_svc_res", run_svc_res, "run_svc_res test",
250                 "[usage] run_svc_res -o <OPERATION> [-m <MIME TYPE>] [-u <URI>] [-d \"<key>,<val>\"]..."},
251                 
252 };
253
254 int callfunc(char* testname)
255 {
256         test_func_t *tmp;
257         int res;
258         int i;
259
260         for(i=0; i<sizeof(test_func)/sizeof(test_func_t); i++)
261         {
262                 tmp = &test_func[i];
263                 if(strcmp(testname,tmp->name)==0){
264                         res = tmp->func();
265                         if(strcmp(testname,"all")){
266                                 if(res < 0)
267                                         printf("... test failed\n");
268                                 else
269                                         printf("... test successs ret = %d\n", res);
270                         }
271                 }
272         }
273         return 0;
274 }
275
276
277 void print_usage(char* progname)
278 {
279         test_func_t *tmp;
280         int i;
281         
282         printf("[usage] %s <cmd> ...\n",progname);
283         printf(" - available cmd list\n");
284
285         for(i=0; i<sizeof(test_func)/sizeof(test_func_t); i++)
286         {
287                 tmp = &test_func[i];
288                 printf("\t%s : %s\n",tmp->name, tmp->desc);
289                 printf("\t\t%s\n",tmp->usage);
290         }
291
292 }
293
294
295 static Eina_Bool run_func(void *data)
296 {
297         callfunc(cmd);
298
299         if(strcmp(cmd,"run_svc_res") == 0 )
300                 return 0;
301         else
302                 ecore_main_loop_quit();
303
304         return 0;
305 }
306
307
308 int main(int argc, char** argv)
309 {
310         if(argc < 3){
311                 print_usage(argv[0]);
312                 exit(0);
313         }
314         
315         ecore_init();
316
317         cmd = argv[1];
318         gargc = argc;
319         gargv = argv;
320         apn_pid = atoi(argv[2]);
321         
322         aul_launch_init(NULL,NULL); 
323
324         //aul_listen_app_dead_signal(dead_tracker,NULL);
325         //aul_listen_app_dead_signal(NULL,NULL);
326
327         ecore_idler_add(run_func, NULL);
328         
329         ecore_main_loop_begin();
330                                         
331         return 0;
332 }
333
334