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.
5 #include "ash/desktop_background/desktop_background_controller.h"
10 #include "ash/ash_switches.h"
11 #include "ash/desktop_background/desktop_background_view.h"
12 #include "ash/desktop_background/desktop_background_widget_controller.h"
13 #include "ash/root_window_controller.h"
14 #include "ash/shell.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/test_user_wallpaper_delegate.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "ui/aura/window_event_dispatcher.h"
26 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
27 #include "ui/compositor/test/layer_animator_test_controller.h"
28 #include "ui/gfx/canvas.h"
30 using aura::RootWindow
;
32 using wallpaper::WallpaperLayout
;
33 using wallpaper::WALLPAPER_LAYOUT_CENTER
;
34 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED
;
35 using wallpaper::WALLPAPER_LAYOUT_STRETCH
;
36 using wallpaper::WALLPAPER_LAYOUT_TILE
;
41 // Containers IDs used for tests.
42 const int kDesktopBackgroundId
= ash::kShellWindowId_DesktopBackgroundContainer
;
43 const int kLockScreenBackgroundId
=
44 ash::kShellWindowId_LockScreenBackgroundContainer
;
46 // Returns number of child windows in a shell window container.
47 int ChildCountForContainer(int container_id
) {
48 Window
* root
= ash::Shell::GetPrimaryRootWindow();
49 Window
* container
= root
->GetChildById(container_id
);
50 return static_cast<int>(container
->children().size());
53 // Steps a widget's layer animation until it is completed. Animations must be
55 void RunAnimationForWidget(views::Widget
* widget
) {
56 // Animations must be enabled for stepping to work.
57 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
58 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION
);
60 ui::Layer
* layer
= widget
->GetNativeView()->layer();
61 ui::LayerAnimatorTestController
controller(layer
->GetAnimator());
62 // Multiple steps are required to complete complex animations.
63 // TODO(vollick): This should not be necessary. crbug.com/154017
64 while (controller
.animator()->is_animating()) {
65 controller
.StartThreadedAnimationsIfNeeded();
66 base::TimeTicks step_time
= controller
.animator()->last_step_time();
67 layer
->GetAnimator()->Step(step_time
+
68 base::TimeDelta::FromMilliseconds(1000));
74 class DesktopBackgroundControllerTest
: public test::AshTestBase
{
76 DesktopBackgroundControllerTest()
78 wallpaper_delegate_(NULL
) {
80 ~DesktopBackgroundControllerTest() override
{}
82 void SetUp() override
{
83 test::AshTestBase::SetUp();
84 // Ash shell initialization creates wallpaper. Reset it so we can manually
85 // control wallpaper creation and animation in our tests.
86 RootWindowController
* root_window_controller
=
87 Shell::GetPrimaryRootWindowController();
88 root_window_controller
->SetWallpaperController(NULL
);
89 root_window_controller
->SetAnimatingWallpaperController(NULL
);
90 controller_
= Shell::GetInstance()->desktop_background_controller();
91 wallpaper_delegate_
= static_cast<test::TestUserWallpaperDelegate
*>(
92 Shell::GetInstance()->user_wallpaper_delegate());
93 controller_
->set_wallpaper_reload_delay_for_test(0);
96 DesktopBackgroundView
* desktop_background_view() {
97 DesktopBackgroundWidgetController
* controller
=
98 Shell::GetPrimaryRootWindowController()
99 ->animating_wallpaper_controller()
100 ->GetController(false);
101 EXPECT_TRUE(controller
);
102 return static_cast<DesktopBackgroundView
*>(
103 controller
->widget()->GetContentsView()->child_at(0));
107 // A color that can be passed to CreateImage(). Specifically chosen to not
108 // conflict with any of the default wallpaper colors.
109 static const SkColor kCustomWallpaperColor
= SK_ColorMAGENTA
;
111 // Creates an image of size |size|.
112 gfx::ImageSkia
CreateImage(int width
, int height
, SkColor color
) {
114 bitmap
.allocN32Pixels(width
, height
);
115 bitmap
.eraseColor(color
);
116 gfx::ImageSkia image
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
120 // Helper function that tests the wallpaper is always fitted to the native
121 // display resolution when the layout is WALLPAPER_LAYOUT_CENTER.
122 void WallpaperFitToNativeResolution(DesktopBackgroundView
* view
,
123 float device_scale_factor
,
127 gfx::Size size
= view
->bounds().size();
128 gfx::Canvas
canvas(size
, device_scale_factor
, true);
129 view
->OnPaint(&canvas
);
131 int canvas_width
= canvas
.sk_canvas()->imageInfo().width();
132 int canvas_height
= canvas
.sk_canvas()->imageInfo().height();
134 bitmap
.allocN32Pixels(canvas_width
, canvas_height
);
135 canvas
.sk_canvas()->readPixels(&bitmap
, 0, 0);
137 for (int i
= 0; i
< canvas_width
; i
++) {
138 for (int j
= 0; j
< canvas_height
; j
++) {
139 if (i
>= (canvas_width
- image_width
) / 2 &&
140 i
< (canvas_width
+ image_width
) / 2 &&
141 j
>= (canvas_height
- image_height
) / 2 &&
142 j
< (canvas_height
+ image_height
) / 2) {
143 EXPECT_EQ(color
, bitmap
.getColor(i
, j
));
145 EXPECT_EQ(SK_ColorBLACK
, bitmap
.getColor(i
, j
));
151 // Runs kAnimatingDesktopController's animation to completion.
152 // TODO(bshe): Don't require tests to run animations; it's slow.
153 void RunDesktopControllerAnimation() {
154 DesktopBackgroundWidgetController
* controller
=
155 Shell::GetPrimaryRootWindowController()
156 ->animating_wallpaper_controller()
157 ->GetController(false);
158 EXPECT_TRUE(controller
);
159 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller
->widget()));
162 DesktopBackgroundController
* controller_
; // Not owned.
164 test::TestUserWallpaperDelegate
* wallpaper_delegate_
;
167 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest
);
170 TEST_F(DesktopBackgroundControllerTest
, BasicReparenting
) {
171 DesktopBackgroundController
* controller
=
172 Shell::GetInstance()->desktop_background_controller();
173 controller
->CreateEmptyWallpaper();
175 // Wallpaper view/window exists in the desktop background container and
176 // nothing is in the lock screen background container.
177 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId
));
178 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId
));
180 // Moving background to lock container should succeed the first time but
181 // subsequent calls should do nothing.
182 EXPECT_TRUE(controller
->MoveDesktopToLockedContainer());
183 EXPECT_FALSE(controller
->MoveDesktopToLockedContainer());
185 // One window is moved from desktop to lock container.
186 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId
));
187 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId
));
189 // Moving background to desktop container should succeed the first time.
190 EXPECT_TRUE(controller
->MoveDesktopToUnlockedContainer());
191 EXPECT_FALSE(controller
->MoveDesktopToUnlockedContainer());
193 // One window is moved from lock to desktop container.
194 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId
));
195 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId
));
198 TEST_F(DesktopBackgroundControllerTest
, ControllerOwnership
) {
199 // We cannot short-circuit animations for this test.
200 ui::ScopedAnimationDurationScaleMode
test_duration_mode(
201 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
203 // Create wallpaper and background view.
204 DesktopBackgroundController
* controller
=
205 Shell::GetInstance()->desktop_background_controller();
206 controller
->CreateEmptyWallpaper();
208 // The new wallpaper is ready to start animating. kAnimatingDesktopController
209 // holds the widget controller instance. kDesktopController will get it later.
210 RootWindowController
* root_window_controller
=
211 Shell::GetPrimaryRootWindowController();
212 EXPECT_TRUE(root_window_controller
->animating_wallpaper_controller()->
213 GetController(false));
215 // kDesktopController will receive the widget controller when the animation
217 EXPECT_FALSE(root_window_controller
->wallpaper_controller());
219 // Force the widget's layer animation to play to completion.
220 RunDesktopControllerAnimation();
222 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
223 EXPECT_FALSE(root_window_controller
->animating_wallpaper_controller()->
224 GetController(false));
225 EXPECT_TRUE(root_window_controller
->wallpaper_controller());
228 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
229 // move all desktop views if there are more than one.
230 TEST_F(DesktopBackgroundControllerTest
, BackgroundMovementDuringUnlock
) {
231 // We cannot short-circuit animations for this test.
232 ui::ScopedAnimationDurationScaleMode
test_duration_mode(
233 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
235 // Reset wallpaper state, see ControllerOwnership above.
236 DesktopBackgroundController
* controller
=
237 Shell::GetInstance()->desktop_background_controller();
238 controller
->CreateEmptyWallpaper();
240 // Run wallpaper show animation to completion.
241 RunDesktopControllerAnimation();
243 // User locks the screen, which moves the background forward.
244 controller
->MoveDesktopToLockedContainer();
246 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
247 // background that will animate in on top of the old one.
248 controller
->CreateEmptyWallpaper();
250 // In this state we have two desktop background views stored in different
251 // properties. Both are in the lock screen background container.
252 RootWindowController
* root_window_controller
=
253 Shell::GetPrimaryRootWindowController();
254 EXPECT_TRUE(root_window_controller
->animating_wallpaper_controller()->
255 GetController(false));
256 EXPECT_TRUE(root_window_controller
->wallpaper_controller());
257 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId
));
258 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId
));
260 // Before the wallpaper's animation completes, user unlocks the screen, which
261 // moves the desktop to the back.
262 controller
->MoveDesktopToUnlockedContainer();
264 // Ensure both desktop backgrounds have moved.
265 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId
));
266 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId
));
268 // Finish the new desktop background animation.
269 RunDesktopControllerAnimation();
271 // Now there is one desktop background, in the back.
272 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId
));
273 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId
));
276 // Test for crbug.com/156542. Animating wallpaper should immediately finish
277 // animation and replace current wallpaper before next animation starts.
278 TEST_F(DesktopBackgroundControllerTest
, ChangeWallpaperQuick
) {
279 // We cannot short-circuit animations for this test.
280 ui::ScopedAnimationDurationScaleMode
test_duration_mode(
281 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
283 // Reset wallpaper state, see ControllerOwnership above.
284 DesktopBackgroundController
* controller
=
285 Shell::GetInstance()->desktop_background_controller();
286 controller
->CreateEmptyWallpaper();
288 // Run wallpaper show animation to completion.
289 RunDesktopControllerAnimation();
291 // Change to a new wallpaper.
292 controller
->CreateEmptyWallpaper();
294 RootWindowController
* root_window_controller
=
295 Shell::GetPrimaryRootWindowController();
296 DesktopBackgroundWidgetController
* animating_controller
=
297 root_window_controller
->animating_wallpaper_controller()->GetController(
299 EXPECT_TRUE(animating_controller
);
300 EXPECT_TRUE(root_window_controller
->wallpaper_controller());
302 // Change to another wallpaper before animation finished.
303 controller
->CreateEmptyWallpaper();
305 // The animating controller should immediately move to desktop controller.
306 EXPECT_EQ(animating_controller
,
307 root_window_controller
->wallpaper_controller());
309 // Cache the new animating controller.
310 animating_controller
= root_window_controller
->
311 animating_wallpaper_controller()->GetController(false);
313 // Run wallpaper show animation to completion.
314 ASSERT_NO_FATAL_FAILURE(
315 RunAnimationForWidget(
316 root_window_controller
->animating_wallpaper_controller()->
317 GetController(false)->widget()));
319 EXPECT_TRUE(root_window_controller
->wallpaper_controller());
320 EXPECT_FALSE(root_window_controller
->animating_wallpaper_controller()->
321 GetController(false));
322 // The desktop controller should be the last created animating controller.
323 EXPECT_EQ(animating_controller
,
324 root_window_controller
->wallpaper_controller());
327 TEST_F(DesktopBackgroundControllerTest
, ResizeCustomWallpaper
) {
328 if (!SupportsMultipleDisplays())
331 UpdateDisplay("320x200");
333 gfx::ImageSkia image
= CreateImage(640, 480, kCustomWallpaperColor
);
335 // Set the image as custom wallpaper, wait for the resize to finish, and check
336 // that the resized image is the expected size.
337 controller_
->SetWallpaperImage(image
, WALLPAPER_LAYOUT_STRETCH
);
338 EXPECT_TRUE(image
.BackedBySameObjectAs(controller_
->GetWallpaper()));
339 content::RunAllBlockingPoolTasksUntilIdle();
340 gfx::ImageSkia resized_image
= controller_
->GetWallpaper();
341 EXPECT_FALSE(image
.BackedBySameObjectAs(resized_image
));
342 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image
.size().ToString());
344 // Load the original wallpaper again and check that we're still using the
345 // previously-resized image instead of doing another resize
346 // (http://crbug.com/321402).
347 controller_
->SetWallpaperImage(image
, WALLPAPER_LAYOUT_STRETCH
);
348 content::RunAllBlockingPoolTasksUntilIdle();
349 EXPECT_TRUE(resized_image
.BackedBySameObjectAs(controller_
->GetWallpaper()));
352 TEST_F(DesktopBackgroundControllerTest
, GetMaxDisplaySize
) {
353 // Device scale factor shouldn't affect the native size.
354 UpdateDisplay("1000x300*2");
357 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
359 // Rotated display should return the rotated size.
360 UpdateDisplay("1000x300*2/r");
363 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
365 // UI Scaling shouldn't affect the native size.
366 UpdateDisplay("1000x300*2@1.5");
369 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
371 if (!SupportsMultipleDisplays())
374 // First display has maximum size.
375 UpdateDisplay("400x300,100x100");
378 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
380 // Second display has maximum size.
381 UpdateDisplay("400x300,500x600");
384 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
386 // Maximum width and height belongs to different displays.
387 UpdateDisplay("400x300,100x500");
390 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
393 // Test that the wallpaper is always fitted to the native display resolution
394 // when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images.
395 TEST_F(DesktopBackgroundControllerTest
, DontSacleWallpaperWithCenterLayout
) {
396 // We cannot short-circuit animations for this test.
397 ui::ScopedAnimationDurationScaleMode
test_duration_mode(
398 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
400 const gfx::Size
high_resolution(3600, 2400);
401 const gfx::Size
low_resolution(360, 240);
402 const float high_dsf
= 2.0f
;
403 const float low_dsf
= 1.0f
;
405 gfx::ImageSkia image_high_res
= CreateImage(
406 high_resolution
.width(), high_resolution
.height(), kCustomWallpaperColor
);
407 gfx::ImageSkia image_low_res
= CreateImage(
408 low_resolution
.width(), low_resolution
.height(), kCustomWallpaperColor
);
410 UpdateDisplay("1200x600*2");
412 SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution"));
413 controller_
->SetWallpaperImage(image_high_res
, WALLPAPER_LAYOUT_CENTER
);
414 WallpaperFitToNativeResolution(
415 desktop_background_view(), high_dsf
, high_resolution
.width(),
416 high_resolution
.height(), kCustomWallpaperColor
);
419 SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution"));
420 controller_
->SetWallpaperImage(image_low_res
, WALLPAPER_LAYOUT_CENTER
);
421 WallpaperFitToNativeResolution(
422 desktop_background_view(), high_dsf
, low_resolution
.width(),
423 low_resolution
.height(), kCustomWallpaperColor
);
426 UpdateDisplay("1200x600");
428 SCOPED_TRACE(base::StringPrintf("1200x600 high resolution"));
429 controller_
->SetWallpaperImage(image_high_res
, WALLPAPER_LAYOUT_CENTER
);
430 WallpaperFitToNativeResolution(
431 desktop_background_view(), low_dsf
, high_resolution
.width(),
432 high_resolution
.height(), kCustomWallpaperColor
);
435 SCOPED_TRACE(base::StringPrintf("1200x600 low resolution"));
436 controller_
->SetWallpaperImage(image_low_res
, WALLPAPER_LAYOUT_CENTER
);
437 WallpaperFitToNativeResolution(
438 desktop_background_view(), low_dsf
, low_resolution
.width(),
439 low_resolution
.height(), kCustomWallpaperColor
);
442 UpdateDisplay("1200x600/u@1.5"); // 1.5 ui scale
444 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution"));
445 controller_
->SetWallpaperImage(image_high_res
, WALLPAPER_LAYOUT_CENTER
);
446 WallpaperFitToNativeResolution(
447 desktop_background_view(), low_dsf
, high_resolution
.width(),
448 high_resolution
.height(), kCustomWallpaperColor
);
451 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution"));
452 controller_
->SetWallpaperImage(image_low_res
, WALLPAPER_LAYOUT_CENTER
);
453 WallpaperFitToNativeResolution(
454 desktop_background_view(), low_dsf
, low_resolution
.width(),
455 low_resolution
.height(), kCustomWallpaperColor
);