OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "ui/gfx/canvas_skia.h" | 9 #include "ui/gfx/canvas_skia.h" |
| 10 #include "ui/gfx/compositor/compositor_observer.h" |
10 #include "ui/gfx/compositor/layer.h" | 11 #include "ui/gfx/compositor/layer.h" |
11 #include "ui/gfx/compositor/test_compositor_host.h" | 12 #include "ui/gfx/compositor/test_compositor_host.h" |
12 | 13 |
13 namespace ui { | 14 namespace ui { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
| 18 // Simple class that exits the current message loop when compositing finishes. |
| 19 class CompositingEndedObserver : public CompositorObserver { |
| 20 public: |
| 21 CompositingEndedObserver() {} |
| 22 virtual ~CompositingEndedObserver() {} |
| 23 |
| 24 // Overridden from CompositorObserver: |
| 25 virtual void OnCompositingEnded() OVERRIDE { |
| 26 MessageLoop::current()->Quit(); |
| 27 } |
| 28 |
| 29 private: |
| 30 DISALLOW_COPY_AND_ASSIGN(CompositingEndedObserver); |
| 31 }; |
| 32 |
17 class TestLayerDelegate : public LayerDelegate { | 33 class TestLayerDelegate : public LayerDelegate { |
18 public: | 34 public: |
19 explicit TestLayerDelegate(Layer* owner) : owner_(owner), color_index_(0) {} | 35 explicit TestLayerDelegate(Layer* owner) : owner_(owner), color_index_(0) {} |
20 virtual ~TestLayerDelegate() {} | 36 virtual ~TestLayerDelegate() {} |
21 | 37 |
22 void AddColor(SkColor color) { | 38 void AddColor(SkColor color) { |
23 colors_.push_back(color); | 39 colors_.push_back(color); |
24 } | 40 } |
25 | 41 |
26 gfx::Size paint_size() const { return paint_size_; } | 42 gfx::Size paint_size() const { return paint_size_; } |
27 int color_index() const { return color_index_; } | 43 int color_index() const { return color_index_; } |
28 | 44 |
29 // Overridden from LayerDelegate: | 45 // Overridden from LayerDelegate: |
30 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | 46 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { |
31 SkBitmap contents = canvas->AsCanvasSkia()->ExtractBitmap(); | 47 SkBitmap contents = canvas->AsCanvasSkia()->ExtractBitmap(); |
32 paint_size_ = gfx::Size(contents.width(), contents.height()); | 48 paint_size_ = gfx::Size(contents.width(), contents.height()); |
33 canvas->FillRectInt(colors_.at(color_index_), 0, 0, | 49 canvas->FillRectInt(colors_.at(color_index_), 0, 0, |
34 contents.width(), | 50 contents.width(), |
35 contents.height()); | 51 contents.height()); |
36 color_index_ = ++color_index_ % colors_.size(); | 52 color_index_ = ++color_index_ % colors_.size(); |
37 | |
38 MessageLoop::current()->Quit(); | |
39 } | 53 } |
40 | 54 |
41 private: | 55 private: |
42 Layer* owner_; | 56 Layer* owner_; |
43 std::vector<SkColor> colors_; | 57 std::vector<SkColor> colors_; |
44 int color_index_; | 58 int color_index_; |
45 gfx::Size paint_size_; | 59 gfx::Size paint_size_; |
46 | 60 |
47 DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate); | 61 DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate); |
48 }; | 62 }; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 101 |
88 void PaintColorToLayer(Layer* layer, SkColor color) { | 102 void PaintColorToLayer(Layer* layer, SkColor color) { |
89 scoped_ptr<gfx::Canvas> canvas(CreateCanvasForLayer(layer)); | 103 scoped_ptr<gfx::Canvas> canvas(CreateCanvasForLayer(layer)); |
90 canvas->FillRectInt(color, 0, 0, layer->bounds().width(), | 104 canvas->FillRectInt(color, 0, 0, layer->bounds().width(), |
91 layer->bounds().height()); | 105 layer->bounds().height()); |
92 layer->SetCanvas(*canvas->AsCanvasSkia(), layer->bounds().origin()); | 106 layer->SetCanvas(*canvas->AsCanvasSkia(), layer->bounds().origin()); |
93 } | 107 } |
94 | 108 |
95 void DrawTree(Layer* root) { | 109 void DrawTree(Layer* root) { |
96 window_->GetCompositor()->NotifyStart(); | 110 window_->GetCompositor()->NotifyStart(); |
97 DrawLayerChildren(root); | 111 root->DrawTree(); |
98 window_->GetCompositor()->NotifyEnd(); | 112 window_->GetCompositor()->NotifyEnd(); |
99 } | 113 } |
100 | 114 |
101 void DrawLayerChildren(Layer* layer) { | |
102 layer->Draw(); | |
103 std::vector<Layer*>::const_iterator it = layer->children().begin(); | |
104 while (it != layer->children().end()) { | |
105 DrawLayerChildren(*it); | |
106 ++it; | |
107 } | |
108 } | |
109 | |
110 void RunPendingMessages() { | 115 void RunPendingMessages() { |
111 MessageLoopForUI::current()->Run(NULL); | 116 MessageLoopForUI::current()->Run(NULL); |
112 } | 117 } |
113 | 118 |
114 protected: | 119 protected: |
115 void set_root_layer(Layer* root_layer) { root_layer_ = root_layer; } | 120 void set_root_layer(Layer* root_layer) { root_layer_ = root_layer; } |
116 | 121 |
117 private: | 122 private: |
118 // Overridden from TestCompositorHostDelegate: | 123 // Overridden from TestCompositorHostDelegate: |
119 virtual void Draw() { | 124 virtual void Draw() { |
120 if (root_layer_) | 125 if (root_layer_) |
121 DrawLayerChildren(root_layer_); | 126 root_layer_->DrawTree(); |
122 } | 127 } |
123 | 128 |
124 MessageLoopForUI message_loop_; | 129 MessageLoopForUI message_loop_; |
125 scoped_ptr<TestCompositorHost> window_; | 130 scoped_ptr<TestCompositorHost> window_; |
126 Layer* root_layer_; | 131 Layer* root_layer_; |
127 | 132 |
128 DISALLOW_COPY_AND_ASSIGN(LayerTest); | 133 DISALLOW_COPY_AND_ASSIGN(LayerTest); |
129 }; | 134 }; |
130 | 135 |
| 136 class LayerQuitOnCompositedTest : public LayerTest { |
| 137 public: |
| 138 LayerQuitOnCompositedTest() {} |
| 139 virtual ~LayerQuitOnCompositedTest() {} |
| 140 |
| 141 // Overridden from LayerTest: |
| 142 virtual void SetUp() OVERRIDE { |
| 143 LayerTest::SetUp(); |
| 144 GetCompositor()->AddObserver(&compositor_observer_); |
| 145 } |
| 146 |
| 147 virtual void TearDown() OVERRIDE { |
| 148 LayerTest::TearDown(); |
| 149 GetCompositor()->RemoveObserver(&compositor_observer_); |
| 150 } |
| 151 |
| 152 private: |
| 153 CompositingEndedObserver compositor_observer_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(LayerQuitOnCompositedTest); |
| 156 }; |
| 157 |
131 } | 158 } |
132 | 159 |
133 TEST_F(LayerTest, Draw) { | 160 TEST_F(LayerTest, Draw) { |
134 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED, | 161 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED, |
135 gfx::Rect(20, 20, 50, 50))); | 162 gfx::Rect(20, 20, 50, 50))); |
136 DrawTree(layer.get()); | 163 DrawTree(layer.get()); |
137 } | 164 } |
138 | 165 |
139 // Create this hierarchy: | 166 // Create this hierarchy: |
140 // L1 - red | 167 // L1 - red |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords); | 225 Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords); |
199 gfx::Point point1_in_l1_coords(25, 25); | 226 gfx::Point point1_in_l1_coords(25, 25); |
200 EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords); | 227 EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords); |
201 | 228 |
202 gfx::Point point2_in_l1_coords(5, 5); | 229 gfx::Point point2_in_l1_coords(5, 5); |
203 Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords); | 230 Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords); |
204 gfx::Point point2_in_l3_coords(-15, -15); | 231 gfx::Point point2_in_l3_coords(-15, -15); |
205 EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords); | 232 EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords); |
206 } | 233 } |
207 | 234 |
208 TEST_F(LayerTest, Delegate) { | 235 TEST_F(LayerQuitOnCompositedTest, Delegate) { |
209 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK, | 236 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK, |
210 gfx::Rect(20, 20, 400, 400))); | 237 gfx::Rect(20, 20, 400, 400))); |
211 TestLayerDelegate delegate(l1.get()); | 238 TestLayerDelegate delegate(l1.get()); |
212 l1->set_delegate(&delegate); | 239 l1->set_delegate(&delegate); |
213 delegate.AddColor(SK_ColorWHITE); | 240 delegate.AddColor(SK_ColorWHITE); |
214 delegate.AddColor(SK_ColorYELLOW); | 241 delegate.AddColor(SK_ColorYELLOW); |
215 delegate.AddColor(SK_ColorGREEN); | 242 delegate.AddColor(SK_ColorGREEN); |
216 | 243 |
217 set_root_layer(l1.get()); | 244 set_root_layer(l1.get()); |
218 | 245 |
219 l1->SchedulePaint(gfx::Rect(0, 0, 400, 400)); | 246 l1->SchedulePaint(gfx::Rect(0, 0, 400, 400)); |
220 RunPendingMessages(); | 247 RunPendingMessages(); |
221 EXPECT_EQ(delegate.color_index(), 1); | 248 EXPECT_EQ(delegate.color_index(), 1); |
222 EXPECT_EQ(delegate.paint_size(), l1->bounds().size()); | 249 EXPECT_EQ(delegate.paint_size(), l1->bounds().size()); |
223 | 250 |
224 l1->SchedulePaint(gfx::Rect(10, 10, 200, 200)); | 251 l1->SchedulePaint(gfx::Rect(10, 10, 200, 200)); |
225 RunPendingMessages(); | 252 RunPendingMessages(); |
226 EXPECT_EQ(delegate.color_index(), 2); | 253 EXPECT_EQ(delegate.color_index(), 2); |
227 EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200)); | 254 EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200)); |
228 | 255 |
229 l1->SchedulePaint(gfx::Rect(5, 5, 50, 50)); | 256 l1->SchedulePaint(gfx::Rect(5, 5, 50, 50)); |
230 RunPendingMessages(); | 257 RunPendingMessages(); |
231 EXPECT_EQ(delegate.color_index(), 0); | 258 EXPECT_EQ(delegate.color_index(), 0); |
232 EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50)); | 259 EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50)); |
233 } | 260 } |
234 | 261 |
| 262 namespace { |
| 263 |
| 264 class DrawTreeLayerDelegate : public LayerDelegate { |
| 265 public: |
| 266 DrawTreeLayerDelegate() : painted_(false) {} |
| 267 virtual ~DrawTreeLayerDelegate() {} |
| 268 |
| 269 void Reset() { |
| 270 painted_ = false; |
| 271 } |
| 272 |
| 273 bool painted() const { return painted_; } |
| 274 |
| 275 // Overridden from LayerDelegate: |
| 276 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { |
| 277 painted_ = true; |
| 278 } |
| 279 |
| 280 private: |
| 281 bool painted_; |
| 282 |
| 283 DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate); |
| 284 }; |
| 285 |
| 286 } // namespace |
| 287 |
| 288 TEST_F(LayerQuitOnCompositedTest, DrawTree) { |
| 289 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
| 290 gfx::Rect(20, 20, 400, 400))); |
| 291 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
| 292 gfx::Rect(10, 10, 350, 350))); |
| 293 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, |
| 294 gfx::Rect(10, 10, 100, 100))); |
| 295 l1->Add(l2.get()); |
| 296 l2->Add(l3.get()); |
| 297 |
| 298 DrawTreeLayerDelegate d1; |
| 299 l1->set_delegate(&d1); |
| 300 DrawTreeLayerDelegate d2; |
| 301 l2->set_delegate(&d2); |
| 302 DrawTreeLayerDelegate d3; |
| 303 l3->set_delegate(&d3); |
| 304 |
| 305 set_root_layer(l1.get()); |
| 306 |
| 307 l2->SchedulePaint(gfx::Rect(5, 5, 5, 5)); |
| 308 RunPendingMessages(); |
| 309 EXPECT_FALSE(d1.painted()); |
| 310 EXPECT_TRUE(d2.painted()); |
| 311 EXPECT_FALSE(d3.painted()); |
| 312 } |
| 313 |
235 } // namespace ui | 314 } // namespace ui |
236 | 315 |
OLD | NEW |