Release Tizen2.0 beta
[framework/api/location-manager.git] / TC / testcase / utc_location_gps_status.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <tet_api.h>
18 #include <locations.h>
19 #include <glib.h>
20
21 enum {
22         POSITIVE_TC_IDX = 0x01,
23         NEGATIVE_TC_IDX,
24 };
25
26 static void startup(void);
27 static void cleanup(void);
28
29 void (*tet_startup) (void) = startup;
30 void (*tet_cleanup) (void) = cleanup;
31
32 static void utc_location_gps_status_get_nmea_p(void);
33 static void utc_location_gps_status_get_nmea_n(void);
34 static void utc_location_gps_status_get_nmea_n_02(void);
35 static void utc_location_gps_status_get_nmea_n_03(void);
36 static void utc_location_gps_status_get_satellite_p(void);
37 static void utc_location_gps_status_get_satellite_n(void);
38 static void utc_location_gps_status_get_satellite_n_02(void);
39 static void utc_location_gps_status_get_satellite_n_03(void);
40 static void utc_location_gps_status_get_satellite_n_04(void);
41 static void utc_location_gps_status_get_satellite_n_05(void);
42 static void utc_location_gps_status_get_satellite_n_06(void);
43 static void utc_location_gps_status_foreach_satellites_in_view_p(void);
44 static void utc_location_gps_status_foreach_satellites_in_view_n(void);
45 static void utc_location_gps_status_foreach_satellites_in_view_n_02(void);
46 static void utc_location_gps_status_foreach_satellites_in_view_n_03(void);
47 static void utc_location_gps_status_foreach_last_satellites_in_view_p(void);
48 static void utc_location_gps_status_foreach_last_satellites_in_view_n(void);
49 static void utc_location_gps_status_foreach_last_satellites_in_view_n_02(void);
50 static void utc_location_gps_status_get_last_satellite_p(void);
51 static void utc_location_gps_status_get_last_satellite_n(void);
52 static void utc_location_gps_status_get_last_satellite_n_02(void);
53 static void utc_location_gps_status_get_last_satellite_n_03(void);
54 static void utc_location_gps_status_get_last_satellite_n_04(void);
55
56
57
58 struct tet_testlist tet_testlist[] = {
59         {utc_location_gps_status_get_nmea_p, POSITIVE_TC_IDX},
60         {utc_location_gps_status_get_nmea_n, NEGATIVE_TC_IDX},
61         {utc_location_gps_status_get_nmea_n_02, NEGATIVE_TC_IDX},
62 //      {utc_location_gps_status_get_nmea_n_03, NEGATIVE_TC_IDX}, //Can't check created location_manager_h
63         {utc_location_gps_status_get_satellite_p, POSITIVE_TC_IDX},
64         {utc_location_gps_status_get_satellite_n, NEGATIVE_TC_IDX},
65         {utc_location_gps_status_get_satellite_n_02, NEGATIVE_TC_IDX},
66 //      {utc_location_gps_status_get_satellite_n_03, NEGATIVE_TC_IDX}, //Can't check created location_manager_h
67         {utc_location_gps_status_get_satellite_n_04, NEGATIVE_TC_IDX},
68         {utc_location_gps_status_get_satellite_n_05, NEGATIVE_TC_IDX},
69         {utc_location_gps_status_get_satellite_n_06, NEGATIVE_TC_IDX},
70         {utc_location_gps_status_foreach_satellites_in_view_p, POSITIVE_TC_IDX},
71         {utc_location_gps_status_foreach_satellites_in_view_n, NEGATIVE_TC_IDX},
72         {utc_location_gps_status_foreach_satellites_in_view_n_02, NEGATIVE_TC_IDX},
73 //      {utc_location_gps_status_foreach_satellites_in_view_n_03, NEGATIVE_TC_IDX }, //Can't check created location_manager_h
74         {utc_location_gps_status_foreach_last_satellites_in_view_p, POSITIVE_TC_IDX},
75         {utc_location_gps_status_foreach_last_satellites_in_view_n, NEGATIVE_TC_IDX},
76         {utc_location_gps_status_foreach_last_satellites_in_view_n_02, NEGATIVE_TC_IDX},
77         {utc_location_gps_status_get_last_satellite_p, POSITIVE_TC_IDX},
78         {utc_location_gps_status_get_last_satellite_n, NEGATIVE_TC_IDX},
79         {utc_location_gps_status_get_last_satellite_n_02, NEGATIVE_TC_IDX},
80         {utc_location_gps_status_get_last_satellite_n_03, NEGATIVE_TC_IDX},
81         {utc_location_gps_status_get_last_satellite_n_04, NEGATIVE_TC_IDX},
82         {NULL, 0},
83 };
84
85 static GMainLoop *g_mainloop = NULL;
86 static GThread *event_thread;
87
88 gpointer GmainThread(gpointer data)
89 {
90         g_mainloop = g_main_loop_new(NULL, 0);
91         g_main_loop_run(g_mainloop);
92
93         return NULL;
94 }
95
96 static bool service_enabled = false;
97
98 static void validate_and_next(char *api_name, int act_ret, int ext_ret, char *fail_msg)
99 {
100         dts_message(api_name, "Actual Result : %d, Expected Result : %d", act_ret, ext_ret);
101         if (act_ret != ext_ret) {
102                 dts_message(api_name, "Fail Message: %s", fail_msg);
103                 dts_fail(api_name);
104         }
105 }
106
107 static void validate_eq(char *api_name, int act_ret, int ext_ret)
108 {
109         dts_message(api_name, "Actual Result : %d, Expected Result : %d", act_ret, ext_ret);
110         if (act_ret == ext_ret) {
111                 dts_pass(api_name);
112         } else {
113                 dts_fail(api_name);
114         }
115 }
116
117 static void unprepare(location_manager_h manager)
118 {
119         location_manager_unset_service_state_changed_cb(manager);
120         location_manager_destroy(manager);
121         service_enabled = false;
122 }
123
124 static void wait_for_service(char *api_name)
125 {
126         int timeout = 0;
127         for (timeout; timeout < 30; timeout++) {
128                 if (service_enabled) {
129                         dts_message(api_name, "Location Service Enabled!!!!");
130                         break;
131                 } else {
132                         dts_message(api_name, "Location Service Disabled!!!!");
133                         sleep(1);
134                 }
135         }
136 }
137
138 static void capi_state_changed_cb(location_service_state_e state, void *user_data)
139 {
140         switch (state) {
141         case LOCATIONS_SERVICE_ENABLED:
142                 service_enabled = true;
143                 break;
144         case LOCATIONS_SERVICE_DISABLED:
145                 service_enabled = false;
146                 break;
147         default:
148                 break;
149         }
150 }
151
152 static location_manager_h manager;
153
154 static void startup(void)
155 {
156         g_setenv("PKG_NAME", "com.samsung.capi-location-gps-status-test", 1);
157         g_setenv("LOCATION_TEST_ENABLE", "1", 1);
158
159 #if !GLIB_CHECK_VERSION (2, 31, 0)
160         if (!g_thread_supported()) {
161                 g_thread_init(NULL);
162         }
163 #endif
164
165         GError *gerr = NULL;
166         event_thread = g_thread_create(GmainThread, NULL, 1, &gerr);
167
168         int ret;
169         ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
170         validate_and_next(__func__, ret, LOCATIONS_ERROR_NONE, "location_manager_create() is failed");
171
172         /* You don't need it
173            ret = location_manager_set_service_state_changed_cb(manager, capi_state_changed_cb, NULL);
174            validate_and_next(__func__, ret, LOCATIONS_ERROR_NONE, "location_manager_set_service_state_changed_cb() is failed");
175          */
176
177         ret = location_manager_start(manager);
178         validate_and_next(__func__, ret, LOCATIONS_ERROR_NONE, "location_manager_start() is failed");
179
180         wait_for_service(__func__);
181 }
182
183 static void cleanup(void)
184 {
185         unprepare(manager);
186         g_main_loop_quit(g_mainloop);
187         g_thread_join(event_thread);
188 }
189
190 static void utc_location_gps_status_get_nmea_p(void)
191 {
192         int ret = LOCATIONS_ERROR_NONE;
193         char *nmea;
194         ret = gps_status_get_nmea(manager, &nmea);
195         validate_eq(__func__, ret, LOCATIONS_ERROR_NONE);
196 }
197
198 static void utc_location_gps_status_get_nmea_n(void)
199 {
200         int ret = LOCATIONS_ERROR_NONE;
201         ret = gps_status_get_nmea(manager, NULL);
202         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
203 }
204
205 static void utc_location_gps_status_get_nmea_n_02(void)
206 {
207         int ret = LOCATIONS_ERROR_NONE;
208         char *nmea;
209         ret = gps_status_get_nmea(NULL, &nmea);
210         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
211
212 }
213
214 static void utc_location_gps_status_get_nmea_n_03(void)
215 {
216         int ret = LOCATIONS_ERROR_NONE;
217         char *nmea;
218         location_manager_h manager_02;
219         ret = gps_status_get_nmea(manager_02, &nmea);
220         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
221
222 }
223
224 static void utc_location_gps_status_get_satellite_p(void)
225 {
226         int ret = LOCATIONS_ERROR_NONE;
227         int num_of_active, num_of_inview;
228         time_t timestamp;
229         ret = gps_status_get_satellite(manager, &num_of_active, &num_of_inview, &timestamp);
230         validate_eq(__func__, ret, LOCATIONS_ERROR_NONE);
231 }
232
233 static void utc_location_gps_status_get_satellite_n(void)
234 {
235         int ret = LOCATIONS_ERROR_NONE;
236         ret = gps_status_get_satellite(manager, NULL, NULL, NULL);
237         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
238 }
239
240 static void utc_location_gps_status_get_satellite_n_02(void)
241 {
242         int ret = LOCATIONS_ERROR_NONE;
243         int num_of_active, num_of_inview;
244         time_t timestamp;
245         ret = gps_status_get_satellite(NULL, &num_of_active, &num_of_inview, &timestamp);
246         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
247 }
248
249 static void utc_location_gps_status_get_satellite_n_03(void)
250 {
251         int ret = LOCATIONS_ERROR_NONE;
252         int num_of_active, num_of_inview;
253         time_t timestamp;
254         location_manager_h manager_02;
255         ret = gps_status_get_satellite(manager_02, &num_of_active, &num_of_inview, &timestamp);
256         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
257 }
258
259 static void utc_location_gps_status_get_satellite_n_04(void)
260 {
261         int ret = LOCATIONS_ERROR_NONE;
262         int num_of_inview;
263         time_t timestamp;
264         ret = gps_status_get_satellite(manager, NULL, &num_of_inview, &timestamp);
265         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
266 }
267
268 static void utc_location_gps_status_get_satellite_n_05(void)
269 {
270         int ret = LOCATIONS_ERROR_NONE;
271         int num_of_active;
272         time_t timestamp;
273         ret = gps_status_get_satellite(manager, &num_of_active, NULL, &timestamp);
274         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
275 }
276
277 static void utc_location_gps_status_get_satellite_n_06(void)
278 {
279         int ret = LOCATIONS_ERROR_NONE;
280         int num_of_active, num_of_inview;
281         ret = gps_status_get_satellite(manager, &num_of_active, &num_of_inview, NULL);
282         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
283 }
284
285 static bool capi_gps_status_get_satellites_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr,
286                                               bool is_in_use, void *user_data)
287 {
288         return true;
289 }
290
291 static void utc_location_gps_status_foreach_satellites_in_view_p(void)
292 {
293         int ret = LOCATIONS_ERROR_NONE;
294         ret = gps_status_foreach_satellites_in_view(manager, capi_gps_status_get_satellites_cb, NULL);
295         validate_eq(__func__, ret, LOCATIONS_ERROR_NONE);
296 }
297
298 static void utc_location_gps_status_foreach_satellites_in_view_n(void)
299 {
300         int ret = LOCATIONS_ERROR_NONE;
301         ret = gps_status_foreach_satellites_in_view(manager, NULL, NULL);
302         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
303 }
304
305 static void utc_location_gps_status_foreach_satellites_in_view_n_02(void)
306 {
307         int ret = LOCATIONS_ERROR_NONE;
308         ret = gps_status_foreach_satellites_in_view(NULL, capi_gps_status_get_satellites_cb, NULL);
309         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
310 }
311
312 static void utc_location_gps_status_foreach_satellites_in_view_n_03(void)
313 {
314         int ret = LOCATIONS_ERROR_NONE;
315         location_manager_h manager_02;
316         ret = gps_status_foreach_satellites_in_view(manager_02, capi_gps_status_get_satellites_cb, NULL);
317         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
318 }
319
320 static bool capi_last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
321                                        void *user_data)
322 {
323         printf("[Last Satellite information]  azimuth  : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation,
324                prn, snr, is_in_use);
325         return true;
326 }
327
328 static void utc_location_gps_status_foreach_last_satellites_in_view_p(void)
329 {
330         int ret = LOCATIONS_ERROR_NONE;
331
332         ret = gps_status_foreach_last_satellites_in_view(manager, capi_last_satellites_foreach_cb, NULL);
333         validate_eq(__func__, ret, LOCATIONS_ERROR_NONE);
334 }
335
336 static void utc_location_gps_status_foreach_last_satellites_in_view_n(void)
337 {
338         int ret = LOCATIONS_ERROR_NONE;
339         ret = gps_status_foreach_last_satellites_in_view(manager, NULL, NULL);
340         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
341 }
342
343 static void utc_location_gps_status_foreach_last_satellites_in_view_n_02(void)
344 {
345         int ret = LOCATIONS_ERROR_NONE;
346         ret = gps_status_foreach_last_satellites_in_view(NULL, capi_last_satellites_foreach_cb, NULL);
347         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
348 }
349
350 static void utc_location_gps_status_get_last_satellite_p(void)
351 {
352         int ret = LOCATIONS_ERROR_NONE;
353
354         time_t timestamp;
355         int num_of_inview, num_of_active;
356         ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, &timestamp);
357         validate_eq(__func__, ret, LOCATIONS_ERROR_NONE);
358 }
359
360 static void utc_location_gps_status_get_last_satellite_n(void)
361 {
362         int ret = LOCATIONS_ERROR_NONE;
363
364         time_t timestamp;
365         int num_of_inview, num_of_active;
366         ret = gps_status_get_last_satellite(NULL, &num_of_active, &num_of_inview, &timestamp);
367         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
368 }
369
370 static void utc_location_gps_status_get_last_satellite_n_02(void)
371 {
372         int ret = LOCATIONS_ERROR_NONE;
373
374         time_t timestamp;
375         int num_of_inview;
376         ret = gps_status_get_last_satellite(manager, NULL, &num_of_inview, &timestamp);
377         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
378 }
379
380 static void utc_location_gps_status_get_last_satellite_n_03(void)
381 {
382         int ret = LOCATIONS_ERROR_NONE;
383
384         time_t timestamp;
385         int num_of_active;
386         ret = gps_status_get_last_satellite(manager, &num_of_active, NULL, &timestamp);
387         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
388 }
389
390 static void utc_location_gps_status_get_last_satellite_n_04(void)
391 {
392         int ret = LOCATIONS_ERROR_NONE;
393
394         int num_of_inview, num_of_active;
395         ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, NULL);
396         validate_eq(__func__, ret, LOCATIONS_ERROR_INVALID_PARAMETER);
397 }
398
399