Debian Codegraph @

tests/conform/timeline.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <clutter/clutter.h>
5
6 #include "test-conform-common.h"
7
8 /* This test runs three timelines at 30 fps with 10 frames. Some of
9 the timelines have markers. Once the timelines are run it then
10 checks that all of the frames were hit, all of the markers were hit
11 and that the completed signal was fired. The timelines are then run
12 again but this time with a timeout source that introduces a
13 delay. This should cause some frames to be skipped. The test is run
14 again but only the markers and the completed signal is checked
15 for. */
16
17 #define FRAME_COUNT 10
18 #define FPS 30
19
20 typedef struct _TimelineData TimelineData;
21
22 struct _TimelineData
23 {
24 int timeline_num;
25
26 guint frame_hit_count[FRAME_COUNT + 1];
27 GSList *markers_hit;
28 guint completed_count;
29 };
30
31 static void
32 timeline_data_init (TimelineData *data, int timeline_num)
33 {
34 memset (data, 0, sizeof (TimelineData));
35 data->timeline_num = timeline_num;
36 }
37
38 static void
39 timeline_data_destroy (TimelineData *data)
40 {
41 g_slist_foreach (data->markers_hit, (GFunc) g_free, NULL);
42 g_slist_free (data->markers_hit);
43 }
44
45 static void
46 timeline_complete_cb (ClutterTimeline *timeline,
47 TimelineData *data)
48 {
49 if (g_test_verbose ())
50 g_print ("%i: Completed\n", data->timeline_num);
51
52 data->completed_count++;
53 }
54
55 static void
56 timeline_new_frame_cb (ClutterTimeline *timeline,
57 gint msec,
58 TimelineData *data)
59 {
60 /* Calculate an approximate frame number from the duration with
61 rounding */
62 int frame_no = ((msec * FRAME_COUNT + (FRAME_COUNT * 1000 / FPS) / 2)
63 / (FRAME_COUNT * 1000 / FPS));
64
65 if (g_test_verbose ())
66 g_print ("%i: Doing frame %d, delta = %i\n",
67 data->timeline_num, frame_no,
68 clutter_timeline_get_delta (timeline));
69
70 g_assert (frame_no >= 0 && frame_no <= FRAME_COUNT);
71
72 data->frame_hit_count[frame_no]++;
73 }
74
75 static void
76 timeline_marker_reached_cb (ClutterTimeline *timeline,
77 const gchar *marker_name,
78 guint frame_num,
79 TimelineData *data)
80 {
81 if (g_test_verbose ())
82 g_print ("%i: Marker '%s' (%d) reached, delta = %i\n",
83 data->timeline_num, marker_name, frame_num,
84 clutter_timeline_get_delta (timeline));
85 data->markers_hit = g_slist_prepend (data->markers_hit,
86 g_strdup (marker_name));
87 }
88
89 static gboolean
90 check_timeline (ClutterTimeline *timeline,
91 TimelineData *data,
92 gboolean check_missed_frames)
93 {
94 gchar **markers;
95 gsize n_markers;
96 guint *marker_reached_count;
97 gboolean succeeded = TRUE;
98 GSList *node;
99 int i;
100 int missed_frame_count = 0;
101 int frame_offset;
102
103 if (clutter_timeline_get_direction (timeline) == CLUTTER_TIMELINE_BACKWARD)
104 frame_offset = 0;
105 else
106 frame_offset = 1;
107
108 markers = clutter_timeline_list_markers (timeline, -1, &n_markers);
109 marker_reached_count = g_new0 (guint, n_markers);
110
111 for (node = data->markers_hit; node; node = node->next)
112 {
113 for (i = 0; i < n_markers; i++)
114 if (!strcmp (node->data, markers[i]))
115 break;
116
117 if (i < n_markers)
118 marker_reached_count[i]++;
119 else
120 {
121 if (g_test_verbose ())
122 g_print ("FAIL: unknown marker '%s' hit for timeline %i\n",
123 (char *) node->data, data->timeline_num);
124 succeeded = FALSE;
125 }
126 }
127
128 for (i = 0; i < n_markers; i++)
129 if (marker_reached_count[i] != 1)
130 {
131 if (g_test_verbose ())
132 g_print ("FAIL: marker '%s' hit %i times for timeline %i\n",
133 markers[i], marker_reached_count[i], data->timeline_num);
134 succeeded = FALSE;
135 }
136
137 if (check_missed_frames)
138 {
139 for (i = 0; i < FRAME_COUNT; i++)
140 if (data->frame_hit_count[i + frame_offset] < 1)
141 missed_frame_count++;
142
143 if (missed_frame_count)
144 {
145 if (g_test_verbose ())
146 g_print ("FAIL: missed %i frame%s for timeline %i\n",
147 missed_frame_count, missed_frame_count == 1 ? "" : "s",
148 data->timeline_num);
149 succeeded = FALSE;
150 }
151 }
152
153 if (data->completed_count != 1)
154 {
155 if (g_test_verbose ())
156 g_print ("FAIL: timeline %i completed %i times\n",
157 data->timeline_num, data->completed_count);
158 succeeded = FALSE;
159 }
160
161 g_strfreev (markers);
162 g_free (marker_reached_count);
163
164 return succeeded;
165 }
166
167 static gboolean
168 timeout_cb (gpointer data G_GNUC_UNUSED)
169 {
170 clutter_main_quit ();
171
172 return FALSE;
173 }
174
175 static gboolean
176 delay_cb (gpointer data)
177 {
178 /* Waste a bit of time so that it will skip frames */
179 g_usleep (G_USEC_PER_SEC * 66 / 1000);
180
181 return TRUE;
182 }
183
184 void
185 timeline_base (TestConformSimpleFixture *fixture,
186 gconstpointer data)
187 {
188 ClutterTimeline *timeline_1;
189 TimelineData data_1;
190 ClutterTimeline *timeline_2;
191 TimelineData data_2;
192 ClutterTimeline *timeline_3;
193 TimelineData data_3;
194 gchar **markers;
195 gsize n_markers;
196 guint delay_tag;
197
198 /* NB: We have to ensure a stage is instantiated else the master
199 * clock wont run... */
200 ClutterActor *stage = clutter_stage_new ();
201
202 timeline_data_init (&data_1, 1);
203 timeline_1 = clutter_timeline_new (FRAME_COUNT * 1000 / FPS);
204 clutter_timeline_add_marker_at_time (timeline_1, "start-marker",
205 0 * 1000 / FPS);
206 clutter_timeline_add_marker_at_time (timeline_1, "foo", 5 * 1000 / FPS);
207 clutter_timeline_add_marker_at_time (timeline_1, "bar", 5 * 1000 / FPS);
208 clutter_timeline_add_marker_at_time (timeline_1, "baz", 5 * 1000 / FPS);
209 clutter_timeline_add_marker_at_time (timeline_1, "near-end-marker",
210 9 * 1000 / FPS);
211 clutter_timeline_add_marker_at_time (timeline_1, "end-marker",
212 10 * 1000 / FPS);
213 markers = clutter_timeline_list_markers (timeline_1, 5 * 1000 / FPS,
214 &n_markers);
215 g_assert (markers != NULL);
216 g_assert (n_markers == 3);
217 g_strfreev (markers);
218
219 timeline_data_init (&data_2, 2);
220 timeline_2 = clutter_timeline_clone (timeline_1);
221 clutter_timeline_add_marker_at_time (timeline_2, "bar", 2 * 1000 / FPS);
222 markers = clutter_timeline_list_markers (timeline_2, -1, &n_markers);
223 g_assert (markers != NULL);
224 g_assert (n_markers == 1);
225 g_assert (strcmp (markers[0], "bar") == 0);
226 g_strfreev (markers);
227
228 timeline_data_init (&data_3, 3);
229 timeline_3 = clutter_timeline_clone (timeline_1);
230 clutter_timeline_set_direction (timeline_3, CLUTTER_TIMELINE_BACKWARD);
231 clutter_timeline_add_marker_at_time (timeline_3, "start-marker",
232 FRAME_COUNT * 1000 / FPS);
233 clutter_timeline_add_marker_at_time (timeline_3, "foo", 5 * 1000 / FPS);
234 clutter_timeline_add_marker_at_time (timeline_3, "baz", 8 * 1000 / FPS);
235 clutter_timeline_add_marker_at_time (timeline_3, "near-end-marker",
236 1 * 1000 / FPS);
237 clutter_timeline_add_marker_at_time (timeline_3, "end-marker",
238 0 * 1000 / FPS);
239
240 g_signal_connect (timeline_1,
241 "marker-reached", G_CALLBACK (timeline_marker_reached_cb),
242 &data_1);
243 g_signal_connect (timeline_1,
244 "new-frame", G_CALLBACK (timeline_new_frame_cb),
245 &data_1);
246 g_signal_connect (timeline_1,
247 "completed", G_CALLBACK (timeline_complete_cb),
248 &data_1);
249
250 g_signal_connect (timeline_2,
251 "marker-reached::bar",
252 G_CALLBACK (timeline_marker_reached_cb),
253 &data_2);
254 g_signal_connect (timeline_2,
255 "new-frame", G_CALLBACK (timeline_new_frame_cb),
256 &data_2);
257 g_signal_connect (timeline_2,
258 "completed", G_CALLBACK (timeline_complete_cb),
259 &data_2);
260
261 g_signal_connect (timeline_3,
262 "marker-reached", G_CALLBACK (timeline_marker_reached_cb),
263 &data_3);
264 g_signal_connect (timeline_3,
265 "new-frame", G_CALLBACK (timeline_new_frame_cb),
266 &data_3);
267 g_signal_connect (timeline_3,
268 "completed", G_CALLBACK (timeline_complete_cb),
269 &data_3);
270
271 if (g_test_verbose ())
272 g_print ("Without delay...\n");
273
274 clutter_timeline_start (timeline_1);
275 clutter_timeline_start (timeline_2);
276 clutter_timeline_start (timeline_3);
277
278 clutter_threads_add_timeout (2000, timeout_cb, NULL);
279
280 clutter_main ();
281
282 g_assert (check_timeline (timeline_1, &data_1, TRUE));
283 g_assert (check_timeline (timeline_2, &data_2, TRUE));
284 g_assert (check_timeline (timeline_3, &data_3, TRUE));
285
286 if (g_test_verbose ())
287 g_print ("With delay...\n");
288
289 timeline_data_destroy (&data_1);
290 timeline_data_init (&data_1, 1);
291 timeline_data_destroy (&data_2);
292 timeline_data_init (&data_2, 2);
293 timeline_data_destroy (&data_3);
294 timeline_data_init (&data_3, 3);
295
296 clutter_timeline_start (timeline_1);
297 clutter_timeline_start (timeline_2);
298 clutter_timeline_start (timeline_3);
299
300 clutter_threads_add_timeout (2000, timeout_cb, NULL);
301 delay_tag = clutter_threads_add_timeout (99, delay_cb, NULL);
302
303 clutter_main ();
304
305 g_assert (check_timeline (timeline_1, &data_1, FALSE));
306 g_assert (check_timeline (timeline_2, &data_2, FALSE));
307 g_assert (check_timeline (timeline_3, &data_3, FALSE));
308
309 g_object_unref (timeline_1);
310 g_object_unref (timeline_2);
311 g_object_unref (timeline_3);
312
313 timeline_data_destroy (&data_1);
314 timeline_data_destroy (&data_2);
315 timeline_data_destroy (&data_3);
316
317 g_source_remove (delay_tag);
318
319 clutter_actor_destroy (stage);
320 }
321
322 void
323 timeline_markers_from_script (TestConformSimpleFixture *fixture,
324 gconstpointer data)
325 {
326 ClutterScript *script = clutter_script_new ();
327 ClutterTimeline *timeline;
328 GError *error = NULL;
329 gchar *test_file;
330 gchar **markers;
331 gsize n_markers;
332
333 test_file = clutter_test_get_data_file ("test-script-timeline-markers.json");
334 clutter_script_load_from_file (script, test_file, &error);
335 if (g_test_verbose () && error != NULL)
336 g_print ("Error: %s", error->message);
337
338 g_assert_no_error (error);
339
340 timeline = CLUTTER_TIMELINE (clutter_script_get_object (script, "timeline0"));
341
342 g_assert (clutter_timeline_has_marker (timeline, "marker0"));
343 g_assert (clutter_timeline_has_marker (timeline, "marker1"));
344 g_assert (!clutter_timeline_has_marker (timeline, "foo"));
345 g_assert (clutter_timeline_has_marker (timeline, "marker2"));
346 g_assert (clutter_timeline_has_marker (timeline, "marker3"));
347
348 markers = clutter_timeline_list_markers (timeline, -1, &n_markers);
349 g_assert_cmpint (n_markers, ==, 4);
350 g_strfreev (markers);
351
352 markers = clutter_timeline_list_markers (timeline, 500, &n_markers);
353 g_assert_cmpint (n_markers, ==, 2);
354 g_assert (markers != NULL);
355 g_assert_cmpstr (markers[0], ==, "marker3");
356 g_assert_cmpstr (markers[1], ==, "marker1");
357 g_strfreev (markers);
358
359 g_object_unref (script);
360
361 g_free (test_file);
362 }