Common test folder and automated testing of sensor APIs - Feature merge from devel...
[platform/core/system/sensord.git] / test / src / auto_test.c
1 /*
2  * sensord
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <time.h>
20 #include <glib.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sensor_internal.h>
24 #include <sensor_common.h>
25 #include <stdbool.h>
26 #include <sensor_common.h>
27 #include <unistd.h>
28
29 #define DEFAULT_EVENT_INTERVAL 100
30
31 static GMainLoop *mainloop;
32 FILE *fp;
33
34 void callback(unsigned int event_type, sensor_event_data_t *event, void *user_data)
35 {
36         g_main_loop_quit(mainloop);
37 }
38
39 bool check_sensor_api(unsigned int event_type, int cond_value)
40 {
41         int result, handle;
42
43         mainloop = g_main_loop_new(NULL, FALSE);
44
45         sensor_type_t sensor_type = event_type >> 16;
46         sensor_t sensor = sensord_get_sensor(sensor_type);
47
48         handle = sensord_connect(sensor);
49
50         if (handle < 0) {
51                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_connect\n", sensor_type, event_type);
52                 return false;
53         }
54
55         bool is_supported;
56         bool result_boolean = sensord_is_supported_event_type(sensor, event_type, &is_supported);
57         if (!result_boolean && !is_supported) {
58                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_is_supported_event\n", sensor_type, event_type);
59                 return false;
60         }
61
62         int output;
63         result_boolean = sensord_get_min_interval(sensor, &output);
64         if (!result_boolean) {
65                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_min_interval\n", sensor_type, event_type);
66                 return false;
67         }
68
69         float output3;
70         result_boolean = sensord_get_resolution(sensor, &output3);
71         if (!result_boolean) {
72                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_resolution\n", sensor_type, event_type);
73                 return false;
74         }
75
76         result_boolean = sensord_get_max_range(sensor, &output3);
77         if (!result_boolean) {
78                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_max_range\n", sensor_type, event_type);
79                 return false;
80         }
81
82         result_boolean = sensord_get_min_range(sensor, &output3);
83         if (!result_boolean) {
84                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_min_range\n", sensor_type, event_type);
85                 return false;
86         }
87
88         sensor_privilege_t output4;
89         result_boolean = sensord_get_privilege(sensor, &output4);
90         if (!result_boolean) {
91                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_privilege\n", sensor_type, event_type);
92                 return false;
93         }
94
95         const char* result_char = sensord_get_vendor(sensor);
96         if (!result_char) {
97                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_vendor\n", sensor_type, event_type);
98                 return false;
99         }
100
101         result_char = sensord_get_name(sensor);
102         if (!result_char) {
103                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_name\n", sensor_type, event_type);
104                 return false;
105         }
106
107         sensor_type_t output_type;
108         result_boolean = sensord_get_type(sensor, &output_type);
109         if (!result_boolean) {
110                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_type\n", sensor_type, event_type);
111                 return false;
112         }
113
114         unsigned int *output2;
115         result_boolean = sensord_get_supported_event_types(sensor, &output2, &output);
116         if (!result_boolean) {
117                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_supported_event_types\n", sensor_type, event_type);
118                 return false;
119         }
120
121         sensor_t *output_list;
122         result_boolean = sensord_get_sensor_list(sensor_type, &output_list, &output);
123         if (!result_boolean) {
124                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_sensor_list\n", sensor_type, event_type);
125                 return false;
126         }
127
128         result = sensord_register_event(handle, event_type, cond_value, 0, callback, NULL);
129
130         if (result < 0) {
131                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_register_event\n", sensor_type, event_type);
132                 return false;
133         }
134
135         result_boolean = sensord_start(handle, 1);
136
137         if (!result_boolean) {
138                 sensord_unregister_event(handle, event_type);
139                 sensord_disconnect(handle);
140                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_start\n", sensor_type, event_type);
141                 return false;
142         }
143
144         sensor_data_t data;
145         result_boolean = sensord_get_data(handle, event_type, &data);
146         if (!result_boolean) {
147                 sensord_unregister_event(handle, event_type);
148                 sensord_disconnect(handle);
149                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_data\n", sensor_type, event_type);
150                 return false;
151         }
152
153         g_main_loop_run(mainloop);
154         g_main_loop_unref(mainloop);
155
156         result_boolean = sensord_change_event_interval(handle, event_type, 101);
157         if (!result_boolean) {
158                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_change_event_interval\n", sensor_type, event_type);
159                 return false;
160         }
161
162         result_boolean = sensord_set_option(handle, SENSOR_OPTION_ON_IN_SCREEN_OFF);
163         if (!result_boolean){
164                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_change_sensor_option\n", sensor_type, event_type);
165                 return false;
166         }
167
168         result_boolean = sensord_unregister_event(handle, event_type);
169
170         if (!result_boolean) {
171                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_unregister_event\n", sensor_type, event_type);
172                 return false;
173         }
174
175         result_boolean = sensord_stop(handle);
176
177         if (!result_boolean) {
178                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_stop\n", sensor_type, event_type);
179                 return false;
180         }
181
182         result_boolean = sensord_disconnect(handle);
183
184         if (!result_boolean) {
185                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_disconnect\n", sensor_type, event_type);
186                 return false;
187         }
188
189         return true;
190 }
191
192 int main(int argc, char **argv)
193 {
194         bool result;
195
196         int interval = DEFAULT_EVENT_INTERVAL;
197         if (argc == 2)
198                 interval = atof(argv[1]);
199
200         fp = fopen("auto_test.output", "w+");
201
202         result = check_sensor_api(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
203         fprintf(fp, "Accelerometer - RAW_DATA_REPORT_ON_TIME - %d\n", result);
204
205         result = check_sensor_api(GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
206         fprintf(fp, "Geomagnetic - RAW_DATA_REPORT_ON_TIME - %d\n", result);
207
208         result = check_sensor_api(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
209         fprintf(fp, "Gravity - RAW_DATA_REPORT_ON_TIME - %d\n", result);
210
211         result = check_sensor_api(GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
212         fprintf(fp, "Gyroscope - RAW_DATA_REPORT_ON_TIME - %d\n", result);
213
214         result = check_sensor_api(LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME, interval);
215         fprintf(fp, "Light - RAW_DATA_REPORT_ON_TIME - %d\n", result);
216
217         result = check_sensor_api(LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
218         fprintf(fp, "Linear Accel - RAW_DATA_REPORT_ON_TIME - %d\n", result);
219
220         result = check_sensor_api(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
221         fprintf(fp, "Orientation - RAW_DATA_REPORT_ON_TIME - %d\n", result);
222
223         result = check_sensor_api(PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
224         fprintf(fp, "Pressure - RAW_DATA_REPORT_ON_TIME - %d\n", result);
225
226         result = check_sensor_api(ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
227         fprintf(fp, "Rotation Vector - RAW_DATA_REPORT_ON_TIME - %d\n", result);
228
229         result = check_sensor_api(TEMPERATURE_EVENT_RAW_DATA_REPORT_ON_TIME, interval);
230         fprintf(fp, "Temperature - RAW_DATA_REPORT_ON_TIME - %d\n", result);
231
232         printf("Logs printed in ./auto_test.output\n");
233         fclose(fp);
234         return 0;
235 }