OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" | 6 #include "base/bind.h" |
7 #include "base/cancelable_callback.h" | |
8 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
9 #include "base/file_util.h" | 8 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
13 #include "base/path_service.h" | 12 #include "base/path_service.h" |
14 #include "base/run_loop.h" | |
15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
17 #include "cc/layers/layer.h" | 15 #include "cc/layers/layer.h" |
18 #include "cc/output/delegated_frame_data.h" | 16 #include "cc/output/delegated_frame_data.h" |
19 #include "cc/test/pixel_test_utils.h" | 17 #include "cc/test/pixel_test_utils.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/compositor/compositor_observer.h" | 19 #include "ui/compositor/compositor_observer.h" |
22 #include "ui/compositor/compositor_setup.h" | 20 #include "ui/compositor/compositor_setup.h" |
23 #include "ui/compositor/layer.h" | 21 #include "ui/compositor/layer.h" |
24 #include "ui/compositor/layer_animation_sequence.h" | 22 #include "ui/compositor/layer_animation_sequence.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 75 } |
78 | 76 |
79 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE { | 77 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE { |
80 return base::Closure(); | 78 return base::Closure(); |
81 } | 79 } |
82 | 80 |
83 private: | 81 private: |
84 SkColor color_; | 82 SkColor color_; |
85 }; | 83 }; |
86 | 84 |
87 const int kDrawWaitTimeOutMs = 1000; | |
88 | |
89 class DrawWaiter : public ui::CompositorObserver { | |
90 public: | |
91 DrawWaiter() : did_draw_(false) {} | |
92 | |
93 bool Wait(ui::Compositor* compositor) { | |
94 did_draw_ = false; | |
95 compositor->AddObserver(this); | |
96 wait_run_loop_.reset(new base::RunLoop()); | |
97 base::CancelableClosure timeout( | |
98 base::Bind(&DrawWaiter::TimedOutWhileWaiting, | |
99 base::Unretained(this))); | |
100 MessageLoop::current()->PostDelayedTask( | |
101 FROM_HERE, timeout.callback(), | |
102 base::TimeDelta::FromMilliseconds(kDrawWaitTimeOutMs)); | |
103 wait_run_loop_->Run(); | |
104 compositor->RemoveObserver(this); | |
105 return did_draw_; | |
106 } | |
107 | |
108 private: | |
109 void TimedOutWhileWaiting() { | |
110 LOG(ERROR) << "Timed out waiting for draw."; | |
111 wait_run_loop_->Quit(); | |
112 } | |
113 | |
114 // ui::CompositorObserver implementation. | |
115 virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE {} | |
116 virtual void OnCompositingStarted(Compositor* compositor, | |
117 base::TimeTicks start_time) OVERRIDE {} | |
118 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE { | |
119 did_draw_ = true; | |
120 wait_run_loop_->Quit(); | |
121 } | |
122 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {} | |
123 virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE {} | |
124 virtual void OnUpdateVSyncParameters(Compositor* compositor, | |
125 base::TimeTicks timebase, | |
126 base::TimeDelta interval) OVERRIDE {} | |
127 | |
128 scoped_ptr<base::RunLoop> wait_run_loop_; | |
129 bool did_draw_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(DrawWaiter); | |
132 }; | |
133 | |
134 class LayerWithRealCompositorTest : public testing::Test { | |
135 public: | |
136 LayerWithRealCompositorTest() { | |
137 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { | |
138 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); | |
139 } else { | |
140 LOG(ERROR) << "Could not open test data directory."; | |
141 } | |
142 } | |
143 virtual ~LayerWithRealCompositorTest() {} | |
144 | |
145 // Overridden from testing::Test: | |
146 virtual void SetUp() OVERRIDE { | |
147 DisableTestCompositor(); | |
148 const gfx::Rect host_bounds(10, 10, 500, 500); | |
149 window_.reset(TestCompositorHost::Create(host_bounds)); | |
150 window_->Show(); | |
151 } | |
152 | |
153 virtual void TearDown() OVERRIDE { | |
154 } | |
155 | |
156 Compositor* GetCompositor() { | |
157 return window_->GetCompositor(); | |
158 } | |
159 | |
160 Layer* CreateLayer(LayerType type) { | |
161 return new Layer(type); | |
162 } | |
163 | |
164 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { | |
165 Layer* layer = new ColoredLayer(color); | |
166 layer->SetBounds(bounds); | |
167 return layer; | |
168 } | |
169 | |
170 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { | |
171 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); | |
172 layer->SetBounds(bounds); | |
173 return layer; | |
174 } | |
175 | |
176 void DrawTree(Layer* root) { | |
177 GetCompositor()->SetRootLayer(root); | |
178 GetCompositor()->ScheduleDraw(); | |
179 WaitForDraw(); | |
180 } | |
181 | |
182 bool ReadPixels(SkBitmap* bitmap) { | |
183 return GetCompositor()->ReadPixels(bitmap, | |
184 gfx::Rect(GetCompositor()->size())); | |
185 } | |
186 | |
187 bool WaitForDraw() { | |
188 DrawWaiter draw_waiter; | |
189 return draw_waiter.Wait(GetCompositor()); | |
190 } | |
191 | |
192 // Invalidates the entire contents of the layer. | |
193 void SchedulePaintForLayer(Layer* layer) { | |
194 layer->SchedulePaint( | |
195 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | |
196 } | |
197 | |
198 const base::FilePath& test_data_directory() const { | |
199 return test_data_directory_; | |
200 } | |
201 | |
202 private: | |
203 scoped_ptr<TestCompositorHost> window_; | |
204 | |
205 // The root directory for test files. | |
206 base::FilePath test_data_directory_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); | |
209 }; | |
210 | |
211 // LayerDelegate that paints colors to the layer. | 85 // LayerDelegate that paints colors to the layer. |
212 class TestLayerDelegate : public LayerDelegate { | 86 class TestLayerDelegate : public LayerDelegate { |
213 public: | 87 public: |
214 explicit TestLayerDelegate() { reset(); } | 88 explicit TestLayerDelegate() { reset(); } |
215 virtual ~TestLayerDelegate() {} | 89 virtual ~TestLayerDelegate() {} |
216 | 90 |
217 void AddColor(SkColor color) { | 91 void AddColor(SkColor color) { |
218 colors_.push_back(color); | 92 colors_.push_back(color); |
219 } | 93 } |
220 | 94 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 229 |
356 bool started_; | 230 bool started_; |
357 bool ended_; | 231 bool ended_; |
358 bool aborted_; | 232 bool aborted_; |
359 | 233 |
360 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); | 234 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); |
361 }; | 235 }; |
362 | 236 |
363 } // namespace | 237 } // namespace |
364 | 238 |
| 239 class LayerWithRealCompositorTest : public testing::Test { |
| 240 public: |
| 241 LayerWithRealCompositorTest() { |
| 242 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { |
| 243 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); |
| 244 } else { |
| 245 LOG(ERROR) << "Could not open test data directory."; |
| 246 } |
| 247 } |
| 248 virtual ~LayerWithRealCompositorTest() {} |
| 249 |
| 250 // Overridden from testing::Test: |
| 251 virtual void SetUp() OVERRIDE { |
| 252 DisableTestCompositor(); |
| 253 const gfx::Rect host_bounds(10, 10, 500, 500); |
| 254 window_.reset(TestCompositorHost::Create(host_bounds)); |
| 255 window_->Show(); |
| 256 } |
| 257 |
| 258 virtual void TearDown() OVERRIDE { |
| 259 } |
| 260 |
| 261 Compositor* GetCompositor() { |
| 262 return window_->GetCompositor(); |
| 263 } |
| 264 |
| 265 Layer* CreateLayer(LayerType type) { |
| 266 return new Layer(type); |
| 267 } |
| 268 |
| 269 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { |
| 270 Layer* layer = new ColoredLayer(color); |
| 271 layer->SetBounds(bounds); |
| 272 return layer; |
| 273 } |
| 274 |
| 275 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { |
| 276 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); |
| 277 layer->SetBounds(bounds); |
| 278 return layer; |
| 279 } |
| 280 |
| 281 void DrawTree(Layer* root) { |
| 282 GetCompositor()->SetRootLayer(root); |
| 283 GetCompositor()->ScheduleDraw(); |
| 284 WaitForDraw(); |
| 285 } |
| 286 |
| 287 bool WaitForDraw() { |
| 288 return GetCompositor()->WaitForDraw(); |
| 289 } |
| 290 |
| 291 bool ReadPixels(SkBitmap* bitmap) { |
| 292 return GetCompositor()->ReadPixels(bitmap, |
| 293 gfx::Rect(GetCompositor()->size())); |
| 294 } |
| 295 |
| 296 // Invalidates the entire contents of the layer. |
| 297 void SchedulePaintForLayer(Layer* layer) { |
| 298 layer->SchedulePaint( |
| 299 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
| 300 } |
| 301 |
| 302 const base::FilePath& test_data_directory() const { |
| 303 return test_data_directory_; |
| 304 } |
| 305 |
| 306 private: |
| 307 scoped_ptr<TestCompositorHost> window_; |
| 308 |
| 309 // The root directory for test files. |
| 310 base::FilePath test_data_directory_; |
| 311 |
| 312 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); |
| 313 }; |
| 314 |
365 #if defined(OS_WIN) | 315 #if defined(OS_WIN) |
366 // These are disabled on windows as they don't run correctly on the buildbot. | 316 // These are disabled on windows as they don't run correctly on the buildbot. |
367 // Reenable once we move to the real compositor. | 317 // Reenable once we move to the real compositor. |
368 #define MAYBE_Delegate DISABLED_Delegate | 318 #define MAYBE_Delegate DISABLED_Delegate |
369 #define MAYBE_Draw DISABLED_Draw | 319 #define MAYBE_Draw DISABLED_Draw |
370 #define MAYBE_DrawTree DISABLED_DrawTree | 320 #define MAYBE_DrawTree DISABLED_DrawTree |
371 #define MAYBE_Hierarchy DISABLED_Hierarchy | 321 #define MAYBE_Hierarchy DISABLED_Hierarchy |
372 #define MAYBE_HierarchyNoTexture DISABLED_HierarchyNoTexture | 322 #define MAYBE_HierarchyNoTexture DISABLED_HierarchyNoTexture |
373 #define MAYBE_DrawPixels DISABLED_DrawPixels | 323 #define MAYBE_DrawPixels DISABLED_DrawPixels |
374 #define MAYBE_SetRootLayer DISABLED_SetRootLayer | 324 #define MAYBE_SetRootLayer DISABLED_SetRootLayer |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 421 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
472 } | 422 } |
473 | 423 |
474 // Invokes DrawTree on the compositor. | 424 // Invokes DrawTree on the compositor. |
475 void Draw() { | 425 void Draw() { |
476 compositor()->ScheduleDraw(); | 426 compositor()->ScheduleDraw(); |
477 WaitForDraw(); | 427 WaitForDraw(); |
478 } | 428 } |
479 | 429 |
480 bool WaitForDraw() { | 430 bool WaitForDraw() { |
481 DrawWaiter draw_waiter; | 431 return compositor()->WaitForDraw(); |
482 return draw_waiter.Wait(compositor()); | |
483 } | 432 } |
484 | 433 |
485 // CompositorDelegate overrides. | 434 // CompositorDelegate overrides. |
486 virtual void ScheduleDraw() OVERRIDE { | 435 virtual void ScheduleDraw() OVERRIDE { |
487 DCHECK(!ui::Compositor::WasInitializedWithThread()); | 436 DCHECK(!ui::Compositor::WasInitializedWithThread()); |
488 if (compositor_) { | 437 if (compositor_) { |
489 MessageLoop::current()->PostTask( | 438 MessageLoop::current()->PostTask( |
490 FROM_HERE, | 439 FROM_HERE, |
491 base::Bind(&Compositor::Draw, compositor_->AsWeakPtr(), false)); | 440 base::Bind(&Compositor::Draw, compositor_->AsWeakPtr(), false)); |
492 } | 441 } |
(...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1424 l1->SetOpacity(0.5f); | 1373 l1->SetOpacity(0.5f); |
1425 | 1374 |
1426 // Change l1's cc::Layer. | 1375 // Change l1's cc::Layer. |
1427 l1->SwitchCCLayerForTest(); | 1376 l1->SwitchCCLayerForTest(); |
1428 | 1377 |
1429 // Ensure that the opacity animation completed. | 1378 // Ensure that the opacity animation completed. |
1430 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); | 1379 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); |
1431 } | 1380 } |
1432 | 1381 |
1433 } // namespace ui | 1382 } // namespace ui |
OLD | NEW |