tizen 2.3.1 release
[framework/telephony/libtcore.git] / unit-test / test-queue.c
1
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <glib.h>
6
7 #include <tcore.h>
8 #include <queue.h>
9 #include <hal.h>
10 #include <user_request.h>
11
12 static int g_flag;
13
14 static TReturn always_send_ok(TcoreHal *hal, unsigned int data_len, void *data)
15 {
16         return TCORE_RETURN_SUCCESS;
17 }
18
19 static struct tcore_hal_operations hops = {
20         .send = always_send_ok,
21 };
22
23 static void test_queue_hal(void)
24 {
25         TcoreHal *h;
26         TcorePending *pending1;
27         TcorePending *pending2;
28         TcorePending *pending3;
29         TcorePending *pending = NULL;
30         TReturn ret;
31
32         h = tcore_hal_new(NULL, "test_hal", &hops, TCORE_HAL_MODE_CUSTOM);
33         g_assert(h);
34
35         ret = tcore_hal_set_power_state(h, TRUE);
36         g_assert(ret == TCORE_RETURN_SUCCESS);
37
38         /* first item */
39         pending1 = tcore_pending_new(NULL, 1);
40         g_assert(pending1);
41         tcore_pending_set_priority(pending1, TCORE_PENDING_PRIORITY_IMMEDIATELY);
42
43         /* second item */
44         pending2 = tcore_pending_new(NULL, 1);
45         g_assert(pending2);
46         tcore_pending_set_auto_free_status_after_sent(pending2, TRUE);
47
48         /* third item */
49         pending3 = tcore_pending_new(NULL, 1);
50         g_assert(pending3);
51         tcore_pending_set_priority(pending3, TCORE_PENDING_PRIORITY_IMMEDIATELY);
52
53         /* send pending1 */
54         ret = tcore_hal_send_request(h, pending1);
55         g_assert(ret == TCORE_RETURN_SUCCESS);
56
57         /* send pending2 -> queue */
58         ret = tcore_hal_send_request(h, pending2);
59         g_assert(ret == TCORE_RETURN_SUCCESS);
60
61         /* remove pending1 */
62         ret = tcore_hal_dispatch_response_data(h, 1, 0, NULL);
63         g_assert(ret == TCORE_RETURN_SUCCESS);
64
65         /* check next pending */
66         pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
67         g_assert(pending == pending2);
68
69         /* force send (because g_main_loop not work in unit test) */
70         tcore_hal_send_force(h);
71
72         /* check next pending (pending2 is auto free) */
73         pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
74         g_assert(pending == NULL);
75
76         /* send pending3 */
77         ret = tcore_hal_send_request(h, pending3);
78         g_assert(ret == TCORE_RETURN_SUCCESS);
79
80         pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
81         g_assert(pending == pending3);
82
83         /* remove pending3 */
84         ret = tcore_hal_dispatch_response_data(h, 1, 0, NULL);
85         g_assert(ret == TCORE_RETURN_SUCCESS);
86
87         /* check next pending (no more pending) */
88         pending = tcore_queue_ref_pending_by_id(tcore_hal_ref_queue(h), 1);
89         g_assert(pending == NULL);
90
91         tcore_hal_free(h);
92 }
93
94 static void test_queue_push_priority(void)
95 {
96         TcoreQueue *q;
97         TcorePending *pending1;
98         TcorePending *pending2;
99         TcorePending *pending3;
100         TcorePending *pending = NULL;
101         TReturn ret;
102
103         q = tcore_queue_new(NULL);
104         g_assert(q);
105
106         /* first item */
107         pending1 = tcore_pending_new(NULL, 1);
108         g_assert(pending1);
109         tcore_pending_set_priority(pending1, TCORE_PENDING_PRIORITY_IMMEDIATELY);
110
111         ret = tcore_queue_push(q, pending1);
112         g_assert(ret == TCORE_RETURN_SUCCESS);
113
114         /* second item */
115         pending2 = tcore_pending_new(NULL, 1);
116         g_assert(pending2);
117
118         ret = tcore_queue_push(q, pending2);
119         g_assert(ret == TCORE_RETURN_SUCCESS);
120
121         /* third item, but push to second item position */
122         pending3 = tcore_pending_new(NULL, 1);
123         g_assert(pending3);
124         tcore_pending_set_priority(pending3, TCORE_PENDING_PRIORITY_IMMEDIATELY);
125
126         ret = tcore_queue_push(q, pending3);
127         g_assert(ret == TCORE_RETURN_SUCCESS);
128
129         /* pop test (order by priority) */
130         pending = tcore_queue_pop_by_id(q, 1);
131         g_assert(pending == pending1);
132
133         pending = tcore_queue_pop_by_id(q, 1);
134         g_assert(pending == pending3);
135
136         pending = tcore_queue_pop_by_id(q, 1);
137         g_assert(pending == pending2);
138
139         tcore_queue_free(q);
140 }
141
142 static void test_queue_push(void)
143 {
144         TcoreQueue *q;
145         TcorePending *pending1;
146         TcorePending *pending2;
147         TcorePending *pending3;
148         TcorePending *pending = NULL;
149         TReturn ret;
150
151         q = tcore_queue_new(NULL);
152         g_assert(q);
153
154         /* first item */
155         pending1 = tcore_pending_new(NULL, 1);
156         g_assert(pending1);
157
158         ret = tcore_queue_push(q, pending1);
159         g_assert(ret == TCORE_RETURN_SUCCESS);
160
161         /* second item */
162         pending2 = tcore_pending_new(NULL, 1);
163         g_assert(pending2);
164
165         ret = tcore_queue_push(q, pending2);
166         g_assert(ret == TCORE_RETURN_SUCCESS);
167
168         /* third item */
169         pending3 = tcore_pending_new(NULL, 1);
170         g_assert(pending3);
171
172         ret = tcore_queue_push(q, pending3);
173         g_assert(ret == TCORE_RETURN_SUCCESS);
174
175         /* pop test (order by push sequence (same priority)) */
176         pending = tcore_queue_pop_by_id(q, 1);
177         g_assert(pending == pending1);
178
179         pending = tcore_queue_pop_by_id(q, 1);
180         g_assert(pending == pending2);
181
182         pending = tcore_queue_pop_by_id(q, 1);
183         g_assert(pending == pending3);
184
185         tcore_queue_free(q);
186 }
187
188 static void on_resp(TcorePending *pending, int data_len, const void *data, void *user_data)
189 {
190         printf("on_resp !!\n");
191         g_flag++;
192 }
193
194 static void test_queue_push_cancel(void)
195 {
196         TcoreHal *h;
197         TcorePending *pending1;
198         TcorePending *pending2;
199         TcorePending *pending3;
200         TcorePending *p;
201         TReturn ret;
202         UserRequest *ur1;
203         UserRequest *ur2;
204
205         h = tcore_hal_new(NULL, "test_hal", &hops, TCORE_HAL_MODE_CUSTOM);
206         g_assert(h);
207
208         ret = tcore_hal_set_power_state(h, TRUE);
209         g_assert(ret == TCORE_RETURN_SUCCESS);
210
211         /* first item */
212         ur1 = tcore_user_request_new(NULL, NULL);
213         g_assert(ur1);
214
215         tcore_user_request_set_command(ur1, TREQ_NETWORK_SEARCH);
216
217         pending1 = tcore_pending_new(NULL, 1);
218         g_assert(pending1);
219
220         tcore_pending_set_response_callback(pending1, on_resp, NULL);
221         tcore_pending_link_user_request(pending1, ur1);
222
223         ret = tcore_hal_send_request(h, pending1);
224         g_assert(ret == TCORE_RETURN_SUCCESS);
225
226         /* force update send state */
227         tcore_pending_emit_send_callback(pending1, TRUE);
228
229         /* second item */
230         ur2 = tcore_user_request_new(NULL, NULL);
231         g_assert(ur2);
232
233         tcore_user_request_set_command(ur2, TREQ_NETWORK_SEARCH);
234
235         pending2 = tcore_pending_new(NULL, 1);
236         g_assert(pending2);
237
238         tcore_pending_set_response_callback(pending2, on_resp, NULL);
239         tcore_pending_link_user_request(pending2, ur2);
240
241         ret = tcore_hal_send_request(h, pending2);
242         g_assert(ret == TCORE_RETURN_SUCCESS);
243
244         /* third item */
245         pending3 = tcore_pending_new(NULL, 1);
246         g_assert(pending3);
247
248         ret = tcore_hal_send_request(h, pending3);
249         g_assert(ret == TCORE_RETURN_SUCCESS);
250
251
252         /* search */
253         p = tcore_queue_search_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH, TRUE);
254         g_assert(p == pending1);
255
256         p = tcore_queue_search_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH, FALSE);
257         g_assert(p == pending2);
258
259         /* cancel */
260         g_flag = 0;
261         ret = tcore_queue_cancel_pending_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH);
262         g_assert(ret == TCORE_RETURN_SUCCESS);
263
264         g_assert(g_flag == 2);
265
266 }
267
268 static void test_queue_new(void)
269 {
270         TcoreQueue *q;
271
272         q = tcore_queue_new(NULL);
273         g_assert(q);
274
275         tcore_queue_free(q);
276 }
277
278 int main(int argc, char **argv)
279 {
280         g_test_init(&argc, &argv, NULL);
281
282         g_test_add_func("/queue/new", test_queue_new);
283         g_test_add_func("/queue/push", test_queue_push);
284         g_test_add_func("/queue/push_priority", test_queue_push_priority);
285         g_test_add_func("/queue/push_cancel", test_queue_push_cancel);
286         g_test_add_func("/queue/hal", test_queue_hal);
287
288         return g_test_run();
289 }