OLD | NEW |
| (Empty) |
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 "ash/common/wallpaper/wallpaper_controller.h" | |
6 | |
7 #include <cmath> | |
8 #include <cstdlib> | |
9 | |
10 #include "ash/common/wallpaper/wallpaper_view.h" | |
11 #include "ash/common/wallpaper/wallpaper_widget_controller.h" | |
12 #include "ash/common/wm_shell.h" | |
13 #include "ash/common/wm_window.h" | |
14 #include "ash/public/cpp/shell_window_ids.h" | |
15 #include "ash/root_window_controller.h" | |
16 #include "ash/test/ash_test_base.h" | |
17 #include "ash/test/test_wallpaper_delegate.h" | |
18 #include "base/message_loop/message_loop.h" | |
19 #include "base/run_loop.h" | |
20 #include "base/strings/stringprintf.h" | |
21 #include "base/threading/sequenced_worker_pool.h" | |
22 #include "third_party/skia/include/core/SkBitmap.h" | |
23 #include "third_party/skia/include/core/SkColor.h" | |
24 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
25 #include "ui/compositor/test/layer_animator_test_controller.h" | |
26 #include "ui/gfx/canvas.h" | |
27 #include "ui/views/widget/widget.h" | |
28 | |
29 using wallpaper::WALLPAPER_LAYOUT_CENTER; | |
30 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED; | |
31 using wallpaper::WALLPAPER_LAYOUT_STRETCH; | |
32 using wallpaper::WALLPAPER_LAYOUT_TILE; | |
33 | |
34 namespace ash { | |
35 namespace { | |
36 | |
37 // Containers IDs used for tests. | |
38 const int kWallpaperId = ash::kShellWindowId_WallpaperContainer; | |
39 const int kLockScreenWallpaperId = | |
40 ash::kShellWindowId_LockScreenWallpaperContainer; | |
41 | |
42 // Returns number of child windows in a shell window container. | |
43 int ChildCountForContainer(int container_id) { | |
44 WmWindow* root = WmShell::Get()->GetPrimaryRootWindow(); | |
45 WmWindow* container = root->GetChildByShellWindowId(container_id); | |
46 return static_cast<int>(container->GetChildren().size()); | |
47 } | |
48 | |
49 // Steps a widget's layer animation until it is completed. Animations must be | |
50 // enabled. | |
51 void RunAnimationForWidget(views::Widget* widget) { | |
52 // Animations must be enabled for stepping to work. | |
53 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(), | |
54 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | |
55 | |
56 ui::Layer* layer = widget->GetLayer(); | |
57 ui::LayerAnimatorTestController controller(layer->GetAnimator()); | |
58 // Multiple steps are required to complete complex animations. | |
59 // TODO(vollick): This should not be necessary. crbug.com/154017 | |
60 while (controller.animator()->is_animating()) { | |
61 controller.StartThreadedAnimationsIfNeeded(); | |
62 base::TimeTicks step_time = controller.animator()->last_step_time(); | |
63 layer->GetAnimator()->Step(step_time + | |
64 base::TimeDelta::FromMilliseconds(1000)); | |
65 } | |
66 } | |
67 | |
68 // Monitors if any task is processed by the message loop. | |
69 class TaskObserver : public base::MessageLoop::TaskObserver { | |
70 public: | |
71 TaskObserver() : processed_(false) {} | |
72 ~TaskObserver() override {} | |
73 | |
74 // MessageLoop::TaskObserver overrides. | |
75 void WillProcessTask(const base::PendingTask& pending_task) override {} | |
76 void DidProcessTask(const base::PendingTask& pending_task) override { | |
77 processed_ = true; | |
78 } | |
79 | |
80 // Returns true if any task was processed. | |
81 bool processed() const { return processed_; } | |
82 | |
83 private: | |
84 bool processed_; | |
85 DISALLOW_COPY_AND_ASSIGN(TaskObserver); | |
86 }; | |
87 | |
88 void RunAllBlockingPoolTasksUntilIdle(base::SequencedWorkerPool* pool) { | |
89 while (true) { | |
90 pool->FlushForTesting(); | |
91 | |
92 TaskObserver task_observer; | |
93 base::MessageLoop::current()->AddTaskObserver(&task_observer); | |
94 base::RunLoop().RunUntilIdle(); | |
95 base::MessageLoop::current()->RemoveTaskObserver(&task_observer); | |
96 | |
97 if (!task_observer.processed()) | |
98 break; | |
99 } | |
100 } | |
101 | |
102 } // namespace | |
103 | |
104 class WallpaperControllerTest : public test::AshTestBase { | |
105 public: | |
106 WallpaperControllerTest() | |
107 : controller_(nullptr), wallpaper_delegate_(nullptr) {} | |
108 ~WallpaperControllerTest() override {} | |
109 | |
110 void SetUp() override { | |
111 test::AshTestBase::SetUp(); | |
112 // Ash shell initialization creates wallpaper. Reset it so we can manually | |
113 // control wallpaper creation and animation in our tests. | |
114 RootWindowController* root_window_controller = | |
115 WmShell::Get()->GetPrimaryRootWindow()->GetRootWindowController(); | |
116 root_window_controller->SetWallpaperWidgetController(nullptr); | |
117 root_window_controller->SetAnimatingWallpaperWidgetController(nullptr); | |
118 controller_ = WmShell::Get()->wallpaper_controller(); | |
119 wallpaper_delegate_ = static_cast<test::TestWallpaperDelegate*>( | |
120 WmShell::Get()->wallpaper_delegate()); | |
121 controller_->set_wallpaper_reload_delay_for_test(0); | |
122 } | |
123 | |
124 WallpaperView* wallpaper_view() { | |
125 WallpaperWidgetController* controller = | |
126 WmShell::Get() | |
127 ->GetPrimaryRootWindow() | |
128 ->GetRootWindowController() | |
129 ->animating_wallpaper_widget_controller() | |
130 ->GetController(false); | |
131 EXPECT_TRUE(controller); | |
132 return static_cast<WallpaperView*>( | |
133 controller->widget()->GetContentsView()->child_at(0)); | |
134 } | |
135 | |
136 protected: | |
137 // A color that can be passed to CreateImage(). Specifically chosen to not | |
138 // conflict with any of the default wallpaper colors. | |
139 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA; | |
140 | |
141 // Creates an image of size |size|. | |
142 gfx::ImageSkia CreateImage(int width, int height, SkColor color) { | |
143 SkBitmap bitmap; | |
144 bitmap.allocN32Pixels(width, height); | |
145 bitmap.eraseColor(color); | |
146 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | |
147 return image; | |
148 } | |
149 | |
150 // Helper function that tests the wallpaper is always fitted to the native | |
151 // display resolution when the layout is WALLPAPER_LAYOUT_CENTER. | |
152 void WallpaperFitToNativeResolution(WallpaperView* view, | |
153 float device_scale_factor, | |
154 int image_width, | |
155 int image_height, | |
156 SkColor color) { | |
157 gfx::Size size = view->bounds().size(); | |
158 gfx::Canvas canvas(size, device_scale_factor, true); | |
159 view->OnPaint(&canvas); | |
160 | |
161 int canvas_width = canvas.sk_canvas()->imageInfo().width(); | |
162 int canvas_height = canvas.sk_canvas()->imageInfo().height(); | |
163 SkBitmap bitmap; | |
164 bitmap.allocN32Pixels(canvas_width, canvas_height); | |
165 canvas.sk_canvas()->readPixels(&bitmap, 0, 0); | |
166 | |
167 for (int i = 0; i < canvas_width; i++) { | |
168 for (int j = 0; j < canvas_height; j++) { | |
169 if (i >= (canvas_width - image_width) / 2 && | |
170 i < (canvas_width + image_width) / 2 && | |
171 j >= (canvas_height - image_height) / 2 && | |
172 j < (canvas_height + image_height) / 2) { | |
173 EXPECT_EQ(color, bitmap.getColor(i, j)); | |
174 } else { | |
175 EXPECT_EQ(SK_ColorBLACK, bitmap.getColor(i, j)); | |
176 } | |
177 } | |
178 } | |
179 } | |
180 | |
181 // Runs AnimatingWallpaperWidgetController's animation to completion. | |
182 // TODO(bshe): Don't require tests to run animations; it's slow. | |
183 void RunDesktopControllerAnimation() { | |
184 WallpaperWidgetController* controller = | |
185 WmShell::Get() | |
186 ->GetPrimaryRootWindow() | |
187 ->GetRootWindowController() | |
188 ->animating_wallpaper_widget_controller() | |
189 ->GetController(false); | |
190 EXPECT_TRUE(controller); | |
191 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget())); | |
192 } | |
193 | |
194 WallpaperController* controller_; // Not owned. | |
195 | |
196 test::TestWallpaperDelegate* wallpaper_delegate_; | |
197 | |
198 private: | |
199 DISALLOW_COPY_AND_ASSIGN(WallpaperControllerTest); | |
200 }; | |
201 | |
202 TEST_F(WallpaperControllerTest, BasicReparenting) { | |
203 WallpaperController* controller = WmShell::Get()->wallpaper_controller(); | |
204 controller->CreateEmptyWallpaper(); | |
205 | |
206 // Wallpaper view/window exists in the wallpaper container and nothing is in | |
207 // the lock screen wallpaper container. | |
208 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
209 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
210 | |
211 // Moving wallpaper to lock container should succeed the first time but | |
212 // subsequent calls should do nothing. | |
213 EXPECT_TRUE(controller->MoveToLockedContainer()); | |
214 EXPECT_FALSE(controller->MoveToLockedContainer()); | |
215 | |
216 // One window is moved from desktop to lock container. | |
217 EXPECT_EQ(0, ChildCountForContainer(kWallpaperId)); | |
218 EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId)); | |
219 | |
220 // Moving wallpaper to desktop container should succeed the first time. | |
221 EXPECT_TRUE(controller->MoveToUnlockedContainer()); | |
222 EXPECT_FALSE(controller->MoveToUnlockedContainer()); | |
223 | |
224 // One window is moved from lock to desktop container. | |
225 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
226 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
227 } | |
228 | |
229 TEST_F(WallpaperControllerTest, ControllerOwnership) { | |
230 // We cannot short-circuit animations for this test. | |
231 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
232 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
233 | |
234 // Create the wallpaper and its view. | |
235 WallpaperController* controller = WmShell::Get()->wallpaper_controller(); | |
236 controller->CreateEmptyWallpaper(); | |
237 | |
238 // The new wallpaper is ready to animate. | |
239 RootWindowController* root_window_controller = | |
240 WmShell::Get()->GetPrimaryRootWindow()->GetRootWindowController(); | |
241 EXPECT_TRUE(root_window_controller->animating_wallpaper_widget_controller() | |
242 ->GetController(false)); | |
243 EXPECT_FALSE(root_window_controller->wallpaper_widget_controller()); | |
244 | |
245 // Force the animation to play to completion. | |
246 RunDesktopControllerAnimation(); | |
247 EXPECT_FALSE(root_window_controller->animating_wallpaper_widget_controller() | |
248 ->GetController(false)); | |
249 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
250 } | |
251 | |
252 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we | |
253 // move all wallpaper views if there are more than one. | |
254 TEST_F(WallpaperControllerTest, WallpaperMovementDuringUnlock) { | |
255 // We cannot short-circuit animations for this test. | |
256 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
257 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
258 | |
259 // Reset wallpaper state, see ControllerOwnership above. | |
260 WallpaperController* controller = WmShell::Get()->wallpaper_controller(); | |
261 controller->CreateEmptyWallpaper(); | |
262 | |
263 // Run wallpaper show animation to completion. | |
264 RunDesktopControllerAnimation(); | |
265 | |
266 // User locks the screen, which moves the wallpaper forward. | |
267 controller->MoveToLockedContainer(); | |
268 | |
269 // Suspend/resume cycle causes wallpaper to refresh, loading a new wallpaper | |
270 // that will animate in on top of the old one. | |
271 controller->CreateEmptyWallpaper(); | |
272 | |
273 // In this state we have two wallpaper views stored in different properties. | |
274 // Both are in the lock screen wallpaper container. | |
275 RootWindowController* root_window_controller = | |
276 WmShell::Get()->GetPrimaryRootWindow()->GetRootWindowController(); | |
277 EXPECT_TRUE(root_window_controller->animating_wallpaper_widget_controller() | |
278 ->GetController(false)); | |
279 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
280 EXPECT_EQ(0, ChildCountForContainer(kWallpaperId)); | |
281 EXPECT_EQ(2, ChildCountForContainer(kLockScreenWallpaperId)); | |
282 | |
283 // Before the wallpaper's animation completes, user unlocks the screen, which | |
284 // moves the wallpaper to the back. | |
285 controller->MoveToUnlockedContainer(); | |
286 | |
287 // Ensure both wallpapers have moved. | |
288 EXPECT_EQ(2, ChildCountForContainer(kWallpaperId)); | |
289 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
290 | |
291 // Finish the new wallpaper animation. | |
292 RunDesktopControllerAnimation(); | |
293 | |
294 // Now there is one wallpaper, in the back. | |
295 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
296 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
297 } | |
298 | |
299 // Test for crbug.com/156542. Animating wallpaper should immediately finish | |
300 // animation and replace current wallpaper before next animation starts. | |
301 TEST_F(WallpaperControllerTest, ChangeWallpaperQuick) { | |
302 // We cannot short-circuit animations for this test. | |
303 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
304 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
305 | |
306 // Reset wallpaper state, see ControllerOwnership above. | |
307 WallpaperController* controller = WmShell::Get()->wallpaper_controller(); | |
308 controller->CreateEmptyWallpaper(); | |
309 | |
310 // Run wallpaper show animation to completion. | |
311 RunDesktopControllerAnimation(); | |
312 | |
313 // Change to a new wallpaper. | |
314 controller->CreateEmptyWallpaper(); | |
315 | |
316 RootWindowController* root_window_controller = | |
317 WmShell::Get()->GetPrimaryRootWindow()->GetRootWindowController(); | |
318 WallpaperWidgetController* animating_controller = | |
319 root_window_controller->animating_wallpaper_widget_controller() | |
320 ->GetController(false); | |
321 EXPECT_TRUE(animating_controller); | |
322 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
323 | |
324 // Change to another wallpaper before animation finished. | |
325 controller->CreateEmptyWallpaper(); | |
326 | |
327 // The animating controller should immediately move to wallpaper controller. | |
328 EXPECT_EQ(animating_controller, | |
329 root_window_controller->wallpaper_widget_controller()); | |
330 | |
331 // Cache the new animating controller. | |
332 animating_controller = | |
333 root_window_controller->animating_wallpaper_widget_controller() | |
334 ->GetController(false); | |
335 | |
336 // Run wallpaper show animation to completion. | |
337 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget( | |
338 root_window_controller->animating_wallpaper_widget_controller() | |
339 ->GetController(false) | |
340 ->widget())); | |
341 | |
342 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
343 EXPECT_FALSE(root_window_controller->animating_wallpaper_widget_controller() | |
344 ->GetController(false)); | |
345 // The wallpaper controller should be the last created animating controller. | |
346 EXPECT_EQ(animating_controller, | |
347 root_window_controller->wallpaper_widget_controller()); | |
348 } | |
349 | |
350 TEST_F(WallpaperControllerTest, ResizeCustomWallpaper) { | |
351 UpdateDisplay("320x200"); | |
352 | |
353 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor); | |
354 | |
355 // Set the image as custom wallpaper, wait for the resize to finish, and check | |
356 // that the resized image is the expected size. | |
357 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH); | |
358 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper())); | |
359 RunAllBlockingPoolTasksUntilIdle(WmShell::Get()->blocking_pool().get()); | |
360 gfx::ImageSkia resized_image = controller_->GetWallpaper(); | |
361 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image)); | |
362 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString()); | |
363 | |
364 // Load the original wallpaper again and check that we're still using the | |
365 // previously-resized image instead of doing another resize | |
366 // (http://crbug.com/321402). | |
367 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH); | |
368 RunAllBlockingPoolTasksUntilIdle(WmShell::Get()->blocking_pool().get()); | |
369 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper())); | |
370 } | |
371 | |
372 TEST_F(WallpaperControllerTest, GetMaxDisplaySize) { | |
373 // Device scale factor shouldn't affect the native size. | |
374 UpdateDisplay("1000x300*2"); | |
375 EXPECT_EQ("1000x300", | |
376 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
377 | |
378 // TODO: mash doesn't support rotation yet, http://crbug.com/695556. | |
379 if (!WmShell::Get()->IsRunningInMash()) { | |
380 // Rotated display should return the rotated size. | |
381 UpdateDisplay("1000x300*2/r"); | |
382 EXPECT_EQ("300x1000", | |
383 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
384 } | |
385 | |
386 // UI Scaling shouldn't affect the native size. | |
387 UpdateDisplay("1000x300*2@1.5"); | |
388 EXPECT_EQ("1000x300", | |
389 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
390 | |
391 // First display has maximum size. | |
392 UpdateDisplay("400x300,100x100"); | |
393 EXPECT_EQ("400x300", | |
394 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
395 | |
396 // Second display has maximum size. | |
397 UpdateDisplay("400x300,500x600"); | |
398 EXPECT_EQ("500x600", | |
399 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
400 | |
401 // Maximum width and height belongs to different displays. | |
402 UpdateDisplay("400x300,100x500"); | |
403 EXPECT_EQ("400x500", | |
404 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
405 } | |
406 | |
407 // Test that the wallpaper is always fitted to the native display resolution | |
408 // when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images. | |
409 TEST_F(WallpaperControllerTest, DontScaleWallpaperWithCenterLayout) { | |
410 // We cannot short-circuit animations for this test. | |
411 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
412 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
413 | |
414 const gfx::Size high_resolution(3600, 2400); | |
415 const gfx::Size low_resolution(360, 240); | |
416 const float high_dsf = 2.0f; | |
417 const float low_dsf = 1.0f; | |
418 | |
419 gfx::ImageSkia image_high_res = CreateImage( | |
420 high_resolution.width(), high_resolution.height(), kCustomWallpaperColor); | |
421 gfx::ImageSkia image_low_res = CreateImage( | |
422 low_resolution.width(), low_resolution.height(), kCustomWallpaperColor); | |
423 | |
424 UpdateDisplay("1200x600*2"); | |
425 { | |
426 SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution")); | |
427 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
428 WallpaperFitToNativeResolution( | |
429 wallpaper_view(), high_dsf, high_resolution.width(), | |
430 high_resolution.height(), kCustomWallpaperColor); | |
431 } | |
432 { | |
433 SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution")); | |
434 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
435 WallpaperFitToNativeResolution( | |
436 wallpaper_view(), high_dsf, low_resolution.width(), | |
437 low_resolution.height(), kCustomWallpaperColor); | |
438 } | |
439 | |
440 UpdateDisplay("1200x600"); | |
441 { | |
442 SCOPED_TRACE(base::StringPrintf("1200x600 high resolution")); | |
443 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
444 WallpaperFitToNativeResolution( | |
445 wallpaper_view(), low_dsf, high_resolution.width(), | |
446 high_resolution.height(), kCustomWallpaperColor); | |
447 } | |
448 { | |
449 SCOPED_TRACE(base::StringPrintf("1200x600 low resolution")); | |
450 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
451 WallpaperFitToNativeResolution( | |
452 wallpaper_view(), low_dsf, low_resolution.width(), | |
453 low_resolution.height(), kCustomWallpaperColor); | |
454 } | |
455 | |
456 UpdateDisplay("1200x600/u@1.5"); // 1.5 ui scale | |
457 { | |
458 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution")); | |
459 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
460 WallpaperFitToNativeResolution( | |
461 wallpaper_view(), low_dsf, high_resolution.width(), | |
462 high_resolution.height(), kCustomWallpaperColor); | |
463 } | |
464 { | |
465 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution")); | |
466 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
467 WallpaperFitToNativeResolution( | |
468 wallpaper_view(), low_dsf, low_resolution.width(), | |
469 low_resolution.height(), kCustomWallpaperColor); | |
470 } | |
471 } | |
472 | |
473 } // namespace ash | |
OLD | NEW |