| 1 |
#define CLUTTER_DISABLE_DEPRECATION_WARNINGS |
| 2 |
#include <clutter/clutter.h> |
| 3 |
|
| 4 |
#define TEST_TYPE_DESTROY (test_destroy_get_type ()) |
| 5 |
#define TEST_DESTROY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_DESTROY, TestDestroy)) |
| 6 |
#define TEST_IS_DESTROY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_DESTROY)) |
| 7 |
|
| 8 |
typedef struct _TestDestroy TestDestroy; |
| 9 |
typedef struct _TestDestroyClass TestDestroyClass; |
| 10 |
|
| 11 |
struct _TestDestroy |
| 12 |
{ |
| 13 |
ClutterActor parent_instance; |
| 14 |
|
| 15 |
ClutterActor *bg; |
| 16 |
ClutterActor *label; |
| 17 |
ClutterActor *tex; |
| 18 |
|
| 19 |
GList *children; |
| 20 |
}; |
| 21 |
|
| 22 |
struct _TestDestroyClass |
| 23 |
{ |
| 24 |
ClutterActorClass parent_class; |
| 25 |
}; |
| 26 |
|
| 27 |
static void clutter_container_init (ClutterContainerIface *iface); |
| 28 |
|
| 29 |
GType test_destroy_get_type (void); |
| 30 |
|
| 31 |
G_DEFINE_TYPE_WITH_CODE (TestDestroy, test_destroy, CLUTTER_TYPE_ACTOR, |
| 32 |
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTAINER, |
| 33 |
clutter_container_init)); |
| 34 |
|
| 35 |
static void |
| 36 |
test_destroy_add (ClutterContainer *container, |
| 37 |
ClutterActor *actor) |
| 38 |
{ |
| 39 |
TestDestroy *self = TEST_DESTROY (container); |
| 40 |
|
| 41 |
if (g_test_verbose ()) |
| 42 |
g_print ("Adding '%s' (type:%s)\n", |
| 43 |
clutter_actor_get_name (actor), |
| 44 |
G_OBJECT_TYPE_NAME (actor)); |
| 45 |
|
| 46 |
self->children = g_list_prepend (self->children, actor); |
| 47 |
clutter_actor_set_parent (actor, CLUTTER_ACTOR (container)); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
test_destroy_remove (ClutterContainer *container, |
| 52 |
ClutterActor *actor) |
| 53 |
{ |
| 54 |
TestDestroy *self = TEST_DESTROY (container); |
| 55 |
|
| 56 |
if (g_test_verbose ()) |
| 57 |
g_print ("Removing '%s' (type:%s)\n", |
| 58 |
clutter_actor_get_name (actor), |
| 59 |
G_OBJECT_TYPE_NAME (actor)); |
| 60 |
|
| 61 |
g_assert (actor != self->bg); |
| 62 |
g_assert (actor != self->label); |
| 63 |
|
| 64 |
if (!g_list_find (self->children, actor)) |
| 65 |
g_assert (actor == self->tex); |
| 66 |
else |
| 67 |
self->children = g_list_remove (self->children, actor); |
| 68 |
|
| 69 |
clutter_actor_unparent (actor); |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
clutter_container_init (ClutterContainerIface *iface) |
| 74 |
{ |
| 75 |
iface->add = test_destroy_add; |
| 76 |
iface->remove = test_destroy_remove; |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
test_destroy_destroy (ClutterActor *self) |
| 81 |
{ |
| 82 |
TestDestroy *test = TEST_DESTROY (self); |
| 83 |
|
| 84 |
if (test->bg != NULL) |
| 85 |
{ |
| 86 |
if (g_test_verbose ()) |
| 87 |
g_print ("Destroying '%s' (type:%s)\n", |
| 88 |
clutter_actor_get_name (test->bg), |
| 89 |
G_OBJECT_TYPE_NAME (test->bg)); |
| 90 |
|
| 91 |
clutter_actor_destroy (test->bg); |
| 92 |
test->bg = NULL; |
| 93 |
} |
| 94 |
|
| 95 |
if (test->label != NULL) |
| 96 |
{ |
| 97 |
if (g_test_verbose ()) |
| 98 |
g_print ("Destroying '%s' (type:%s)\n", |
| 99 |
clutter_actor_get_name (test->label), |
| 100 |
G_OBJECT_TYPE_NAME (test->label)); |
| 101 |
|
| 102 |
clutter_actor_destroy (test->label); |
| 103 |
test->label = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
if (test->tex != NULL) |
| 107 |
{ |
| 108 |
if (g_test_verbose ()) |
| 109 |
g_print ("Destroying '%s' (type:%s)\n", |
| 110 |
clutter_actor_get_name (test->tex), |
| 111 |
G_OBJECT_TYPE_NAME (test->tex)); |
| 112 |
|
| 113 |
clutter_actor_destroy (test->tex); |
| 114 |
test->tex = NULL; |
| 115 |
} |
| 116 |
|
| 117 |
g_list_foreach (test->children, (GFunc) clutter_actor_destroy, NULL); |
| 118 |
g_list_free (test->children); |
| 119 |
test->children = NULL; |
| 120 |
|
| 121 |
if (CLUTTER_ACTOR_CLASS (test_destroy_parent_class)->destroy) |
| 122 |
CLUTTER_ACTOR_CLASS (test_destroy_parent_class)->destroy (self); |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
test_destroy_class_init (TestDestroyClass *klass) |
| 127 |
{ |
| 128 |
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass); |
| 129 |
|
| 130 |
actor_class->destroy = test_destroy_destroy; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
test_destroy_init (TestDestroy *self) |
| 135 |
{ |
| 136 |
clutter_actor_push_internal (CLUTTER_ACTOR (self)); |
| 137 |
|
| 138 |
if (g_test_verbose ()) |
| 139 |
g_print ("Adding internal children...\n"); |
| 140 |
|
| 141 |
self->bg = clutter_rectangle_new (); |
| 142 |
clutter_actor_set_parent (self->bg, CLUTTER_ACTOR (self)); |
| 143 |
clutter_actor_set_name (self->bg, "Background"); |
| 144 |
|
| 145 |
self->label = clutter_text_new (); |
| 146 |
clutter_actor_set_parent (self->label, CLUTTER_ACTOR (self)); |
| 147 |
clutter_actor_set_name (self->label, "Label"); |
| 148 |
|
| 149 |
clutter_actor_pop_internal (CLUTTER_ACTOR (self)); |
| 150 |
|
| 151 |
self->tex = clutter_texture_new (); |
| 152 |
clutter_actor_set_parent (self->tex, CLUTTER_ACTOR (self)); |
| 153 |
clutter_actor_set_name (self->tex, "Texture"); |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
on_destroy (ClutterActor *actor, |
| 158 |
gpointer data) |
| 159 |
{ |
| 160 |
gboolean *destroy_called = data; |
| 161 |
|
| 162 |
*destroy_called = TRUE; |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
actor_destruction (void) |
| 167 |
{ |
| 168 |
ClutterActor *test = g_object_new (TEST_TYPE_DESTROY, NULL); |
| 169 |
ClutterActor *child = clutter_rectangle_new (); |
| 170 |
gboolean destroy_called = FALSE; |
| 171 |
|
| 172 |
g_object_ref_sink (test); |
| 173 |
|
| 174 |
g_object_add_weak_pointer (G_OBJECT (test), (gpointer *) &test); |
| 175 |
g_object_add_weak_pointer (G_OBJECT (child), (gpointer *) &child); |
| 176 |
|
| 177 |
if (g_test_verbose ()) |
| 178 |
g_print ("Adding external child...\n"); |
| 179 |
|
| 180 |
clutter_actor_set_name (child, "Child"); |
| 181 |
clutter_container_add_actor (CLUTTER_CONTAINER (test), child); |
| 182 |
g_signal_connect (child, "destroy", G_CALLBACK (on_destroy), &destroy_called); |
| 183 |
|
| 184 |
if (g_test_verbose ()) |
| 185 |
g_print ("Calling destroy()...\n"); |
| 186 |
|
| 187 |
clutter_actor_destroy (test); |
| 188 |
g_assert (destroy_called); |
| 189 |
g_assert_null (child); |
| 190 |
g_assert_null (test); |
| 191 |
} |
| 192 |
|
| 193 |
CLUTTER_TEST_SUITE ( |
| 194 |
CLUTTER_TEST_UNIT ("/actor/destruction", actor_destruction) |
| 195 |
) |