8e8ba5de2675ed76074e7db7b837ae0aa686156c
[profile/ivi/pygobject2.git] / tests / test-floating.c
1 /*
2  * test-floating.c - Source for TestFloatingWithSinkFunc and TestFloatingWithoutSinkFunc
3  * Copyright (C) 2010 Collabora Ltd.
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.1 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 Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "test-floating.h"
21
22 /* TestFloatingWithSinkFunc */
23
24 G_DEFINE_TYPE(TestFloatingWithSinkFunc, test_floating_with_sink_func, G_TYPE_INITIALLY_UNOWNED)
25
26 static void
27 test_floating_with_sink_func_finalize (GObject *gobject)
28 {
29   TestFloatingWithSinkFunc *object = TEST_FLOATING_WITH_SINK_FUNC (gobject);
30
31   if (g_object_is_floating (object))
32     {
33       g_warning ("A floating object was finalized. This means that someone\n"
34                  "called g_object_unref() on an object that had only a floating\n"
35                  "reference; the initial floating reference is not owned by anyone\n"
36                  "and must be removed with g_object_ref_sink().");
37     }
38   
39   G_OBJECT_CLASS (test_floating_with_sink_func_parent_class)->finalize (gobject);
40 }
41
42 static void
43 test_floating_with_sink_func_class_init (TestFloatingWithSinkFuncClass *klass)
44 {
45   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
46
47   gobject_class->finalize = test_floating_with_sink_func_finalize;
48 }
49
50 static void
51 test_floating_with_sink_func_init (TestFloatingWithSinkFunc *self)
52 {
53 }
54
55 void
56 sink_test_floating_with_sink_func (GObject *object)
57 {
58     if (g_object_is_floating(object)) {
59             g_object_ref_sink(object);
60     }
61 }
62
63 /* TestFloatingWithoutSinkFunc */
64
65 G_DEFINE_TYPE(TestFloatingWithoutSinkFunc, test_floating_without_sink_func, G_TYPE_INITIALLY_UNOWNED)
66
67 static void
68 test_floating_without_sink_func_finalize (GObject *gobject)
69 {
70   TestFloatingWithoutSinkFunc *object = TEST_FLOATING_WITHOUT_SINK_FUNC (gobject);
71
72   if (g_object_is_floating (object))
73     {
74       g_warning ("A floating object was finalized. This means that someone\n"
75                  "called g_object_unref() on an object that had only a floating\n"
76                  "reference; the initial floating reference is not owned by anyone\n"
77                  "and must be removed without g_object_ref_sink().");
78     }
79
80   G_OBJECT_CLASS (test_floating_without_sink_func_parent_class)->finalize (gobject);
81 }
82
83 static void
84 test_floating_without_sink_func_class_init (TestFloatingWithoutSinkFuncClass *klass)
85 {
86   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87
88   gobject_class->finalize = test_floating_without_sink_func_finalize;
89 }
90
91 static void
92 test_floating_without_sink_func_init (TestFloatingWithoutSinkFunc *self)
93 {
94 }
95
96 /* TestOwnedByLibrary */
97
98 G_DEFINE_TYPE(TestOwnedByLibrary, test_owned_by_library, G_TYPE_OBJECT)
99
100 static GSList *obl_instance_list = NULL;
101
102 static void
103 test_owned_by_library_class_init (TestOwnedByLibraryClass *klass)
104 {
105 }
106
107 static void
108 test_owned_by_library_init (TestOwnedByLibrary *self)
109 {
110     g_object_ref (self);
111     obl_instance_list = g_slist_prepend (obl_instance_list, self);
112 }
113
114 void
115 test_owned_by_library_release (TestOwnedByLibrary *self)
116 {
117     obl_instance_list = g_slist_remove (obl_instance_list, self);
118     g_object_unref (self);
119 }
120
121 GSList *
122 test_owned_by_library_get_instance_list (void)
123 {
124     return obl_instance_list;
125 }
126
127 /* TestFloatingAndSunk
128  * This object is mimicking the GtkWindow behaviour, ie a GInitiallyUnowned subclass
129  * whose floating reference has already been sunk when g_object_new() returns it.
130  * The reference is already sunk because the instance is already owned by the instance
131  * list.
132  */
133
134 G_DEFINE_TYPE(TestFloatingAndSunk, test_floating_and_sunk, G_TYPE_INITIALLY_UNOWNED)
135
136 static GSList *fas_instance_list = NULL;
137
138 static void
139 test_floating_and_sunk_class_init (TestFloatingAndSunkClass *klass)
140 {
141 }
142
143 static void
144 test_floating_and_sunk_init (TestFloatingAndSunk *self)
145 {
146     g_object_ref_sink (self);
147     fas_instance_list = g_slist_prepend (fas_instance_list, self);
148 }
149
150 void
151 test_floating_and_sunk_release (TestFloatingAndSunk *self)
152 {
153     fas_instance_list = g_slist_remove (fas_instance_list, self);
154     g_object_unref (self);
155 }
156
157 GSList *
158 test_floating_and_sunk_get_instance_list (void)
159 {
160     return fas_instance_list;
161 }