Add iOS bots to 10% CQ experiment
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob129a27d82b01edf46ee104248ddef8dca111dcdf
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.h"
7 #include "base/command_line.h"
8 #include "cc/animation/animation.h"
9 #include "cc/animation/animation_registrar.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/base/switches.h"
13 #include "cc/input/input_handler.h"
14 #include "cc/layers/content_layer.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_impl.h"
17 #include "cc/test/animation_test_common.h"
18 #include "cc/test/begin_frame_args_test.h"
19 #include "cc/test/fake_external_begin_frame_source.h"
20 #include "cc/test/fake_layer_tree_host_client.h"
21 #include "cc/test/fake_output_surface.h"
22 #include "cc/test/test_context_provider.h"
23 #include "cc/test/test_gpu_memory_buffer_manager.h"
24 #include "cc/test/test_shared_bitmap_manager.h"
25 #include "cc/test/tiled_layer_test_common.h"
26 #include "cc/trees/layer_tree_host_client.h"
27 #include "cc/trees/layer_tree_host_impl.h"
28 #include "cc/trees/layer_tree_host_single_thread_client.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "cc/trees/single_thread_proxy.h"
31 #include "cc/trees/thread_proxy.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "ui/gfx/frame_time.h"
34 #include "ui/gfx/geometry/size_conversions.h"
36 namespace cc {
38 TestHooks::TestHooks() {}
40 TestHooks::~TestHooks() {}
42 DrawResult TestHooks::PrepareToDrawOnThread(
43 LayerTreeHostImpl* host_impl,
44 LayerTreeHostImpl::FrameData* frame_data,
45 DrawResult draw_result) {
46 return draw_result;
49 scoped_ptr<Rasterizer> TestHooks::CreateRasterizer(
50 LayerTreeHostImpl* host_impl) {
51 return host_impl->LayerTreeHostImpl::CreateRasterizer();
54 void TestHooks::CreateResourceAndTileTaskWorkerPool(
55 LayerTreeHostImpl* host_impl,
56 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
57 scoped_ptr<ResourcePool>* resource_pool,
58 scoped_ptr<ResourcePool>* staging_resource_pool) {
59 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
60 tile_task_worker_pool, resource_pool, staging_resource_pool);
63 // Adapts ThreadProxy for test. Injects test hooks for testing.
64 class ThreadProxyForTest : public ThreadProxy {
65 public:
66 static scoped_ptr<Proxy> Create(
67 TestHooks* test_hooks,
68 LayerTreeHost* host,
69 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
70 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
71 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
72 return make_scoped_ptr(new ThreadProxyForTest(
73 test_hooks,
74 host,
75 main_task_runner,
76 impl_task_runner,
77 external_begin_frame_source.Pass()));
80 ~ThreadProxyForTest() override {}
82 private:
83 TestHooks* test_hooks_;
85 void WillBeginImplFrame(const BeginFrameArgs& args) override {
86 ThreadProxy::WillBeginImplFrame(args);
87 test_hooks_->WillBeginImplFrame(args);
90 void ScheduledActionSendBeginMainFrame() override {
91 test_hooks_->ScheduledActionWillSendBeginMainFrame();
92 ThreadProxy::ScheduledActionSendBeginMainFrame();
93 test_hooks_->ScheduledActionSendBeginMainFrame();
96 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
97 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
98 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
99 return result;
102 void ScheduledActionAnimate() override {
103 ThreadProxy::ScheduledActionAnimate();
104 test_hooks_->ScheduledActionAnimate();
107 void ScheduledActionCommit() override {
108 ThreadProxy::ScheduledActionCommit();
109 test_hooks_->ScheduledActionCommit();
112 void ScheduledActionBeginOutputSurfaceCreation() override {
113 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
114 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
117 void ScheduledActionPrepareTiles() override {
118 ThreadProxy::ScheduledActionPrepareTiles();
119 test_hooks_->ScheduledActionPrepareTiles();
122 ThreadProxyForTest(
123 TestHooks* test_hooks,
124 LayerTreeHost* host,
125 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
126 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
127 scoped_ptr<BeginFrameSource> external_begin_frame_source)
128 : ThreadProxy(host, main_task_runner,
129 impl_task_runner,
130 external_begin_frame_source.Pass()),
131 test_hooks_(test_hooks) {}
134 // Adapts ThreadProxy for test. Injects test hooks for testing.
135 class SingleThreadProxyForTest : public SingleThreadProxy {
136 public:
137 static scoped_ptr<Proxy> Create(
138 TestHooks* test_hooks,
139 LayerTreeHost* host,
140 LayerTreeHostSingleThreadClient* client,
141 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
142 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
143 return make_scoped_ptr(new SingleThreadProxyForTest(
144 test_hooks, host, client, main_task_runner,
145 external_begin_frame_source.Pass()));
148 ~SingleThreadProxyForTest() override {}
150 private:
151 TestHooks* test_hooks_;
153 void WillBeginImplFrame(const BeginFrameArgs& args) override {
154 SingleThreadProxy::WillBeginImplFrame(args);
155 test_hooks_->WillBeginImplFrame(args);
158 void ScheduledActionSendBeginMainFrame() override {
159 test_hooks_->ScheduledActionWillSendBeginMainFrame();
160 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
161 test_hooks_->ScheduledActionSendBeginMainFrame();
164 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
165 DrawResult result =
166 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
167 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
168 return result;
171 void ScheduledActionAnimate() override {
172 SingleThreadProxy::ScheduledActionAnimate();
173 test_hooks_->ScheduledActionAnimate();
176 void ScheduledActionCommit() override {
177 SingleThreadProxy::ScheduledActionCommit();
178 test_hooks_->ScheduledActionCommit();
181 void ScheduledActionBeginOutputSurfaceCreation() override {
182 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
183 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
186 void ScheduledActionPrepareTiles() override {
187 SingleThreadProxy::ScheduledActionPrepareTiles();
188 test_hooks_->ScheduledActionPrepareTiles();
191 SingleThreadProxyForTest(
192 TestHooks* test_hooks,
193 LayerTreeHost* host,
194 LayerTreeHostSingleThreadClient* client,
195 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
196 scoped_ptr<BeginFrameSource> external_begin_frame_source)
197 : SingleThreadProxy(host, client, main_task_runner,
198 external_begin_frame_source.Pass()),
199 test_hooks_(test_hooks) {}
202 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
203 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
204 public:
205 static scoped_ptr<LayerTreeHostImplForTesting> Create(
206 TestHooks* test_hooks,
207 const LayerTreeSettings& settings,
208 LayerTreeHostImplClient* host_impl_client,
209 Proxy* proxy,
210 SharedBitmapManager* shared_bitmap_manager,
211 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
212 RenderingStatsInstrumentation* stats_instrumentation) {
213 return make_scoped_ptr(
214 new LayerTreeHostImplForTesting(test_hooks,
215 settings,
216 host_impl_client,
217 proxy,
218 shared_bitmap_manager,
219 gpu_memory_buffer_manager,
220 stats_instrumentation));
223 protected:
224 LayerTreeHostImplForTesting(
225 TestHooks* test_hooks,
226 const LayerTreeSettings& settings,
227 LayerTreeHostImplClient* host_impl_client,
228 Proxy* proxy,
229 SharedBitmapManager* shared_bitmap_manager,
230 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
231 RenderingStatsInstrumentation* stats_instrumentation)
232 : LayerTreeHostImpl(settings,
233 host_impl_client,
234 proxy,
235 stats_instrumentation,
236 shared_bitmap_manager,
237 gpu_memory_buffer_manager,
239 test_hooks_(test_hooks),
240 block_notify_ready_to_activate_for_testing_(false),
241 notify_ready_to_activate_was_blocked_(false) {}
243 scoped_ptr<Rasterizer> CreateRasterizer() override {
244 return test_hooks_->CreateRasterizer(this);
247 void CreateResourceAndTileTaskWorkerPool(
248 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
249 scoped_ptr<ResourcePool>* resource_pool,
250 scoped_ptr<ResourcePool>* staging_resource_pool) override {
251 test_hooks_->CreateResourceAndTileTaskWorkerPool(
252 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
255 void WillBeginImplFrame(const BeginFrameArgs& args) override {
256 LayerTreeHostImpl::WillBeginImplFrame(args);
257 test_hooks_->WillBeginImplFrameOnThread(this, args);
260 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
261 LayerTreeHostImpl::BeginMainFrameAborted(reason);
262 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
265 void BeginCommit() override {
266 LayerTreeHostImpl::BeginCommit();
267 test_hooks_->BeginCommitOnThread(this);
270 void CommitComplete() override {
271 LayerTreeHostImpl::CommitComplete();
272 test_hooks_->CommitCompleteOnThread(this);
275 DrawResult PrepareToDraw(FrameData* frame) override {
276 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
277 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
280 void DrawLayers(FrameData* frame, base::TimeTicks frame_begin_time) override {
281 LayerTreeHostImpl::DrawLayers(frame, frame_begin_time);
282 test_hooks_->DrawLayersOnThread(this);
285 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
286 bool result = LayerTreeHostImpl::SwapBuffers(frame);
287 test_hooks_->SwapBuffersOnThread(this, result);
288 return result;
291 void DidSwapBuffersComplete() override {
292 LayerTreeHostImpl::DidSwapBuffersComplete();
293 test_hooks_->SwapBuffersCompleteOnThread(this);
296 void ReclaimResources(const CompositorFrameAck* ack) override {
297 LayerTreeHostImpl::ReclaimResources(ack);
300 void NotifyReadyToActivate() override {
301 if (block_notify_ready_to_activate_for_testing_) {
302 notify_ready_to_activate_was_blocked_ = true;
303 } else {
304 LayerTreeHostImpl::NotifyReadyToActivate();
305 test_hooks_->NotifyReadyToActivateOnThread(this);
309 void NotifyReadyToDraw() override {
310 LayerTreeHostImpl::NotifyReadyToDraw();
311 test_hooks_->NotifyReadyToDrawOnThread(this);
314 void BlockNotifyReadyToActivateForTesting(bool block) override {
315 CHECK(settings().impl_side_painting);
316 CHECK(proxy()->ImplThreadTaskRunner())
317 << "Not supported for single-threaded mode.";
318 block_notify_ready_to_activate_for_testing_ = block;
319 if (!block && notify_ready_to_activate_was_blocked_) {
320 NotifyReadyToActivate();
321 notify_ready_to_activate_was_blocked_ = false;
325 void ActivateSyncTree() override {
326 test_hooks_->WillActivateTreeOnThread(this);
327 LayerTreeHostImpl::ActivateSyncTree();
328 DCHECK(!pending_tree());
329 test_hooks_->DidActivateTreeOnThread(this);
332 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
333 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
334 test_hooks_->InitializedRendererOnThread(this, success);
335 return success;
338 void SetVisible(bool visible) override {
339 LayerTreeHostImpl::SetVisible(visible);
340 test_hooks_->DidSetVisibleOnImplTree(this, visible);
343 void AnimateLayers(base::TimeTicks monotonic_time) override {
344 test_hooks_->WillAnimateLayers(this, monotonic_time);
345 LayerTreeHostImpl::AnimateLayers(monotonic_time);
346 test_hooks_->AnimateLayers(this, monotonic_time);
349 void UpdateAnimationState(bool start_ready_animations) override {
350 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
351 bool has_unfinished_animation = false;
352 AnimationRegistrar::AnimationControllerMap::const_iterator iter =
353 active_animation_controllers().begin();
354 for (; iter != active_animation_controllers().end(); ++iter) {
355 if (iter->second->HasActiveAnimation()) {
356 has_unfinished_animation = true;
357 break;
360 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
363 void NotifyTileStateChanged(const Tile* tile) override {
364 LayerTreeHostImpl::NotifyTileStateChanged(tile);
365 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
368 private:
369 TestHooks* test_hooks_;
370 bool block_notify_ready_to_activate_for_testing_;
371 bool notify_ready_to_activate_was_blocked_;
374 // Implementation of LayerTreeHost callback interface.
375 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
376 public LayerTreeHostSingleThreadClient {
377 public:
378 static scoped_ptr<LayerTreeHostClientForTesting> Create(
379 TestHooks* test_hooks) {
380 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
382 ~LayerTreeHostClientForTesting() override {}
384 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
386 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
388 void BeginMainFrame(const BeginFrameArgs& args) override {
389 test_hooks_->BeginMainFrame(args);
392 void Layout() override { test_hooks_->Layout(); }
394 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
395 const gfx::Vector2dF& outer_delta,
396 const gfx::Vector2dF& elastic_overscroll_delta,
397 float page_scale,
398 float top_controls_delta) override {
399 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
400 elastic_overscroll_delta, page_scale,
401 top_controls_delta);
403 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
404 float scale,
405 float top_controls_delta) override {
406 test_hooks_->ApplyViewportDeltas(scroll_delta,
407 scale,
408 top_controls_delta);
411 void RequestNewOutputSurface() override {
412 test_hooks_->RequestNewOutputSurface();
415 void DidInitializeOutputSurface() override {
416 test_hooks_->DidInitializeOutputSurface();
419 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
420 test_hooks_->SendBeginFramesToChildren(args);
423 void DidFailToInitializeOutputSurface() override {
424 test_hooks_->DidFailToInitializeOutputSurface();
425 RequestNewOutputSurface();
428 void WillCommit() override { test_hooks_->WillCommit(); }
430 void DidCommit() override { test_hooks_->DidCommit(); }
432 void DidCommitAndDrawFrame() override {
433 test_hooks_->DidCommitAndDrawFrame();
436 void DidCompleteSwapBuffers() override {
437 test_hooks_->DidCompleteSwapBuffers();
440 void DidPostSwapBuffers() override {}
441 void DidAbortSwapBuffers() override {}
442 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
443 void DidCompletePageScaleAnimation() override {}
444 void BeginMainFrameNotExpectedSoon() override {}
446 private:
447 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
448 : test_hooks_(test_hooks) {}
450 TestHooks* test_hooks_;
453 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
454 class LayerTreeHostForTesting : public LayerTreeHost {
455 public:
456 static scoped_ptr<LayerTreeHostForTesting> Create(
457 TestHooks* test_hooks,
458 LayerTreeHostClientForTesting* client,
459 const LayerTreeSettings& settings,
460 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
461 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
462 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
463 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
464 new LayerTreeHostForTesting(test_hooks, client, settings));
465 if (impl_task_runner.get()) {
466 layer_tree_host->InitializeForTesting(
467 ThreadProxyForTest::Create(test_hooks,
468 layer_tree_host.get(),
469 main_task_runner,
470 impl_task_runner,
471 external_begin_frame_source.Pass()));
472 } else {
473 layer_tree_host->InitializeForTesting(
474 SingleThreadProxyForTest::Create(
475 test_hooks,
476 layer_tree_host.get(),
477 client,
478 main_task_runner,
479 external_begin_frame_source.Pass()));
481 return layer_tree_host.Pass();
484 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
485 LayerTreeHostImplClient* host_impl_client) override {
486 return LayerTreeHostImplForTesting::Create(
487 test_hooks_,
488 settings(),
489 host_impl_client,
490 proxy(),
491 shared_bitmap_manager_.get(),
492 gpu_memory_buffer_manager_.get(),
493 rendering_stats_instrumentation());
496 void SetNeedsCommit() override {
497 if (!test_started_)
498 return;
499 LayerTreeHost::SetNeedsCommit();
502 void set_test_started(bool started) { test_started_ = started; }
504 private:
505 LayerTreeHostForTesting(TestHooks* test_hooks,
506 LayerTreeHostClient* client,
507 const LayerTreeSettings& settings)
508 : LayerTreeHost(client, NULL, NULL, settings),
509 shared_bitmap_manager_(new TestSharedBitmapManager),
510 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager),
511 test_hooks_(test_hooks),
512 test_started_(false) {}
514 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
515 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
516 TestHooks* test_hooks_;
517 bool test_started_;
520 LayerTreeTest::LayerTreeTest()
521 : output_surface_(nullptr),
522 external_begin_frame_source_(nullptr),
523 beginning_(false),
524 end_when_begin_returns_(false),
525 timed_out_(false),
526 scheduled_(false),
527 started_(false),
528 ended_(false),
529 delegating_renderer_(false),
530 verify_property_trees_(true),
531 timeout_seconds_(0),
532 weak_factory_(this) {
533 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
535 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
536 // specified (for running in a debugger).
537 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
538 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
539 timeout_seconds_ = 5;
542 LayerTreeTest::~LayerTreeTest() {}
544 void LayerTreeTest::EndTest() {
545 if (ended_)
546 return;
547 ended_ = true;
549 // For the case where we EndTest during BeginTest(), set a flag to indicate
550 // that the test should end the second BeginTest regains control.
551 if (beginning_) {
552 end_when_begin_returns_ = true;
553 } else {
554 main_task_runner_->PostTask(
555 FROM_HERE,
556 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
560 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
561 main_task_runner_->PostDelayedTask(
562 FROM_HERE,
563 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
564 base::TimeDelta::FromMilliseconds(delay_milliseconds));
567 void LayerTreeTest::PostAddAnimationToMainThread(
568 Layer* layer_to_receive_animation) {
569 main_task_runner_->PostTask(
570 FROM_HERE,
571 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
572 base::Unretained(layer_to_receive_animation), 0.000004));
575 void LayerTreeTest::PostAddInstantAnimationToMainThread(
576 Layer* layer_to_receive_animation) {
577 main_task_runner_->PostTask(
578 FROM_HERE,
579 base::Bind(&LayerTreeTest::DispatchAddAnimation,
580 main_thread_weak_ptr_,
581 base::Unretained(layer_to_receive_animation),
582 0.0));
585 void LayerTreeTest::PostAddLongAnimationToMainThread(
586 Layer* layer_to_receive_animation) {
587 main_task_runner_->PostTask(
588 FROM_HERE,
589 base::Bind(&LayerTreeTest::DispatchAddAnimation,
590 main_thread_weak_ptr_,
591 base::Unretained(layer_to_receive_animation),
592 1.0));
595 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
596 main_task_runner_->PostTask(
597 FROM_HERE,
598 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
599 main_thread_weak_ptr_, defer_commits));
602 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
603 main_task_runner_->PostTask(FROM_HERE,
604 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
605 main_thread_weak_ptr_));
608 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
609 main_task_runner_->PostTask(
610 FROM_HERE,
611 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
612 main_thread_weak_ptr_));
615 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
616 main_task_runner_->PostTask(FROM_HERE,
617 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
618 main_thread_weak_ptr_));
621 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
622 const gfx::Rect& damage_rect) {
623 main_task_runner_->PostTask(
624 FROM_HERE,
625 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
626 main_thread_weak_ptr_,
627 damage_rect));
630 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
631 main_task_runner_->PostTask(
632 FROM_HERE,
633 base::Bind(
634 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
637 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
638 main_task_runner_->PostTask(
639 FROM_HERE,
640 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
641 main_thread_weak_ptr_));
644 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
645 main_task_runner_->PostTask(
646 FROM_HERE,
647 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
648 main_thread_weak_ptr_));
651 void LayerTreeTest::WillBeginTest() {
652 layer_tree_host_->SetLayerTreeHostClientReady();
655 void LayerTreeTest::DoBeginTest() {
656 client_ = LayerTreeHostClientForTesting::Create(this);
658 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
659 if (settings_.use_external_begin_frame_source) {
660 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
661 settings_.renderer_settings.refresh_rate));
662 external_begin_frame_source_ = external_begin_frame_source.get();
665 DCHECK(!impl_thread_ || impl_thread_->message_loop_proxy().get());
666 layer_tree_host_ = LayerTreeHostForTesting::Create(
667 this,
668 client_.get(),
669 settings_,
670 base::MessageLoopProxy::current(),
671 impl_thread_ ? impl_thread_->message_loop_proxy() : NULL,
672 external_begin_frame_source.Pass());
673 ASSERT_TRUE(layer_tree_host_);
675 started_ = true;
676 beginning_ = true;
677 SetupTree();
678 WillBeginTest();
679 BeginTest();
680 beginning_ = false;
681 if (end_when_begin_returns_)
682 RealEndTest();
684 // Allow commits to happen once BeginTest() has had a chance to post tasks
685 // so that those tasks will happen before the first commit.
686 if (layer_tree_host_) {
687 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
688 ->set_test_started(true);
692 void LayerTreeTest::SetupTree() {
693 if (!layer_tree_host_->root_layer()) {
694 scoped_refptr<Layer> root_layer = Layer::Create();
695 root_layer->SetBounds(gfx::Size(1, 1));
696 root_layer->SetIsDrawable(true);
697 layer_tree_host_->SetRootLayer(root_layer);
700 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
701 gfx::Size device_root_bounds = gfx::ToCeiledSize(
702 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
703 layer_tree_host_->SetViewportSize(device_root_bounds);
706 void LayerTreeTest::Timeout() {
707 timed_out_ = true;
708 EndTest();
711 void LayerTreeTest::RealEndTest() {
712 if (layer_tree_host_ && !timed_out_ &&
713 proxy()->MainFrameWillHappenForTesting()) {
714 main_task_runner_->PostTask(
715 FROM_HERE,
716 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
717 return;
720 base::MessageLoop::current()->Quit();
723 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
724 double animation_duration) {
725 DCHECK(!proxy() || proxy()->IsMainThread());
727 if (layer_to_receive_animation) {
728 AddOpacityTransitionToLayer(
729 layer_to_receive_animation, animation_duration, 0, 0.5, true);
733 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
734 DCHECK(!proxy() || proxy()->IsMainThread());
736 if (layer_tree_host_)
737 layer_tree_host_->SetDeferCommits(defer_commits);
740 void LayerTreeTest::DispatchSetNeedsCommit() {
741 DCHECK(!proxy() || proxy()->IsMainThread());
743 if (layer_tree_host_)
744 layer_tree_host_->SetNeedsCommit();
747 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
748 DCHECK(!proxy() || proxy()->IsMainThread());
750 if (layer_tree_host_)
751 layer_tree_host_->SetNeedsUpdateLayers();
754 void LayerTreeTest::DispatchSetNeedsRedraw() {
755 DCHECK(!proxy() || proxy()->IsMainThread());
757 if (layer_tree_host_)
758 layer_tree_host_->SetNeedsRedraw();
761 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
762 DCHECK(!proxy() || proxy()->IsMainThread());
764 if (layer_tree_host_)
765 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
768 void LayerTreeTest::DispatchSetVisible(bool visible) {
769 DCHECK(!proxy() || proxy()->IsMainThread());
770 if (layer_tree_host_)
771 layer_tree_host_->SetVisible(visible);
774 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
775 DCHECK(!proxy() || proxy()->IsMainThread());
777 if (layer_tree_host_)
778 layer_tree_host_->SetNextCommitForcesRedraw();
781 void LayerTreeTest::DispatchCompositeImmediately() {
782 DCHECK(!proxy() || proxy()->IsMainThread());
783 if (layer_tree_host_)
784 layer_tree_host_->Composite(gfx::FrameTime::Now());
787 void LayerTreeTest::RunTest(bool threaded,
788 bool delegating_renderer,
789 bool impl_side_painting) {
790 if (threaded) {
791 impl_thread_.reset(new base::Thread("Compositor"));
792 ASSERT_TRUE(impl_thread_->Start());
795 main_task_runner_ = base::MessageLoopProxy::current();
797 delegating_renderer_ = delegating_renderer;
799 // Spend less time waiting for BeginFrame because the output is
800 // mocked out.
801 settings_.renderer_settings.refresh_rate = 200.0;
802 settings_.background_animation_rate = 200.0;
803 settings_.impl_side_painting = impl_side_painting;
804 settings_.verify_property_trees = verify_property_trees_;
805 InitializeSettings(&settings_);
807 main_task_runner_->PostTask(
808 FROM_HERE,
809 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
811 if (timeout_seconds_) {
812 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
813 main_task_runner_->PostDelayedTask(
814 FROM_HERE,
815 timeout_.callback(),
816 base::TimeDelta::FromSeconds(timeout_seconds_));
819 base::MessageLoop::current()->Run();
820 DestroyLayerTreeHost();
822 timeout_.Cancel();
824 ASSERT_FALSE(layer_tree_host_.get());
825 client_ = nullptr;
826 if (timed_out_) {
827 FAIL() << "Test timed out";
828 return;
830 AfterTest();
833 void LayerTreeTest::RunTestWithImplSidePainting() {
834 RunTest(true, false, true);
837 void LayerTreeTest::RequestNewOutputSurface() {
838 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
841 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
842 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
843 DCHECK_EQ(delegating_renderer_,
844 output_surface->capabilities().delegated_rendering);
845 output_surface_ = output_surface.get();
847 if (settings_.use_external_begin_frame_source) {
848 DCHECK(external_begin_frame_source_);
849 DCHECK(external_begin_frame_source_->is_ready());
851 return output_surface.Pass();
854 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
855 if (delegating_renderer_)
856 return FakeOutputSurface::CreateDelegating3d();
857 else
858 return FakeOutputSurface::Create3d();
861 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
862 return static_cast<TestContextProvider*>(output_surface_->context_provider())
863 ->TestContext3d();
866 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
867 const {
868 if (impl->pending_tree())
869 return impl->pending_tree()->source_frame_number();
870 if (impl->active_tree())
871 return impl->active_tree()->source_frame_number();
872 // Source frames start at 0, so this is invalid.
873 return -1;
876 void LayerTreeTest::DestroyLayerTreeHost() {
877 if (layer_tree_host_ && layer_tree_host_->root_layer())
878 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
879 layer_tree_host_ = nullptr;
882 } // namespace cc