Infobar material design refresh: bg color
[chromium-blink-merge.git] / cc / scheduler / begin_frame_source_unittest.cc
blob1f85622878e91680efd7f5ea56402c3653d81ada
1 // Copyright 2011 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 <deque>
6 #include <string>
8 #include "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/test/test_simple_task_runner.h"
11 #include "cc/scheduler/begin_frame_source.h"
12 #include "cc/test/begin_frame_args_test.h"
13 #include "cc/test/scheduler_test_common.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 // Macros to help set up expected calls on the MockBeginFrameObserver.
18 #define EXPECT_BEGIN_FRAME_DROP(obs, frame_time, deadline, interval) \
19 { \
20 ::testing::Expectation exp = \
21 EXPECT_CALL((obs), OnBeginFrame(CreateBeginFrameArgsForTesting( \
22 BEGINFRAME_FROM_HERE, frame_time, deadline, \
23 interval))).InSequence((obs).sequence); \
26 #define EXPECT_BEGIN_FRAME_USED(obs, frame_time, deadline, interval) \
27 { \
28 BeginFrameArgs args = CreateBeginFrameArgsForTesting( \
29 BEGINFRAME_FROM_HERE, frame_time, deadline, interval); \
30 ::testing::Expectation exp = \
31 EXPECT_CALL((obs), OnBeginFrame(args)).InSequence((obs).sequence); \
32 EXPECT_CALL((obs), LastUsedBeginFrameArgs()) \
33 .Times(::testing::AnyNumber()) \
34 .After(exp) \
35 .WillRepeatedly(::testing::Return(args)); \
38 // Macros to send BeginFrameArgs on a FakeBeginFrameSink (and verify resulting
39 // observer behaviour).
40 #define SEND_BEGIN_FRAME(args_equal_to, source, frame_time, deadline, \
41 interval) \
42 { \
43 BeginFrameArgs old_args = (source).TestLastUsedBeginFrameArgs(); \
44 BeginFrameArgs new_args = CreateBeginFrameArgsForTesting( \
45 BEGINFRAME_FROM_HERE, frame_time, deadline, interval); \
46 ASSERT_FALSE(old_args == new_args); \
47 (source).TestOnBeginFrame(new_args); \
48 EXPECT_EQ(args_equal_to, (source).TestLastUsedBeginFrameArgs()); \
51 // When dropping LastUsedBeginFrameArgs **shouldn't** change.
52 #define SEND_BEGIN_FRAME_DROP(source, frame_time, deadline, interval) \
53 SEND_BEGIN_FRAME(old_args, source, frame_time, deadline, interval);
55 // When used LastUsedBeginFrameArgs **should** be updated.
56 #define SEND_BEGIN_FRAME_USED(source, frame_time, deadline, interval) \
57 SEND_BEGIN_FRAME(new_args, source, frame_time, deadline, interval);
59 namespace cc {
60 namespace {
62 class MockBeginFrameObserver : public BeginFrameObserver {
63 public:
64 MOCK_METHOD1(OnBeginFrame, void(const BeginFrameArgs&));
65 MOCK_CONST_METHOD0(LastUsedBeginFrameArgs, const BeginFrameArgs());
67 virtual void AsValueInto(base::trace_event::TracedValue* dict) const {
68 dict->SetString("type", "MockBeginFrameObserver");
69 dict->BeginDictionary("last_begin_frame_args");
70 LastUsedBeginFrameArgs().AsValueInto(dict);
71 dict->EndDictionary();
74 // A value different from the normal default returned by a BeginFrameObserver
75 // so it is easiable traced back here.
76 static const BeginFrameArgs kDefaultBeginFrameArgs;
78 MockBeginFrameObserver() {
79 // Set a "default" value returned by LastUsedBeginFrameArgs so that gMock
80 // doesn't fail an assert and instead returns useful information.
81 EXPECT_CALL(*this, LastUsedBeginFrameArgs())
82 .Times(::testing::AnyNumber())
83 .InSequence(sequence)
84 .WillRepeatedly(::testing::Return(kDefaultBeginFrameArgs));
86 virtual ~MockBeginFrameObserver() {}
88 ::testing::Sequence sequence;
91 TEST(MockBeginFrameObserverTest, ExpectOnBeginFrame) {
92 ::testing::NiceMock<MockBeginFrameObserver> obs;
93 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
94 EXPECT_BEGIN_FRAME_USED(obs, 400, 600, 300);
95 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
97 EXPECT_EQ(obs.LastUsedBeginFrameArgs(),
98 MockBeginFrameObserver::kDefaultBeginFrameArgs);
100 obs.OnBeginFrame(CreateBeginFrameArgsForTesting(
101 BEGINFRAME_FROM_HERE, 100, 200,
102 300)); // One call to LastUsedBeginFrameArgs
103 EXPECT_EQ(
104 obs.LastUsedBeginFrameArgs(),
105 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300));
107 obs.OnBeginFrame(CreateBeginFrameArgsForTesting(
108 BEGINFRAME_FROM_HERE, 400, 600,
109 300)); // Multiple calls to LastUsedBeginFrameArgs
110 EXPECT_EQ(
111 obs.LastUsedBeginFrameArgs(),
112 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 400, 600, 300));
113 EXPECT_EQ(
114 obs.LastUsedBeginFrameArgs(),
115 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 400, 600, 300));
117 obs.OnBeginFrame(CreateBeginFrameArgsForTesting(
118 BEGINFRAME_FROM_HERE, 700, 900,
119 300)); // No calls to LastUsedBeginFrameArgs
122 TEST(MockBeginFrameObserverTest, ExpectOnBeginFrameStatus) {
123 ::testing::NiceMock<MockBeginFrameObserver> obs;
124 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
125 EXPECT_BEGIN_FRAME_DROP(obs, 400, 600, 300);
126 EXPECT_BEGIN_FRAME_DROP(obs, 450, 650, 300);
127 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
129 EXPECT_EQ(obs.LastUsedBeginFrameArgs(),
130 MockBeginFrameObserver::kDefaultBeginFrameArgs);
132 // Used
133 obs.OnBeginFrame(
134 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300));
135 EXPECT_EQ(
136 obs.LastUsedBeginFrameArgs(),
137 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300));
139 // Dropped
140 obs.OnBeginFrame(
141 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 400, 600, 300));
142 EXPECT_EQ(
143 obs.LastUsedBeginFrameArgs(),
144 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300));
146 // Dropped
147 obs.OnBeginFrame(
148 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 450, 650, 300));
149 EXPECT_EQ(
150 obs.LastUsedBeginFrameArgs(),
151 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300));
153 // Used
154 obs.OnBeginFrame(
155 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 700, 900, 300));
156 EXPECT_EQ(
157 obs.LastUsedBeginFrameArgs(),
158 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 700, 900, 300));
161 const BeginFrameArgs MockBeginFrameObserver::kDefaultBeginFrameArgs =
162 CreateBeginFrameArgsForTesting(
163 #ifdef NDEBUG
164 nullptr,
165 #else
166 FROM_HERE_WITH_EXPLICIT_FUNCTION(
167 "MockBeginFrameObserver::kDefaultBeginFrameArgs"),
168 #endif
171 -1);
173 // BeginFrameObserverBase testing ---------------------------------------
174 class MockMinimalBeginFrameObserverBase : public BeginFrameObserverBase {
175 public:
176 MOCK_METHOD1(OnBeginFrameDerivedImpl, bool(const BeginFrameArgs&));
177 int64_t dropped_begin_frame_args() const { return dropped_begin_frame_args_; }
180 TEST(BeginFrameObserverBaseTest, OnBeginFrameImplementation) {
181 using ::testing::Return;
182 MockMinimalBeginFrameObserverBase obs;
183 ::testing::InSequence ordered; // These calls should be ordered
185 // Initial conditions
186 EXPECT_EQ(BeginFrameArgs(), obs.LastUsedBeginFrameArgs());
187 EXPECT_EQ(0, obs.dropped_begin_frame_args());
189 #ifndef NDEBUG
190 EXPECT_DEATH({ obs.OnBeginFrame(BeginFrameArgs()); }, "");
191 #endif
193 BeginFrameArgs args1 =
194 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 100, 200, 300);
195 EXPECT_CALL(obs, OnBeginFrameDerivedImpl(args1)).WillOnce(Return(true));
196 obs.OnBeginFrame(args1);
197 EXPECT_EQ(args1, obs.LastUsedBeginFrameArgs());
198 EXPECT_EQ(0, obs.dropped_begin_frame_args());
200 #ifndef NDEBUG
201 EXPECT_DEATH({
202 obs.OnBeginFrame(CreateBeginFrameArgsForTesting(
203 BEGINFRAME_FROM_HERE, 50, 200, 300));
205 "");
206 #endif
208 // Returning false shouldn't update the LastUsedBeginFrameArgs value.
209 BeginFrameArgs args2 =
210 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 200, 300, 400);
211 EXPECT_CALL(obs, OnBeginFrameDerivedImpl(args2)).WillOnce(Return(false));
212 obs.OnBeginFrame(args2);
213 EXPECT_EQ(args1, obs.LastUsedBeginFrameArgs());
214 EXPECT_EQ(1, obs.dropped_begin_frame_args());
216 BeginFrameArgs args3 =
217 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, 150, 300, 400);
218 EXPECT_CALL(obs, OnBeginFrameDerivedImpl(args3)).WillOnce(Return(true));
219 obs.OnBeginFrame(args3);
220 EXPECT_EQ(args3, obs.LastUsedBeginFrameArgs());
221 EXPECT_EQ(1, obs.dropped_begin_frame_args());
224 // BeginFrameSource testing ----------------------------------------------
225 TEST(BeginFrameSourceBaseTest, ObserverManipulation) {
226 MockBeginFrameObserver obs;
227 MockBeginFrameObserver otherObs;
228 FakeBeginFrameSource source;
230 source.AddObserver(&obs);
231 EXPECT_EQ(&obs, source.GetObserver());
233 #ifndef NDEBUG
234 // Adding an observer when an observer already exists should DCHECK fail.
235 EXPECT_DEATH({ source.AddObserver(&otherObs); }, "");
237 // Removing wrong observer should DCHECK fail.
238 EXPECT_DEATH({ source.RemoveObserver(&otherObs); }, "");
240 // Removing an observer when there is no observer should DCHECK fail.
241 EXPECT_DEATH({
242 source.RemoveObserver(&obs);
243 source.RemoveObserver(&obs);
245 "");
246 #endif
247 source.RemoveObserver(&obs);
249 source.AddObserver(&otherObs);
250 EXPECT_EQ(&otherObs, source.GetObserver());
251 source.RemoveObserver(&otherObs);
254 TEST(BeginFrameSourceBaseTest, Observer) {
255 FakeBeginFrameSource source;
256 MockBeginFrameObserver obs;
257 source.AddObserver(&obs);
258 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
259 EXPECT_BEGIN_FRAME_DROP(obs, 400, 600, 300);
260 EXPECT_BEGIN_FRAME_DROP(obs, 450, 650, 300);
261 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
263 SEND_BEGIN_FRAME_USED(source, 100, 200, 300);
264 SEND_BEGIN_FRAME_DROP(source, 400, 600, 300);
265 SEND_BEGIN_FRAME_DROP(source, 450, 650, 300);
266 SEND_BEGIN_FRAME_USED(source, 700, 900, 300);
269 TEST(BeginFrameSourceBaseTest, NoObserver) {
270 FakeBeginFrameSource source;
271 SEND_BEGIN_FRAME_DROP(source, 100, 200, 300);
274 TEST(BeginFrameSourceBaseTest, NeedsBeginFrames) {
275 FakeBeginFrameSource source;
276 EXPECT_FALSE(source.NeedsBeginFrames());
277 source.SetNeedsBeginFrames(true);
278 EXPECT_TRUE(source.NeedsBeginFrames());
279 source.SetNeedsBeginFrames(false);
280 EXPECT_FALSE(source.NeedsBeginFrames());
283 class LoopingBeginFrameObserver : public BeginFrameObserverBase {
284 public:
285 BeginFrameSource* source_;
287 void AsValueInto(base::trace_event::TracedValue* dict) const override {
288 dict->SetString("type", "LoopingBeginFrameObserver");
289 dict->BeginDictionary("source");
290 source_->AsValueInto(dict);
291 dict->EndDictionary();
294 protected:
295 // BeginFrameObserverBase
296 bool OnBeginFrameDerivedImpl(const BeginFrameArgs& args) override {
297 return true;
301 TEST(BeginFrameSourceBaseTest, DetectAsValueIntoLoop) {
302 LoopingBeginFrameObserver obs;
303 FakeBeginFrameSource source;
305 obs.source_ = &source;
306 source.AddObserver(&obs);
308 scoped_refptr<base::trace_event::TracedValue> state =
309 new base::trace_event::TracedValue();
310 source.AsValueInto(state.get());
313 // BackToBackBeginFrameSource testing -----------------------------------------
314 class TestBackToBackBeginFrameSource : public BackToBackBeginFrameSource {
315 public:
316 static scoped_ptr<TestBackToBackBeginFrameSource> Create(
317 base::SimpleTestTickClock* now_src,
318 base::SingleThreadTaskRunner* task_runner) {
319 return make_scoped_ptr(
320 new TestBackToBackBeginFrameSource(now_src, task_runner));
323 protected:
324 TestBackToBackBeginFrameSource(base::SimpleTestTickClock* now_src,
325 base::SingleThreadTaskRunner* task_runner)
326 : BackToBackBeginFrameSource(task_runner), now_src_(now_src) {}
328 base::TimeTicks Now() override { return now_src_->NowTicks(); }
330 // Not owned.
331 base::SimpleTestTickClock* now_src_;
334 class BackToBackBeginFrameSourceTest : public ::testing::Test {
335 public:
336 static const int64_t kDeadline;
337 static const int64_t kInterval;
339 scoped_ptr<base::SimpleTestTickClock> now_src_;
340 scoped_refptr<OrderedSimpleTaskRunner> task_runner_;
341 scoped_ptr<TestBackToBackBeginFrameSource> source_;
342 scoped_ptr<MockBeginFrameObserver> obs_;
344 void SetUp() override {
345 now_src_.reset(new base::SimpleTestTickClock());
346 now_src_->Advance(base::TimeDelta::FromMicroseconds(1000));
347 task_runner_ =
348 make_scoped_refptr(new OrderedSimpleTaskRunner(now_src_.get(), false));
349 task_runner_->SetRunTaskLimit(1);
350 source_ = TestBackToBackBeginFrameSource::Create(now_src_.get(),
351 task_runner_.get());
352 obs_ = make_scoped_ptr(new ::testing::StrictMock<MockBeginFrameObserver>());
353 source_->AddObserver(obs_.get());
356 void TearDown() override { obs_.reset(); }
359 const int64_t BackToBackBeginFrameSourceTest::kDeadline =
360 BeginFrameArgs::DefaultInterval().ToInternalValue();
362 const int64_t BackToBackBeginFrameSourceTest::kInterval =
363 BeginFrameArgs::DefaultInterval().ToInternalValue();
365 TEST_F(BackToBackBeginFrameSourceTest, SetNeedsBeginFramesSendsBeginFrame) {
366 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
367 source_->SetNeedsBeginFrames(true);
368 EXPECT_TRUE(task_runner_->HasPendingTasks());
369 task_runner_->RunUntilIdle();
371 EXPECT_BEGIN_FRAME_USED(*obs_, 1100, 1100 + kDeadline, kInterval);
372 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
373 source_->DidFinishFrame(0);
374 task_runner_->RunUntilIdle();
377 TEST_F(BackToBackBeginFrameSourceTest,
378 DidFinishFrameThenSetNeedsBeginFramesProducesNoFrame) {
379 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
380 source_->SetNeedsBeginFrames(true);
381 task_runner_->RunUntilIdle();
383 source_->SetNeedsBeginFrames(false);
384 source_->DidFinishFrame(0);
386 EXPECT_FALSE(task_runner_->HasPendingTasks());
389 TEST_F(BackToBackBeginFrameSourceTest,
390 SetNeedsBeginFramesThenDidFinishFrameProducesNoFrame) {
391 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
392 source_->SetNeedsBeginFrames(true);
393 task_runner_->RunUntilIdle();
395 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
396 source_->DidFinishFrame(0);
397 source_->SetNeedsBeginFrames(false);
399 EXPECT_TRUE(task_runner_->HasPendingTasks());
400 task_runner_->RunUntilIdle();
403 TEST_F(BackToBackBeginFrameSourceTest,
404 DidFinishFrameThenTogglingSetNeedsBeginFramesProducesCorrectFrame) {
405 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
406 source_->SetNeedsBeginFrames(true);
407 task_runner_->RunUntilIdle();
409 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
411 source_->SetNeedsBeginFrames(false);
412 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
413 source_->DidFinishFrame(0);
414 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
415 source_->SetNeedsBeginFrames(false);
416 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
417 source_->SetNeedsBeginFrames(true);
419 EXPECT_BEGIN_FRAME_USED(*obs_, 1130, 1130 + kDeadline, kInterval);
420 EXPECT_TRUE(task_runner_->HasPendingTasks());
421 task_runner_->RunUntilIdle();
424 TEST_F(BackToBackBeginFrameSourceTest,
425 TogglingSetNeedsBeginFramesThenDidFinishFrameProducesCorrectFrame) {
426 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
427 source_->SetNeedsBeginFrames(true);
428 task_runner_->RunUntilIdle();
430 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
431 source_->DidFinishFrame(0);
432 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
433 source_->SetNeedsBeginFrames(false);
434 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
435 source_->SetNeedsBeginFrames(true);
436 now_src_->Advance(base::TimeDelta::FromMicroseconds(10));
438 EXPECT_BEGIN_FRAME_USED(*obs_, 1130, 1130 + kDeadline, kInterval);
439 EXPECT_TRUE(task_runner_->HasPendingTasks());
440 task_runner_->RunUntilIdle();
443 TEST_F(BackToBackBeginFrameSourceTest, DidFinishFrameNeedsBeginFrameFalse) {
444 source_->SetNeedsBeginFrames(false);
445 source_->DidFinishFrame(0);
446 EXPECT_FALSE(task_runner_->RunPendingTasks());
449 TEST_F(BackToBackBeginFrameSourceTest, DidFinishFrameRemainingFrames) {
450 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
451 source_->SetNeedsBeginFrames(true);
452 task_runner_->RunUntilIdle();
454 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
456 source_->DidFinishFrame(3);
457 EXPECT_FALSE(task_runner_->HasPendingTasks());
458 source_->DidFinishFrame(2);
459 EXPECT_FALSE(task_runner_->HasPendingTasks());
460 source_->DidFinishFrame(1);
461 EXPECT_FALSE(task_runner_->HasPendingTasks());
463 EXPECT_BEGIN_FRAME_USED(*obs_, 1100, 1100 + kDeadline, kInterval);
464 source_->DidFinishFrame(0);
465 EXPECT_EQ(base::TimeDelta(), task_runner_->DelayToNextTaskTime());
466 task_runner_->RunUntilIdle();
469 TEST_F(BackToBackBeginFrameSourceTest, DidFinishFrameMultipleCallsIdempotent) {
470 source_->SetNeedsBeginFrames(true);
471 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
472 task_runner_->RunUntilIdle();
474 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
475 source_->DidFinishFrame(0);
476 source_->DidFinishFrame(0);
477 source_->DidFinishFrame(0);
478 EXPECT_BEGIN_FRAME_USED(*obs_, 1100, 1100 + kDeadline, kInterval);
479 task_runner_->RunUntilIdle();
481 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
482 source_->DidFinishFrame(0);
483 source_->DidFinishFrame(0);
484 source_->DidFinishFrame(0);
485 EXPECT_BEGIN_FRAME_USED(*obs_, 1200, 1200 + kDeadline, kInterval);
486 task_runner_->RunUntilIdle();
489 TEST_F(BackToBackBeginFrameSourceTest, DelayInPostedTaskProducesCorrectFrame) {
490 EXPECT_BEGIN_FRAME_USED(*obs_, 1000, 1000 + kDeadline, kInterval);
491 source_->SetNeedsBeginFrames(true);
492 task_runner_->RunUntilIdle();
494 now_src_->Advance(base::TimeDelta::FromMicroseconds(100));
495 source_->DidFinishFrame(0);
496 now_src_->Advance(base::TimeDelta::FromMicroseconds(50));
497 EXPECT_BEGIN_FRAME_USED(*obs_, 1150, 1150 + kDeadline, kInterval);
499 EXPECT_TRUE(task_runner_->HasPendingTasks());
500 task_runner_->RunUntilIdle();
503 // SyntheticBeginFrameSource testing ------------------------------------------
504 class SyntheticBeginFrameSourceTest : public ::testing::Test {
505 public:
506 scoped_ptr<base::SimpleTestTickClock> now_src_;
507 scoped_refptr<OrderedSimpleTaskRunner> task_runner_;
508 scoped_ptr<TestSyntheticBeginFrameSource> source_;
509 scoped_ptr<MockBeginFrameObserver> obs_;
511 void SetUp() override {
512 now_src_.reset(new base::SimpleTestTickClock());
513 now_src_->Advance(base::TimeDelta::FromMicroseconds(1000));
514 task_runner_ =
515 make_scoped_refptr(new OrderedSimpleTaskRunner(now_src_.get(), false));
516 source_ = TestSyntheticBeginFrameSource::Create(
517 now_src_.get(), task_runner_.get(),
518 base::TimeDelta::FromMicroseconds(10000));
519 obs_ = make_scoped_ptr(new MockBeginFrameObserver());
520 source_->AddObserver(obs_.get());
523 void TearDown() override { obs_.reset(); }
526 TEST_F(SyntheticBeginFrameSourceTest,
527 SetNeedsBeginFramesCallsOnBeginFrameWithMissedTick) {
528 now_src_->Advance(base::TimeDelta::FromMicroseconds(9010));
529 EXPECT_CALL((*obs_), OnBeginFrame(CreateBeginFrameArgsForTesting(
530 BEGINFRAME_FROM_HERE, 10000, 20000, 10000,
531 BeginFrameArgs::MISSED)));
532 source_->SetNeedsBeginFrames(true); // Should cause the last tick to be sent
533 // No tasks should need to be run for this to occur.
536 TEST_F(SyntheticBeginFrameSourceTest,
537 SetNeedsBeginFramesCallsCausesOnBeginFrame) {
538 source_->SetNeedsBeginFrames(true);
539 EXPECT_EQ(10000, task_runner_->NextTaskTime().ToInternalValue());
541 EXPECT_BEGIN_FRAME_USED(*obs_, 10000, 20000, 10000);
542 now_src_->Advance(base::TimeDelta::FromMicroseconds(9010));
543 task_runner_->RunPendingTasks();
546 TEST_F(SyntheticBeginFrameSourceTest, BasicOperation) {
547 task_runner_->SetAutoAdvanceNowToPendingTasks(true);
549 source_->SetNeedsBeginFrames(true);
550 EXPECT_BEGIN_FRAME_USED(*obs_, 10000, 20000, 10000);
551 EXPECT_BEGIN_FRAME_USED(*obs_, 20000, 30000, 10000);
552 EXPECT_BEGIN_FRAME_USED(*obs_, 30000, 40000, 10000);
553 task_runner_->RunUntilTime(base::TimeTicks::FromInternalValue(30001));
555 source_->SetNeedsBeginFrames(false);
556 // No new frames....
557 task_runner_->RunUntilTime(base::TimeTicks::FromInternalValue(60000));
560 TEST_F(SyntheticBeginFrameSourceTest, VSyncChanges) {
561 task_runner_->SetAutoAdvanceNowToPendingTasks(true);
562 source_->SetNeedsBeginFrames(true);
564 EXPECT_BEGIN_FRAME_USED(*obs_, 10000, 20000, 10000);
565 EXPECT_BEGIN_FRAME_USED(*obs_, 20000, 30000, 10000);
566 EXPECT_BEGIN_FRAME_USED(*obs_, 30000, 40000, 10000);
567 task_runner_->RunUntilTime(base::TimeTicks::FromInternalValue(30001));
569 // Update the vsync information
570 source_->OnUpdateVSyncParameters(base::TimeTicks::FromInternalValue(27500),
571 base::TimeDelta::FromMicroseconds(10001));
573 EXPECT_BEGIN_FRAME_USED(*obs_, 40000, 47502, 10001);
574 EXPECT_BEGIN_FRAME_USED(*obs_, 47502, 57503, 10001);
575 EXPECT_BEGIN_FRAME_USED(*obs_, 57503, 67504, 10001);
576 task_runner_->RunUntilTime(base::TimeTicks::FromInternalValue(60000));
579 // BeginFrameSourceMultiplexer testing -----------------------------------
580 class BeginFrameSourceMultiplexerTest : public ::testing::Test {
581 protected:
582 void SetUp() override {
583 mux_ = BeginFrameSourceMultiplexer::Create();
585 source1_store_ = make_scoped_ptr(new FakeBeginFrameSource());
586 source2_store_ = make_scoped_ptr(new FakeBeginFrameSource());
587 source3_store_ = make_scoped_ptr(new FakeBeginFrameSource());
589 source1_ = source1_store_.get();
590 source2_ = source2_store_.get();
591 source3_ = source3_store_.get();
594 void TearDown() override {
595 // Make sure the mux is torn down before the sources.
596 mux_.reset();
599 scoped_ptr<BeginFrameSourceMultiplexer> mux_;
600 FakeBeginFrameSource* source1_;
601 FakeBeginFrameSource* source2_;
602 FakeBeginFrameSource* source3_;
604 private:
605 scoped_ptr<FakeBeginFrameSource> source1_store_;
606 scoped_ptr<FakeBeginFrameSource> source2_store_;
607 scoped_ptr<FakeBeginFrameSource> source3_store_;
610 TEST_F(BeginFrameSourceMultiplexerTest, SourcesManipulation) {
611 EXPECT_EQ(NULL, mux_->ActiveSource());
613 mux_->AddSource(source1_);
614 EXPECT_EQ(source1_, mux_->ActiveSource());
616 mux_->SetActiveSource(NULL);
617 EXPECT_EQ(NULL, mux_->ActiveSource());
619 mux_->SetActiveSource(source1_);
621 #ifndef NDEBUG
622 // Setting a source which isn't in the mux as active should DCHECK fail.
623 EXPECT_DEATH({ mux_->SetActiveSource(source2_); }, "");
625 // Adding a source which is already added should DCHECK fail.
626 EXPECT_DEATH({ mux_->AddSource(source1_); }, "");
628 // Removing a source which isn't in the mux should DCHECK fail.
629 EXPECT_DEATH({ mux_->RemoveSource(source2_); }, "");
631 // Removing the active source fails
632 EXPECT_DEATH({ mux_->RemoveSource(source1_); }, "");
633 #endif
635 // Test manipulation doesn't segfault.
636 mux_->AddSource(source2_);
637 mux_->RemoveSource(source2_);
639 mux_->AddSource(source2_);
640 mux_->SetActiveSource(source2_);
641 EXPECT_EQ(source2_, mux_->ActiveSource());
643 mux_->RemoveSource(source1_);
646 TEST_F(BeginFrameSourceMultiplexerTest, NeedsBeginFrames) {
647 mux_->AddSource(source1_);
648 mux_->AddSource(source2_);
649 mux_->SetActiveSource(source1_);
650 EXPECT_EQ(source1_->NeedsBeginFrames(), false);
651 EXPECT_EQ(source2_->NeedsBeginFrames(), false);
653 // Check SetNeedsFrames works
654 mux_->SetNeedsBeginFrames(true);
655 EXPECT_EQ(source1_->NeedsBeginFrames(), true);
656 EXPECT_EQ(source2_->NeedsBeginFrames(), false);
658 mux_->SetNeedsBeginFrames(false);
659 EXPECT_EQ(source1_->NeedsBeginFrames(), false);
660 EXPECT_EQ(source2_->NeedsBeginFrames(), false);
662 // Checking that switching the source makes SetNeedsFrames on the
663 // subsources correctly.
664 mux_->SetNeedsBeginFrames(true);
666 mux_->SetActiveSource(source1_);
667 EXPECT_EQ(source1_->NeedsBeginFrames(), true);
668 EXPECT_EQ(source2_->NeedsBeginFrames(), false);
670 mux_->SetActiveSource(source2_);
671 EXPECT_EQ(source1_->NeedsBeginFrames(), false);
672 EXPECT_EQ(source2_->NeedsBeginFrames(), true);
675 TEST_F(BeginFrameSourceMultiplexerTest, BeginFramesSimple) {
676 mux_->AddSource(source1_);
677 mux_->AddSource(source2_);
678 mux_->SetActiveSource(source1_);
680 MockBeginFrameObserver obs;
681 mux_->AddObserver(&obs);
682 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
683 EXPECT_BEGIN_FRAME_USED(obs, 400, 600, 300);
685 mux_->SetActiveSource(source1_);
687 SEND_BEGIN_FRAME_USED(*source1_, 100, 200, 300);
688 SEND_BEGIN_FRAME_DROP(*source2_, 200, 500, 300);
690 mux_->SetActiveSource(source2_);
691 SEND_BEGIN_FRAME_USED(*source2_, 400, 600, 300);
692 SEND_BEGIN_FRAME_DROP(*source1_, 500, 700, 300);
695 TEST_F(BeginFrameSourceMultiplexerTest, BeginFramesBackwardsProtection) {
696 mux_->AddSource(source1_);
697 mux_->AddSource(source2_);
699 MockBeginFrameObserver obs;
700 mux_->AddObserver(&obs);
701 EXPECT_BEGIN_FRAME_USED(obs, 400, 600, 300);
702 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
703 EXPECT_BEGIN_FRAME_USED(obs, 1000, 1200, 300);
704 EXPECT_BEGIN_FRAME_USED(obs, 1001, 1201, 301);
706 mux_->SetActiveSource(source1_);
707 SEND_BEGIN_FRAME_USED(*source1_, 400, 600, 300);
708 SEND_BEGIN_FRAME_USED(*source1_, 700, 900, 300);
710 mux_->SetActiveSource(source2_);
711 SEND_BEGIN_FRAME_DROP(*source2_, 699, 899, 300);
712 SEND_BEGIN_FRAME_USED(*source2_, 1000, 1200, 300);
714 mux_->SetActiveSource(source1_);
715 SEND_BEGIN_FRAME_USED(*source1_, 1001, 1201, 301);
718 TEST_F(BeginFrameSourceMultiplexerTest, MinimumIntervalNegativeFails) {
719 #ifndef NDEBUG
720 EXPECT_DEATH(
721 { mux_->SetMinimumInterval(base::TimeDelta::FromInternalValue(-100)); },
722 "");
723 #endif
726 TEST_F(BeginFrameSourceMultiplexerTest, MinimumIntervalZero) {
727 mux_->SetMinimumInterval(base::TimeDelta());
728 mux_->AddSource(source1_);
730 MockBeginFrameObserver obs;
731 mux_->AddObserver(&obs);
732 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
733 EXPECT_BEGIN_FRAME_USED(obs, 400, 600, 300);
734 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
736 SEND_BEGIN_FRAME_USED(*source1_, 100, 200, 300);
737 SEND_BEGIN_FRAME_USED(*source1_, 400, 600, 300);
738 SEND_BEGIN_FRAME_USED(*source1_, 700, 900, 300);
741 TEST_F(BeginFrameSourceMultiplexerTest, MinimumIntervalBasic) {
742 mux_->SetMinimumInterval(base::TimeDelta::FromInternalValue(600));
743 mux_->AddSource(source1_);
745 MockBeginFrameObserver obs;
746 mux_->AddObserver(&obs);
747 EXPECT_BEGIN_FRAME_USED(obs, 100, 200, 300);
748 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
750 SEND_BEGIN_FRAME_USED(*source1_, 100, 200, 300);
751 SEND_BEGIN_FRAME_DROP(*source1_, 400, 600, 300);
752 SEND_BEGIN_FRAME_USED(*source1_, 700, 900, 300);
755 TEST_F(BeginFrameSourceMultiplexerTest, MinimumIntervalWithMultipleSources) {
756 mux_->SetMinimumInterval(base::TimeDelta::FromMicroseconds(150));
757 mux_->AddSource(source1_);
758 mux_->AddSource(source2_);
760 MockBeginFrameObserver obs;
761 mux_->AddObserver(&obs);
762 EXPECT_BEGIN_FRAME_USED(obs, 400, 600, 300);
763 EXPECT_BEGIN_FRAME_USED(obs, 700, 900, 300);
764 EXPECT_BEGIN_FRAME_USED(obs, 1050, 1250, 300);
766 mux_->SetActiveSource(source1_);
767 SEND_BEGIN_FRAME_USED(*source1_, 400, 600, 300);
768 SEND_BEGIN_FRAME_USED(*source1_, 700, 900, 300);
770 mux_->SetActiveSource(source2_);
771 SEND_BEGIN_FRAME_DROP(*source2_, 750, 1050, 300);
772 SEND_BEGIN_FRAME_USED(*source2_, 1050, 1250, 300);
774 mux_->SetActiveSource(source1_);
775 SEND_BEGIN_FRAME_DROP(*source2_, 1100, 1400, 300);
778 } // namespace
779 } // namespace cc