Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / cc / test / layer_tree_test.cc
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.
4
5 #include "cc/test/layer_tree_test.h"
6
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/fake_layer_tree_host_client.h"
19 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/test_context_provider.h"
21 #include "cc/test/test_shared_bitmap_manager.h"
22 #include "cc/test/tiled_layer_test_common.h"
23 #include "cc/trees/layer_tree_host_client.h"
24 #include "cc/trees/layer_tree_host_impl.h"
25 #include "cc/trees/layer_tree_host_single_thread_client.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/single_thread_proxy.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "ui/gfx/frame_time.h"
30 #include "ui/gfx/size_conversions.h"
31
32 namespace cc {
33
34 TestHooks::TestHooks() {}
35
36 TestHooks::~TestHooks() {}
37
38 DrawSwapReadbackResult::DrawResult TestHooks::PrepareToDrawOnThread(
39     LayerTreeHostImpl* host_impl,
40     LayerTreeHostImpl::FrameData* frame_data,
41     DrawSwapReadbackResult::DrawResult draw_result) {
42   return draw_result;
43 }
44
45 base::TimeDelta TestHooks::LowFrequencyAnimationInterval() const {
46   return base::TimeDelta::FromMilliseconds(16);
47 }
48
49 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
50 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
51  public:
52   static scoped_ptr<LayerTreeHostImplForTesting> Create(
53       TestHooks* test_hooks,
54       const LayerTreeSettings& settings,
55       LayerTreeHostImplClient* host_impl_client,
56       Proxy* proxy,
57       SharedBitmapManager* manager,
58       RenderingStatsInstrumentation* stats_instrumentation) {
59     return make_scoped_ptr(
60         new LayerTreeHostImplForTesting(test_hooks,
61                                         settings,
62                                         host_impl_client,
63                                         proxy,
64                                         manager,
65                                         stats_instrumentation));
66   }
67
68  protected:
69   LayerTreeHostImplForTesting(
70       TestHooks* test_hooks,
71       const LayerTreeSettings& settings,
72       LayerTreeHostImplClient* host_impl_client,
73       Proxy* proxy,
74       SharedBitmapManager* manager,
75       RenderingStatsInstrumentation* stats_instrumentation)
76       : LayerTreeHostImpl(settings,
77                           host_impl_client,
78                           proxy,
79                           stats_instrumentation,
80                           manager,
81                           0),
82         test_hooks_(test_hooks),
83         block_notify_ready_to_activate_for_testing_(false),
84         notify_ready_to_activate_was_blocked_(false) {}
85
86   virtual void WillBeginImplFrame(const BeginFrameArgs& args) OVERRIDE {
87     LayerTreeHostImpl::WillBeginImplFrame(args);
88     test_hooks_->WillBeginImplFrameOnThread(this, args);
89   }
90
91   virtual void BeginMainFrameAborted(bool did_handle) OVERRIDE {
92     LayerTreeHostImpl::BeginMainFrameAborted(did_handle);
93     test_hooks_->BeginMainFrameAbortedOnThread(this, did_handle);
94   }
95
96   virtual void BeginCommit() OVERRIDE {
97     LayerTreeHostImpl::BeginCommit();
98     test_hooks_->BeginCommitOnThread(this);
99   }
100
101   virtual void CommitComplete() OVERRIDE {
102     LayerTreeHostImpl::CommitComplete();
103     test_hooks_->CommitCompleteOnThread(this);
104
105     if (!settings().impl_side_painting) {
106       test_hooks_->WillActivateTreeOnThread(this);
107       test_hooks_->DidActivateTreeOnThread(this);
108     }
109   }
110
111   virtual DrawSwapReadbackResult::DrawResult PrepareToDraw(
112       FrameData* frame,
113       const gfx::Rect& damage_rect) OVERRIDE {
114     DrawSwapReadbackResult::DrawResult draw_result =
115         LayerTreeHostImpl::PrepareToDraw(frame, damage_rect);
116     return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
117   }
118
119   virtual void DrawLayers(FrameData* frame,
120                           base::TimeTicks frame_begin_time) OVERRIDE {
121     LayerTreeHostImpl::DrawLayers(frame, frame_begin_time);
122     test_hooks_->DrawLayersOnThread(this);
123   }
124
125   virtual bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) OVERRIDE {
126     bool result = LayerTreeHostImpl::SwapBuffers(frame);
127     test_hooks_->SwapBuffersOnThread(this, result);
128     return result;
129   }
130
131   virtual void DidSwapBuffersComplete() OVERRIDE {
132     LayerTreeHostImpl::DidSwapBuffersComplete();
133     test_hooks_->SwapBuffersCompleteOnThread(this);
134   }
135
136   virtual void ReclaimResources(const CompositorFrameAck* ack) OVERRIDE {
137     LayerTreeHostImpl::ReclaimResources(ack);
138   }
139
140   virtual void UpdateVisibleTiles() OVERRIDE {
141     LayerTreeHostImpl::UpdateVisibleTiles();
142     test_hooks_->UpdateVisibleTilesOnThread(this);
143   }
144
145   virtual void NotifyReadyToActivate() OVERRIDE {
146     if (block_notify_ready_to_activate_for_testing_)
147       notify_ready_to_activate_was_blocked_ = true;
148     else
149       client_->NotifyReadyToActivate();
150   }
151
152   virtual void BlockNotifyReadyToActivateForTesting(bool block) OVERRIDE {
153     block_notify_ready_to_activate_for_testing_ = block;
154     if (!block && notify_ready_to_activate_was_blocked_) {
155       NotifyReadyToActivate();
156       notify_ready_to_activate_was_blocked_ = false;
157     }
158   }
159
160   virtual void ActivatePendingTree() OVERRIDE {
161     test_hooks_->WillActivateTreeOnThread(this);
162     LayerTreeHostImpl::ActivatePendingTree();
163     DCHECK(!pending_tree());
164     test_hooks_->DidActivateTreeOnThread(this);
165   }
166
167   virtual bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface)
168       OVERRIDE {
169     bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
170     test_hooks_->InitializedRendererOnThread(this, success);
171     return success;
172   }
173
174   virtual void SetVisible(bool visible) OVERRIDE {
175     LayerTreeHostImpl::SetVisible(visible);
176     test_hooks_->DidSetVisibleOnImplTree(this, visible);
177   }
178
179   virtual void AnimateLayers(base::TimeTicks monotonic_time) OVERRIDE {
180     test_hooks_->WillAnimateLayers(this, monotonic_time);
181     LayerTreeHostImpl::AnimateLayers(monotonic_time);
182     test_hooks_->AnimateLayers(this, monotonic_time);
183   }
184
185   virtual void UpdateAnimationState(bool start_ready_animations) OVERRIDE {
186     LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
187     bool has_unfinished_animation = false;
188     AnimationRegistrar::AnimationControllerMap::const_iterator iter =
189         active_animation_controllers().begin();
190     for (; iter != active_animation_controllers().end(); ++iter) {
191       if (iter->second->HasActiveAnimation()) {
192         has_unfinished_animation = true;
193         break;
194       }
195     }
196     test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
197   }
198
199   virtual base::TimeDelta LowFrequencyAnimationInterval() const OVERRIDE {
200     return test_hooks_->LowFrequencyAnimationInterval();
201   }
202
203  private:
204   TestHooks* test_hooks_;
205   bool block_notify_ready_to_activate_for_testing_;
206   bool notify_ready_to_activate_was_blocked_;
207 };
208
209 // Implementation of LayerTreeHost callback interface.
210 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
211                                       public LayerTreeHostSingleThreadClient {
212  public:
213   static scoped_ptr<LayerTreeHostClientForTesting> Create(
214       TestHooks* test_hooks) {
215     return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
216   }
217   virtual ~LayerTreeHostClientForTesting() {}
218
219   virtual void WillBeginMainFrame(int frame_id) OVERRIDE {
220     test_hooks_->WillBeginMainFrame();
221   }
222
223   virtual void DidBeginMainFrame() OVERRIDE {
224     test_hooks_->DidBeginMainFrame();
225   }
226
227   virtual void Animate(base::TimeTicks monotonic_time) OVERRIDE {
228     test_hooks_->Animate(monotonic_time);
229   }
230
231   virtual void Layout() OVERRIDE { test_hooks_->Layout(); }
232
233   virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
234                                    float scale) OVERRIDE {
235     test_hooks_->ApplyScrollAndScale(scroll_delta, scale);
236   }
237
238   virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback)
239       OVERRIDE {
240     return test_hooks_->CreateOutputSurface(fallback);
241   }
242
243   virtual void DidInitializeOutputSurface() OVERRIDE {
244     test_hooks_->DidInitializeOutputSurface();
245   }
246
247   virtual void DidFailToInitializeOutputSurface() OVERRIDE {
248     test_hooks_->DidFailToInitializeOutputSurface();
249   }
250
251   virtual void WillCommit() OVERRIDE { test_hooks_->WillCommit(); }
252
253   virtual void DidCommit() OVERRIDE { test_hooks_->DidCommit(); }
254
255   virtual void DidCommitAndDrawFrame() OVERRIDE {
256     test_hooks_->DidCommitAndDrawFrame();
257   }
258
259   virtual void DidCompleteSwapBuffers() OVERRIDE {
260     test_hooks_->DidCompleteSwapBuffers();
261   }
262
263   virtual void ScheduleComposite() OVERRIDE {
264     test_hooks_->ScheduleComposite();
265   }
266
267   virtual void ScheduleAnimation() OVERRIDE {
268     test_hooks_->ScheduleAnimation();
269   }
270
271   virtual void DidPostSwapBuffers() OVERRIDE {}
272   virtual void DidAbortSwapBuffers() OVERRIDE {}
273
274  private:
275   explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
276       : test_hooks_(test_hooks) {}
277
278   TestHooks* test_hooks_;
279 };
280
281 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
282 class LayerTreeHostForTesting : public LayerTreeHost {
283  public:
284   static scoped_ptr<LayerTreeHostForTesting> Create(
285       TestHooks* test_hooks,
286       LayerTreeHostClientForTesting* client,
287       const LayerTreeSettings& settings,
288       scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
289     scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
290         new LayerTreeHostForTesting(test_hooks, client, settings));
291     if (impl_task_runner.get())
292       layer_tree_host->InitializeThreaded(impl_task_runner);
293     else
294       layer_tree_host->InitializeSingleThreaded(client);
295     return layer_tree_host.Pass();
296   }
297
298   virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
299       LayerTreeHostImplClient* host_impl_client) OVERRIDE {
300     return LayerTreeHostImplForTesting::Create(
301                test_hooks_,
302                settings(),
303                host_impl_client,
304                proxy(),
305                shared_bitmap_manager_.get(),
306                rendering_stats_instrumentation()).PassAs<LayerTreeHostImpl>();
307   }
308
309   virtual void SetNeedsCommit() OVERRIDE {
310     if (!test_started_)
311       return;
312     LayerTreeHost::SetNeedsCommit();
313   }
314
315   void set_test_started(bool started) { test_started_ = started; }
316
317   virtual void DidDeferCommit() OVERRIDE { test_hooks_->DidDeferCommit(); }
318
319  private:
320   LayerTreeHostForTesting(TestHooks* test_hooks,
321                           LayerTreeHostClient* client,
322                           const LayerTreeSettings& settings)
323       : LayerTreeHost(client, NULL, settings),
324         shared_bitmap_manager_(new TestSharedBitmapManager()),
325         test_hooks_(test_hooks),
326         test_started_(false) {}
327
328   scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
329   TestHooks* test_hooks_;
330   bool test_started_;
331 };
332
333 LayerTreeTest::LayerTreeTest()
334     : beginning_(false),
335       end_when_begin_returns_(false),
336       timed_out_(false),
337       scheduled_(false),
338       schedule_when_set_visible_true_(false),
339       started_(false),
340       ended_(false),
341       delegating_renderer_(false),
342       timeout_seconds_(0),
343       weak_factory_(this) {
344   main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
345
346   // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
347   // specified (for running in a debugger).
348   CommandLine* command_line = CommandLine::ForCurrentProcess();
349   if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
350     timeout_seconds_ = 5;
351 }
352
353 LayerTreeTest::~LayerTreeTest() {}
354
355 void LayerTreeTest::EndTest() {
356   if (ended_)
357     return;
358   ended_ = true;
359
360   // For the case where we EndTest during BeginTest(), set a flag to indicate
361   // that the test should end the second BeginTest regains control.
362   if (beginning_) {
363     end_when_begin_returns_ = true;
364   } else {
365     main_task_runner_->PostTask(
366         FROM_HERE,
367         base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
368   }
369 }
370
371 void LayerTreeTest::EndTestAfterDelay(int delay_milliseconds) {
372   main_task_runner_->PostDelayedTask(
373       FROM_HERE,
374       base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
375       base::TimeDelta::FromMilliseconds(delay_milliseconds));
376 }
377
378 void LayerTreeTest::PostAddAnimationToMainThread(
379     Layer* layer_to_receive_animation) {
380   main_task_runner_->PostTask(
381       FROM_HERE,
382       base::Bind(&LayerTreeTest::DispatchAddAnimation,
383                  main_thread_weak_ptr_,
384                  base::Unretained(layer_to_receive_animation),
385                  0.000001));
386 }
387
388 void LayerTreeTest::PostAddInstantAnimationToMainThread(
389     Layer* layer_to_receive_animation) {
390   main_task_runner_->PostTask(
391       FROM_HERE,
392       base::Bind(&LayerTreeTest::DispatchAddAnimation,
393                  main_thread_weak_ptr_,
394                  base::Unretained(layer_to_receive_animation),
395                  0.0));
396 }
397
398 void LayerTreeTest::PostAddLongAnimationToMainThread(
399     Layer* layer_to_receive_animation) {
400   main_task_runner_->PostTask(
401       FROM_HERE,
402       base::Bind(&LayerTreeTest::DispatchAddAnimation,
403                  main_thread_weak_ptr_,
404                  base::Unretained(layer_to_receive_animation),
405                  1.0));
406 }
407
408 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
409   main_task_runner_->PostTask(FROM_HERE,
410                               base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
411                                          main_thread_weak_ptr_));
412 }
413
414 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
415   main_task_runner_->PostTask(
416       FROM_HERE,
417       base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
418                  main_thread_weak_ptr_));
419 }
420
421 void LayerTreeTest::PostReadbackToMainThread() {
422   main_task_runner_->PostTask(
423       FROM_HERE,
424       base::Bind(&LayerTreeTest::DispatchReadback, main_thread_weak_ptr_));
425 }
426
427 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
428   main_task_runner_->PostTask(FROM_HERE,
429                               base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
430                                          main_thread_weak_ptr_));
431 }
432
433 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
434     const gfx::Rect& damage_rect) {
435   main_task_runner_->PostTask(
436       FROM_HERE,
437       base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
438                  main_thread_weak_ptr_,
439                  damage_rect));
440 }
441
442 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
443   main_task_runner_->PostTask(
444       FROM_HERE,
445       base::Bind(
446           &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
447 }
448
449 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
450   main_task_runner_->PostTask(
451       FROM_HERE,
452       base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
453                  main_thread_weak_ptr_));
454 }
455
456 void LayerTreeTest::WillBeginTest() {
457   layer_tree_host_->SetLayerTreeHostClientReady();
458 }
459
460 void LayerTreeTest::DoBeginTest() {
461   client_ = LayerTreeHostClientForTesting::Create(this);
462
463   DCHECK(!impl_thread_ || impl_thread_->message_loop_proxy().get());
464   layer_tree_host_ = LayerTreeHostForTesting::Create(
465       this,
466       client_.get(),
467       settings_,
468       impl_thread_ ? impl_thread_->message_loop_proxy() : NULL);
469   ASSERT_TRUE(layer_tree_host_);
470
471   started_ = true;
472   beginning_ = true;
473   SetupTree();
474   WillBeginTest();
475   BeginTest();
476   beginning_ = false;
477   if (end_when_begin_returns_)
478     RealEndTest();
479
480   // Allow commits to happen once BeginTest() has had a chance to post tasks
481   // so that those tasks will happen before the first commit.
482   if (layer_tree_host_) {
483     static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
484         ->set_test_started(true);
485   }
486 }
487
488 void LayerTreeTest::SetupTree() {
489   if (!layer_tree_host_->root_layer()) {
490     scoped_refptr<Layer> root_layer = Layer::Create();
491     root_layer->SetAnchorPoint(gfx::PointF());
492     root_layer->SetBounds(gfx::Size(1, 1));
493     root_layer->SetIsDrawable(true);
494     layer_tree_host_->SetRootLayer(root_layer);
495   }
496
497   gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
498   gfx::Size device_root_bounds = gfx::ToCeiledSize(
499       gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
500   layer_tree_host_->SetViewportSize(device_root_bounds);
501 }
502
503 void LayerTreeTest::Timeout() {
504   timed_out_ = true;
505   EndTest();
506 }
507
508 void LayerTreeTest::ScheduleComposite() {
509   if (!started_ || scheduled_)
510     return;
511   scheduled_ = true;
512   main_task_runner_->PostTask(
513       FROM_HERE,
514       base::Bind(&LayerTreeTest::DispatchComposite, main_thread_weak_ptr_));
515 }
516
517 void LayerTreeTest::RealEndTest() {
518   if (layer_tree_host_ && proxy()->CommitPendingForTesting()) {
519     main_task_runner_->PostTask(
520         FROM_HERE,
521         base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
522     return;
523   }
524
525   base::MessageLoop::current()->Quit();
526 }
527
528 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
529                                          double animation_duration) {
530   DCHECK(!proxy() || proxy()->IsMainThread());
531
532   if (layer_to_receive_animation) {
533     AddOpacityTransitionToLayer(
534         layer_to_receive_animation, animation_duration, 0, 0.5, true);
535   }
536 }
537
538 void LayerTreeTest::DispatchSetNeedsCommit() {
539   DCHECK(!proxy() || proxy()->IsMainThread());
540
541   if (layer_tree_host_)
542     layer_tree_host_->SetNeedsCommit();
543 }
544
545 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
546   DCHECK(!proxy() || proxy()->IsMainThread());
547
548   if (layer_tree_host_)
549     layer_tree_host_->SetNeedsUpdateLayers();
550 }
551
552 void LayerTreeTest::DispatchReadback() {
553   DCHECK(!proxy() || proxy()->IsMainThread());
554
555   if (layer_tree_host_) {
556     char pixels[4];
557     layer_tree_host()->CompositeAndReadback(&pixels, gfx::Rect(0, 0, 1, 1));
558   }
559 }
560
561 void LayerTreeTest::DispatchSetNeedsRedraw() {
562   DCHECK(!proxy() || proxy()->IsMainThread());
563
564   if (layer_tree_host_)
565     layer_tree_host_->SetNeedsRedraw();
566 }
567
568 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
569   DCHECK(!proxy() || proxy()->IsMainThread());
570
571   if (layer_tree_host_)
572     layer_tree_host_->SetNeedsRedrawRect(damage_rect);
573 }
574
575 void LayerTreeTest::DispatchSetVisible(bool visible) {
576   DCHECK(!proxy() || proxy()->IsMainThread());
577
578   if (!layer_tree_host_)
579     return;
580
581   layer_tree_host_->SetVisible(visible);
582
583   // If the LTH is being made visible and a previous ScheduleComposite() was
584   // deferred because the LTH was not visible, re-schedule the composite now.
585   if (layer_tree_host_->visible() && schedule_when_set_visible_true_)
586     ScheduleComposite();
587 }
588
589 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
590   DCHECK(!proxy() || proxy()->IsMainThread());
591
592   if (layer_tree_host_)
593     layer_tree_host_->SetNextCommitForcesRedraw();
594 }
595
596 void LayerTreeTest::DispatchComposite() {
597   scheduled_ = false;
598
599   if (!layer_tree_host_)
600     return;
601
602   // If the LTH is not visible, defer the composite until the LTH is made
603   // visible.
604   if (!layer_tree_host_->visible()) {
605     schedule_when_set_visible_true_ = true;
606     return;
607   }
608
609   schedule_when_set_visible_true_ = false;
610   base::TimeTicks now = gfx::FrameTime::Now();
611   layer_tree_host_->Composite(now);
612 }
613
614 void LayerTreeTest::RunTest(bool threaded,
615                             bool delegating_renderer,
616                             bool impl_side_painting) {
617   if (threaded) {
618     impl_thread_.reset(new base::Thread("Compositor"));
619     ASSERT_TRUE(impl_thread_->Start());
620   }
621
622   main_task_runner_ = base::MessageLoopProxy::current();
623
624   delegating_renderer_ = delegating_renderer;
625
626   // Spend less time waiting for BeginFrame because the output is
627   // mocked out.
628   settings_.refresh_rate = 200.0;
629   if (impl_side_painting) {
630     DCHECK(threaded)
631         << "Don't run single thread + impl side painting, it doesn't exist.";
632     settings_.impl_side_painting = true;
633   }
634   InitializeSettings(&settings_);
635
636   main_task_runner_->PostTask(
637       FROM_HERE,
638       base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
639
640   if (timeout_seconds_) {
641     timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
642     main_task_runner_->PostDelayedTask(
643         FROM_HERE,
644         timeout_.callback(),
645         base::TimeDelta::FromSeconds(timeout_seconds_));
646   }
647
648   base::MessageLoop::current()->Run();
649   DestroyLayerTreeHost();
650
651   timeout_.Cancel();
652
653   ASSERT_FALSE(layer_tree_host_.get());
654   client_.reset();
655   if (timed_out_) {
656     FAIL() << "Test timed out";
657     return;
658   }
659   AfterTest();
660 }
661
662 void LayerTreeTest::RunTestWithImplSidePainting() {
663   RunTest(true, false, true);
664 }
665
666 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface(bool fallback) {
667   scoped_ptr<FakeOutputSurface> output_surface =
668       CreateFakeOutputSurface(fallback);
669   if (output_surface) {
670     DCHECK_EQ(delegating_renderer_,
671               output_surface->capabilities().delegated_rendering);
672   }
673   output_surface_ = output_surface.get();
674   return output_surface.PassAs<OutputSurface>();
675 }
676
677 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface(
678     bool fallback) {
679   if (delegating_renderer_)
680     return FakeOutputSurface::CreateDelegating3d();
681   else
682     return FakeOutputSurface::Create3d();
683 }
684
685 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
686   return static_cast<TestContextProvider*>(
687       output_surface_->context_provider().get())->TestContext3d();
688 }
689
690 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
691     const {
692   if (impl->pending_tree())
693     return impl->pending_tree()->source_frame_number();
694   if (impl->active_tree())
695     return impl->active_tree()->source_frame_number();
696   // Source frames start at 0, so this is invalid.
697   return -1;
698 }
699
700 void LayerTreeTest::DestroyLayerTreeHost() {
701   if (layer_tree_host_ && layer_tree_host_->root_layer())
702     layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
703   layer_tree_host_.reset();
704 }
705
706 }  // namespace cc