tests: More safely check if objects where destroyed
authorThibault Saunier <thibault.saunier@collabora.com>
Wed, 3 Jul 2013 16:48:58 +0000 (12:48 -0400)
committerMathieu Duponchelle <mathieu.duponchelle@epitech.eu>
Tue, 9 Jul 2013 20:13:42 +0000 (22:13 +0200)
Check if an object rthat has already been freed has been destroyed is not safe.
Add a helper function that uses weak reference to check that objects that are expected
to be destroyed when unrefing an object are actually destroyed.

tests/check/ges/basic.c
tests/check/ges/clip.c
tests/check/ges/test-utils.c
tests/check/ges/test-utils.h
tests/check/ges/timelineedition.c
tests/check/ges/transition.c

index 28c3cb4..05e60f4 100644 (file)
@@ -514,11 +514,7 @@ GST_START_TEST (test_ges_timeline_remove_track)
   tmp = ges_layer_get_clips (layer);
   assert_equals_int (g_list_length (tmp), 3);
 
-  gst_object_unref (timeline);
-  fail_if (G_IS_OBJECT (layer));
-  fail_if (G_IS_OBJECT (t1));
-  fail_if (G_IS_OBJECT (t2));
-  fail_if (G_IS_OBJECT (t3));
+  check_destroyed (G_OBJECT (timeline), G_OBJECT (layer), t1, t2, t3, NULL);
 }
 
 GST_END_TEST;
index 1d76c10..ee366a5 100644 (file)
@@ -203,10 +203,8 @@ GST_START_TEST (test_split_object)
   /* 1 ref for the Clip, 1 ref for the Track and 1 ref for the timeline */
   ASSERT_OBJECT_REFCOUNT (splittrackelement, "splittrackelement", 3);
 
-  g_object_unref (timeline);
-  fail_if (G_IS_OBJECT (splitclip));
-  fail_if (G_IS_OBJECT (clip));
-  fail_if (G_IS_OBJECT (splittrackelement));
+  check_destroyed (G_OBJECT (timeline), G_OBJECT (splitclip), clip,
+      splittrackelement, NULL);
 }
 
 GST_END_TEST;
@@ -367,10 +365,8 @@ GST_START_TEST (test_clip_refcount_remove_child)
   fail_unless (called == TRUE);
   fail_if (G_IS_OBJECT (effect));
 
-  gst_object_unref (track);
-  gst_object_unref (clip);
-  fail_if (G_IS_OBJECT (track));
-  fail_if (G_IS_OBJECT (clip));
+  check_destroyed (G_OBJECT (track), NULL, NULL);
+  check_destroyed (G_OBJECT (clip), NULL, NULL);
 }
 
 GST_END_TEST;
index e0897a4..a3d6fa0 100644 (file)
 #include "test-utils.h"
 #include <gio/gio.h>
 
+typedef struct _DestroyedObjectStruct
+{
+  GObject *object;
+  gboolean destroyed;
+} DestroyedObjectStruct;
+
 gchar *
 ges_test_get_audio_only_uri (void)
 {
@@ -100,3 +106,46 @@ ges_test_create_pipeline (GESTimeline * timeline)
   return pipeline;
 
 }
+
+static void
+weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
+{
+  destroyed->destroyed = TRUE;
+}
+
+void
+check_destroyed (GObject * object_to_unref, GObject * first_object, ...)
+{
+  GObject *object;
+  GList *objs = NULL, *tmp;
+  DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
+
+  destroyed->object = object_to_unref;
+  g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
+  objs = g_list_prepend (objs, destroyed);
+
+  if (first_object) {
+    va_list varargs;
+
+    object = first_object;
+
+    va_start (varargs, first_object);
+    while (object) {
+      destroyed = g_slice_new0 (DestroyedObjectStruct);
+      destroyed->object = object;
+      g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
+      objs = g_list_prepend (objs, destroyed);
+      object = va_arg (varargs, GObject *);
+    }
+    va_end (varargs);
+  }
+  gst_object_unref (object_to_unref);
+
+  for (tmp = objs; tmp; tmp = tmp->next) {
+    fail_unless (((DestroyedObjectStruct *) tmp->data)->destroyed == TRUE,
+        "%p is not destroyed", ((DestroyedObjectStruct *) tmp->data)->object);
+    g_slice_free (DestroyedObjectStruct, tmp->data);
+  }
+  g_list_free (objs);
+
+}
index 069604e..6210dc4 100644 (file)
@@ -36,6 +36,7 @@ gchar * ges_test_get_audio_only_uri (void);
 gchar * ges_test_get_audio_video_uri (void);
 gchar * ges_test_get_image_uri (void);
 gchar * ges_test_file_uri (const gchar *filename);
+void check_destroyed (GObject *object_to_unref, GObject *first_object, ...) G_GNUC_NULL_TERMINATED;
 
 #define gnl_object_check(gnlobj, start, duration, mstart, mduration, priority, active) { \
   guint64 pstart, pdur, inpoint, pprio, pact;                  \
index b2f63f6..3d42d24 100644 (file)
@@ -470,17 +470,8 @@ GST_START_TEST (test_snapping)
   ASSERT_OBJECT_REFCOUNT (clip1, "Second clip", 1);
   ASSERT_OBJECT_REFCOUNT (clip2, "Third clip", 1);
 
-  gst_object_unref (timeline);
-
-  /* Check we destroyed everything */
-  fail_if (G_IS_OBJECT (timeline));
-  fail_if (G_IS_OBJECT (trackelement));
-  fail_if (G_IS_OBJECT (trackelement1));
-  fail_if (G_IS_OBJECT (trackelement2));
-  fail_if (G_IS_OBJECT (clip));
-  fail_if (G_IS_OBJECT (clip1));
-  fail_if (G_IS_OBJECT (clip2));
-  fail_if (G_IS_OBJECT (layer));
+  check_destroyed (G_OBJECT (timeline), G_OBJECT (trackelement),
+      trackelement1, trackelement2, clip, clip1, clip2, layer, NULL);
 }
 
 GST_END_TEST;
index 233ac64..c8906ea 100644 (file)
@@ -165,9 +165,7 @@ GST_START_TEST (test_transition_properties)
       (GES_VIDEO_TRANSITION (trackelement)), 1);
   assert_equals_int (GES_TRANSITION_CLIP (clip)->vtype, 1);
 
-  gst_object_unref (timeline);
-  fail_if (G_IS_OBJECT (track));
-  fail_if (G_IS_OBJECT (clip));
+  check_destroyed (G_OBJECT (timeline), G_OBJECT (track), clip, NULL);
 }
 
 GST_END_TEST;