Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_controller_unittest.cc
blob537112272a50f44799800b44c93528810f35fe1b
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_controller_observer.h"
12 #include "ash/desktop_background/desktop_background_widget_controller.h"
13 #include "ash/root_window_controller.h"
14 #include "ash/shell.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/display_manager_test_api.h"
18 #include "ash/test/test_user_wallpaper_delegate.h"
19 #include "base/command_line.h"
20 #include "base/file_util.h"
21 #include "base/files/file_path.h"
22 #include "base/files/scoped_temp_dir.h"
23 #include "base/message_loop/message_loop.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "content/public/test/test_utils.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "third_party/skia/include/core/SkColor.h"
30 #include "ui/aura/root_window.h"
31 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
32 #include "ui/compositor/test/layer_animator_test_controller.h"
33 #include "ui/gfx/codec/jpeg_codec.h"
34 #include "ui/gfx/point.h"
35 #include "ui/gfx/rect.h"
37 using aura::RootWindow;
38 using aura::Window;
40 namespace ash {
41 namespace {
43 // Containers IDs used for tests.
44 const int kDesktopBackgroundId =
45 ash::internal::kShellWindowId_DesktopBackgroundContainer;
46 const int kLockScreenBackgroundId =
47 ash::internal::kShellWindowId_LockScreenBackgroundContainer;
49 // Returns number of child windows in a shell window container.
50 int ChildCountForContainer(int container_id) {
51 Window* root = ash::Shell::GetPrimaryRootWindow();
52 Window* container = root->GetChildById(container_id);
53 return static_cast<int>(container->children().size());
56 class TestObserver : public DesktopBackgroundControllerObserver {
57 public:
58 explicit TestObserver(DesktopBackgroundController* controller)
59 : controller_(controller) {
60 DCHECK(controller_);
61 controller_->AddObserver(this);
64 virtual ~TestObserver() {
65 controller_->RemoveObserver(this);
68 void WaitForWallpaperDataChanged() {
69 base::MessageLoop::current()->Run();
72 // DesktopBackgroundControllerObserver overrides:
73 virtual void OnWallpaperDataChanged() OVERRIDE {
74 base::MessageLoop::current()->Quit();
77 private:
78 DesktopBackgroundController* controller_;
81 // Steps a widget's layer animation until it is completed. Animations must be
82 // enabled.
83 void RunAnimationForWidget(views::Widget* widget) {
84 // Animations must be enabled for stepping to work.
85 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
86 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
88 ui::Layer* layer = widget->GetNativeView()->layer();
89 ui::LayerAnimatorTestController controller(layer->GetAnimator());
90 gfx::AnimationContainerElement* element = layer->GetAnimator();
91 // Multiple steps are required to complete complex animations.
92 // TODO(vollick): This should not be necessary. crbug.com/154017
93 while (controller.animator()->is_animating()) {
94 controller.StartThreadedAnimationsIfNeeded();
95 base::TimeTicks step_time = controller.animator()->last_step_time();
96 element->Step(step_time + base::TimeDelta::FromMilliseconds(1000));
100 } // namespace
102 class DesktopBackgroundControllerTest : public test::AshTestBase {
103 public:
104 DesktopBackgroundControllerTest()
105 : command_line_(CommandLine::NO_PROGRAM),
106 controller_(NULL) {
108 virtual ~DesktopBackgroundControllerTest() {}
110 virtual 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 internal::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::TestUserWallpaperDelegate*>(
120 Shell::GetInstance()->user_wallpaper_delegate());
121 controller_->set_wallpaper_reload_delay_for_test(0);
124 protected:
125 // Colors used for different default wallpapers by
126 // WriteWallpapersAndSetFlags().
127 static const SkColor kLargeWallpaperColor = SK_ColorRED;
128 static const SkColor kSmallWallpaperColor = SK_ColorGREEN;
129 static const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE;
130 static const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW;
132 // A color that can be passed to CreateImage(). Specifically chosen to not
133 // conflict with any of the default wallpaper colors.
134 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
136 // Dimension used for width and height of default wallpaper images. A
137 // small value is used to minimize the amount of time spent compressing
138 // and writing images.
139 static const int kWallpaperSize = 2;
141 // Creates an image of size |size|.
142 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
143 SkBitmap bitmap;
144 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
145 bitmap.allocPixels();
146 bitmap.eraseColor(color);
147 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
148 return image;
151 // Runs kAnimatingDesktopController's animation to completion.
152 // TODO(bshe): Don't require tests to run animations; it's slow.
153 void RunDesktopControllerAnimation() {
154 internal::DesktopBackgroundWidgetController* controller =
155 Shell::GetPrimaryRootWindowController()->
156 animating_wallpaper_controller()->GetController(false);
157 ASSERT_NO_FATAL_FAILURE(RunAnimationForWidget(controller->widget()));
160 // Returns true if the color at the center of |image| is close to
161 // |expected_color|. (The center is used so small wallpaper images can be
162 // used.)
163 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) {
164 if (image.size().IsEmpty()) {
165 LOG(ERROR) << "Image is empty";
166 return false;
169 const SkBitmap* bitmap = image.bitmap();
170 if (!bitmap) {
171 LOG(ERROR) << "Unable to get bitmap from image";
172 return false;
175 bitmap->lockPixels();
176 gfx::Point center = gfx::Rect(image.size()).CenterPoint();
177 SkColor image_color = bitmap->getColor(center.x(), center.y());
178 bitmap->unlockPixels();
180 const int kDiff = 3;
181 if (std::abs(static_cast<int>(SkColorGetA(image_color)) -
182 static_cast<int>(SkColorGetA(expected_color))) > kDiff ||
183 std::abs(static_cast<int>(SkColorGetR(image_color)) -
184 static_cast<int>(SkColorGetR(expected_color))) > kDiff ||
185 std::abs(static_cast<int>(SkColorGetG(image_color)) -
186 static_cast<int>(SkColorGetG(expected_color))) > kDiff ||
187 std::abs(static_cast<int>(SkColorGetB(image_color)) -
188 static_cast<int>(SkColorGetB(expected_color))) > kDiff) {
189 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color
190 << " but got 0x" << image_color;
191 return false;
194 return true;
197 // Writes a JPEG image of the specified size and color to |path|. Returns
198 // true on success.
199 bool WriteJPEGFile(const base::FilePath& path,
200 int width,
201 int height,
202 SkColor color) {
203 SkBitmap bitmap;
204 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0);
205 bitmap.allocPixels();
206 bitmap.eraseColor(color);
208 const int kQuality = 80;
209 std::vector<unsigned char> output;
210 if (!gfx::JPEGCodec::Encode(
211 static_cast<const unsigned char*>(bitmap.getPixels()),
212 gfx::JPEGCodec::FORMAT_SkBitmap, width, height, bitmap.rowBytes(),
213 kQuality, &output)) {
214 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
215 return false;
218 size_t bytes_written = file_util::WriteFile(
219 path, reinterpret_cast<const char*>(&output[0]), output.size());
220 if (bytes_written != output.size()) {
221 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
222 << output.size() << " to " << path.value();
223 return false;
226 return true;
229 // Initializes |wallpaper_dir_|, writes JPEG wallpaper images to it, and
230 // passes |controller_| a command line instructing it to use the images.
231 // Only needs to be called (once) by tests that want to test loading of
232 // default wallpapers.
233 void WriteWallpapersAndSetFlags() {
234 wallpaper_dir_.reset(new base::ScopedTempDir);
235 ASSERT_TRUE(wallpaper_dir_->CreateUniqueTempDir());
237 const base::FilePath kLargePath =
238 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("large.jpg"));
239 ASSERT_TRUE(WriteJPEGFile(kLargePath, kWallpaperSize, kWallpaperSize,
240 kLargeWallpaperColor));
241 command_line_.AppendSwitchPath(
242 switches::kAshDefaultWallpaperLarge, kLargePath);
244 const base::FilePath kSmallPath =
245 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("small.jpg"));
246 ASSERT_TRUE(WriteJPEGFile(kSmallPath, kWallpaperSize, kWallpaperSize,
247 kSmallWallpaperColor));
248 command_line_.AppendSwitchPath(
249 switches::kAshDefaultWallpaperSmall, kSmallPath);
251 const base::FilePath kLargeGuestPath =
252 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_large.jpg"));
253 ASSERT_TRUE(WriteJPEGFile(kLargeGuestPath, kWallpaperSize, kWallpaperSize,
254 kLargeGuestWallpaperColor));
255 command_line_.AppendSwitchPath(
256 switches::kAshGuestWallpaperLarge, kLargeGuestPath);
258 const base::FilePath kSmallGuestPath =
259 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_small.jpg"));
260 ASSERT_TRUE(WriteJPEGFile(kSmallGuestPath, kWallpaperSize, kWallpaperSize,
261 kSmallGuestWallpaperColor));
262 command_line_.AppendSwitchPath(
263 switches::kAshGuestWallpaperSmall, kSmallGuestPath);
265 controller_->set_command_line_for_testing(&command_line_);
268 // Custom command line passed to DesktopBackgroundController by
269 // WriteWallpapersAndSetFlags().
270 CommandLine command_line_;
272 // Directory created by WriteWallpapersAndSetFlags() to store default
273 // wallpaper images.
274 scoped_ptr<base::ScopedTempDir> wallpaper_dir_;
276 DesktopBackgroundController* controller_; // Not owned.
278 test::TestUserWallpaperDelegate* wallpaper_delegate_;
280 private:
281 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundControllerTest);
284 TEST_F(DesktopBackgroundControllerTest, BasicReparenting) {
285 DesktopBackgroundController* controller =
286 Shell::GetInstance()->desktop_background_controller();
287 controller->CreateEmptyWallpaper();
289 // Wallpaper view/window exists in the desktop background container and
290 // nothing is in the lock screen background container.
291 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
292 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
294 // Moving background to lock container should succeed the first time but
295 // subsequent calls should do nothing.
296 EXPECT_TRUE(controller->MoveDesktopToLockedContainer());
297 EXPECT_FALSE(controller->MoveDesktopToLockedContainer());
299 // One window is moved from desktop to lock container.
300 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
301 EXPECT_EQ(1, ChildCountForContainer(kLockScreenBackgroundId));
303 // Moving background to desktop container should succeed the first time.
304 EXPECT_TRUE(controller->MoveDesktopToUnlockedContainer());
305 EXPECT_FALSE(controller->MoveDesktopToUnlockedContainer());
307 // One window is moved from lock to desktop container.
308 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
309 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
312 TEST_F(DesktopBackgroundControllerTest, ControllerOwnership) {
313 // We cannot short-circuit animations for this test.
314 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
315 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
317 // Create wallpaper and background view.
318 DesktopBackgroundController* controller =
319 Shell::GetInstance()->desktop_background_controller();
320 controller->CreateEmptyWallpaper();
322 // The new wallpaper is ready to start animating. kAnimatingDesktopController
323 // holds the widget controller instance. kDesktopController will get it later.
324 internal::RootWindowController* root_window_controller =
325 Shell::GetPrimaryRootWindowController();
326 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
327 GetController(false));
329 // kDesktopController will receive the widget controller when the animation
330 // is done.
331 EXPECT_FALSE(root_window_controller->wallpaper_controller());
333 // Force the widget's layer animation to play to completion.
334 RunDesktopControllerAnimation();
336 // Ownership has moved from kAnimatingDesktopController to kDesktopController.
337 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
338 GetController(false));
339 EXPECT_TRUE(root_window_controller->wallpaper_controller());
342 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
343 // move all desktop views if there are more than one.
344 TEST_F(DesktopBackgroundControllerTest, BackgroundMovementDuringUnlock) {
345 // We cannot short-circuit animations for this test.
346 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
347 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
349 // Reset wallpaper state, see ControllerOwnership above.
350 DesktopBackgroundController* controller =
351 Shell::GetInstance()->desktop_background_controller();
352 controller->CreateEmptyWallpaper();
354 // Run wallpaper show animation to completion.
355 RunDesktopControllerAnimation();
357 // User locks the screen, which moves the background forward.
358 controller->MoveDesktopToLockedContainer();
360 // Suspend/resume cycle causes wallpaper to refresh, loading a new desktop
361 // background that will animate in on top of the old one.
362 controller->CreateEmptyWallpaper();
364 // In this state we have two desktop background views stored in different
365 // properties. Both are in the lock screen background container.
366 internal::RootWindowController* root_window_controller =
367 Shell::GetPrimaryRootWindowController();
368 EXPECT_TRUE(root_window_controller->animating_wallpaper_controller()->
369 GetController(false));
370 EXPECT_TRUE(root_window_controller->wallpaper_controller());
371 EXPECT_EQ(0, ChildCountForContainer(kDesktopBackgroundId));
372 EXPECT_EQ(2, ChildCountForContainer(kLockScreenBackgroundId));
374 // Before the wallpaper's animation completes, user unlocks the screen, which
375 // moves the desktop to the back.
376 controller->MoveDesktopToUnlockedContainer();
378 // Ensure both desktop backgrounds have moved.
379 EXPECT_EQ(2, ChildCountForContainer(kDesktopBackgroundId));
380 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
382 // Finish the new desktop background animation.
383 RunDesktopControllerAnimation();
385 // Now there is one desktop background, in the back.
386 EXPECT_EQ(1, ChildCountForContainer(kDesktopBackgroundId));
387 EXPECT_EQ(0, ChildCountForContainer(kLockScreenBackgroundId));
390 // Test for crbug.com/156542. Animating wallpaper should immediately finish
391 // animation and replace current wallpaper before next animation starts.
392 TEST_F(DesktopBackgroundControllerTest, ChangeWallpaperQuick) {
393 // We cannot short-circuit animations for this test.
394 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
395 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
397 // Reset wallpaper state, see ControllerOwnership above.
398 DesktopBackgroundController* controller =
399 Shell::GetInstance()->desktop_background_controller();
400 controller->CreateEmptyWallpaper();
402 // Run wallpaper show animation to completion.
403 RunDesktopControllerAnimation();
405 // Change to a new wallpaper.
406 controller->CreateEmptyWallpaper();
408 internal::RootWindowController* root_window_controller =
409 Shell::GetPrimaryRootWindowController();
410 internal::DesktopBackgroundWidgetController* animating_controller =
411 root_window_controller->animating_wallpaper_controller()->
412 GetController(false);
413 EXPECT_TRUE(animating_controller);
414 EXPECT_TRUE(root_window_controller->wallpaper_controller());
416 // Change to another wallpaper before animation finished.
417 controller->CreateEmptyWallpaper();
419 // The animating controller should immediately move to desktop controller.
420 EXPECT_EQ(animating_controller,
421 root_window_controller->wallpaper_controller());
423 // Cache the new animating controller.
424 animating_controller = root_window_controller->
425 animating_wallpaper_controller()->GetController(false);
427 // Run wallpaper show animation to completion.
428 ASSERT_NO_FATAL_FAILURE(
429 RunAnimationForWidget(
430 root_window_controller->animating_wallpaper_controller()->
431 GetController(false)->widget()));
433 EXPECT_TRUE(root_window_controller->wallpaper_controller());
434 EXPECT_FALSE(root_window_controller->animating_wallpaper_controller()->
435 GetController(false));
436 // The desktop controller should be the last created animating controller.
437 EXPECT_EQ(animating_controller,
438 root_window_controller->wallpaper_controller());
441 TEST_F(DesktopBackgroundControllerTest, DisplayChange) {
442 // TODO(derat|oshima|bshe): Host windows can't be resized on Win8.
443 if (!SupportsHostWindowResize())
444 return;
446 // Set the wallpaper to ensure that UpdateWallpaper() will be called when the
447 // display configuration changes.
448 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
449 wallpaper_delegate_->set_custom_wallpaper(image);
450 controller_->SetCustomWallpaper(image, WALLPAPER_LAYOUT_STRETCH);
452 // Small wallpaper images should be used for configurations less than or
453 // equal to kSmallWallpaperMaxWidth by kSmallWallpaperMaxHeight, even if
454 // multiple displays are connected.
455 test::DisplayManagerTestApi display_manager_test_api(
456 Shell::GetInstance()->display_manager());
457 display_manager_test_api.UpdateDisplay("800x600");
458 RunAllPendingInMessageLoop();
459 EXPECT_EQ(WALLPAPER_RESOLUTION_SMALL,
460 controller_->GetAppropriateResolution());
461 EXPECT_EQ(0, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
463 display_manager_test_api.UpdateDisplay("800x600,800x600");
464 RunAllPendingInMessageLoop();
465 EXPECT_EQ(WALLPAPER_RESOLUTION_SMALL,
466 controller_->GetAppropriateResolution());
467 EXPECT_EQ(0, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
469 display_manager_test_api.UpdateDisplay("1366x800");
470 RunAllPendingInMessageLoop();
471 EXPECT_EQ(WALLPAPER_RESOLUTION_SMALL,
472 controller_->GetAppropriateResolution());
473 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
475 // At larger sizes, large wallpapers should be used.
476 display_manager_test_api.UpdateDisplay("1367x800");
477 RunAllPendingInMessageLoop();
478 EXPECT_EQ(WALLPAPER_RESOLUTION_LARGE,
479 controller_->GetAppropriateResolution());
480 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
482 display_manager_test_api.UpdateDisplay("1367x801");
483 RunAllPendingInMessageLoop();
484 EXPECT_EQ(WALLPAPER_RESOLUTION_LARGE,
485 controller_->GetAppropriateResolution());
486 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
488 display_manager_test_api.UpdateDisplay("2560x1700");
489 RunAllPendingInMessageLoop();
490 EXPECT_EQ(WALLPAPER_RESOLUTION_LARGE,
491 controller_->GetAppropriateResolution());
492 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
494 // Rotated smaller screen may use larger image.
495 display_manager_test_api.UpdateDisplay("800x600/r");
496 RunAllPendingInMessageLoop();
497 EXPECT_EQ(WALLPAPER_RESOLUTION_SMALL,
498 controller_->GetAppropriateResolution());
499 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
501 display_manager_test_api.UpdateDisplay("800x600/r,800x600");
502 RunAllPendingInMessageLoop();
503 EXPECT_EQ(WALLPAPER_RESOLUTION_SMALL,
504 controller_->GetAppropriateResolution());
505 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
506 display_manager_test_api.UpdateDisplay("1366x800/r");
507 RunAllPendingInMessageLoop();
508 EXPECT_EQ(WALLPAPER_RESOLUTION_LARGE,
509 controller_->GetAppropriateResolution());
510 EXPECT_EQ(1, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
512 // Max display size didn't chagne.
513 display_manager_test_api.UpdateDisplay("900x800/r,400x1366");
514 RunAllPendingInMessageLoop();
515 EXPECT_EQ(0, wallpaper_delegate_->GetUpdateWallpaperCountAndReset());
518 // Test that DesktopBackgroundController loads the appropriate wallpaper
519 // images as specified via command-line flags in various situations.
520 // Splitting these into separate tests avoids needing to run animations.
521 // TODO(derat): Combine these into a single test -- see
522 // RunDesktopControllerAnimation()'s TODO.
523 TEST_F(DesktopBackgroundControllerTest, SmallDefaultWallpaper) {
524 if (!SupportsMultipleDisplays())
525 return;
527 WriteWallpapersAndSetFlags();
528 TestObserver observer(controller_);
530 // At 800x600, the small wallpaper should be loaded.
531 test::DisplayManagerTestApi display_manager_test_api(
532 Shell::GetInstance()->display_manager());
533 display_manager_test_api.UpdateDisplay("800x600");
534 ASSERT_TRUE(controller_->SetDefaultWallpaper(false));
535 observer.WaitForWallpaperDataChanged();
536 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
537 kSmallWallpaperColor));
539 // Requesting the same wallpaper again should be a no-op.
540 ASSERT_FALSE(controller_->SetDefaultWallpaper(false));
543 TEST_F(DesktopBackgroundControllerTest, LargeDefaultWallpaper) {
544 if (!SupportsMultipleDisplays())
545 return;
547 WriteWallpapersAndSetFlags();
548 TestObserver observer(controller_);
549 test::DisplayManagerTestApi display_manager_test_api(
550 Shell::GetInstance()->display_manager());
551 display_manager_test_api.UpdateDisplay("1600x1200");
552 ASSERT_TRUE(controller_->SetDefaultWallpaper(false));
553 observer.WaitForWallpaperDataChanged();
554 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
555 kLargeWallpaperColor));
558 TEST_F(DesktopBackgroundControllerTest, LargeDefaultWallpaperWhenRotated) {
559 if (!SupportsMultipleDisplays())
560 return;
561 WriteWallpapersAndSetFlags();
562 TestObserver observer(controller_);
563 test::DisplayManagerTestApi display_manager_test_api(
564 Shell::GetInstance()->display_manager());
566 display_manager_test_api.UpdateDisplay("1200x800/r");
567 ASSERT_TRUE(controller_->SetDefaultWallpaper(false));
568 observer.WaitForWallpaperDataChanged();
569 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
570 kLargeWallpaperColor));
573 TEST_F(DesktopBackgroundControllerTest, SmallGuestWallpaper) {
574 if (!SupportsMultipleDisplays())
575 return;
577 WriteWallpapersAndSetFlags();
578 TestObserver observer(controller_);
579 test::DisplayManagerTestApi display_manager_test_api(
580 Shell::GetInstance()->display_manager());
581 display_manager_test_api.UpdateDisplay("800x600");
582 ASSERT_TRUE(controller_->SetDefaultWallpaper(true));
583 observer.WaitForWallpaperDataChanged();
584 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
585 kSmallGuestWallpaperColor));
588 TEST_F(DesktopBackgroundControllerTest, LargeGuestWallpaper) {
589 if (!SupportsMultipleDisplays())
590 return;
592 WriteWallpapersAndSetFlags();
593 TestObserver observer(controller_);
594 test::DisplayManagerTestApi display_manager_test_api(
595 Shell::GetInstance()->display_manager());
596 display_manager_test_api.UpdateDisplay("1600x1200");
597 ASSERT_TRUE(controller_->SetDefaultWallpaper(true));
598 observer.WaitForWallpaperDataChanged();
599 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
600 kLargeGuestWallpaperColor));
603 TEST_F(DesktopBackgroundControllerTest, ResizeCustomWallpaper) {
604 if (!SupportsMultipleDisplays())
605 return;
607 test::DisplayManagerTestApi display_manager_test_api(
608 Shell::GetInstance()->display_manager());
609 display_manager_test_api.UpdateDisplay("320x200");
611 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
613 // Set the image as custom wallpaper, wait for the resize to finish, and check
614 // that the resized image is the expected size.
615 controller_->SetCustomWallpaper(image, WALLPAPER_LAYOUT_STRETCH);
616 EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
617 content::BrowserThread::GetBlockingPool()->FlushForTesting();
618 content::RunAllPendingInMessageLoop();
619 gfx::ImageSkia resized_image = controller_->GetWallpaper();
620 EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
621 EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
623 // Load the original wallpaper again and check that we're still using the
624 // previously-resized image instead of doing another resize
625 // (http://crbug.com/321402).
626 controller_->SetCustomWallpaper(image, WALLPAPER_LAYOUT_STRETCH);
627 content::BrowserThread::GetBlockingPool()->FlushForTesting();
628 content::RunAllPendingInMessageLoop();
629 EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
632 TEST_F(DesktopBackgroundControllerTest, GetMaxDisplaySize) {
633 // Device scale factor shouldn't affect the native size.
634 UpdateDisplay("1000x300*2");
635 EXPECT_EQ(
636 "1000x300",
637 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
639 // Rotated display should return the rotated size.
640 UpdateDisplay("1000x300*2/r");
641 EXPECT_EQ(
642 "300x1000",
643 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
645 // UI Scaling shouldn't affect the native size.
646 UpdateDisplay("1000x300*2@1.5");
647 EXPECT_EQ(
648 "1000x300",
649 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
651 if (!SupportsMultipleDisplays())
652 return;
654 // First display has maximum size.
655 UpdateDisplay("400x300,100x100");
656 EXPECT_EQ(
657 "400x300",
658 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
660 // Second display has maximum size.
661 UpdateDisplay("400x300,500x600");
662 EXPECT_EQ(
663 "500x600",
664 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
666 // Maximum width and height belongs to different displays.
667 UpdateDisplay("400x300,100x500");
668 EXPECT_EQ(
669 "400x500",
670 DesktopBackgroundController::GetMaxDisplaySizeInNative().ToString());
673 TEST_F(DesktopBackgroundControllerTest, SwitchBetweenDefaultAndCustom) {
674 // Start loading the default wallpaper.
675 UpdateDisplay("640x480");
676 WriteWallpapersAndSetFlags();
677 ASSERT_TRUE(controller_->SetDefaultWallpaper(false));
679 // Custom wallpaper should be applied immediately, canceling the default
680 // wallpaper load task.
681 gfx::ImageSkia image = CreateImage(640, 480, kCustomWallpaperColor);
682 controller_->SetCustomWallpaper(image, WALLPAPER_LAYOUT_STRETCH);
683 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
684 kCustomWallpaperColor));
686 // A call to SetDefaultWallpaper() should return true now, indicating that a
687 // new load task was started (since the previous one was interrupted by
688 // SetCustomWallpaper()). See http://crbug.com/327443.
689 TestObserver observer(controller_);
690 ASSERT_TRUE(controller_->SetDefaultWallpaper(false));
691 observer.WaitForWallpaperDataChanged();
692 EXPECT_TRUE(ImageIsNearColor(controller_->GetWallpaper(),
693 kSmallWallpaperColor));
696 } // namespace ash