OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | 5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
6 | 6 |
7 #include "ash/ash_resources/grit/ash_resources.h" | 7 #include "ash/ash_resources/grit/ash_resources.h" |
| 8 #include "ash/ash_switches.h" |
8 #include "ash/desktop_background/desktop_background_controller.h" | 9 #include "ash/desktop_background/desktop_background_controller.h" |
9 #include "ash/desktop_background/desktop_background_controller_observer.h" | 10 #include "ash/desktop_background/desktop_background_controller_observer.h" |
| 11 #include "ash/desktop_background/desktop_background_controller_test_api.h" |
10 #include "ash/display/display_manager.h" | 12 #include "ash/display/display_manager.h" |
11 #include "ash/shell.h" | 13 #include "ash/shell.h" |
| 14 #include "ash/test/ash_test_base.h" |
| 15 #include "ash/test/ash_test_helper.h" |
12 #include "ash/test/display_manager_test_api.h" | 16 #include "ash/test/display_manager_test_api.h" |
| 17 #include "ash/test/test_user_wallpaper_delegate.h" |
13 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 19 #include "base/compiler_specific.h" |
14 #include "base/file_util.h" | 20 #include "base/file_util.h" |
15 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
| 22 #include "base/macros.h" |
16 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
17 #include "base/path_service.h" | 24 #include "base/path_service.h" |
18 #include "base/prefs/scoped_user_pref_update.h" | 25 #include "base/prefs/scoped_user_pref_update.h" |
19 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
20 #include "base/time/time.h" | 27 #include "base/time/time.h" |
21 #include "base/values.h" | 28 #include "base/values.h" |
22 #include "chrome/browser/chromeos/login/user.h" | 29 #include "chrome/browser/chromeos/login/user.h" |
23 #include "chrome/browser/chromeos/login/user_manager.h" | 30 #include "chrome/browser/chromeos/login/user_manager.h" |
24 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/test/base/in_process_browser_test.h" | 33 #include "chrome/test/base/in_process_browser_test.h" |
27 #include "chrome/test/base/testing_browser_process.h" | 34 #include "chrome/test/base/testing_browser_process.h" |
28 #include "chromeos/chromeos_switches.h" | 35 #include "chromeos/chromeos_switches.h" |
29 #include "chromeos/dbus/cryptohome_client.h" | 36 #include "chromeos/dbus/cryptohome_client.h" |
30 #include "content/public/test/test_utils.h" | 37 #include "content/public/test/test_utils.h" |
31 #include "ui/aura/env.h" | 38 #include "ui/aura/env.h" |
32 #include "ui/base/resource/resource_bundle.h" | 39 #include "ui/base/resource/resource_bundle.h" |
| 40 #include "ui/gfx/codec/jpeg_codec.h" |
| 41 #include "ui/gfx/point.h" |
| 42 #include "ui/gfx/rect.h" |
33 | 43 |
34 using namespace ash; | 44 using namespace ash; |
35 | 45 |
36 namespace chromeos { | 46 namespace chromeos { |
37 | 47 |
38 namespace { | 48 namespace { |
39 | 49 |
40 const int kLargeWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_LARGE; | 50 const int kLargeWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_LARGE; |
41 const int kSmallWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_SMALL; | 51 const int kSmallWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_SMALL; |
42 | 52 |
43 int kLargeWallpaperWidth = 256; | 53 int kLargeWallpaperWidth = 256; |
44 int kLargeWallpaperHeight = ash::kLargeWallpaperMaxHeight; | 54 int kLargeWallpaperHeight = chromeos::kLargeWallpaperMaxHeight; |
45 int kSmallWallpaperWidth = 256; | 55 int kSmallWallpaperWidth = 256; |
46 int kSmallWallpaperHeight = ash::kSmallWallpaperMaxHeight; | 56 int kSmallWallpaperHeight = chromeos::kSmallWallpaperMaxHeight; |
47 | 57 |
48 const char kTestUser1[] = "test1@domain.com"; | 58 const char kTestUser1[] = "test1@domain.com"; |
49 const char kTestUser1Hash[] = "test1@domain.com-hash"; | 59 const char kTestUser1Hash[] = "test1@domain.com-hash"; |
50 const char kTestUser2[] = "test2@domain.com"; | 60 const char kTestUser2[] = "test2@domain.com"; |
51 const char kTestUser2Hash[] = "test2@domain.com-hash"; | 61 const char kTestUser2Hash[] = "test2@domain.com-hash"; |
52 | 62 |
53 } // namespace | 63 } // namespace |
54 | 64 |
55 class WallpaperManagerBrowserTest : public InProcessBrowserTest, | 65 class WallpaperManagerBrowserTest : public InProcessBrowserTest, |
56 public DesktopBackgroundControllerObserver, | |
57 public testing::WithParamInterface<bool> { | 66 public testing::WithParamInterface<bool> { |
58 public: | 67 public: |
59 WallpaperManagerBrowserTest () : controller_(NULL), | 68 WallpaperManagerBrowserTest () : controller_(NULL), |
60 local_state_(NULL) { | 69 local_state_(NULL) { |
61 } | 70 } |
62 | 71 |
63 virtual ~WallpaperManagerBrowserTest () {} | 72 virtual ~WallpaperManagerBrowserTest () {} |
64 | 73 |
65 virtual void SetUpOnMainThread() OVERRIDE { | 74 virtual void SetUpOnMainThread() OVERRIDE { |
66 controller_ = ash::Shell::GetInstance()->desktop_background_controller(); | 75 controller_ = ash::Shell::GetInstance()->desktop_background_controller(); |
67 controller_->AddObserver(this); | |
68 local_state_ = g_browser_process->local_state(); | 76 local_state_ = g_browser_process->local_state(); |
| 77 DesktopBackgroundController::TestAPI(controller_) |
| 78 .set_wallpaper_reload_delay_for_test(0); |
69 UpdateDisplay("800x600"); | 79 UpdateDisplay("800x600"); |
70 } | 80 } |
71 | 81 |
72 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 82 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
73 command_line->AppendSwitch(switches::kLoginManager); | 83 command_line->AppendSwitch(switches::kLoginManager); |
74 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); | 84 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); |
75 if (GetParam()) | 85 if (GetParam()) |
76 command_line->AppendSwitch(::switches::kMultiProfiles); | 86 command_line->AppendSwitch(::switches::kMultiProfiles); |
77 } | 87 } |
78 | 88 |
79 virtual void CleanUpOnMainThread() OVERRIDE { | 89 virtual void CleanUpOnMainThread() OVERRIDE { |
80 controller_->RemoveObserver(this); | |
81 controller_ = NULL; | 90 controller_ = NULL; |
82 } | 91 } |
83 | 92 |
84 // Update the display configuration as given in |display_specs|. See | 93 // Update the display configuration as given in |display_specs|. See |
85 // ash::test::DisplayManagerTestApi::UpdateDisplay for more details. | 94 // ash::test::DisplayManagerTestApi::UpdateDisplay for more details. |
86 void UpdateDisplay(const std::string& display_specs) { | 95 void UpdateDisplay(const std::string& display_specs) { |
87 ash::test::DisplayManagerTestApi display_manager_test_api( | 96 ash::test::DisplayManagerTestApi display_manager_test_api( |
88 ash::Shell::GetInstance()->display_manager()); | 97 ash::Shell::GetInstance()->display_manager()); |
89 display_manager_test_api.UpdateDisplay(display_specs); | 98 display_manager_test_api.UpdateDisplay(display_specs); |
90 } | 99 } |
91 | 100 |
92 void WaitAsyncWallpaperLoadStarted() { | 101 void WaitAsyncWallpaperLoadStarted() { |
93 base::MessageLoop::current()->RunUntilIdle(); | 102 base::RunLoop().RunUntilIdle(); |
94 } | 103 } |
95 | 104 |
96 void WaitAsyncWallpaperLoadFinished() { | 105 void WaitAsyncWallpaperLoadFinished() { |
97 base::MessageLoop::current()->RunUntilIdle(); | 106 base::RunLoop().RunUntilIdle(); |
98 while (WallpaperManager::Get()->loading_.size()) { | 107 while (WallpaperManager::Get()->loading_.size()) { |
99 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 108 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
100 base::MessageLoop::current()->RunUntilIdle(); | 109 base::RunLoop().RunUntilIdle(); |
101 } | 110 } |
102 } | 111 } |
103 | 112 |
104 virtual void OnWallpaperDataChanged() OVERRIDE { | 113 protected: |
105 base::MessageLoop::current()->Quit(); | 114 // Colors used for different default wallpapers by |
106 } | 115 // WriteWallpapers(). |
| 116 static const SkColor kLargeWallpaperColor = SK_ColorRED; |
| 117 static const SkColor kSmallWallpaperColor = SK_ColorGREEN; |
| 118 static const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE; |
| 119 static const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW; |
107 | 120 |
108 protected: | 121 // A color that can be passed to CreateImage(). Specifically chosen to not |
| 122 // conflict with any of the default wallpaper colors. |
| 123 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA; |
| 124 |
| 125 // Dimension used for width and height of default wallpaper images. A |
| 126 // small value is used to minimize the amount of time spent compressing |
| 127 // and writing images. |
| 128 static const int kWallpaperSize = 2; |
| 129 |
109 // Return custom wallpaper path. Create directory if not exist. | 130 // Return custom wallpaper path. Create directory if not exist. |
110 base::FilePath GetCustomWallpaperPath(const char* sub_dir, | 131 base::FilePath GetCustomWallpaperPath(const char* sub_dir, |
111 const std::string& username_hash, | 132 const std::string& username_hash, |
112 const std::string& id) { | 133 const std::string& id) { |
113 base::FilePath wallpaper_path = | 134 base::FilePath wallpaper_path = |
114 WallpaperManager::Get()->GetCustomWallpaperPath(sub_dir, | 135 WallpaperManager::Get()->GetCustomWallpaperPath(sub_dir, |
115 username_hash, | 136 username_hash, |
116 id); | 137 id); |
117 if (!base::DirectoryExists(wallpaper_path.DirName())) | 138 if (!base::DirectoryExists(wallpaper_path.DirName())) |
118 base::CreateDirectory(wallpaper_path.DirName()); | 139 base::CreateDirectory(wallpaper_path.DirName()); |
(...skipping 17 matching lines...) Expand all Loading... |
136 wallpaper_path, | 157 wallpaper_path, |
137 reinterpret_cast<const char*>(image_data->front()), | 158 reinterpret_cast<const char*>(image_data->front()), |
138 image_data->size()); | 159 image_data->size()); |
139 EXPECT_EQ(static_cast<int>(image_data->size()), written); | 160 EXPECT_EQ(static_cast<int>(image_data->size()), written); |
140 } | 161 } |
141 | 162 |
142 int LoadedWallpapers() { | 163 int LoadedWallpapers() { |
143 return WallpaperManager::Get()->loaded_wallpapers(); | 164 return WallpaperManager::Get()->loaded_wallpapers(); |
144 } | 165 } |
145 | 166 |
| 167 // Creates a test image of size 1x1. |
| 168 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) { |
| 169 SkBitmap bitmap; |
| 170 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 171 bitmap.allocPixels(); |
| 172 bitmap.eraseColor(color); |
| 173 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 174 } |
| 175 |
| 176 // Writes a JPEG image of the specified size and color to |path|. Returns |
| 177 // true on success. |
| 178 bool WriteJPEGFile(const base::FilePath& path, |
| 179 int width, |
| 180 int height, |
| 181 SkColor color) { |
| 182 SkBitmap bitmap; |
| 183 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0); |
| 184 bitmap.allocPixels(); |
| 185 bitmap.eraseColor(color); |
| 186 |
| 187 const int kQuality = 80; |
| 188 std::vector<unsigned char> output; |
| 189 if (!gfx::JPEGCodec::Encode( |
| 190 static_cast<const unsigned char*>(bitmap.getPixels()), |
| 191 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 192 width, |
| 193 height, |
| 194 bitmap.rowBytes(), |
| 195 kQuality, |
| 196 &output)) { |
| 197 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; |
| 198 return false; |
| 199 } |
| 200 |
| 201 size_t bytes_written = base::WriteFile( |
| 202 path, reinterpret_cast<const char*>(&output[0]), output.size()); |
| 203 if (bytes_written != output.size()) { |
| 204 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of " |
| 205 << output.size() << " to " << path.value(); |
| 206 return false; |
| 207 } |
| 208 |
| 209 return true; |
| 210 } |
| 211 |
| 212 // Initializes default wallpaper paths "*default_*file" and writes JPEG |
| 213 // wallpaper images to them. |
| 214 // Only needs to be called (once) by tests that want to test loading of |
| 215 // default wallpapers. |
| 216 void WriteWallpapers() { |
| 217 wallpaper_dir_.reset(new base::ScopedTempDir); |
| 218 ASSERT_TRUE(wallpaper_dir_->CreateUniqueTempDir()); |
| 219 |
| 220 std::vector<std::string> options; |
| 221 options.push_back(std::string("WM_Test_cmdline")); |
| 222 const base::FilePath small_file = |
| 223 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("small.jpg")); |
| 224 options.push_back(std::string("--") + |
| 225 ash::switches::kAshDefaultWallpaperSmall + "=" + |
| 226 small_file.value()); |
| 227 const base::FilePath large_file = |
| 228 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("large.jpg")); |
| 229 options.push_back(std::string("--") + |
| 230 ash::switches::kAshDefaultWallpaperLarge + "=" + |
| 231 large_file.value()); |
| 232 const base::FilePath guest_small_file = |
| 233 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_small.jpg")); |
| 234 options.push_back(std::string("--") + |
| 235 ash::switches::kAshGuestWallpaperSmall + "=" + |
| 236 guest_small_file.value()); |
| 237 const base::FilePath guest_large_file = |
| 238 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_large.jpg")); |
| 239 options.push_back(std::string("--") + |
| 240 ash::switches::kAshGuestWallpaperLarge + "=" + |
| 241 guest_large_file.value()); |
| 242 |
| 243 ASSERT_TRUE(WriteJPEGFile(small_file, |
| 244 kWallpaperSize, |
| 245 kWallpaperSize, |
| 246 kSmallWallpaperColor)); |
| 247 ASSERT_TRUE(WriteJPEGFile(large_file, |
| 248 kWallpaperSize, |
| 249 kWallpaperSize, |
| 250 kLargeWallpaperColor)); |
| 251 ASSERT_TRUE(WriteJPEGFile(guest_small_file, |
| 252 kWallpaperSize, |
| 253 kWallpaperSize, |
| 254 kSmallGuestWallpaperColor)); |
| 255 ASSERT_TRUE(WriteJPEGFile(guest_large_file, |
| 256 kWallpaperSize, |
| 257 kWallpaperSize, |
| 258 kLargeGuestWallpaperColor)); |
| 259 |
| 260 wallpaper_manager_command_line_.reset(new base::CommandLine(options)); |
| 261 WallpaperManager::Get()->SetCommandLineForTesting( |
| 262 wallpaper_manager_command_line_.get()); |
| 263 } |
| 264 |
| 265 // Returns true if the color at the center of |image| is close to |
| 266 // |expected_color|. (The center is used so small wallpaper images can be |
| 267 // used.) |
| 268 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) { |
| 269 if (image.size().IsEmpty()) { |
| 270 LOG(ERROR) << "Image is empty"; |
| 271 return false; |
| 272 } |
| 273 |
| 274 const SkBitmap* bitmap = image.bitmap(); |
| 275 if (!bitmap) { |
| 276 LOG(ERROR) << "Unable to get bitmap from image"; |
| 277 return false; |
| 278 } |
| 279 |
| 280 bitmap->lockPixels(); |
| 281 gfx::Point center = gfx::Rect(image.size()).CenterPoint(); |
| 282 SkColor image_color = bitmap->getColor(center.x(), center.y()); |
| 283 bitmap->unlockPixels(); |
| 284 |
| 285 const int kDiff = 3; |
| 286 if (std::abs(static_cast<int>(SkColorGetA(image_color)) - |
| 287 static_cast<int>(SkColorGetA(expected_color))) > kDiff || |
| 288 std::abs(static_cast<int>(SkColorGetR(image_color)) - |
| 289 static_cast<int>(SkColorGetR(expected_color))) > kDiff || |
| 290 std::abs(static_cast<int>(SkColorGetG(image_color)) - |
| 291 static_cast<int>(SkColorGetG(expected_color))) > kDiff || |
| 292 std::abs(static_cast<int>(SkColorGetB(image_color)) - |
| 293 static_cast<int>(SkColorGetB(expected_color))) > kDiff) { |
| 294 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color |
| 295 << " but got 0x" << image_color; |
| 296 return false; |
| 297 } |
| 298 |
| 299 return true; |
| 300 } |
| 301 |
146 DesktopBackgroundController* controller_; | 302 DesktopBackgroundController* controller_; |
147 PrefService* local_state_; | 303 PrefService* local_state_; |
| 304 scoped_ptr<base::CommandLine> wallpaper_manager_command_line_; |
| 305 |
| 306 // Directory created by WriteWallpapersAndSetFlags() to store default |
| 307 // wallpaper images. |
| 308 scoped_ptr<base::ScopedTempDir> wallpaper_dir_; |
148 | 309 |
149 private: | 310 private: |
150 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerBrowserTest); | 311 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerBrowserTest); |
151 }; | 312 }; |
152 | 313 |
153 // Tests that the appropriate custom wallpaper (large vs. small) is loaded | 314 // Tests that the appropriate custom wallpaper (large vs. small) is loaded |
154 // depending on the desktop resolution. | 315 // depending on the desktop resolution. |
155 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, | 316 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, |
156 LoadCustomLargeWallpaperForLargeExternalScreen) { | 317 LoadCustomLargeWallpaperForLargeExternalScreen) { |
157 WallpaperManager* wallpaper_manager = WallpaperManager::Get(); | 318 WallpaperManager* wallpaper_manager = WallpaperManager::Get(); |
(...skipping 30 matching lines...) Expand all Loading... |
188 WaitAsyncWallpaperLoadFinished(); | 349 WaitAsyncWallpaperLoadFinished(); |
189 gfx::ImageSkia wallpaper = controller_->GetWallpaper(); | 350 gfx::ImageSkia wallpaper = controller_->GetWallpaper(); |
190 | 351 |
191 // Display is initialized to 800x600. The small resolution custom wallpaper is | 352 // Display is initialized to 800x600. The small resolution custom wallpaper is |
192 // expected. | 353 // expected. |
193 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width()); | 354 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width()); |
194 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height()); | 355 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height()); |
195 | 356 |
196 // Hook up another 800x600 display. This shouldn't trigger a reload. | 357 // Hook up another 800x600 display. This shouldn't trigger a reload. |
197 UpdateDisplay("800x600,800x600"); | 358 UpdateDisplay("800x600,800x600"); |
198 content::RunAllPendingInMessageLoop(); | 359 WaitAsyncWallpaperLoadFinished(); |
199 // The small resolution custom wallpaper is expected. | 360 // The small resolution custom wallpaper is expected. |
200 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width()); | 361 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width()); |
201 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height()); | 362 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height()); |
202 | 363 |
203 // Detach the secondary display. | 364 // Detach the secondary display. |
204 UpdateDisplay("800x600"); | 365 UpdateDisplay("800x600"); |
205 // Hook up a 2000x2000 display. The large resolution custom wallpaper should | 366 // Hook up a 2000x2000 display. The large resolution custom wallpaper should |
206 // be loaded. | 367 // be loaded. |
207 UpdateDisplay("800x600,2000x2000"); | 368 UpdateDisplay("800x600,2000x2000"); |
208 WaitAsyncWallpaperLoadFinished(); | 369 WaitAsyncWallpaperLoadFinished(); |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 testing::Bool()); | 724 testing::Bool()); |
564 | 725 |
565 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCrashRestoreInstantiation, | 726 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCrashRestoreInstantiation, |
566 WallpaperManagerBrowserTestCrashRestore, | 727 WallpaperManagerBrowserTestCrashRestore, |
567 testing::Bool()); | 728 testing::Bool()); |
568 | 729 |
569 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCacheUpdateInstantiation, | 730 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCacheUpdateInstantiation, |
570 WallpaperManagerBrowserTestCacheUpdate, | 731 WallpaperManagerBrowserTestCacheUpdate, |
571 testing::Bool()); | 732 testing::Bool()); |
572 | 733 |
| 734 // ---------------------------------------------------------------------- |
| 735 // Test default wallpapers. |
| 736 |
| 737 class TestObserver : public WallpaperManager::Observer { |
| 738 public: |
| 739 explicit TestObserver(WallpaperManager* wallpaper_manager) |
| 740 : update_wallpaper_count_(0), wallpaper_manager_(wallpaper_manager) { |
| 741 DCHECK(wallpaper_manager_); |
| 742 wallpaper_manager_->AddObserver(this); |
| 743 } |
| 744 |
| 745 virtual ~TestObserver() { |
| 746 wallpaper_manager_->RemoveObserver(this); |
| 747 } |
| 748 |
| 749 virtual void OnWallpaperAnimationFinished(const std::string&) OVERRIDE { |
| 750 } |
| 751 |
| 752 virtual void OnUpdateWallpaperForTesting() OVERRIDE { |
| 753 ++update_wallpaper_count_; |
| 754 } |
| 755 |
| 756 int GetUpdateWallpaperCountAndReset() { |
| 757 const size_t old = update_wallpaper_count_; |
| 758 update_wallpaper_count_ = 0; |
| 759 return old; |
| 760 } |
| 761 |
| 762 private: |
| 763 int update_wallpaper_count_; |
| 764 WallpaperManager* wallpaper_manager_; |
| 765 |
| 766 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 767 }; |
| 768 |
| 769 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, DisplayChange) { |
| 770 // TODO(derat|oshima|bshe): Host windows can't be resized on Win8. |
| 771 if (!ash::test::AshTestHelper::SupportsHostWindowResize()) |
| 772 return; |
| 773 |
| 774 TestObserver observer(WallpaperManager::Get()); |
| 775 |
| 776 // Set the wallpaper to ensure that UpdateWallpaper() will be called when the |
| 777 // display configuration changes. |
| 778 gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor); |
| 779 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH); |
| 780 |
| 781 // Small wallpaper images should be used for configurations less than or |
| 782 // equal to kSmallWallpaperMaxWidth by kSmallWallpaperMaxHeight, even if |
| 783 // multiple displays are connected. |
| 784 UpdateDisplay("800x600"); |
| 785 WaitAsyncWallpaperLoadFinished(); |
| 786 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL, |
| 787 WallpaperManager::Get()->GetAppropriateResolution()); |
| 788 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset()); |
| 789 |
| 790 UpdateDisplay("800x600,800x600"); |
| 791 WaitAsyncWallpaperLoadFinished(); |
| 792 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL, |
| 793 WallpaperManager::Get()->GetAppropriateResolution()); |
| 794 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset()); |
| 795 |
| 796 UpdateDisplay("1366x800"); |
| 797 WaitAsyncWallpaperLoadFinished(); |
| 798 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL, |
| 799 WallpaperManager::Get()->GetAppropriateResolution()); |
| 800 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 801 |
| 802 // At larger sizes, large wallpapers should be used. |
| 803 UpdateDisplay("1367x800"); |
| 804 WaitAsyncWallpaperLoadFinished(); |
| 805 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE, |
| 806 WallpaperManager::Get()->GetAppropriateResolution()); |
| 807 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 808 |
| 809 UpdateDisplay("1367x801"); |
| 810 WaitAsyncWallpaperLoadFinished(); |
| 811 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE, |
| 812 WallpaperManager::Get()->GetAppropriateResolution()); |
| 813 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 814 |
| 815 UpdateDisplay("2560x1700"); |
| 816 WaitAsyncWallpaperLoadFinished(); |
| 817 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE, |
| 818 WallpaperManager::Get()->GetAppropriateResolution()); |
| 819 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 820 |
| 821 // Rotated smaller screen may use larger image. |
| 822 UpdateDisplay("800x600/r"); |
| 823 WaitAsyncWallpaperLoadFinished(); |
| 824 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL, |
| 825 WallpaperManager::Get()->GetAppropriateResolution()); |
| 826 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 827 |
| 828 UpdateDisplay("800x600/r,800x600"); |
| 829 WaitAsyncWallpaperLoadFinished(); |
| 830 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL, |
| 831 WallpaperManager::Get()->GetAppropriateResolution()); |
| 832 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 833 UpdateDisplay("1366x800/r"); |
| 834 WaitAsyncWallpaperLoadFinished(); |
| 835 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE, |
| 836 WallpaperManager::Get()->GetAppropriateResolution()); |
| 837 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset()); |
| 838 |
| 839 // Max display size didn't chagne. |
| 840 UpdateDisplay("900x800/r,400x1366"); |
| 841 WaitAsyncWallpaperLoadFinished(); |
| 842 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset()); |
| 843 } |
| 844 |
| 845 // Test that WallpaperManager loads the appropriate wallpaper |
| 846 // images as specified via command-line flags in various situations. |
| 847 // Splitting these into separate tests avoids needing to run animations. |
| 848 // TODO(derat): Combine these into a single test |
| 849 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, SmallDefaultWallpaper) { |
| 850 if (!ash::test::AshTestHelper::SupportsMultipleDisplays()) |
| 851 return; |
| 852 |
| 853 WriteWallpapers(); |
| 854 |
| 855 // At 800x600, the small wallpaper should be loaded. |
| 856 UpdateDisplay("800x600"); |
| 857 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 858 WaitAsyncWallpaperLoadFinished(); |
| 859 EXPECT_TRUE( |
| 860 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor)); |
| 861 } |
| 862 |
| 863 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, LargeDefaultWallpaper) { |
| 864 if (!ash::test::AshTestHelper::SupportsMultipleDisplays()) |
| 865 return; |
| 866 |
| 867 WriteWallpapers(); |
| 868 UpdateDisplay("1600x1200"); |
| 869 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 870 WaitAsyncWallpaperLoadFinished(); |
| 871 EXPECT_TRUE( |
| 872 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor)); |
| 873 } |
| 874 |
| 875 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, |
| 876 LargeDefaultWallpaperWhenRotated) { |
| 877 if (!ash::test::AshTestHelper::SupportsMultipleDisplays()) |
| 878 return; |
| 879 WriteWallpapers(); |
| 880 |
| 881 UpdateDisplay("1200x800/r"); |
| 882 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 883 WaitAsyncWallpaperLoadFinished(); |
| 884 EXPECT_TRUE( |
| 885 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor)); |
| 886 } |
| 887 |
| 888 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, SmallGuestWallpaper) { |
| 889 if (!ash::test::AshTestHelper::SupportsMultipleDisplays()) |
| 890 return; |
| 891 WriteWallpapers(); |
| 892 UserManager::Get()->UserLoggedIn( |
| 893 UserManager::kGuestUserName, UserManager::kGuestUserName, false); |
| 894 UpdateDisplay("800x600"); |
| 895 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 896 WaitAsyncWallpaperLoadFinished(); |
| 897 EXPECT_TRUE( |
| 898 ImageIsNearColor(controller_->GetWallpaper(), kSmallGuestWallpaperColor)); |
| 899 } |
| 900 |
| 901 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, LargeGuestWallpaper) { |
| 902 if (!ash::test::AshTestHelper::SupportsMultipleDisplays()) |
| 903 return; |
| 904 |
| 905 WriteWallpapers(); |
| 906 UserManager::Get()->UserLoggedIn( |
| 907 UserManager::kGuestUserName, UserManager::kGuestUserName, false); |
| 908 UpdateDisplay("1600x1200"); |
| 909 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 910 WaitAsyncWallpaperLoadFinished(); |
| 911 EXPECT_TRUE( |
| 912 ImageIsNearColor(controller_->GetWallpaper(), kLargeGuestWallpaperColor)); |
| 913 } |
| 914 |
| 915 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, |
| 916 SwitchBetweenDefaultAndCustom) { |
| 917 // Start loading the default wallpaper. |
| 918 UpdateDisplay("640x480"); |
| 919 WriteWallpapers(); |
| 920 UserManager::Get()->UserLoggedIn(UserManager::kStubUser, "test_hash", false); |
| 921 |
| 922 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 923 |
| 924 // Custom wallpaper should be applied immediately, canceling the default |
| 925 // wallpaper load task. |
| 926 gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor); |
| 927 UserImage wallpaper(image); |
| 928 WallpaperManager::Get()->SetCustomWallpaper(UserManager::kStubUser, |
| 929 "test_hash", |
| 930 "test-nofile.jpeg", |
| 931 WALLPAPER_LAYOUT_STRETCH, |
| 932 User::CUSTOMIZED, |
| 933 wallpaper, |
| 934 true); |
| 935 WaitAsyncWallpaperLoadFinished(); |
| 936 |
| 937 EXPECT_TRUE( |
| 938 ImageIsNearColor(controller_->GetWallpaper(), kCustomWallpaperColor)); |
| 939 |
| 940 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); |
| 941 WaitAsyncWallpaperLoadFinished(); |
| 942 |
| 943 EXPECT_TRUE( |
| 944 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor)); |
| 945 } |
| 946 |
573 } // namespace chromeos | 947 } // namespace chromeos |
OLD | NEW |