gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gobject / tests / basic-signals.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2013 Red Hat, Inc.
3  * Copy and pasted from accumulator.c and modified.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <glib-object.h>
22
23 #include "testcommon.h"
24
25 /* What this test tests is the behavior of signal disconnection
26  * from within a signal handler for the signal being disconnected.
27  *
28  * The test demonstrates that signal handlers disconnected from a signal
29  * from an earlier handler in the same emission will not be run.
30  *
31  * It also demonstrates that signal handlers connected from a signal
32  * from an earlier handler in the same emission will not be run.
33  */
34
35 /* TestObject, a parent class for TestObject */
36 #define TEST_TYPE_OBJECT         (test_object_get_type ())
37 typedef struct _TestObject        TestObject;
38 typedef struct _TestObjectClass   TestObjectClass;
39
40 static gboolean callback1_ran = FALSE, callback2_ran = FALSE,
41   callback3_ran = FALSE, default_handler_ran = FALSE;
42
43 struct _TestObject
44 {
45   GObject parent_instance;
46 };
47
48 struct _TestObjectClass
49 {
50   GObjectClass parent_class;
51
52   void   (*test_signal) (TestObject *object);
53 };
54
55 static GType test_object_get_type (void);
56
57 static void
58 test_object_real_signal (TestObject *object)
59 {
60   default_handler_ran = TRUE;
61 }
62
63 static void
64 test_object_signal_callback3 (TestObject *object,
65                               gpointer    data)
66 {
67   callback3_ran = TRUE;
68 }
69
70 static void
71 test_object_signal_callback2 (TestObject *object,
72                               gpointer    data)
73 {
74   callback2_ran = TRUE;
75 }
76
77 static void
78 test_object_signal_callback1 (TestObject *object,
79                               gpointer    data)
80 {
81   callback1_ran = TRUE;
82   g_signal_handlers_disconnect_by_func (G_OBJECT (object),
83                                         test_object_signal_callback2,
84                                         data);
85   g_signal_connect (object, "test-signal",
86                     G_CALLBACK (test_object_signal_callback3), NULL);
87 }
88
89 static void
90 test_object_class_init (TestObjectClass *class)
91 {
92   class->test_signal = test_object_real_signal;
93
94   g_signal_new ("test-signal",
95                 G_OBJECT_CLASS_TYPE (class),
96                 G_SIGNAL_RUN_LAST,
97                 G_STRUCT_OFFSET (TestObjectClass, test_signal),
98                 NULL, NULL, NULL, G_TYPE_NONE, 0);
99 }
100
101 static DEFINE_TYPE(TestObject, test_object,
102                    test_object_class_init, NULL, NULL,
103                    G_TYPE_OBJECT)
104
105 static void
106 test_basic_signals (void)
107 {
108   TestObject *object;
109
110   object = g_object_new (TEST_TYPE_OBJECT, NULL);
111
112   g_signal_connect (object, "test-signal",
113                     G_CALLBACK (test_object_signal_callback1), NULL);
114   g_signal_connect (object, "test-signal",
115                     G_CALLBACK (test_object_signal_callback2), NULL);
116   g_signal_emit_by_name (object, "test-signal");
117
118   g_assert_true (callback1_ran);
119   g_assert_false (callback2_ran);
120   g_assert_false (callback3_ran);
121   g_assert_true (default_handler_ran);
122
123   g_object_unref (object);
124 }
125
126 int
127 main (int  argc,
128       char *argv[])
129 {
130   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
131                           G_LOG_LEVEL_WARNING |
132                           G_LOG_LEVEL_CRITICAL);
133
134   g_test_init (&argc, &argv, NULL);
135
136   g_test_add_func ("/gobject/basic-signals", test_basic_signals);
137
138   return g_test_run ();
139 }