73f82e22dafe5e359580455a16428417dd5cb698
[platform/core/api/maps-service.git] / test / maps_test_search_route.c
1 #include "maps_test.h"
2 #include "maps_test_search_route.h"
3 #include "maps_test_log.h"
4 #include "maps_test_util.h"
5
6
7 static maps_distance_unit_e g_distance_unit = MAPS_DISTANCE_UNIT_M;
8
9 static void startup()
10 {
11 }
12
13 static void cleanup(bool result)
14 {
15         print_result(result);
16 }
17
18
19
20
21
22 static bool __maps_route_segment_maneuver_cb(int index, int total, maps_route_maneuver_h maneuver, void *user_data)
23 {
24         if (!maneuver) return false;
25
26         LOG_VARIABLES();
27         LOG_START("%smaneuver [%d/%d] = {", LOG_PREFIX, index+1, total);
28
29         if (maps_route_maneuver_get_instruction_text(maneuver, &lv.strval) == MAPS_ERROR_NONE)
30                 LOG_PRINT("%s%sinstruction = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
31
32         if (maps_route_maneuver_get_distance_to_next_instruction(maneuver, &lv.dblval) == MAPS_ERROR_NONE)
33                 LOG_PRINT("%s%sdistance = %.2f %s", LOG_PREFIX, LOG_PREFIX, lv.dblval, get_distance_unit_string(g_distance_unit));
34
35         if (maps_route_maneuver_get_time_to_next_instruction(maneuver, &lv.intval) == MAPS_ERROR_NONE)
36                 LOG_PRINT("%s%stime = %d sec", LOG_PREFIX, LOG_PREFIX, lv.intval);
37
38         if (maps_route_maneuver_get_direction_id(maneuver, (maps_route_direction_e*)&lv.intval) == MAPS_ERROR_NONE)
39                 LOG_PRINT("%s%sdirection_id = %s (%d)", LOG_PREFIX, LOG_PREFIX, get_direction_string(lv.intval), lv.intval);
40
41         if (maps_route_maneuver_get_turn_type(maneuver, (maps_route_turn_type_e*)&lv.intval) == MAPS_ERROR_NONE)
42                 LOG_PRINT("%s%sturn_type = %s (%d)", LOG_PREFIX, LOG_PREFIX, get_turn_type_string(lv.intval), lv.intval);
43
44         maps_coordinates_h coord;
45         if (maps_route_maneuver_get_position(maneuver, &coord) == MAPS_ERROR_NONE) {
46                 double lat, lng;
47                 maps_coordinates_get_latitude(coord, &lat);
48                 maps_coordinates_get_longitude(coord, &lng);
49                 LOG_PRINT("%s%sposition = %f, %f", LOG_PREFIX, LOG_PREFIX, lat, lng);
50                 maps_coordinates_destroy(coord);
51         }
52
53         if (maps_route_maneuver_get_road_name(maneuver, &lv.strval) == MAPS_ERROR_NONE)
54                 LOG_PRINT("%s%sroad_name = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
55
56         if (maps_route_maneuver_get_locale(maneuver, &lv.strval) == MAPS_ERROR_NONE)
57                 LOG_PRINT("%s%slocale = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
58
59         maps_route_maneuver_destroy(maneuver);
60
61         LOG_FINISH("%s}", LOG_PREFIX);
62         return true;
63 }
64
65
66 static bool __maps_route_path_cb(int index, int total, maps_coordinates_h coordinates, void *user_data)
67 {
68         if (!coordinates) return false;
69
70         char buffer[30] = "";
71         snprintf(buffer, sizeof(buffer), "path [%d/%d]", index+1, total);
72
73         log_coordinates("", buffer, coordinates);
74         maps_coordinates_destroy(coordinates);
75         return true;
76 }
77
78 static bool __maps_route_segment_path_cb(int index, int total, maps_coordinates_h coordinates, void *user_data)
79 {
80         if (!coordinates) return false;
81
82         char buffer[30] = "";
83         snprintf(buffer, sizeof(buffer), "path [%d/%d]", index+1, total);
84
85         log_coordinates(LOG_PREFIX, buffer, coordinates);
86         maps_coordinates_destroy(coordinates);
87         return true;
88 }
89
90
91 static bool __maps_route_segment_cb(int index, int total, maps_route_segment_h segment, void *user_data)
92 {
93         LOG_VARIABLES();
94         LOG_START("segment [%d/%d] = {", index+1, total);
95
96         double distance;
97         if (maps_route_segment_get_distance(segment, &distance) == MAPS_ERROR_NONE && distance > 0)
98                 LOG_PRINT("%sdistance = %.2f %s", LOG_PREFIX, distance, get_distance_unit_string(g_distance_unit));
99
100         long duration = 0;
101         if (maps_route_segment_get_duration(segment, &duration) == MAPS_ERROR_NONE && duration > 0)
102                 LOG_PRINT("%sduration = %ld sec", LOG_PREFIX, duration);
103
104         maps_coordinates_h origin;
105         if (maps_route_segment_get_origin(segment, &origin) == MAPS_ERROR_NONE) {
106                 log_coordinates(LOG_PREFIX, "origin", origin);
107                 maps_coordinates_destroy(origin);
108         }
109
110         maps_coordinates_h destination;
111         if (maps_route_segment_get_destination(segment, &destination) == MAPS_ERROR_NONE) {
112                 log_coordinates(LOG_PREFIX,  "destination", destination);
113                 maps_coordinates_destroy(destination);
114         }
115
116         maps_area_h area;
117         if (maps_route_segment_get_bounding_box(segment, &area) == MAPS_ERROR_NONE) {
118                 log_area(LOG_PREFIX, area);
119                 maps_area_destroy(area);
120         }
121
122         maps_route_segment_foreach_maneuver(segment, __maps_route_segment_maneuver_cb, user_data);
123
124         maps_route_segment_foreach_path(segment, __maps_route_segment_path_cb, user_data);
125
126         maps_route_segment_destroy(segment);
127
128         LOG_FINISH("}");
129         return true;
130 }
131
132
133 static bool __log_route(maps_route_h route, void *user_data)
134 {
135         LOG_VARIABLES();
136         LOG_START("");
137
138         if (maps_route_get_route_id(route, &lv.strval) == MAPS_ERROR_NONE)
139                 LOG_PRINT("route_id = %s", lv.strval);
140
141         maps_coordinates_h coord;
142         double lat, lon;
143         if (maps_route_get_origin(route, &coord) == MAPS_ERROR_NONE) {
144                 maps_coordinates_get_latitude_longitude(coord, &lat, &lon);
145                 maps_coordinates_destroy(coord);
146                 LOG_PRINT("origin = %f, %f", lat, lon);
147         }
148         if (maps_route_get_destination(route, &coord) == MAPS_ERROR_NONE) {
149                 maps_coordinates_get_latitude_longitude(coord, &lat, &lon);
150                 maps_coordinates_destroy(coord);
151                 LOG_PRINT("destination = %f, %f", lat, lon);
152         }
153
154         if (maps_route_get_distance_unit(route, (maps_distance_unit_e*)&lv.intval) == MAPS_ERROR_NONE) {
155                 g_distance_unit = lv.intval;
156                 LOG_PRINT("distance_unit = '%s' (%d)", get_distance_unit_string(lv.intval), lv.intval);
157         }
158
159         if (maps_route_get_total_distance(route, &lv.dblval) == MAPS_ERROR_NONE)
160                 LOG_PRINT("distance = %.2f %s", lv.dblval, get_distance_unit_string(g_distance_unit));
161
162         if (maps_route_get_total_duration(route, &lv.longval) == MAPS_ERROR_NONE)
163                 LOG_PRINT("duration = %ld sec", lv.longval);
164
165         if (maps_route_get_transport_mode(route, (maps_route_transport_mode_e*)&lv.intval) == MAPS_ERROR_NONE)
166                 LOG_PRINT("transport = %s", get_transport_string(lv.intval));
167
168         maps_area_h area = NULL;
169         if (maps_route_get_bounding_box(route, &area) == MAPS_ERROR_NONE) {
170            log_area("", area);
171                 maps_area_destroy(area);
172         }
173
174         maps_route_foreach_path(route, __maps_route_path_cb, user_data);
175
176         maps_route_foreach_segment(route, __maps_route_segment_cb, user_data);
177
178         LOG_FINISH("");
179         return true;
180 }
181
182
183 /*
184  *
185  *  query functions to use APIs
186  *
187  */
188 static bool __maps_service_search_route_cb(maps_error_e error,
189                                                  int request_id, int index,
190                                                  int total, maps_route_h route,
191                                                  void *user_data)
192 {
193         LOG_VARIABLES();
194         LOG_START_TITLE("%s [%d/%d] ", (user_data ? (char*)user_data : __FUNCTION__), MIN(index+1, total), total);
195
196         if (error) {
197                 cleanup(false);
198                 return false;
199         }
200
201         __log_route(route, user_data);
202         maps_route_destroy(route);
203
204         LOG_FINISH("");
205
206         if (index == total - 1)
207                 cleanup(true);
208         return true;
209 }
210
211
212 int maps_test_search_route(maps_service_h maps_svc)
213 {
214         startup();
215         int request_id, error;
216
217         maps_coordinates_h origin = NULL, dest = NULL;
218         maps_coordinates_create(52.525324, 13.369911, &origin);
219         maps_coordinates_create(52.469051, 13.441912, &dest);
220
221         error = maps_service_search_route(maps_svc, origin, dest, maps_pref,
222                         __maps_service_search_route_cb, "Search Route", &request_id);
223
224         maps_coordinates_destroy(origin);
225         maps_coordinates_destroy(dest);
226
227         if (error != MAPS_ERROR_NONE)
228                 cleanup(false);
229         return error;
230 }
231