Update test console app
[platform/core/api/maps-service.git] / test / maps_test_search_place.c
1 #include "maps_test.h"
2 #include "maps_test_search_place.h"
3 #include "maps_test_log.h"
4 #include "maps_test_util.h"
5
6 static maps_place_filter_h maps_place_filter = NULL;
7 static int ref_cnt = 0;
8 static int unlimited_cnt = 0;
9
10 static void startup()
11 {
12         unlimited_cnt = 0;
13         if (ref_cnt++) return;
14
15         if (maps_place_filter) return;
16         maps_preference_set_max_results(maps_pref, 5);
17         maps_preference_set_language(maps_pref, "ko-KR");
18         maps_place_filter_create(&maps_place_filter);
19 }
20
21 static void cleanup()
22 {
23         unlimited_cnt = 0;
24         if (--ref_cnt > 0) return;
25
26         if (!maps_place_filter) return;
27         maps_place_filter_destroy(maps_place_filter);
28         maps_place_filter = NULL;
29 }
30
31 static void cleanup_callback(int error)
32 {
33         print_result(error);
34         cleanup();
35 }
36
37
38
39
40 /*
41  *
42  *      callback functions for APIs
43  *
44  */
45
46 static bool __maps_place_attribute_cb(int index, int total, maps_place_attribute_h attribute, void *user_data)
47 {
48         if (!attribute) return false;
49
50         LOG_VARIABLES();
51         LOG_START("attributes [%d/%d] = {", MIN(index+1, total), total);
52
53         if (maps_place_attribute_get_id(attribute, &lv.strval) == MAPS_ERROR_NONE)
54                 LOG_PRINT("%sid = %s", LOG_PREFIX, lv.strval);
55
56         if (maps_place_attribute_get_label(attribute, &lv.strval) == MAPS_ERROR_NONE)
57                 LOG_PRINT("%slabel = %s", LOG_PREFIX, lv.strval);
58
59         if (maps_place_attribute_get_text(attribute, &lv.strval) == MAPS_ERROR_NONE)
60                 LOG_PRINT("%stext = %s", LOG_PREFIX, lv.strval);
61
62         maps_place_attribute_destroy(attribute);
63
64         LOG_FINISH("}");
65         return true;
66 }
67
68 static bool __maps_place_categories_cb(int index, int total, maps_place_category_h category, void *user_data)
69 {
70         if (!category) return false;
71         LOG_VARIABLES();
72         LOG_START("categories [%d/%d] = {", MIN(index+1, total), total);
73
74         if (maps_place_category_get_name(category, &lv.strval) == MAPS_ERROR_NONE)
75                 LOG_PRINT("%sname = %s", LOG_PREFIX, lv.strval);
76
77         if (maps_place_category_get_id(category, &lv.strval) == MAPS_ERROR_NONE)
78                 LOG_PRINT("%sid = %s", LOG_PREFIX, lv.strval);
79
80         if (maps_place_category_get_url(category, &lv.strval) == MAPS_ERROR_NONE)
81                 LOG_PRINT("%surl = %s", LOG_PREFIX, lv.strval);
82
83         maps_place_category_destroy(category);
84
85         LOG_FINISH("}");
86         return true;
87 }
88
89 static bool __maps_place_contacts_cb(int index, int total, maps_place_contact_h contact, void *user_data)
90 {
91         if (!contact) return false;
92         LOG_VARIABLES();
93         LOG_START("contacts [%d/%d] = {", MIN(index+1, total), total);
94
95         if (maps_place_contact_get_label(contact, &lv.strval) == MAPS_ERROR_NONE)
96                 LOG_PRINT("%slabel = %s", LOG_PREFIX, lv.strval);
97
98         if (maps_place_contact_get_type(contact, &lv.strval) == MAPS_ERROR_NONE)
99                 LOG_PRINT("%stype = %s", LOG_PREFIX, lv.strval);
100
101         if (maps_place_contact_get_value(contact, &lv.strval) == MAPS_ERROR_NONE)
102                 LOG_PRINT("%svalue = %s", LOG_PREFIX, lv.strval);
103
104         maps_place_contact_destroy(contact);
105
106         LOG_FINISH("}");
107         return true;
108 }
109
110 static bool __maps_place_editorials_cb(int index, int total, maps_place_editorial_h editorial, void *user_data)
111 {
112         if (!editorial) return false;
113         LOG_VARIABLES();
114         LOG_START("editorial [%d/%d] = {", MIN(index+1, total), total);
115
116         if (maps_place_editorial_get_description(editorial, &lv.strval) == MAPS_ERROR_NONE)
117                 LOG_PRINT("%sdescription = %s", LOG_PREFIX, lv.strval);
118
119         if (maps_place_editorial_get_language(editorial, &lv.strval) == MAPS_ERROR_NONE)
120                 LOG_PRINT("%slanguage = %s", LOG_PREFIX, lv.strval);
121
122         maps_place_media_h media = NULL;
123         if (maps_place_editorial_get_media(editorial, &media) == MAPS_ERROR_NONE) {
124                 log_place_media("       ", media);
125                 maps_place_media_destroy(media);
126         }
127
128         maps_place_editorial_destroy(editorial);
129
130         LOG_FINISH("}");
131         return true;
132 }
133
134 static bool __maps_place_images_cb(int index, int total, maps_place_image_h image, void *user_data)
135 {
136         if (!image) return false;
137         LOG_VARIABLES();
138         LOG_START("image [%d/%d] = {", MIN(index+1, total), total);
139
140         if (maps_place_image_get_id(image, &lv.strval) == MAPS_ERROR_NONE)
141                 LOG_PRINT("%sid = %s", LOG_PREFIX, lv.strval);
142
143         if (maps_place_image_get_url(image, &lv.strval) == MAPS_ERROR_NONE)
144                 LOG_PRINT("%surl = %s", LOG_PREFIX, lv.strval);
145
146         if (maps_place_image_get_width(image, &lv.intval) == MAPS_ERROR_NONE)
147                 LOG_PRINT("%swidth = %d", LOG_PREFIX, lv.intval);
148
149         if (maps_place_image_get_height(image, &lv.intval) == MAPS_ERROR_NONE)
150                 LOG_PRINT("%sheight = %d", LOG_PREFIX, lv.intval);
151
152         maps_place_link_object_h user_link = NULL;
153         if (maps_place_image_get_user_link(image, &user_link) == MAPS_ERROR_NONE) {
154                 log_place_link_object(LOG_PREFIX, "user link", user_link);
155                 maps_place_link_object_destroy(user_link);
156         }
157
158         maps_place_media_h media = NULL;
159         if (maps_place_image_get_media(image, &media) == MAPS_ERROR_NONE) {
160                 log_place_media(LOG_PREFIX, media);
161                 maps_place_media_destroy(media);
162         }
163
164         maps_place_image_destroy(image);
165
166         LOG_FINISH("}");
167         return true;
168 }
169
170 static bool __maps_place_properties_cb(int index, int total, char* key, void* value, void* user_data)
171 {
172         LOG_VARIABLES();
173         LOG_START("properties [%d/%d] %s = %s", MIN(index+1, total), total, (char*)key, (char*)value);
174         LOG_FINISH("");
175         return true;
176 }
177
178 static bool __maps_place_reviews_cb(int index, int total, maps_place_review_h review, void *user_data)
179 {
180         if (!review) return false;
181         LOG_VARIABLES();
182         LOG_START("review [%d/%d] {", MIN(index+1, total), total);
183
184         if (maps_place_review_get_date(review, &lv.strval) == MAPS_ERROR_NONE)
185                 LOG_PRINT("%sdate = %s", LOG_PREFIX, lv.strval);
186
187         if (maps_place_review_get_title(review, &lv.strval) == MAPS_ERROR_NONE)
188                 LOG_PRINT("%stitle = %s", LOG_PREFIX, lv.strval);
189
190         if (maps_place_review_get_rating(review, &lv.dblval) == MAPS_ERROR_NONE)
191                 LOG_PRINT("%srating = %.2f", LOG_PREFIX, lv.dblval);
192
193         if (maps_place_review_get_description(review, &lv.strval) == MAPS_ERROR_NONE)
194                 LOG_PRINT("%sdescription = %s", LOG_PREFIX, lv.strval);
195
196         maps_place_media_h media = NULL;
197         if (maps_place_review_get_media(review, &media) == MAPS_ERROR_NONE) {
198                 log_place_media(LOG_PREFIX, media);
199                 maps_place_media_destroy(media);
200         }
201
202         maps_place_link_object_h user_link = NULL;
203         if (maps_place_review_get_user_link(review, &user_link) == MAPS_ERROR_NONE) {
204                 log_place_link_object(LOG_PREFIX, "user link", user_link);
205                 maps_place_link_object_destroy(user_link);
206         }
207
208         maps_place_review_destroy(review);
209
210         LOG_FINISH("}");
211         return true;
212 }
213
214 static bool __log_place(char *prefix, maps_place_h place)
215 {
216         LOG_VARIABLES();
217         LOG_START("");
218
219         if (maps_place_get_id(place, &lv.strval) == MAPS_ERROR_NONE)
220                 LOG_PRINT("%sid = %s", prefix, lv.strval);
221
222         if (maps_place_get_name(place, &lv.strval) == MAPS_ERROR_NONE)
223                 LOG_PRINT("%sname = %s", prefix, lv.strval);
224
225         if (maps_place_get_uri(place, &lv.strval) == MAPS_ERROR_NONE)
226                 LOG_PRINT("%suri = %s", prefix, lv.strval);
227
228         maps_coordinates_h coord = NULL;
229         if (maps_place_get_location(place, &coord) == MAPS_ERROR_NONE) {
230                 double lat, lng;
231                 maps_coordinates_get_latitude(coord, &lat);
232                 maps_coordinates_get_longitude(coord, &lng);
233                 LOG_PRINT("%slocation = %f, %f", prefix, lat, lng);
234                 maps_coordinates_destroy(coord);
235         }
236
237         int distance = 0;
238         maps_distance_unit_e distance_unit;
239         if (maps_place_get_distance(place, &distance) == MAPS_ERROR_NONE) {
240                 maps_preference_get_distance_unit(maps_pref, &distance_unit);
241                 LOG_PRINT("%sdistance = %d %s", prefix, distance, get_distance_unit_string(distance_unit));
242         }
243
244         maps_address_h address = NULL;
245         if (maps_place_get_address(place, &address) == MAPS_ERROR_NONE) {
246                 log_address(prefix, address);
247                 maps_address_destroy(address);
248         }
249
250         maps_place_rating_h rating = NULL;
251         if (maps_place_get_rating(place, &rating) == MAPS_ERROR_NONE && rating) {
252                 log_place_rating(prefix, rating);
253                 maps_place_rating_destroy(rating);
254         }
255
256         int ret = maps_place_foreach_property(place, __maps_place_properties_cb, NULL);
257         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
258                 LOG_PRINT("%sproperties : error (%d)", prefix, ret);
259
260         ret = maps_place_foreach_category(place, __maps_place_categories_cb, NULL);
261         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
262                 LOG_PRINT("%scategories : error (%d)", prefix, ret);
263
264         ret = maps_place_foreach_attribute(place, __maps_place_attribute_cb, NULL);
265         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
266                 LOG_PRINT("%sattributes : error (%d)", prefix, ret);
267
268         ret = maps_place_foreach_contact(place, __maps_place_contacts_cb, NULL);
269         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
270                 LOG_PRINT("%scontacts : error (%d)", prefix, ret);
271
272         ret = maps_place_foreach_editorial(place, __maps_place_editorials_cb, NULL);
273         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
274                 LOG_PRINT("%seditorials : error (%d)", prefix, ret);
275
276         ret = maps_place_foreach_image(place, __maps_place_images_cb, NULL);
277         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
278                 LOG_PRINT("%simages : error (%d)", prefix, ret);
279
280         ret = maps_place_foreach_review(place, __maps_place_reviews_cb, NULL);
281         if (ret != MAPS_ERROR_NONE && ret != MAPS_ERROR_NOT_FOUND)
282                 LOG_PRINT("%sreviews : error (%d)", prefix, ret);
283
284         LOG_FINISH("");
285         return true;
286 }
287
288 static bool __maps_service_search_place_cb(maps_error_e error, int request_id , int index, int total, maps_place_h place, void* user_data)
289 {
290         LOG_VARIABLES();
291         LOG_START_TITLE("%s [%d/%d] ", (user_data ? (char*)user_data : __FUNCTION__), MIN(index+1, total), total);
292
293         if (error) {
294                 cleanup_callback(error);
295                 return false;
296         }
297
298         __log_place("", place);
299         maps_place_destroy(place);
300
301         LOG_FINISH("");
302
303         if (index == total - 1)
304                 cleanup_callback(error);
305         return true;
306 }
307
308
309 static bool __maps_place_cb(int index, maps_place_h place, void *user_data)
310 {
311         LOG_VARIABLES();
312         LOG_START_TITLE("%s [%d] ", (user_data ? (char*)user_data : __FUNCTION__), index+1);
313
314         __log_place("", place);
315         maps_place_destroy(place);
316
317         LOG_FINISH("");
318         return true;
319 }
320
321 static void __maps_service_search_place_list_cb(maps_error_e error,
322                  int request_id, int total, maps_place_list_h place_list, void *user_data)
323 {
324         if (error) {
325                 print_result(error);
326                 cleanup_callback(error);
327                 return;
328         }
329         maps_place_list_foreach(place_list, __maps_place_cb, user_data);
330         cleanup_callback(error);
331 }
332
333
334
335
336 /*
337  *
338  *      query functions to use APIs
339  *
340  */
341
342 int maps_test_search_place(maps_service_h maps_svc)
343 {
344         startup();
345         int request_id = 0, error;
346
347         maps_coordinates_h coordinates;
348         maps_coordinates_create(48.85784, 2.29516, &coordinates);
349
350         maps_place_filter_set_place_name(maps_place_filter, "Berlin");
351         maps_preference_set_property(maps_pref, MAPS_PLACE_FILTER_SORT_BY, "distance");
352
353         error = maps_service_search_place(maps_svc, coordinates, 5, maps_place_filter, maps_pref,
354                         __maps_service_search_place_cb, "Search Place", &request_id);
355
356         maps_coordinates_destroy(coordinates);
357
358         if (error != MAPS_ERROR_NONE)
359                 cleanup();
360         return error;
361 }
362
363 int maps_test_search_place_by_address(maps_service_h maps_svc)
364 {
365         startup();
366         int request_id = 0, error;
367
368         maps_coordinates_h top_left, bottom_right;
369         maps_area_h area;
370         maps_coordinates_create(15.665354, 74.311523, &top_left);
371         maps_coordinates_create(10.617418, 79.145508, &bottom_right);
372         maps_area_create_rectangle(top_left, bottom_right, &area);
373
374         error = maps_service_search_place_by_address(maps_svc, "Bengaluru",
375                         area, maps_place_filter, maps_pref,
376                         __maps_service_search_place_cb, "Search Place By Address", &request_id);
377
378         maps_coordinates_destroy(top_left);
379         maps_coordinates_destroy(bottom_right);
380         maps_area_destroy(area);
381
382         if (error != MAPS_ERROR_NONE)
383                 cleanup();
384         return error;
385 }
386
387 int maps_test_search_place_by_area(maps_service_h maps_svc)
388 {
389         startup();
390         int request_id = 0, error;
391
392         maps_coordinates_h top_left, bottom_right;
393         maps_area_h area;
394         maps_coordinates_create(52.51605, 12.37691, &top_left);
395         maps_coordinates_create(54.12351, 14.22351, &bottom_right);
396         maps_area_create_rectangle(top_left, bottom_right, &area);
397
398         error = maps_service_search_place_by_area(maps_svc, area, maps_place_filter, maps_pref,
399                         __maps_service_search_place_cb, "Search Place By Area", &request_id);
400
401         maps_coordinates_destroy(top_left);
402         maps_coordinates_destroy(bottom_right);
403         maps_area_destroy(area);
404
405         if (error != MAPS_ERROR_NONE)
406                 cleanup();
407         return error;
408 }
409
410
411 int maps_test_search_place_list(maps_service_h maps_svc)
412 {
413         startup();
414         int request_id = 0, error;
415
416         maps_coordinates_h coordinates;
417         maps_area_h area;
418         maps_coordinates_create(52.5167, 13.383, &coordinates);
419         maps_area_create_circle(coordinates, 5000, &area);
420
421         error = maps_service_search_place_list(maps_svc, area, maps_place_filter, maps_pref,
422                         __maps_service_search_place_list_cb, "Search Place List", &request_id);
423
424         maps_coordinates_destroy(coordinates);
425         maps_area_destroy(area);
426
427         if (error != MAPS_ERROR_NONE)
428                 cleanup();
429         return error;
430 }
431
432
433 int maps_test_search_place_cancel(maps_service_h maps_svc)
434 {
435         startup();
436         int request_id = 0, error;
437
438         maps_coordinates_h coordinates;
439         maps_coordinates_create(48.85784, 2.29516, &coordinates);
440
441         maps_place_filter_set_place_name(maps_place_filter, "Berlin");
442         maps_preference_set_property(maps_pref, MAPS_PLACE_FILTER_SORT_BY, "distance");
443
444         error = maps_service_search_place(maps_svc, coordinates, 5, maps_place_filter, maps_pref,
445                         __maps_service_search_place_cb, "Search Place", &request_id);
446
447         maps_service_cancel_request(maps_svc, request_id);
448         maps_coordinates_destroy(coordinates);
449
450         if (error != MAPS_ERROR_NONE)
451                 cleanup();
452         return error;
453 }
454