Do not clear ephemeral device permissions when an app is suspended.
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob20ea9c60a5d60393492e44486b09c3419361181c
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 "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "cc/animation/animation.h"
12 #include "cc/animation/animation_registrar.h"
13 #include "cc/animation/layer_animation_controller.h"
14 #include "cc/animation/timing_function.h"
15 #include "cc/base/switches.h"
16 #include "cc/input/input_handler.h"
17 #include "cc/layers/content_layer.h"
18 #include "cc/layers/layer.h"
19 #include "cc/layers/layer_impl.h"
20 #include "cc/test/animation_test_common.h"
21 #include "cc/test/begin_frame_args_test.h"
22 #include "cc/test/fake_external_begin_frame_source.h"
23 #include "cc/test/fake_layer_tree_host_client.h"
24 #include "cc/test/fake_output_surface.h"
25 #include "cc/test/test_context_provider.h"
26 #include "cc/test/test_gpu_memory_buffer_manager.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "cc/test/test_task_graph_runner.h"
29 #include "cc/test/tiled_layer_test_common.h"
30 #include "cc/trees/layer_tree_host_client.h"
31 #include "cc/trees/layer_tree_host_impl.h"
32 #include "cc/trees/layer_tree_host_single_thread_client.h"
33 #include "cc/trees/layer_tree_impl.h"
34 #include "cc/trees/single_thread_proxy.h"
35 #include "cc/trees/thread_proxy.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "ui/gfx/geometry/size_conversions.h"
39 namespace cc {
41 TestHooks::TestHooks() {}
43 TestHooks::~TestHooks() {}
45 DrawResult TestHooks::PrepareToDrawOnThread(
46 LayerTreeHostImpl* host_impl,
47 LayerTreeHostImpl::FrameData* frame_data,
48 DrawResult draw_result) {
49 return draw_result;
52 void TestHooks::CreateResourceAndTileTaskWorkerPool(
53 LayerTreeHostImpl* host_impl,
54 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
55 scoped_ptr<ResourcePool>* resource_pool,
56 scoped_ptr<ResourcePool>* staging_resource_pool) {
57 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
58 tile_task_worker_pool, resource_pool, staging_resource_pool);
61 // Adapts ThreadProxy for test. Injects test hooks for testing.
62 class ThreadProxyForTest : public ThreadProxy {
63 public:
64 static scoped_ptr<Proxy> Create(
65 TestHooks* test_hooks,
66 LayerTreeHost* host,
67 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
68 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
69 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
70 return make_scoped_ptr(new ThreadProxyForTest(
71 test_hooks,
72 host,
73 main_task_runner,
74 impl_task_runner,
75 external_begin_frame_source.Pass()));
78 ~ThreadProxyForTest() override {}
80 private:
81 TestHooks* test_hooks_;
83 void ScheduledActionSendBeginMainFrame() override {
84 test_hooks_->ScheduledActionWillSendBeginMainFrame();
85 ThreadProxy::ScheduledActionSendBeginMainFrame();
86 test_hooks_->ScheduledActionSendBeginMainFrame();
89 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
90 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
91 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
92 return result;
95 void ScheduledActionAnimate() override {
96 ThreadProxy::ScheduledActionAnimate();
97 test_hooks_->ScheduledActionAnimate();
100 void ScheduledActionCommit() override {
101 ThreadProxy::ScheduledActionCommit();
102 test_hooks_->ScheduledActionCommit();
105 void ScheduledActionBeginOutputSurfaceCreation() override {
106 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
107 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
110 void ScheduledActionPrepareTiles() override {
111 ThreadProxy::ScheduledActionPrepareTiles();
112 test_hooks_->ScheduledActionPrepareTiles();
115 void ScheduledActionInvalidateOutputSurface() override {
116 ThreadProxy::ScheduledActionInvalidateOutputSurface();
117 test_hooks_->ScheduledActionInvalidateOutputSurface();
120 void SendBeginMainFrameNotExpectedSoon() override {
121 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
122 test_hooks_->SendBeginMainFrameNotExpectedSoon();
125 ThreadProxyForTest(
126 TestHooks* test_hooks,
127 LayerTreeHost* host,
128 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
129 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
130 scoped_ptr<BeginFrameSource> external_begin_frame_source)
131 : ThreadProxy(host, main_task_runner,
132 impl_task_runner,
133 external_begin_frame_source.Pass()),
134 test_hooks_(test_hooks) {}
137 // Adapts ThreadProxy for test. Injects test hooks for testing.
138 class SingleThreadProxyForTest : public SingleThreadProxy {
139 public:
140 static scoped_ptr<Proxy> Create(
141 TestHooks* test_hooks,
142 LayerTreeHost* host,
143 LayerTreeHostSingleThreadClient* client,
144 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
145 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
146 return make_scoped_ptr(new SingleThreadProxyForTest(
147 test_hooks, host, client, main_task_runner,
148 external_begin_frame_source.Pass()));
151 ~SingleThreadProxyForTest() override {}
153 private:
154 TestHooks* test_hooks_;
156 void ScheduledActionSendBeginMainFrame() override {
157 test_hooks_->ScheduledActionWillSendBeginMainFrame();
158 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
159 test_hooks_->ScheduledActionSendBeginMainFrame();
162 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
163 DrawResult result =
164 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
165 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
166 return result;
169 void ScheduledActionAnimate() override {
170 SingleThreadProxy::ScheduledActionAnimate();
171 test_hooks_->ScheduledActionAnimate();
174 void ScheduledActionCommit() override {
175 SingleThreadProxy::ScheduledActionCommit();
176 test_hooks_->ScheduledActionCommit();
179 void ScheduledActionBeginOutputSurfaceCreation() override {
180 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
181 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
184 void ScheduledActionPrepareTiles() override {
185 SingleThreadProxy::ScheduledActionPrepareTiles();
186 test_hooks_->ScheduledActionPrepareTiles();
189 void ScheduledActionInvalidateOutputSurface() override {
190 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
191 test_hooks_->ScheduledActionInvalidateOutputSurface();
194 void SendBeginMainFrameNotExpectedSoon() override {
195 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
196 test_hooks_->SendBeginMainFrameNotExpectedSoon();
199 SingleThreadProxyForTest(
200 TestHooks* test_hooks,
201 LayerTreeHost* host,
202 LayerTreeHostSingleThreadClient* client,
203 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
204 scoped_ptr<BeginFrameSource> external_begin_frame_source)
205 : SingleThreadProxy(host, client, main_task_runner,
206 external_begin_frame_source.Pass()),
207 test_hooks_(test_hooks) {}
210 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
211 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
212 public:
213 static scoped_ptr<LayerTreeHostImplForTesting> Create(
214 TestHooks* test_hooks,
215 const LayerTreeSettings& settings,
216 LayerTreeHostImplClient* host_impl_client,
217 Proxy* proxy,
218 SharedBitmapManager* shared_bitmap_manager,
219 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
220 TaskGraphRunner* task_graph_runner,
221 RenderingStatsInstrumentation* stats_instrumentation) {
222 return make_scoped_ptr(new LayerTreeHostImplForTesting(
223 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
224 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
227 protected:
228 LayerTreeHostImplForTesting(
229 TestHooks* test_hooks,
230 const LayerTreeSettings& settings,
231 LayerTreeHostImplClient* host_impl_client,
232 Proxy* proxy,
233 SharedBitmapManager* shared_bitmap_manager,
234 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
235 TaskGraphRunner* task_graph_runner,
236 RenderingStatsInstrumentation* stats_instrumentation)
237 : LayerTreeHostImpl(settings,
238 host_impl_client,
239 proxy,
240 stats_instrumentation,
241 shared_bitmap_manager,
242 gpu_memory_buffer_manager,
243 task_graph_runner,
245 test_hooks_(test_hooks),
246 block_notify_ready_to_activate_for_testing_(false),
247 notify_ready_to_activate_was_blocked_(false) {}
249 void CreateResourceAndTileTaskWorkerPool(
250 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
251 scoped_ptr<ResourcePool>* resource_pool,
252 scoped_ptr<ResourcePool>* staging_resource_pool) override {
253 test_hooks_->CreateResourceAndTileTaskWorkerPool(
254 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
257 void WillBeginImplFrame(const BeginFrameArgs& args) override {
258 LayerTreeHostImpl::WillBeginImplFrame(args);
259 test_hooks_->WillBeginImplFrameOnThread(this, args);
262 void DidFinishImplFrame() override {
263 LayerTreeHostImpl::DidFinishImplFrame();
264 test_hooks_->DidFinishImplFrameOnThread(this);
267 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
268 LayerTreeHostImpl::BeginMainFrameAborted(reason);
269 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
272 void BeginCommit() override {
273 LayerTreeHostImpl::BeginCommit();
274 test_hooks_->BeginCommitOnThread(this);
277 void CommitComplete() override {
278 LayerTreeHostImpl::CommitComplete();
279 test_hooks_->CommitCompleteOnThread(this);
282 DrawResult PrepareToDraw(FrameData* frame) override {
283 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
284 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
287 void DrawLayers(FrameData* frame) override {
288 LayerTreeHostImpl::DrawLayers(frame);
289 test_hooks_->DrawLayersOnThread(this);
292 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
293 bool result = LayerTreeHostImpl::SwapBuffers(frame);
294 test_hooks_->SwapBuffersOnThread(this, result);
295 return result;
298 void DidSwapBuffersComplete() override {
299 LayerTreeHostImpl::DidSwapBuffersComplete();
300 test_hooks_->SwapBuffersCompleteOnThread(this);
303 void ReclaimResources(const CompositorFrameAck* ack) override {
304 LayerTreeHostImpl::ReclaimResources(ack);
307 void NotifyReadyToActivate() override {
308 if (block_notify_ready_to_activate_for_testing_) {
309 notify_ready_to_activate_was_blocked_ = true;
310 } else {
311 LayerTreeHostImpl::NotifyReadyToActivate();
312 test_hooks_->NotifyReadyToActivateOnThread(this);
316 void NotifyReadyToDraw() override {
317 LayerTreeHostImpl::NotifyReadyToDraw();
318 test_hooks_->NotifyReadyToDrawOnThread(this);
321 void BlockNotifyReadyToActivateForTesting(bool block) override {
322 CHECK(settings().impl_side_painting);
323 CHECK(proxy()->ImplThreadTaskRunner())
324 << "Not supported for single-threaded mode.";
325 block_notify_ready_to_activate_for_testing_ = block;
326 if (!block && notify_ready_to_activate_was_blocked_) {
327 NotifyReadyToActivate();
328 notify_ready_to_activate_was_blocked_ = false;
332 void ActivateSyncTree() override {
333 test_hooks_->WillActivateTreeOnThread(this);
334 LayerTreeHostImpl::ActivateSyncTree();
335 DCHECK(!pending_tree());
336 test_hooks_->DidActivateTreeOnThread(this);
339 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
340 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
341 test_hooks_->InitializedRendererOnThread(this, success);
342 return success;
345 void SetVisible(bool visible) override {
346 LayerTreeHostImpl::SetVisible(visible);
347 test_hooks_->DidSetVisibleOnImplTree(this, visible);
350 void AnimateLayers(base::TimeTicks monotonic_time) override {
351 test_hooks_->WillAnimateLayers(this, monotonic_time);
352 LayerTreeHostImpl::AnimateLayers(monotonic_time);
353 test_hooks_->AnimateLayers(this, monotonic_time);
356 void UpdateAnimationState(bool start_ready_animations) override {
357 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
358 bool has_unfinished_animation = false;
359 for (const auto& it :
360 animation_registrar()->active_animation_controllers_for_testing()) {
361 if (it.second->HasActiveAnimation()) {
362 has_unfinished_animation = true;
363 break;
366 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
369 void NotifyTileStateChanged(const Tile* tile) override {
370 LayerTreeHostImpl::NotifyTileStateChanged(tile);
371 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
374 private:
375 TestHooks* test_hooks_;
376 bool block_notify_ready_to_activate_for_testing_;
377 bool notify_ready_to_activate_was_blocked_;
380 // Implementation of LayerTreeHost callback interface.
381 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
382 public LayerTreeHostSingleThreadClient {
383 public:
384 static scoped_ptr<LayerTreeHostClientForTesting> Create(
385 TestHooks* test_hooks) {
386 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
388 ~LayerTreeHostClientForTesting() override {}
390 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
392 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
394 void BeginMainFrame(const BeginFrameArgs& args) override {
395 test_hooks_->BeginMainFrame(args);
398 void Layout() override { test_hooks_->Layout(); }
400 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
401 const gfx::Vector2dF& outer_delta,
402 const gfx::Vector2dF& elastic_overscroll_delta,
403 float page_scale,
404 float top_controls_delta) override {
405 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
406 elastic_overscroll_delta, page_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 SharedBitmapManager* shared_bitmap_manager,
459 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
460 TaskGraphRunner* task_graph_runner,
461 const LayerTreeSettings& settings,
462 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
463 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
464 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
465 LayerTreeHost::InitParams params;
466 params.client = client;
467 params.shared_bitmap_manager = shared_bitmap_manager;
468 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
469 params.task_graph_runner = task_graph_runner;
470 params.settings = &settings;
471 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
472 new LayerTreeHostForTesting(test_hooks, &params));
473 if (impl_task_runner.get()) {
474 layer_tree_host->InitializeForTesting(
475 ThreadProxyForTest::Create(test_hooks,
476 layer_tree_host.get(),
477 main_task_runner,
478 impl_task_runner,
479 external_begin_frame_source.Pass()));
480 } else {
481 layer_tree_host->InitializeForTesting(
482 SingleThreadProxyForTest::Create(
483 test_hooks,
484 layer_tree_host.get(),
485 client,
486 main_task_runner,
487 external_begin_frame_source.Pass()));
489 return layer_tree_host.Pass();
492 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
493 LayerTreeHostImplClient* host_impl_client) override {
494 return LayerTreeHostImplForTesting::Create(
495 test_hooks_, settings(), host_impl_client, proxy(),
496 shared_bitmap_manager(), gpu_memory_buffer_manager(),
497 task_graph_runner(), rendering_stats_instrumentation());
500 void SetNeedsCommit() override {
501 if (!test_started_)
502 return;
503 LayerTreeHost::SetNeedsCommit();
506 void set_test_started(bool started) { test_started_ = started; }
508 private:
509 LayerTreeHostForTesting(TestHooks* test_hooks,
510 LayerTreeHost::InitParams* params)
511 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
513 TestHooks* test_hooks_;
514 bool test_started_;
517 LayerTreeTest::LayerTreeTest()
518 : output_surface_(nullptr),
519 external_begin_frame_source_(nullptr),
520 beginning_(false),
521 end_when_begin_returns_(false),
522 timed_out_(false),
523 scheduled_(false),
524 started_(false),
525 ended_(false),
526 delegating_renderer_(false),
527 verify_property_trees_(false),
528 timeout_seconds_(0),
529 weak_factory_(this) {
530 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
532 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
533 // specified (for running in a debugger).
534 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
535 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
536 timeout_seconds_ = 5;
539 LayerTreeTest::~LayerTreeTest() {}
541 void LayerTreeTest::EndTest() {
542 if (ended_)
543 return;
544 ended_ = true;
546 // For the case where we EndTest during BeginTest(), set a flag to indicate
547 // that the test should end the second BeginTest regains control.
548 if (beginning_) {
549 end_when_begin_returns_ = true;
550 } else {
551 main_task_runner_->PostTask(
552 FROM_HERE,
553 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
557 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
558 main_task_runner_->PostDelayedTask(
559 FROM_HERE,
560 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
561 base::TimeDelta::FromMilliseconds(delay_milliseconds));
564 void LayerTreeTest::PostAddAnimationToMainThread(
565 Layer* layer_to_receive_animation) {
566 main_task_runner_->PostTask(
567 FROM_HERE,
568 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
569 base::Unretained(layer_to_receive_animation), 0.000004));
572 void LayerTreeTest::PostAddInstantAnimationToMainThread(
573 Layer* layer_to_receive_animation) {
574 main_task_runner_->PostTask(
575 FROM_HERE,
576 base::Bind(&LayerTreeTest::DispatchAddAnimation,
577 main_thread_weak_ptr_,
578 base::Unretained(layer_to_receive_animation),
579 0.0));
582 void LayerTreeTest::PostAddLongAnimationToMainThread(
583 Layer* layer_to_receive_animation) {
584 main_task_runner_->PostTask(
585 FROM_HERE,
586 base::Bind(&LayerTreeTest::DispatchAddAnimation,
587 main_thread_weak_ptr_,
588 base::Unretained(layer_to_receive_animation),
589 1.0));
592 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
593 main_task_runner_->PostTask(
594 FROM_HERE,
595 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
596 main_thread_weak_ptr_, defer_commits));
599 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
600 main_task_runner_->PostTask(FROM_HERE,
601 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
602 main_thread_weak_ptr_));
605 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
606 main_task_runner_->PostTask(
607 FROM_HERE,
608 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
609 main_thread_weak_ptr_));
612 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
613 main_task_runner_->PostTask(FROM_HERE,
614 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
615 main_thread_weak_ptr_));
618 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
619 const gfx::Rect& damage_rect) {
620 main_task_runner_->PostTask(
621 FROM_HERE,
622 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
623 main_thread_weak_ptr_,
624 damage_rect));
627 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
628 main_task_runner_->PostTask(
629 FROM_HERE,
630 base::Bind(
631 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
634 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
635 main_task_runner_->PostTask(
636 FROM_HERE,
637 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
638 main_thread_weak_ptr_));
641 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
642 main_task_runner_->PostTask(
643 FROM_HERE,
644 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
645 main_thread_weak_ptr_));
648 void LayerTreeTest::WillBeginTest() {
649 layer_tree_host_->SetLayerTreeHostClientReady();
652 void LayerTreeTest::DoBeginTest() {
653 client_ = LayerTreeHostClientForTesting::Create(this);
655 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
656 if (settings_.use_external_begin_frame_source) {
657 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
658 settings_.renderer_settings.refresh_rate));
659 external_begin_frame_source_ = external_begin_frame_source.get();
662 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
663 layer_tree_host_ = LayerTreeHostForTesting::Create(
664 this, client_.get(), shared_bitmap_manager_.get(),
665 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
666 base::ThreadTaskRunnerHandle::Get(),
667 impl_thread_ ? impl_thread_->task_runner() : NULL,
668 external_begin_frame_source.Pass());
669 ASSERT_TRUE(layer_tree_host_);
671 started_ = true;
672 beginning_ = true;
673 SetupTree();
674 WillBeginTest();
675 BeginTest();
676 beginning_ = false;
677 if (end_when_begin_returns_)
678 RealEndTest();
680 // Allow commits to happen once BeginTest() has had a chance to post tasks
681 // so that those tasks will happen before the first commit.
682 if (layer_tree_host_) {
683 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
684 ->set_test_started(true);
688 void LayerTreeTest::SetupTree() {
689 if (!layer_tree_host_->root_layer()) {
690 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
691 root_layer->SetBounds(gfx::Size(1, 1));
692 root_layer->SetIsDrawable(true);
693 layer_tree_host_->SetRootLayer(root_layer);
696 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
697 gfx::Size device_root_bounds = gfx::ToCeiledSize(
698 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
699 layer_tree_host_->SetViewportSize(device_root_bounds);
702 void LayerTreeTest::Timeout() {
703 timed_out_ = true;
704 EndTest();
707 void LayerTreeTest::RealEndTest() {
708 // TODO(mithro): Make this method only end when not inside an impl frame.
709 if (layer_tree_host_ && !timed_out_ &&
710 proxy()->MainFrameWillHappenForTesting()) {
711 main_task_runner_->PostTask(
712 FROM_HERE,
713 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
714 return;
717 base::MessageLoop::current()->Quit();
720 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
721 double animation_duration) {
722 DCHECK(!proxy() || proxy()->IsMainThread());
724 if (layer_to_receive_animation) {
725 AddOpacityTransitionToLayer(
726 layer_to_receive_animation, animation_duration, 0, 0.5, true);
730 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
731 DCHECK(!proxy() || proxy()->IsMainThread());
733 if (layer_tree_host_)
734 layer_tree_host_->SetDeferCommits(defer_commits);
737 void LayerTreeTest::DispatchSetNeedsCommit() {
738 DCHECK(!proxy() || proxy()->IsMainThread());
740 if (layer_tree_host_)
741 layer_tree_host_->SetNeedsCommit();
744 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
745 DCHECK(!proxy() || proxy()->IsMainThread());
747 if (layer_tree_host_)
748 layer_tree_host_->SetNeedsUpdateLayers();
751 void LayerTreeTest::DispatchSetNeedsRedraw() {
752 DCHECK(!proxy() || proxy()->IsMainThread());
754 if (layer_tree_host_)
755 layer_tree_host_->SetNeedsRedraw();
758 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
759 DCHECK(!proxy() || proxy()->IsMainThread());
761 if (layer_tree_host_)
762 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
765 void LayerTreeTest::DispatchSetVisible(bool visible) {
766 DCHECK(!proxy() || proxy()->IsMainThread());
767 if (layer_tree_host_)
768 layer_tree_host_->SetVisible(visible);
771 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
772 DCHECK(!proxy() || proxy()->IsMainThread());
774 if (layer_tree_host_)
775 layer_tree_host_->SetNextCommitForcesRedraw();
778 void LayerTreeTest::DispatchCompositeImmediately() {
779 DCHECK(!proxy() || proxy()->IsMainThread());
780 if (layer_tree_host_)
781 layer_tree_host_->Composite(base::TimeTicks::Now());
784 void LayerTreeTest::RunTest(bool threaded,
785 bool delegating_renderer,
786 bool impl_side_painting) {
787 if (threaded) {
788 impl_thread_.reset(new base::Thread("Compositor"));
789 ASSERT_TRUE(impl_thread_->Start());
792 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
794 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
795 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
796 task_graph_runner_.reset(new TestTaskGraphRunner);
798 delegating_renderer_ = delegating_renderer;
800 // Spend less time waiting for BeginFrame because the output is
801 // mocked out.
802 settings_.renderer_settings.refresh_rate = 200.0;
803 settings_.background_animation_rate = 200.0;
804 settings_.impl_side_painting = impl_side_painting;
805 settings_.verify_property_trees = verify_property_trees_;
806 InitializeSettings(&settings_);
807 InitializeLayerSettings(&layer_settings_);
809 main_task_runner_->PostTask(
810 FROM_HERE,
811 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
813 if (timeout_seconds_) {
814 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
815 main_task_runner_->PostDelayedTask(
816 FROM_HERE,
817 timeout_.callback(),
818 base::TimeDelta::FromSeconds(timeout_seconds_));
821 base::MessageLoop::current()->Run();
822 DestroyLayerTreeHost();
824 timeout_.Cancel();
826 ASSERT_FALSE(layer_tree_host_.get());
827 client_ = nullptr;
828 if (timed_out_) {
829 FAIL() << "Test timed out";
830 return;
832 AfterTest();
835 void LayerTreeTest::RunTestWithImplSidePainting() {
836 RunTest(true, false, true);
839 void LayerTreeTest::RequestNewOutputSurface() {
840 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
843 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
844 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
845 DCHECK_EQ(delegating_renderer_,
846 output_surface->capabilities().delegated_rendering);
847 output_surface_ = output_surface.get();
849 if (settings_.use_external_begin_frame_source) {
850 DCHECK(external_begin_frame_source_);
851 DCHECK(external_begin_frame_source_->is_ready());
853 return output_surface.Pass();
856 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
857 if (delegating_renderer_)
858 return FakeOutputSurface::CreateDelegating3d();
859 else
860 return FakeOutputSurface::Create3d();
863 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
864 return static_cast<TestContextProvider*>(output_surface_->context_provider())
865 ->TestContext3d();
868 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
869 const {
870 if (impl->pending_tree())
871 return impl->pending_tree()->source_frame_number();
872 if (impl->active_tree())
873 return impl->active_tree()->source_frame_number();
874 // Source frames start at 0, so this is invalid.
875 return -1;
878 void LayerTreeTest::DestroyLayerTreeHost() {
879 if (layer_tree_host_ && layer_tree_host_->root_layer())
880 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
881 layer_tree_host_ = nullptr;
884 LayerTreeHost* LayerTreeTest::layer_tree_host() {
885 // We check for a null proxy here as we sometimes ask for the layer tree host
886 // when the proxy does not exist, often for checking settings after a test has
887 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
888 // elsewhere in this file for other examples.
889 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
890 return layer_tree_host_.get();
893 } // namespace cc