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
{
35 VSyncTimeSourceTest() : timer_(VSyncTimeSource::Create(&provider_
)) {
36 timer_
->SetClient(&client_
);
40 FakeTimeSourceClient client_
;
41 FakeVSyncProvider provider_
;
42 scoped_refptr
<VSyncTimeSource
> timer_
;
45 TEST_F(VSyncTimeSourceTest
, TaskPostedAndTickCalled
) {
46 EXPECT_FALSE(provider_
.IsVSyncNotificationEnabled());
48 timer_
->SetActive(true);
49 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
51 base::TimeTicks frame_time
= base::TimeTicks::Now();
52 provider_
.Trigger(frame_time
);
53 EXPECT_TRUE(client_
.TickCalled());
54 EXPECT_EQ(timer_
->LastTickTime(), frame_time
);
57 TEST_F(VSyncTimeSourceTest
, NotificationDisabledLazily
) {
58 base::TimeTicks frame_time
= base::TimeTicks::Now();
60 // Enable timer and trigger sync once.
61 timer_
->SetActive(true);
62 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
63 provider_
.Trigger(frame_time
);
64 EXPECT_TRUE(client_
.TickCalled());
66 // Disabling the timer should not disable vsync notification immediately.
68 timer_
->SetActive(false);
69 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
71 // At the next vsync the notification is disabled, but the timer isn't ticked.
72 provider_
.Trigger(frame_time
);
73 EXPECT_FALSE(provider_
.IsVSyncNotificationEnabled());
74 EXPECT_FALSE(client_
.TickCalled());
76 // The notification should not be disabled multiple times.
77 provider_
.RequestVSyncNotification(timer_
.get());
78 provider_
.Trigger(frame_time
);
79 EXPECT_TRUE(provider_
.IsVSyncNotificationEnabled());
80 EXPECT_FALSE(client_
.TickCalled());
83 TEST_F(VSyncTimeSourceTest
, ValidNextTickTime
) {
84 base::TimeTicks frame_time
= base::TimeTicks::Now();
85 base::TimeDelta interval
= base::TimeDelta::FromSeconds(1);
87 ASSERT_EQ(timer_
->NextTickTime(), base::TimeTicks());
89 timer_
->SetActive(true);
90 provider_
.Trigger(frame_time
);
91 ASSERT_EQ(timer_
->NextTickTime(), frame_time
);
93 timer_
->SetTimebaseAndInterval(frame_time
, interval
);
94 ASSERT_EQ(timer_
->NextTickTime(), frame_time
+ interval
);