Initialize Tizen 2.3
[framework/api/bluetooth.git] / mobile / TC / testcase / utc_network_bluetooth_device_discovery_negative.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 <bluetooth.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <glib.h>
22
23 enum {
24         POSITIVE_TC_IDX = 0x01,
25         NEGATIVE_TC_IDX,
26 };
27
28 static GMainLoop *mainloop;
29 gboolean timeout_func(gpointer data);
30
31 static void startup(void);
32 static void cleanup(void);
33
34 void (*tet_startup) (void) = startup;
35 void (*tet_cleanup) (void) = cleanup;
36
37 static void utc_network_bluetooth_device_discovery_cancel_n(void);
38 static void utc_network_bluetooth_device_discovery_unset_state_changed_cb_n(void);
39 static void utc_network_bluetooth_device_discovery_set_state_changed_cb_n(void);
40 static void utc_network_bluetooth_device_discovery_start_n(void);
41 static void utc_network_bluetooth_device_discovery_get_status_n(void);
42
43 void adapter_state_changed_cb_for_discovery_n(int result,
44                                               bt_adapter_state_e adapter_state,
45                                               void *user_data);
46
47 struct tet_testlist tet_testlist[] = {
48         {utc_network_bluetooth_device_discovery_cancel_n, NEGATIVE_TC_IDX},
49         {utc_network_bluetooth_device_discovery_set_state_changed_cb_n, NEGATIVE_TC_IDX},
50         {utc_network_bluetooth_device_discovery_get_status_n, NEGATIVE_TC_IDX},
51         {utc_network_bluetooth_device_discovery_start_n, NEGATIVE_TC_IDX},
52         {utc_network_bluetooth_device_discovery_unset_state_changed_cb_n, NEGATIVE_TC_IDX},
53         {NULL, 0},
54 };
55
56 static void startup(void)
57 {
58         int ret = BT_ERROR_NONE;
59         int timeout_id = 0;
60
61         /* start of TC */
62         mainloop = g_main_loop_new(NULL, FALSE);
63
64         bt_initialize();
65         if (bt_adapter_set_state_changed_cb(adapter_state_changed_cb_for_discovery_n, "startup") != BT_ERROR_NONE) {
66                 tet_printf("DTS may fail because bt_adapter_set_state_changed_cb() failed");
67         }
68
69         tet_printf("bt_adapter_disable() was called.");
70         ret = bt_adapter_disable();
71         if (ret == BT_ERROR_NONE) {
72                 tet_printf("bt_adapter_disable() succeeded.");
73                 timeout_id = g_timeout_add(60000, timeout_func, mainloop);
74                 g_main_loop_run(mainloop);
75                 g_source_remove(timeout_id);
76         } else if (ret == BT_ERROR_NOT_ENABLED) {
77                 tet_printf("Bluetooth adapter is not enabled.");
78         } else {
79                 tet_printf("DTS may fail because bt_adapter_disable() failed");
80         }
81
82         if (bt_adapter_unset_state_changed_cb() != BT_ERROR_NONE) {
83                 tet_printf("bt_adapter_set_state_changed_cb() failed.");
84         }
85
86         tet_printf("TC start");
87 }
88
89 static void cleanup(void)
90 {
91         /* end of TC */
92         bt_deinitialize();
93         tet_printf("TC end");
94 }
95
96 gboolean timeout_func(gpointer data)
97 {
98         tet_printf("Callback: Timeout.");
99         g_main_loop_quit((GMainLoop *) data);
100         return FALSE;
101 }
102
103 /**
104  * @brief Callback funtions
105  */
106 void adapter_state_changed_cb_for_discovery_n(int result,
107                                               state_e adapter_state,
108                                               void *user_data)
109 {
110         tet_printf("Callback: bt_adapter_state_changed_cb was called.");
111         if (user_data != NULL && !strcmp((char *)user_data, "startup")) {
112                 if (adapter_state == BT_ADAPTER_DISABLED &&
113                     result == BT_ERROR_NONE) {
114                         tet_printf("Callback: BT was disabled. DTS will be started.");
115                 } else {
116                         tet_printf("Callback: BT was not disabled. DTS will be started but DTS may fail.");
117                 }
118
119                 if (mainloop) {
120                         g_main_loop_quit(mainloop);
121                 }
122         }
123 }
124
125 /**
126   *@brief Negative test case of bt_adapter_stop_device_discovery()
127  */
128 static void utc_network_bluetooth_device_discovery_cancel_n(void)
129 {
130         int ret = bt_adapter_stop_device_discovery();
131
132         dts_check_eq("bt_adapter_stop_device_discovery", ret,
133                      BT_ERROR_NOT_ENABLED,
134                      "BT_ERROR_NOT_ENABLED must be returned when BT is not enabled");
135 }
136
137 /**
138   *@brief Negative test case of bt_adapter_unset_device_discovery_state_changed_cb()
139  */
140 static void utc_network_bluetooth_device_discovery_unset_state_changed_cb_n(void)
141 {
142         int ret = BT_ERROR_NONE;
143
144         if (bt_deinitialize() != BT_ERROR_NONE) {
145                 dts_fail("bt_adapter_unset_device_discovery_state_changed_cb",
146                          "bt_deinitialize() failed.");
147         }
148         ret = bt_adapter_unset_device_discovery_state_changed_cb();
149         dts_check_eq("bt_adapter_unset_device_discovery_state_changed_cb", ret,
150                      BT_ERROR_NOT_INITIALIZED,
151                      "BT_ERROR_NOT_INITIALIZED must be returned when BT service is not initialized.");
152 }
153
154 /**
155   *@brief Negative test case of bt_adapter_set_device_discovery_state_changed_cb()
156  */
157 static void utc_network_bluetooth_device_discovery_set_state_changed_cb_n(void)
158 {
159         int ret = BT_ERROR_NONE;
160
161         ret = bt_adapter_set_device_discovery_state_changed_cb(NULL, NULL);
162         dts_check_eq("bt_adapter_set_device_discovery_state_changed_cb", ret,
163                      BT_ERROR_INVALID_PARAMETER,
164                      "BT_ERROR_INVALID_PARAMETER must be returned when callback parameter is NULL");
165 }
166
167 /**
168   *@brief Negative test case of bt_adapter_start_device_discovery()
169  */
170 static void utc_network_bluetooth_device_discovery_start_n(void)
171 {
172         bt_error_e ret = BT_ERROR_NONE;
173
174         ret = bt_adapter_start_device_discovery();;
175         dts_check_eq("bt_adapter_start_device_discovery", ret,
176                      BT_ERROR_NOT_ENABLED,
177                      "BT_ERROR_NOT_ENABLED must be returned when BT is not enabled");
178 }
179
180 /**
181   *@brief Negative test case of bt_adapter_is_discovering()
182  */
183 static void utc_network_bluetooth_device_discovery_get_status_n(void)
184 {
185         bt_error_e ret = BT_ERROR_NONE;
186         bool status = false;
187
188         ret = bt_adapter_is_discovering(&status);
189         dts_check_eq("bt_adapter_is_discovering", ret, BT_ERROR_NOT_ENABLED,
190                      "BT_ERROR_NOT_ENABLED must be returned when BT is not enabled");
191 }