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