caps: improve _do_simplify
[platform/upstream/gstreamer.git] / tests / check / gst / gstclock.c
1 /* GStreamer
2  * Copyright (C) 2010 Alessandro Decina <alessandro.decina@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 static void
23 weak_notify (gpointer data, GObject * object)
24 {
25   *(gboolean *) data = FALSE;
26 }
27
28 static GstClockReturn
29 fake_wait_async (GstClock * clock, GstClockEntry * entry)
30 {
31   return GST_CLOCK_OK;
32 }
33
34 GST_START_TEST (test_set_master_refcount)
35 {
36   GstClock *master, *slave;
37   GstClockClass *klass;
38   gboolean master_alive = TRUE;
39
40   /* create master and slave */
41   master = g_object_new (GST_TYPE_CLOCK, "name", "TestClockMaster", NULL);
42   slave = g_object_new (GST_TYPE_CLOCK, "name", "TestClockMaster", NULL);
43   GST_OBJECT_FLAG_SET (slave, GST_CLOCK_FLAG_CAN_SET_MASTER);
44
45   /* look ma! i'm doing monkey patching in C */
46   klass = GST_CLOCK_GET_CLASS (master);
47   klass->wait_async = fake_wait_async;
48
49   fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
50   fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 1);
51
52   g_object_weak_ref (G_OBJECT (master), weak_notify, &master_alive);
53   fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
54
55   gst_clock_set_master (slave, master);
56   /* slave stores master in slave->master */
57   fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 2);
58   /* master stores a ref to slave in master->clockid */
59   fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 2);
60
61   /* discard our ref */
62   gst_object_unref (master);
63
64   /* master should still be reffed inside slave */
65   fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
66   fail_unless (master_alive);
67
68   /* drop the last ref to mater */
69   gst_clock_set_master (slave, NULL);
70
71   fail_if (master_alive);
72   fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 1);
73
74   gst_object_unref (slave);
75
76   klass->wait_async = NULL;
77 }
78
79 GST_END_TEST;
80
81 static Suite *
82 gst_clock_suite (void)
83 {
84   Suite *s = suite_create ("GstClock");
85   TCase *tc_chain = tcase_create ("clock");
86
87   suite_add_tcase (s, tc_chain);
88   tcase_add_test (tc_chain, test_set_master_refcount);
89
90   return s;
91 }
92
93 GST_CHECK_MAIN (gst_clock);