First public contribution.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #undef G_DISABLE_ASSERT
35 #include "mrt2_glib2_test.h"
38 GMainLoop *loop,*idle_loop;
39 GMainContext *context;
42 GSource *source1,*source2;
44 gboolean e1_complete,e2_complete;
48 if(e1_complete && e2_complete)
49 g_main_loop_quit(loop);
53 gboolean prepare(GSource *source,gint *timeout)
59 gboolean check(GSource *source)
64 gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
66 if(callback(user_data))
72 gboolean my_callback(int* data)
77 //checks g_main_loop_is_running
78 g_assert(g_main_loop_is_running(loop));
80 depth = g_main_depth();
103 gboolean fd_prepare(GSource *source,gint *timeout)
109 gboolean fd_check(GSource *source)
111 GPollFD *pollfd = source->poll_fds->data;
112 if(pollfd->revents && (G_IO_IN | G_IO_HUP | G_IO_ERR))
118 gboolean fd_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
120 if(callback(user_data))
126 gboolean fd_callback(int* data)
128 pthread_join(thread1, NULL);
129 read(fd[0],buf,sizeof(buf));
131 // This asserts actually checks whether the callback for fd is successful or not
132 // In other words it checks if g_source_add_poll was successful or not.
133 g_assert(!strcmp((const char *)buf,"fd poll example"));
139 g_source_unref(source2);
147 void* thread_function(void *data)
150 write(fd[1],"fd poll example",15);
154 void main_loop_test()
156 GMainContext *default_context;
160 int user_data = 0,fd_data = 0;
164 GSourceFuncs SourceFuncs =
172 GSourceFuncs fd_SourceFuncs =
186 pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
189 pthread_create(&thread1, NULL, thread_function, NULL);
191 context = g_main_context_new();
193 //g_main_context_add_poll(context,&pollfd,0);
195 source1 = g_source_new(&SourceFuncs,sizeof(GSource));
196 g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL);
198 id = g_source_attach(source1,context);
200 g_assert(g_source_get_id(source1) == id);
202 g_source_set_priority(source1,0);
204 g_assert(g_source_get_priority(source1) == 0);
206 loop = g_main_loop_new(context, FALSE);
208 default_context = g_main_loop_get_context(loop);
210 //checks g_main_loop_get_context
211 g_assert(default_context == context);
213 //checks g_main_loop_is_running
214 g_assert(g_main_loop_is_running(loop) == FALSE);
216 depth = g_main_depth();
218 //checks g_main_depth
219 g_assert(depth == 0);
221 g_source_get_current_time(source1,&time);
223 g_assert(time.tv_usec > 0);
225 g_source_set_can_recurse(source1,TRUE);
227 g_assert(g_source_get_can_recurse(source1) == TRUE);
229 source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource));
230 g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL);
231 g_source_add_poll(source2,&pollfd);
233 g_source_remove_poll(source2,&pollfd);
235 // checks g_source_remove_poll
236 g_assert(source2->poll_fds == NULL);
238 g_source_add_poll(source2,&pollfd);
240 // checks whether g_source_add_poll is successful.
241 // one more check is done in fd_callback.
242 // If that function is callled we are sure that add poll was successful
243 g_assert(source2->poll_fds->data == &pollfd);
245 source3 = g_source_ref(source2);
247 g_assert(source3 == source2 && source2->ref_count == 2);
249 g_source_unref(source3);
251 id = g_source_attach(source2,context);
253 //checks g_main_context_pending
254 g_assert(g_main_context_pending(context));
256 g_main_loop_run(loop);
258 // ref is called here. Thats why two unrefs are called. If the 2nd unref is
259 // callled with the unref, code should crash
260 g_main_loop_ref(loop);
262 g_main_loop_unref(loop);
264 g_main_loop_unref(loop);
266 //checks the number of times the call back function is called
267 g_assert(user_data == 100);
269 // checks whether set poll was successful and call back for the same
271 g_assert(fd_data == 1);
274 gint dummy_poll_func(GPollFD *ufds,guint nfsd,gint timeout_)
282 GPollFunc func = g_main_context_get_poll_func(context);
284 g_assert(func != NULL);
286 g_main_context_set_poll_func(context,dummy_poll_func);
287 func = g_main_context_get_poll_func(context);
289 g_assert(func == dummy_poll_func);
292 gboolean function(int *data)
295 g_main_loop_quit(idle_loop);
303 GSource *s = g_idle_source_new();
305 idle_loop = g_main_loop_new(NULL, FALSE);
306 g_source_attach(s,context);
308 g_idle_add((GSourceFunc)function,&user_data);
310 retVal = g_idle_remove_by_data(&user_data);
312 //checks g_idle_remove_by_data
313 g_assert(retVal == TRUE);
316 g_idle_add((GSourceFunc)function,&user_data);
318 retVal = g_source_remove_by_user_data(&user_data);
320 //checks g_source_remove_by_user_data
321 g_assert(retVal == TRUE);
323 g_idle_add((GSourceFunc)function,&user_data);
325 g_main_loop_run(idle_loop);
327 g_main_loop_unref(idle_loop);
329 //checks whether the function was run or not
330 g_assert(user_data == 1);
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);
347 testResultXml("main_loop_test");
348 #endif /* EMULATOR */