Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / compositor / layer_unittest.cc
1 // Copyright (c) 2012 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 "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/compiler_specific.h"
8 #include "base/debug/trace_event.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "cc/layers/delegated_frame_provider.h"
18 #include "cc/layers/delegated_frame_resource_collection.h"
19 #include "cc/layers/layer.h"
20 #include "cc/output/copy_output_request.h"
21 #include "cc/output/copy_output_result.h"
22 #include "cc/output/delegated_frame_data.h"
23 #include "cc/test/pixel_test_utils.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/compositor/compositor_observer.h"
26 #include "ui/compositor/layer.h"
27 #include "ui/compositor/layer_animation_sequence.h"
28 #include "ui/compositor/layer_animator.h"
29 #include "ui/compositor/test/context_factories_for_test.h"
30 #include "ui/compositor/test/draw_waiter_for_test.h"
31 #include "ui/compositor/test/test_compositor_host.h"
32 #include "ui/compositor/test/test_layers.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/codec/png_codec.h"
35 #include "ui/gfx/gfx_paths.h"
36 #include "ui/gfx/skia_util.h"
37
38 using cc::MatchesPNGFile;
39
40 namespace ui {
41
42 namespace {
43
44 // There are three test classes in here that configure the Compositor and
45 // Layer's slightly differently:
46 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
47 //   is typically the base class you want to use.
48 // - LayerWithDelegateTest uses LayerDelegate on the delegates.
49 // - LayerWithRealCompositorTest when a real compositor is required for testing.
50 //    - Slow because they bring up a window and run the real compositor. This
51 //      is typically not what you want.
52
53 class ColoredLayer : public Layer, public LayerDelegate {
54  public:
55   explicit ColoredLayer(SkColor color)
56       : Layer(LAYER_TEXTURED),
57         color_(color) {
58     set_delegate(this);
59   }
60
61   virtual ~ColoredLayer() { }
62
63   // Overridden from LayerDelegate:
64   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
65     canvas->DrawColor(color_);
66   }
67
68   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
69   }
70
71   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
72     return base::Closure();
73   }
74
75  private:
76   SkColor color_;
77 };
78
79 class LayerWithRealCompositorTest : public testing::Test {
80  public:
81   LayerWithRealCompositorTest() {
82     if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
83       test_data_directory_ = test_data_directory_.AppendASCII("compositor");
84     } else {
85       LOG(ERROR) << "Could not open test data directory.";
86     }
87   }
88   virtual ~LayerWithRealCompositorTest() {}
89
90   // Overridden from testing::Test:
91   virtual void SetUp() OVERRIDE {
92     bool enable_pixel_output = true;
93     InitializeContextFactoryForTests(enable_pixel_output);
94     Compositor::Initialize();
95
96     const gfx::Rect host_bounds(10, 10, 500, 500);
97     compositor_host_.reset(TestCompositorHost::Create(host_bounds));
98     compositor_host_->Show();
99   }
100
101   virtual void TearDown() OVERRIDE {
102     compositor_host_.reset();
103     TerminateContextFactoryForTests();
104     Compositor::Terminate();
105   }
106
107   Compositor* GetCompositor() { return compositor_host_->GetCompositor(); }
108
109   Layer* CreateLayer(LayerType type) {
110     return new Layer(type);
111   }
112
113   Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) {
114     Layer* layer = new ColoredLayer(color);
115     layer->SetBounds(bounds);
116     return layer;
117   }
118
119   Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
120     Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
121     layer->SetBounds(bounds);
122     return layer;
123   }
124
125   void DrawTree(Layer* root) {
126     GetCompositor()->SetRootLayer(root);
127     GetCompositor()->ScheduleDraw();
128     WaitForDraw();
129   }
130
131   bool ReadPixels(SkBitmap* bitmap) {
132     return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
133   }
134
135   bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) {
136     scoped_refptr<ReadbackHolder> holder(new ReadbackHolder);
137     scoped_ptr<cc::CopyOutputRequest> request =
138         cc::CopyOutputRequest::CreateBitmapRequest(
139             base::Bind(&ReadbackHolder::OutputRequestCallback, holder));
140     request->set_area(source_rect);
141
142     GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass());
143
144     // Wait for copy response.  This needs to wait as the compositor could
145     // be in the middle of a draw right now, and the commit with the
146     // copy output request may not be done on the first draw.
147     for (int i = 0; i < 2; i++) {
148       GetCompositor()->ScheduleDraw();
149       WaitForDraw();
150     }
151
152     if (holder->completed()) {
153       *bitmap = holder->result();
154       return true;
155     }
156
157     // Callback never called.
158     NOTREACHED();
159     return false;
160   }
161
162   void WaitForDraw() {
163     ui::DrawWaiterForTest::Wait(GetCompositor());
164   }
165
166   void WaitForCommit() {
167     ui::DrawWaiterForTest::WaitForCommit(GetCompositor());
168   }
169
170   // Invalidates the entire contents of the layer.
171   void SchedulePaintForLayer(Layer* layer) {
172     layer->SchedulePaint(
173         gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
174   }
175
176   const base::FilePath& test_data_directory() const {
177     return test_data_directory_;
178   }
179
180  private:
181   class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> {
182    public:
183     ReadbackHolder() : completed_(false) {}
184
185     void OutputRequestCallback(scoped_ptr<cc::CopyOutputResult> result) {
186       DCHECK(!completed_);
187       result_ = result->TakeBitmap();
188       completed_ = true;
189     }
190     bool completed() const {
191       return completed_;
192     };
193     const SkBitmap& result() const { return *result_; }
194
195    private:
196     friend class base::RefCountedThreadSafe<ReadbackHolder>;
197
198     virtual ~ReadbackHolder() {}
199
200     scoped_ptr<SkBitmap> result_;
201     bool completed_;
202   };
203
204   scoped_ptr<TestCompositorHost> compositor_host_;
205
206   // The root directory for test files.
207   base::FilePath test_data_directory_;
208
209   DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
210 };
211
212 // LayerDelegate that paints colors to the layer.
213 class TestLayerDelegate : public LayerDelegate {
214  public:
215   explicit TestLayerDelegate() { reset(); }
216   virtual ~TestLayerDelegate() {}
217
218   void AddColor(SkColor color) {
219     colors_.push_back(color);
220   }
221
222   const gfx::Size& paint_size() const { return paint_size_; }
223   int color_index() const { return color_index_; }
224
225   std::string ToScaleString() const {
226     return base::StringPrintf("%.1f %.1f", scale_x_, scale_y_);
227   }
228
229   float device_scale_factor() const {
230     return device_scale_factor_;
231   }
232
233   // Overridden from LayerDelegate:
234   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
235     SkISize size = canvas->sk_canvas()->getBaseLayerSize();
236     paint_size_ = gfx::Size(size.width(), size.height());
237     canvas->FillRect(gfx::Rect(paint_size_), colors_[color_index_]);
238     color_index_ = (color_index_ + 1) % static_cast<int>(colors_.size());
239     const SkMatrix& matrix = canvas->sk_canvas()->getTotalMatrix();
240     scale_x_ = matrix.getScaleX();
241     scale_y_ = matrix.getScaleY();
242   }
243
244   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
245     device_scale_factor_ = device_scale_factor;
246   }
247
248   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
249     return base::Closure();
250   }
251
252   void reset() {
253     color_index_ = 0;
254     paint_size_.SetSize(0, 0);
255     scale_x_ = scale_y_ = 0.0f;
256     device_scale_factor_ = 0.0f;
257   }
258
259  private:
260   std::vector<SkColor> colors_;
261   int color_index_;
262   gfx::Size paint_size_;
263   float scale_x_;
264   float scale_y_;
265   float device_scale_factor_;
266
267   DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate);
268 };
269
270 // LayerDelegate that verifies that a layer was asked to update its canvas.
271 class DrawTreeLayerDelegate : public LayerDelegate {
272  public:
273   DrawTreeLayerDelegate() : painted_(false) {}
274   virtual ~DrawTreeLayerDelegate() {}
275
276   void Reset() {
277     painted_ = false;
278   }
279
280   bool painted() const { return painted_; }
281
282  private:
283   // Overridden from LayerDelegate:
284   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
285     painted_ = true;
286   }
287   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
288   }
289   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
290     return base::Closure();
291   }
292
293   bool painted_;
294
295   DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate);
296 };
297
298 // The simplest possible layer delegate. Does nothing.
299 class NullLayerDelegate : public LayerDelegate {
300  public:
301   NullLayerDelegate() {}
302   virtual ~NullLayerDelegate() {}
303
304  private:
305   // Overridden from LayerDelegate:
306   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
307   }
308   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
309   }
310   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
311     return base::Closure();
312   }
313
314   DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate);
315 };
316
317 // Remembers if it has been notified.
318 class TestCompositorObserver : public CompositorObserver {
319  public:
320   TestCompositorObserver()
321       : committed_(false), started_(false), ended_(false), aborted_(false) {}
322
323   bool committed() const { return committed_; }
324   bool notified() const { return started_ && ended_; }
325   bool aborted() const { return aborted_; }
326
327   void Reset() {
328     committed_ = false;
329     started_ = false;
330     ended_ = false;
331     aborted_ = false;
332   }
333
334  private:
335   virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE {
336     committed_ = true;
337   }
338
339   virtual void OnCompositingStarted(Compositor* compositor,
340                                     base::TimeTicks start_time) OVERRIDE {
341     started_ = true;
342   }
343
344   virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE {
345     ended_ = true;
346   }
347
348   virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {
349     aborted_ = true;
350   }
351
352   virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE {
353   }
354
355   bool committed_;
356   bool started_;
357   bool ended_;
358   bool aborted_;
359
360   DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver);
361 };
362
363 }  // namespace
364
365 TEST_F(LayerWithRealCompositorTest, Draw) {
366   scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED,
367                                            gfx::Rect(20, 20, 50, 50)));
368   DrawTree(layer.get());
369 }
370
371 // Create this hierarchy:
372 // L1 - red
373 // +-- L2 - blue
374 // |   +-- L3 - yellow
375 // +-- L4 - magenta
376 //
377 TEST_F(LayerWithRealCompositorTest, Hierarchy) {
378   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
379                                         gfx::Rect(20, 20, 400, 400)));
380   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
381                                         gfx::Rect(10, 10, 350, 350)));
382   scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
383                                         gfx::Rect(5, 5, 25, 25)));
384   scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA,
385                                         gfx::Rect(300, 300, 100, 100)));
386
387   l1->Add(l2.get());
388   l1->Add(l4.get());
389   l2->Add(l3.get());
390
391   DrawTree(l1.get());
392 }
393
394 class LayerWithDelegateTest : public testing::Test {
395  public:
396   LayerWithDelegateTest() {}
397   virtual ~LayerWithDelegateTest() {}
398
399   // Overridden from testing::Test:
400   virtual void SetUp() OVERRIDE {
401     bool enable_pixel_output = false;
402     InitializeContextFactoryForTests(enable_pixel_output);
403     Compositor::Initialize();
404
405     const gfx::Rect host_bounds(1000, 1000);
406     compositor_host_.reset(TestCompositorHost::Create(host_bounds));
407     compositor_host_->Show();
408   }
409
410   virtual void TearDown() OVERRIDE {
411     compositor_host_.reset();
412     TerminateContextFactoryForTests();
413     Compositor::Terminate();
414   }
415
416   Compositor* compositor() { return compositor_host_->GetCompositor(); }
417
418   virtual Layer* CreateLayer(LayerType type) {
419     return new Layer(type);
420   }
421
422   Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) {
423     Layer* layer = new ColoredLayer(color);
424     layer->SetBounds(bounds);
425     return layer;
426   }
427
428   virtual Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
429     Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
430     layer->SetBounds(bounds);
431     return layer;
432   }
433
434   void DrawTree(Layer* root) {
435     compositor()->SetRootLayer(root);
436     Draw();
437   }
438
439   // Invalidates the entire contents of the layer.
440   void SchedulePaintForLayer(Layer* layer) {
441     layer->SchedulePaint(
442         gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
443   }
444
445   // Invokes DrawTree on the compositor.
446   void Draw() {
447     compositor()->ScheduleDraw();
448     WaitForDraw();
449   }
450
451   void WaitForDraw() {
452     DrawWaiterForTest::Wait(compositor());
453   }
454
455   void WaitForCommit() {
456     DrawWaiterForTest::WaitForCommit(compositor());
457   }
458
459  private:
460   scoped_ptr<TestCompositorHost> compositor_host_;
461
462   DISALLOW_COPY_AND_ASSIGN(LayerWithDelegateTest);
463 };
464
465 // L1
466 //  +-- L2
467 TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Simple) {
468   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
469                                         gfx::Rect(20, 20, 400, 400)));
470   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
471                                         gfx::Rect(10, 10, 350, 350)));
472   l1->Add(l2.get());
473   DrawTree(l1.get());
474
475   gfx::Point point1_in_l2_coords(5, 5);
476   Layer::ConvertPointToLayer(l2.get(), l1.get(), &point1_in_l2_coords);
477   gfx::Point point1_in_l1_coords(15, 15);
478   EXPECT_EQ(point1_in_l1_coords, point1_in_l2_coords);
479
480   gfx::Point point2_in_l1_coords(5, 5);
481   Layer::ConvertPointToLayer(l1.get(), l2.get(), &point2_in_l1_coords);
482   gfx::Point point2_in_l2_coords(-5, -5);
483   EXPECT_EQ(point2_in_l2_coords, point2_in_l1_coords);
484 }
485
486 // L1
487 //  +-- L2
488 //       +-- L3
489 TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Medium) {
490   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
491                                         gfx::Rect(20, 20, 400, 400)));
492   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
493                                         gfx::Rect(10, 10, 350, 350)));
494   scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
495                                         gfx::Rect(10, 10, 100, 100)));
496   l1->Add(l2.get());
497   l2->Add(l3.get());
498   DrawTree(l1.get());
499
500   gfx::Point point1_in_l3_coords(5, 5);
501   Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords);
502   gfx::Point point1_in_l1_coords(25, 25);
503   EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords);
504
505   gfx::Point point2_in_l1_coords(5, 5);
506   Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords);
507   gfx::Point point2_in_l3_coords(-15, -15);
508   EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords);
509 }
510
511 TEST_F(LayerWithRealCompositorTest, Delegate) {
512   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK,
513                                         gfx::Rect(20, 20, 400, 400)));
514   GetCompositor()->SetRootLayer(l1.get());
515   WaitForDraw();
516
517   TestLayerDelegate delegate;
518   l1->set_delegate(&delegate);
519   delegate.AddColor(SK_ColorWHITE);
520   delegate.AddColor(SK_ColorYELLOW);
521   delegate.AddColor(SK_ColorGREEN);
522
523   l1->SchedulePaint(gfx::Rect(0, 0, 400, 400));
524   WaitForDraw();
525
526   EXPECT_EQ(delegate.color_index(), 1);
527   EXPECT_EQ(delegate.paint_size(), l1->bounds().size());
528
529   l1->SchedulePaint(gfx::Rect(10, 10, 200, 200));
530   WaitForDraw();
531   EXPECT_EQ(delegate.color_index(), 2);
532   EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200));
533
534   l1->SchedulePaint(gfx::Rect(5, 5, 50, 50));
535   WaitForDraw();
536   EXPECT_EQ(delegate.color_index(), 0);
537   EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50));
538 }
539
540 TEST_F(LayerWithRealCompositorTest, DrawTree) {
541   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
542                                         gfx::Rect(20, 20, 400, 400)));
543   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
544                                         gfx::Rect(10, 10, 350, 350)));
545   scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
546                                         gfx::Rect(10, 10, 100, 100)));
547   l1->Add(l2.get());
548   l2->Add(l3.get());
549
550   GetCompositor()->SetRootLayer(l1.get());
551   WaitForDraw();
552
553   DrawTreeLayerDelegate d1;
554   l1->set_delegate(&d1);
555   DrawTreeLayerDelegate d2;
556   l2->set_delegate(&d2);
557   DrawTreeLayerDelegate d3;
558   l3->set_delegate(&d3);
559
560   l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
561   WaitForDraw();
562   EXPECT_FALSE(d1.painted());
563   EXPECT_TRUE(d2.painted());
564   EXPECT_FALSE(d3.painted());
565 }
566
567 // Tests no-texture Layers.
568 // Create this hierarchy:
569 // L1 - red
570 // +-- L2 - NO TEXTURE
571 // |   +-- L3 - yellow
572 // +-- L4 - magenta
573 //
574 TEST_F(LayerWithRealCompositorTest, HierarchyNoTexture) {
575   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
576                                         gfx::Rect(20, 20, 400, 400)));
577   scoped_ptr<Layer> l2(CreateNoTextureLayer(gfx::Rect(10, 10, 350, 350)));
578   scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
579                                         gfx::Rect(5, 5, 25, 25)));
580   scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA,
581                                         gfx::Rect(300, 300, 100, 100)));
582
583   l1->Add(l2.get());
584   l1->Add(l4.get());
585   l2->Add(l3.get());
586
587   GetCompositor()->SetRootLayer(l1.get());
588   WaitForDraw();
589
590   DrawTreeLayerDelegate d2;
591   l2->set_delegate(&d2);
592   DrawTreeLayerDelegate d3;
593   l3->set_delegate(&d3);
594
595   l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
596   l3->SchedulePaint(gfx::Rect(5, 5, 5, 5));
597   WaitForDraw();
598
599   // |d2| should not have received a paint notification since it has no texture.
600   EXPECT_FALSE(d2.painted());
601   // |d3| should have received a paint notification.
602   EXPECT_TRUE(d3.painted());
603 }
604
605 class LayerWithNullDelegateTest : public LayerWithDelegateTest {
606  public:
607   LayerWithNullDelegateTest() {}
608   virtual ~LayerWithNullDelegateTest() {}
609
610   virtual void SetUp() OVERRIDE {
611     LayerWithDelegateTest::SetUp();
612     default_layer_delegate_.reset(new NullLayerDelegate());
613   }
614
615   virtual Layer* CreateLayer(LayerType type) OVERRIDE {
616     Layer* layer = new Layer(type);
617     layer->set_delegate(default_layer_delegate_.get());
618     return layer;
619   }
620
621   Layer* CreateTextureRootLayer(const gfx::Rect& bounds) {
622     Layer* layer = CreateTextureLayer(bounds);
623     compositor()->SetRootLayer(layer);
624     return layer;
625   }
626
627   Layer* CreateTextureLayer(const gfx::Rect& bounds) {
628     Layer* layer = CreateLayer(LAYER_TEXTURED);
629     layer->SetBounds(bounds);
630     return layer;
631   }
632
633   virtual Layer* CreateNoTextureLayer(const gfx::Rect& bounds) OVERRIDE {
634     Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
635     layer->SetBounds(bounds);
636     return layer;
637   }
638
639  private:
640   scoped_ptr<NullLayerDelegate> default_layer_delegate_;
641
642   DISALLOW_COPY_AND_ASSIGN(LayerWithNullDelegateTest);
643 };
644
645 TEST_F(LayerWithNullDelegateTest, EscapedDebugNames) {
646   scoped_ptr<Layer> layer(CreateLayer(LAYER_NOT_DRAWN));
647   std::string name = "\"\'\\/\b\f\n\r\t\n";
648   layer->set_name(name);
649   scoped_refptr<base::debug::ConvertableToTraceFormat> debug_info =
650     layer->TakeDebugInfo();
651   EXPECT_TRUE(!!debug_info);
652   std::string json;
653   debug_info->AppendAsTraceFormat(&json);
654   base::JSONReader json_reader;
655   scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(json));
656   EXPECT_TRUE(!!debug_info_value);
657   EXPECT_TRUE(debug_info_value->IsType(base::Value::TYPE_DICTIONARY));
658   base::DictionaryValue* dictionary = 0;
659   EXPECT_TRUE(debug_info_value->GetAsDictionary(&dictionary));
660   std::string roundtrip;
661   EXPECT_TRUE(dictionary->GetString("layer_name", &roundtrip));
662   EXPECT_EQ(name, roundtrip);
663 }
664
665 void ReturnMailbox(bool* run, uint32 sync_point, bool is_lost) {
666   *run = true;
667 }
668
669 TEST_F(LayerWithNullDelegateTest, SwitchLayerPreservesCCLayerState) {
670   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
671                                         gfx::Rect(20, 20, 400, 400)));
672   l1->SetFillsBoundsOpaquely(true);
673   l1->SetForceRenderSurface(true);
674   l1->SetVisible(false);
675
676   EXPECT_EQ(gfx::PointF().ToString(),
677             l1->cc_layer()->anchor_point().ToString());
678   EXPECT_TRUE(l1->cc_layer()->DrawsContent());
679   EXPECT_TRUE(l1->cc_layer()->contents_opaque());
680   EXPECT_TRUE(l1->cc_layer()->force_render_surface());
681   EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
682
683   cc::Layer* before_layer = l1->cc_layer();
684
685   bool callback1_run = false;
686   cc::TextureMailbox mailbox(gpu::Mailbox::Generate(), 0, 0);
687   l1->SetTextureMailbox(mailbox,
688                         cc::SingleReleaseCallback::Create(
689                             base::Bind(ReturnMailbox, &callback1_run)),
690                         gfx::Size(1, 1));
691
692   EXPECT_NE(before_layer, l1->cc_layer());
693
694   EXPECT_EQ(gfx::PointF().ToString(),
695             l1->cc_layer()->anchor_point().ToString());
696   EXPECT_TRUE(l1->cc_layer()->DrawsContent());
697   EXPECT_TRUE(l1->cc_layer()->contents_opaque());
698   EXPECT_TRUE(l1->cc_layer()->force_render_surface());
699   EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
700   EXPECT_FALSE(callback1_run);
701
702   bool callback2_run = false;
703   mailbox = cc::TextureMailbox(gpu::Mailbox::Generate(), 0, 0);
704   l1->SetTextureMailbox(mailbox,
705                         cc::SingleReleaseCallback::Create(
706                             base::Bind(ReturnMailbox, &callback2_run)),
707                         gfx::Size(1, 1));
708   EXPECT_TRUE(callback1_run);
709   EXPECT_FALSE(callback2_run);
710
711   l1->SetShowPaintedContent();
712   EXPECT_EQ(gfx::PointF().ToString(),
713             l1->cc_layer()->anchor_point().ToString());
714   EXPECT_TRUE(l1->cc_layer()->DrawsContent());
715   EXPECT_TRUE(l1->cc_layer()->contents_opaque());
716   EXPECT_TRUE(l1->cc_layer()->force_render_surface());
717   EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
718   EXPECT_TRUE(callback2_run);
719 }
720
721 // Various visibile/drawn assertions.
722 TEST_F(LayerWithNullDelegateTest, Visibility) {
723   scoped_ptr<Layer> l1(new Layer(LAYER_TEXTURED));
724   scoped_ptr<Layer> l2(new Layer(LAYER_TEXTURED));
725   scoped_ptr<Layer> l3(new Layer(LAYER_TEXTURED));
726   l1->Add(l2.get());
727   l2->Add(l3.get());
728
729   NullLayerDelegate delegate;
730   l1->set_delegate(&delegate);
731   l2->set_delegate(&delegate);
732   l3->set_delegate(&delegate);
733
734   // Layers should initially be drawn.
735   EXPECT_TRUE(l1->IsDrawn());
736   EXPECT_TRUE(l2->IsDrawn());
737   EXPECT_TRUE(l3->IsDrawn());
738   EXPECT_FALSE(l1->cc_layer()->hide_layer_and_subtree());
739   EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
740   EXPECT_FALSE(l3->cc_layer()->hide_layer_and_subtree());
741
742   compositor()->SetRootLayer(l1.get());
743
744   Draw();
745
746   l1->SetVisible(false);
747   EXPECT_FALSE(l1->IsDrawn());
748   EXPECT_FALSE(l2->IsDrawn());
749   EXPECT_FALSE(l3->IsDrawn());
750   EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
751   EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
752   EXPECT_FALSE(l3->cc_layer()->hide_layer_and_subtree());
753
754   l3->SetVisible(false);
755   EXPECT_FALSE(l1->IsDrawn());
756   EXPECT_FALSE(l2->IsDrawn());
757   EXPECT_FALSE(l3->IsDrawn());
758   EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
759   EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
760   EXPECT_TRUE(l3->cc_layer()->hide_layer_and_subtree());
761
762   l1->SetVisible(true);
763   EXPECT_TRUE(l1->IsDrawn());
764   EXPECT_TRUE(l2->IsDrawn());
765   EXPECT_FALSE(l3->IsDrawn());
766   EXPECT_FALSE(l1->cc_layer()->hide_layer_and_subtree());
767   EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
768   EXPECT_TRUE(l3->cc_layer()->hide_layer_and_subtree());
769 }
770
771 // Checks that stacking-related methods behave as advertised.
772 TEST_F(LayerWithNullDelegateTest, Stacking) {
773   scoped_ptr<Layer> root(new Layer(LAYER_NOT_DRAWN));
774   scoped_ptr<Layer> l1(new Layer(LAYER_TEXTURED));
775   scoped_ptr<Layer> l2(new Layer(LAYER_TEXTURED));
776   scoped_ptr<Layer> l3(new Layer(LAYER_TEXTURED));
777   l1->set_name("1");
778   l2->set_name("2");
779   l3->set_name("3");
780   root->Add(l3.get());
781   root->Add(l2.get());
782   root->Add(l1.get());
783
784   // Layers' children are stored in bottom-to-top order.
785   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
786
787   root->StackAtTop(l3.get());
788   EXPECT_EQ("2 1 3", test::ChildLayerNamesAsString(*root.get()));
789
790   root->StackAtTop(l1.get());
791   EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
792
793   root->StackAtTop(l1.get());
794   EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
795
796   root->StackAbove(l2.get(), l3.get());
797   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
798
799   root->StackAbove(l1.get(), l3.get());
800   EXPECT_EQ("3 1 2", test::ChildLayerNamesAsString(*root.get()));
801
802   root->StackAbove(l2.get(), l1.get());
803   EXPECT_EQ("3 1 2", test::ChildLayerNamesAsString(*root.get()));
804
805   root->StackAtBottom(l2.get());
806   EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
807
808   root->StackAtBottom(l3.get());
809   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
810
811   root->StackAtBottom(l3.get());
812   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
813
814   root->StackBelow(l2.get(), l3.get());
815   EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
816
817   root->StackBelow(l1.get(), l3.get());
818   EXPECT_EQ("2 1 3", test::ChildLayerNamesAsString(*root.get()));
819
820   root->StackBelow(l3.get(), l2.get());
821   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
822
823   root->StackBelow(l3.get(), l2.get());
824   EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
825
826   root->StackBelow(l3.get(), l1.get());
827   EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
828 }
829
830 // Verifies SetBounds triggers the appropriate painting/drawing.
831 TEST_F(LayerWithNullDelegateTest, SetBoundsSchedulesPaint) {
832   scoped_ptr<Layer> l1(CreateTextureLayer(gfx::Rect(0, 0, 200, 200)));
833   compositor()->SetRootLayer(l1.get());
834
835   Draw();
836
837   l1->SetBounds(gfx::Rect(5, 5, 200, 200));
838
839   // The CompositorDelegate (us) should have been told to draw for a move.
840   WaitForDraw();
841
842   l1->SetBounds(gfx::Rect(5, 5, 100, 100));
843
844   // The CompositorDelegate (us) should have been told to draw for a resize.
845   WaitForDraw();
846 }
847
848 // Checks that pixels are actually drawn to the screen with a read back.
849 TEST_F(LayerWithRealCompositorTest, DrawPixels) {
850   gfx::Size viewport_size = GetCompositor()->size();
851
852   // The window should be some non-trivial size but may not be exactly
853   // 500x500 on all platforms/bots.
854   EXPECT_GE(viewport_size.width(), 200);
855   EXPECT_GE(viewport_size.height(), 200);
856
857   int blue_height = 10;
858
859   scoped_ptr<Layer> layer(
860       CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size)));
861   scoped_ptr<Layer> layer2(
862       CreateColorLayer(SK_ColorBLUE,
863                        gfx::Rect(0, 0, viewport_size.width(), blue_height)));
864
865   layer->Add(layer2.get());
866
867   DrawTree(layer.get());
868
869   SkBitmap bitmap;
870   ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size)));
871   ASSERT_FALSE(bitmap.empty());
872
873   SkAutoLockPixels lock(bitmap);
874   for (int x = 0; x < viewport_size.width(); x++) {
875     for (int y = 0; y < viewport_size.height(); y++) {
876       SkColor actual_color = bitmap.getColor(x, y);
877       SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED;
878       EXPECT_EQ(expected_color, actual_color)
879           << "Pixel error at x=" << x << " y=" << y << "; "
880           << "actual RGBA=("
881           << SkColorGetR(actual_color) << ","
882           << SkColorGetG(actual_color) << ","
883           << SkColorGetB(actual_color) << ","
884           << SkColorGetA(actual_color) << "); "
885           << "expected RGBA=("
886           << SkColorGetR(expected_color) << ","
887           << SkColorGetG(expected_color) << ","
888           << SkColorGetB(expected_color) << ","
889           << SkColorGetA(expected_color) << ")";
890     }
891   }
892 }
893
894 // Checks the logic around Compositor::SetRootLayer and Layer::SetCompositor.
895 TEST_F(LayerWithRealCompositorTest, SetRootLayer) {
896   Compositor* compositor = GetCompositor();
897   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
898                                         gfx::Rect(20, 20, 400, 400)));
899   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
900                                         gfx::Rect(10, 10, 350, 350)));
901
902   EXPECT_EQ(NULL, l1->GetCompositor());
903   EXPECT_EQ(NULL, l2->GetCompositor());
904
905   compositor->SetRootLayer(l1.get());
906   EXPECT_EQ(compositor, l1->GetCompositor());
907
908   l1->Add(l2.get());
909   EXPECT_EQ(compositor, l2->GetCompositor());
910
911   l1->Remove(l2.get());
912   EXPECT_EQ(NULL, l2->GetCompositor());
913
914   l1->Add(l2.get());
915   EXPECT_EQ(compositor, l2->GetCompositor());
916
917   compositor->SetRootLayer(NULL);
918   EXPECT_EQ(NULL, l1->GetCompositor());
919   EXPECT_EQ(NULL, l2->GetCompositor());
920 }
921
922 // Checks that compositor observers are notified when:
923 // - DrawTree is called,
924 // - After ScheduleDraw is called, or
925 // - Whenever SetBounds, SetOpacity or SetTransform are called.
926 // TODO(vollick): could be reorganized into compositor_unittest.cc
927 TEST_F(LayerWithRealCompositorTest, CompositorObservers) {
928   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
929                                         gfx::Rect(20, 20, 400, 400)));
930   scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
931                                         gfx::Rect(10, 10, 350, 350)));
932   l1->Add(l2.get());
933   TestCompositorObserver observer;
934   GetCompositor()->AddObserver(&observer);
935
936   // Explicitly called DrawTree should cause the observers to be notified.
937   // NOTE: this call to DrawTree sets l1 to be the compositor's root layer.
938   DrawTree(l1.get());
939   EXPECT_TRUE(observer.notified());
940
941   // ScheduleDraw without any visible change should cause a commit.
942   observer.Reset();
943   l1->ScheduleDraw();
944   WaitForCommit();
945   EXPECT_TRUE(observer.committed());
946
947   // Moving, but not resizing, a layer should alert the observers.
948   observer.Reset();
949   l2->SetBounds(gfx::Rect(0, 0, 350, 350));
950   WaitForDraw();
951   EXPECT_TRUE(observer.notified());
952
953   // So should resizing a layer.
954   observer.Reset();
955   l2->SetBounds(gfx::Rect(0, 0, 400, 400));
956   WaitForDraw();
957   EXPECT_TRUE(observer.notified());
958
959   // Opacity changes should alert the observers.
960   observer.Reset();
961   l2->SetOpacity(0.5f);
962   WaitForDraw();
963   EXPECT_TRUE(observer.notified());
964
965   // So should setting the opacity back.
966   observer.Reset();
967   l2->SetOpacity(1.0f);
968   WaitForDraw();
969   EXPECT_TRUE(observer.notified());
970
971   // Setting the transform of a layer should alert the observers.
972   observer.Reset();
973   gfx::Transform transform;
974   transform.Translate(200.0, 200.0);
975   transform.Rotate(90.0);
976   transform.Translate(-200.0, -200.0);
977   l2->SetTransform(transform);
978   WaitForDraw();
979   EXPECT_TRUE(observer.notified());
980
981   // A change resulting in an aborted swap buffer should alert the observer
982   // and also signal an abort.
983   observer.Reset();
984   l2->SetOpacity(0.1f);
985   GetCompositor()->DidAbortSwapBuffers();
986   WaitForDraw();
987   EXPECT_TRUE(observer.notified());
988   EXPECT_TRUE(observer.aborted());
989
990   GetCompositor()->RemoveObserver(&observer);
991
992   // Opacity changes should no longer alert the removed observer.
993   observer.Reset();
994   l2->SetOpacity(0.5f);
995   WaitForDraw();
996
997   EXPECT_FALSE(observer.notified());
998 }
999
1000 // Checks that modifying the hierarchy correctly affects final composite.
1001 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) {
1002   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1003
1004   // l0
1005   //  +-l11
1006   //  | +-l21
1007   //  +-l12
1008   scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1009                                         gfx::Rect(0, 0, 50, 50)));
1010   scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1011                                          gfx::Rect(0, 0, 25, 25)));
1012   scoped_ptr<Layer> l21(CreateColorLayer(SK_ColorMAGENTA,
1013                                          gfx::Rect(0, 0, 15, 15)));
1014   scoped_ptr<Layer> l12(CreateColorLayer(SK_ColorBLUE,
1015                                          gfx::Rect(10, 10, 25, 25)));
1016
1017   base::FilePath ref_img1 =
1018       test_data_directory().AppendASCII("ModifyHierarchy1.png");
1019   base::FilePath ref_img2 =
1020       test_data_directory().AppendASCII("ModifyHierarchy2.png");
1021   SkBitmap bitmap;
1022
1023   l0->Add(l11.get());
1024   l11->Add(l21.get());
1025   l0->Add(l12.get());
1026   DrawTree(l0.get());
1027   ASSERT_TRUE(ReadPixels(&bitmap));
1028   ASSERT_FALSE(bitmap.empty());
1029   // WritePNGFile(bitmap, ref_img1);
1030   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1031
1032   l0->StackAtTop(l11.get());
1033   DrawTree(l0.get());
1034   ASSERT_TRUE(ReadPixels(&bitmap));
1035   ASSERT_FALSE(bitmap.empty());
1036   // WritePNGFile(bitmap, ref_img2);
1037   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1038
1039   // should restore to original configuration
1040   l0->StackAbove(l12.get(), l11.get());
1041   DrawTree(l0.get());
1042   ASSERT_TRUE(ReadPixels(&bitmap));
1043   ASSERT_FALSE(bitmap.empty());
1044   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1045
1046   // l11 back to front
1047   l0->StackAtTop(l11.get());
1048   DrawTree(l0.get());
1049   ASSERT_TRUE(ReadPixels(&bitmap));
1050   ASSERT_FALSE(bitmap.empty());
1051   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1052
1053   // should restore to original configuration
1054   l0->StackAbove(l12.get(), l11.get());
1055   DrawTree(l0.get());
1056   ASSERT_TRUE(ReadPixels(&bitmap));
1057   ASSERT_FALSE(bitmap.empty());
1058   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1059
1060   // l11 back to front
1061   l0->StackAbove(l11.get(), l12.get());
1062   DrawTree(l0.get());
1063   ASSERT_TRUE(ReadPixels(&bitmap));
1064   ASSERT_FALSE(bitmap.empty());
1065   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1066 }
1067
1068 // Opacity is rendered correctly.
1069 // Checks that modifying the hierarchy correctly affects final composite.
1070 TEST_F(LayerWithRealCompositorTest, Opacity) {
1071   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1072
1073   // l0
1074   //  +-l11
1075   scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1076                                         gfx::Rect(0, 0, 50, 50)));
1077   scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1078                                          gfx::Rect(0, 0, 25, 25)));
1079
1080   base::FilePath ref_img = test_data_directory().AppendASCII("Opacity.png");
1081
1082   l11->SetOpacity(0.75);
1083   l0->Add(l11.get());
1084   DrawTree(l0.get());
1085   SkBitmap bitmap;
1086   ASSERT_TRUE(ReadPixels(&bitmap));
1087   ASSERT_FALSE(bitmap.empty());
1088   // WritePNGFile(bitmap, ref_img);
1089   EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1090 }
1091
1092 namespace {
1093
1094 class SchedulePaintLayerDelegate : public LayerDelegate {
1095  public:
1096   SchedulePaintLayerDelegate() : paint_count_(0), layer_(NULL) {}
1097
1098   virtual ~SchedulePaintLayerDelegate() {}
1099
1100   void set_layer(Layer* layer) {
1101     layer_ = layer;
1102     layer_->set_delegate(this);
1103   }
1104
1105   void SetSchedulePaintRect(const gfx::Rect& rect) {
1106     schedule_paint_rect_ = rect;
1107   }
1108
1109   int GetPaintCountAndClear() {
1110     int value = paint_count_;
1111     paint_count_ = 0;
1112     return value;
1113   }
1114
1115   const gfx::RectF& last_clip_rect() const { return last_clip_rect_; }
1116
1117  private:
1118   // Overridden from LayerDelegate:
1119   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
1120     paint_count_++;
1121     if (!schedule_paint_rect_.IsEmpty()) {
1122       layer_->SchedulePaint(schedule_paint_rect_);
1123       schedule_paint_rect_ = gfx::Rect();
1124     }
1125     SkRect sk_clip_rect;
1126     if (canvas->sk_canvas()->getClipBounds(&sk_clip_rect))
1127       last_clip_rect_ = gfx::SkRectToRectF(sk_clip_rect);
1128   }
1129
1130   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
1131   }
1132
1133   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
1134     return base::Closure();
1135   }
1136
1137   int paint_count_;
1138   Layer* layer_;
1139   gfx::Rect schedule_paint_rect_;
1140   gfx::RectF last_clip_rect_;
1141
1142   DISALLOW_COPY_AND_ASSIGN(SchedulePaintLayerDelegate);
1143 };
1144
1145 }  // namespace
1146
1147 // Verifies that if SchedulePaint is invoked during painting the layer is still
1148 // marked dirty.
1149 TEST_F(LayerWithDelegateTest, SchedulePaintFromOnPaintLayer) {
1150   scoped_ptr<Layer> root(CreateColorLayer(SK_ColorRED,
1151                                           gfx::Rect(0, 0, 500, 500)));
1152   SchedulePaintLayerDelegate child_delegate;
1153   scoped_ptr<Layer> child(CreateColorLayer(SK_ColorBLUE,
1154                                            gfx::Rect(0, 0, 200, 200)));
1155   child_delegate.set_layer(child.get());
1156
1157   root->Add(child.get());
1158
1159   SchedulePaintForLayer(root.get());
1160   DrawTree(root.get());
1161   child->SchedulePaint(gfx::Rect(0, 0, 20, 20));
1162   EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1163
1164   // Set a rect so that when OnPaintLayer() is invoked SchedulePaint is invoked
1165   // again.
1166   child_delegate.SetSchedulePaintRect(gfx::Rect(10, 10, 30, 30));
1167   WaitForCommit();
1168   EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1169
1170   // Because SchedulePaint() was invoked from OnPaintLayer() |child| should
1171   // still need to be painted.
1172   WaitForCommit();
1173   EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1174   EXPECT_TRUE(child_delegate.last_clip_rect().Contains(
1175                   gfx::Rect(10, 10, 30, 30)));
1176 }
1177
1178 TEST_F(LayerWithRealCompositorTest, ScaleUpDown) {
1179   scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1180                                           gfx::Rect(10, 20, 200, 220)));
1181   TestLayerDelegate root_delegate;
1182   root_delegate.AddColor(SK_ColorWHITE);
1183   root->set_delegate(&root_delegate);
1184
1185   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1186                                         gfx::Rect(10, 20, 140, 180)));
1187   TestLayerDelegate l1_delegate;
1188   l1_delegate.AddColor(SK_ColorWHITE);
1189   l1->set_delegate(&l1_delegate);
1190
1191   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1192   GetCompositor()->SetRootLayer(root.get());
1193   root->Add(l1.get());
1194   WaitForDraw();
1195
1196   EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1197   EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1198   gfx::Size size_in_pixel = root->cc_layer()->bounds();
1199   EXPECT_EQ("200x220", size_in_pixel.ToString());
1200   size_in_pixel = l1->cc_layer()->bounds();
1201   EXPECT_EQ("140x180", size_in_pixel.ToString());
1202   // No scale change, so no scale notification.
1203   EXPECT_EQ(0.0f, root_delegate.device_scale_factor());
1204   EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1205
1206   EXPECT_EQ("200x220", root_delegate.paint_size().ToString());
1207   EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1208
1209   // Scale up to 2.0. Changing scale doesn't change the bounds in DIP.
1210   GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1211   EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1212   EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1213   // Pixel size must have been scaled up.
1214   size_in_pixel = root->cc_layer()->bounds();
1215   EXPECT_EQ("400x440", size_in_pixel.ToString());
1216   size_in_pixel = l1->cc_layer()->bounds();
1217   EXPECT_EQ("280x360", size_in_pixel.ToString());
1218   // New scale factor must have been notified.
1219   EXPECT_EQ(2.0f, root_delegate.device_scale_factor());
1220   EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1221
1222   // Canvas size must have been scaled down up.
1223   WaitForDraw();
1224   EXPECT_EQ("400x440", root_delegate.paint_size().ToString());
1225   EXPECT_EQ("2.0 2.0", root_delegate.ToScaleString());
1226   EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1227   EXPECT_EQ("2.0 2.0", l1_delegate.ToScaleString());
1228
1229   // Scale down back to 1.0f.
1230   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1231   EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1232   EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1233   // Pixel size must have been scaled down.
1234   size_in_pixel = root->cc_layer()->bounds();
1235   EXPECT_EQ("200x220", size_in_pixel.ToString());
1236   size_in_pixel = l1->cc_layer()->bounds();
1237   EXPECT_EQ("140x180", size_in_pixel.ToString());
1238   // New scale factor must have been notified.
1239   EXPECT_EQ(1.0f, root_delegate.device_scale_factor());
1240   EXPECT_EQ(1.0f, l1_delegate.device_scale_factor());
1241
1242   // Canvas size must have been scaled down too.
1243   WaitForDraw();
1244   EXPECT_EQ("200x220", root_delegate.paint_size().ToString());
1245   EXPECT_EQ("1.0 1.0", root_delegate.ToScaleString());
1246   EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1247   EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1248
1249   root_delegate.reset();
1250   l1_delegate.reset();
1251   // Just changing the size shouldn't notify the scale change nor
1252   // trigger repaint.
1253   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(1000, 1000));
1254   // No scale change, so no scale notification.
1255   EXPECT_EQ(0.0f, root_delegate.device_scale_factor());
1256   EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1257   WaitForDraw();
1258   EXPECT_EQ("0x0", root_delegate.paint_size().ToString());
1259   EXPECT_EQ("0.0 0.0", root_delegate.ToScaleString());
1260   EXPECT_EQ("0x0", l1_delegate.paint_size().ToString());
1261   EXPECT_EQ("0.0 0.0", l1_delegate.ToScaleString());
1262 }
1263
1264 TEST_F(LayerWithRealCompositorTest, ScaleReparent) {
1265   scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1266                                           gfx::Rect(10, 20, 200, 220)));
1267   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1268                                         gfx::Rect(10, 20, 140, 180)));
1269   TestLayerDelegate l1_delegate;
1270   l1_delegate.AddColor(SK_ColorWHITE);
1271   l1->set_delegate(&l1_delegate);
1272
1273   GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1274   GetCompositor()->SetRootLayer(root.get());
1275   WaitForDraw();
1276
1277   root->Add(l1.get());
1278   EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1279   gfx::Size size_in_pixel = l1->cc_layer()->bounds();
1280   EXPECT_EQ("140x180", size_in_pixel.ToString());
1281   EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1282
1283   WaitForDraw();
1284   EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1285   EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1286
1287   // Remove l1 from root and change the scale.
1288   root->Remove(l1.get());
1289   EXPECT_EQ(NULL, l1->parent());
1290   EXPECT_EQ(NULL, l1->GetCompositor());
1291   GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1292   // Sanity check on root and l1.
1293   EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1294   size_in_pixel = l1->cc_layer()->bounds();
1295   EXPECT_EQ("140x180", size_in_pixel.ToString());
1296
1297
1298   root->Add(l1.get());
1299   EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1300   size_in_pixel = l1->cc_layer()->bounds();
1301   EXPECT_EQ("280x360", size_in_pixel.ToString());
1302   EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1303   WaitForDraw();
1304   EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1305   EXPECT_EQ("2.0 2.0", l1_delegate.ToScaleString());
1306 }
1307
1308 // Tests layer::set_scale_content(false).
1309 TEST_F(LayerWithRealCompositorTest, NoScaleCanvas) {
1310   scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1311                                           gfx::Rect(10, 20, 200, 220)));
1312   scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1313                                         gfx::Rect(10, 20, 140, 180)));
1314   l1->set_scale_content(false);
1315   root->Add(l1.get());
1316   TestLayerDelegate l1_delegate;
1317   l1_delegate.AddColor(SK_ColorWHITE);
1318   l1->set_delegate(&l1_delegate);
1319
1320   GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1321   GetCompositor()->SetRootLayer(root.get());
1322   // Scale factor change is notified regardless of scale_content flag.
1323   EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1324
1325   WaitForDraw();
1326   EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1327   EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1328 }
1329
1330 // Verifies that when changing bounds on a layer that is invisible, and then
1331 // made visible, the right thing happens:
1332 // - if just a move, then no painting should happen.
1333 // - if a resize, the layer should be repainted.
1334 TEST_F(LayerWithDelegateTest, SetBoundsWhenInvisible) {
1335   scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1336
1337   scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1338   child->SetBounds(gfx::Rect(0, 0, 500, 500));
1339   DrawTreeLayerDelegate delegate;
1340   child->set_delegate(&delegate);
1341   root->Add(child.get());
1342
1343   // Paint once for initial damage.
1344   child->SetVisible(true);
1345   DrawTree(root.get());
1346
1347   // Reset into invisible state.
1348   child->SetVisible(false);
1349   DrawTree(root.get());
1350   delegate.Reset();
1351
1352   // Move layer.
1353   child->SetBounds(gfx::Rect(200, 200, 500, 500));
1354   child->SetVisible(true);
1355   DrawTree(root.get());
1356   EXPECT_FALSE(delegate.painted());
1357
1358   // Reset into invisible state.
1359   child->SetVisible(false);
1360   DrawTree(root.get());
1361   delegate.Reset();
1362
1363   // Resize layer.
1364   child->SetBounds(gfx::Rect(200, 200, 400, 400));
1365   child->SetVisible(true);
1366   DrawTree(root.get());
1367   EXPECT_TRUE(delegate.painted());
1368 }
1369
1370 static scoped_ptr<cc::DelegatedFrameData> MakeFrameData(gfx::Size size) {
1371   scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData);
1372   scoped_ptr<cc::RenderPass> render_pass(cc::RenderPass::Create());
1373   render_pass->SetNew(
1374       cc::RenderPass::Id(1, 1), gfx::Rect(size), gfx::Rect(), gfx::Transform());
1375   frame_data->render_pass_list.push_back(render_pass.Pass());
1376   return frame_data.Pass();
1377 }
1378
1379 TEST_F(LayerWithDelegateTest, DelegatedLayer) {
1380   scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1381
1382   scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1383
1384   child->SetBounds(gfx::Rect(0, 0, 10, 10));
1385   child->SetVisible(true);
1386   root->Add(child.get());
1387   DrawTree(root.get());
1388
1389   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection =
1390       new cc::DelegatedFrameResourceCollection;
1391   scoped_refptr<cc::DelegatedFrameProvider> frame_provider;
1392
1393   // Content matches layer size.
1394   frame_provider = new cc::DelegatedFrameProvider(
1395       resource_collection.get(), MakeFrameData(gfx::Size(10, 10)));
1396   child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1397   EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1398             gfx::Size(10, 10).ToString());
1399
1400   // Content larger than layer.
1401   child->SetBounds(gfx::Rect(0, 0, 5, 5));
1402   EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1403             gfx::Size(5, 5).ToString());
1404
1405   // Content smaller than layer.
1406   child->SetBounds(gfx::Rect(0, 0, 10, 10));
1407   frame_provider = new cc::DelegatedFrameProvider(
1408       resource_collection.get(), MakeFrameData(gfx::Size(5, 5)));
1409   child->SetShowDelegatedContent(frame_provider, gfx::Size(5, 5));
1410   EXPECT_EQ(child->cc_layer()->bounds().ToString(), gfx::Size(5, 5).ToString());
1411
1412   // Hi-DPI content on low-DPI layer.
1413   frame_provider = new cc::DelegatedFrameProvider(
1414       resource_collection.get(), MakeFrameData(gfx::Size(20, 20)));
1415   child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1416   EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1417             gfx::Size(10, 10).ToString());
1418
1419   // Hi-DPI content on hi-DPI layer.
1420   compositor()->SetScaleAndSize(2.f, gfx::Size(1000, 1000));
1421   EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1422             gfx::Size(20, 20).ToString());
1423
1424   // Low-DPI content on hi-DPI layer.
1425   frame_provider = new cc::DelegatedFrameProvider(
1426       resource_collection.get(), MakeFrameData(gfx::Size(10, 10)));
1427   child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1428   EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1429             gfx::Size(20, 20).ToString());
1430 }
1431
1432 TEST_F(LayerWithDelegateTest, ExternalContent) {
1433   scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1434   scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1435
1436   child->SetBounds(gfx::Rect(0, 0, 10, 10));
1437   child->SetVisible(true);
1438   root->Add(child.get());
1439
1440   // The layer is already showing painted content, so the cc layer won't change.
1441   scoped_refptr<cc::Layer> before = child->cc_layer();
1442   child->SetShowPaintedContent();
1443   EXPECT_TRUE(child->cc_layer());
1444   EXPECT_EQ(before, child->cc_layer());
1445
1446   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection =
1447       new cc::DelegatedFrameResourceCollection;
1448   scoped_refptr<cc::DelegatedFrameProvider> frame_provider =
1449       new cc::DelegatedFrameProvider(resource_collection.get(),
1450                                      MakeFrameData(gfx::Size(10, 10)));
1451
1452   // Showing delegated content changes the underlying cc layer.
1453   before = child->cc_layer();
1454   child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1455   EXPECT_TRUE(child->cc_layer());
1456   EXPECT_NE(before, child->cc_layer());
1457
1458   // Changing to painted content should change the underlying cc layer.
1459   before = child->cc_layer();
1460   child->SetShowPaintedContent();
1461   EXPECT_TRUE(child->cc_layer());
1462   EXPECT_NE(before, child->cc_layer());
1463 }
1464
1465 // Tests Layer::AddThreadedAnimation and Layer::RemoveThreadedAnimation.
1466 TEST_F(LayerWithRealCompositorTest, AddRemoveThreadedAnimations) {
1467   scoped_ptr<Layer> root(CreateLayer(LAYER_TEXTURED));
1468   scoped_ptr<Layer> l1(CreateLayer(LAYER_TEXTURED));
1469   scoped_ptr<Layer> l2(CreateLayer(LAYER_TEXTURED));
1470
1471   l1->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1472   l2->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1473
1474   EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1475
1476   // Trigger a threaded animation.
1477   l1->SetOpacity(0.5f);
1478
1479   EXPECT_TRUE(l1->HasPendingThreadedAnimations());
1480
1481   // Ensure we can remove a pending threaded animation.
1482   l1->GetAnimator()->StopAnimating();
1483
1484   EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1485
1486   // Trigger another threaded animation.
1487   l1->SetOpacity(0.2f);
1488
1489   EXPECT_TRUE(l1->HasPendingThreadedAnimations());
1490
1491   root->Add(l1.get());
1492   GetCompositor()->SetRootLayer(root.get());
1493
1494   // Now that l1 is part of a tree, it should have dispatched the pending
1495   // animation.
1496   EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1497
1498   // Ensure that l1 no longer holds on to animations.
1499   l1->SetOpacity(0.1f);
1500   EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1501
1502   // Ensure that adding a layer to an existing tree causes its pending
1503   // animations to get dispatched.
1504   l2->SetOpacity(0.5f);
1505   EXPECT_TRUE(l2->HasPendingThreadedAnimations());
1506
1507   l1->Add(l2.get());
1508   EXPECT_FALSE(l2->HasPendingThreadedAnimations());
1509 }
1510
1511 // Tests that in-progress threaded animations complete when a Layer's
1512 // cc::Layer changes.
1513 TEST_F(LayerWithRealCompositorTest, SwitchCCLayerAnimations) {
1514   scoped_ptr<Layer> root(CreateLayer(LAYER_TEXTURED));
1515   scoped_ptr<Layer> l1(CreateLayer(LAYER_TEXTURED));
1516   GetCompositor()->SetRootLayer(root.get());
1517   root->Add(l1.get());
1518
1519   l1->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1520
1521   EXPECT_FLOAT_EQ(l1->opacity(), 1.0f);
1522
1523   // Trigger a threaded animation.
1524   l1->SetOpacity(0.5f);
1525
1526   // Change l1's cc::Layer.
1527   l1->SwitchCCLayerForTest();
1528
1529   // Ensure that the opacity animation completed.
1530   EXPECT_FLOAT_EQ(l1->opacity(), 0.5f);
1531 }
1532
1533 }  // namespace ui