Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: ash/desktop_background/desktop_background_controller_unittest.cc

Issue 2290473004: Rename ash desktop_background to wallpaper. (Closed)
Patch Set: Address comments. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/desktop_background/desktop_background_controller.h"
6
7 #include <cmath>
8 #include <cstdlib>
9
10 #include "ash/common/shell_window_ids.h"
11 #include "ash/common/wm_shell.h"
12 #include "ash/desktop_background/desktop_background_view.h"
13 #include "ash/desktop_background/desktop_background_widget_controller.h"
14 #include "ash/root_window_controller.h"
15 #include "ash/shell.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 kDesktopBackgroundId = ash::kShellWindowId_DesktopBackgroundContainer;
39 const int kLockScreenBackgroundId =
40 ash::kShellWindowId_LockScreenBackgroundContainer;
41
42 // Returns number of child windows in a shell window container.
43 int ChildCountForContainer(int container_id) {
44 aura::Window* root = ash::Shell::GetPrimaryRootWindow();
45 aura::Window* container = root->GetChildById(container_id);
46 return static_cast<int>(container->children().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->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));
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 DesktopBackgroundControllerTest : public test::AshTestBase {
105 public:
106 DesktopBackgroundControllerTest()
107 : controller_(NULL), wallpaper_delegate_(NULL) {}
108 ~DesktopBackgroundControllerTest() 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 Shell::GetPrimaryRootWindowController();
116 root_window_controller->SetWallpaperController(NULL);
117 root_window_controller->SetAnimatingWallpaperController(NULL);
118 controller_ = Shell::GetInstance()->desktop_background_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 DesktopBackgroundView* desktop_background_view() {
125 DesktopBackgroundWidgetController* controller =
126 Shell::GetPrimaryRootWindowController()
127 ->animating_wallpaper_controller()
128 ->GetController(false);
129 EXPECT_TRUE(controller);
130 return static_cast<DesktopBackgroundView*>(
131 controller->widget()->GetContentsView()->child_at(0));
132 }
133
134 protected:
135 // A color that can be passed to CreateImage(). Specifically chosen to not
136 // conflict with any of the default wallpaper colors.
137 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
138
139 // Creates an image of size |size|.
140 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
141 SkBitmap bitmap;
142 bitmap.allocN32Pixels(width, height);
143 bitmap.eraseColor(color);
144 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
145 return image;
146 }
147
148 // Helper function that tests the wallpaper is always fitted to the native
149 // display resolution when the layout is WALLPAPER_LAYOUT_CENTER.
150 void WallpaperFitToNativeResolution(DesktopBackgroundView* view,
151 float device_scale_factor,
152 int image_width,
153 int image_height,
154 SkColor color) {
155 gfx::Size size = view->bounds().size();
156 gfx::Canvas canvas(size, device_scale_factor, true);
157 view->OnPaint(&canvas);
158
159 int canvas_width = canvas.sk_canvas()->imageInfo().width();
160 int canvas_height = canvas.sk_canvas()->imageInfo().height();
161 SkBitmap bitmap;
162 bitmap.allocN32Pixels(canvas_width, canvas_height);
163 canvas.sk_canvas()->readPixels(&bitmap, 0, 0);
164
165 for (int i = 0; i < canvas_width; i++) {
166 for (int j = 0; j < canvas_height; j++) {
167 if (i >= (canvas_width - image_width) / 2 &&
168 i < (canvas_width + image_width) / 2 &&
169 j >= (canvas_height - image_height) / 2 &&
170 j < (canvas_height + image_height) / 2) {
171 EXPECT_EQ(color, bitmap.getColor(i, j));
172 } else {
173 EXPECT_EQ(SK_ColorBLACK, bitmap.getColor(i, j));
174 }
175 }
176 }
177 }
178
179 // Runs kAnimatingDesktopController's animation to completion.
180 // TODO(bshe): Don't require tests to run animations; it's slow.
181 void RunDesktopControllerAnimation() {
182 DesktopBackgroundWidgetController* controller =
183 Shell::GetPrimaryRootWindowController()
184 ->animating_wallpaper_controller()
185 ->GetController(false);
186 EXPECT_TRUE(controller);
187 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget()));
188 }
189
190 DesktopBackgroundController* controller_; // Not owned.
191
192 test::TestWallpaperDelegate* wallpaper_delegate_;
193
194 private:
195 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest);
196 };
197
198 TEST_F(DesktopBackgroundControllerTest, BasicReparenting) {
199 DesktopBackgroundController* controller =
200 Shell::GetInstance()->desktop_background_controller();
201 controller->CreateEmptyWallpaper();
202
203 // Wallpaper view/window exists in the desktop background container and
204 // nothing is in the lock screen background container.
205 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
206 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
207
208 // Moving background to lock container should succeed the first time but
209 // subsequent calls should do nothing.
210 EXPECT_TRUE(controller->MoveDesktopToLockedContainer());
211 EXPECT_FALSE(controller->MoveDesktopToLockedContainer());
212
213 // One window is moved from desktop to lock container.
214 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
215 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId));
216
217 // Moving background to desktop container should succeed the first time.
218 EXPECT_TRUE(controller->MoveDesktopToUnlockedContainer());
219 EXPECT_FALSE(controller->MoveDesktopToUnlockedContainer());
220
221 // One window is moved from lock to desktop container.
222 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
223 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
224 }
225
226 TEST_F(DesktopBackgroundControllerTest, ControllerOwnership) {
227 // We cannot short-circuit animations for this test.
228 ui::ScopedAnimationDurationScaleMode test_duration_mode(
229 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
230
231 // Create wallpaper and background view.
232 DesktopBackgroundController* controller =
233 Shell::GetInstance()->desktop_background_controller();
234 controller->CreateEmptyWallpaper();
235
236 // The new wallpaper is ready to start animating. kAnimatingDesktopController
237 // holds the widget controller instance. kDesktopController will get it later.
238 RootWindowController* root_window_controller =
239 Shell::GetPrimaryRootWindowController();
240 EXPECT_TRUE(
241 root_window_controller->animating_wallpaper_controller()->GetController(
242 false));
243
244 // kDesktopController will receive the widget controller when the animation
245 // is done.
246 EXPECT_FALSE(root_window_controller->wallpaper_controller());
247
248 // Force the widget's layer animation to play to completion.
249 RunDesktopControllerAnimation();
250
251 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
252 EXPECT_FALSE(
253 root_window_controller->animating_wallpaper_controller()->GetController(
254 false));
255 EXPECT_TRUE(root_window_controller->wallpaper_controller());
256 }
257
258 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
259 // move all desktop views if there are more than one.
260 TEST_F(DesktopBackgroundControllerTest, BackgroundMovementDuringUnlock) {
261 // We cannot short-circuit animations for this test.
262 ui::ScopedAnimationDurationScaleMode test_duration_mode(
263 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
264
265 // Reset wallpaper state, see ControllerOwnership above.
266 DesktopBackgroundController* controller =
267 Shell::GetInstance()->desktop_background_controller();
268 controller->CreateEmptyWallpaper();
269
270 // Run wallpaper show animation to completion.
271 RunDesktopControllerAnimation();
272
273 // User locks the screen, which moves the background forward.
274 controller->MoveDesktopToLockedContainer();
275
276 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
277 // background that will animate in on top of the old one.
278 controller->CreateEmptyWallpaper();
279
280 // In this state we have two desktop background views stored in different
281 // properties. Both are in the lock screen background container.
282 RootWindowController* root_window_controller =
283 Shell::GetPrimaryRootWindowController();
284 EXPECT_TRUE(
285 root_window_controller->animating_wallpaper_controller()->GetController(
286 false));
287 EXPECT_TRUE(root_window_controller->wallpaper_controller());
288 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
289 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId));
290
291 // Before the wallpaper's animation completes, user unlocks the screen, which
292 // moves the desktop to the back.
293 controller->MoveDesktopToUnlockedContainer();
294
295 // Ensure both desktop backgrounds have moved.
296 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId));
297 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
298
299 // Finish the new desktop background animation.
300 RunDesktopControllerAnimation();
301
302 // Now there is one desktop background, in the back.
303 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
304 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
305 }
306
307 // Test for crbug.com/156542. Animating wallpaper should immediately finish
308 // animation and replace current wallpaper before next animation starts.
309 TEST_F(DesktopBackgroundControllerTest, ChangeWallpaperQuick) {
310 // We cannot short-circuit animations for this test.
311 ui::ScopedAnimationDurationScaleMode test_duration_mode(
312 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
313
314 // Reset wallpaper state, see ControllerOwnership above.
315 DesktopBackgroundController* controller =
316 Shell::GetInstance()->desktop_background_controller();
317 controller->CreateEmptyWallpaper();
318
319 // Run wallpaper show animation to completion.
320 RunDesktopControllerAnimation();
321
322 // Change to a new wallpaper.
323 controller->CreateEmptyWallpaper();
324
325 RootWindowController* root_window_controller =
326 Shell::GetPrimaryRootWindowController();
327 DesktopBackgroundWidgetController* animating_controller =
328 root_window_controller->animating_wallpaper_controller()->GetController(
329 false);
330 EXPECT_TRUE(animating_controller);
331 EXPECT_TRUE(root_window_controller->wallpaper_controller());
332
333 // Change to another wallpaper before animation finished.
334 controller->CreateEmptyWallpaper();
335
336 // The animating controller should immediately move to desktop controller.
337 EXPECT_EQ(animating_controller,
338 root_window_controller->wallpaper_controller());
339
340 // Cache the new animating controller.
341 animating_controller =
342 root_window_controller->animating_wallpaper_controller()->GetController(
343 false);
344
345 // Run wallpaper show animation to completion.
346 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(
347 root_window_controller->animating_wallpaper_controller()
348 ->GetController(false)
349 ->widget()));
350
351 EXPECT_TRUE(root_window_controller->wallpaper_controller());
352 EXPECT_FALSE(
353 root_window_controller->animating_wallpaper_controller()->GetController(
354 false));
355 // The desktop controller should be the last created animating controller.
356 EXPECT_EQ(animating_controller,
357 root_window_controller->wallpaper_controller());
358 }
359
360 TEST_F(DesktopBackgroundControllerTest, ResizeCustomWallpaper) {
361 if (!SupportsMultipleDisplays())
362 return;
363
364 UpdateDisplay("320x200");
365
366 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
367
368 // Set the image as custom wallpaper, wait for the resize to finish, and check
369 // that the resized image is the expected size.
370 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
371 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
372 RunAllBlockingPoolTasksUntilIdle(Shell::GetInstance()->blocking_pool());
373 gfx::ImageSkia resized_image = controller_->GetWallpaper();
374 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
375 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
376
377 // Load the original wallpaper again and check that we're still using the
378 // previously-resized image instead of doing another resize
379 // (http://crbug.com/321402).
380 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
381 RunAllBlockingPoolTasksUntilIdle(Shell::GetInstance()->blocking_pool());
382 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
383 }
384
385 #if defined(OS_WIN) && !defined(USE_ASH)
386 // TODO(msw): Broken on Windows. http://crbug.com/584038
387 #define MAYBE_GetMaxDisplaySize DISABLED_GetMaxDisplaySize
388 #else
389 #define MAYBE_GetMaxDisplaySize GetMaxDisplaySize
390 #endif
391 TEST_F(DesktopBackgroundControllerTest, MAYBE_GetMaxDisplaySize) {
392 // Device scale factor shouldn't affect the native size.
393 UpdateDisplay("1000x300*2");
394 EXPECT_EQ(
395 "1000x300",
396 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
397
398 // Rotated display should return the rotated size.
399 UpdateDisplay("1000x300*2/r");
400 EXPECT_EQ(
401 "300x1000",
402 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
403
404 // UI Scaling shouldn't affect the native size.
405 UpdateDisplay("1000x300*2@1.5");
406 EXPECT_EQ(
407 "1000x300",
408 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
409
410 if (!SupportsMultipleDisplays())
411 return;
412
413 // First display has maximum size.
414 UpdateDisplay("400x300,100x100");
415 EXPECT_EQ(
416 "400x300",
417 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
418
419 // Second display has maximum size.
420 UpdateDisplay("400x300,500x600");
421 EXPECT_EQ(
422 "500x600",
423 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
424
425 // Maximum width and height belongs to different displays.
426 UpdateDisplay("400x300,100x500");
427 EXPECT_EQ(
428 "400x500",
429 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
430 }
431
432 // Test that the wallpaper is always fitted to the native display resolution
433 // when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images.
434 TEST_F(DesktopBackgroundControllerTest, DontScaleWallpaperWithCenterLayout) {
435 // We cannot short-circuit animations for this test.
436 ui::ScopedAnimationDurationScaleMode test_duration_mode(
437 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
438
439 const gfx::Size high_resolution(3600, 2400);
440 const gfx::Size low_resolution(360, 240);
441 const float high_dsf = 2.0f;
442 const float low_dsf = 1.0f;
443
444 gfx::ImageSkia image_high_res = CreateImage(
445 high_resolution.width(), high_resolution.height(), kCustomWallpaperColor);
446 gfx::ImageSkia image_low_res = CreateImage(
447 low_resolution.width(), low_resolution.height(), kCustomWallpaperColor);
448
449 UpdateDisplay("1200x600*2");
450 {
451 SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution"));
452 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER);
453 WallpaperFitToNativeResolution(
454 desktop_background_view(), high_dsf, high_resolution.width(),
455 high_resolution.height(), kCustomWallpaperColor);
456 }
457 {
458 SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution"));
459 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER);
460 WallpaperFitToNativeResolution(
461 desktop_background_view(), high_dsf, low_resolution.width(),
462 low_resolution.height(), kCustomWallpaperColor);
463 }
464
465 UpdateDisplay("1200x600");
466 {
467 SCOPED_TRACE(base::StringPrintf("1200x600 high resolution"));
468 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER);
469 WallpaperFitToNativeResolution(
470 desktop_background_view(), low_dsf, high_resolution.width(),
471 high_resolution.height(), kCustomWallpaperColor);
472 }
473 {
474 SCOPED_TRACE(base::StringPrintf("1200x600 low resolution"));
475 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER);
476 WallpaperFitToNativeResolution(
477 desktop_background_view(), low_dsf, low_resolution.width(),
478 low_resolution.height(), kCustomWallpaperColor);
479 }
480
481 UpdateDisplay("1200x600/u@1.5"); // 1.5 ui scale
482 {
483 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution"));
484 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER);
485 WallpaperFitToNativeResolution(
486 desktop_background_view(), low_dsf, high_resolution.width(),
487 high_resolution.height(), kCustomWallpaperColor);
488 }
489 {
490 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution"));
491 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER);
492 WallpaperFitToNativeResolution(
493 desktop_background_view(), low_dsf, low_resolution.width(),
494 low_resolution.height(), kCustomWallpaperColor);
495 }
496 }
497
498 } // namespace ash
OLDNEW
« no previous file with comments | « ash/desktop_background/desktop_background_controller_observer.h ('k') | ash/desktop_background/desktop_background_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698