Disable accessible touch exploration by default.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_controller_unittest.cc
blobfa2e2a44e4a5ba0684bea236356f6056ea7f577e
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/display_manager_test_api.h"
17 #include "ash/test/test_user_wallpaper_delegate.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "content/public/test/test_utils.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"
29 using aura::RootWindow;
30 using aura::Window;
32 namespace ash {
33 namespace {
35 // Containers IDs used for tests.
36 const int kDesktopBackgroundId = ash::kShellWindowId_DesktopBackgroundContainer;
37 const int kLockScreenBackgroundId =
38 ash::kShellWindowId_LockScreenBackgroundContainer;
40 // Returns number of child windows in a shell window container.
41 int ChildCountForContainer(int container_id) {
42 Window* root = ash::Shell::GetPrimaryRootWindow();
43 Window* container = root->GetChildById(container_id);
44 return static_cast<int>(container->children().size());
47 // Steps a widget's layer animation until it is completed. Animations must be
48 // enabled.
49 void RunAnimationForWidget(views::Widget* widget) {
50 // Animations must be enabled for stepping to work.
51 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
52 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
54 ui::Layer* layer = widget->GetNativeView()->layer();
55 ui::LayerAnimatorTestController controller(layer->GetAnimator());
56 // Multiple steps are required to complete complex animations.
57 // TODO(vollick): This should not be necessary. crbug.com/154017
58 while (controller.animator()->is_animating()) {
59 controller.StartThreadedAnimationsIfNeeded();
60 base::TimeTicks step_time = controller.animator()->last_step_time();
61 layer->GetAnimator()->Step(step_time +
62 base::TimeDelta::FromMilliseconds(1000));
66 } // namespace
68 class DesktopBackgroundControllerTest : public test::AshTestBase {
69 public:
70 DesktopBackgroundControllerTest()
71 : controller_(NULL) {
73 virtual ~DesktopBackgroundControllerTest() {}
75 virtual void SetUp() OVERRIDE {
76 test::AshTestBase::SetUp();
77 // Ash shell initialization creates wallpaper. Reset it so we can manually
78 // control wallpaper creation and animation in our tests.
79 RootWindowController* root_window_controller =
80 Shell::GetPrimaryRootWindowController();
81 root_window_controller->SetWallpaperController(NULL);
82 root_window_controller->SetAnimatingWallpaperController(NULL);
83 controller_ = Shell::GetInstance()->desktop_background_controller();
84 wallpaper_delegate_ = static_cast<test::TestUserWallpaperDelegate*>(
85 Shell::GetInstance()->user_wallpaper_delegate());
86 controller_->set_wallpaper_reload_delay_for_test(0);
89 protected:
90 // A color that can be passed to CreateImage(). Specifically chosen to not
91 // conflict with any of the default wallpaper colors.
92 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
94 // Creates an image of size |size|.
95 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
96 SkBitmap bitmap;
97 bitmap.allocN32Pixels(width, height);
98 bitmap.eraseColor(color);
99 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
100 return image;
103 // Runs kAnimatingDesktopController's animation to completion.
104 // TODO(bshe): Don't require tests to run animations; it's slow.
105 void RunDesktopControllerAnimation() {
106 DesktopBackgroundWidgetController* controller =
107 Shell::GetPrimaryRootWindowController()
108 ->animating_wallpaper_controller()
109 ->GetController(false);
110 EXPECT_TRUE(!!controller);
111 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget()));
114 DesktopBackgroundController* controller_; // Not owned.
116 test::TestUserWallpaperDelegate* wallpaper_delegate_;
118 private:
119 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest);
122 TEST_F(DesktopBackgroundControllerTest, BasicReparenting) {
123 DesktopBackgroundController* controller =
124 Shell::GetInstance()->desktop_background_controller();
125 controller->CreateEmptyWallpaper();
127 // Wallpaper view/window exists in the desktop background container and
128 // nothing is in the lock screen background container.
129 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
130 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
132 // Moving background to lock container should succeed the first time but
133 // subsequent calls should do nothing.
134 EXPECT_TRUE(controller->MoveDesktopToLockedContainer());
135 EXPECT_FALSE(controller->MoveDesktopToLockedContainer());
137 // One window is moved from desktop to lock container.
138 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
139 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId));
141 // Moving background to desktop container should succeed the first time.
142 EXPECT_TRUE(controller->MoveDesktopToUnlockedContainer());
143 EXPECT_FALSE(controller->MoveDesktopToUnlockedContainer());
145 // One window is moved from lock to desktop container.
146 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
147 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
150 TEST_F(DesktopBackgroundControllerTest, ControllerOwnership) {
151 // We cannot short-circuit animations for this test.
152 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
153 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
155 // Create wallpaper and background view.
156 DesktopBackgroundController* controller =
157 Shell::GetInstance()->desktop_background_controller();
158 controller->CreateEmptyWallpaper();
160 // The new wallpaper is ready to start animating. kAnimatingDesktopController
161 // holds the widget controller instance. kDesktopController will get it later.
162 RootWindowController* root_window_controller =
163 Shell::GetPrimaryRootWindowController();
164 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
165 GetController(false));
167 // kDesktopController will receive the widget controller when the animation
168 // is done.
169 EXPECT_FALSE(root_window_controller->wallpaper_controller());
171 // Force the widget's layer animation to play to completion.
172 RunDesktopControllerAnimation();
174 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
175 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
176 GetController(false));
177 EXPECT_TRUE(root_window_controller->wallpaper_controller());
180 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
181 // move all desktop views if there are more than one.
182 TEST_F(DesktopBackgroundControllerTest, BackgroundMovementDuringUnlock) {
183 // We cannot short-circuit animations for this test.
184 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
185 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
187 // Reset wallpaper state, see ControllerOwnership above.
188 DesktopBackgroundController* controller =
189 Shell::GetInstance()->desktop_background_controller();
190 controller->CreateEmptyWallpaper();
192 // Run wallpaper show animation to completion.
193 RunDesktopControllerAnimation();
195 // User locks the screen, which moves the background forward.
196 controller->MoveDesktopToLockedContainer();
198 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
199 // background that will animate in on top of the old one.
200 controller->CreateEmptyWallpaper();
202 // In this state we have two desktop background views stored in different
203 // properties. Both are in the lock screen background container.
204 RootWindowController* root_window_controller =
205 Shell::GetPrimaryRootWindowController();
206 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
207 GetController(false));
208 EXPECT_TRUE(root_window_controller->wallpaper_controller());
209 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
210 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId));
212 // Before the wallpaper's animation completes, user unlocks the screen, which
213 // moves the desktop to the back.
214 controller->MoveDesktopToUnlockedContainer();
216 // Ensure both desktop backgrounds have moved.
217 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId));
218 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
220 // Finish the new desktop background animation.
221 RunDesktopControllerAnimation();
223 // Now there is one desktop background, in the back.
224 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
225 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
228 // Test for crbug.com/156542. Animating wallpaper should immediately finish
229 // animation and replace current wallpaper before next animation starts.
230 TEST_F(DesktopBackgroundControllerTest, ChangeWallpaperQuick) {
231 // We cannot short-circuit animations for this test.
232 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
233 ui::ScopedAnimationDurationScaleMode::NORMAL_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 // Change to a new wallpaper.
244 controller->CreateEmptyWallpaper();
246 RootWindowController* root_window_controller =
247 Shell::GetPrimaryRootWindowController();
248 DesktopBackgroundWidgetController* animating_controller =
249 root_window_controller->animating_wallpaper_controller()->GetController(
250 false);
251 EXPECT_TRUE(animating_controller);
252 EXPECT_TRUE(root_window_controller->wallpaper_controller());
254 // Change to another wallpaper before animation finished.
255 controller->CreateEmptyWallpaper();
257 // The animating controller should immediately move to desktop controller.
258 EXPECT_EQ(animating_controller,
259 root_window_controller->wallpaper_controller());
261 // Cache the new animating controller.
262 animating_controller = root_window_controller->
263 animating_wallpaper_controller()->GetController(false);
265 // Run wallpaper show animation to completion.
266 ASSERT_NO_FATAL_FAILURE(
267 RunAnimationForWidget(
268 root_window_controller->animating_wallpaper_controller()->
269 GetController(false)->widget()));
271 EXPECT_TRUE(root_window_controller->wallpaper_controller());
272 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
273 GetController(false));
274 // The desktop controller should be the last created animating controller.
275 EXPECT_EQ(animating_controller,
276 root_window_controller->wallpaper_controller());
279 TEST_F(DesktopBackgroundControllerTest, ResizeCustomWallpaper) {
280 if (!SupportsMultipleDisplays())
281 return;
283 test::DisplayManagerTestApi display_manager_test_api(
284 Shell::GetInstance()->display_manager());
285 display_manager_test_api.UpdateDisplay("320x200");
287 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
289 // Set the image as custom wallpaper, wait for the resize to finish, and check
290 // that the resized image is the expected size.
291 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
292 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
293 content::BrowserThread::GetBlockingPool()->FlushForTesting();
294 content::RunAllPendingInMessageLoop();
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::BrowserThread::GetBlockingPool()->FlushForTesting();
304 content::RunAllPendingInMessageLoop();
305 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
308 TEST_F(DesktopBackgroundControllerTest, GetMaxDisplaySize) {
309 // Device scale factor shouldn't affect the native size.
310 UpdateDisplay("1000x300*2");
311 EXPECT_EQ(
312 "1000x300",
313 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
315 // Rotated display should return the rotated size.
316 UpdateDisplay("1000x300*2/r");
317 EXPECT_EQ(
318 "300x1000",
319 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
321 // UI Scaling shouldn't affect the native size.
322 UpdateDisplay("1000x300*2@1.5");
323 EXPECT_EQ(
324 "1000x300",
325 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
327 if (!SupportsMultipleDisplays())
328 return;
330 // First display has maximum size.
331 UpdateDisplay("400x300,100x100");
332 EXPECT_EQ(
333 "400x300",
334 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
336 // Second display has maximum size.
337 UpdateDisplay("400x300,500x600");
338 EXPECT_EQ(
339 "500x600",
340 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
342 // Maximum width and height belongs to different displays.
343 UpdateDisplay("400x300,100x500");
344 EXPECT_EQ(
345 "400x500",
346 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
350 } // namespace ash