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