From ca82cde6dddf0761d2b2aca03f19830393906bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Wed, 25 Mar 2020 19:20:42 +0100 Subject: [PATCH] tests/frame-clock: Add test that switches frame clock mid timeline https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1285 --- .../clutter/conform/frame-clock-timeline.c | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/tests/clutter/conform/frame-clock-timeline.c b/src/tests/clutter/conform/frame-clock-timeline.c index 66ef030f2..f00a938da 100644 --- a/src/tests/clutter/conform/frame-clock-timeline.c +++ b/src/tests/clutter/conform/frame-clock-timeline.c @@ -108,6 +108,92 @@ frame_clock_timeline_basic (void) g_assert_null (frame_clock); } +static void +on_switch_reached (ClutterTimeline *timeline, + const char *marker_name, + unsigned int frame_number, + ClutterFrameClock *new_frame_clock) +{ + ClutterFrameClock *old_frame_clock; + + old_frame_clock = clutter_timeline_get_frame_clock (timeline); + clutter_frame_clock_inhibit (old_frame_clock); + + clutter_timeline_set_frame_clock (timeline, new_frame_clock); +} + +static void +frame_clock_timeline_switch (void) +{ + GMainLoop *main_loop; + ClutterFrameClock *frame_clock2; + ClutterFrameClock *frame_clock1; + ClutterTimeline *timeline; + int frame_counter; + int64_t before_us; + int64_t after_us; + + main_loop = g_main_loop_new (NULL, FALSE); + + frame_clock1 = clutter_frame_clock_new (refresh_rate, + &timeline_frame_listener_iface, + NULL); + g_object_add_weak_pointer (G_OBJECT (frame_clock1), (gpointer *) &frame_clock1); + frame_clock2 = clutter_frame_clock_new (refresh_rate, + &timeline_frame_listener_iface, + NULL); + g_object_add_weak_pointer (G_OBJECT (frame_clock2), (gpointer *) &frame_clock2); + + timeline = g_object_new (CLUTTER_TYPE_TIMELINE, + "duration", 1000, + "frame-clock", frame_clock1, + NULL); + g_object_add_weak_pointer (G_OBJECT (timeline), (gpointer *) &timeline); + + clutter_timeline_add_marker_at_time (timeline, "switch", 500); + + frame_counter = 0; + + g_signal_connect (timeline, "marker-reached::switch", + G_CALLBACK (on_switch_reached), + frame_clock2); + g_signal_connect (timeline, "new-frame", + G_CALLBACK (on_timeline_new_frame), + &frame_counter); + g_signal_connect (timeline, "completed", + G_CALLBACK (on_timeline_completed), + main_loop); + + clutter_timeline_start (timeline); + + before_us = g_get_monotonic_time (); + + g_main_loop_run (main_loop); + + after_us = g_get_monotonic_time (); + + g_assert_cmpint (after_us - before_us, + >=, + ms2us (clutter_timeline_get_duration (timeline))); + + g_assert (clutter_timeline_get_frame_clock (timeline) == frame_clock2); + + /* The duration is 1s, with a 60hz clock, and we switch after 0.5s. To verify + * we continued to get frames, check that we have a bit more than half of the + * frames accounted for. + */ + g_assert_cmpint (frame_counter, >, 35); + + g_main_loop_unref (main_loop); + g_object_unref (timeline); + g_assert_null (timeline); + g_object_unref (frame_clock1); + g_assert_null (frame_clock1); + g_object_unref (frame_clock2); + g_assert_null (frame_clock2); +} + CLUTTER_TEST_SUITE ( CLUTTER_TEST_UNIT ("/frame-clock/timeline/basic", frame_clock_timeline_basic) + CLUTTER_TEST_UNIT ("/frame-clock/timeline/switch", frame_clock_timeline_switch) )