Index: chrome/browser/chromeos/login/wallpaper_manager_unittest.cc |
diff --git a/chrome/browser/chromeos/login/wallpaper_manager_unittest.cc b/chrome/browser/chromeos/login/wallpaper_manager_unittest.cc |
index c47745631a58bfd68fef53bc63731ed414d7c342..cbb833b258eaf9952db307054a3db09e20bc8f13 100644 |
--- a/chrome/browser/chromeos/login/wallpaper_manager_unittest.cc |
+++ b/chrome/browser/chromeos/login/wallpaper_manager_unittest.cc |
@@ -7,10 +7,14 @@ |
#include "ash/ash_resources/grit/ash_resources.h" |
#include "ash/desktop_background/desktop_background_controller.h" |
+#include "ash/desktop_background/desktop_background_controller_observer.h" |
#include "ash/shell.h" |
#include "ash/test/ash_test_base.h" |
+#include "ash/test/display_manager_test_api.h" |
#include "base/command_line.h" |
#include "base/file_util.h" |
+#include "base/files/file_path.h" |
+#include "base/files/scoped_temp_dir.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/prefs/pref_service.h" |
#include "base/prefs/testing_pref_service.h" |
@@ -20,6 +24,7 @@ |
#include "chrome/browser/chromeos/login/wallpaper_manager.h" |
#include "chrome/browser/chromeos/settings/cros_settings.h" |
#include "chrome/browser/chromeos/settings/device_settings_service.h" |
+#include "chrome/browser/prefs/browser_prefs.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/test/base/testing_browser_process.h" |
#include "chromeos/chromeos_switches.h" |
@@ -27,6 +32,8 @@ |
#include "chromeos/settings/cros_settings_provider.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "ui/base/resource/resource_bundle.h" |
+#include "ui/gfx/point.h" |
+#include "ui/gfx/rect.h" |
using namespace ash; |
@@ -35,6 +42,31 @@ const char kTestUser1Hash[] = "test-user@example.com-hash"; |
namespace chromeos { |
+class TestObserver : public DesktopBackgroundControllerObserver { |
+ public: |
+ explicit TestObserver(DesktopBackgroundController* controller) |
+ : controller_(controller) { |
+ DCHECK(controller_); |
+ controller_->AddObserver(this); |
+ } |
+ |
+ virtual ~TestObserver() { |
+ controller_->RemoveObserver(this); |
+ } |
+ |
+ void WaitForWallpaperDataChanged() { |
+ base::MessageLoop::current()->Run(); |
+ } |
+ |
+ // DesktopBackgroundControllerObserver overrides: |
+ virtual void OnWallpaperDataChanged() OVERRIDE { |
+ base::MessageLoop::current()->Quit(); |
+ } |
+ |
+ private: |
+ DesktopBackgroundController* controller_; |
+}; |
+ |
class WallpaperManagerTest : public test::AshTestBase { |
public: |
WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {} |
@@ -47,16 +79,12 @@ class WallpaperManagerTest : public test::AshTestBase { |
// Register an in-memory local settings instance. |
local_state_.reset(new TestingPrefServiceSimple); |
TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get()); |
- UserManager::RegisterPrefs(local_state_->registry()); |
- // Wallpaper manager and user image managers prefs will be accessed by the |
- // unit-test as well. |
- UserImageManager::RegisterPrefs(local_state_->registry()); |
- WallpaperManager::RegisterPrefs(local_state_->registry()); |
+ chrome::RegisterLocalState(local_state_->registry()); |
- StartupUtils::RegisterPrefs(local_state_->registry()); |
StartupUtils::MarkDeviceRegistered(); |
ResetUserManager(); |
+ controller_ = ash::Shell::GetInstance()->desktop_background_controller(); |
} |
virtual void TearDown() OVERRIDE { |
@@ -86,7 +114,104 @@ class WallpaperManagerTest : public test::AshTestBase { |
base::MessageLoop::current()->RunUntilIdle(); |
} |
+ const base::FilePath& GetWallpaperManagerGuestSmallFile() const { |
+ return WallpaperManager::Get()->guest_default_small_wallpaper_file_; |
+ } |
+ |
+ const base::FilePath& GetWallpaperManagerGuestLargeFile() const { |
+ return WallpaperManager::Get()->guest_default_large_wallpaper_file_; |
+ } |
+ |
protected: |
+ // Colors used for different default wallpapers by |
+ // InitWallpapers(). |
+ static const SkColor kLargeWallpaperColor = SK_ColorRED; |
+ static const SkColor kSmallWallpaperColor = SK_ColorGREEN; |
+ static const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE; |
+ static const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW; |
+ |
+ // A color that can be passed to CreateImage(). Specifically chosen to not |
+ // conflict with any of the default wallpaper colors. |
+ static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA; |
+ |
+ // Dimension used for width and height of default wallpaper images. A |
+ // small value is used to minimize the amount of time spent compressing |
+ // and writing images. |
+ static const int kWallpaperSize = 2; |
+ |
+ // Creates a test image of size 1x1. |
+ gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) { |
+ SkBitmap bitmap; |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
+ bitmap.allocPixels(); |
+ bitmap.eraseColor(color); |
+ return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
+ } |
+ |
+ // Initializes default wallpaper paths "*default_*file" and writes JPEG |
+ // wallpaper images to them. |
+ // Only needs to be called (once) by tests that want to test loading of |
+ // default wallpapers. |
+ void InitWallpapers() { |
+ WallpaperManager::Get()->default_small_wallpaper_file_ = |
+ base::FilePath(FILE_PATH_LITERAL("small.jpg")); |
+ WallpaperManager::Get()->default_large_wallpaper_file_ = |
+ base::FilePath(FILE_PATH_LITERAL("large.jpg")); |
+ WallpaperManager::Get()->guest_default_small_wallpaper_file_ = |
+ base::FilePath(FILE_PATH_LITERAL("guest_small.jpg")); |
+ WallpaperManager::Get()->guest_default_large_wallpaper_file_ = |
+ base::FilePath(FILE_PATH_LITERAL("guest_large.jpg")); |
+ WallpaperManager::Get()->default_small_wallpaper_image_.reset( |
+ new gfx::ImageSkia(CreateTestImage( |
+ kWallpaperSize, kWallpaperSize, kSmallWallpaperColor))); |
+ WallpaperManager::Get()->default_large_wallpaper_image_.reset( |
+ new gfx::ImageSkia(CreateTestImage( |
+ kWallpaperSize, kWallpaperSize, kLargeWallpaperColor))); |
+ WallpaperManager::Get()->guest_default_small_wallpaper_image_.reset( |
+ new gfx::ImageSkia(CreateTestImage( |
+ kWallpaperSize, kWallpaperSize, kSmallGuestWallpaperColor))); |
+ WallpaperManager::Get()->guest_default_large_wallpaper_image_.reset( |
+ new gfx::ImageSkia(CreateTestImage( |
+ kWallpaperSize, kWallpaperSize, kLargeGuestWallpaperColor))); |
+ } |
+ |
+ // Returns true if the color at the center of |image| is close to |
+ // |expected_color|. (The center is used so small wallpaper images can be |
+ // used.) |
+ bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) { |
+ if (image.size().IsEmpty()) { |
+ LOG(ERROR) << "Image is empty"; |
+ return false; |
+ } |
+ |
+ const SkBitmap* bitmap = image.bitmap(); |
+ if (!bitmap) { |
+ LOG(ERROR) << "Unable to get bitmap from image"; |
+ return false; |
+ } |
+ |
+ bitmap->lockPixels(); |
+ gfx::Point center = gfx::Rect(image.size()).CenterPoint(); |
+ SkColor image_color = bitmap->getColor(center.x(), center.y()); |
+ bitmap->unlockPixels(); |
+ |
+ const int kDiff = 3; |
+ if (std::abs(static_cast<int>(SkColorGetA(image_color)) - |
+ static_cast<int>(SkColorGetA(expected_color))) > kDiff || |
+ std::abs(static_cast<int>(SkColorGetR(image_color)) - |
+ static_cast<int>(SkColorGetR(expected_color))) > kDiff || |
+ std::abs(static_cast<int>(SkColorGetG(image_color)) - |
+ static_cast<int>(SkColorGetG(expected_color))) > kDiff || |
+ std::abs(static_cast<int>(SkColorGetB(image_color)) - |
+ static_cast<int>(SkColorGetB(expected_color))) > kDiff) { |
+ LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color |
+ << " but got 0x" << image_color; |
+ return false; |
+ } |
+ |
+ return true; |
+ } |
+ |
CommandLine command_line_; |
scoped_ptr<TestingPrefServiceSimple> local_state_; |
@@ -96,40 +221,123 @@ class WallpaperManagerTest : public test::AshTestBase { |
scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; |
+ DesktopBackgroundController* controller_; |
+ |
private: |
DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest); |
}; |
-// Test for crbug.com/260755. If this test fails, it is probably because the |
-// wallpaper of last logged in user is set as guest wallpaper. |
-TEST_F(WallpaperManagerTest, GuestUserUseGuestWallpaper) { |
- UserManager::Get()->UserLoggedIn(kTestUser1, kTestUser1Hash, false); |
- |
- std::string relative_path = |
- base::FilePath(kTestUser1Hash).Append(FILE_PATH_LITERAL("DUMMY")).value(); |
- // Saves wallpaper info to local state for user |kTestUser1|. |
- WallpaperInfo info = { |
- relative_path, |
- WALLPAPER_LAYOUT_CENTER_CROPPED, |
- User::CUSTOMIZED, |
- base::Time::Now().LocalMidnight() |
- }; |
- WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true); |
- ResetUserManager(); |
- |
- AppendGuestSwitch(); |
- scoped_ptr<WallpaperManager::TestApi> test_api; |
- test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get())); |
- // If last logged in user's wallpaper is used in function InitializeWallpaper, |
- // this test will crash. InitializeWallpaper should be a noop after |
- // AppendGuestSwitch being called. |
- WallpaperManager::Get()->InitializeWallpaper(); |
- EXPECT_TRUE(test_api->current_wallpaper_path().empty()); |
- UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, |
- UserManager::kGuestUserName, false); |
- WaitAsyncWallpaperLoad(); |
- EXPECT_FALSE(ash::Shell::GetInstance()->desktop_background_controller()-> |
- SetDefaultWallpaper(true)); |
+// Test that WallpaperManager loads the appropriate wallpaper |
+// images as specified via command-line flags in various situations. |
+// Splitting these into separate tests avoids needing to run animations. |
+// TODO(derat): Combine these into a single test |
+TEST_F(WallpaperManagerTest, SmallDefaultWallpaper) { |
+ if (!SupportsMultipleDisplays()) |
+ return; |
+ |
+ InitWallpapers(); |
+ TestObserver observer(controller_); |
+ |
+ // At 800x600, the small wallpaper should be loaded. |
+ test::DisplayManagerTestApi display_manager_test_api( |
+ Shell::GetInstance()->display_manager()); |
+ display_manager_test_api.UpdateDisplay("800x600"); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor)); |
+} |
+ |
+TEST_F(WallpaperManagerTest, LargeDefaultWallpaper) { |
+ if (!SupportsMultipleDisplays()) |
+ return; |
+ |
+ InitWallpapers(); |
+ TestObserver observer(controller_); |
+ test::DisplayManagerTestApi display_manager_test_api( |
+ Shell::GetInstance()->display_manager()); |
+ display_manager_test_api.UpdateDisplay("1600x1200"); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor)); |
+} |
+ |
+TEST_F(WallpaperManagerTest, LargeDefaultWallpaperWhenRotated) { |
+ if (!SupportsMultipleDisplays()) |
+ return; |
+ InitWallpapers(); |
+ TestObserver observer(controller_); |
+ test::DisplayManagerTestApi display_manager_test_api( |
+ Shell::GetInstance()->display_manager()); |
+ |
+ display_manager_test_api.UpdateDisplay("1200x800/r"); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor)); |
+} |
+ |
+TEST_F(WallpaperManagerTest, SmallGuestWallpaper) { |
+ if (!SupportsMultipleDisplays()) |
+ return; |
+ InitWallpapers(); |
+ UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, "", false); |
+ TestObserver observer(controller_); |
+ test::DisplayManagerTestApi display_manager_test_api( |
+ Shell::GetInstance()->display_manager()); |
+ display_manager_test_api.UpdateDisplay("800x600"); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kSmallGuestWallpaperColor)); |
+} |
+ |
+TEST_F(WallpaperManagerTest, LargeGuestWallpaper) { |
+ if (!SupportsMultipleDisplays()) |
+ return; |
+ |
+ InitWallpapers(); |
+ UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, "", false); |
+ TestObserver observer(controller_); |
+ test::DisplayManagerTestApi display_manager_test_api( |
+ Shell::GetInstance()->display_manager()); |
+ display_manager_test_api.UpdateDisplay("1600x1200"); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kLargeGuestWallpaperColor)); |
+} |
+ |
+TEST_F(WallpaperManagerTest, SwitchBetweenDefaultAndCustom) { |
+ // Start loading the default wallpaper. |
+ UpdateDisplay("640x480"); |
+ InitWallpapers(); |
+ UserManager::Get()->UserLoggedIn(UserManager::kStubUser, "test_hash", false); |
+ |
+ TestObserver observer(controller_); |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ |
+ // Custom wallpaper should be applied immediately, canceling the default |
+ // wallpaper load task. |
+ gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor); |
+ UserImage wallpaper(image); |
+ WallpaperManager::Get()->SetCustomWallpaper(UserManager::kStubUser, |
+ "test_hash", |
+ "test-nofile.jpeg", |
+ WALLPAPER_LAYOUT_STRETCH, |
+ User::CUSTOMIZED, |
+ wallpaper, |
+ true); |
+ observer.WaitForWallpaperDataChanged(); |
+ |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kCustomWallpaperColor)); |
+ |
+ WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
+ observer.WaitForWallpaperDataChanged(); |
+ EXPECT_TRUE( |
+ ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor)); |
} |
class WallpaperManagerCacheTest : public test::AshTestBase { |