[GCM] Remove GCMDriver::Purge and GCMClient::CheckOut
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob9653d8dda8a800482072ea12f4e427197f0e01f9
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/test/layer_tree_test.h"
7 #include "base/command_line.h"
8 #include "cc/animation/animation.h"
9 #include "cc/animation/animation_registrar.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/base/switches.h"
13 #include "cc/input/input_handler.h"
14 #include "cc/layers/content_layer.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_impl.h"
17 #include "cc/test/animation_test_common.h"
18 #include "cc/test/begin_frame_args_test.h"
19 #include "cc/test/fake_external_begin_frame_source.h"
20 #include "cc/test/fake_layer_tree_host_client.h"
21 #include "cc/test/fake_output_surface.h"
22 #include "cc/test/test_context_provider.h"
23 #include "cc/test/test_gpu_memory_buffer_manager.h"
24 #include "cc/test/test_shared_bitmap_manager.h"
25 #include "cc/test/tiled_layer_test_common.h"
26 #include "cc/trees/layer_tree_host_client.h"
27 #include "cc/trees/layer_tree_host_impl.h"
28 #include "cc/trees/layer_tree_host_single_thread_client.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "cc/trees/single_thread_proxy.h"
31 #include "cc/trees/thread_proxy.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "ui/gfx/frame_time.h"
34 #include "ui/gfx/geometry/size_conversions.h"
36 namespace cc {
38 TestHooks::TestHooks() {}
40 TestHooks::~TestHooks() {}
42 DrawResult TestHooks::PrepareToDrawOnThread(
43 LayerTreeHostImpl* host_impl,
44 LayerTreeHostImpl::FrameData* frame_data,
45 DrawResult draw_result) {
46 return draw_result;
49 scoped_ptr<Rasterizer> TestHooks::CreateRasterizer(
50 LayerTreeHostImpl* host_impl) {
51 return host_impl->LayerTreeHostImpl::CreateRasterizer();
54 void TestHooks::CreateResourceAndTileTaskWorkerPool(
55 LayerTreeHostImpl* host_impl,
56 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
57 scoped_ptr<ResourcePool>* resource_pool,
58 scoped_ptr<ResourcePool>* staging_resource_pool) {
59 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
60 tile_task_worker_pool, resource_pool, staging_resource_pool);
63 // Adapts ThreadProxy for test. Injects test hooks for testing.
64 class ThreadProxyForTest : public ThreadProxy {
65 public:
66 static scoped_ptr<Proxy> Create(
67 TestHooks* test_hooks,
68 LayerTreeHost* host,
69 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
70 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
71 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
72 return make_scoped_ptr(new ThreadProxyForTest(
73 test_hooks,
74 host,
75 main_task_runner,
76 impl_task_runner,
77 external_begin_frame_source.Pass()));
80 ~ThreadProxyForTest() override {}
82 void test() {
83 test_hooks_->Layout();
86 private:
87 TestHooks* test_hooks_;
89 void ScheduledActionSendBeginMainFrame() override {
90 test_hooks_->ScheduledActionWillSendBeginMainFrame();
91 ThreadProxy::ScheduledActionSendBeginMainFrame();
92 test_hooks_->ScheduledActionSendBeginMainFrame();
95 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
96 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
97 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
98 return result;
101 void ScheduledActionAnimate() override {
102 ThreadProxy::ScheduledActionAnimate();
103 test_hooks_->ScheduledActionAnimate();
106 void ScheduledActionCommit() override {
107 ThreadProxy::ScheduledActionCommit();
108 test_hooks_->ScheduledActionCommit();
111 void ScheduledActionBeginOutputSurfaceCreation() override {
112 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
113 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
116 void ScheduledActionPrepareTiles() override {
117 ThreadProxy::ScheduledActionPrepareTiles();
118 test_hooks_->ScheduledActionPrepareTiles();
121 ThreadProxyForTest(
122 TestHooks* test_hooks,
123 LayerTreeHost* host,
124 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
125 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
126 scoped_ptr<BeginFrameSource> external_begin_frame_source)
127 : ThreadProxy(host, main_task_runner,
128 impl_task_runner,
129 external_begin_frame_source.Pass()),
130 test_hooks_(test_hooks) {}
133 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
134 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
135 public:
136 static scoped_ptr<LayerTreeHostImplForTesting> Create(
137 TestHooks* test_hooks,
138 const LayerTreeSettings& settings,
139 LayerTreeHostImplClient* host_impl_client,
140 Proxy* proxy,
141 SharedBitmapManager* shared_bitmap_manager,
142 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
143 RenderingStatsInstrumentation* stats_instrumentation) {
144 return make_scoped_ptr(
145 new LayerTreeHostImplForTesting(test_hooks,
146 settings,
147 host_impl_client,
148 proxy,
149 shared_bitmap_manager,
150 gpu_memory_buffer_manager,
151 stats_instrumentation));
154 protected:
155 LayerTreeHostImplForTesting(
156 TestHooks* test_hooks,
157 const LayerTreeSettings& settings,
158 LayerTreeHostImplClient* host_impl_client,
159 Proxy* proxy,
160 SharedBitmapManager* shared_bitmap_manager,
161 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
162 RenderingStatsInstrumentation* stats_instrumentation)
163 : LayerTreeHostImpl(settings,
164 host_impl_client,
165 proxy,
166 stats_instrumentation,
167 shared_bitmap_manager,
168 gpu_memory_buffer_manager,
170 test_hooks_(test_hooks),
171 block_notify_ready_to_activate_for_testing_(false),
172 notify_ready_to_activate_was_blocked_(false) {}
174 scoped_ptr<Rasterizer> CreateRasterizer() override {
175 return test_hooks_->CreateRasterizer(this);
178 void CreateResourceAndTileTaskWorkerPool(
179 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
180 scoped_ptr<ResourcePool>* resource_pool,
181 scoped_ptr<ResourcePool>* staging_resource_pool) override {
182 test_hooks_->CreateResourceAndTileTaskWorkerPool(
183 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
186 void WillBeginImplFrame(const BeginFrameArgs& args) override {
187 LayerTreeHostImpl::WillBeginImplFrame(args);
188 test_hooks_->WillBeginImplFrameOnThread(this, args);
191 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
192 LayerTreeHostImpl::BeginMainFrameAborted(reason);
193 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
196 void BeginCommit() override {
197 LayerTreeHostImpl::BeginCommit();
198 test_hooks_->BeginCommitOnThread(this);
201 void CommitComplete() override {
202 LayerTreeHostImpl::CommitComplete();
203 test_hooks_->CommitCompleteOnThread(this);
206 DrawResult PrepareToDraw(FrameData* frame) override {
207 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
208 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
211 void DrawLayers(FrameData* frame, base::TimeTicks frame_begin_time) override {
212 LayerTreeHostImpl::DrawLayers(frame, frame_begin_time);
213 test_hooks_->DrawLayersOnThread(this);
216 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
217 bool result = LayerTreeHostImpl::SwapBuffers(frame);
218 test_hooks_->SwapBuffersOnThread(this, result);
219 return result;
222 void DidSwapBuffersComplete() override {
223 LayerTreeHostImpl::DidSwapBuffersComplete();
224 test_hooks_->SwapBuffersCompleteOnThread(this);
227 void ReclaimResources(const CompositorFrameAck* ack) override {
228 LayerTreeHostImpl::ReclaimResources(ack);
231 void NotifyReadyToActivate() override {
232 if (block_notify_ready_to_activate_for_testing_) {
233 notify_ready_to_activate_was_blocked_ = true;
234 } else {
235 LayerTreeHostImpl::NotifyReadyToActivate();
236 test_hooks_->NotifyReadyToActivateOnThread(this);
240 void NotifyReadyToDraw() override {
241 LayerTreeHostImpl::NotifyReadyToDraw();
242 test_hooks_->NotifyReadyToDrawOnThread(this);
245 void BlockNotifyReadyToActivateForTesting(bool block) override {
246 CHECK(settings().impl_side_painting);
247 CHECK(proxy()->ImplThreadTaskRunner())
248 << "Not supported for single-threaded mode.";
249 block_notify_ready_to_activate_for_testing_ = block;
250 if (!block && notify_ready_to_activate_was_blocked_) {
251 NotifyReadyToActivate();
252 notify_ready_to_activate_was_blocked_ = false;
256 void ActivateSyncTree() override {
257 test_hooks_->WillActivateTreeOnThread(this);
258 LayerTreeHostImpl::ActivateSyncTree();
259 DCHECK(!pending_tree());
260 test_hooks_->DidActivateTreeOnThread(this);
263 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
264 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
265 test_hooks_->InitializedRendererOnThread(this, success);
266 return success;
269 void SetVisible(bool visible) override {
270 LayerTreeHostImpl::SetVisible(visible);
271 test_hooks_->DidSetVisibleOnImplTree(this, visible);
274 void AnimateLayers(base::TimeTicks monotonic_time) override {
275 test_hooks_->WillAnimateLayers(this, monotonic_time);
276 LayerTreeHostImpl::AnimateLayers(monotonic_time);
277 test_hooks_->AnimateLayers(this, monotonic_time);
280 void UpdateAnimationState(bool start_ready_animations) override {
281 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
282 bool has_unfinished_animation = false;
283 AnimationRegistrar::AnimationControllerMap::const_iterator iter =
284 active_animation_controllers().begin();
285 for (; iter != active_animation_controllers().end(); ++iter) {
286 if (iter->second->HasActiveAnimation()) {
287 has_unfinished_animation = true;
288 break;
291 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
294 void NotifyTileStateChanged(const Tile* tile) override {
295 LayerTreeHostImpl::NotifyTileStateChanged(tile);
296 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
299 private:
300 TestHooks* test_hooks_;
301 bool block_notify_ready_to_activate_for_testing_;
302 bool notify_ready_to_activate_was_blocked_;
305 // Implementation of LayerTreeHost callback interface.
306 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
307 public LayerTreeHostSingleThreadClient {
308 public:
309 static scoped_ptr<LayerTreeHostClientForTesting> Create(
310 TestHooks* test_hooks) {
311 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
313 ~LayerTreeHostClientForTesting() override {}
315 void WillBeginMainFrame(int frame_id) override {
316 test_hooks_->WillBeginMainFrame();
319 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
321 void BeginMainFrame(const BeginFrameArgs& args) override {
322 test_hooks_->BeginMainFrame(args);
325 void Layout() override { test_hooks_->Layout(); }
327 void ApplyViewportDeltas(const gfx::Vector2d& inner_delta,
328 const gfx::Vector2d& outer_delta,
329 const gfx::Vector2dF& elastic_overscroll_delta,
330 float page_scale,
331 float top_controls_delta) override {
332 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
333 elastic_overscroll_delta, page_scale,
334 top_controls_delta);
336 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
337 float scale,
338 float top_controls_delta) override {
339 test_hooks_->ApplyViewportDeltas(scroll_delta,
340 scale,
341 top_controls_delta);
344 void RequestNewOutputSurface() override {
345 test_hooks_->RequestNewOutputSurface();
348 void DidInitializeOutputSurface() override {
349 test_hooks_->DidInitializeOutputSurface();
352 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
353 test_hooks_->SendBeginFramesToChildren(args);
356 void DidFailToInitializeOutputSurface() override {
357 test_hooks_->DidFailToInitializeOutputSurface();
358 RequestNewOutputSurface();
361 void WillCommit() override { test_hooks_->WillCommit(); }
363 void DidCommit() override { test_hooks_->DidCommit(); }
365 void DidCommitAndDrawFrame() override {
366 test_hooks_->DidCommitAndDrawFrame();
369 void DidCompleteSwapBuffers() override {
370 test_hooks_->DidCompleteSwapBuffers();
373 void DidPostSwapBuffers() override {}
374 void DidAbortSwapBuffers() override {}
375 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
377 private:
378 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
379 : test_hooks_(test_hooks) {}
381 TestHooks* test_hooks_;
384 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
385 class LayerTreeHostForTesting : public LayerTreeHost {
386 public:
387 static scoped_ptr<LayerTreeHostForTesting> Create(
388 TestHooks* test_hooks,
389 LayerTreeHostClientForTesting* client,
390 const LayerTreeSettings& settings,
391 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
392 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
393 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
394 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
395 new LayerTreeHostForTesting(test_hooks, client, settings));
396 if (impl_task_runner.get()) {
397 layer_tree_host->InitializeForTesting(
398 ThreadProxyForTest::Create(test_hooks,
399 layer_tree_host.get(),
400 main_task_runner,
401 impl_task_runner,
402 external_begin_frame_source.Pass()));
403 } else {
404 layer_tree_host->InitializeForTesting(SingleThreadProxy::Create(
405 layer_tree_host.get(),
406 client,
407 main_task_runner,
408 external_begin_frame_source.Pass()));
410 return layer_tree_host.Pass();
413 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
414 LayerTreeHostImplClient* host_impl_client) override {
415 return LayerTreeHostImplForTesting::Create(
416 test_hooks_,
417 settings(),
418 host_impl_client,
419 proxy(),
420 shared_bitmap_manager_.get(),
421 gpu_memory_buffer_manager_.get(),
422 rendering_stats_instrumentation());
425 void SetNeedsCommit() override {
426 if (!test_started_)
427 return;
428 LayerTreeHost::SetNeedsCommit();
431 void set_test_started(bool started) { test_started_ = started; }
433 void DidDeferCommit() override { test_hooks_->DidDeferCommit(); }
435 private:
436 LayerTreeHostForTesting(TestHooks* test_hooks,
437 LayerTreeHostClient* client,
438 const LayerTreeSettings& settings)
439 : LayerTreeHost(client, NULL, NULL, settings),
440 shared_bitmap_manager_(new TestSharedBitmapManager),
441 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager),
442 test_hooks_(test_hooks),
443 test_started_(false) {}
445 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
446 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
447 TestHooks* test_hooks_;
448 bool test_started_;
451 LayerTreeTest::LayerTreeTest()
452 : output_surface_(nullptr),
453 external_begin_frame_source_(nullptr),
454 beginning_(false),
455 end_when_begin_returns_(false),
456 timed_out_(false),
457 scheduled_(false),
458 started_(false),
459 ended_(false),
460 delegating_renderer_(false),
461 verify_property_trees_(true),
462 timeout_seconds_(0),
463 weak_factory_(this) {
464 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
466 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
467 // specified (for running in a debugger).
468 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
469 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
470 timeout_seconds_ = 5;
473 LayerTreeTest::~LayerTreeTest() {}
475 void LayerTreeTest::EndTest() {
476 if (ended_)
477 return;
478 ended_ = true;
480 // For the case where we EndTest during BeginTest(), set a flag to indicate
481 // that the test should end the second BeginTest regains control.
482 if (beginning_) {
483 end_when_begin_returns_ = true;
484 } else {
485 main_task_runner_->PostTask(
486 FROM_HERE,
487 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
491 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
492 main_task_runner_->PostDelayedTask(
493 FROM_HERE,
494 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
495 base::TimeDelta::FromMilliseconds(delay_milliseconds));
498 void LayerTreeTest::PostAddAnimationToMainThread(
499 Layer* layer_to_receive_animation) {
500 main_task_runner_->PostTask(
501 FROM_HERE,
502 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
503 base::Unretained(layer_to_receive_animation), 0.000004));
506 void LayerTreeTest::PostAddInstantAnimationToMainThread(
507 Layer* layer_to_receive_animation) {
508 main_task_runner_->PostTask(
509 FROM_HERE,
510 base::Bind(&LayerTreeTest::DispatchAddAnimation,
511 main_thread_weak_ptr_,
512 base::Unretained(layer_to_receive_animation),
513 0.0));
516 void LayerTreeTest::PostAddLongAnimationToMainThread(
517 Layer* layer_to_receive_animation) {
518 main_task_runner_->PostTask(
519 FROM_HERE,
520 base::Bind(&LayerTreeTest::DispatchAddAnimation,
521 main_thread_weak_ptr_,
522 base::Unretained(layer_to_receive_animation),
523 1.0));
526 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
527 main_task_runner_->PostTask(FROM_HERE,
528 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
529 main_thread_weak_ptr_));
532 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
533 main_task_runner_->PostTask(
534 FROM_HERE,
535 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
536 main_thread_weak_ptr_));
539 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
540 main_task_runner_->PostTask(FROM_HERE,
541 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
542 main_thread_weak_ptr_));
545 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
546 const gfx::Rect& damage_rect) {
547 main_task_runner_->PostTask(
548 FROM_HERE,
549 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
550 main_thread_weak_ptr_,
551 damage_rect));
554 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
555 main_task_runner_->PostTask(
556 FROM_HERE,
557 base::Bind(
558 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
561 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
562 main_task_runner_->PostTask(
563 FROM_HERE,
564 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
565 main_thread_weak_ptr_));
568 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
569 main_task_runner_->PostTask(
570 FROM_HERE,
571 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
572 main_thread_weak_ptr_));
575 void LayerTreeTest::WillBeginTest() {
576 layer_tree_host_->SetLayerTreeHostClientReady();
579 void LayerTreeTest::DoBeginTest() {
580 client_ = LayerTreeHostClientForTesting::Create(this);
582 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
583 if (settings_.use_external_begin_frame_source) {
584 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
585 settings_.renderer_settings.refresh_rate));
586 external_begin_frame_source_ = external_begin_frame_source.get();
589 DCHECK(!impl_thread_ || impl_thread_->message_loop_proxy().get());
590 layer_tree_host_ = LayerTreeHostForTesting::Create(
591 this,
592 client_.get(),
593 settings_,
594 base::MessageLoopProxy::current(),
595 impl_thread_ ? impl_thread_->message_loop_proxy() : NULL,
596 external_begin_frame_source.Pass());
597 ASSERT_TRUE(layer_tree_host_);
599 started_ = true;
600 beginning_ = true;
601 SetupTree();
602 WillBeginTest();
603 BeginTest();
604 beginning_ = false;
605 if (end_when_begin_returns_)
606 RealEndTest();
608 // Allow commits to happen once BeginTest() has had a chance to post tasks
609 // so that those tasks will happen before the first commit.
610 if (layer_tree_host_) {
611 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
612 ->set_test_started(true);
616 void LayerTreeTest::SetupTree() {
617 if (!layer_tree_host_->root_layer()) {
618 scoped_refptr<Layer> root_layer = Layer::Create();
619 root_layer->SetBounds(gfx::Size(1, 1));
620 root_layer->SetIsDrawable(true);
621 layer_tree_host_->SetRootLayer(root_layer);
624 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
625 gfx::Size device_root_bounds = gfx::ToCeiledSize(
626 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
627 layer_tree_host_->SetViewportSize(device_root_bounds);
630 void LayerTreeTest::Timeout() {
631 timed_out_ = true;
632 EndTest();
635 void LayerTreeTest::RealEndTest() {
636 if (layer_tree_host_ && !timed_out_ &&
637 proxy()->MainFrameWillHappenForTesting()) {
638 main_task_runner_->PostTask(
639 FROM_HERE,
640 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
641 return;
644 base::MessageLoop::current()->Quit();
647 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
648 double animation_duration) {
649 DCHECK(!proxy() || proxy()->IsMainThread());
651 if (layer_to_receive_animation) {
652 AddOpacityTransitionToLayer(
653 layer_to_receive_animation, animation_duration, 0, 0.5, true);
657 void LayerTreeTest::DispatchSetNeedsCommit() {
658 DCHECK(!proxy() || proxy()->IsMainThread());
660 if (layer_tree_host_)
661 layer_tree_host_->SetNeedsCommit();
664 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
665 DCHECK(!proxy() || proxy()->IsMainThread());
667 if (layer_tree_host_)
668 layer_tree_host_->SetNeedsUpdateLayers();
671 void LayerTreeTest::DispatchSetNeedsRedraw() {
672 DCHECK(!proxy() || proxy()->IsMainThread());
674 if (layer_tree_host_)
675 layer_tree_host_->SetNeedsRedraw();
678 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
679 DCHECK(!proxy() || proxy()->IsMainThread());
681 if (layer_tree_host_)
682 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
685 void LayerTreeTest::DispatchSetVisible(bool visible) {
686 DCHECK(!proxy() || proxy()->IsMainThread());
687 if (layer_tree_host_)
688 layer_tree_host_->SetVisible(visible);
691 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
692 DCHECK(!proxy() || proxy()->IsMainThread());
694 if (layer_tree_host_)
695 layer_tree_host_->SetNextCommitForcesRedraw();
698 void LayerTreeTest::DispatchCompositeImmediately() {
699 DCHECK(!proxy() || proxy()->IsMainThread());
700 if (layer_tree_host_)
701 layer_tree_host_->Composite(gfx::FrameTime::Now());
704 void LayerTreeTest::RunTest(bool threaded,
705 bool delegating_renderer,
706 bool impl_side_painting) {
707 if (threaded) {
708 impl_thread_.reset(new base::Thread("Compositor"));
709 ASSERT_TRUE(impl_thread_->Start());
712 main_task_runner_ = base::MessageLoopProxy::current();
714 delegating_renderer_ = delegating_renderer;
716 // Spend less time waiting for BeginFrame because the output is
717 // mocked out.
718 settings_.renderer_settings.refresh_rate = 200.0;
719 settings_.background_animation_rate = 200.0;
720 settings_.impl_side_painting = impl_side_painting;
721 settings_.verify_property_trees = verify_property_trees_;
722 InitializeSettings(&settings_);
724 main_task_runner_->PostTask(
725 FROM_HERE,
726 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
728 if (timeout_seconds_) {
729 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
730 main_task_runner_->PostDelayedTask(
731 FROM_HERE,
732 timeout_.callback(),
733 base::TimeDelta::FromSeconds(timeout_seconds_));
736 base::MessageLoop::current()->Run();
737 DestroyLayerTreeHost();
739 timeout_.Cancel();
741 ASSERT_FALSE(layer_tree_host_.get());
742 client_ = nullptr;
743 if (timed_out_) {
744 FAIL() << "Test timed out";
745 return;
747 AfterTest();
750 void LayerTreeTest::RunTestWithImplSidePainting() {
751 RunTest(true, false, true);
754 void LayerTreeTest::RequestNewOutputSurface() {
755 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
758 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
759 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
760 DCHECK_EQ(delegating_renderer_,
761 output_surface->capabilities().delegated_rendering);
762 output_surface_ = output_surface.get();
764 if (settings_.use_external_begin_frame_source) {
765 DCHECK(external_begin_frame_source_);
766 DCHECK(external_begin_frame_source_->is_ready());
768 return output_surface.Pass();
771 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
772 if (delegating_renderer_)
773 return FakeOutputSurface::CreateDelegating3d();
774 else
775 return FakeOutputSurface::Create3d();
778 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
779 return static_cast<TestContextProvider*>(output_surface_->context_provider())
780 ->TestContext3d();
783 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
784 const {
785 if (impl->pending_tree())
786 return impl->pending_tree()->source_frame_number();
787 if (impl->active_tree())
788 return impl->active_tree()->source_frame_number();
789 // Source frames start at 0, so this is invalid.
790 return -1;
793 void LayerTreeTest::DestroyLayerTreeHost() {
794 if (layer_tree_host_ && layer_tree_host_->root_layer())
795 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
796 layer_tree_host_ = nullptr;
799 } // namespace cc