sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General Public
|
sl@0
|
15 |
* License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*
|
sl@0
|
19 |
* Description:
|
sl@0
|
20 |
*
|
sl@0
|
21 |
*/
|
sl@0
|
22 |
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
#undef G_DISABLE_ASSERT
|
sl@0
|
26 |
#undef G_LOG_DOMAIN
|
sl@0
|
27 |
|
sl@0
|
28 |
#include <stdio.h>
|
sl@0
|
29 |
#include <string.h>
|
sl@0
|
30 |
#include "glib.h"
|
sl@0
|
31 |
#include <pthread.h>
|
sl@0
|
32 |
#include <unistd.h>
|
sl@0
|
33 |
|
sl@0
|
34 |
#ifdef SYMBIAN
|
sl@0
|
35 |
#include "mrt2_glib2_test.h"
|
sl@0
|
36 |
#endif /*SYMBIAN*/
|
sl@0
|
37 |
|
sl@0
|
38 |
GMainLoop *loop,*idle_loop;
|
sl@0
|
39 |
GMainContext *context;
|
sl@0
|
40 |
int fd[2];
|
sl@0
|
41 |
char *buf[100];
|
sl@0
|
42 |
GSource *source1,*source2;
|
sl@0
|
43 |
pthread_t thread1;
|
sl@0
|
44 |
gboolean e1_complete,e2_complete;
|
sl@0
|
45 |
|
sl@0
|
46 |
void quit()
|
sl@0
|
47 |
{
|
sl@0
|
48 |
if(e1_complete && e2_complete)
|
sl@0
|
49 |
g_main_loop_quit(loop);
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
|
sl@0
|
53 |
gboolean prepare(GSource *source,gint *timeout)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
*timeout = 0;
|
sl@0
|
56 |
return TRUE;
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
gboolean check(GSource *source)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
return TRUE;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
|
sl@0
|
65 |
{
|
sl@0
|
66 |
if(callback(user_data))
|
sl@0
|
67 |
return TRUE;
|
sl@0
|
68 |
else
|
sl@0
|
69 |
return FALSE;
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
gboolean my_callback(int* data)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
static int i = 100;
|
sl@0
|
75 |
int depth;
|
sl@0
|
76 |
|
sl@0
|
77 |
//checks g_main_loop_is_running
|
sl@0
|
78 |
g_assert(g_main_loop_is_running(loop));
|
sl@0
|
79 |
|
sl@0
|
80 |
depth = g_main_depth();
|
sl@0
|
81 |
|
sl@0
|
82 |
//checks g_main_depth
|
sl@0
|
83 |
g_assert(depth == 1);
|
sl@0
|
84 |
|
sl@0
|
85 |
i--;
|
sl@0
|
86 |
|
sl@0
|
87 |
//printf("%d\n",i);
|
sl@0
|
88 |
|
sl@0
|
89 |
if(i<0)
|
sl@0
|
90 |
{
|
sl@0
|
91 |
e1_complete = TRUE;
|
sl@0
|
92 |
quit();
|
sl@0
|
93 |
return FALSE;
|
sl@0
|
94 |
}
|
sl@0
|
95 |
else
|
sl@0
|
96 |
{
|
sl@0
|
97 |
++(*data);
|
sl@0
|
98 |
return TRUE;
|
sl@0
|
99 |
}
|
sl@0
|
100 |
}
|
sl@0
|
101 |
|
sl@0
|
102 |
|
sl@0
|
103 |
gboolean fd_prepare(GSource *source,gint *timeout)
|
sl@0
|
104 |
{
|
sl@0
|
105 |
*timeout = -1;
|
sl@0
|
106 |
return FALSE;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
gboolean fd_check(GSource *source)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
GPollFD *pollfd = source->poll_fds->data;
|
sl@0
|
112 |
if(pollfd->revents && (G_IO_IN | G_IO_HUP | G_IO_ERR))
|
sl@0
|
113 |
return TRUE;
|
sl@0
|
114 |
else
|
sl@0
|
115 |
return FALSE;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
gboolean fd_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
|
sl@0
|
119 |
{
|
sl@0
|
120 |
if(callback(user_data))
|
sl@0
|
121 |
return TRUE;
|
sl@0
|
122 |
else
|
sl@0
|
123 |
return FALSE;
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
gboolean fd_callback(int* data)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
pthread_join(thread1, NULL);
|
sl@0
|
129 |
read(fd[0],buf,sizeof(buf));
|
sl@0
|
130 |
|
sl@0
|
131 |
// This asserts actually checks whether the callback for fd is successful or not
|
sl@0
|
132 |
// In other words it checks if g_source_add_poll was successful or not.
|
sl@0
|
133 |
g_assert(!strcmp((const char *)buf,"fd poll example"));
|
sl@0
|
134 |
|
sl@0
|
135 |
*data = 1;
|
sl@0
|
136 |
//printf("%s",buf);
|
sl@0
|
137 |
//getchar();
|
sl@0
|
138 |
|
sl@0
|
139 |
g_source_unref(source2);
|
sl@0
|
140 |
|
sl@0
|
141 |
e2_complete = TRUE;
|
sl@0
|
142 |
quit();
|
sl@0
|
143 |
return FALSE;
|
sl@0
|
144 |
}
|
sl@0
|
145 |
|
sl@0
|
146 |
|
sl@0
|
147 |
void* thread_function(void *data)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
sleep(1);
|
sl@0
|
150 |
write(fd[1],"fd poll example",15);
|
sl@0
|
151 |
return NULL;
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
void main_loop_test()
|
sl@0
|
155 |
{
|
sl@0
|
156 |
GMainContext *default_context;
|
sl@0
|
157 |
int depth;
|
sl@0
|
158 |
int id;
|
sl@0
|
159 |
GTimeVal time ={0,};
|
sl@0
|
160 |
int user_data = 0,fd_data = 0;
|
sl@0
|
161 |
GPollFD pollfd;
|
sl@0
|
162 |
GSource *source3;
|
sl@0
|
163 |
|
sl@0
|
164 |
GSourceFuncs SourceFuncs =
|
sl@0
|
165 |
{
|
sl@0
|
166 |
prepare,
|
sl@0
|
167 |
check,
|
sl@0
|
168 |
dispatch,
|
sl@0
|
169 |
NULL
|
sl@0
|
170 |
};
|
sl@0
|
171 |
|
sl@0
|
172 |
GSourceFuncs fd_SourceFuncs =
|
sl@0
|
173 |
{
|
sl@0
|
174 |
fd_prepare,
|
sl@0
|
175 |
fd_check,
|
sl@0
|
176 |
fd_dispatch,
|
sl@0
|
177 |
NULL
|
sl@0
|
178 |
};
|
sl@0
|
179 |
|
sl@0
|
180 |
e1_complete = FALSE;
|
sl@0
|
181 |
e2_complete = FALSE;
|
sl@0
|
182 |
|
sl@0
|
183 |
pipe(fd);
|
sl@0
|
184 |
|
sl@0
|
185 |
pollfd.fd = fd[0];
|
sl@0
|
186 |
pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
|
sl@0
|
187 |
pollfd.revents = 0;
|
sl@0
|
188 |
|
sl@0
|
189 |
pthread_create(&thread1, NULL, thread_function, NULL);
|
sl@0
|
190 |
|
sl@0
|
191 |
context = g_main_context_new();
|
sl@0
|
192 |
|
sl@0
|
193 |
//g_main_context_add_poll(context,&pollfd,0);
|
sl@0
|
194 |
|
sl@0
|
195 |
source1 = g_source_new(&SourceFuncs,sizeof(GSource));
|
sl@0
|
196 |
g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL);
|
sl@0
|
197 |
|
sl@0
|
198 |
id = g_source_attach(source1,context);
|
sl@0
|
199 |
|
sl@0
|
200 |
g_assert(g_source_get_id(source1) == id);
|
sl@0
|
201 |
|
sl@0
|
202 |
g_source_set_priority(source1,0);
|
sl@0
|
203 |
|
sl@0
|
204 |
g_assert(g_source_get_priority(source1) == 0);
|
sl@0
|
205 |
|
sl@0
|
206 |
loop = g_main_loop_new(context, FALSE);
|
sl@0
|
207 |
|
sl@0
|
208 |
default_context = g_main_loop_get_context(loop);
|
sl@0
|
209 |
|
sl@0
|
210 |
//checks g_main_loop_get_context
|
sl@0
|
211 |
g_assert(default_context == context);
|
sl@0
|
212 |
|
sl@0
|
213 |
//checks g_main_loop_is_running
|
sl@0
|
214 |
g_assert(g_main_loop_is_running(loop) == FALSE);
|
sl@0
|
215 |
|
sl@0
|
216 |
depth = g_main_depth();
|
sl@0
|
217 |
|
sl@0
|
218 |
//checks g_main_depth
|
sl@0
|
219 |
g_assert(depth == 0);
|
sl@0
|
220 |
|
sl@0
|
221 |
g_source_get_current_time(source1,&time);
|
sl@0
|
222 |
|
sl@0
|
223 |
g_assert(time.tv_usec > 0);
|
sl@0
|
224 |
|
sl@0
|
225 |
g_source_set_can_recurse(source1,TRUE);
|
sl@0
|
226 |
|
sl@0
|
227 |
g_assert(g_source_get_can_recurse(source1) == TRUE);
|
sl@0
|
228 |
|
sl@0
|
229 |
source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource));
|
sl@0
|
230 |
g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL);
|
sl@0
|
231 |
g_source_add_poll(source2,&pollfd);
|
sl@0
|
232 |
|
sl@0
|
233 |
g_source_remove_poll(source2,&pollfd);
|
sl@0
|
234 |
|
sl@0
|
235 |
// checks g_source_remove_poll
|
sl@0
|
236 |
g_assert(source2->poll_fds == NULL);
|
sl@0
|
237 |
|
sl@0
|
238 |
g_source_add_poll(source2,&pollfd);
|
sl@0
|
239 |
|
sl@0
|
240 |
// checks whether g_source_add_poll is successful.
|
sl@0
|
241 |
// one more check is done in fd_callback.
|
sl@0
|
242 |
// If that function is callled we are sure that add poll was successful
|
sl@0
|
243 |
g_assert(source2->poll_fds->data == &pollfd);
|
sl@0
|
244 |
|
sl@0
|
245 |
source3 = g_source_ref(source2);
|
sl@0
|
246 |
|
sl@0
|
247 |
g_assert(source3 == source2 && source2->ref_count == 2);
|
sl@0
|
248 |
|
sl@0
|
249 |
g_source_unref(source3);
|
sl@0
|
250 |
|
sl@0
|
251 |
id = g_source_attach(source2,context);
|
sl@0
|
252 |
|
sl@0
|
253 |
//checks g_main_context_pending
|
sl@0
|
254 |
g_assert(g_main_context_pending(context));
|
sl@0
|
255 |
|
sl@0
|
256 |
g_main_loop_run(loop);
|
sl@0
|
257 |
|
sl@0
|
258 |
// ref is called here. Thats why two unrefs are called. If the 2nd unref is
|
sl@0
|
259 |
// callled with the unref, code should crash
|
sl@0
|
260 |
g_main_loop_ref(loop);
|
sl@0
|
261 |
|
sl@0
|
262 |
g_main_loop_unref(loop);
|
sl@0
|
263 |
|
sl@0
|
264 |
g_main_loop_unref(loop);
|
sl@0
|
265 |
|
sl@0
|
266 |
//checks the number of times the call back function is called
|
sl@0
|
267 |
g_assert(user_data == 100);
|
sl@0
|
268 |
|
sl@0
|
269 |
// checks whether set poll was successful and call back for the same
|
sl@0
|
270 |
// was called
|
sl@0
|
271 |
g_assert(fd_data == 1);
|
sl@0
|
272 |
}
|
sl@0
|
273 |
|
sl@0
|
274 |
gint dummy_poll_func(GPollFD *ufds,guint nfsd,gint timeout_)
|
sl@0
|
275 |
{
|
sl@0
|
276 |
return 0;
|
sl@0
|
277 |
}
|
sl@0
|
278 |
|
sl@0
|
279 |
|
sl@0
|
280 |
void misc_test()
|
sl@0
|
281 |
{
|
sl@0
|
282 |
GPollFunc func = g_main_context_get_poll_func(context);
|
sl@0
|
283 |
|
sl@0
|
284 |
g_assert(func != NULL);
|
sl@0
|
285 |
|
sl@0
|
286 |
g_main_context_set_poll_func(context,dummy_poll_func);
|
sl@0
|
287 |
func = g_main_context_get_poll_func(context);
|
sl@0
|
288 |
|
sl@0
|
289 |
g_assert(func == dummy_poll_func);
|
sl@0
|
290 |
}
|
sl@0
|
291 |
|
sl@0
|
292 |
gboolean function(int *data)
|
sl@0
|
293 |
{
|
sl@0
|
294 |
*data = 1;
|
sl@0
|
295 |
g_main_loop_quit(idle_loop);
|
sl@0
|
296 |
return FALSE;
|
sl@0
|
297 |
}
|
sl@0
|
298 |
|
sl@0
|
299 |
void idle_test()
|
sl@0
|
300 |
{
|
sl@0
|
301 |
int user_data = 0;
|
sl@0
|
302 |
gboolean retVal;
|
sl@0
|
303 |
GSource *s = g_idle_source_new();
|
sl@0
|
304 |
|
sl@0
|
305 |
idle_loop = g_main_loop_new(NULL, FALSE);
|
sl@0
|
306 |
g_source_attach(s,context);
|
sl@0
|
307 |
|
sl@0
|
308 |
g_idle_add((GSourceFunc)function,&user_data);
|
sl@0
|
309 |
|
sl@0
|
310 |
retVal = g_idle_remove_by_data(&user_data);
|
sl@0
|
311 |
|
sl@0
|
312 |
//checks g_idle_remove_by_data
|
sl@0
|
313 |
g_assert(retVal == TRUE);
|
sl@0
|
314 |
retVal = FALSE;
|
sl@0
|
315 |
|
sl@0
|
316 |
g_idle_add((GSourceFunc)function,&user_data);
|
sl@0
|
317 |
|
sl@0
|
318 |
retVal = g_source_remove_by_user_data(&user_data);
|
sl@0
|
319 |
|
sl@0
|
320 |
//checks g_source_remove_by_user_data
|
sl@0
|
321 |
g_assert(retVal == TRUE);
|
sl@0
|
322 |
|
sl@0
|
323 |
g_idle_add((GSourceFunc)function,&user_data);
|
sl@0
|
324 |
|
sl@0
|
325 |
g_main_loop_run(idle_loop);
|
sl@0
|
326 |
|
sl@0
|
327 |
g_main_loop_unref(idle_loop);
|
sl@0
|
328 |
|
sl@0
|
329 |
//checks whether the function was run or not
|
sl@0
|
330 |
g_assert(user_data == 1);
|
sl@0
|
331 |
}
|
sl@0
|
332 |
|
sl@0
|
333 |
int main()
|
sl@0
|
334 |
{
|
sl@0
|
335 |
|
sl@0
|
336 |
#ifdef SYMBIAN
|
sl@0
|
337 |
|
sl@0
|
338 |
g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
|
sl@0
|
339 |
#endif
|
sl@0
|
340 |
|
sl@0
|
341 |
//main_loop_test();
|
sl@0
|
342 |
//misc_test();
|
sl@0
|
343 |
//idle_test();
|
sl@0
|
344 |
|
sl@0
|
345 |
|
sl@0
|
346 |
#ifdef SYMBIAN
|
sl@0
|
347 |
testResultXml("main_loop_test");
|
sl@0
|
348 |
#endif /* EMULATOR */
|
sl@0
|
349 |
|
sl@0
|
350 |
return 0;
|
sl@0
|
351 |
} |