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.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
21 #include <glib-object.h>
23 #include "testcommon.h"
25 /* What this test tests is the behavior of signal disconnection
26 * from within a signal handler for the signal being disconnected.
28 * The test demonstrates that signal handlers disconnected from a signal
29 * from an earlier handler in the same emission will not be run.
31 * It also demonstrates that signal handlers connected from a signal
32 * from an earlier handler in the same emission will not be run.
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;
40 static gboolean callback1_ran = FALSE, callback2_ran = FALSE,
41 callback3_ran = FALSE, default_handler_ran = FALSE;
45 GObject parent_instance;
48 struct _TestObjectClass
50 GObjectClass parent_class;
52 void (*test_signal) (TestObject *object);
55 static GType test_object_get_type (void);
58 test_object_real_signal (TestObject *object)
60 default_handler_ran = TRUE;
64 test_object_signal_callback3 (TestObject *object,
71 test_object_signal_callback2 (TestObject *object,
78 test_object_signal_callback1 (TestObject *object,
82 g_signal_handlers_disconnect_by_func (G_OBJECT (object),
83 test_object_signal_callback2,
85 g_signal_connect (object, "test-signal",
86 G_CALLBACK (test_object_signal_callback3), NULL);
90 test_object_class_init (TestObjectClass *class)
92 class->test_signal = test_object_real_signal;
94 g_signal_new ("test-signal",
95 G_OBJECT_CLASS_TYPE (class),
97 G_STRUCT_OFFSET (TestObjectClass, test_signal),
98 NULL, NULL, NULL, G_TYPE_NONE, 0);
101 static DEFINE_TYPE(TestObject, test_object,
102 test_object_class_init, NULL, NULL,
106 test_basic_signals (void)
110 object = g_object_new (TEST_TYPE_OBJECT, NULL);
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");
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);
123 g_object_unref (object);
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);
134 g_test_init (&argc, &argv, NULL);
136 g_test_add_func ("/gobject/basic-signals", test_basic_signals);
138 return g_test_run ();