~ubuntu-branches/ubuntu/saucy/glib2.0/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/gobject/signals.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-09-25 10:40:24 UTC
  • mfrom: (1.63.27) (172.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130925104024-e64unm8urjjmpr9a
Tags: 2.38.0-1ubuntu1
* Resynchronise on Debian, remaining change
  - Build-Depend on python:any for cross building

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General
 
16
 * Public License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#undef        G_LOG_DOMAIN
 
22
#define       G_LOG_DOMAIN "TestSignals"
 
23
 
 
24
#undef G_DISABLE_ASSERT
 
25
#undef G_DISABLE_CHECKS
 
26
#undef G_DISABLE_CAST_CHECKS
 
27
 
 
28
#include <glib-object.h>
 
29
 
 
30
#include "testcommon.h"
 
31
 
 
32
/* What this test tests is the behavior of signal disconnection
 
33
 * from within a signal handler for the signal being disconnected.
 
34
 *
 
35
 * The test demonstrates that signal handlers disconnected from a signal
 
36
 * from an earlier handler in the same emission will not be run.
 
37
 *
 
38
 * It also demonstrates that signal handlers connected from a signal
 
39
 * from an earlier handler in the same emission will not be run.
 
40
 */
 
41
 
 
42
/*
 
43
 * TestObject, a parent class for TestObject
 
44
 */
 
45
#define TEST_TYPE_OBJECT         (test_object_get_type ())
 
46
typedef struct _TestObject        TestObject;
 
47
typedef struct _TestObjectClass   TestObjectClass;
 
48
static gboolean callback1_ran = FALSE, callback2_ran = FALSE, callback3_ran = FALSE, default_handler_ran = FALSE;
 
49
 
 
50
struct _TestObject
 
51
{
 
52
  GObject parent_instance;
 
53
};
 
54
struct _TestObjectClass
 
55
{
 
56
  GObjectClass parent_class;
 
57
 
 
58
  void   (*test_signal) (TestObject *object);
 
59
};
 
60
 
 
61
static GType test_object_get_type (void);
 
62
 
 
63
static void
 
64
test_object_real_signal (TestObject *object)
 
65
{
 
66
  default_handler_ran = TRUE;
 
67
}
 
68
 
 
69
static void
 
70
test_object_signal_callback3 (TestObject *object,
 
71
                              gpointer    data)
 
72
{
 
73
  callback3_ran = TRUE;
 
74
}
 
75
 
 
76
static void
 
77
test_object_signal_callback2 (TestObject *object,
 
78
                              gpointer    data)
 
79
{
 
80
  callback2_ran = TRUE;
 
81
}
 
82
 
 
83
static void
 
84
test_object_signal_callback1 (TestObject *object,
 
85
                              gpointer    data)
 
86
{
 
87
  callback1_ran = TRUE;
 
88
  g_signal_handlers_disconnect_by_func (G_OBJECT (object),
 
89
                                        test_object_signal_callback2,
 
90
                                        data);
 
91
  g_signal_connect (object, "test-signal",
 
92
                    G_CALLBACK (test_object_signal_callback3), NULL);
 
93
}
 
94
 
 
95
static void
 
96
test_object_class_init (TestObjectClass *class)
 
97
{
 
98
  class->test_signal = test_object_real_signal;
 
99
 
 
100
  g_signal_new ("test-signal",
 
101
                G_OBJECT_CLASS_TYPE (class),
 
102
                G_SIGNAL_RUN_LAST,
 
103
                G_STRUCT_OFFSET (TestObjectClass, test_signal),
 
104
                NULL, NULL, NULL, G_TYPE_NONE, 0);
 
105
}
 
106
 
 
107
static DEFINE_TYPE(TestObject, test_object,
 
108
                   test_object_class_init, NULL, NULL,
 
109
                   G_TYPE_OBJECT)
 
110
 
 
111
int
 
112
main (int   argc,
 
113
      char *argv[])
 
114
{
 
115
  TestObject *object;
 
116
 
 
117
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
 
118
                          G_LOG_LEVEL_WARNING |
 
119
                          G_LOG_LEVEL_CRITICAL);
 
120
 
 
121
  object = g_object_new (TEST_TYPE_OBJECT, NULL);
 
122
 
 
123
  g_signal_connect (object, "test-signal",
 
124
                    G_CALLBACK (test_object_signal_callback1), NULL);
 
125
  g_signal_connect (object, "test-signal",
 
126
                    G_CALLBACK (test_object_signal_callback2), NULL);
 
127
  g_signal_emit_by_name (object, "test-signal");
 
128
 
 
129
  g_assert (callback1_ran);
 
130
  g_assert (!callback2_ran);
 
131
  g_assert (!callback3_ran);
 
132
  g_assert (default_handler_ran);
 
133
 
 
134
  return 0;
 
135
}