Add logging.store and logging.uploadStored to thunk.js for Hangouts.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_controller_unittest.cc
blobecd1c3a0f4e4d256cc1682f31deb058c575301e9
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/test/test_utils.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
25 #include "ui/compositor/test/layer_animator_test_controller.h"
27 using aura::RootWindow;
28 using aura::Window;
29 using wallpaper::WallpaperLayout;
30 using wallpaper::WALLPAPER_LAYOUT_CENTER;
31 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
32 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
33 using wallpaper::WALLPAPER_LAYOUT_TILE;
35 namespace ash {
36 namespace {
38 // Containers IDs used for tests.
39 const int kDesktopBackgroundId = ash::kShellWindowId_DesktopBackgroundContainer;
40 const int kLockScreenBackgroundId =
41 ash::kShellWindowId_LockScreenBackgroundContainer;
43 // Returns number of child windows in a shell window container.
44 int ChildCountForContainer(int container_id) {
45 Window* root = ash::Shell::GetPrimaryRootWindow();
46 Window* container = root->GetChildById(container_id);
47 return static_cast<int>(container->children().size());
50 // Steps a widget's layer animation until it is completed. Animations must be
51 // enabled.
52 void RunAnimationForWidget(views::Widget* widget) {
53 // Animations must be enabled for stepping to work.
54 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
55 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
57 ui::Layer* layer = widget->GetNativeView()->layer();
58 ui::LayerAnimatorTestController controller(layer->GetAnimator());
59 // Multiple steps are required to complete complex animations.
60 // TODO(vollick): This should not be necessary. crbug.com/154017
61 while (controller.animator()->is_animating()) {
62 controller.StartThreadedAnimationsIfNeeded();
63 base::TimeTicks step_time = controller.animator()->last_step_time();
64 layer->GetAnimator()->Step(step_time +
65 base::TimeDelta::FromMilliseconds(1000));
69 } // namespace
71 class DesktopBackgroundControllerTest : public test::AshTestBase {
72 public:
73 DesktopBackgroundControllerTest()
74 : controller_(NULL),
75 wallpaper_delegate_(NULL) {
77 ~DesktopBackgroundControllerTest() override {}
79 void SetUp() override {
80 test::AshTestBase::SetUp();
81 // Ash shell initialization creates wallpaper. Reset it so we can manually
82 // control wallpaper creation and animation in our tests.
83 RootWindowController* root_window_controller =
84 Shell::GetPrimaryRootWindowController();
85 root_window_controller->SetWallpaperController(NULL);
86 root_window_controller->SetAnimatingWallpaperController(NULL);
87 controller_ = Shell::GetInstance()->desktop_background_controller();
88 wallpaper_delegate_ = static_cast<test::TestUserWallpaperDelegate*>(
89 Shell::GetInstance()->user_wallpaper_delegate());
90 controller_->set_wallpaper_reload_delay_for_test(0);
93 protected:
94 // A color that can be passed to CreateImage(). Specifically chosen to not
95 // conflict with any of the default wallpaper colors.
96 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
98 // Creates an image of size |size|.
99 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
100 SkBitmap bitmap;
101 bitmap.allocN32Pixels(width, height);
102 bitmap.eraseColor(color);
103 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
104 return image;
107 // Runs kAnimatingDesktopController's animation to completion.
108 // TODO(bshe): Don't require tests to run animations; it's slow.
109 void RunDesktopControllerAnimation() {
110 DesktopBackgroundWidgetController* controller =
111 Shell::GetPrimaryRootWindowController()
112 ->animating_wallpaper_controller()
113 ->GetController(false);
114 EXPECT_TRUE(controller);
115 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget()));
118 DesktopBackgroundController* controller_; // Not owned.
120 test::TestUserWallpaperDelegate* wallpaper_delegate_;
122 private:
123 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest);
126 TEST_F(DesktopBackgroundControllerTest, BasicReparenting) {
127 DesktopBackgroundController* controller =
128 Shell::GetInstance()->desktop_background_controller();
129 controller->CreateEmptyWallpaper();
131 // Wallpaper view/window exists in the desktop background container and
132 // nothing is in the lock screen background container.
133 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
134 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
136 // Moving background to lock container should succeed the first time but
137 // subsequent calls should do nothing.
138 EXPECT_TRUE(controller->MoveDesktopToLockedContainer());
139 EXPECT_FALSE(controller->MoveDesktopToLockedContainer());
141 // One window is moved from desktop to lock container.
142 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
143 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId));
145 // Moving background to desktop container should succeed the first time.
146 EXPECT_TRUE(controller->MoveDesktopToUnlockedContainer());
147 EXPECT_FALSE(controller->MoveDesktopToUnlockedContainer());
149 // One window is moved from lock to desktop container.
150 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
151 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
154 TEST_F(DesktopBackgroundControllerTest, ControllerOwnership) {
155 // We cannot short-circuit animations for this test.
156 ui::ScopedAnimationDurationScaleMode test_duration_mode(
157 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
159 // Create wallpaper and background view.
160 DesktopBackgroundController* controller =
161 Shell::GetInstance()->desktop_background_controller();
162 controller->CreateEmptyWallpaper();
164 // The new wallpaper is ready to start animating. kAnimatingDesktopController
165 // holds the widget controller instance. kDesktopController will get it later.
166 RootWindowController* root_window_controller =
167 Shell::GetPrimaryRootWindowController();
168 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
169 GetController(false));
171 // kDesktopController will receive the widget controller when the animation
172 // is done.
173 EXPECT_FALSE(root_window_controller->wallpaper_controller());
175 // Force the widget's layer animation to play to completion.
176 RunDesktopControllerAnimation();
178 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
179 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
180 GetController(false));
181 EXPECT_TRUE(root_window_controller->wallpaper_controller());
184 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
185 // move all desktop views if there are more than one.
186 TEST_F(DesktopBackgroundControllerTest, BackgroundMovementDuringUnlock) {
187 // We cannot short-circuit animations for this test.
188 ui::ScopedAnimationDurationScaleMode test_duration_mode(
189 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
191 // Reset wallpaper state, see ControllerOwnership above.
192 DesktopBackgroundController* controller =
193 Shell::GetInstance()->desktop_background_controller();
194 controller->CreateEmptyWallpaper();
196 // Run wallpaper show animation to completion.
197 RunDesktopControllerAnimation();
199 // User locks the screen, which moves the background forward.
200 controller->MoveDesktopToLockedContainer();
202 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
203 // background that will animate in on top of the old one.
204 controller->CreateEmptyWallpaper();
206 // In this state we have two desktop background views stored in different
207 // properties. Both are in the lock screen background container.
208 RootWindowController* root_window_controller =
209 Shell::GetPrimaryRootWindowController();
210 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
211 GetController(false));
212 EXPECT_TRUE(root_window_controller->wallpaper_controller());
213 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
214 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId));
216 // Before the wallpaper's animation completes, user unlocks the screen, which
217 // moves the desktop to the back.
218 controller->MoveDesktopToUnlockedContainer();
220 // Ensure both desktop backgrounds have moved.
221 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId));
222 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
224 // Finish the new desktop background animation.
225 RunDesktopControllerAnimation();
227 // Now there is one desktop background, in the back.
228 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
229 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
232 // Test for crbug.com/156542. Animating wallpaper should immediately finish
233 // animation and replace current wallpaper before next animation starts.
234 TEST_F(DesktopBackgroundControllerTest, ChangeWallpaperQuick) {
235 // We cannot short-circuit animations for this test.
236 ui::ScopedAnimationDurationScaleMode test_duration_mode(
237 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
239 // Reset wallpaper state, see ControllerOwnership above.
240 DesktopBackgroundController* controller =
241 Shell::GetInstance()->desktop_background_controller();
242 controller->CreateEmptyWallpaper();
244 // Run wallpaper show animation to completion.
245 RunDesktopControllerAnimation();
247 // Change to a new wallpaper.
248 controller->CreateEmptyWallpaper();
250 RootWindowController* root_window_controller =
251 Shell::GetPrimaryRootWindowController();
252 DesktopBackgroundWidgetController* animating_controller =
253 root_window_controller->animating_wallpaper_controller()->GetController(
254 false);
255 EXPECT_TRUE(animating_controller);
256 EXPECT_TRUE(root_window_controller->wallpaper_controller());
258 // Change to another wallpaper before animation finished.
259 controller->CreateEmptyWallpaper();
261 // The animating controller should immediately move to desktop controller.
262 EXPECT_EQ(animating_controller,
263 root_window_controller->wallpaper_controller());
265 // Cache the new animating controller.
266 animating_controller = root_window_controller->
267 animating_wallpaper_controller()->GetController(false);
269 // Run wallpaper show animation to completion.
270 ASSERT_NO_FATAL_FAILURE(
271 RunAnimationForWidget(
272 root_window_controller->animating_wallpaper_controller()->
273 GetController(false)->widget()));
275 EXPECT_TRUE(root_window_controller->wallpaper_controller());
276 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
277 GetController(false));
278 // The desktop controller should be the last created animating controller.
279 EXPECT_EQ(animating_controller,
280 root_window_controller->wallpaper_controller());
283 TEST_F(DesktopBackgroundControllerTest, ResizeCustomWallpaper) {
284 if (!SupportsMultipleDisplays())
285 return;
287 test::DisplayManagerTestApi display_manager_test_api(
288 Shell::GetInstance()->display_manager());
289 display_manager_test_api.UpdateDisplay("320x200");
291 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
293 // Set the image as custom wallpaper, wait for the resize to finish, and check
294 // that the resized image is the expected size.
295 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
296 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
297 content::RunAllBlockingPoolTasksUntilIdle();
298 gfx::ImageSkia resized_image = controller_->GetWallpaper();
299 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
300 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
302 // Load the original wallpaper again and check that we're still using the
303 // previously-resized image instead of doing another resize
304 // (http://crbug.com/321402).
305 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
306 content::RunAllBlockingPoolTasksUntilIdle();
307 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
310 TEST_F(DesktopBackgroundControllerTest, GetMaxDisplaySize) {
311 // Device scale factor shouldn't affect the native size.
312 UpdateDisplay("1000x300*2");
313 EXPECT_EQ(
314 "1000x300",
315 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
317 // Rotated display should return the rotated size.
318 UpdateDisplay("1000x300*2/r");
319 EXPECT_EQ(
320 "300x1000",
321 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
323 // UI Scaling shouldn't affect the native size.
324 UpdateDisplay("1000x300*2@1.5");
325 EXPECT_EQ(
326 "1000x300",
327 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
329 if (!SupportsMultipleDisplays())
330 return;
332 // First display has maximum size.
333 UpdateDisplay("400x300,100x100");
334 EXPECT_EQ(
335 "400x300",
336 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
338 // Second display has maximum size.
339 UpdateDisplay("400x300,500x600");
340 EXPECT_EQ(
341 "500x600",
342 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
344 // Maximum width and height belongs to different displays.
345 UpdateDisplay("400x300,100x500");
346 EXPECT_EQ(
347 "400x500",
348 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
352 } // namespace ash