MediaCodecPlayer implementation - stage 6 (respect audio permission)
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blobc87c36b780ba0f07a8008cf00a459ca8e131013d
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 void CreateVirtualViewportLayers(Layer* root_layer,
41 scoped_refptr<Layer> outer_scroll_layer,
42 const gfx::Size& inner_bounds,
43 const gfx::Size& outer_bounds,
44 LayerTreeHost* host,
45 const LayerSettings& layer_settings) {
46 scoped_refptr<Layer> inner_viewport_container_layer =
47 Layer::Create(layer_settings);
48 scoped_refptr<Layer> inner_viewport_scroll_layer =
49 Layer::Create(layer_settings);
50 scoped_refptr<Layer> outer_viewport_container_layer =
51 Layer::Create(layer_settings);
52 scoped_refptr<Layer> page_scale_layer = Layer::Create(layer_settings);
54 root_layer->AddChild(inner_viewport_container_layer);
55 inner_viewport_container_layer->AddChild(page_scale_layer);
56 page_scale_layer->AddChild(inner_viewport_scroll_layer);
57 inner_viewport_scroll_layer->AddChild(outer_viewport_container_layer);
58 outer_viewport_container_layer->AddChild(outer_scroll_layer);
60 inner_viewport_scroll_layer->SetScrollClipLayerId(
61 inner_viewport_container_layer->id());
62 outer_scroll_layer->SetScrollClipLayerId(
63 outer_viewport_container_layer->id());
65 inner_viewport_container_layer->SetBounds(inner_bounds);
66 inner_viewport_scroll_layer->SetBounds(outer_bounds);
67 outer_viewport_container_layer->SetBounds(outer_bounds);
69 inner_viewport_scroll_layer->SetIsContainerForFixedPositionLayers(true);
70 outer_scroll_layer->SetIsContainerForFixedPositionLayers(true);
71 host->RegisterViewportLayers(NULL,
72 root_layer,
73 inner_viewport_scroll_layer,
74 outer_scroll_layer);
77 void CreateVirtualViewportLayers(Layer* root_layer,
78 const gfx::Size& inner_bounds,
79 const gfx::Size& outer_bounds,
80 const gfx::Size& scroll_bounds,
81 LayerTreeHost* host,
82 const LayerSettings& layer_settings) {
83 scoped_refptr<Layer> outer_viewport_scroll_layer =
84 Layer::Create(layer_settings);
86 outer_viewport_scroll_layer->SetBounds(scroll_bounds);
87 outer_viewport_scroll_layer->SetIsDrawable(true);
88 CreateVirtualViewportLayers(root_layer,
89 outer_viewport_scroll_layer,
90 inner_bounds,
91 outer_bounds,
92 host,
93 layer_settings);
96 TestHooks::TestHooks() {}
98 TestHooks::~TestHooks() {}
100 DrawResult TestHooks::PrepareToDrawOnThread(
101 LayerTreeHostImpl* host_impl,
102 LayerTreeHostImpl::FrameData* frame_data,
103 DrawResult draw_result) {
104 return draw_result;
107 void TestHooks::CreateResourceAndTileTaskWorkerPool(
108 LayerTreeHostImpl* host_impl,
109 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
110 scoped_ptr<ResourcePool>* resource_pool) {
111 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
112 tile_task_worker_pool, resource_pool);
115 // Adapts ThreadProxy for test. Injects test hooks for testing.
116 class ThreadProxyForTest : public ThreadProxy {
117 public:
118 static scoped_ptr<Proxy> Create(
119 TestHooks* test_hooks,
120 LayerTreeHost* host,
121 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
122 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
123 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
124 return make_scoped_ptr(new ThreadProxyForTest(
125 test_hooks,
126 host,
127 main_task_runner,
128 impl_task_runner,
129 external_begin_frame_source.Pass()));
132 ~ThreadProxyForTest() override {}
134 private:
135 TestHooks* test_hooks_;
137 void SetNeedsUpdateLayers() override {
138 ThreadProxy::SetNeedsUpdateLayers();
139 test_hooks_->DidSetNeedsUpdateLayers();
142 void ScheduledActionSendBeginMainFrame() override {
143 test_hooks_->ScheduledActionWillSendBeginMainFrame();
144 ThreadProxy::ScheduledActionSendBeginMainFrame();
145 test_hooks_->ScheduledActionSendBeginMainFrame();
148 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
149 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
150 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
151 return result;
154 void ScheduledActionAnimate() override {
155 ThreadProxy::ScheduledActionAnimate();
156 test_hooks_->ScheduledActionAnimate();
159 void ScheduledActionCommit() override {
160 ThreadProxy::ScheduledActionCommit();
161 test_hooks_->ScheduledActionCommit();
164 void ScheduledActionBeginOutputSurfaceCreation() override {
165 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
166 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
169 void ScheduledActionPrepareTiles() override {
170 ThreadProxy::ScheduledActionPrepareTiles();
171 test_hooks_->ScheduledActionPrepareTiles();
174 void ScheduledActionInvalidateOutputSurface() override {
175 ThreadProxy::ScheduledActionInvalidateOutputSurface();
176 test_hooks_->ScheduledActionInvalidateOutputSurface();
179 void SendBeginMainFrameNotExpectedSoon() override {
180 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
181 test_hooks_->SendBeginMainFrameNotExpectedSoon();
184 ThreadProxyForTest(
185 TestHooks* test_hooks,
186 LayerTreeHost* host,
187 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
188 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
189 scoped_ptr<BeginFrameSource> external_begin_frame_source)
190 : ThreadProxy(host, main_task_runner,
191 impl_task_runner,
192 external_begin_frame_source.Pass()),
193 test_hooks_(test_hooks) {}
196 // Adapts ThreadProxy for test. Injects test hooks for testing.
197 class SingleThreadProxyForTest : public SingleThreadProxy {
198 public:
199 static scoped_ptr<Proxy> Create(
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 return make_scoped_ptr(new SingleThreadProxyForTest(
206 test_hooks, host, client, main_task_runner,
207 external_begin_frame_source.Pass()));
210 ~SingleThreadProxyForTest() override {}
212 private:
213 TestHooks* test_hooks_;
215 void ScheduledActionSendBeginMainFrame() override {
216 test_hooks_->ScheduledActionWillSendBeginMainFrame();
217 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
218 test_hooks_->ScheduledActionSendBeginMainFrame();
221 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
222 DrawResult result =
223 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
224 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
225 return result;
228 void ScheduledActionAnimate() override {
229 SingleThreadProxy::ScheduledActionAnimate();
230 test_hooks_->ScheduledActionAnimate();
233 void ScheduledActionCommit() override {
234 SingleThreadProxy::ScheduledActionCommit();
235 test_hooks_->ScheduledActionCommit();
238 void ScheduledActionBeginOutputSurfaceCreation() override {
239 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
240 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
243 void ScheduledActionPrepareTiles() override {
244 SingleThreadProxy::ScheduledActionPrepareTiles();
245 test_hooks_->ScheduledActionPrepareTiles();
248 void ScheduledActionInvalidateOutputSurface() override {
249 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
250 test_hooks_->ScheduledActionInvalidateOutputSurface();
253 void SendBeginMainFrameNotExpectedSoon() override {
254 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
255 test_hooks_->SendBeginMainFrameNotExpectedSoon();
258 SingleThreadProxyForTest(
259 TestHooks* test_hooks,
260 LayerTreeHost* host,
261 LayerTreeHostSingleThreadClient* client,
262 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
263 scoped_ptr<BeginFrameSource> external_begin_frame_source)
264 : SingleThreadProxy(host, client, main_task_runner,
265 external_begin_frame_source.Pass()),
266 test_hooks_(test_hooks) {}
269 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
270 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
271 public:
272 static scoped_ptr<LayerTreeHostImplForTesting> Create(
273 TestHooks* test_hooks,
274 const LayerTreeSettings& settings,
275 LayerTreeHostImplClient* host_impl_client,
276 Proxy* proxy,
277 SharedBitmapManager* shared_bitmap_manager,
278 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
279 TaskGraphRunner* task_graph_runner,
280 RenderingStatsInstrumentation* stats_instrumentation) {
281 return make_scoped_ptr(new LayerTreeHostImplForTesting(
282 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
283 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
286 protected:
287 LayerTreeHostImplForTesting(
288 TestHooks* test_hooks,
289 const LayerTreeSettings& settings,
290 LayerTreeHostImplClient* host_impl_client,
291 Proxy* proxy,
292 SharedBitmapManager* shared_bitmap_manager,
293 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
294 TaskGraphRunner* task_graph_runner,
295 RenderingStatsInstrumentation* stats_instrumentation)
296 : LayerTreeHostImpl(settings,
297 host_impl_client,
298 proxy,
299 stats_instrumentation,
300 shared_bitmap_manager,
301 gpu_memory_buffer_manager,
302 task_graph_runner,
304 test_hooks_(test_hooks),
305 block_notify_ready_to_activate_for_testing_(false),
306 notify_ready_to_activate_was_blocked_(false) {}
308 void CreateResourceAndTileTaskWorkerPool(
309 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
310 scoped_ptr<ResourcePool>* resource_pool) override {
311 test_hooks_->CreateResourceAndTileTaskWorkerPool(
312 this, tile_task_worker_pool, resource_pool);
315 void WillBeginImplFrame(const BeginFrameArgs& args) override {
316 LayerTreeHostImpl::WillBeginImplFrame(args);
317 test_hooks_->WillBeginImplFrameOnThread(this, args);
320 void DidFinishImplFrame() override {
321 LayerTreeHostImpl::DidFinishImplFrame();
322 test_hooks_->DidFinishImplFrameOnThread(this);
325 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
326 LayerTreeHostImpl::BeginMainFrameAborted(reason);
327 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
330 void BeginCommit() override {
331 LayerTreeHostImpl::BeginCommit();
332 test_hooks_->BeginCommitOnThread(this);
335 void CommitComplete() override {
336 test_hooks_->WillCommitCompleteOnThread(this);
337 LayerTreeHostImpl::CommitComplete();
338 test_hooks_->CommitCompleteOnThread(this);
341 bool PrepareTiles() override {
342 test_hooks_->WillPrepareTiles(this);
343 return LayerTreeHostImpl::PrepareTiles();
346 DrawResult PrepareToDraw(FrameData* frame) override {
347 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
348 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
351 void DrawLayers(FrameData* frame) override {
352 LayerTreeHostImpl::DrawLayers(frame);
353 test_hooks_->DrawLayersOnThread(this);
356 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
357 bool result = LayerTreeHostImpl::SwapBuffers(frame);
358 test_hooks_->SwapBuffersOnThread(this, result);
359 return result;
362 void DidSwapBuffersComplete() override {
363 LayerTreeHostImpl::DidSwapBuffersComplete();
364 test_hooks_->SwapBuffersCompleteOnThread(this);
367 void ReclaimResources(const CompositorFrameAck* ack) override {
368 LayerTreeHostImpl::ReclaimResources(ack);
371 void NotifyReadyToActivate() override {
372 if (block_notify_ready_to_activate_for_testing_) {
373 notify_ready_to_activate_was_blocked_ = true;
374 } else {
375 LayerTreeHostImpl::NotifyReadyToActivate();
376 test_hooks_->NotifyReadyToActivateOnThread(this);
380 void NotifyReadyToDraw() override {
381 LayerTreeHostImpl::NotifyReadyToDraw();
382 test_hooks_->NotifyReadyToDrawOnThread(this);
385 void NotifyAllTileTasksCompleted() override {
386 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
387 test_hooks_->NotifyAllTileTasksCompleted(this);
390 void BlockNotifyReadyToActivateForTesting(bool block) override {
391 CHECK(proxy()->ImplThreadTaskRunner())
392 << "Not supported for single-threaded mode.";
393 block_notify_ready_to_activate_for_testing_ = block;
394 if (!block && notify_ready_to_activate_was_blocked_) {
395 NotifyReadyToActivate();
396 notify_ready_to_activate_was_blocked_ = false;
400 void ActivateSyncTree() override {
401 test_hooks_->WillActivateTreeOnThread(this);
402 LayerTreeHostImpl::ActivateSyncTree();
403 DCHECK(!pending_tree());
404 test_hooks_->DidActivateTreeOnThread(this);
407 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
408 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
409 test_hooks_->InitializedRendererOnThread(this, success);
410 return success;
413 void SetVisible(bool visible) override {
414 LayerTreeHostImpl::SetVisible(visible);
415 test_hooks_->DidSetVisibleOnImplTree(this, visible);
418 void AnimateLayers(base::TimeTicks monotonic_time) override {
419 test_hooks_->WillAnimateLayers(this, monotonic_time);
420 LayerTreeHostImpl::AnimateLayers(monotonic_time);
421 test_hooks_->AnimateLayers(this, monotonic_time);
424 void UpdateAnimationState(bool start_ready_animations) override {
425 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
426 bool has_unfinished_animation = false;
427 AnimationRegistrar* registrar =
428 animation_registrar() ? animation_registrar()
429 : animation_host()->animation_registrar();
430 for (const auto& it :
431 registrar->active_animation_controllers_for_testing()) {
432 if (it.second->HasActiveAnimation()) {
433 has_unfinished_animation = true;
434 break;
437 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
440 void NotifyTileStateChanged(const Tile* tile) override {
441 LayerTreeHostImpl::NotifyTileStateChanged(tile);
442 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
445 private:
446 TestHooks* test_hooks_;
447 bool block_notify_ready_to_activate_for_testing_;
448 bool notify_ready_to_activate_was_blocked_;
451 // Implementation of LayerTreeHost callback interface.
452 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
453 public LayerTreeHostSingleThreadClient {
454 public:
455 static scoped_ptr<LayerTreeHostClientForTesting> Create(
456 TestHooks* test_hooks) {
457 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
459 ~LayerTreeHostClientForTesting() override {}
461 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
463 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
465 void BeginMainFrame(const BeginFrameArgs& args) override {
466 test_hooks_->BeginMainFrame(args);
469 void Layout() override { test_hooks_->Layout(); }
471 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
472 const gfx::Vector2dF& outer_delta,
473 const gfx::Vector2dF& elastic_overscroll_delta,
474 float page_scale,
475 float top_controls_delta) override {
476 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
477 elastic_overscroll_delta, page_scale,
478 top_controls_delta);
481 void RequestNewOutputSurface() override {
482 test_hooks_->RequestNewOutputSurface();
485 void DidInitializeOutputSurface() override {
486 test_hooks_->DidInitializeOutputSurface();
489 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
490 test_hooks_->SendBeginFramesToChildren(args);
493 void DidFailToInitializeOutputSurface() override {
494 test_hooks_->DidFailToInitializeOutputSurface();
495 RequestNewOutputSurface();
498 void WillCommit() override { test_hooks_->WillCommit(); }
500 void DidCommit() override { test_hooks_->DidCommit(); }
502 void DidCommitAndDrawFrame() override {
503 test_hooks_->DidCommitAndDrawFrame();
506 void DidCompleteSwapBuffers() override {
507 test_hooks_->DidCompleteSwapBuffers();
510 void DidPostSwapBuffers() override {}
511 void DidAbortSwapBuffers() override {}
512 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
513 void DidCompletePageScaleAnimation() override {}
514 void BeginMainFrameNotExpectedSoon() override {}
516 void RecordFrameTimingEvents(
517 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
518 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
519 override {}
521 private:
522 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
523 : test_hooks_(test_hooks) {}
525 TestHooks* test_hooks_;
528 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
529 class LayerTreeHostForTesting : public LayerTreeHost {
530 public:
531 static scoped_ptr<LayerTreeHostForTesting> Create(
532 TestHooks* test_hooks,
533 LayerTreeHostClientForTesting* client,
534 SharedBitmapManager* shared_bitmap_manager,
535 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
536 TaskGraphRunner* task_graph_runner,
537 const LayerTreeSettings& settings,
538 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
539 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
540 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
541 LayerTreeHost::InitParams params;
542 params.client = client;
543 params.shared_bitmap_manager = shared_bitmap_manager;
544 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
545 params.task_graph_runner = task_graph_runner;
546 params.settings = &settings;
547 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
548 new LayerTreeHostForTesting(test_hooks, &params));
549 if (impl_task_runner.get()) {
550 layer_tree_host->InitializeForTesting(
551 ThreadProxyForTest::Create(test_hooks,
552 layer_tree_host.get(),
553 main_task_runner,
554 impl_task_runner,
555 external_begin_frame_source.Pass()));
556 } else {
557 layer_tree_host->InitializeForTesting(
558 SingleThreadProxyForTest::Create(
559 test_hooks,
560 layer_tree_host.get(),
561 client,
562 main_task_runner,
563 external_begin_frame_source.Pass()));
565 return layer_tree_host.Pass();
568 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
569 LayerTreeHostImplClient* host_impl_client) override {
570 return LayerTreeHostImplForTesting::Create(
571 test_hooks_, settings(), host_impl_client, proxy(),
572 shared_bitmap_manager(), gpu_memory_buffer_manager(),
573 task_graph_runner(), rendering_stats_instrumentation());
576 void SetNeedsCommit() override {
577 if (!test_started_)
578 return;
579 LayerTreeHost::SetNeedsCommit();
582 void SetNeedsUpdateLayers() override {
583 if (!test_started_)
584 return;
585 LayerTreeHost::SetNeedsUpdateLayers();
588 void set_test_started(bool started) { test_started_ = started; }
590 private:
591 LayerTreeHostForTesting(TestHooks* test_hooks,
592 LayerTreeHost::InitParams* params)
593 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
595 TestHooks* test_hooks_;
596 bool test_started_;
599 LayerTreeTest::LayerTreeTest()
600 : output_surface_(nullptr),
601 external_begin_frame_source_(nullptr),
602 beginning_(false),
603 end_when_begin_returns_(false),
604 timed_out_(false),
605 scheduled_(false),
606 started_(false),
607 ended_(false),
608 delegating_renderer_(false),
609 verify_property_trees_(true),
610 timeout_seconds_(0),
611 weak_factory_(this) {
612 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
614 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
615 // specified (for running in a debugger).
616 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
617 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
618 timeout_seconds_ = 5;
621 LayerTreeTest::~LayerTreeTest() {}
623 void LayerTreeTest::EndTest() {
624 if (ended_)
625 return;
626 ended_ = true;
628 // For the case where we EndTest during BeginTest(), set a flag to indicate
629 // that the test should end the second BeginTest regains control.
630 if (beginning_) {
631 end_when_begin_returns_ = true;
632 } else {
633 main_task_runner_->PostTask(
634 FROM_HERE,
635 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
639 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
640 main_task_runner_->PostDelayedTask(
641 FROM_HERE,
642 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
643 base::TimeDelta::FromMilliseconds(delay_milliseconds));
646 void LayerTreeTest::PostAddAnimationToMainThread(
647 Layer* layer_to_receive_animation) {
648 main_task_runner_->PostTask(
649 FROM_HERE,
650 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
651 base::Unretained(layer_to_receive_animation), 0.000004));
654 void LayerTreeTest::PostAddInstantAnimationToMainThread(
655 Layer* layer_to_receive_animation) {
656 main_task_runner_->PostTask(
657 FROM_HERE,
658 base::Bind(&LayerTreeTest::DispatchAddAnimation,
659 main_thread_weak_ptr_,
660 base::Unretained(layer_to_receive_animation),
661 0.0));
664 void LayerTreeTest::PostAddLongAnimationToMainThread(
665 Layer* layer_to_receive_animation) {
666 main_task_runner_->PostTask(
667 FROM_HERE,
668 base::Bind(&LayerTreeTest::DispatchAddAnimation,
669 main_thread_weak_ptr_,
670 base::Unretained(layer_to_receive_animation),
671 1.0));
674 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
675 AnimationPlayer* player_to_receive_animation) {
676 main_task_runner_->PostTask(
677 FROM_HERE,
678 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
679 main_thread_weak_ptr_,
680 base::Unretained(player_to_receive_animation), 0.000004));
683 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
684 AnimationPlayer* player_to_receive_animation) {
685 main_task_runner_->PostTask(
686 FROM_HERE,
687 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
688 main_thread_weak_ptr_,
689 base::Unretained(player_to_receive_animation), 0.0));
692 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
693 AnimationPlayer* player_to_receive_animation) {
694 main_task_runner_->PostTask(
695 FROM_HERE,
696 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
697 main_thread_weak_ptr_,
698 base::Unretained(player_to_receive_animation), 1.0));
701 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
702 main_task_runner_->PostTask(
703 FROM_HERE,
704 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
705 main_thread_weak_ptr_, defer_commits));
708 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
709 main_task_runner_->PostTask(FROM_HERE,
710 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
711 main_thread_weak_ptr_));
714 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
715 main_task_runner_->PostTask(
716 FROM_HERE,
717 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
718 main_thread_weak_ptr_));
721 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
722 main_task_runner_->PostTask(FROM_HERE,
723 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
724 main_thread_weak_ptr_));
727 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
728 const gfx::Rect& damage_rect) {
729 main_task_runner_->PostTask(
730 FROM_HERE,
731 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
732 main_thread_weak_ptr_,
733 damage_rect));
736 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
737 main_task_runner_->PostTask(
738 FROM_HERE,
739 base::Bind(
740 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
743 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
744 main_task_runner_->PostTask(
745 FROM_HERE,
746 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
747 main_thread_weak_ptr_));
750 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
751 main_task_runner_->PostTask(
752 FROM_HERE,
753 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
754 main_thread_weak_ptr_));
757 void LayerTreeTest::WillBeginTest() {
758 layer_tree_host_->SetLayerTreeHostClientReady();
761 void LayerTreeTest::DoBeginTest() {
762 client_ = LayerTreeHostClientForTesting::Create(this);
764 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
765 if (settings_.use_external_begin_frame_source) {
766 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
767 settings_.renderer_settings.refresh_rate));
768 external_begin_frame_source_ = external_begin_frame_source.get();
771 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
772 layer_tree_host_ = LayerTreeHostForTesting::Create(
773 this, client_.get(), shared_bitmap_manager_.get(),
774 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
775 base::ThreadTaskRunnerHandle::Get(),
776 impl_thread_ ? impl_thread_->task_runner() : NULL,
777 external_begin_frame_source.Pass());
778 ASSERT_TRUE(layer_tree_host_);
780 started_ = true;
781 beginning_ = true;
782 SetupTree();
783 WillBeginTest();
784 BeginTest();
785 beginning_ = false;
786 if (end_when_begin_returns_)
787 RealEndTest();
789 // Allow commits to happen once BeginTest() has had a chance to post tasks
790 // so that those tasks will happen before the first commit.
791 if (layer_tree_host_) {
792 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
793 ->set_test_started(true);
797 void LayerTreeTest::SetupTree() {
798 if (!layer_tree_host_->root_layer()) {
799 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
800 root_layer->SetBounds(gfx::Size(1, 1));
801 root_layer->SetIsDrawable(true);
802 layer_tree_host_->SetRootLayer(root_layer);
805 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
806 gfx::Size device_root_bounds = gfx::ToCeiledSize(
807 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
808 layer_tree_host_->SetViewportSize(device_root_bounds);
811 void LayerTreeTest::Timeout() {
812 timed_out_ = true;
813 EndTest();
816 void LayerTreeTest::RealEndTest() {
817 // TODO(mithro): Make this method only end when not inside an impl frame.
818 if (layer_tree_host_ && !timed_out_ &&
819 proxy()->MainFrameWillHappenForTesting()) {
820 main_task_runner_->PostTask(
821 FROM_HERE,
822 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
823 return;
826 base::MessageLoop::current()->Quit();
829 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
830 double animation_duration) {
831 DCHECK(!proxy() || proxy()->IsMainThread());
833 if (layer_to_receive_animation) {
834 AddOpacityTransitionToLayer(
835 layer_to_receive_animation, animation_duration, 0, 0.5, true);
839 void LayerTreeTest::DispatchAddAnimationToPlayer(
840 AnimationPlayer* player_to_receive_animation,
841 double animation_duration) {
842 DCHECK(!proxy() || proxy()->IsMainThread());
844 if (player_to_receive_animation) {
845 AddOpacityTransitionToPlayer(player_to_receive_animation,
846 animation_duration, 0, 0.5, true);
850 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
851 DCHECK(!proxy() || proxy()->IsMainThread());
853 if (layer_tree_host_)
854 layer_tree_host_->SetDeferCommits(defer_commits);
857 void LayerTreeTest::DispatchSetNeedsCommit() {
858 DCHECK(!proxy() || proxy()->IsMainThread());
860 if (layer_tree_host_)
861 layer_tree_host_->SetNeedsCommit();
864 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
865 DCHECK(!proxy() || proxy()->IsMainThread());
867 if (layer_tree_host_)
868 layer_tree_host_->SetNeedsUpdateLayers();
871 void LayerTreeTest::DispatchSetNeedsRedraw() {
872 DCHECK(!proxy() || proxy()->IsMainThread());
874 if (layer_tree_host_)
875 layer_tree_host_->SetNeedsRedraw();
878 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
879 DCHECK(!proxy() || proxy()->IsMainThread());
881 if (layer_tree_host_)
882 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
885 void LayerTreeTest::DispatchSetVisible(bool visible) {
886 DCHECK(!proxy() || proxy()->IsMainThread());
887 if (layer_tree_host_)
888 layer_tree_host_->SetVisible(visible);
891 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
892 DCHECK(!proxy() || proxy()->IsMainThread());
894 if (layer_tree_host_)
895 layer_tree_host_->SetNextCommitForcesRedraw();
898 void LayerTreeTest::DispatchCompositeImmediately() {
899 DCHECK(!proxy() || proxy()->IsMainThread());
900 if (layer_tree_host_)
901 layer_tree_host_->Composite(base::TimeTicks::Now());
904 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
905 if (threaded) {
906 impl_thread_.reset(new base::Thread("Compositor"));
907 ASSERT_TRUE(impl_thread_->Start());
910 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
912 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
913 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
914 task_graph_runner_.reset(new TestTaskGraphRunner);
916 delegating_renderer_ = delegating_renderer;
918 // Spend less time waiting for BeginFrame because the output is
919 // mocked out.
920 settings_.renderer_settings.refresh_rate = 200.0;
921 settings_.background_animation_rate = 200.0;
922 settings_.verify_property_trees = verify_property_trees_;
923 InitializeSettings(&settings_);
924 InitializeLayerSettings(&layer_settings_);
926 main_task_runner_->PostTask(
927 FROM_HERE,
928 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
930 if (timeout_seconds_) {
931 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
932 main_task_runner_->PostDelayedTask(
933 FROM_HERE,
934 timeout_.callback(),
935 base::TimeDelta::FromSeconds(timeout_seconds_));
938 base::MessageLoop::current()->Run();
939 DestroyLayerTreeHost();
941 timeout_.Cancel();
943 ASSERT_FALSE(layer_tree_host_.get());
944 client_ = nullptr;
945 if (timed_out_) {
946 FAIL() << "Test timed out";
947 return;
949 AfterTest();
952 void LayerTreeTest::RequestNewOutputSurface() {
953 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
956 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
957 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
958 DCHECK_EQ(delegating_renderer_,
959 output_surface->capabilities().delegated_rendering);
960 output_surface_ = output_surface.get();
962 if (settings_.use_external_begin_frame_source) {
963 DCHECK(external_begin_frame_source_);
964 DCHECK(external_begin_frame_source_->is_ready());
966 return output_surface.Pass();
969 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
970 if (delegating_renderer_)
971 return FakeOutputSurface::CreateDelegating3d();
972 else
973 return FakeOutputSurface::Create3d();
976 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
977 return static_cast<TestContextProvider*>(output_surface_->context_provider())
978 ->TestContext3d();
981 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
982 const {
983 if (impl->pending_tree())
984 return impl->pending_tree()->source_frame_number();
985 if (impl->active_tree())
986 return impl->active_tree()->source_frame_number();
987 // Source frames start at 0, so this is invalid.
988 return -1;
991 void LayerTreeTest::DestroyLayerTreeHost() {
992 if (layer_tree_host_ && layer_tree_host_->root_layer())
993 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
994 layer_tree_host_ = nullptr;
997 LayerTreeHost* LayerTreeTest::layer_tree_host() {
998 // We check for a null proxy here as we sometimes ask for the layer tree host
999 // when the proxy does not exist, often for checking settings after a test has
1000 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
1001 // elsewhere in this file for other examples.
1002 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
1003 return layer_tree_host_.get();
1006 } // namespace cc