OLD | NEW |
| (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/wallpaper/wallpaper_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/root_window_controller.h" | |
13 #include "ash/shell.h" | |
14 #include "ash/test/ash_test_base.h" | |
15 #include "ash/test/test_wallpaper_delegate.h" | |
16 #include "ash/wallpaper/wallpaper_view.h" | |
17 #include "ash/wallpaper/wallpaper_widget_controller.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 kWallpaperId = ash::kShellWindowId_WallpaperContainer; | |
39 const int kLockScreenWallpaperId = | |
40 ash::kShellWindowId_LockScreenWallpaperContainer; | |
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 WallpaperControllerTest : public test::AshTestBase { | |
105 public: | |
106 WallpaperControllerTest() | |
107 : controller_(nullptr), wallpaper_delegate_(nullptr) {} | |
108 ~WallpaperControllerTest() 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->SetWallpaperWidgetController(nullptr); | |
117 root_window_controller->SetAnimatingWallpaperWidgetController(nullptr); | |
118 controller_ = Shell::GetInstance()->wallpaper_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 WallpaperView* wallpaper_view() { | |
125 WallpaperWidgetController* controller = | |
126 Shell::GetPrimaryRootWindowController() | |
127 ->animating_wallpaper_widget_controller() | |
128 ->GetController(false); | |
129 EXPECT_TRUE(controller); | |
130 return static_cast<WallpaperView*>( | |
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(WallpaperView* 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 AnimatingWallpaperWidgetController's animation to completion. | |
180 // TODO(bshe): Don't require tests to run animations; it's slow. | |
181 void RunDesktopControllerAnimation() { | |
182 WallpaperWidgetController* controller = | |
183 Shell::GetPrimaryRootWindowController() | |
184 ->animating_wallpaper_widget_controller() | |
185 ->GetController(false); | |
186 EXPECT_TRUE(controller); | |
187 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget())); | |
188 } | |
189 | |
190 WallpaperController* controller_; // Not owned. | |
191 | |
192 test::TestWallpaperDelegate* wallpaper_delegate_; | |
193 | |
194 private: | |
195 DISALLOW_COPY_AND_ASSIGN(WallpaperControllerTest); | |
196 }; | |
197 | |
198 TEST_F(WallpaperControllerTest, BasicReparenting) { | |
199 WallpaperController* controller = | |
200 Shell::GetInstance()->wallpaper_controller(); | |
201 controller->CreateEmptyWallpaper(); | |
202 | |
203 // Wallpaper view/window exists in the wallpaper container and nothing is in | |
204 // the lock screen wallpaper container. | |
205 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
206 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
207 | |
208 // Moving wallpaper to lock container should succeed the first time but | |
209 // subsequent calls should do nothing. | |
210 EXPECT_TRUE(controller->MoveToLockedContainer()); | |
211 EXPECT_FALSE(controller->MoveToLockedContainer()); | |
212 | |
213 // One window is moved from desktop to lock container. | |
214 EXPECT_EQ(0, ChildCountForContainer(kWallpaperId)); | |
215 EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId)); | |
216 | |
217 // Moving wallpaper to desktop container should succeed the first time. | |
218 EXPECT_TRUE(controller->MoveToUnlockedContainer()); | |
219 EXPECT_FALSE(controller->MoveToUnlockedContainer()); | |
220 | |
221 // One window is moved from lock to desktop container. | |
222 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
223 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
224 } | |
225 | |
226 TEST_F(WallpaperControllerTest, 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 the wallpaper and its view. | |
232 WallpaperController* controller = | |
233 Shell::GetInstance()->wallpaper_controller(); | |
234 controller->CreateEmptyWallpaper(); | |
235 | |
236 // The new wallpaper is ready to animate. | |
237 RootWindowController* root_window_controller = | |
238 Shell::GetPrimaryRootWindowController(); | |
239 EXPECT_TRUE(root_window_controller->animating_wallpaper_widget_controller() | |
240 ->GetController(false)); | |
241 EXPECT_FALSE(root_window_controller->wallpaper_widget_controller()); | |
242 | |
243 // Force the animation to play to completion. | |
244 RunDesktopControllerAnimation(); | |
245 EXPECT_FALSE(root_window_controller->animating_wallpaper_widget_controller() | |
246 ->GetController(false)); | |
247 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
248 } | |
249 | |
250 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we | |
251 // move all wallpaper views if there are more than one. | |
252 TEST_F(WallpaperControllerTest, WallpaperMovementDuringUnlock) { | |
253 // We cannot short-circuit animations for this test. | |
254 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
255 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
256 | |
257 // Reset wallpaper state, see ControllerOwnership above. | |
258 WallpaperController* controller = | |
259 Shell::GetInstance()->wallpaper_controller(); | |
260 controller->CreateEmptyWallpaper(); | |
261 | |
262 // Run wallpaper show animation to completion. | |
263 RunDesktopControllerAnimation(); | |
264 | |
265 // User locks the screen, which moves the wallpaper forward. | |
266 controller->MoveToLockedContainer(); | |
267 | |
268 // Suspend/resume cycle causes wallpaper to refresh, loading a new wallpaper | |
269 // that will animate in on top of the old one. | |
270 controller->CreateEmptyWallpaper(); | |
271 | |
272 // In this state we have two wallpaper views stored in different properties. | |
273 // Both are in the lock screen wallpaper container. | |
274 RootWindowController* root_window_controller = | |
275 Shell::GetPrimaryRootWindowController(); | |
276 EXPECT_TRUE(root_window_controller->animating_wallpaper_widget_controller() | |
277 ->GetController(false)); | |
278 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
279 EXPECT_EQ(0, ChildCountForContainer(kWallpaperId)); | |
280 EXPECT_EQ(2, ChildCountForContainer(kLockScreenWallpaperId)); | |
281 | |
282 // Before the wallpaper's animation completes, user unlocks the screen, which | |
283 // moves the wallpaper to the back. | |
284 controller->MoveToUnlockedContainer(); | |
285 | |
286 // Ensure both wallpapers have moved. | |
287 EXPECT_EQ(2, ChildCountForContainer(kWallpaperId)); | |
288 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
289 | |
290 // Finish the new wallpaper animation. | |
291 RunDesktopControllerAnimation(); | |
292 | |
293 // Now there is one wallpaper, in the back. | |
294 EXPECT_EQ(1, ChildCountForContainer(kWallpaperId)); | |
295 EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId)); | |
296 } | |
297 | |
298 // Test for crbug.com/156542. Animating wallpaper should immediately finish | |
299 // animation and replace current wallpaper before next animation starts. | |
300 TEST_F(WallpaperControllerTest, ChangeWallpaperQuick) { | |
301 // We cannot short-circuit animations for this test. | |
302 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
303 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
304 | |
305 // Reset wallpaper state, see ControllerOwnership above. | |
306 WallpaperController* controller = | |
307 Shell::GetInstance()->wallpaper_controller(); | |
308 controller->CreateEmptyWallpaper(); | |
309 | |
310 // Run wallpaper show animation to completion. | |
311 RunDesktopControllerAnimation(); | |
312 | |
313 // Change to a new wallpaper. | |
314 controller->CreateEmptyWallpaper(); | |
315 | |
316 RootWindowController* root_window_controller = | |
317 Shell::GetPrimaryRootWindowController(); | |
318 WallpaperWidgetController* animating_controller = | |
319 root_window_controller->animating_wallpaper_widget_controller() | |
320 ->GetController(false); | |
321 EXPECT_TRUE(animating_controller); | |
322 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
323 | |
324 // Change to another wallpaper before animation finished. | |
325 controller->CreateEmptyWallpaper(); | |
326 | |
327 // The animating controller should immediately move to wallpaper controller. | |
328 EXPECT_EQ(animating_controller, | |
329 root_window_controller->wallpaper_widget_controller()); | |
330 | |
331 // Cache the new animating controller. | |
332 animating_controller = | |
333 root_window_controller->animating_wallpaper_widget_controller() | |
334 ->GetController(false); | |
335 | |
336 // Run wallpaper show animation to completion. | |
337 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget( | |
338 root_window_controller->animating_wallpaper_widget_controller() | |
339 ->GetController(false) | |
340 ->widget())); | |
341 | |
342 EXPECT_TRUE(root_window_controller->wallpaper_widget_controller()); | |
343 EXPECT_FALSE(root_window_controller->animating_wallpaper_widget_controller() | |
344 ->GetController(false)); | |
345 // The wallpaper controller should be the last created animating controller. | |
346 EXPECT_EQ(animating_controller, | |
347 root_window_controller->wallpaper_widget_controller()); | |
348 } | |
349 | |
350 TEST_F(WallpaperControllerTest, ResizeCustomWallpaper) { | |
351 if (!SupportsMultipleDisplays()) | |
352 return; | |
353 | |
354 UpdateDisplay("320x200"); | |
355 | |
356 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor); | |
357 | |
358 // Set the image as custom wallpaper, wait for the resize to finish, and check | |
359 // that the resized image is the expected size. | |
360 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH); | |
361 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper())); | |
362 RunAllBlockingPoolTasksUntilIdle(Shell::GetInstance()->blocking_pool()); | |
363 gfx::ImageSkia resized_image = controller_->GetWallpaper(); | |
364 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image)); | |
365 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString()); | |
366 | |
367 // Load the original wallpaper again and check that we're still using the | |
368 // previously-resized image instead of doing another resize | |
369 // (http://crbug.com/321402). | |
370 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH); | |
371 RunAllBlockingPoolTasksUntilIdle(Shell::GetInstance()->blocking_pool()); | |
372 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper())); | |
373 } | |
374 | |
375 #if defined(OS_WIN) && !defined(USE_ASH) | |
376 // TODO(msw): Broken on Windows. http://crbug.com/584038 | |
377 #define MAYBE_GetMaxDisplaySize DISABLED_GetMaxDisplaySize | |
378 #else | |
379 #define MAYBE_GetMaxDisplaySize GetMaxDisplaySize | |
380 #endif | |
381 TEST_F(WallpaperControllerTest, MAYBE_GetMaxDisplaySize) { | |
382 // Device scale factor shouldn't affect the native size. | |
383 UpdateDisplay("1000x300*2"); | |
384 EXPECT_EQ("1000x300", | |
385 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
386 | |
387 // Rotated display should return the rotated size. | |
388 UpdateDisplay("1000x300*2/r"); | |
389 EXPECT_EQ("300x1000", | |
390 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
391 | |
392 // UI Scaling shouldn't affect the native size. | |
393 UpdateDisplay("1000x300*2@1.5"); | |
394 EXPECT_EQ("1000x300", | |
395 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
396 | |
397 if (!SupportsMultipleDisplays()) | |
398 return; | |
399 | |
400 // First display has maximum size. | |
401 UpdateDisplay("400x300,100x100"); | |
402 EXPECT_EQ("400x300", | |
403 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
404 | |
405 // Second display has maximum size. | |
406 UpdateDisplay("400x300,500x600"); | |
407 EXPECT_EQ("500x600", | |
408 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
409 | |
410 // Maximum width and height belongs to different displays. | |
411 UpdateDisplay("400x300,100x500"); | |
412 EXPECT_EQ("400x500", | |
413 WallpaperController::GetMaxDisplaySizeInNative().ToString()); | |
414 } | |
415 | |
416 // Test that the wallpaper is always fitted to the native display resolution | |
417 // when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images. | |
418 TEST_F(WallpaperControllerTest, DontScaleWallpaperWithCenterLayout) { | |
419 // We cannot short-circuit animations for this test. | |
420 ui::ScopedAnimationDurationScaleMode test_duration_mode( | |
421 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
422 | |
423 const gfx::Size high_resolution(3600, 2400); | |
424 const gfx::Size low_resolution(360, 240); | |
425 const float high_dsf = 2.0f; | |
426 const float low_dsf = 1.0f; | |
427 | |
428 gfx::ImageSkia image_high_res = CreateImage( | |
429 high_resolution.width(), high_resolution.height(), kCustomWallpaperColor); | |
430 gfx::ImageSkia image_low_res = CreateImage( | |
431 low_resolution.width(), low_resolution.height(), kCustomWallpaperColor); | |
432 | |
433 UpdateDisplay("1200x600*2"); | |
434 { | |
435 SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution")); | |
436 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
437 WallpaperFitToNativeResolution( | |
438 wallpaper_view(), high_dsf, high_resolution.width(), | |
439 high_resolution.height(), kCustomWallpaperColor); | |
440 } | |
441 { | |
442 SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution")); | |
443 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
444 WallpaperFitToNativeResolution( | |
445 wallpaper_view(), high_dsf, low_resolution.width(), | |
446 low_resolution.height(), kCustomWallpaperColor); | |
447 } | |
448 | |
449 UpdateDisplay("1200x600"); | |
450 { | |
451 SCOPED_TRACE(base::StringPrintf("1200x600 high resolution")); | |
452 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
453 WallpaperFitToNativeResolution( | |
454 wallpaper_view(), low_dsf, high_resolution.width(), | |
455 high_resolution.height(), kCustomWallpaperColor); | |
456 } | |
457 { | |
458 SCOPED_TRACE(base::StringPrintf("1200x600 low resolution")); | |
459 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
460 WallpaperFitToNativeResolution( | |
461 wallpaper_view(), low_dsf, low_resolution.width(), | |
462 low_resolution.height(), kCustomWallpaperColor); | |
463 } | |
464 | |
465 UpdateDisplay("1200x600/u@1.5"); // 1.5 ui scale | |
466 { | |
467 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution")); | |
468 controller_->SetWallpaperImage(image_high_res, WALLPAPER_LAYOUT_CENTER); | |
469 WallpaperFitToNativeResolution( | |
470 wallpaper_view(), low_dsf, high_resolution.width(), | |
471 high_resolution.height(), kCustomWallpaperColor); | |
472 } | |
473 { | |
474 SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution")); | |
475 controller_->SetWallpaperImage(image_low_res, WALLPAPER_LAYOUT_CENTER); | |
476 WallpaperFitToNativeResolution( | |
477 wallpaper_view(), low_dsf, low_resolution.width(), | |
478 low_resolution.height(), kCustomWallpaperColor); | |
479 } | |
480 } | |
481 | |
482 } // namespace ash | |
OLD | NEW |