Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / cc / scheduler / vsync_time_source_unittest.cc
blob4834630e6a1592b1a86025c4b26ce104317b9704
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"
10 namespace cc {
11 namespace {
13 class FakeVSyncProvider : public VSyncProvider {
14 public:
15 FakeVSyncProvider() : client_(NULL) {}
17 // VSyncProvider implementation.
18 virtual void RequestVSyncNotification(VSyncClient* client) OVERRIDE {
19 client_ = client;
22 bool IsVSyncNotificationEnabled() const { return client_ != NULL; }
24 void Trigger(base::TimeTicks frame_time) {
25 if (client_)
26 client_->DidVSync(frame_time);
29 private:
30 VSyncClient* client_;
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_);
41 protected:
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.
69 client_.Reset();
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.
102 client_.Reset();
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);
123 } // namespace
124 } // namespace cc