Update V8 to version 4.6.72.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_controller_unittest.cc
blobb51fca198b6fb936a4a23131d62e615a6194791f
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"
7 #include <cmath>
8 #include <cstdlib>
10 #include "ash/ash_switches.h"
11 #include "ash/desktop_background/desktop_background_widget_controller.h"
12 #include "ash/root_window_controller.h"
13 #include "ash/shell.h"
14 #include "ash/shell_window_ids.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/test/test_user_wallpaper_delegate.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "content/public/test/test_utils.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
24 #include "ui/compositor/test/layer_animator_test_controller.h"
26 using aura::RootWindow;
27 using aura::Window;
28 using wallpaper::WallpaperLayout;
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;
34 namespace ash {
35 namespace {
37 // Containers IDs used for tests.
38 const int kDesktopBackgroundId = ash::kShellWindowId_DesktopBackgroundContainer;
39 const int kLockScreenBackgroundId =
40 ash::kShellWindowId_LockScreenBackgroundContainer;
42 // Returns number of child windows in a shell window container.
43 int ChildCountForContainer(int container_id) {
44 Window* root = ash::Shell::GetPrimaryRootWindow();
45 Window* container = root->GetChildById(container_id);
46 return static_cast<int>(container->children().size());
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);
56 ui::Layer* layer = widget->GetNativeView()->layer();
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));
68 } // namespace
70 class DesktopBackgroundControllerTest : public test::AshTestBase {
71 public:
72 DesktopBackgroundControllerTest()
73 : controller_(NULL),
74 wallpaper_delegate_(NULL) {
76 ~DesktopBackgroundControllerTest() override {}
78 void SetUp() override {
79 test::AshTestBase::SetUp();
80 // Ash shell initialization creates wallpaper. Reset it so we can manually
81 // control wallpaper creation and animation in our tests.
82 RootWindowController* root_window_controller =
83 Shell::GetPrimaryRootWindowController();
84 root_window_controller->SetWallpaperController(NULL);
85 root_window_controller->SetAnimatingWallpaperController(NULL);
86 controller_ = Shell::GetInstance()->desktop_background_controller();
87 wallpaper_delegate_ = static_cast<test::TestUserWallpaperDelegate*>(
88 Shell::GetInstance()->user_wallpaper_delegate());
89 controller_->set_wallpaper_reload_delay_for_test(0);
92 protected:
93 // A color that can be passed to CreateImage(). Specifically chosen to not
94 // conflict with any of the default wallpaper colors.
95 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
97 // Creates an image of size |size|.
98 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
99 SkBitmap bitmap;
100 bitmap.allocN32Pixels(width, height);
101 bitmap.eraseColor(color);
102 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
103 return image;
106 // Runs kAnimatingDesktopController's animation to completion.
107 // TODO(bshe): Don't require tests to run animations; it's slow.
108 void RunDesktopControllerAnimation() {
109 DesktopBackgroundWidgetController* controller =
110 Shell::GetPrimaryRootWindowController()
111 ->animating_wallpaper_controller()
112 ->GetController(false);
113 EXPECT_TRUE(controller);
114 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget()));
117 DesktopBackgroundController* controller_; // Not owned.
119 test::TestUserWallpaperDelegate* wallpaper_delegate_;
121 private:
122 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest);
125 TEST_F(DesktopBackgroundControllerTest, BasicReparenting) {
126 DesktopBackgroundController* controller =
127 Shell::GetInstance()->desktop_background_controller();
128 controller->CreateEmptyWallpaper();
130 // Wallpaper view/window exists in the desktop background container and
131 // nothing is in the lock screen background container.
132 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
133 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
135 // Moving background to lock container should succeed the first time but
136 // subsequent calls should do nothing.
137 EXPECT_TRUE(controller->MoveDesktopToLockedContainer());
138 EXPECT_FALSE(controller->MoveDesktopToLockedContainer());
140 // One window is moved from desktop to lock container.
141 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
142 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId));
144 // Moving background to desktop container should succeed the first time.
145 EXPECT_TRUE(controller->MoveDesktopToUnlockedContainer());
146 EXPECT_FALSE(controller->MoveDesktopToUnlockedContainer());
148 // One window is moved from lock to desktop container.
149 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
150 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
153 TEST_F(DesktopBackgroundControllerTest, ControllerOwnership) {
154 // We cannot short-circuit animations for this test.
155 ui::ScopedAnimationDurationScaleMode test_duration_mode(
156 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
158 // Create wallpaper and background view.
159 DesktopBackgroundController* controller =
160 Shell::GetInstance()->desktop_background_controller();
161 controller->CreateEmptyWallpaper();
163 // The new wallpaper is ready to start animating. kAnimatingDesktopController
164 // holds the widget controller instance. kDesktopController will get it later.
165 RootWindowController* root_window_controller =
166 Shell::GetPrimaryRootWindowController();
167 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
168 GetController(false));
170 // kDesktopController will receive the widget controller when the animation
171 // is done.
172 EXPECT_FALSE(root_window_controller->wallpaper_controller());
174 // Force the widget's layer animation to play to completion.
175 RunDesktopControllerAnimation();
177 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
178 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
179 GetController(false));
180 EXPECT_TRUE(root_window_controller->wallpaper_controller());
183 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
184 // move all desktop views if there are more than one.
185 TEST_F(DesktopBackgroundControllerTest, BackgroundMovementDuringUnlock) {
186 // We cannot short-circuit animations for this test.
187 ui::ScopedAnimationDurationScaleMode test_duration_mode(
188 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
190 // Reset wallpaper state, see ControllerOwnership above.
191 DesktopBackgroundController* controller =
192 Shell::GetInstance()->desktop_background_controller();
193 controller->CreateEmptyWallpaper();
195 // Run wallpaper show animation to completion.
196 RunDesktopControllerAnimation();
198 // User locks the screen, which moves the background forward.
199 controller->MoveDesktopToLockedContainer();
201 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
202 // background that will animate in on top of the old one.
203 controller->CreateEmptyWallpaper();
205 // In this state we have two desktop background views stored in different
206 // properties. Both are in the lock screen background container.
207 RootWindowController* root_window_controller =
208 Shell::GetPrimaryRootWindowController();
209 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
210 GetController(false));
211 EXPECT_TRUE(root_window_controller->wallpaper_controller());
212 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
213 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId));
215 // Before the wallpaper's animation completes, user unlocks the screen, which
216 // moves the desktop to the back.
217 controller->MoveDesktopToUnlockedContainer();
219 // Ensure both desktop backgrounds have moved.
220 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId));
221 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
223 // Finish the new desktop background animation.
224 RunDesktopControllerAnimation();
226 // Now there is one desktop background, in the back.
227 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
228 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
231 // Test for crbug.com/156542. Animating wallpaper should immediately finish
232 // animation and replace current wallpaper before next animation starts.
233 TEST_F(DesktopBackgroundControllerTest, ChangeWallpaperQuick) {
234 // We cannot short-circuit animations for this test.
235 ui::ScopedAnimationDurationScaleMode test_duration_mode(
236 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
238 // Reset wallpaper state, see ControllerOwnership above.
239 DesktopBackgroundController* controller =
240 Shell::GetInstance()->desktop_background_controller();
241 controller->CreateEmptyWallpaper();
243 // Run wallpaper show animation to completion.
244 RunDesktopControllerAnimation();
246 // Change to a new wallpaper.
247 controller->CreateEmptyWallpaper();
249 RootWindowController* root_window_controller =
250 Shell::GetPrimaryRootWindowController();
251 DesktopBackgroundWidgetController* animating_controller =
252 root_window_controller->animating_wallpaper_controller()->GetController(
253 false);
254 EXPECT_TRUE(animating_controller);
255 EXPECT_TRUE(root_window_controller->wallpaper_controller());
257 // Change to another wallpaper before animation finished.
258 controller->CreateEmptyWallpaper();
260 // The animating controller should immediately move to desktop controller.
261 EXPECT_EQ(animating_controller,
262 root_window_controller->wallpaper_controller());
264 // Cache the new animating controller.
265 animating_controller = root_window_controller->
266 animating_wallpaper_controller()->GetController(false);
268 // Run wallpaper show animation to completion.
269 ASSERT_NO_FATAL_FAILURE(
270 RunAnimationForWidget(
271 root_window_controller->animating_wallpaper_controller()->
272 GetController(false)->widget()));
274 EXPECT_TRUE(root_window_controller->wallpaper_controller());
275 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
276 GetController(false));
277 // The desktop controller should be the last created animating controller.
278 EXPECT_EQ(animating_controller,
279 root_window_controller->wallpaper_controller());
282 TEST_F(DesktopBackgroundControllerTest, ResizeCustomWallpaper) {
283 if (!SupportsMultipleDisplays())
284 return;
286 UpdateDisplay("320x200");
288 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
290 // Set the image as custom wallpaper, wait for the resize to finish, and check
291 // that the resized image is the expected size.
292 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
293 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
294 content::RunAllBlockingPoolTasksUntilIdle();
295 gfx::ImageSkia resized_image = controller_->GetWallpaper();
296 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
297 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
299 // Load the original wallpaper again and check that we're still using the
300 // previously-resized image instead of doing another resize
301 // (http://crbug.com/321402).
302 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
303 content::RunAllBlockingPoolTasksUntilIdle();
304 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
307 TEST_F(DesktopBackgroundControllerTest, GetMaxDisplaySize) {
308 // Device scale factor shouldn't affect the native size.
309 UpdateDisplay("1000x300*2");
310 EXPECT_EQ(
311 "1000x300",
312 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
314 // Rotated display should return the rotated size.
315 UpdateDisplay("1000x300*2/r");
316 EXPECT_EQ(
317 "300x1000",
318 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
320 // UI Scaling shouldn't affect the native size.
321 UpdateDisplay("1000x300*2@1.5");
322 EXPECT_EQ(
323 "1000x300",
324 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
326 if (!SupportsMultipleDisplays())
327 return;
329 // First display has maximum size.
330 UpdateDisplay("400x300,100x100");
331 EXPECT_EQ(
332 "400x300",
333 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
335 // Second display has maximum size.
336 UpdateDisplay("400x300,500x600");
337 EXPECT_EQ(
338 "500x600",
339 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
341 // Maximum width and height belongs to different displays.
342 UpdateDisplay("400x300,100x500");
343 EXPECT_EQ(
344 "400x500",
345 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
349 } // namespace ash