sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include "glib.h" sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: GMainLoop *loop,*idle_loop; sl@0: GMainContext *context; sl@0: int fd[2]; sl@0: char *buf[100]; sl@0: GSource *source1,*source2; sl@0: pthread_t thread1; sl@0: gboolean e1_complete,e2_complete; sl@0: sl@0: void quit() sl@0: { sl@0: if(e1_complete && e2_complete) sl@0: g_main_loop_quit(loop); sl@0: } sl@0: sl@0: sl@0: gboolean prepare(GSource *source,gint *timeout) sl@0: { sl@0: *timeout = 0; sl@0: return TRUE; sl@0: } sl@0: sl@0: gboolean check(GSource *source) sl@0: { sl@0: return TRUE; sl@0: } sl@0: sl@0: gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data) sl@0: { sl@0: if(callback(user_data)) sl@0: return TRUE; sl@0: else sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean my_callback(int* data) sl@0: { sl@0: static int i = 100; sl@0: int depth; sl@0: sl@0: //checks g_main_loop_is_running sl@0: g_assert(g_main_loop_is_running(loop)); sl@0: sl@0: depth = g_main_depth(); sl@0: sl@0: //checks g_main_depth sl@0: g_assert(depth == 1); sl@0: sl@0: i--; sl@0: sl@0: //printf("%d\n",i); sl@0: sl@0: if(i<0) sl@0: { sl@0: e1_complete = TRUE; sl@0: quit(); sl@0: return FALSE; sl@0: } sl@0: else sl@0: { sl@0: ++(*data); sl@0: return TRUE; sl@0: } sl@0: } sl@0: sl@0: sl@0: gboolean fd_prepare(GSource *source,gint *timeout) sl@0: { sl@0: *timeout = -1; sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean fd_check(GSource *source) sl@0: { sl@0: GPollFD *pollfd = source->poll_fds->data; sl@0: if(pollfd->revents && (G_IO_IN | G_IO_HUP | G_IO_ERR)) sl@0: return TRUE; sl@0: else sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean fd_dispatch(GSource *source,GSourceFunc callback,gpointer user_data) sl@0: { sl@0: if(callback(user_data)) sl@0: return TRUE; sl@0: else sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean fd_callback(int* data) sl@0: { sl@0: pthread_join(thread1, NULL); sl@0: read(fd[0],buf,sizeof(buf)); sl@0: sl@0: // This asserts actually checks whether the callback for fd is successful or not sl@0: // In other words it checks if g_source_add_poll was successful or not. sl@0: g_assert(!strcmp((const char *)buf,"fd poll example")); sl@0: sl@0: *data = 1; sl@0: //printf("%s",buf); sl@0: //getchar(); sl@0: sl@0: g_source_unref(source2); sl@0: sl@0: e2_complete = TRUE; sl@0: quit(); sl@0: return FALSE; sl@0: } sl@0: sl@0: sl@0: void* thread_function(void *data) sl@0: { sl@0: sleep(1); sl@0: write(fd[1],"fd poll example",15); sl@0: return NULL; sl@0: } sl@0: sl@0: void main_loop_test() sl@0: { sl@0: GMainContext *default_context; sl@0: int depth; sl@0: int id; sl@0: GTimeVal time ={0,}; sl@0: int user_data = 0,fd_data = 0; sl@0: GPollFD pollfd; sl@0: GSource *source3; sl@0: sl@0: GSourceFuncs SourceFuncs = sl@0: { sl@0: prepare, sl@0: check, sl@0: dispatch, sl@0: NULL sl@0: }; sl@0: sl@0: GSourceFuncs fd_SourceFuncs = sl@0: { sl@0: fd_prepare, sl@0: fd_check, sl@0: fd_dispatch, sl@0: NULL sl@0: }; sl@0: sl@0: e1_complete = FALSE; sl@0: e2_complete = FALSE; sl@0: sl@0: pipe(fd); sl@0: sl@0: pollfd.fd = fd[0]; sl@0: pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; sl@0: pollfd.revents = 0; sl@0: sl@0: pthread_create(&thread1, NULL, thread_function, NULL); sl@0: sl@0: context = g_main_context_new(); sl@0: sl@0: //g_main_context_add_poll(context,&pollfd,0); sl@0: sl@0: source1 = g_source_new(&SourceFuncs,sizeof(GSource)); sl@0: g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL); sl@0: sl@0: id = g_source_attach(source1,context); sl@0: sl@0: g_assert(g_source_get_id(source1) == id); sl@0: sl@0: g_source_set_priority(source1,0); sl@0: sl@0: g_assert(g_source_get_priority(source1) == 0); sl@0: sl@0: loop = g_main_loop_new(context, FALSE); sl@0: sl@0: default_context = g_main_loop_get_context(loop); sl@0: sl@0: //checks g_main_loop_get_context sl@0: g_assert(default_context == context); sl@0: sl@0: //checks g_main_loop_is_running sl@0: g_assert(g_main_loop_is_running(loop) == FALSE); sl@0: sl@0: depth = g_main_depth(); sl@0: sl@0: //checks g_main_depth sl@0: g_assert(depth == 0); sl@0: sl@0: g_source_get_current_time(source1,&time); sl@0: sl@0: g_assert(time.tv_usec > 0); sl@0: sl@0: g_source_set_can_recurse(source1,TRUE); sl@0: sl@0: g_assert(g_source_get_can_recurse(source1) == TRUE); sl@0: sl@0: source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource)); sl@0: g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL); sl@0: g_source_add_poll(source2,&pollfd); sl@0: sl@0: g_source_remove_poll(source2,&pollfd); sl@0: sl@0: // checks g_source_remove_poll sl@0: g_assert(source2->poll_fds == NULL); sl@0: sl@0: g_source_add_poll(source2,&pollfd); sl@0: sl@0: // checks whether g_source_add_poll is successful. sl@0: // one more check is done in fd_callback. sl@0: // If that function is callled we are sure that add poll was successful sl@0: g_assert(source2->poll_fds->data == &pollfd); sl@0: sl@0: source3 = g_source_ref(source2); sl@0: sl@0: g_assert(source3 == source2 && source2->ref_count == 2); sl@0: sl@0: g_source_unref(source3); sl@0: sl@0: id = g_source_attach(source2,context); sl@0: sl@0: //checks g_main_context_pending sl@0: g_assert(g_main_context_pending(context)); sl@0: sl@0: g_main_loop_run(loop); sl@0: sl@0: // ref is called here. Thats why two unrefs are called. If the 2nd unref is sl@0: // callled with the unref, code should crash sl@0: g_main_loop_ref(loop); sl@0: sl@0: g_main_loop_unref(loop); sl@0: sl@0: g_main_loop_unref(loop); sl@0: sl@0: //checks the number of times the call back function is called sl@0: g_assert(user_data == 100); sl@0: sl@0: // checks whether set poll was successful and call back for the same sl@0: // was called sl@0: g_assert(fd_data == 1); sl@0: } sl@0: sl@0: gint dummy_poll_func(GPollFD *ufds,guint nfsd,gint timeout_) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: void misc_test() sl@0: { sl@0: GPollFunc func = g_main_context_get_poll_func(context); sl@0: sl@0: g_assert(func != NULL); sl@0: sl@0: g_main_context_set_poll_func(context,dummy_poll_func); sl@0: func = g_main_context_get_poll_func(context); sl@0: sl@0: g_assert(func == dummy_poll_func); sl@0: } sl@0: sl@0: gboolean function(int *data) sl@0: { sl@0: *data = 1; sl@0: g_main_loop_quit(idle_loop); sl@0: return FALSE; sl@0: } sl@0: sl@0: void idle_test() sl@0: { sl@0: int user_data = 0; sl@0: gboolean retVal; sl@0: GSource *s = g_idle_source_new(); sl@0: sl@0: idle_loop = g_main_loop_new(NULL, FALSE); sl@0: g_source_attach(s,context); sl@0: sl@0: g_idle_add((GSourceFunc)function,&user_data); sl@0: sl@0: retVal = g_idle_remove_by_data(&user_data); sl@0: sl@0: //checks g_idle_remove_by_data sl@0: g_assert(retVal == TRUE); sl@0: retVal = FALSE; sl@0: sl@0: g_idle_add((GSourceFunc)function,&user_data); sl@0: sl@0: retVal = g_source_remove_by_user_data(&user_data); sl@0: sl@0: //checks g_source_remove_by_user_data sl@0: g_assert(retVal == TRUE); sl@0: sl@0: g_idle_add((GSourceFunc)function,&user_data); sl@0: sl@0: g_main_loop_run(idle_loop); sl@0: sl@0: g_main_loop_unref(idle_loop); sl@0: sl@0: //checks whether the function was run or not sl@0: g_assert(user_data == 1); sl@0: } sl@0: sl@0: int main() sl@0: { sl@0: sl@0: #ifdef SYMBIAN sl@0: sl@0: 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: #endif sl@0: sl@0: //main_loop_test(); sl@0: //misc_test(); sl@0: //idle_test(); sl@0: sl@0: sl@0: #ifdef SYMBIAN sl@0: testResultXml("main_loop_test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 0; sl@0: }