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.
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) \
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) \
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()) \
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, \
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);
62 class MockBeginFrameObserver
: public BeginFrameObserver
{
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())
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
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
111 obs
.LastUsedBeginFrameArgs(),
112 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 400, 600, 300));
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
);
134 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 100, 200, 300));
136 obs
.LastUsedBeginFrameArgs(),
137 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 100, 200, 300));
141 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 400, 600, 300));
143 obs
.LastUsedBeginFrameArgs(),
144 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 100, 200, 300));
148 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 450, 650, 300));
150 obs
.LastUsedBeginFrameArgs(),
151 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 100, 200, 300));
155 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 700, 900, 300));
157 obs
.LastUsedBeginFrameArgs(),
158 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 700, 900, 300));
161 const BeginFrameArgs
MockBeginFrameObserver::kDefaultBeginFrameArgs
=
162 CreateBeginFrameArgsForTesting(
166 FROM_HERE_WITH_EXPLICIT_FUNCTION(
167 "MockBeginFrameObserver::kDefaultBeginFrameArgs"),
173 // BeginFrameObserverMixIn testing ---------------------------------------
174 class MockMinimalBeginFrameObserverMixIn
: public BeginFrameObserverMixIn
{
176 MOCK_METHOD1(OnBeginFrameMixInDelegate
, bool(const BeginFrameArgs
&));
177 int64_t dropped_begin_frame_args() const { return dropped_begin_frame_args_
; }
180 TEST(BeginFrameObserverMixInTest
, OnBeginFrameImplementation
) {
181 using ::testing::Return
;
182 MockMinimalBeginFrameObserverMixIn 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());
190 EXPECT_DEATH({ obs
.OnBeginFrame(BeginFrameArgs()); }, "");
193 BeginFrameArgs args1
=
194 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 100, 200, 300);
195 EXPECT_CALL(obs
, OnBeginFrameMixInDelegate(args1
)).WillOnce(Return(true));
196 obs
.OnBeginFrame(args1
);
197 EXPECT_EQ(args1
, obs
.LastUsedBeginFrameArgs());
198 EXPECT_EQ(0, obs
.dropped_begin_frame_args());
202 obs
.OnBeginFrame(CreateBeginFrameArgsForTesting(
203 BEGINFRAME_FROM_HERE
, 50, 200, 300));
208 // Returning false shouldn't update the LastUsedBeginFrameArgs value.
209 BeginFrameArgs args2
=
210 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, 200, 300, 400);
211 EXPECT_CALL(obs
, OnBeginFrameMixInDelegate(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
, OnBeginFrameMixInDelegate(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(BeginFrameSourceMixInTest
, ObserverManipulation
) {
226 MockBeginFrameObserver obs
;
227 MockBeginFrameObserver otherObs
;
228 FakeBeginFrameSource source
;
230 source
.AddObserver(&obs
);
231 EXPECT_EQ(&obs
, source
.GetObserver());
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.
242 source
.RemoveObserver(&obs
);
243 source
.RemoveObserver(&obs
);
247 source
.RemoveObserver(&obs
);
249 source
.AddObserver(&otherObs
);
250 EXPECT_EQ(&otherObs
, source
.GetObserver());
251 source
.RemoveObserver(&otherObs
);
254 TEST(BeginFrameSourceMixInTest
, 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(BeginFrameSourceMixInTest
, NoObserver
) {
270 FakeBeginFrameSource source
;
271 SEND_BEGIN_FRAME_DROP(source
, 100, 200, 300);
274 TEST(BeginFrameSourceMixInTest
, 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 BeginFrameObserverMixIn
{
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();
295 // BeginFrameObserverMixIn
296 bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) override
{
301 TEST(BeginFrameSourceMixInTest
, 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
{
316 static scoped_ptr
<TestBackToBackBeginFrameSource
> Create(
317 scoped_refptr
<TestNowSource
> now_src
,
318 base::SingleThreadTaskRunner
* task_runner
) {
319 return make_scoped_ptr(
320 new TestBackToBackBeginFrameSource(now_src
, task_runner
));
324 TestBackToBackBeginFrameSource(scoped_refptr
<TestNowSource
> now_src
,
325 base::SingleThreadTaskRunner
* task_runner
)
326 : BackToBackBeginFrameSource(task_runner
), now_src_(now_src
) {}
328 base::TimeTicks
Now() override
{ return now_src_
->Now(); }
330 scoped_refptr
<TestNowSource
> now_src_
;
333 class BackToBackBeginFrameSourceTest
: public ::testing::Test
{
335 static const int64_t kDeadline
;
336 static const int64_t kInterval
;
338 scoped_refptr
<TestNowSource
> now_src_
;
339 scoped_refptr
<OrderedSimpleTaskRunner
> task_runner_
;
340 scoped_ptr
<TestBackToBackBeginFrameSource
> source_
;
341 scoped_ptr
<MockBeginFrameObserver
> obs_
;
343 void SetUp() override
{
344 now_src_
= TestNowSource::Create(1000);
346 make_scoped_refptr(new OrderedSimpleTaskRunner(now_src_
, false));
347 task_runner_
->SetRunTaskLimit(1);
349 TestBackToBackBeginFrameSource::Create(now_src_
, task_runner_
.get());
350 obs_
= make_scoped_ptr(new ::testing::StrictMock
<MockBeginFrameObserver
>());
351 source_
->AddObserver(obs_
.get());
354 void TearDown() override
{ obs_
.reset(); }
357 const int64_t BackToBackBeginFrameSourceTest::kDeadline
=
358 BeginFrameArgs::DefaultInterval().ToInternalValue();
360 const int64_t BackToBackBeginFrameSourceTest::kInterval
=
361 BeginFrameArgs::DefaultInterval().ToInternalValue();
363 TEST_F(BackToBackBeginFrameSourceTest
, SetNeedsBeginFramesSendsBeginFrame
) {
364 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
365 source_
->SetNeedsBeginFrames(true);
366 EXPECT_TRUE(task_runner_
->HasPendingTasks());
367 task_runner_
->RunUntilIdle();
369 EXPECT_BEGIN_FRAME_USED(*obs_
, 1100, 1100 + kDeadline
, kInterval
);
370 now_src_
->AdvanceNowMicroseconds(100);
371 source_
->DidFinishFrame(0);
372 task_runner_
->RunUntilIdle();
375 TEST_F(BackToBackBeginFrameSourceTest
,
376 DidFinishFrameThenSetNeedsBeginFramesProducesNoFrame
) {
377 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
378 source_
->SetNeedsBeginFrames(true);
379 task_runner_
->RunUntilIdle();
381 source_
->SetNeedsBeginFrames(false);
382 source_
->DidFinishFrame(0);
384 EXPECT_FALSE(task_runner_
->HasPendingTasks());
387 TEST_F(BackToBackBeginFrameSourceTest
,
388 SetNeedsBeginFramesThenDidFinishFrameProducesNoFrame
) {
389 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
390 source_
->SetNeedsBeginFrames(true);
391 task_runner_
->RunUntilIdle();
393 now_src_
->AdvanceNowMicroseconds(100);
394 source_
->DidFinishFrame(0);
395 source_
->SetNeedsBeginFrames(false);
397 EXPECT_TRUE(task_runner_
->HasPendingTasks());
398 task_runner_
->RunUntilIdle();
401 TEST_F(BackToBackBeginFrameSourceTest
,
402 DidFinishFrameThenTogglingSetNeedsBeginFramesProducesCorrectFrame
) {
403 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
404 source_
->SetNeedsBeginFrames(true);
405 task_runner_
->RunUntilIdle();
407 now_src_
->AdvanceNowMicroseconds(100);
409 source_
->SetNeedsBeginFrames(false);
410 now_src_
->AdvanceNowMicroseconds(10);
411 source_
->DidFinishFrame(0);
412 now_src_
->AdvanceNowMicroseconds(10);
413 source_
->SetNeedsBeginFrames(false);
414 now_src_
->AdvanceNowMicroseconds(10);
415 source_
->SetNeedsBeginFrames(true);
417 EXPECT_BEGIN_FRAME_USED(*obs_
, 1130, 1130 + kDeadline
, kInterval
);
418 EXPECT_TRUE(task_runner_
->HasPendingTasks());
419 task_runner_
->RunUntilIdle();
422 TEST_F(BackToBackBeginFrameSourceTest
,
423 TogglingSetNeedsBeginFramesThenDidFinishFrameProducesCorrectFrame
) {
424 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
425 source_
->SetNeedsBeginFrames(true);
426 task_runner_
->RunUntilIdle();
428 now_src_
->AdvanceNowMicroseconds(100);
429 source_
->DidFinishFrame(0);
430 now_src_
->AdvanceNowMicroseconds(10);
431 source_
->SetNeedsBeginFrames(false);
432 now_src_
->AdvanceNowMicroseconds(10);
433 source_
->SetNeedsBeginFrames(true);
434 now_src_
->AdvanceNowMicroseconds(10);
436 EXPECT_BEGIN_FRAME_USED(*obs_
, 1130, 1130 + kDeadline
, kInterval
);
437 EXPECT_TRUE(task_runner_
->HasPendingTasks());
438 task_runner_
->RunUntilIdle();
441 TEST_F(BackToBackBeginFrameSourceTest
, DidFinishFrameNeedsBeginFrameFalse
) {
442 source_
->SetNeedsBeginFrames(false);
443 source_
->DidFinishFrame(0);
444 EXPECT_FALSE(task_runner_
->RunPendingTasks());
447 TEST_F(BackToBackBeginFrameSourceTest
, DidFinishFrameRemainingFrames
) {
448 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
449 source_
->SetNeedsBeginFrames(true);
450 task_runner_
->RunUntilIdle();
452 now_src_
->AdvanceNowMicroseconds(100);
454 source_
->DidFinishFrame(3);
455 EXPECT_FALSE(task_runner_
->HasPendingTasks());
456 source_
->DidFinishFrame(2);
457 EXPECT_FALSE(task_runner_
->HasPendingTasks());
458 source_
->DidFinishFrame(1);
459 EXPECT_FALSE(task_runner_
->HasPendingTasks());
461 EXPECT_BEGIN_FRAME_USED(*obs_
, 1100, 1100 + kDeadline
, kInterval
);
462 source_
->DidFinishFrame(0);
463 EXPECT_EQ(base::TimeDelta(), task_runner_
->DelayToNextTaskTime());
464 task_runner_
->RunUntilIdle();
467 TEST_F(BackToBackBeginFrameSourceTest
, DidFinishFrameMultipleCallsIdempotent
) {
468 source_
->SetNeedsBeginFrames(true);
469 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
470 task_runner_
->RunUntilIdle();
472 now_src_
->AdvanceNowMicroseconds(100);
473 source_
->DidFinishFrame(0);
474 source_
->DidFinishFrame(0);
475 source_
->DidFinishFrame(0);
476 EXPECT_BEGIN_FRAME_USED(*obs_
, 1100, 1100 + kDeadline
, kInterval
);
477 task_runner_
->RunUntilIdle();
479 now_src_
->AdvanceNowMicroseconds(100);
480 source_
->DidFinishFrame(0);
481 source_
->DidFinishFrame(0);
482 source_
->DidFinishFrame(0);
483 EXPECT_BEGIN_FRAME_USED(*obs_
, 1200, 1200 + kDeadline
, kInterval
);
484 task_runner_
->RunUntilIdle();
487 TEST_F(BackToBackBeginFrameSourceTest
, DelayInPostedTaskProducesCorrectFrame
) {
488 EXPECT_BEGIN_FRAME_USED(*obs_
, 1000, 1000 + kDeadline
, kInterval
);
489 source_
->SetNeedsBeginFrames(true);
490 task_runner_
->RunUntilIdle();
492 now_src_
->AdvanceNowMicroseconds(100);
493 source_
->DidFinishFrame(0);
494 now_src_
->AdvanceNowMicroseconds(50);
495 EXPECT_BEGIN_FRAME_USED(*obs_
, 1150, 1150 + kDeadline
, kInterval
);
497 EXPECT_TRUE(task_runner_
->HasPendingTasks());
498 task_runner_
->RunUntilIdle();
501 // SyntheticBeginFrameSource testing ------------------------------------------
502 class SyntheticBeginFrameSourceTest
: public ::testing::Test
{
504 scoped_refptr
<TestNowSource
> now_src_
;
505 scoped_refptr
<OrderedSimpleTaskRunner
> task_runner_
;
506 scoped_ptr
<TestSyntheticBeginFrameSource
> source_
;
507 scoped_ptr
<MockBeginFrameObserver
> obs_
;
509 void SetUp() override
{
510 now_src_
= TestNowSource::Create(1000);
512 make_scoped_refptr(new OrderedSimpleTaskRunner(now_src_
, false));
513 source_
= TestSyntheticBeginFrameSource::Create(
514 now_src_
, task_runner_
.get(), base::TimeDelta::FromMicroseconds(10000));
515 obs_
= make_scoped_ptr(new MockBeginFrameObserver());
516 source_
->AddObserver(obs_
.get());
519 void TearDown() override
{ obs_
.reset(); }
522 TEST_F(SyntheticBeginFrameSourceTest
,
523 SetNeedsBeginFramesCallsOnBeginFrameWithMissedTick
) {
524 now_src_
->SetNowMicroseconds(10010);
525 EXPECT_CALL((*obs_
), OnBeginFrame(CreateBeginFrameArgsForTesting(
526 BEGINFRAME_FROM_HERE
, 10000, 20000, 10000,
527 BeginFrameArgs::MISSED
)));
528 source_
->SetNeedsBeginFrames(true); // Should cause the last tick to be sent
529 // No tasks should need to be run for this to occur.
532 TEST_F(SyntheticBeginFrameSourceTest
,
533 SetNeedsBeginFramesCallsCausesOnBeginFrame
) {
534 source_
->SetNeedsBeginFrames(true);
535 EXPECT_EQ(10000, task_runner_
->NextTaskTime().ToInternalValue());
537 EXPECT_BEGIN_FRAME_USED(*obs_
, 10000, 20000, 10000);
538 now_src_
->SetNowMicroseconds(10010);
539 task_runner_
->RunPendingTasks();
542 TEST_F(SyntheticBeginFrameSourceTest
, BasicOperation
) {
543 task_runner_
->SetAutoAdvanceNowToPendingTasks(true);
545 source_
->SetNeedsBeginFrames(true);
546 EXPECT_BEGIN_FRAME_USED(*obs_
, 10000, 20000, 10000);
547 EXPECT_BEGIN_FRAME_USED(*obs_
, 20000, 30000, 10000);
548 EXPECT_BEGIN_FRAME_USED(*obs_
, 30000, 40000, 10000);
549 task_runner_
->RunUntilTime(base::TimeTicks::FromInternalValue(30001));
551 source_
->SetNeedsBeginFrames(false);
553 task_runner_
->RunUntilTime(base::TimeTicks::FromInternalValue(60000));
556 TEST_F(SyntheticBeginFrameSourceTest
, VSyncChanges
) {
557 task_runner_
->SetAutoAdvanceNowToPendingTasks(true);
558 source_
->SetNeedsBeginFrames(true);
560 EXPECT_BEGIN_FRAME_USED(*obs_
, 10000, 20000, 10000);
561 EXPECT_BEGIN_FRAME_USED(*obs_
, 20000, 30000, 10000);
562 EXPECT_BEGIN_FRAME_USED(*obs_
, 30000, 40000, 10000);
563 task_runner_
->RunUntilTime(base::TimeTicks::FromInternalValue(30001));
565 // Update the vsync information
566 source_
->OnUpdateVSyncParameters(base::TimeTicks::FromInternalValue(27500),
567 base::TimeDelta::FromMicroseconds(10001));
569 EXPECT_BEGIN_FRAME_USED(*obs_
, 40000, 47502, 10001);
570 EXPECT_BEGIN_FRAME_USED(*obs_
, 47502, 57503, 10001);
571 EXPECT_BEGIN_FRAME_USED(*obs_
, 57503, 67504, 10001);
572 task_runner_
->RunUntilTime(base::TimeTicks::FromInternalValue(60000));
575 // BeginFrameSourceMultiplexer testing -----------------------------------
576 class BeginFrameSourceMultiplexerTest
: public ::testing::Test
{
578 void SetUp() override
{
579 mux_
= BeginFrameSourceMultiplexer::Create();
581 source1_store_
= make_scoped_ptr(new FakeBeginFrameSource());
582 source2_store_
= make_scoped_ptr(new FakeBeginFrameSource());
583 source3_store_
= make_scoped_ptr(new FakeBeginFrameSource());
585 source1_
= source1_store_
.get();
586 source2_
= source2_store_
.get();
587 source3_
= source3_store_
.get();
590 void TearDown() override
{
591 // Make sure the mux is torn down before the sources.
595 scoped_ptr
<BeginFrameSourceMultiplexer
> mux_
;
596 FakeBeginFrameSource
* source1_
;
597 FakeBeginFrameSource
* source2_
;
598 FakeBeginFrameSource
* source3_
;
601 scoped_ptr
<FakeBeginFrameSource
> source1_store_
;
602 scoped_ptr
<FakeBeginFrameSource
> source2_store_
;
603 scoped_ptr
<FakeBeginFrameSource
> source3_store_
;
606 TEST_F(BeginFrameSourceMultiplexerTest
, SourcesManipulation
) {
607 EXPECT_EQ(NULL
, mux_
->ActiveSource());
609 mux_
->AddSource(source1_
);
610 EXPECT_EQ(source1_
, mux_
->ActiveSource());
612 mux_
->SetActiveSource(NULL
);
613 EXPECT_EQ(NULL
, mux_
->ActiveSource());
615 mux_
->SetActiveSource(source1_
);
618 // Setting a source which isn't in the mux as active should DCHECK fail.
619 EXPECT_DEATH({ mux_
->SetActiveSource(source2_
); }, "");
621 // Adding a source which is already added should DCHECK fail.
622 EXPECT_DEATH({ mux_
->AddSource(source1_
); }, "");
624 // Removing a source which isn't in the mux should DCHECK fail.
625 EXPECT_DEATH({ mux_
->RemoveSource(source2_
); }, "");
627 // Removing the active source fails
628 EXPECT_DEATH({ mux_
->RemoveSource(source1_
); }, "");
631 // Test manipulation doesn't segfault.
632 mux_
->AddSource(source2_
);
633 mux_
->RemoveSource(source2_
);
635 mux_
->AddSource(source2_
);
636 mux_
->SetActiveSource(source2_
);
637 EXPECT_EQ(source2_
, mux_
->ActiveSource());
639 mux_
->RemoveSource(source1_
);
642 TEST_F(BeginFrameSourceMultiplexerTest
, NeedsBeginFrames
) {
643 mux_
->AddSource(source1_
);
644 mux_
->AddSource(source2_
);
645 mux_
->SetActiveSource(source1_
);
646 EXPECT_EQ(source1_
->NeedsBeginFrames(), false);
647 EXPECT_EQ(source2_
->NeedsBeginFrames(), false);
649 // Check SetNeedsFrames works
650 mux_
->SetNeedsBeginFrames(true);
651 EXPECT_EQ(source1_
->NeedsBeginFrames(), true);
652 EXPECT_EQ(source2_
->NeedsBeginFrames(), false);
654 mux_
->SetNeedsBeginFrames(false);
655 EXPECT_EQ(source1_
->NeedsBeginFrames(), false);
656 EXPECT_EQ(source2_
->NeedsBeginFrames(), false);
658 // Checking that switching the source makes SetNeedsFrames on the
659 // subsources correctly.
660 mux_
->SetNeedsBeginFrames(true);
662 mux_
->SetActiveSource(source1_
);
663 EXPECT_EQ(source1_
->NeedsBeginFrames(), true);
664 EXPECT_EQ(source2_
->NeedsBeginFrames(), false);
666 mux_
->SetActiveSource(source2_
);
667 EXPECT_EQ(source1_
->NeedsBeginFrames(), false);
668 EXPECT_EQ(source2_
->NeedsBeginFrames(), true);
671 TEST_F(BeginFrameSourceMultiplexerTest
, BeginFramesSimple
) {
672 mux_
->AddSource(source1_
);
673 mux_
->AddSource(source2_
);
674 mux_
->SetActiveSource(source1_
);
676 MockBeginFrameObserver obs
;
677 mux_
->AddObserver(&obs
);
678 EXPECT_BEGIN_FRAME_USED(obs
, 100, 200, 300);
679 EXPECT_BEGIN_FRAME_USED(obs
, 400, 600, 300);
681 mux_
->SetActiveSource(source1_
);
683 SEND_BEGIN_FRAME_USED(*source1_
, 100, 200, 300);
684 SEND_BEGIN_FRAME_DROP(*source2_
, 200, 500, 300);
686 mux_
->SetActiveSource(source2_
);
687 SEND_BEGIN_FRAME_USED(*source2_
, 400, 600, 300);
688 SEND_BEGIN_FRAME_DROP(*source1_
, 500, 700, 300);
691 TEST_F(BeginFrameSourceMultiplexerTest
, BeginFramesBackwardsProtection
) {
692 mux_
->AddSource(source1_
);
693 mux_
->AddSource(source2_
);
695 MockBeginFrameObserver obs
;
696 mux_
->AddObserver(&obs
);
697 EXPECT_BEGIN_FRAME_USED(obs
, 400, 600, 300);
698 EXPECT_BEGIN_FRAME_USED(obs
, 700, 900, 300);
699 EXPECT_BEGIN_FRAME_USED(obs
, 1000, 1200, 300);
700 EXPECT_BEGIN_FRAME_USED(obs
, 1001, 1201, 301);
702 mux_
->SetActiveSource(source1_
);
703 SEND_BEGIN_FRAME_USED(*source1_
, 400, 600, 300);
704 SEND_BEGIN_FRAME_USED(*source1_
, 700, 900, 300);
706 mux_
->SetActiveSource(source2_
);
707 SEND_BEGIN_FRAME_DROP(*source2_
, 699, 899, 300);
708 SEND_BEGIN_FRAME_USED(*source2_
, 1000, 1200, 300);
710 mux_
->SetActiveSource(source1_
);
711 SEND_BEGIN_FRAME_USED(*source1_
, 1001, 1201, 301);
714 TEST_F(BeginFrameSourceMultiplexerTest
, MinimumIntervalNegativeFails
) {
717 { mux_
->SetMinimumInterval(base::TimeDelta::FromInternalValue(-100)); },
722 TEST_F(BeginFrameSourceMultiplexerTest
, MinimumIntervalZero
) {
723 mux_
->SetMinimumInterval(base::TimeDelta());
724 mux_
->AddSource(source1_
);
726 MockBeginFrameObserver obs
;
727 mux_
->AddObserver(&obs
);
728 EXPECT_BEGIN_FRAME_USED(obs
, 100, 200, 300);
729 EXPECT_BEGIN_FRAME_USED(obs
, 400, 600, 300);
730 EXPECT_BEGIN_FRAME_USED(obs
, 700, 900, 300);
732 SEND_BEGIN_FRAME_USED(*source1_
, 100, 200, 300);
733 SEND_BEGIN_FRAME_USED(*source1_
, 400, 600, 300);
734 SEND_BEGIN_FRAME_USED(*source1_
, 700, 900, 300);
737 TEST_F(BeginFrameSourceMultiplexerTest
, MinimumIntervalBasic
) {
738 mux_
->SetMinimumInterval(base::TimeDelta::FromInternalValue(600));
739 mux_
->AddSource(source1_
);
741 MockBeginFrameObserver obs
;
742 mux_
->AddObserver(&obs
);
743 EXPECT_BEGIN_FRAME_USED(obs
, 100, 200, 300);
744 EXPECT_BEGIN_FRAME_USED(obs
, 700, 900, 300);
746 SEND_BEGIN_FRAME_USED(*source1_
, 100, 200, 300);
747 SEND_BEGIN_FRAME_DROP(*source1_
, 400, 600, 300);
748 SEND_BEGIN_FRAME_USED(*source1_
, 700, 900, 300);
751 TEST_F(BeginFrameSourceMultiplexerTest
, MinimumIntervalWithMultipleSources
) {
752 mux_
->SetMinimumInterval(base::TimeDelta::FromMicroseconds(150));
753 mux_
->AddSource(source1_
);
754 mux_
->AddSource(source2_
);
756 MockBeginFrameObserver obs
;
757 mux_
->AddObserver(&obs
);
758 EXPECT_BEGIN_FRAME_USED(obs
, 400, 600, 300);
759 EXPECT_BEGIN_FRAME_USED(obs
, 700, 900, 300);
760 EXPECT_BEGIN_FRAME_USED(obs
, 1050, 1250, 300);
762 mux_
->SetActiveSource(source1_
);
763 SEND_BEGIN_FRAME_USED(*source1_
, 400, 600, 300);
764 SEND_BEGIN_FRAME_USED(*source1_
, 700, 900, 300);
766 mux_
->SetActiveSource(source2_
);
767 SEND_BEGIN_FRAME_DROP(*source2_
, 750, 1050, 300);
768 SEND_BEGIN_FRAME_USED(*source2_
, 1050, 1250, 300);
770 mux_
->SetActiveSource(source1_
);
771 SEND_BEGIN_FRAME_DROP(*source2_
, 1100, 1400, 300);