ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob9690362009b7cad80035b68c0e02b1e93a466d60
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 for (const auto& it :
353 animation_registrar()->active_animation_controllers()) {
354 if (it.second->HasActiveAnimation()) {
355 has_unfinished_animation = true;
356 break;
359 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
362 void NotifyTileStateChanged(const Tile* tile) override {
363 LayerTreeHostImpl::NotifyTileStateChanged(tile);
364 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
367 private:
368 TestHooks* test_hooks_;
369 bool block_notify_ready_to_activate_for_testing_;
370 bool notify_ready_to_activate_was_blocked_;
373 // Implementation of LayerTreeHost callback interface.
374 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
375 public LayerTreeHostSingleThreadClient {
376 public:
377 static scoped_ptr<LayerTreeHostClientForTesting> Create(
378 TestHooks* test_hooks) {
379 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
381 ~LayerTreeHostClientForTesting() override {}
383 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
385 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
387 void BeginMainFrame(const BeginFrameArgs& args) override {
388 test_hooks_->BeginMainFrame(args);
391 void Layout() override { test_hooks_->Layout(); }
393 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
394 const gfx::Vector2dF& outer_delta,
395 const gfx::Vector2dF& elastic_overscroll_delta,
396 float page_scale,
397 float top_controls_delta) override {
398 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
399 elastic_overscroll_delta, page_scale,
400 top_controls_delta);
402 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
403 float scale,
404 float top_controls_delta) override {
405 test_hooks_->ApplyViewportDeltas(scroll_delta,
406 scale,
407 top_controls_delta);
410 void RequestNewOutputSurface() override {
411 test_hooks_->RequestNewOutputSurface();
414 void DidInitializeOutputSurface() override {
415 test_hooks_->DidInitializeOutputSurface();
418 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
419 test_hooks_->SendBeginFramesToChildren(args);
422 void DidFailToInitializeOutputSurface() override {
423 test_hooks_->DidFailToInitializeOutputSurface();
424 RequestNewOutputSurface();
427 void WillCommit() override { test_hooks_->WillCommit(); }
429 void DidCommit() override { test_hooks_->DidCommit(); }
431 void DidCommitAndDrawFrame() override {
432 test_hooks_->DidCommitAndDrawFrame();
435 void DidCompleteSwapBuffers() override {
436 test_hooks_->DidCompleteSwapBuffers();
439 void DidPostSwapBuffers() override {}
440 void DidAbortSwapBuffers() override {}
441 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
442 void DidCompletePageScaleAnimation() override {}
443 void BeginMainFrameNotExpectedSoon() override {}
445 private:
446 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
447 : test_hooks_(test_hooks) {}
449 TestHooks* test_hooks_;
452 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
453 class LayerTreeHostForTesting : public LayerTreeHost {
454 public:
455 static scoped_ptr<LayerTreeHostForTesting> Create(
456 TestHooks* test_hooks,
457 LayerTreeHostClientForTesting* client,
458 const LayerTreeSettings& settings,
459 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
460 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
461 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
462 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
463 new LayerTreeHostForTesting(test_hooks, client, settings));
464 if (impl_task_runner.get()) {
465 layer_tree_host->InitializeForTesting(
466 ThreadProxyForTest::Create(test_hooks,
467 layer_tree_host.get(),
468 main_task_runner,
469 impl_task_runner,
470 external_begin_frame_source.Pass()));
471 } else {
472 layer_tree_host->InitializeForTesting(
473 SingleThreadProxyForTest::Create(
474 test_hooks,
475 layer_tree_host.get(),
476 client,
477 main_task_runner,
478 external_begin_frame_source.Pass()));
480 return layer_tree_host.Pass();
483 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
484 LayerTreeHostImplClient* host_impl_client) override {
485 return LayerTreeHostImplForTesting::Create(
486 test_hooks_,
487 settings(),
488 host_impl_client,
489 proxy(),
490 shared_bitmap_manager_.get(),
491 gpu_memory_buffer_manager_.get(),
492 rendering_stats_instrumentation());
495 void SetNeedsCommit() override {
496 if (!test_started_)
497 return;
498 LayerTreeHost::SetNeedsCommit();
501 void set_test_started(bool started) { test_started_ = started; }
503 private:
504 LayerTreeHostForTesting(TestHooks* test_hooks,
505 LayerTreeHostClient* client,
506 const LayerTreeSettings& settings)
507 : LayerTreeHost(client, NULL, NULL, settings),
508 shared_bitmap_manager_(new TestSharedBitmapManager),
509 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager),
510 test_hooks_(test_hooks),
511 test_started_(false) {}
513 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
514 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
515 TestHooks* test_hooks_;
516 bool test_started_;
519 LayerTreeTest::LayerTreeTest()
520 : output_surface_(nullptr),
521 external_begin_frame_source_(nullptr),
522 beginning_(false),
523 end_when_begin_returns_(false),
524 timed_out_(false),
525 scheduled_(false),
526 started_(false),
527 ended_(false),
528 delegating_renderer_(false),
529 verify_property_trees_(true),
530 timeout_seconds_(0),
531 weak_factory_(this) {
532 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
534 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
535 // specified (for running in a debugger).
536 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
537 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
538 timeout_seconds_ = 5;
541 LayerTreeTest::~LayerTreeTest() {}
543 void LayerTreeTest::EndTest() {
544 if (ended_)
545 return;
546 ended_ = true;
548 // For the case where we EndTest during BeginTest(), set a flag to indicate
549 // that the test should end the second BeginTest regains control.
550 if (beginning_) {
551 end_when_begin_returns_ = true;
552 } else {
553 main_task_runner_->PostTask(
554 FROM_HERE,
555 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
559 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
560 main_task_runner_->PostDelayedTask(
561 FROM_HERE,
562 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
563 base::TimeDelta::FromMilliseconds(delay_milliseconds));
566 void LayerTreeTest::PostAddAnimationToMainThread(
567 Layer* layer_to_receive_animation) {
568 main_task_runner_->PostTask(
569 FROM_HERE,
570 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
571 base::Unretained(layer_to_receive_animation), 0.000004));
574 void LayerTreeTest::PostAddInstantAnimationToMainThread(
575 Layer* layer_to_receive_animation) {
576 main_task_runner_->PostTask(
577 FROM_HERE,
578 base::Bind(&LayerTreeTest::DispatchAddAnimation,
579 main_thread_weak_ptr_,
580 base::Unretained(layer_to_receive_animation),
581 0.0));
584 void LayerTreeTest::PostAddLongAnimationToMainThread(
585 Layer* layer_to_receive_animation) {
586 main_task_runner_->PostTask(
587 FROM_HERE,
588 base::Bind(&LayerTreeTest::DispatchAddAnimation,
589 main_thread_weak_ptr_,
590 base::Unretained(layer_to_receive_animation),
591 1.0));
594 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
595 main_task_runner_->PostTask(
596 FROM_HERE,
597 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
598 main_thread_weak_ptr_, defer_commits));
601 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
602 main_task_runner_->PostTask(FROM_HERE,
603 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
604 main_thread_weak_ptr_));
607 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
608 main_task_runner_->PostTask(
609 FROM_HERE,
610 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
611 main_thread_weak_ptr_));
614 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
615 main_task_runner_->PostTask(FROM_HERE,
616 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
617 main_thread_weak_ptr_));
620 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
621 const gfx::Rect& damage_rect) {
622 main_task_runner_->PostTask(
623 FROM_HERE,
624 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
625 main_thread_weak_ptr_,
626 damage_rect));
629 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
630 main_task_runner_->PostTask(
631 FROM_HERE,
632 base::Bind(
633 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
636 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
637 main_task_runner_->PostTask(
638 FROM_HERE,
639 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
640 main_thread_weak_ptr_));
643 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
644 main_task_runner_->PostTask(
645 FROM_HERE,
646 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
647 main_thread_weak_ptr_));
650 void LayerTreeTest::WillBeginTest() {
651 layer_tree_host_->SetLayerTreeHostClientReady();
654 void LayerTreeTest::DoBeginTest() {
655 client_ = LayerTreeHostClientForTesting::Create(this);
657 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
658 if (settings_.use_external_begin_frame_source) {
659 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
660 settings_.renderer_settings.refresh_rate));
661 external_begin_frame_source_ = external_begin_frame_source.get();
664 DCHECK(!impl_thread_ || impl_thread_->message_loop_proxy().get());
665 layer_tree_host_ = LayerTreeHostForTesting::Create(
666 this,
667 client_.get(),
668 settings_,
669 base::MessageLoopProxy::current(),
670 impl_thread_ ? impl_thread_->message_loop_proxy() : NULL,
671 external_begin_frame_source.Pass());
672 ASSERT_TRUE(layer_tree_host_);
674 started_ = true;
675 beginning_ = true;
676 SetupTree();
677 WillBeginTest();
678 BeginTest();
679 beginning_ = false;
680 if (end_when_begin_returns_)
681 RealEndTest();
683 // Allow commits to happen once BeginTest() has had a chance to post tasks
684 // so that those tasks will happen before the first commit.
685 if (layer_tree_host_) {
686 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
687 ->set_test_started(true);
691 void LayerTreeTest::SetupTree() {
692 if (!layer_tree_host_->root_layer()) {
693 scoped_refptr<Layer> root_layer = Layer::Create();
694 root_layer->SetBounds(gfx::Size(1, 1));
695 root_layer->SetIsDrawable(true);
696 layer_tree_host_->SetRootLayer(root_layer);
699 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
700 gfx::Size device_root_bounds = gfx::ToCeiledSize(
701 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
702 layer_tree_host_->SetViewportSize(device_root_bounds);
705 void LayerTreeTest::Timeout() {
706 timed_out_ = true;
707 EndTest();
710 void LayerTreeTest::RealEndTest() {
711 if (layer_tree_host_ && !timed_out_ &&
712 proxy()->MainFrameWillHappenForTesting()) {
713 main_task_runner_->PostTask(
714 FROM_HERE,
715 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
716 return;
719 base::MessageLoop::current()->Quit();
722 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
723 double animation_duration) {
724 DCHECK(!proxy() || proxy()->IsMainThread());
726 if (layer_to_receive_animation) {
727 AddOpacityTransitionToLayer(
728 layer_to_receive_animation, animation_duration, 0, 0.5, true);
732 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
733 DCHECK(!proxy() || proxy()->IsMainThread());
735 if (layer_tree_host_)
736 layer_tree_host_->SetDeferCommits(defer_commits);
739 void LayerTreeTest::DispatchSetNeedsCommit() {
740 DCHECK(!proxy() || proxy()->IsMainThread());
742 if (layer_tree_host_)
743 layer_tree_host_->SetNeedsCommit();
746 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
747 DCHECK(!proxy() || proxy()->IsMainThread());
749 if (layer_tree_host_)
750 layer_tree_host_->SetNeedsUpdateLayers();
753 void LayerTreeTest::DispatchSetNeedsRedraw() {
754 DCHECK(!proxy() || proxy()->IsMainThread());
756 if (layer_tree_host_)
757 layer_tree_host_->SetNeedsRedraw();
760 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
761 DCHECK(!proxy() || proxy()->IsMainThread());
763 if (layer_tree_host_)
764 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
767 void LayerTreeTest::DispatchSetVisible(bool visible) {
768 DCHECK(!proxy() || proxy()->IsMainThread());
769 if (layer_tree_host_)
770 layer_tree_host_->SetVisible(visible);
773 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
774 DCHECK(!proxy() || proxy()->IsMainThread());
776 if (layer_tree_host_)
777 layer_tree_host_->SetNextCommitForcesRedraw();
780 void LayerTreeTest::DispatchCompositeImmediately() {
781 DCHECK(!proxy() || proxy()->IsMainThread());
782 if (layer_tree_host_)
783 layer_tree_host_->Composite(gfx::FrameTime::Now());
786 void LayerTreeTest::RunTest(bool threaded,
787 bool delegating_renderer,
788 bool impl_side_painting) {
789 if (threaded) {
790 impl_thread_.reset(new base::Thread("Compositor"));
791 ASSERT_TRUE(impl_thread_->Start());
794 main_task_runner_ = base::MessageLoopProxy::current();
796 delegating_renderer_ = delegating_renderer;
798 // Spend less time waiting for BeginFrame because the output is
799 // mocked out.
800 settings_.renderer_settings.refresh_rate = 200.0;
801 settings_.background_animation_rate = 200.0;
802 settings_.impl_side_painting = impl_side_painting;
803 settings_.verify_property_trees = verify_property_trees_;
804 InitializeSettings(&settings_);
806 main_task_runner_->PostTask(
807 FROM_HERE,
808 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
810 if (timeout_seconds_) {
811 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
812 main_task_runner_->PostDelayedTask(
813 FROM_HERE,
814 timeout_.callback(),
815 base::TimeDelta::FromSeconds(timeout_seconds_));
818 base::MessageLoop::current()->Run();
819 DestroyLayerTreeHost();
821 timeout_.Cancel();
823 ASSERT_FALSE(layer_tree_host_.get());
824 client_ = nullptr;
825 if (timed_out_) {
826 FAIL() << "Test timed out";
827 return;
829 AfterTest();
832 void LayerTreeTest::RunTestWithImplSidePainting() {
833 RunTest(true, false, true);
836 void LayerTreeTest::RequestNewOutputSurface() {
837 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
840 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
841 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
842 DCHECK_EQ(delegating_renderer_,
843 output_surface->capabilities().delegated_rendering);
844 output_surface_ = output_surface.get();
846 if (settings_.use_external_begin_frame_source) {
847 DCHECK(external_begin_frame_source_);
848 DCHECK(external_begin_frame_source_->is_ready());
850 return output_surface.Pass();
853 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
854 if (delegating_renderer_)
855 return FakeOutputSurface::CreateDelegating3d();
856 else
857 return FakeOutputSurface::Create3d();
860 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
861 return static_cast<TestContextProvider*>(output_surface_->context_provider())
862 ->TestContext3d();
865 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
866 const {
867 if (impl->pending_tree())
868 return impl->pending_tree()->source_frame_number();
869 if (impl->active_tree())
870 return impl->active_tree()->source_frame_number();
871 // Source frames start at 0, so this is invalid.
872 return -1;
875 void LayerTreeTest::DestroyLayerTreeHost() {
876 if (layer_tree_host_ && layer_tree_host_->root_layer())
877 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
878 layer_tree_host_ = nullptr;
881 } // namespace cc