1 // Copyright 2015 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 "base/run_loop.h"
6 #include "base/thread_task_runner_handle.h"
7 #include "cc/output/begin_frame_args.h"
8 #include "cc/test/begin_frame_args_test.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/compositor/compositor.h"
12 #include "ui/compositor/test/context_factories_for_test.h"
20 class MockCompositorBeginFrameObserver
: public CompositorBeginFrameObserver
{
22 MOCK_METHOD1(OnSendBeginFrame
, void(const cc::BeginFrameArgs
&));
25 // Test fixture for tests that require a ui::Compositor with a real task
27 class CompositorTest
: public testing::Test
{
30 ~CompositorTest() override
{}
32 void SetUp() override
{
33 task_runner_
= base::ThreadTaskRunnerHandle::Get();
35 ui::ContextFactory
* context_factory
=
36 ui::InitializeContextFactoryForTests(false);
38 compositor_
.reset(new ui::Compositor(context_factory
, task_runner_
));
39 compositor_
->SetAcceleratedWidgetAndStartCompositor(
40 gfx::kNullAcceleratedWidget
);
42 void TearDown() override
{
44 ui::TerminateContextFactoryForTests();
48 base::SingleThreadTaskRunner
* task_runner() { return task_runner_
.get(); }
49 ui::Compositor
* compositor() { return compositor_
.get(); }
52 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
53 scoped_ptr
<ui::Compositor
> compositor_
;
55 DISALLOW_COPY_AND_ASSIGN(CompositorTest
);
60 TEST_F(CompositorTest
, LocksTimeOut
) {
61 scoped_refptr
<ui::CompositorLock
> lock
;
63 base::RunLoop run_loop
;
64 // Ensure that the lock times out by default.
65 lock
= compositor()->GetCompositorLock();
66 EXPECT_TRUE(compositor()->IsLocked());
67 task_runner()->PostDelayedTask(
68 FROM_HERE
, run_loop
.QuitClosure(),
69 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
71 EXPECT_FALSE(compositor()->IsLocked());
75 base::RunLoop run_loop
;
76 // Ensure that the lock does not time out when set.
77 compositor()->SetLocksWillTimeOut(false);
78 lock
= compositor()->GetCompositorLock();
79 EXPECT_TRUE(compositor()->IsLocked());
80 task_runner()->PostDelayedTask(
81 FROM_HERE
, run_loop
.QuitClosure(),
82 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
84 EXPECT_TRUE(compositor()->IsLocked());
88 TEST_F(CompositorTest
, AddAndRemoveBeginFrameObserver
) {
89 cc::BeginFrameArgs args
=
90 cc::CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
,
91 base::TimeTicks::FromInternalValue(33));
93 // Simulate to trigger new BeginFrame by using |args|.
94 compositor()->SendBeginFramesToChildren(args
);
96 // When |missed_begin_frame_args_| is sent, its type is set to MISSED.
97 cc::BeginFrameArgs
expected_args(args
);
98 expected_args
.type
= cc::BeginFrameArgs::MISSED
;
100 MockCompositorBeginFrameObserver test_observer
;
101 MockCompositorBeginFrameObserver test_observer2
;
102 EXPECT_CALL(test_observer
, OnSendBeginFrame(expected_args
));
103 EXPECT_CALL(test_observer2
, OnSendBeginFrame(expected_args
));
105 // When new observer is added, Compositor immediately calls OnSendBeginFrame
106 // with |missed_begin_frame_args_|.
107 compositor()->AddBeginFrameObserver(&test_observer
);
108 compositor()->AddBeginFrameObserver(&test_observer2
);
109 Mock::VerifyAndClearExpectations(&test_observer
);
110 Mock::VerifyAndClearExpectations(&test_observer2
);
112 // When |test_observer2| is removed and added again, it will be called again.
113 EXPECT_CALL(test_observer2
, OnSendBeginFrame(expected_args
));
114 compositor()->RemoveBeginFrameObserver(&test_observer2
);
115 compositor()->AddBeginFrameObserver(&test_observer2
);
116 Mock::VerifyAndClearExpectations(&test_observer2
);
118 // When all observer is removed, |missed_begin_frame_args_| is invalidated.
119 // So, it is not used for newly added observer.
120 EXPECT_CALL(test_observer2
, OnSendBeginFrame(_
)).Times(0);
121 compositor()->RemoveBeginFrameObserver(&test_observer
);
122 compositor()->RemoveBeginFrameObserver(&test_observer2
);
123 compositor()->AddBeginFrameObserver(&test_observer2
);
124 Mock::VerifyAndClearExpectations(&test_observer2
);
126 compositor()->RemoveBeginFrameObserver(&test_observer2
);