Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / tests / check / gst / gstobject.c
1 /* GStreamer
2  *
3  * unit test for GstObject
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 /*
30   Create a fake subclass
31  */
32 typedef struct _GstFakeObjectClass GstFakeObjectClass;
33 typedef struct _GstFakeObject GstFakeObject;
34
35 struct _GstFakeObject
36 {
37   GstObject object;
38 };
39
40 struct _GstFakeObjectClass
41 {
42   GstObjectClass parent_class;
43 };
44
45 //static GstObjectClass *parent_class = NULL;
46 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
47
48 static GType
49 gst_fake_object_get_type (void)
50 {
51   static volatile gsize fake_object_type = 0;
52
53   if (g_once_init_enter (&fake_object_type)) {
54     GType type;
55     static const GTypeInfo fake_object_info = {
56       sizeof (GstFakeObjectClass),
57       NULL,                     //gst_fake_object_base_class_init,
58       NULL,                     //gst_fake_object_base_class_finalize,
59       NULL,                     //(GClassInitFunc) gst_fake_object_class_init,
60       NULL,
61       NULL,
62       sizeof (GstFakeObject),
63       0,
64       NULL,                     //(GInstanceInitFunc) gst_fake_object_init,
65       NULL
66     };
67
68     type = g_type_register_static (GST_TYPE_OBJECT,
69         "GstFakeObject", &fake_object_info, 0);
70     g_once_init_leave (&fake_object_type, type);
71   }
72   return fake_object_type;
73 }
74
75 #ifndef HAVE_OSX
76 /* g_object_new on abstract GstObject should fail */
77 GST_START_TEST (test_fail_abstract_new)
78 {
79   GstObject *object;
80
81   ASSERT_CRITICAL (object = g_object_new (gst_object_get_type (), NULL));
82   fail_unless (object == NULL, "Created an instance of abstract GstObject");
83 }
84
85 GST_END_TEST;
86 #endif
87
88 /* g_object_new on GstFakeObject should succeed */
89 GST_START_TEST (test_fake_object_new)
90 {
91   GstObject *object;
92
93   object = g_object_new (gst_fake_object_get_type (), NULL);
94   fail_if (object == NULL, "Failed to create instance of GstFakeObject");
95   fail_unless (GST_IS_OBJECT (object),
96       "GstFakeObject instance is not a GstObject");
97   gst_object_unref (object);
98 }
99
100 GST_END_TEST;
101
102 /* GstFakeObject name tests */
103 GST_START_TEST (test_fake_object_name)
104 {
105   GstObject *object;
106   gchar *name;
107   gchar *name2;
108
109   object = g_object_new (gst_fake_object_get_type (), NULL);
110
111   name = gst_object_get_name (object);
112   fail_if (name == NULL, "Newly created object has no name");
113   fail_if (strncmp (name, "fakeobject", 10) != 0,
114       "Random name %s does not start with Gst", name);
115   g_free (name);
116
117   /* give a random name by setting with NULL;
118    * GstFakeObject class -> fakeobject%d */
119   gst_object_set_name (object, NULL);
120   name = gst_object_get_name (object);
121   fail_if (name == NULL, "Random name was not assigned");
122   fail_if (strncmp (name, "fakeobject", 10) != 0,
123       "Random name %s does not start with Gst", name);
124   g_free (name);
125
126   gst_object_set_name (object, "fake");
127   name = gst_object_get_name (object);
128   fail_if (name == NULL, "Failed to get name of GstFakeObject");
129   fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
130
131   /* change the gotten name to see that it's a copy and not the original */
132   name[0] = 'm';
133   name2 = gst_object_get_name (object);
134   fail_if (strcmp (name2, "fake") != 0,
135       "Copy of object name affected actual object name");
136   g_free (name);
137   g_free (name2);
138
139   gst_object_unref (object);
140 }
141
142 GST_END_TEST;
143
144 /* thread function for threaded name change test */
145 static gpointer
146 thread_name_object (GstObject * object)
147 {
148   gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
149
150   THREAD_START ();
151
152   /* give main thread a head start */
153   g_usleep (100000);
154
155   /* write our name repeatedly */
156   g_message ("THREAD %s: starting loop\n", thread_id);
157   while (THREAD_TEST_RUNNING ()) {
158     gst_object_set_name (object, thread_id);
159     /* a minimal sleep invokes a thread switch */
160     THREAD_SWITCH ();
161   }
162
163   /* thread is done, so let's return */
164   g_message ("THREAD %s: set name\n", thread_id);
165   g_free (thread_id);
166
167   return NULL;
168 }
169
170 #if 0
171 GST_START_TEST (test_fake_object_name_threaded_wrong)
172 {
173   GstObject *object;
174   gchar *name;
175   gint i;
176   gboolean expected_failure = FALSE;
177
178   g_message ("\nTEST: set/get without lock\n");
179
180   object = g_object_new (gst_fake_object_get_type (), NULL);
181   gst_object_set_name (object, "main");
182
183   MAIN_START_THREADS (5, thread_name_object, object);
184
185   /* start looping and set/get name repeatedly */
186   for (i = 0; i < 1000; ++i) {
187     gst_object_set_name (object, "main");
188     THREAD_SWITCH ();
189     name = gst_object_get_name (object);
190     if (strcmp (name, "main") != 0) {
191       g_message ("MAIN: expected failure during run %d\n", i);
192       expected_failure = TRUE;
193       g_free (name);
194       break;
195     }
196     g_free (name);
197   }
198   MAIN_STOP_THREADS ();
199
200   gst_object_unref (object);
201
202   fail_unless (expected_failure, "name did not get changed");
203 }
204
205 GST_END_TEST;
206 #endif
207
208 /*
209  * main thread sets and gets name directly on struct inside the object lock
210  * succeed because lock is held during set/get, and threads are locked out
211  */
212 GST_START_TEST (test_fake_object_name_threaded_right)
213 {
214   GstObject *object;
215   gchar *name;
216   gint i;
217
218   g_message ("\nTEST: set/get inside lock\n");
219
220   object = g_object_new (gst_fake_object_get_type (), NULL);
221   gst_object_set_name (object, "main");
222
223   MAIN_START_THREADS (5, thread_name_object, object);
224
225   /* start looping and set/get name repeatedly */
226   for (i = 0; i < 1000; ++i) {
227     GST_OBJECT_LOCK (object);
228     g_free (GST_OBJECT_NAME (object));
229     GST_OBJECT_NAME (object) = g_strdup ("main");
230     THREAD_SWITCH ();
231     name = g_strdup (GST_OBJECT_NAME (object));
232     GST_OBJECT_UNLOCK (object);
233
234     fail_unless (strcmp (name, "main") == 0,
235         "Name got changed while lock held during run %d", i);
236     g_free (name);
237   }
238   MAIN_STOP_THREADS ();
239   gst_object_unref (object);
240 }
241
242 GST_END_TEST;
243 /*
244  * main thread creates lots of objects
245  * child threads sets default names on objects
246  * then main thread checks uniqueness of object names
247  */
248
249 static GList *object_list = NULL;
250 static gint num_objects = 1000;
251 static gint num_threads = 5;
252
253 /* thread function for threaded default name change test */
254 static gpointer
255 thread_name_object_default (int *i)
256 {
257   int j;
258
259   THREAD_START ();
260
261   for (j = *i; j < num_objects; j += num_threads) {
262     GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
263
264     /* g_message ("THREAD %p: setting default name on object %d\n",
265        g_thread_self (), j); */
266     gst_object_set_name (o, NULL);
267     THREAD_SWITCH ();
268   }
269
270   /* thread is done, so let's return */
271   g_message ("THREAD %p: set name\n", g_thread_self ());
272   g_free (i);
273
274   return NULL;
275 }
276
277 static gint
278 gst_object_name_compare (GstObject * o, GstObject * p)
279 {
280   gint result;
281
282   GST_OBJECT_LOCK (o);
283   GST_OBJECT_LOCK (p);
284
285   if (o->name == NULL && p->name == NULL) {
286     result = 0;
287   } else if (o->name == NULL) {
288     result = -1;
289   } else if (p->name == NULL) {
290     result = 1;
291   } else {
292     result = strcmp (o->name, p->name);
293   }
294
295   GST_OBJECT_UNLOCK (p);
296   GST_OBJECT_UNLOCK (o);
297
298   return result;
299 }
300
301 GST_START_TEST (test_fake_object_name_threaded_unique)
302 {
303   GstObject *object;
304   gint i;
305   gint *ip;
306   gchar *name1, *name2;
307   GList *l;
308
309   g_message ("\nTEST: uniqueness of default names\n");
310
311   for (i = 0; i < num_objects; ++i) {
312     object = g_object_new (gst_fake_object_get_type (), NULL);
313     object_list = g_list_append (object_list, object);
314   }
315
316   MAIN_INIT ();
317
318   mark_point ();
319   for (i = 0; i < num_threads; ++i) {
320     ip = g_new (gint, 1);
321     *ip = i;
322     MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
323   }
324
325   mark_point ();
326   MAIN_SYNCHRONIZE ();
327   mark_point ();
328   MAIN_STOP_THREADS ();
329
330   /* sort GList based on object name */
331   /* FIXME: sort and test */
332   object_list =
333       g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
334
335   name1 = gst_object_get_name (GST_OBJECT (object_list->data));
336   for (l = object_list->next; l->next; l = l->next) {
337     g_message ("object with name %s\n", name1);
338     name2 = gst_object_get_name (GST_OBJECT (l->data));
339     fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
340     g_free (name1);
341     name1 = name2;
342   }
343   g_free (name1);
344
345   /* free stuff */
346   g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
347 }
348
349 GST_END_TEST;
350
351 /* parentage test on GstFakeObject */
352 GST_START_TEST (test_fake_object_parentage)
353 {
354   GstObject *object1, *object2;
355   GstObject *parent;
356   gboolean result;
357
358   /* create new object */
359   object1 = g_object_new (gst_fake_object_get_type (), NULL);
360   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
361   fail_unless (GST_IS_OBJECT (object1),
362       "GstFakeObject instance is not a GstObject");
363   fail_unless (GST_OBJECT_IS_FLOATING (object1),
364       "GstFakeObject instance is not floating");
365
366   /* check the parent */
367   parent = gst_object_get_parent (object1);
368   fail_if (parent != NULL, "GstFakeObject has parent");
369   /* try to set a NULL parent, this should give a warning */
370   ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
371   fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
372   /* try to set itself as parent, we expect a warning here */
373   ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
374   fail_if (result == TRUE, "GstFakeObject accepted itself as parent");
375
376   /* should still be floating */
377   fail_unless (GST_OBJECT_IS_FLOATING (object1),
378       "GstFakeObject instance is not floating");
379
380   /* create another object */
381   object2 = g_object_new (gst_fake_object_get_type (), NULL);
382   fail_if (object2 == NULL,
383       "Failed to create another instance of GstFakeObject");
384   fail_unless (GST_IS_OBJECT (object2),
385       "second GstFakeObject instance is not a GstObject");
386   fail_unless (GST_OBJECT_IS_FLOATING (object1),
387       "GstFakeObject instance is not floating");
388
389   /* try to set other object as parent */
390   result = gst_object_set_parent (object1, object2);
391   fail_if (result == FALSE,
392       "GstFakeObject could not accept other object as parent");
393
394   /* should not be floating anymore */
395   fail_if (GST_OBJECT_IS_FLOATING (object1),
396       "GstFakeObject instance is still floating");
397   /* parent should still be floating */
398   fail_unless (GST_OBJECT_IS_FLOATING (object2),
399       "GstFakeObject instance is not floating");
400
401   /* check the parent */
402   parent = gst_object_get_parent (object1);
403   fail_if (parent != object2, "GstFakeObject has wrong parent");
404   gst_object_unref (parent);
405   /* try to set other object as parent again */
406   result = gst_object_set_parent (object1, object2);
407   fail_if (result == TRUE, "GstFakeObject could set parent twice");
408
409   /* ref before unparenting */
410   gst_object_ref (object1);
411   /* clear parent of object */
412   gst_object_unparent (object1);
413
414   /* check the parent */
415   parent = gst_object_get_parent (object1);
416   fail_if (parent != NULL, "GstFakeObject has parent");
417
418   /* object should not be floating */
419   fail_if (GST_OBJECT_IS_FLOATING (object1),
420       "GstFakeObject instance is floating again");
421
422   gst_object_unref (object1);
423   gst_object_unref (object2);
424 }
425
426 GST_END_TEST;
427
428 /* parentage test dispose on GstFakeObject, since our testcase
429  * does not handle the parent relation completely, the parent does
430  * not hold a ref to the child, we cannot dispose the parent to
431  * dipose the child as well. This test needs to be run with DEBUG
432  * info to check if the finalize methods are called correctly. */
433 GST_START_TEST (test_fake_object_parentage_dispose)
434 {
435   GstObject *object1, *object2;
436   gboolean result;
437
438   object1 = g_object_new (gst_fake_object_get_type (), NULL);
439   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
440
441   object2 = g_object_new (gst_fake_object_get_type (), NULL);
442   fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
443
444   /* try to set other object as parent */
445   result = gst_object_set_parent (object1, object2);
446   fail_if (result == FALSE,
447       "GstFakeObject could not accept other object as parent");
448
449   /* clear parent of object */
450   gst_object_unparent (object1);
451
452   /* now dispose parent */
453   gst_object_unref (object2);
454 }
455
456 GST_END_TEST;
457
458 GST_START_TEST (test_fake_object_has_ancestor)
459 {
460   GstObject *object1, *object2, *object3, *object4;
461   gboolean result;
462
463   object1 = g_object_new (gst_fake_object_get_type (), NULL);
464   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
465
466   object2 = g_object_new (gst_fake_object_get_type (), NULL);
467   fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
468
469   object3 = g_object_new (gst_fake_object_get_type (), NULL);
470   fail_if (object3 == NULL, "Failed to create instance of GstFakeObject");
471
472   object4 = g_object_new (gst_fake_object_get_type (), NULL);
473   fail_if (object4 == NULL, "Failed to create instance of GstFakeObject");
474
475   /* try to set other object as parent */
476   result = gst_object_set_parent (object1, object3);
477   fail_if (result == FALSE,
478       "GstFakeObject could not accept other object as parent");
479   result = gst_object_set_parent (object2, object3);
480   fail_if (result == FALSE,
481       "GstFakeObject could not accept other object as parent");
482   result = gst_object_set_parent (object3, object4);
483   fail_if (result == FALSE,
484       "GstFakeObject could not accept other object as parent");
485
486   fail_unless (gst_object_has_ancestor (object1, object1));
487   fail_if (gst_object_has_ancestor (object1, object2));
488   fail_unless (gst_object_has_ancestor (object1, object3));
489   fail_unless (gst_object_has_ancestor (object1, object4));
490   fail_if (gst_object_has_ancestor (object3, object1));
491   fail_if (gst_object_has_ancestor (object4, object1));
492   fail_unless (gst_object_has_ancestor (object3, object4));
493   fail_if (gst_object_has_ancestor (object4, object3));
494   fail_unless (gst_object_has_ancestor (object4, object4));
495
496   /* unparent everything */
497   gst_object_unparent (object3);
498   gst_object_unparent (object2);
499   gst_object_unparent (object1);
500
501   /* now dispose objects */
502   gst_object_unref (object4);
503 }
504
505 GST_END_TEST;
506
507 /* test: try renaming a parented object, make sure it fails */
508
509 static Suite *
510 gst_object_suite (void)
511 {
512   Suite *s = suite_create ("GstObject");
513   TCase *tc_chain = tcase_create ("general");
514
515   /* turn off timeout */
516   tcase_set_timeout (tc_chain, 60);
517
518   suite_add_tcase (s, tc_chain);
519   tcase_add_test (tc_chain, test_fake_object_new);
520   tcase_add_test (tc_chain, test_fake_object_name);
521 #if 0
522   tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
523 #endif
524   tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
525   tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
526   tcase_add_test (tc_chain, test_fake_object_parentage);
527   tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
528
529   tcase_add_test (tc_chain, test_fake_object_has_ancestor);
530   //tcase_add_checked_fixture (tc_chain, setup, teardown);
531
532   /* FIXME: GLib shouldn't crash here, but issue a warning and return a NULL
533    * object, or at least g_error() and then abort properly ... (tpm) */
534 #ifndef HAVE_OSX
535   /* Disabled for OS/X because a) it's a pretty silly test anyway and
536    * b) different OS/X versions raise different signals and it isn't worth
537    * the effort to try and detect which one should be producing which
538    */
539   /* SEGV tests go last so we can debug the others */
540   if (g_getenv ("CK_FORK") == NULL || strcmp (g_getenv ("CK_FORK"), "no") != 0)
541     tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
542 #endif
543
544   return s;
545 }
546
547 GST_CHECK_MAIN (gst_object);