Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / cc / test / layer_tree_test_common.cc
blobf710f4bd048f9cfdca2802d89921ab374f87490f
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 "cc/test/layer_tree_test_common.h"
7 #include "cc/animation.h"
8 #include "cc/animation_registrar.h"
9 #include "cc/content_layer.h"
10 #include "cc/font_atlas.h"
11 #include "cc/input_handler.h"
12 #include "cc/layer.h"
13 #include "cc/layer_animation_controller.h"
14 #include "cc/layer_impl.h"
15 #include "cc/layer_tree_host_impl.h"
16 #include "cc/single_thread_proxy.h"
17 #include "cc/thread_impl.h"
18 #include "cc/test/animation_test_common.h"
19 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/occlusion_tracker_test_common.h"
21 #include "cc/test/tiled_layer_test_common.h"
22 #include "cc/timing_function.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation.h"
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
26 #include "ui/gfx/size_conversions.h"
28 using namespace WebKit;
30 namespace cc {
32 bool TestHooks::prepareToDrawOnThread(cc::LayerTreeHostImpl*, LayerTreeHostImpl::FrameData&, bool)
34 return true;
37 bool TestHooks::canActivatePendingTree()
39 return true;
42 scoped_ptr<OutputSurface> TestHooks::createOutputSurface()
44 return createFakeOutputSurface();
47 scoped_ptr<MockLayerTreeHostImpl> MockLayerTreeHostImpl::create(TestHooks* testHooks, const LayerTreeSettings& settings, LayerTreeHostImplClient* client, Proxy* proxy)
49 return make_scoped_ptr(new MockLayerTreeHostImpl(testHooks, settings, client, proxy));
52 void MockLayerTreeHostImpl::beginCommit()
54 LayerTreeHostImpl::beginCommit();
55 m_testHooks->beginCommitOnThread(this);
58 void MockLayerTreeHostImpl::commitComplete()
60 LayerTreeHostImpl::commitComplete();
61 m_testHooks->commitCompleteOnThread(this);
64 bool MockLayerTreeHostImpl::prepareToDraw(FrameData& frame)
66 bool result = LayerTreeHostImpl::prepareToDraw(frame);
67 if (!m_testHooks->prepareToDrawOnThread(this, frame, result))
68 result = false;
69 return result;
72 void MockLayerTreeHostImpl::drawLayers(FrameData& frame)
74 LayerTreeHostImpl::drawLayers(frame);
75 m_testHooks->drawLayersOnThread(this);
78 void MockLayerTreeHostImpl::activatePendingTreeIfNeeded()
80 if (!pendingTree())
81 return;
83 if (m_testHooks->canActivatePendingTree())
84 activatePendingTree();
87 void MockLayerTreeHostImpl::animateLayers(base::TimeTicks monotonicTime, base::Time wallClockTime)
89 m_testHooks->willAnimateLayers(this, monotonicTime);
90 LayerTreeHostImpl::animateLayers(monotonicTime, wallClockTime);
91 bool hasUnfinishedAnimation = false;
92 AnimationRegistrar::AnimationControllerMap::const_iterator iter = activeAnimationControllers().begin();
93 for (; iter != activeAnimationControllers().end(); ++iter) {
94 if (iter->second->hasActiveAnimation()) {
95 hasUnfinishedAnimation = true;
96 break;
99 m_testHooks->animateLayers(this, monotonicTime, hasUnfinishedAnimation);
102 base::TimeDelta MockLayerTreeHostImpl::lowFrequencyAnimationInterval() const
104 return base::TimeDelta::FromMilliseconds(16);
107 MockLayerTreeHostImpl::MockLayerTreeHostImpl(TestHooks* testHooks, const LayerTreeSettings& settings, LayerTreeHostImplClient* client, Proxy* proxy)
108 : LayerTreeHostImpl(settings, client, proxy)
109 , m_testHooks(testHooks)
113 // Adapts LayerTreeHost for test. Injects MockLayerTreeHostImpl.
114 class MockLayerTreeHost : public cc::LayerTreeHost {
115 public:
116 static scoped_ptr<MockLayerTreeHost> create(TestHooks* testHooks, cc::LayerTreeHostClient* client, const cc::LayerTreeSettings& settings, scoped_ptr<cc::Thread> implThread)
118 scoped_ptr<MockLayerTreeHost> layerTreeHost(new MockLayerTreeHost(testHooks, client, settings));
119 bool success = layerTreeHost->initialize(implThread.Pass());
120 EXPECT_TRUE(success);
121 return layerTreeHost.Pass();
124 virtual scoped_ptr<cc::LayerTreeHostImpl> createLayerTreeHostImpl(cc::LayerTreeHostImplClient* client)
126 return MockLayerTreeHostImpl::create(m_testHooks, settings(), client, proxy()).PassAs<cc::LayerTreeHostImpl>();
129 virtual void setNeedsCommit() OVERRIDE
131 if (!m_testStarted)
132 return;
133 LayerTreeHost::setNeedsCommit();
136 void setTestStarted(bool started) { m_testStarted = started; }
138 virtual void didDeferCommit() OVERRIDE
140 m_testHooks->didDeferCommit();
143 private:
144 MockLayerTreeHost(TestHooks* testHooks, cc::LayerTreeHostClient* client, const cc::LayerTreeSettings& settings)
145 : LayerTreeHost(client, settings)
146 , m_testHooks(testHooks)
147 , m_testStarted(false)
151 TestHooks* m_testHooks;
152 bool m_testStarted;
155 // Implementation of LayerTreeHost callback interface.
156 class ThreadedMockLayerTreeHostClient : public MockLayerImplTreeHostClient {
157 public:
158 static scoped_ptr<ThreadedMockLayerTreeHostClient> create(TestHooks* testHooks)
160 return make_scoped_ptr(new ThreadedMockLayerTreeHostClient(testHooks));
163 virtual void willBeginFrame() OVERRIDE
167 virtual void didBeginFrame() OVERRIDE
171 virtual void animate(double monotonicTime) OVERRIDE
173 m_testHooks->animate(base::TimeTicks::FromInternalValue(monotonicTime * base::Time::kMicrosecondsPerSecond));
176 virtual void layout() OVERRIDE
178 m_testHooks->layout();
181 virtual void applyScrollAndScale(gfx::Vector2d scrollDelta, float scale) OVERRIDE
183 m_testHooks->applyScrollAndScale(scrollDelta, scale);
186 virtual scoped_ptr<OutputSurface> createOutputSurface() OVERRIDE
188 return m_testHooks->createOutputSurface();
191 virtual void didRecreateOutputSurface(bool succeeded) OVERRIDE
193 m_testHooks->didRecreateOutputSurface(succeeded);
196 virtual scoped_ptr<InputHandler> createInputHandler() OVERRIDE
198 return scoped_ptr<InputHandler>();
201 virtual void willCommit() OVERRIDE
205 virtual void didCommit() OVERRIDE
207 m_testHooks->didCommit();
210 virtual void didCommitAndDrawFrame() OVERRIDE
212 m_testHooks->didCommitAndDrawFrame();
215 virtual void didCompleteSwapBuffers() OVERRIDE
219 virtual void scheduleComposite() OVERRIDE
221 m_testHooks->scheduleComposite();
224 virtual scoped_ptr<FontAtlas> createFontAtlas() OVERRIDE
226 return scoped_ptr<FontAtlas>();
229 private:
230 explicit ThreadedMockLayerTreeHostClient(TestHooks* testHooks) : m_testHooks(testHooks) { }
232 TestHooks* m_testHooks;
235 ThreadedTest::ThreadedTest()
236 : m_beginning(false)
237 , m_endWhenBeginReturns(false)
238 , m_timedOut(false)
239 , m_scheduled(false)
240 , m_started(false)
241 , m_implThread(0)
242 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this))
244 m_mainThreadWeakPtr = m_weakFactory.GetWeakPtr();
247 ThreadedTest::~ThreadedTest()
251 void ThreadedTest::endTest()
253 // For the case where we endTest during beginTest(), set a flag to indicate that
254 // the test should end the second beginTest regains control.
255 if (m_beginning)
256 m_endWhenBeginReturns = true;
257 else
258 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::realEndTest, m_mainThreadWeakPtr));
261 void ThreadedTest::endTestAfterDelay(int delayMilliseconds)
263 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::endTest, m_mainThreadWeakPtr));
266 void ThreadedTest::postAddAnimationToMainThread(Layer* layerToReceiveAnimation)
268 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAddAnimation, m_mainThreadWeakPtr, base::Unretained(layerToReceiveAnimation)));
271 void ThreadedTest::postAddInstantAnimationToMainThread()
273 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAddInstantAnimation, m_mainThreadWeakPtr));
276 void ThreadedTest::postSetNeedsCommitToMainThread()
278 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetNeedsCommit, m_mainThreadWeakPtr));
281 void ThreadedTest::postAcquireLayerTextures()
283 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAcquireLayerTextures, m_mainThreadWeakPtr));
286 void ThreadedTest::postSetNeedsRedrawToMainThread()
288 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetNeedsRedraw, m_mainThreadWeakPtr));
291 void ThreadedTest::postSetVisibleToMainThread(bool visible)
293 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetVisible, m_mainThreadWeakPtr, visible));
296 void ThreadedTest::doBeginTest()
298 m_client = ThreadedMockLayerTreeHostClient::create(this);
300 scoped_ptr<cc::Thread> implCCThread(NULL);
301 if (m_implThread)
302 implCCThread = cc::ThreadImpl::createForDifferentThread(m_implThread->message_loop_proxy());
303 m_layerTreeHost = MockLayerTreeHost::create(this, m_client.get(), m_settings, implCCThread.Pass());
304 ASSERT_TRUE(m_layerTreeHost.get());
306 m_started = true;
307 m_beginning = true;
308 setupTree();
309 m_layerTreeHost->setSurfaceReady();
310 beginTest();
311 m_beginning = false;
312 if (m_endWhenBeginReturns)
313 realEndTest();
315 LayerTreeHost::setNeedsFilterContext(false);
317 // Allow commits to happen once beginTest() has had a chance to post tasks
318 // so that those tasks will happen before the first commit.
319 if (m_layerTreeHost)
320 static_cast<MockLayerTreeHost*>(m_layerTreeHost.get())->setTestStarted(true);
323 void ThreadedTest::setupTree()
325 if (!m_layerTreeHost->rootLayer()) {
326 scoped_refptr<Layer> rootLayer = Layer::create();
327 rootLayer->setBounds(gfx::Size(1, 1));
328 m_layerTreeHost->setRootLayer(rootLayer);
331 gfx::Size rootBounds = m_layerTreeHost->rootLayer()->bounds();
332 gfx::Size deviceRootBounds = gfx::ToCeiledSize(
333 gfx::ScaleSize(rootBounds, m_layerTreeHost->deviceScaleFactor()));
334 m_layerTreeHost->setViewportSize(rootBounds, deviceRootBounds);
337 void ThreadedTest::timeout()
339 m_timedOut = true;
340 endTest();
343 void ThreadedTest::scheduleComposite()
345 if (!m_started || m_scheduled)
346 return;
347 m_scheduled = true;
348 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchComposite, m_mainThreadWeakPtr));
351 void ThreadedTest::realEndTest()
353 if (m_layerTreeHost && proxy()->commitPendingForTesting()) {
354 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::realEndTest, m_mainThreadWeakPtr));
355 return;
358 MessageLoop::current()->Quit();
361 void ThreadedTest::dispatchAddInstantAnimation()
363 DCHECK(!proxy() || proxy()->isMainThread());
365 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer())
366 addOpacityTransitionToLayer(*m_layerTreeHost->rootLayer(), 0, 0, 0.5, false);
369 void ThreadedTest::dispatchAddAnimation(Layer* layerToReceiveAnimation)
371 DCHECK(!proxy() || proxy()->isMainThread());
373 if (layerToReceiveAnimation)
374 addOpacityTransitionToLayer(*layerToReceiveAnimation, 10, 0, 0.5, true);
377 void ThreadedTest::dispatchSetNeedsCommit()
379 DCHECK(!proxy() || proxy()->isMainThread());
381 if (m_layerTreeHost.get())
382 m_layerTreeHost->setNeedsCommit();
385 void ThreadedTest::dispatchAcquireLayerTextures()
387 DCHECK(!proxy() || proxy()->isMainThread());
389 if (m_layerTreeHost.get())
390 m_layerTreeHost->acquireLayerTextures();
393 void ThreadedTest::dispatchSetNeedsRedraw()
395 DCHECK(!proxy() || proxy()->isMainThread());
397 if (m_layerTreeHost.get())
398 m_layerTreeHost->setNeedsRedraw();
401 void ThreadedTest::dispatchSetVisible(bool visible)
403 DCHECK(!proxy() || proxy()->isMainThread());
405 if (m_layerTreeHost.get())
406 m_layerTreeHost->setVisible(visible);
409 void ThreadedTest::dispatchComposite()
411 m_scheduled = false;
412 if (m_layerTreeHost.get())
413 m_layerTreeHost->composite();
416 void ThreadedTest::runTest(bool threaded)
418 if (threaded) {
419 m_implThread.reset(new base::Thread("ThreadedTest"));
420 ASSERT_TRUE(m_implThread->Start());
423 m_mainCCThread = cc::ThreadImpl::createForCurrentThread();
425 initializeSettings(m_settings);
427 m_mainCCThread->postTask(base::Bind(&ThreadedTest::doBeginTest, base::Unretained(this)));
428 m_timeout.Reset(base::Bind(&ThreadedTest::timeout, base::Unretained(this)));
429 m_mainCCThread->postDelayedTask(m_timeout.callback(), 5000);
430 MessageLoop::current()->Run();
431 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer())
432 m_layerTreeHost->rootLayer()->setLayerTreeHost(0);
433 m_layerTreeHost.reset();
435 m_timeout.Cancel();
437 ASSERT_FALSE(m_layerTreeHost.get());
438 m_client.reset();
439 if (m_timedOut) {
440 FAIL() << "Test timed out";
441 return;
443 afterTest();
446 } // namespace cc