OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "cc/scheduler/vsync_time_source.h" | |
6 | |
7 #include "cc/test/scheduler_test_common.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace cc { | |
11 namespace { | |
12 | |
13 class FakeVSyncProvider : public VSyncProvider { | |
14 public: | |
15 FakeVSyncProvider() : client_(NULL) {} | |
16 | |
17 // VSyncProvider implementation. | |
18 virtual void RequestVSyncNotification(VSyncClient* client) OVERRIDE { | |
19 client_ = client; | |
20 } | |
21 | |
22 bool IsVSyncNotificationEnabled() const { return client_ != NULL; } | |
23 | |
24 void Trigger(base::TimeTicks frame_time) { | |
25 if (client_) | |
26 client_->DidVSync(frame_time); | |
27 } | |
28 | |
29 private: | |
30 VSyncClient* client_; | |
31 }; | |
32 | |
33 class VSyncTimeSourceTest : public testing::Test { | |
34 public: | |
35 VSyncTimeSourceTest() | |
36 : timer_(VSyncTimeSource::Create( | |
37 &provider_, VSyncTimeSource::DISABLE_ON_NEXT_TICK)) { | |
38 timer_->SetClient(&client_); | |
39 } | |
40 | |
41 protected: | |
42 FakeTimeSourceClient client_; | |
43 FakeVSyncProvider provider_; | |
44 scoped_refptr<VSyncTimeSource> timer_; | |
45 }; | |
46 | |
47 TEST_F(VSyncTimeSourceTest, TaskPostedAndTickCalled) { | |
48 EXPECT_FALSE(provider_.IsVSyncNotificationEnabled()); | |
49 | |
50 timer_->SetActive(true); | |
51 EXPECT_TRUE(provider_.IsVSyncNotificationEnabled()); | |
52 | |
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); | |
57 } | |
58 | |
59 TEST_F(VSyncTimeSourceTest, NotificationDisabledLazily) { | |
60 base::TimeTicks frame_time = base::TimeTicks::Now(); | |
61 | |
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()); | |
67 | |
68 // Disabling the timer should not disable vsync notification immediately. | |
69 client_.Reset(); | |
70 timer_->SetActive(false); | |
71 EXPECT_TRUE(provider_.IsVSyncNotificationEnabled()); | |
72 | |
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()); | |
77 | |
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()); | |
83 } | |
84 | |
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(); | |
90 | |
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()); | |
96 | |
97 // Disable timer should disable vsync notification immediately. | |
98 timer_->SetActive(false); | |
99 EXPECT_FALSE(provider_.IsVSyncNotificationEnabled()); | |
100 | |
101 // Enable again and timer can be ticked again. | |
102 client_.Reset(); | |
103 timer_->SetActive(true); | |
104 EXPECT_TRUE(provider_.IsVSyncNotificationEnabled()); | |
105 provider_.Trigger(frame_time); | |
106 EXPECT_TRUE(client_.TickCalled()); | |
107 } | |
108 | |
109 TEST_F(VSyncTimeSourceTest, ValidNextTickTime) { | |
110 base::TimeTicks frame_time = base::TimeTicks::Now(); | |
111 base::TimeDelta interval = base::TimeDelta::FromSeconds(1); | |
112 | |
113 ASSERT_EQ(timer_->NextTickTime(), base::TimeTicks()); | |
114 | |
115 timer_->SetActive(true); | |
116 provider_.Trigger(frame_time); | |
117 ASSERT_EQ(timer_->NextTickTime(), frame_time); | |
118 | |
119 timer_->SetTimebaseAndInterval(frame_time, interval); | |
120 ASSERT_EQ(timer_->NextTickTime(), frame_time + interval); | |
121 } | |
122 | |
123 } // namespace | |
124 } // namespace cc | |
OLD | NEW |