1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/scheduler/vsync_time_source.h"
7 #include "cc/test/scheduler_test_common.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 class FakeVSyncProvider
: public VSyncProvider
{
15 FakeVSyncProvider() : client_(NULL
) {}
17 // VSyncProvider implementation.
18 virtual void RequestVSyncNotification(VSyncClient
* client
) OVERRIDE
{
22 bool IsVSyncNotificationEnabled() const { return client_
!= NULL
; }
24 void Trigger(base::TimeTicks frame_time
) {
26 client_
->DidVSync(frame_time
);
33 class VSyncTimeSourceTest
: public testing::Test
{
36 : timer_(VSyncTimeSource::Create(
37 &provider_
, VSyncTimeSource::DISABLE_ON_NEXT_TICK
)) {
38 timer_
->SetClient(&client_
);
42 FakeTimeSourceClient client_
;
43 FakeVSyncProvider provider_
;
44 scoped_refptr
<VSyncTimeSource
> timer_
;
47 TEST_F(VSyncTimeSourceTest
, TaskPostedAndTickCalled
) {
48 EXPECT_FALSE(provider_
.IsVSyncNotificationEnabled());
50 timer_
->SetActive(true);
51 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
53 base::TimeTicks frame_time
= base::TimeTicks::Now();
54 provider_
.Trigger(frame_time
);
55 EXPECT_TRUE(client_
.TickCalled());
56 EXPECT_EQ(timer_
->LastTickTime(), frame_time
);
59 TEST_F(VSyncTimeSourceTest
, NotificationDisabledLazily
) {
60 base::TimeTicks frame_time
= base::TimeTicks::Now();
62 // Enable timer and trigger sync once.
63 timer_
->SetActive(true);
64 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
65 provider_
.Trigger(frame_time
);
66 EXPECT_TRUE(client_
.TickCalled());
68 // Disabling the timer should not disable vsync notification immediately.
70 timer_
->SetActive(false);
71 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
73 // At the next vsync the notification is disabled, but the timer isn't ticked.
74 provider_
.Trigger(frame_time
);
75 EXPECT_FALSE(provider_
.IsVSyncNotificationEnabled());
76 EXPECT_FALSE(client_
.TickCalled());
78 // The notification should not be disabled multiple times.
79 provider_
.RequestVSyncNotification(timer_
.get());
80 provider_
.Trigger(frame_time
);
81 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
82 EXPECT_FALSE(client_
.TickCalled());
85 TEST_F(VSyncTimeSourceTest
, NotificationDisabledImmediatelyForSetting
) {
86 timer_
= VSyncTimeSource::Create(&provider_
,
87 VSyncTimeSource::DISABLE_SYNCHRONOUSLY
);
88 timer_
->SetClient(&client_
);
89 base::TimeTicks frame_time
= base::TimeTicks::Now();
91 // Enable timer and trigger sync once.
92 timer_
->SetActive(true);
93 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
94 provider_
.Trigger(frame_time
);
95 EXPECT_TRUE(client_
.TickCalled());
97 // Disable timer should disable vsync notification immediately.
98 timer_
->SetActive(false);
99 EXPECT_FALSE(provider_
.IsVSyncNotificationEnabled());
101 // Enable again and timer can be ticked again.
103 timer_
->SetActive(true);
104 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
105 provider_
.Trigger(frame_time
);
106 EXPECT_TRUE(client_
.TickCalled());
109 TEST_F(VSyncTimeSourceTest
, ValidNextTickTime
) {
110 base::TimeTicks frame_time
= base::TimeTicks::Now();
111 base::TimeDelta interval
= base::TimeDelta::FromSeconds(1);
113 ASSERT_EQ(timer_
->NextTickTime(), base::TimeTicks());
115 timer_
->SetActive(true);
116 provider_
.Trigger(frame_time
);
117 ASSERT_EQ(timer_
->NextTickTime(), frame_time
);
119 timer_
->SetTimebaseAndInterval(frame_time
, interval
);
120 ASSERT_EQ(timer_
->NextTickTime(), frame_time
+ interval
);