[Android] Introduce new UMA action for when user copies Image URL from context menu
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob95e24788e8d97b05fdb04df26b3547177a4d9233
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_host.h"
13 #include "cc/animation/animation_registrar.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/animation/timing_function.h"
16 #include "cc/base/switches.h"
17 #include "cc/input/input_handler.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/trees/layer_tree_host_client.h"
30 #include "cc/trees/layer_tree_host_impl.h"
31 #include "cc/trees/layer_tree_host_single_thread_client.h"
32 #include "cc/trees/layer_tree_impl.h"
33 #include "cc/trees/single_thread_proxy.h"
34 #include "cc/trees/thread_proxy.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "ui/gfx/geometry/size_conversions.h"
38 namespace cc {
40 TestHooks::TestHooks() {}
42 TestHooks::~TestHooks() {}
44 DrawResult TestHooks::PrepareToDrawOnThread(
45 LayerTreeHostImpl* host_impl,
46 LayerTreeHostImpl::FrameData* frame_data,
47 DrawResult draw_result) {
48 return draw_result;
51 void TestHooks::CreateResourceAndTileTaskWorkerPool(
52 LayerTreeHostImpl* host_impl,
53 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
54 scoped_ptr<ResourcePool>* resource_pool,
55 scoped_ptr<ResourcePool>* staging_resource_pool) {
56 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
57 tile_task_worker_pool, resource_pool, staging_resource_pool);
60 // Adapts ThreadProxy for test. Injects test hooks for testing.
61 class ThreadProxyForTest : public ThreadProxy {
62 public:
63 static scoped_ptr<Proxy> Create(
64 TestHooks* test_hooks,
65 LayerTreeHost* host,
66 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
67 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
68 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
69 return make_scoped_ptr(new ThreadProxyForTest(
70 test_hooks,
71 host,
72 main_task_runner,
73 impl_task_runner,
74 external_begin_frame_source.Pass()));
77 ~ThreadProxyForTest() override {}
79 private:
80 TestHooks* test_hooks_;
82 void ScheduledActionSendBeginMainFrame() override {
83 test_hooks_->ScheduledActionWillSendBeginMainFrame();
84 ThreadProxy::ScheduledActionSendBeginMainFrame();
85 test_hooks_->ScheduledActionSendBeginMainFrame();
88 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
89 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
90 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
91 return result;
94 void ScheduledActionAnimate() override {
95 ThreadProxy::ScheduledActionAnimate();
96 test_hooks_->ScheduledActionAnimate();
99 void ScheduledActionCommit() override {
100 ThreadProxy::ScheduledActionCommit();
101 test_hooks_->ScheduledActionCommit();
104 void ScheduledActionBeginOutputSurfaceCreation() override {
105 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
106 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
109 void ScheduledActionPrepareTiles() override {
110 ThreadProxy::ScheduledActionPrepareTiles();
111 test_hooks_->ScheduledActionPrepareTiles();
114 void ScheduledActionInvalidateOutputSurface() override {
115 ThreadProxy::ScheduledActionInvalidateOutputSurface();
116 test_hooks_->ScheduledActionInvalidateOutputSurface();
119 void SendBeginMainFrameNotExpectedSoon() override {
120 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
121 test_hooks_->SendBeginMainFrameNotExpectedSoon();
124 ThreadProxyForTest(
125 TestHooks* test_hooks,
126 LayerTreeHost* host,
127 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
128 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
129 scoped_ptr<BeginFrameSource> external_begin_frame_source)
130 : ThreadProxy(host, main_task_runner,
131 impl_task_runner,
132 external_begin_frame_source.Pass()),
133 test_hooks_(test_hooks) {}
136 // Adapts ThreadProxy for test. Injects test hooks for testing.
137 class SingleThreadProxyForTest : public SingleThreadProxy {
138 public:
139 static scoped_ptr<Proxy> Create(
140 TestHooks* test_hooks,
141 LayerTreeHost* host,
142 LayerTreeHostSingleThreadClient* client,
143 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
144 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
145 return make_scoped_ptr(new SingleThreadProxyForTest(
146 test_hooks, host, client, main_task_runner,
147 external_begin_frame_source.Pass()));
150 ~SingleThreadProxyForTest() override {}
152 private:
153 TestHooks* test_hooks_;
155 void ScheduledActionSendBeginMainFrame() override {
156 test_hooks_->ScheduledActionWillSendBeginMainFrame();
157 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
158 test_hooks_->ScheduledActionSendBeginMainFrame();
161 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
162 DrawResult result =
163 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
164 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
165 return result;
168 void ScheduledActionAnimate() override {
169 SingleThreadProxy::ScheduledActionAnimate();
170 test_hooks_->ScheduledActionAnimate();
173 void ScheduledActionCommit() override {
174 SingleThreadProxy::ScheduledActionCommit();
175 test_hooks_->ScheduledActionCommit();
178 void ScheduledActionBeginOutputSurfaceCreation() override {
179 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
180 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
183 void ScheduledActionPrepareTiles() override {
184 SingleThreadProxy::ScheduledActionPrepareTiles();
185 test_hooks_->ScheduledActionPrepareTiles();
188 void ScheduledActionInvalidateOutputSurface() override {
189 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
190 test_hooks_->ScheduledActionInvalidateOutputSurface();
193 void SendBeginMainFrameNotExpectedSoon() override {
194 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
195 test_hooks_->SendBeginMainFrameNotExpectedSoon();
198 SingleThreadProxyForTest(
199 TestHooks* test_hooks,
200 LayerTreeHost* host,
201 LayerTreeHostSingleThreadClient* client,
202 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
203 scoped_ptr<BeginFrameSource> external_begin_frame_source)
204 : SingleThreadProxy(host, client, main_task_runner,
205 external_begin_frame_source.Pass()),
206 test_hooks_(test_hooks) {}
209 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
210 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
211 public:
212 static scoped_ptr<LayerTreeHostImplForTesting> Create(
213 TestHooks* test_hooks,
214 const LayerTreeSettings& settings,
215 LayerTreeHostImplClient* host_impl_client,
216 Proxy* proxy,
217 SharedBitmapManager* shared_bitmap_manager,
218 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
219 TaskGraphRunner* task_graph_runner,
220 RenderingStatsInstrumentation* stats_instrumentation) {
221 return make_scoped_ptr(new LayerTreeHostImplForTesting(
222 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
223 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
226 protected:
227 LayerTreeHostImplForTesting(
228 TestHooks* test_hooks,
229 const LayerTreeSettings& settings,
230 LayerTreeHostImplClient* host_impl_client,
231 Proxy* proxy,
232 SharedBitmapManager* shared_bitmap_manager,
233 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
234 TaskGraphRunner* task_graph_runner,
235 RenderingStatsInstrumentation* stats_instrumentation)
236 : LayerTreeHostImpl(settings,
237 host_impl_client,
238 proxy,
239 stats_instrumentation,
240 shared_bitmap_manager,
241 gpu_memory_buffer_manager,
242 task_graph_runner,
244 test_hooks_(test_hooks),
245 block_notify_ready_to_activate_for_testing_(false),
246 notify_ready_to_activate_was_blocked_(false) {}
248 void CreateResourceAndTileTaskWorkerPool(
249 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
250 scoped_ptr<ResourcePool>* resource_pool,
251 scoped_ptr<ResourcePool>* staging_resource_pool) override {
252 test_hooks_->CreateResourceAndTileTaskWorkerPool(
253 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
256 void WillBeginImplFrame(const BeginFrameArgs& args) override {
257 LayerTreeHostImpl::WillBeginImplFrame(args);
258 test_hooks_->WillBeginImplFrameOnThread(this, args);
261 void DidFinishImplFrame() override {
262 LayerTreeHostImpl::DidFinishImplFrame();
263 test_hooks_->DidFinishImplFrameOnThread(this);
266 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
267 LayerTreeHostImpl::BeginMainFrameAborted(reason);
268 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
271 void BeginCommit() override {
272 LayerTreeHostImpl::BeginCommit();
273 test_hooks_->BeginCommitOnThread(this);
276 void CommitComplete() override {
277 LayerTreeHostImpl::CommitComplete();
278 test_hooks_->CommitCompleteOnThread(this);
281 DrawResult PrepareToDraw(FrameData* frame) override {
282 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
283 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
286 void DrawLayers(FrameData* frame) override {
287 LayerTreeHostImpl::DrawLayers(frame);
288 test_hooks_->DrawLayersOnThread(this);
291 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
292 bool result = LayerTreeHostImpl::SwapBuffers(frame);
293 test_hooks_->SwapBuffersOnThread(this, result);
294 return result;
297 void DidSwapBuffersComplete() override {
298 LayerTreeHostImpl::DidSwapBuffersComplete();
299 test_hooks_->SwapBuffersCompleteOnThread(this);
302 void ReclaimResources(const CompositorFrameAck* ack) override {
303 LayerTreeHostImpl::ReclaimResources(ack);
306 void NotifyReadyToActivate() override {
307 if (block_notify_ready_to_activate_for_testing_) {
308 notify_ready_to_activate_was_blocked_ = true;
309 } else {
310 LayerTreeHostImpl::NotifyReadyToActivate();
311 test_hooks_->NotifyReadyToActivateOnThread(this);
315 void NotifyReadyToDraw() override {
316 LayerTreeHostImpl::NotifyReadyToDraw();
317 test_hooks_->NotifyReadyToDrawOnThread(this);
320 void NotifyAllTileTasksCompleted() override {
321 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
322 test_hooks_->NotifyAllTileTasksCompleted(this);
325 void BlockNotifyReadyToActivateForTesting(bool block) override {
326 CHECK(proxy()->ImplThreadTaskRunner())
327 << "Not supported for single-threaded mode.";
328 block_notify_ready_to_activate_for_testing_ = block;
329 if (!block && notify_ready_to_activate_was_blocked_) {
330 NotifyReadyToActivate();
331 notify_ready_to_activate_was_blocked_ = false;
335 void ActivateSyncTree() override {
336 test_hooks_->WillActivateTreeOnThread(this);
337 LayerTreeHostImpl::ActivateSyncTree();
338 DCHECK(!pending_tree());
339 test_hooks_->DidActivateTreeOnThread(this);
342 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
343 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
344 test_hooks_->InitializedRendererOnThread(this, success);
345 return success;
348 void SetVisible(bool visible) override {
349 LayerTreeHostImpl::SetVisible(visible);
350 test_hooks_->DidSetVisibleOnImplTree(this, visible);
353 void AnimateLayers(base::TimeTicks monotonic_time) override {
354 test_hooks_->WillAnimateLayers(this, monotonic_time);
355 LayerTreeHostImpl::AnimateLayers(monotonic_time);
356 test_hooks_->AnimateLayers(this, monotonic_time);
359 void UpdateAnimationState(bool start_ready_animations) override {
360 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
361 bool has_unfinished_animation = false;
362 AnimationRegistrar* registrar =
363 animation_registrar() ? animation_registrar()
364 : animation_host()->animation_registrar();
365 for (const auto& it :
366 registrar->active_animation_controllers_for_testing()) {
367 if (it.second->HasActiveAnimation()) {
368 has_unfinished_animation = true;
369 break;
372 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
375 void NotifyTileStateChanged(const Tile* tile) override {
376 LayerTreeHostImpl::NotifyTileStateChanged(tile);
377 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
380 private:
381 TestHooks* test_hooks_;
382 bool block_notify_ready_to_activate_for_testing_;
383 bool notify_ready_to_activate_was_blocked_;
386 // Implementation of LayerTreeHost callback interface.
387 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
388 public LayerTreeHostSingleThreadClient {
389 public:
390 static scoped_ptr<LayerTreeHostClientForTesting> Create(
391 TestHooks* test_hooks) {
392 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
394 ~LayerTreeHostClientForTesting() override {}
396 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
398 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
400 void BeginMainFrame(const BeginFrameArgs& args) override {
401 test_hooks_->BeginMainFrame(args);
404 void Layout() override { test_hooks_->Layout(); }
406 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
407 const gfx::Vector2dF& outer_delta,
408 const gfx::Vector2dF& elastic_overscroll_delta,
409 float page_scale,
410 float top_controls_delta) override {
411 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
412 elastic_overscroll_delta, page_scale,
413 top_controls_delta);
416 void RequestNewOutputSurface() override {
417 test_hooks_->RequestNewOutputSurface();
420 void DidInitializeOutputSurface() override {
421 test_hooks_->DidInitializeOutputSurface();
424 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
425 test_hooks_->SendBeginFramesToChildren(args);
428 void DidFailToInitializeOutputSurface() override {
429 test_hooks_->DidFailToInitializeOutputSurface();
430 RequestNewOutputSurface();
433 void WillCommit() override { test_hooks_->WillCommit(); }
435 void DidCommit() override { test_hooks_->DidCommit(); }
437 void DidCommitAndDrawFrame() override {
438 test_hooks_->DidCommitAndDrawFrame();
441 void DidCompleteSwapBuffers() override {
442 test_hooks_->DidCompleteSwapBuffers();
445 void DidPostSwapBuffers() override {}
446 void DidAbortSwapBuffers() override {}
447 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
448 void DidCompletePageScaleAnimation() override {}
449 void BeginMainFrameNotExpectedSoon() override {}
451 void RecordFrameTimingEvents(
452 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
453 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
454 override {}
456 private:
457 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
458 : test_hooks_(test_hooks) {}
460 TestHooks* test_hooks_;
463 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
464 class LayerTreeHostForTesting : public LayerTreeHost {
465 public:
466 static scoped_ptr<LayerTreeHostForTesting> Create(
467 TestHooks* test_hooks,
468 LayerTreeHostClientForTesting* client,
469 SharedBitmapManager* shared_bitmap_manager,
470 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
471 TaskGraphRunner* task_graph_runner,
472 const LayerTreeSettings& settings,
473 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
474 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
475 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
476 LayerTreeHost::InitParams params;
477 params.client = client;
478 params.shared_bitmap_manager = shared_bitmap_manager;
479 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
480 params.task_graph_runner = task_graph_runner;
481 params.settings = &settings;
482 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
483 new LayerTreeHostForTesting(test_hooks, &params));
484 if (impl_task_runner.get()) {
485 layer_tree_host->InitializeForTesting(
486 ThreadProxyForTest::Create(test_hooks,
487 layer_tree_host.get(),
488 main_task_runner,
489 impl_task_runner,
490 external_begin_frame_source.Pass()));
491 } else {
492 layer_tree_host->InitializeForTesting(
493 SingleThreadProxyForTest::Create(
494 test_hooks,
495 layer_tree_host.get(),
496 client,
497 main_task_runner,
498 external_begin_frame_source.Pass()));
500 return layer_tree_host.Pass();
503 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
504 LayerTreeHostImplClient* host_impl_client) override {
505 return LayerTreeHostImplForTesting::Create(
506 test_hooks_, settings(), host_impl_client, proxy(),
507 shared_bitmap_manager(), gpu_memory_buffer_manager(),
508 task_graph_runner(), rendering_stats_instrumentation());
511 void SetNeedsCommit() override {
512 if (!test_started_)
513 return;
514 LayerTreeHost::SetNeedsCommit();
517 void set_test_started(bool started) { test_started_ = started; }
519 private:
520 LayerTreeHostForTesting(TestHooks* test_hooks,
521 LayerTreeHost::InitParams* params)
522 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
524 TestHooks* test_hooks_;
525 bool test_started_;
528 LayerTreeTest::LayerTreeTest()
529 : output_surface_(nullptr),
530 external_begin_frame_source_(nullptr),
531 beginning_(false),
532 end_when_begin_returns_(false),
533 timed_out_(false),
534 scheduled_(false),
535 started_(false),
536 ended_(false),
537 delegating_renderer_(false),
538 verify_property_trees_(true),
539 timeout_seconds_(0),
540 weak_factory_(this) {
541 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
543 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
544 // specified (for running in a debugger).
545 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
546 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
547 timeout_seconds_ = 5;
550 LayerTreeTest::~LayerTreeTest() {}
552 void LayerTreeTest::EndTest() {
553 if (ended_)
554 return;
555 ended_ = true;
557 // For the case where we EndTest during BeginTest(), set a flag to indicate
558 // that the test should end the second BeginTest regains control.
559 if (beginning_) {
560 end_when_begin_returns_ = true;
561 } else {
562 main_task_runner_->PostTask(
563 FROM_HERE,
564 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
568 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
569 main_task_runner_->PostDelayedTask(
570 FROM_HERE,
571 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
572 base::TimeDelta::FromMilliseconds(delay_milliseconds));
575 void LayerTreeTest::PostAddAnimationToMainThread(
576 Layer* layer_to_receive_animation) {
577 main_task_runner_->PostTask(
578 FROM_HERE,
579 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
580 base::Unretained(layer_to_receive_animation), 0.000004));
583 void LayerTreeTest::PostAddInstantAnimationToMainThread(
584 Layer* layer_to_receive_animation) {
585 main_task_runner_->PostTask(
586 FROM_HERE,
587 base::Bind(&LayerTreeTest::DispatchAddAnimation,
588 main_thread_weak_ptr_,
589 base::Unretained(layer_to_receive_animation),
590 0.0));
593 void LayerTreeTest::PostAddLongAnimationToMainThread(
594 Layer* layer_to_receive_animation) {
595 main_task_runner_->PostTask(
596 FROM_HERE,
597 base::Bind(&LayerTreeTest::DispatchAddAnimation,
598 main_thread_weak_ptr_,
599 base::Unretained(layer_to_receive_animation),
600 1.0));
603 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
604 AnimationPlayer* player_to_receive_animation) {
605 main_task_runner_->PostTask(
606 FROM_HERE,
607 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
608 main_thread_weak_ptr_,
609 base::Unretained(player_to_receive_animation), 0.000004));
612 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
613 AnimationPlayer* player_to_receive_animation) {
614 main_task_runner_->PostTask(
615 FROM_HERE,
616 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
617 main_thread_weak_ptr_,
618 base::Unretained(player_to_receive_animation), 0.0));
621 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
622 AnimationPlayer* player_to_receive_animation) {
623 main_task_runner_->PostTask(
624 FROM_HERE,
625 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
626 main_thread_weak_ptr_,
627 base::Unretained(player_to_receive_animation), 1.0));
630 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
631 main_task_runner_->PostTask(
632 FROM_HERE,
633 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
634 main_thread_weak_ptr_, defer_commits));
637 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
638 main_task_runner_->PostTask(FROM_HERE,
639 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
640 main_thread_weak_ptr_));
643 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
644 main_task_runner_->PostTask(
645 FROM_HERE,
646 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
647 main_thread_weak_ptr_));
650 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
651 main_task_runner_->PostTask(FROM_HERE,
652 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
653 main_thread_weak_ptr_));
656 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
657 const gfx::Rect& damage_rect) {
658 main_task_runner_->PostTask(
659 FROM_HERE,
660 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
661 main_thread_weak_ptr_,
662 damage_rect));
665 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
666 main_task_runner_->PostTask(
667 FROM_HERE,
668 base::Bind(
669 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
672 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
673 main_task_runner_->PostTask(
674 FROM_HERE,
675 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
676 main_thread_weak_ptr_));
679 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
680 main_task_runner_->PostTask(
681 FROM_HERE,
682 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
683 main_thread_weak_ptr_));
686 void LayerTreeTest::WillBeginTest() {
687 layer_tree_host_->SetLayerTreeHostClientReady();
690 void LayerTreeTest::DoBeginTest() {
691 client_ = LayerTreeHostClientForTesting::Create(this);
693 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
694 if (settings_.use_external_begin_frame_source) {
695 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
696 settings_.renderer_settings.refresh_rate));
697 external_begin_frame_source_ = external_begin_frame_source.get();
700 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
701 layer_tree_host_ = LayerTreeHostForTesting::Create(
702 this, client_.get(), shared_bitmap_manager_.get(),
703 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
704 base::ThreadTaskRunnerHandle::Get(),
705 impl_thread_ ? impl_thread_->task_runner() : NULL,
706 external_begin_frame_source.Pass());
707 ASSERT_TRUE(layer_tree_host_);
709 started_ = true;
710 beginning_ = true;
711 SetupTree();
712 WillBeginTest();
713 BeginTest();
714 beginning_ = false;
715 if (end_when_begin_returns_)
716 RealEndTest();
718 // Allow commits to happen once BeginTest() has had a chance to post tasks
719 // so that those tasks will happen before the first commit.
720 if (layer_tree_host_) {
721 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
722 ->set_test_started(true);
726 void LayerTreeTest::SetupTree() {
727 if (!layer_tree_host_->root_layer()) {
728 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
729 root_layer->SetBounds(gfx::Size(1, 1));
730 root_layer->SetIsDrawable(true);
731 layer_tree_host_->SetRootLayer(root_layer);
734 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
735 gfx::Size device_root_bounds = gfx::ToCeiledSize(
736 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
737 layer_tree_host_->SetViewportSize(device_root_bounds);
740 void LayerTreeTest::Timeout() {
741 timed_out_ = true;
742 EndTest();
745 void LayerTreeTest::RealEndTest() {
746 // TODO(mithro): Make this method only end when not inside an impl frame.
747 if (layer_tree_host_ && !timed_out_ &&
748 proxy()->MainFrameWillHappenForTesting()) {
749 main_task_runner_->PostTask(
750 FROM_HERE,
751 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
752 return;
755 base::MessageLoop::current()->Quit();
758 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
759 double animation_duration) {
760 DCHECK(!proxy() || proxy()->IsMainThread());
762 if (layer_to_receive_animation) {
763 AddOpacityTransitionToLayer(
764 layer_to_receive_animation, animation_duration, 0, 0.5, true);
768 void LayerTreeTest::DispatchAddAnimationToPlayer(
769 AnimationPlayer* player_to_receive_animation,
770 double animation_duration) {
771 DCHECK(!proxy() || proxy()->IsMainThread());
773 if (player_to_receive_animation) {
774 AddOpacityTransitionToPlayer(player_to_receive_animation,
775 animation_duration, 0, 0.5, true);
779 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
780 DCHECK(!proxy() || proxy()->IsMainThread());
782 if (layer_tree_host_)
783 layer_tree_host_->SetDeferCommits(defer_commits);
786 void LayerTreeTest::DispatchSetNeedsCommit() {
787 DCHECK(!proxy() || proxy()->IsMainThread());
789 if (layer_tree_host_)
790 layer_tree_host_->SetNeedsCommit();
793 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
794 DCHECK(!proxy() || proxy()->IsMainThread());
796 if (layer_tree_host_)
797 layer_tree_host_->SetNeedsUpdateLayers();
800 void LayerTreeTest::DispatchSetNeedsRedraw() {
801 DCHECK(!proxy() || proxy()->IsMainThread());
803 if (layer_tree_host_)
804 layer_tree_host_->SetNeedsRedraw();
807 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
808 DCHECK(!proxy() || proxy()->IsMainThread());
810 if (layer_tree_host_)
811 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
814 void LayerTreeTest::DispatchSetVisible(bool visible) {
815 DCHECK(!proxy() || proxy()->IsMainThread());
816 if (layer_tree_host_)
817 layer_tree_host_->SetVisible(visible);
820 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
821 DCHECK(!proxy() || proxy()->IsMainThread());
823 if (layer_tree_host_)
824 layer_tree_host_->SetNextCommitForcesRedraw();
827 void LayerTreeTest::DispatchCompositeImmediately() {
828 DCHECK(!proxy() || proxy()->IsMainThread());
829 if (layer_tree_host_)
830 layer_tree_host_->Composite(base::TimeTicks::Now());
833 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
834 if (threaded) {
835 impl_thread_.reset(new base::Thread("Compositor"));
836 ASSERT_TRUE(impl_thread_->Start());
839 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
841 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
842 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
843 task_graph_runner_.reset(new TestTaskGraphRunner);
845 delegating_renderer_ = delegating_renderer;
847 // Spend less time waiting for BeginFrame because the output is
848 // mocked out.
849 settings_.renderer_settings.refresh_rate = 200.0;
850 settings_.background_animation_rate = 200.0;
851 settings_.verify_property_trees = verify_property_trees_;
852 InitializeSettings(&settings_);
853 InitializeLayerSettings(&layer_settings_);
855 main_task_runner_->PostTask(
856 FROM_HERE,
857 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
859 if (timeout_seconds_) {
860 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
861 main_task_runner_->PostDelayedTask(
862 FROM_HERE,
863 timeout_.callback(),
864 base::TimeDelta::FromSeconds(timeout_seconds_));
867 base::MessageLoop::current()->Run();
868 DestroyLayerTreeHost();
870 timeout_.Cancel();
872 ASSERT_FALSE(layer_tree_host_.get());
873 client_ = nullptr;
874 if (timed_out_) {
875 FAIL() << "Test timed out";
876 return;
878 AfterTest();
881 void LayerTreeTest::RequestNewOutputSurface() {
882 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
885 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
886 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
887 DCHECK_EQ(delegating_renderer_,
888 output_surface->capabilities().delegated_rendering);
889 output_surface_ = output_surface.get();
891 if (settings_.use_external_begin_frame_source) {
892 DCHECK(external_begin_frame_source_);
893 DCHECK(external_begin_frame_source_->is_ready());
895 return output_surface.Pass();
898 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
899 if (delegating_renderer_)
900 return FakeOutputSurface::CreateDelegating3d();
901 else
902 return FakeOutputSurface::Create3d();
905 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
906 return static_cast<TestContextProvider*>(output_surface_->context_provider())
907 ->TestContext3d();
910 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
911 const {
912 if (impl->pending_tree())
913 return impl->pending_tree()->source_frame_number();
914 if (impl->active_tree())
915 return impl->active_tree()->source_frame_number();
916 // Source frames start at 0, so this is invalid.
917 return -1;
920 void LayerTreeTest::DestroyLayerTreeHost() {
921 if (layer_tree_host_ && layer_tree_host_->root_layer())
922 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
923 layer_tree_host_ = nullptr;
926 LayerTreeHost* LayerTreeTest::layer_tree_host() {
927 // We check for a null proxy here as we sometimes ask for the layer tree host
928 // when the proxy does not exist, often for checking settings after a test has
929 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
930 // elsewhere in this file for other examples.
931 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
932 return layer_tree_host_.get();
935 } // namespace cc