Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob865ce595bebcd307a134d2516fd5d681f5dd921d
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 LayerTreeHostImpl::CommitComplete();
337 test_hooks_->CommitCompleteOnThread(this);
340 DrawResult PrepareToDraw(FrameData* frame) override {
341 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
342 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
345 void DrawLayers(FrameData* frame) override {
346 LayerTreeHostImpl::DrawLayers(frame);
347 test_hooks_->DrawLayersOnThread(this);
350 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
351 bool result = LayerTreeHostImpl::SwapBuffers(frame);
352 test_hooks_->SwapBuffersOnThread(this, result);
353 return result;
356 void DidSwapBuffersComplete() override {
357 LayerTreeHostImpl::DidSwapBuffersComplete();
358 test_hooks_->SwapBuffersCompleteOnThread(this);
361 void ReclaimResources(const CompositorFrameAck* ack) override {
362 LayerTreeHostImpl::ReclaimResources(ack);
365 void NotifyReadyToActivate() override {
366 if (block_notify_ready_to_activate_for_testing_) {
367 notify_ready_to_activate_was_blocked_ = true;
368 } else {
369 LayerTreeHostImpl::NotifyReadyToActivate();
370 test_hooks_->NotifyReadyToActivateOnThread(this);
374 void NotifyReadyToDraw() override {
375 LayerTreeHostImpl::NotifyReadyToDraw();
376 test_hooks_->NotifyReadyToDrawOnThread(this);
379 void NotifyAllTileTasksCompleted() override {
380 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
381 test_hooks_->NotifyAllTileTasksCompleted(this);
384 void BlockNotifyReadyToActivateForTesting(bool block) override {
385 CHECK(proxy()->ImplThreadTaskRunner())
386 << "Not supported for single-threaded mode.";
387 block_notify_ready_to_activate_for_testing_ = block;
388 if (!block && notify_ready_to_activate_was_blocked_) {
389 NotifyReadyToActivate();
390 notify_ready_to_activate_was_blocked_ = false;
394 void ActivateSyncTree() override {
395 test_hooks_->WillActivateTreeOnThread(this);
396 LayerTreeHostImpl::ActivateSyncTree();
397 DCHECK(!pending_tree());
398 test_hooks_->DidActivateTreeOnThread(this);
401 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
402 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
403 test_hooks_->InitializedRendererOnThread(this, success);
404 return success;
407 void SetVisible(bool visible) override {
408 LayerTreeHostImpl::SetVisible(visible);
409 test_hooks_->DidSetVisibleOnImplTree(this, visible);
412 void AnimateLayers(base::TimeTicks monotonic_time) override {
413 test_hooks_->WillAnimateLayers(this, monotonic_time);
414 LayerTreeHostImpl::AnimateLayers(monotonic_time);
415 test_hooks_->AnimateLayers(this, monotonic_time);
418 void UpdateAnimationState(bool start_ready_animations) override {
419 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
420 bool has_unfinished_animation = false;
421 AnimationRegistrar* registrar =
422 animation_registrar() ? animation_registrar()
423 : animation_host()->animation_registrar();
424 for (const auto& it :
425 registrar->active_animation_controllers_for_testing()) {
426 if (it.second->HasActiveAnimation()) {
427 has_unfinished_animation = true;
428 break;
431 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
434 void NotifyTileStateChanged(const Tile* tile) override {
435 LayerTreeHostImpl::NotifyTileStateChanged(tile);
436 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
439 private:
440 TestHooks* test_hooks_;
441 bool block_notify_ready_to_activate_for_testing_;
442 bool notify_ready_to_activate_was_blocked_;
445 // Implementation of LayerTreeHost callback interface.
446 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
447 public LayerTreeHostSingleThreadClient {
448 public:
449 static scoped_ptr<LayerTreeHostClientForTesting> Create(
450 TestHooks* test_hooks) {
451 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
453 ~LayerTreeHostClientForTesting() override {}
455 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
457 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
459 void BeginMainFrame(const BeginFrameArgs& args) override {
460 test_hooks_->BeginMainFrame(args);
463 void Layout() override { test_hooks_->Layout(); }
465 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
466 const gfx::Vector2dF& outer_delta,
467 const gfx::Vector2dF& elastic_overscroll_delta,
468 float page_scale,
469 float top_controls_delta) override {
470 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
471 elastic_overscroll_delta, page_scale,
472 top_controls_delta);
475 void RequestNewOutputSurface() override {
476 test_hooks_->RequestNewOutputSurface();
479 void DidInitializeOutputSurface() override {
480 test_hooks_->DidInitializeOutputSurface();
483 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
484 test_hooks_->SendBeginFramesToChildren(args);
487 void DidFailToInitializeOutputSurface() override {
488 test_hooks_->DidFailToInitializeOutputSurface();
489 RequestNewOutputSurface();
492 void WillCommit() override { test_hooks_->WillCommit(); }
494 void DidCommit() override { test_hooks_->DidCommit(); }
496 void DidCommitAndDrawFrame() override {
497 test_hooks_->DidCommitAndDrawFrame();
500 void DidCompleteSwapBuffers() override {
501 test_hooks_->DidCompleteSwapBuffers();
504 void DidPostSwapBuffers() override {}
505 void DidAbortSwapBuffers() override {}
506 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
507 void DidCompletePageScaleAnimation() override {}
508 void BeginMainFrameNotExpectedSoon() override {}
510 void RecordFrameTimingEvents(
511 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
512 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
513 override {}
515 private:
516 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
517 : test_hooks_(test_hooks) {}
519 TestHooks* test_hooks_;
522 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
523 class LayerTreeHostForTesting : public LayerTreeHost {
524 public:
525 static scoped_ptr<LayerTreeHostForTesting> Create(
526 TestHooks* test_hooks,
527 LayerTreeHostClientForTesting* client,
528 SharedBitmapManager* shared_bitmap_manager,
529 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
530 TaskGraphRunner* task_graph_runner,
531 const LayerTreeSettings& settings,
532 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
533 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
534 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
535 LayerTreeHost::InitParams params;
536 params.client = client;
537 params.shared_bitmap_manager = shared_bitmap_manager;
538 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
539 params.task_graph_runner = task_graph_runner;
540 params.settings = &settings;
541 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
542 new LayerTreeHostForTesting(test_hooks, &params));
543 if (impl_task_runner.get()) {
544 layer_tree_host->InitializeForTesting(
545 ThreadProxyForTest::Create(test_hooks,
546 layer_tree_host.get(),
547 main_task_runner,
548 impl_task_runner,
549 external_begin_frame_source.Pass()));
550 } else {
551 layer_tree_host->InitializeForTesting(
552 SingleThreadProxyForTest::Create(
553 test_hooks,
554 layer_tree_host.get(),
555 client,
556 main_task_runner,
557 external_begin_frame_source.Pass()));
559 return layer_tree_host.Pass();
562 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
563 LayerTreeHostImplClient* host_impl_client) override {
564 return LayerTreeHostImplForTesting::Create(
565 test_hooks_, settings(), host_impl_client, proxy(),
566 shared_bitmap_manager(), gpu_memory_buffer_manager(),
567 task_graph_runner(), rendering_stats_instrumentation());
570 void SetNeedsCommit() override {
571 if (!test_started_)
572 return;
573 LayerTreeHost::SetNeedsCommit();
576 void SetNeedsUpdateLayers() override {
577 if (!test_started_)
578 return;
579 LayerTreeHost::SetNeedsUpdateLayers();
582 void set_test_started(bool started) { test_started_ = started; }
584 private:
585 LayerTreeHostForTesting(TestHooks* test_hooks,
586 LayerTreeHost::InitParams* params)
587 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
589 TestHooks* test_hooks_;
590 bool test_started_;
593 LayerTreeTest::LayerTreeTest()
594 : output_surface_(nullptr),
595 external_begin_frame_source_(nullptr),
596 beginning_(false),
597 end_when_begin_returns_(false),
598 timed_out_(false),
599 scheduled_(false),
600 started_(false),
601 ended_(false),
602 delegating_renderer_(false),
603 verify_property_trees_(true),
604 timeout_seconds_(0),
605 weak_factory_(this) {
606 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
608 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
609 // specified (for running in a debugger).
610 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
611 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
612 timeout_seconds_ = 5;
615 LayerTreeTest::~LayerTreeTest() {}
617 void LayerTreeTest::EndTest() {
618 if (ended_)
619 return;
620 ended_ = true;
622 // For the case where we EndTest during BeginTest(), set a flag to indicate
623 // that the test should end the second BeginTest regains control.
624 if (beginning_) {
625 end_when_begin_returns_ = true;
626 } else {
627 main_task_runner_->PostTask(
628 FROM_HERE,
629 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
633 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
634 main_task_runner_->PostDelayedTask(
635 FROM_HERE,
636 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
637 base::TimeDelta::FromMilliseconds(delay_milliseconds));
640 void LayerTreeTest::PostAddAnimationToMainThread(
641 Layer* layer_to_receive_animation) {
642 main_task_runner_->PostTask(
643 FROM_HERE,
644 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
645 base::Unretained(layer_to_receive_animation), 0.000004));
648 void LayerTreeTest::PostAddInstantAnimationToMainThread(
649 Layer* layer_to_receive_animation) {
650 main_task_runner_->PostTask(
651 FROM_HERE,
652 base::Bind(&LayerTreeTest::DispatchAddAnimation,
653 main_thread_weak_ptr_,
654 base::Unretained(layer_to_receive_animation),
655 0.0));
658 void LayerTreeTest::PostAddLongAnimationToMainThread(
659 Layer* layer_to_receive_animation) {
660 main_task_runner_->PostTask(
661 FROM_HERE,
662 base::Bind(&LayerTreeTest::DispatchAddAnimation,
663 main_thread_weak_ptr_,
664 base::Unretained(layer_to_receive_animation),
665 1.0));
668 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
669 AnimationPlayer* player_to_receive_animation) {
670 main_task_runner_->PostTask(
671 FROM_HERE,
672 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
673 main_thread_weak_ptr_,
674 base::Unretained(player_to_receive_animation), 0.000004));
677 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
678 AnimationPlayer* player_to_receive_animation) {
679 main_task_runner_->PostTask(
680 FROM_HERE,
681 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
682 main_thread_weak_ptr_,
683 base::Unretained(player_to_receive_animation), 0.0));
686 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
687 AnimationPlayer* player_to_receive_animation) {
688 main_task_runner_->PostTask(
689 FROM_HERE,
690 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
691 main_thread_weak_ptr_,
692 base::Unretained(player_to_receive_animation), 1.0));
695 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
696 main_task_runner_->PostTask(
697 FROM_HERE,
698 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
699 main_thread_weak_ptr_, defer_commits));
702 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
703 main_task_runner_->PostTask(FROM_HERE,
704 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
705 main_thread_weak_ptr_));
708 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
709 main_task_runner_->PostTask(
710 FROM_HERE,
711 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
712 main_thread_weak_ptr_));
715 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
716 main_task_runner_->PostTask(FROM_HERE,
717 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
718 main_thread_weak_ptr_));
721 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
722 const gfx::Rect& damage_rect) {
723 main_task_runner_->PostTask(
724 FROM_HERE,
725 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
726 main_thread_weak_ptr_,
727 damage_rect));
730 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
731 main_task_runner_->PostTask(
732 FROM_HERE,
733 base::Bind(
734 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
737 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
738 main_task_runner_->PostTask(
739 FROM_HERE,
740 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
741 main_thread_weak_ptr_));
744 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
745 main_task_runner_->PostTask(
746 FROM_HERE,
747 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
748 main_thread_weak_ptr_));
751 void LayerTreeTest::WillBeginTest() {
752 layer_tree_host_->SetLayerTreeHostClientReady();
755 void LayerTreeTest::DoBeginTest() {
756 client_ = LayerTreeHostClientForTesting::Create(this);
758 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
759 if (settings_.use_external_begin_frame_source) {
760 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
761 settings_.renderer_settings.refresh_rate));
762 external_begin_frame_source_ = external_begin_frame_source.get();
765 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
766 layer_tree_host_ = LayerTreeHostForTesting::Create(
767 this, client_.get(), shared_bitmap_manager_.get(),
768 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
769 base::ThreadTaskRunnerHandle::Get(),
770 impl_thread_ ? impl_thread_->task_runner() : NULL,
771 external_begin_frame_source.Pass());
772 ASSERT_TRUE(layer_tree_host_);
774 started_ = true;
775 beginning_ = true;
776 SetupTree();
777 WillBeginTest();
778 BeginTest();
779 beginning_ = false;
780 if (end_when_begin_returns_)
781 RealEndTest();
783 // Allow commits to happen once BeginTest() has had a chance to post tasks
784 // so that those tasks will happen before the first commit.
785 if (layer_tree_host_) {
786 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
787 ->set_test_started(true);
791 void LayerTreeTest::SetupTree() {
792 if (!layer_tree_host_->root_layer()) {
793 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
794 root_layer->SetBounds(gfx::Size(1, 1));
795 root_layer->SetIsDrawable(true);
796 layer_tree_host_->SetRootLayer(root_layer);
799 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
800 gfx::Size device_root_bounds = gfx::ToCeiledSize(
801 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
802 layer_tree_host_->SetViewportSize(device_root_bounds);
805 void LayerTreeTest::Timeout() {
806 timed_out_ = true;
807 EndTest();
810 void LayerTreeTest::RealEndTest() {
811 // TODO(mithro): Make this method only end when not inside an impl frame.
812 if (layer_tree_host_ && !timed_out_ &&
813 proxy()->MainFrameWillHappenForTesting()) {
814 main_task_runner_->PostTask(
815 FROM_HERE,
816 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
817 return;
820 base::MessageLoop::current()->Quit();
823 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
824 double animation_duration) {
825 DCHECK(!proxy() || proxy()->IsMainThread());
827 if (layer_to_receive_animation) {
828 AddOpacityTransitionToLayer(
829 layer_to_receive_animation, animation_duration, 0, 0.5, true);
833 void LayerTreeTest::DispatchAddAnimationToPlayer(
834 AnimationPlayer* player_to_receive_animation,
835 double animation_duration) {
836 DCHECK(!proxy() || proxy()->IsMainThread());
838 if (player_to_receive_animation) {
839 AddOpacityTransitionToPlayer(player_to_receive_animation,
840 animation_duration, 0, 0.5, true);
844 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
845 DCHECK(!proxy() || proxy()->IsMainThread());
847 if (layer_tree_host_)
848 layer_tree_host_->SetDeferCommits(defer_commits);
851 void LayerTreeTest::DispatchSetNeedsCommit() {
852 DCHECK(!proxy() || proxy()->IsMainThread());
854 if (layer_tree_host_)
855 layer_tree_host_->SetNeedsCommit();
858 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
859 DCHECK(!proxy() || proxy()->IsMainThread());
861 if (layer_tree_host_)
862 layer_tree_host_->SetNeedsUpdateLayers();
865 void LayerTreeTest::DispatchSetNeedsRedraw() {
866 DCHECK(!proxy() || proxy()->IsMainThread());
868 if (layer_tree_host_)
869 layer_tree_host_->SetNeedsRedraw();
872 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
873 DCHECK(!proxy() || proxy()->IsMainThread());
875 if (layer_tree_host_)
876 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
879 void LayerTreeTest::DispatchSetVisible(bool visible) {
880 DCHECK(!proxy() || proxy()->IsMainThread());
881 if (layer_tree_host_)
882 layer_tree_host_->SetVisible(visible);
885 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
886 DCHECK(!proxy() || proxy()->IsMainThread());
888 if (layer_tree_host_)
889 layer_tree_host_->SetNextCommitForcesRedraw();
892 void LayerTreeTest::DispatchCompositeImmediately() {
893 DCHECK(!proxy() || proxy()->IsMainThread());
894 if (layer_tree_host_)
895 layer_tree_host_->Composite(base::TimeTicks::Now());
898 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
899 if (threaded) {
900 impl_thread_.reset(new base::Thread("Compositor"));
901 ASSERT_TRUE(impl_thread_->Start());
904 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
906 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
907 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
908 task_graph_runner_.reset(new TestTaskGraphRunner);
910 delegating_renderer_ = delegating_renderer;
912 // Spend less time waiting for BeginFrame because the output is
913 // mocked out.
914 settings_.renderer_settings.refresh_rate = 200.0;
915 settings_.background_animation_rate = 200.0;
916 settings_.verify_property_trees = verify_property_trees_;
917 InitializeSettings(&settings_);
918 InitializeLayerSettings(&layer_settings_);
920 main_task_runner_->PostTask(
921 FROM_HERE,
922 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
924 if (timeout_seconds_) {
925 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
926 main_task_runner_->PostDelayedTask(
927 FROM_HERE,
928 timeout_.callback(),
929 base::TimeDelta::FromSeconds(timeout_seconds_));
932 base::MessageLoop::current()->Run();
933 DestroyLayerTreeHost();
935 timeout_.Cancel();
937 ASSERT_FALSE(layer_tree_host_.get());
938 client_ = nullptr;
939 if (timed_out_) {
940 FAIL() << "Test timed out";
941 return;
943 AfterTest();
946 void LayerTreeTest::RequestNewOutputSurface() {
947 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
950 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
951 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
952 DCHECK_EQ(delegating_renderer_,
953 output_surface->capabilities().delegated_rendering);
954 output_surface_ = output_surface.get();
956 if (settings_.use_external_begin_frame_source) {
957 DCHECK(external_begin_frame_source_);
958 DCHECK(external_begin_frame_source_->is_ready());
960 return output_surface.Pass();
963 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
964 if (delegating_renderer_)
965 return FakeOutputSurface::CreateDelegating3d();
966 else
967 return FakeOutputSurface::Create3d();
970 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
971 return static_cast<TestContextProvider*>(output_surface_->context_provider())
972 ->TestContext3d();
975 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
976 const {
977 if (impl->pending_tree())
978 return impl->pending_tree()->source_frame_number();
979 if (impl->active_tree())
980 return impl->active_tree()->source_frame_number();
981 // Source frames start at 0, so this is invalid.
982 return -1;
985 void LayerTreeTest::DestroyLayerTreeHost() {
986 if (layer_tree_host_ && layer_tree_host_->root_layer())
987 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
988 layer_tree_host_ = nullptr;
991 LayerTreeHost* LayerTreeTest::layer_tree_host() {
992 // We check for a null proxy here as we sometimes ask for the layer tree host
993 // when the proxy does not exist, often for checking settings after a test has
994 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
995 // elsewhere in this file for other examples.
996 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
997 return layer_tree_host_.get();
1000 } // namespace cc