check: add GstTestClock as a deterministic clock for testing
[platform/upstream/gstreamer.git] / tests / check / libs / gsttestclock.c
1 /*
2  * Unit test for a deterministic clock for Gstreamer unit tests
3  *
4  * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
5  * Copyright (C) 2012 Sebastian Rasmussen <sebastian.rasmussen@axis.com>
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 #include <gst/check/gstcheck.h>
24 #include <gst/check/gsttestclock.h>
25
26 GST_START_TEST (test_object_flags)
27 {
28   GstClock *clock = gst_test_clock_new ();
29   g_assert (GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC));
30   g_assert (GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC));
31   g_assert (GST_OBJECT_FLAG_IS_SET (clock,
32           GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC));
33   g_assert (GST_OBJECT_FLAG_IS_SET (clock,
34           GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC));
35   gst_object_unref (clock);
36 }
37
38 GST_END_TEST;
39
40 GST_START_TEST (test_resolution_query)
41 {
42   GstClock *clock = gst_test_clock_new ();
43   g_assert_cmpuint (gst_clock_get_resolution (clock), ==, 1);
44   gst_object_unref (clock);
45 }
46
47 GST_END_TEST;
48
49 GST_START_TEST (test_start_time)
50 {
51   GstClock *clock;
52   guint64 start_time;
53
54   clock = gst_test_clock_new ();
55   g_assert_cmpuint (gst_clock_get_time (clock), ==, 0);
56   g_object_get (clock, "start-time", &start_time, NULL);
57   g_assert_cmpuint (start_time, ==, 0);
58   gst_object_unref (clock);
59
60   clock = gst_test_clock_new_with_start_time (GST_SECOND);
61   g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
62   g_object_get (clock, "start-time", &start_time, NULL);
63   g_assert_cmpuint (start_time, ==, GST_SECOND);
64   gst_object_unref (clock);
65 }
66
67 GST_END_TEST;
68
69 GST_START_TEST (test_set_time)
70 {
71   GstClock *clock = gst_test_clock_new_with_start_time (GST_SECOND);
72   gst_test_clock_set_time (GST_TEST_CLOCK (clock), GST_SECOND);
73   g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
74   gst_test_clock_set_time (GST_TEST_CLOCK (clock), GST_SECOND + 1);
75   g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND + 1);
76   gst_object_unref (clock);
77 }
78
79 GST_END_TEST;
80
81 GST_START_TEST (test_advance_time)
82 {
83   GstClock *clock = gst_test_clock_new_with_start_time (GST_SECOND);
84   gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 0);
85   g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
86   gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 42 * GST_MSECOND);
87   g_assert_cmpuint (gst_clock_get_time (clock), ==,
88       GST_SECOND + (42 * GST_MSECOND));
89   gst_object_unref (clock);
90 }
91
92 GST_END_TEST;
93
94 static Suite *
95 gst_test_clock_suite (void)
96 {
97   Suite *s = suite_create ("GstTestClock");
98   TCase *tc_chain = tcase_create ("testclock");
99
100   suite_add_tcase (s, tc_chain);
101
102   tcase_add_test (tc_chain, test_object_flags);
103   tcase_add_test (tc_chain, test_resolution_query);
104   tcase_add_test (tc_chain, test_start_time);
105   tcase_add_test (tc_chain, test_set_time);
106   tcase_add_test (tc_chain, test_advance_time);
107
108   return s;
109 }
110
111 GST_CHECK_MAIN (gst_test_clock);