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(gfx::kNullAcceleratedWidget
,
39 context_factory
, task_runner_
));
41 void TearDown() override
{
43 ui::TerminateContextFactoryForTests();
47 base::SingleThreadTaskRunner
* task_runner() { return task_runner_
.get(); }
48 ui::Compositor
* compositor() { return compositor_
.get(); }
51 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
52 scoped_ptr
<ui::Compositor
> compositor_
;
54 DISALLOW_COPY_AND_ASSIGN(CompositorTest
);
59 TEST_F(CompositorTest
, LocksTimeOut
) {
60 scoped_refptr
<ui::CompositorLock
> lock
;
62 base::RunLoop run_loop
;
63 // Ensure that the lock times out by default.
64 lock
= compositor()->GetCompositorLock();
65 EXPECT_TRUE(compositor()->IsLocked());
66 task_runner()->PostDelayedTask(
67 FROM_HERE
, run_loop
.QuitClosure(),
68 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
70 EXPECT_FALSE(compositor()->IsLocked());
74 base::RunLoop run_loop
;
75 // Ensure that the lock does not time out when set.
76 compositor()->SetLocksWillTimeOut(false);
77 lock
= compositor()->GetCompositorLock();
78 EXPECT_TRUE(compositor()->IsLocked());
79 task_runner()->PostDelayedTask(
80 FROM_HERE
, run_loop
.QuitClosure(),
81 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
83 EXPECT_TRUE(compositor()->IsLocked());
87 TEST_F(CompositorTest
, AddAndRemoveBeginFrameObserver
) {
88 cc::BeginFrameArgs args
=
89 cc::CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
,
90 base::TimeTicks::FromInternalValue(33));
92 // Simulate to trigger new BeginFrame by using |args|.
93 compositor()->SendBeginFramesToChildren(args
);
95 // When |missed_begin_frame_args_| is sent, its type is set to MISSED.
96 cc::BeginFrameArgs
expected_args(args
);
97 expected_args
.type
= cc::BeginFrameArgs::MISSED
;
99 MockCompositorBeginFrameObserver test_observer
;
100 MockCompositorBeginFrameObserver test_observer2
;
101 EXPECT_CALL(test_observer
, OnSendBeginFrame(expected_args
));
102 EXPECT_CALL(test_observer2
, OnSendBeginFrame(expected_args
));
104 // When new observer is added, Compositor immediately calls OnSendBeginFrame
105 // with |missed_begin_frame_args_|.
106 compositor()->AddBeginFrameObserver(&test_observer
);
107 compositor()->AddBeginFrameObserver(&test_observer2
);
108 Mock::VerifyAndClearExpectations(&test_observer
);
109 Mock::VerifyAndClearExpectations(&test_observer2
);
111 // When |test_observer2| is removed and added again, it will be called again.
112 EXPECT_CALL(test_observer2
, OnSendBeginFrame(expected_args
));
113 compositor()->RemoveBeginFrameObserver(&test_observer2
);
114 compositor()->AddBeginFrameObserver(&test_observer2
);
115 Mock::VerifyAndClearExpectations(&test_observer2
);
117 // When all observer is removed, |missed_begin_frame_args_| is invalidated.
118 // So, it is not used for newly added observer.
119 EXPECT_CALL(test_observer2
, OnSendBeginFrame(_
)).Times(0);
120 compositor()->RemoveBeginFrameObserver(&test_observer
);
121 compositor()->RemoveBeginFrameObserver(&test_observer2
);
122 compositor()->AddBeginFrameObserver(&test_observer2
);
123 Mock::VerifyAndClearExpectations(&test_observer2
);
125 compositor()->RemoveBeginFrameObserver(&test_observer2
);