Remove unnecessary includes
[platform/upstream/glib.git] / glib / tests / mainloop.c
1 /* Unit tests for GMainLoop
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work 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.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 #include <glib.h>
24
25 static gboolean cb (gpointer data)
26 {
27   return FALSE;
28 }
29
30 static void
31 test_maincontext_basic (void)
32 {
33   GMainContext *ctx;
34   GSource *source;
35   GSourceFuncs funcs;
36   guint id;
37   gpointer data = &funcs;
38
39   ctx = g_main_context_new ();
40
41   g_assert (!g_main_context_pending (ctx));
42   g_assert (!g_main_context_iteration (ctx, FALSE));
43
44   source = g_source_new (&funcs, sizeof (GSource));
45   g_assert (g_source_get_priority (source) == G_PRIORITY_DEFAULT);
46
47   g_assert (!g_source_get_can_recurse (source));
48   g_assert (g_source_get_name (source) == NULL);
49
50   g_source_set_can_recurse (source, TRUE);
51   g_source_set_name (source, "d");
52
53   g_assert (g_source_get_can_recurse (source));
54   g_assert_cmpstr (g_source_get_name (source), ==, "d");
55
56   g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
57   g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
58
59   id = g_source_attach (source, ctx);
60   g_assert_cmpint (g_source_get_id (source), ==, id);
61   g_assert (g_main_context_find_source_by_id (ctx, id) == source);
62
63   g_source_set_priority (source, G_PRIORITY_HIGH);
64   g_assert (g_source_get_priority (source) == G_PRIORITY_HIGH);
65
66   g_main_context_unref (ctx);
67
68   ctx = g_main_context_default ();
69   source = g_source_new (&funcs, sizeof (GSource));
70   g_source_set_funcs (source, &funcs);
71   g_source_set_callback (source, cb, data, NULL);
72   id = g_source_attach (source, ctx);
73   g_source_set_name_by_id (id, "e");
74   g_assert_cmpstr (g_source_get_name (source), ==, "e");
75   g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
76 }
77
78 static void
79 test_mainloop_basic (void)
80 {
81   GMainLoop *loop;
82   GMainContext *ctx;
83
84   loop = g_main_loop_new (NULL, FALSE);
85
86   g_assert (!g_main_loop_is_running (loop));
87
88   g_main_loop_ref (loop);
89
90   ctx = g_main_loop_get_context (loop);
91   g_assert (ctx == g_main_context_default ());
92
93   g_main_loop_unref (loop);
94
95   g_assert (g_main_depth () == 0);
96 }
97
98 int
99 main (int argc, char *argv[])
100 {
101   g_test_init (&argc, &argv, NULL);
102
103   g_test_add_func ("/maincontext/basic", test_maincontext_basic);
104   g_test_add_func ("/mainloop/basic", test_mainloop_basic);
105
106   return g_test_run ();
107 }