ac1ddc927b2783b2d06eb0a78c83a033c954414c
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / haltest / bluetooth_hal_tc.cpp
1 /*
2  * Copyright (c) 2014 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  * @file        bluetooth_hal_tc.cpp
18  * @author      abc
19  * @version     1.0
20  * @brief       BT hal tests
21  */
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <unistd.h>
26 #include <glib.h>
27 #include <system_info.h>
28
29 #include "bluetooth.h"
30 #include "oal-manager.h"
31 #include "oal-adapter-mgr.h"
32
33 using ::testing::EmptyTestEventListener;
34 using ::testing::InitGoogleTest;
35 using ::testing::Test;
36 using ::testing::TestCase;
37 using ::testing::TestEventListeners;
38 using ::testing::TestInfo;
39 using ::testing::TestPartResult;
40 using ::testing::UnitTest;
41
42 #define TIMEOUT_6SECONDS 6000
43 #define TIMEOUT_2SECONDS 2000
44 #define TIMEOUT_500MILISECONDS 500
45
46 #define FEATURE_BT "http://tizen.org/feature/network.bluetooth"
47
48 #define SKIP_NOT_SUPPORTED(bFeature) {\
49     if (!bFeature) {\
50         printf("Feature is not support\n");\
51         return;\
52     } \
53 }
54
55 static bool g_bFeatureBT;
56
57 static GMainLoop *mainloop = NULL;
58
59 static gboolean timeout_func(gpointer data)
60 {
61     g_main_loop_quit((GMainLoop *)data);
62     return FALSE;
63 }
64
65 static void wait_for_async(int timeout)
66 {
67     int timeout_id = 0;
68
69     mainloop = g_main_loop_new(NULL, FALSE);
70
71     timeout_id = g_timeout_add(timeout, timeout_func, mainloop);
72     if (timeout_id < 0)
73         return;
74
75     g_main_loop_run(mainloop);
76 }
77
78 static void __bt_oal_event_receiver(int event_type, gpointer event_data, gsize len)
79 {
80 /*
81         printf("event_type: [%d], data size: [%d]\n", event_type, len);
82 */
83 }
84
85 static bool __check_feature_supported(char *key)
86 {
87         bool value = false;
88         int ret = system_info_get_platform_bool(key, &value);
89
90         EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
91
92         return value;
93 }
94
95 TEST(BluetoothHAL_test, oal_bt_init_deinit_p) {
96         g_bFeatureBT = __check_feature_supported((char*)FEATURE_BT);
97         SKIP_NOT_SUPPORTED(g_bFeatureBT);
98
99         int ret = OAL_STATUS_SUCCESS;
100
101         ret = oal_bt_init(__bt_oal_event_receiver);
102         ASSERT_TRUE(ret == OAL_STATUS_PENDING);
103
104         wait_for_async(TIMEOUT_500MILISECONDS);
105
106         oal_bt_deinit();
107
108         wait_for_async(TIMEOUT_500MILISECONDS);
109 }
110
111 TEST(BluetoothHAL_test, adapter_enable_p) {
112         SKIP_NOT_SUPPORTED(g_bFeatureBT);
113
114         int ret = OAL_STATUS_SUCCESS;
115         gboolean powered = FALSE;
116
117         ret = oal_bt_init(__bt_oal_event_receiver);
118         ASSERT_TRUE(ret == OAL_STATUS_PENDING);
119
120         wait_for_async(TIMEOUT_500MILISECONDS);
121
122         ret = adapter_get_powered_status(&powered);
123
124         /* Already enabled */
125         if (powered == TRUE)
126                 return;
127
128         ret = adapter_enable();
129         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
130
131         wait_for_async(TIMEOUT_6SECONDS); /* Wait for 6 seconds */
132 }
133
134 TEST(BluetoothHAL_test, adapter_disable_p) {
135         SKIP_NOT_SUPPORTED(g_bFeatureBT);
136
137         int ret = OAL_STATUS_SUCCESS;
138
139         ret = adapter_disable();
140         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
141
142         wait_for_async(TIMEOUT_6SECONDS);
143 }
144
145 TEST(BluetoothHAL_test, adapter_get_powered_status_p) {
146         SKIP_NOT_SUPPORTED(g_bFeatureBT);
147
148         int ret = OAL_STATUS_SUCCESS;
149         gboolean powered = FALSE;
150
151         /* Precondition : BT enable */
152         ret = adapter_enable();
153         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
154
155         wait_for_async(TIMEOUT_6SECONDS);
156
157         ret = adapter_get_powered_status(&powered);
158         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
159 }
160
161 TEST(BluetoothHAL_test, adapter_start_stop_inquiry_p) {
162         SKIP_NOT_SUPPORTED(g_bFeatureBT);
163
164         int ret = OAL_STATUS_SUCCESS;
165
166         ret = adapter_start_inquiry(0);
167         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
168
169         wait_for_async(TIMEOUT_2SECONDS);
170
171         ret = adapter_stop_inquiry();
172         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
173         wait_for_async(TIMEOUT_2SECONDS);
174 }
175
176 TEST(BluetoothHAL_test, adapter_get_properties_p) {
177         SKIP_NOT_SUPPORTED(g_bFeatureBT);
178
179         int ret = OAL_STATUS_SUCCESS;
180
181         ret = adapter_get_properties();
182         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
183
184         wait_for_async(TIMEOUT_500MILISECONDS);
185 }
186
187 TEST(BluetoothHAL_test, adapter_get_address_p) {
188         SKIP_NOT_SUPPORTED(g_bFeatureBT);
189
190         int ret = OAL_STATUS_SUCCESS;
191
192         ret = adapter_get_address();
193         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
194
195         wait_for_async(TIMEOUT_500MILISECONDS);
196 }
197
198 TEST(BluetoothHAL_test, adapter_get_version_p) {
199         SKIP_NOT_SUPPORTED(g_bFeatureBT);
200
201         int ret = OAL_STATUS_SUCCESS;
202
203         ret = adapter_get_version();
204         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
205
206         wait_for_async(TIMEOUT_500MILISECONDS);
207 }
208
209 TEST(BluetoothHAL_test, adapter_get_name_p) {
210         SKIP_NOT_SUPPORTED(g_bFeatureBT);
211
212         int ret = OAL_STATUS_SUCCESS;
213
214         ret = adapter_get_name();
215         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
216
217         wait_for_async(TIMEOUT_500MILISECONDS);
218 }
219
220 TEST(BluetoothHAL_test, adapter_set_name_p) {
221         SKIP_NOT_SUPPORTED(g_bFeatureBT);
222
223         int ret = OAL_STATUS_SUCCESS;
224
225         ret = adapter_set_name((char *)"TizenHAL");
226         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
227
228         wait_for_async(TIMEOUT_500MILISECONDS);
229 }
230
231 TEST(BluetoothHAL_test, adapter_is_discoverable_p) {
232         SKIP_NOT_SUPPORTED(g_bFeatureBT);
233
234         int ret = OAL_STATUS_SUCCESS;
235         int scan_mode = 0;
236
237         ret = adapter_is_discoverable(&scan_mode);
238         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
239 }
240
241 TEST(BluetoothHAL_test, adapter_is_connectable_p) {
242         SKIP_NOT_SUPPORTED(g_bFeatureBT);
243
244         int ret = OAL_STATUS_SUCCESS;
245         int connectable = 0;
246
247         ret = adapter_is_connectable(&connectable);
248         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
249 }
250
251 TEST(BluetoothHAL_test, adapter_get_discoverable_timeout_p) {
252         SKIP_NOT_SUPPORTED(g_bFeatureBT);
253
254         int ret = OAL_STATUS_SUCCESS;
255         int timeout = 0;
256
257         ret = adapter_get_discoverable_timeout(&timeout);
258         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
259 }
260
261 TEST(BluetoothHAL_test, adapter_get_service_uuids_p) {
262         SKIP_NOT_SUPPORTED(g_bFeatureBT);
263
264         int ret = OAL_STATUS_SUCCESS;
265
266         ret = adapter_get_service_uuids();
267         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
268
269         wait_for_async(TIMEOUT_500MILISECONDS);
270 }
271
272 TEST(BluetoothHAL_test, adapter_set_connectable_p) {
273         SKIP_NOT_SUPPORTED(g_bFeatureBT);
274
275         int ret = OAL_STATUS_SUCCESS;
276
277         ret = adapter_set_connectable(BT_SCAN_MODE_CONNECTABLE);
278         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
279
280         wait_for_async(TIMEOUT_500MILISECONDS);
281 }
282
283 TEST(BluetoothHAL_test, adapter_set_discoverable_timeout_p) {
284         SKIP_NOT_SUPPORTED(g_bFeatureBT);
285
286         int ret = OAL_STATUS_SUCCESS;
287
288         ret = adapter_set_discoverable_timeout(0);
289         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
290
291         wait_for_async(TIMEOUT_500MILISECONDS);
292
293         /* Post condition : Disable BT */
294         ret = adapter_disable();
295         ASSERT_TRUE(ret == OAL_STATUS_SUCCESS);
296
297         wait_for_async(TIMEOUT_6SECONDS);
298
299         oal_bt_deinit();
300
301         wait_for_async(TIMEOUT_500MILISECONDS);
302 }
303
304
305 int main(int argc, char **argv) {
306   InitGoogleTest(&argc, argv);
307
308   return RUN_ALL_TESTS();
309 }