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