Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / wallpaper_manager_browsertest.cc
blob0a749262a32a16d8637a775a761c22786160e3e5
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 "chrome/browser/chromeos/login/wallpaper_manager.h"
7 #include "ash/ash_switches.h"
8 #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_test_api.h"
11 #include "ash/display/display_manager.h"
12 #include "ash/shell.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/test/ash_test_helper.h"
15 #include "ash/test/display_manager_test_api.h"
16 #include "ash/test/test_user_wallpaper_delegate.h"
17 #include "base/command_line.h"
18 #include "base/compiler_specific.h"
19 #include "base/file_util.h"
20 #include "base/files/file_path.h"
21 #include "base/macros.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/path_service.h"
24 #include "base/prefs/scoped_user_pref_update.h"
25 #include "base/strings/string_number_conversions.h"
26 #include "base/time/time.h"
27 #include "base/values.h"
28 #include "chrome/browser/chromeos/login/user.h"
29 #include "chrome/browser/chromeos/login/user_manager.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/test/base/in_process_browser_test.h"
33 #include "chrome/test/base/testing_browser_process.h"
34 #include "chromeos/chromeos_switches.h"
35 #include "content/public/test/test_utils.h"
36 #include "grit/ash_resources.h"
37 #include "ui/aura/env.h"
38 #include "ui/base/resource/resource_bundle.h"
39 #include "ui/gfx/codec/jpeg_codec.h"
40 #include "ui/gfx/point.h"
41 #include "ui/gfx/rect.h"
43 using namespace ash;
45 namespace chromeos {
47 namespace {
49 const int kLargeWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_LARGE;
50 const int kSmallWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_SMALL;
52 int kLargeWallpaperWidth = 256;
53 int kLargeWallpaperHeight = chromeos::kLargeWallpaperMaxHeight;
54 int kSmallWallpaperWidth = 256;
55 int kSmallWallpaperHeight = chromeos::kSmallWallpaperMaxHeight;
57 const char kTestUser1[] = "test1@domain.com";
58 const char kTestUser1Hash[] = "test1@domain.com-hash";
59 const char kTestUser2[] = "test2@domain.com";
60 const char kTestUser2Hash[] = "test2@domain.com-hash";
62 } // namespace
64 class WallpaperManagerBrowserTest : public InProcessBrowserTest,
65 public testing::WithParamInterface<bool> {
66 public:
67 WallpaperManagerBrowserTest () : controller_(NULL),
68 local_state_(NULL) {
71 virtual ~WallpaperManagerBrowserTest () {}
73 virtual void SetUpOnMainThread() OVERRIDE {
74 controller_ = ash::Shell::GetInstance()->desktop_background_controller();
75 local_state_ = g_browser_process->local_state();
76 DesktopBackgroundController::TestAPI(controller_)
77 .set_wallpaper_reload_delay_for_test(0);
78 UpdateDisplay("800x600");
81 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
82 command_line->AppendSwitch(switches::kLoginManager);
83 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
84 if (GetParam())
85 command_line->AppendSwitch(::switches::kMultiProfiles);
88 virtual void CleanUpOnMainThread() OVERRIDE {
89 controller_ = NULL;
92 // Update the display configuration as given in |display_specs|. See
93 // ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
94 void UpdateDisplay(const std::string& display_specs) {
95 ash::test::DisplayManagerTestApi display_manager_test_api(
96 ash::Shell::GetInstance()->display_manager());
97 display_manager_test_api.UpdateDisplay(display_specs);
98 LOG(ERROR) << "UpdateDisplay(display_specs='" << display_specs
99 << "') done.";
100 WallpaperManager::GetAppropriateResolutionForTesting();
103 void WaitAsyncWallpaperLoadStarted() {
104 base::RunLoop().RunUntilIdle();
107 void WaitAsyncWallpaperLoadFinished() {
108 base::RunLoop().RunUntilIdle();
109 while (WallpaperManager::Get()->loading_.size()) {
110 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
111 base::RunLoop().RunUntilIdle();
115 protected:
116 // Colors used for different default wallpapers by
117 // WriteWallpapers().
118 static const SkColor kLargeWallpaperColor = SK_ColorRED;
119 static const SkColor kSmallWallpaperColor = SK_ColorGREEN;
120 static const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE;
121 static const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW;
123 // A color that can be passed to CreateImage(). Specifically chosen to not
124 // conflict with any of the default wallpaper colors.
125 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
127 // Dimension used for width and height of default wallpaper images. A
128 // small value is used to minimize the amount of time spent compressing
129 // and writing images.
130 static const int kWallpaperSize = 2;
132 // Return custom wallpaper path. Create directory if not exist.
133 base::FilePath GetCustomWallpaperPath(const char* sub_dir,
134 const std::string& username_hash,
135 const std::string& id) {
136 base::FilePath wallpaper_path =
137 WallpaperManager::Get()->GetCustomWallpaperPath(sub_dir,
138 username_hash,
139 id);
140 if (!base::DirectoryExists(wallpaper_path.DirName()))
141 base::CreateDirectory(wallpaper_path.DirName());
143 return wallpaper_path;
146 // Logs in |username|.
147 void LogIn(const std::string& username, const std::string& username_hash) {
148 UserManager::Get()->UserLoggedIn(username, username_hash, false);
149 WaitAsyncWallpaperLoadStarted();
152 // Saves bitmap |resource_id| to disk.
153 void SaveUserWallpaperData(const base::FilePath& wallpaper_path,
154 int resource_id) {
155 scoped_refptr<base::RefCountedStaticMemory> image_data(
156 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
157 resource_id, ui::SCALE_FACTOR_100P));
158 int written = base::WriteFile(
159 wallpaper_path,
160 reinterpret_cast<const char*>(image_data->front()),
161 image_data->size());
162 EXPECT_EQ(static_cast<int>(image_data->size()), written);
165 int LoadedWallpapers() {
166 return WallpaperManager::Get()->loaded_wallpapers();
169 void ClearDisposableWallpaperCache() {
170 WallpaperManager::Get()->ClearDisposableWallpaperCache();
173 // Creates a test image of size 1x1.
174 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) {
175 SkBitmap bitmap;
176 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
177 bitmap.allocPixels();
178 bitmap.eraseColor(color);
179 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
182 // Writes a JPEG image of the specified size and color to |path|. Returns
183 // true on success.
184 bool WriteJPEGFile(const base::FilePath& path,
185 int width,
186 int height,
187 SkColor color) {
188 SkBitmap bitmap;
189 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0);
190 bitmap.allocPixels();
191 bitmap.eraseColor(color);
193 const int kQuality = 80;
194 std::vector<unsigned char> output;
195 if (!gfx::JPEGCodec::Encode(
196 static_cast<const unsigned char*>(bitmap.getPixels()),
197 gfx::JPEGCodec::FORMAT_SkBitmap,
198 width,
199 height,
200 bitmap.rowBytes(),
201 kQuality,
202 &output)) {
203 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
204 return false;
207 size_t bytes_written = base::WriteFile(
208 path, reinterpret_cast<const char*>(&output[0]), output.size());
209 if (bytes_written != output.size()) {
210 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
211 << output.size() << " to " << path.value();
212 return false;
215 return true;
218 // Initializes default wallpaper paths "*default_*file" and writes JPEG
219 // wallpaper images to them.
220 // Only needs to be called (once) by tests that want to test loading of
221 // default wallpapers.
222 void WriteWallpapers() {
223 wallpaper_dir_.reset(new base::ScopedTempDir);
224 ASSERT_TRUE(wallpaper_dir_->CreateUniqueTempDir());
226 std::vector<std::string> options;
227 options.push_back(std::string("WM_Test_cmdline"));
228 const base::FilePath small_file =
229 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("small.jpg"));
230 options.push_back(std::string("--") +
231 ash::switches::kAshDefaultWallpaperSmall + "=" +
232 small_file.value());
233 const base::FilePath large_file =
234 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("large.jpg"));
235 options.push_back(std::string("--") +
236 ash::switches::kAshDefaultWallpaperLarge + "=" +
237 large_file.value());
238 const base::FilePath guest_small_file =
239 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_small.jpg"));
240 options.push_back(std::string("--") +
241 ash::switches::kAshGuestWallpaperSmall + "=" +
242 guest_small_file.value());
243 const base::FilePath guest_large_file =
244 wallpaper_dir_->path().Append(FILE_PATH_LITERAL("guest_large.jpg"));
245 options.push_back(std::string("--") +
246 ash::switches::kAshGuestWallpaperLarge + "=" +
247 guest_large_file.value());
249 ASSERT_TRUE(WriteJPEGFile(small_file,
250 kWallpaperSize,
251 kWallpaperSize,
252 kSmallWallpaperColor));
253 ASSERT_TRUE(WriteJPEGFile(large_file,
254 kWallpaperSize,
255 kWallpaperSize,
256 kLargeWallpaperColor));
257 ASSERT_TRUE(WriteJPEGFile(guest_small_file,
258 kWallpaperSize,
259 kWallpaperSize,
260 kSmallGuestWallpaperColor));
261 ASSERT_TRUE(WriteJPEGFile(guest_large_file,
262 kWallpaperSize,
263 kWallpaperSize,
264 kLargeGuestWallpaperColor));
266 wallpaper_manager_command_line_.reset(new base::CommandLine(options));
267 WallpaperManager::Get()->SetCommandLineForTesting(
268 wallpaper_manager_command_line_.get());
271 // Returns true if the color at the center of |image| is close to
272 // |expected_color|. (The center is used so small wallpaper images can be
273 // used.)
274 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) {
275 if (image.size().IsEmpty()) {
276 LOG(ERROR) << "Image is empty";
277 return false;
280 const SkBitmap* bitmap = image.bitmap();
281 if (!bitmap) {
282 LOG(ERROR) << "Unable to get bitmap from image";
283 return false;
286 bitmap->lockPixels();
287 gfx::Point center = gfx::Rect(image.size()).CenterPoint();
288 SkColor image_color = bitmap->getColor(center.x(), center.y());
289 bitmap->unlockPixels();
291 const int kDiff = 3;
292 if (std::abs(static_cast<int>(SkColorGetA(image_color)) -
293 static_cast<int>(SkColorGetA(expected_color))) > kDiff ||
294 std::abs(static_cast<int>(SkColorGetR(image_color)) -
295 static_cast<int>(SkColorGetR(expected_color))) > kDiff ||
296 std::abs(static_cast<int>(SkColorGetG(image_color)) -
297 static_cast<int>(SkColorGetG(expected_color))) > kDiff ||
298 std::abs(static_cast<int>(SkColorGetB(image_color)) -
299 static_cast<int>(SkColorGetB(expected_color))) > kDiff) {
300 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color
301 << " but got 0x" << image_color;
302 return false;
305 return true;
308 DesktopBackgroundController* controller_;
309 PrefService* local_state_;
310 scoped_ptr<base::CommandLine> wallpaper_manager_command_line_;
312 // Directory created by WriteWallpapersAndSetFlags() to store default
313 // wallpaper images.
314 scoped_ptr<base::ScopedTempDir> wallpaper_dir_;
316 private:
317 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerBrowserTest);
320 // Tests that the appropriate custom wallpaper (large vs. small) is loaded
321 // depending on the desktop resolution.
322 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
323 LoadCustomLargeWallpaperForLargeExternalScreen) {
324 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
325 LogIn(kTestUser1, kTestUser1Hash);
326 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
327 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
328 kSmallWallpaperSubDir,
329 kTestUser1Hash,
330 id);
331 base::FilePath large_wallpaper_path = GetCustomWallpaperPath(
332 kLargeWallpaperSubDir,
333 kTestUser1Hash,
334 id);
336 // Saves the small/large resolution wallpapers to small/large custom
337 // wallpaper paths.
338 SaveUserWallpaperData(small_wallpaper_path,
339 kSmallWallpaperResourceId);
340 SaveUserWallpaperData(large_wallpaper_path,
341 kLargeWallpaperResourceId);
343 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
344 // Saves wallpaper info to local state for user |kTestUser1|.
345 WallpaperInfo info = {
346 relative_path,
347 WALLPAPER_LAYOUT_CENTER_CROPPED,
348 User::CUSTOMIZED,
349 base::Time::Now().LocalMidnight()
351 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
353 // Set the wallpaper for |kTestUser1|.
354 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
355 WaitAsyncWallpaperLoadFinished();
356 gfx::ImageSkia wallpaper = controller_->GetWallpaper();
358 // Display is initialized to 800x600. The small resolution custom wallpaper is
359 // expected.
360 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width());
361 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height());
363 // Hook up another 800x600 display. This shouldn't trigger a reload.
364 UpdateDisplay("800x600,800x600");
365 WaitAsyncWallpaperLoadFinished();
366 // The small resolution custom wallpaper is expected.
367 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width());
368 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height());
370 // Detach the secondary display.
371 UpdateDisplay("800x600");
372 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
373 // be loaded.
374 UpdateDisplay("800x600,2000x2000");
375 WaitAsyncWallpaperLoadFinished();
376 wallpaper = controller_->GetWallpaper();
378 // The large resolution custom wallpaper is expected.
379 EXPECT_EQ(kLargeWallpaperWidth, wallpaper.width());
380 EXPECT_EQ(kLargeWallpaperHeight, wallpaper.height());
382 // Detach the secondary display.
383 UpdateDisplay("800x600");
384 // Hook up the 2000x2000 display again. The large resolution default wallpaper
385 // should persist. Test for crbug/165788.
386 UpdateDisplay("800x600,2000x2000");
387 WaitAsyncWallpaperLoadFinished();
388 wallpaper = controller_->GetWallpaper();
390 // The large resolution custom wallpaper is expected.
391 EXPECT_EQ(kLargeWallpaperWidth, wallpaper.width());
392 EXPECT_EQ(kLargeWallpaperHeight, wallpaper.height());
395 // If chrome tries to reload the same wallpaper twice, the latter request should
396 // be prevented. Otherwise, there are some strange animation issues as
397 // described in crbug.com/158383.
398 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
399 PreventReloadingSameWallpaper) {
400 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
401 // New user log in, a default wallpaper is loaded.
402 LogIn(kTestUser1, kTestUser1Hash);
403 EXPECT_EQ(1, LoadedWallpapers());
404 // Loads the same wallpaper before the initial one finished. It should be
405 // prevented.
406 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
407 WaitAsyncWallpaperLoadFinished();
408 EXPECT_EQ(1, LoadedWallpapers());
409 // Loads the same wallpaper after the initial one finished. It should be
410 // prevented.
411 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
412 WaitAsyncWallpaperLoadFinished();
413 EXPECT_EQ(1, LoadedWallpapers());
414 ClearDisposableWallpaperCache();
416 // Change wallpaper to a custom wallpaper.
417 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
418 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
419 kSmallWallpaperSubDir,
420 kTestUser1Hash,
421 id);
422 SaveUserWallpaperData(small_wallpaper_path,
423 kSmallWallpaperResourceId);
425 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
426 // Saves wallpaper info to local state for user |kTestUser1|.
427 WallpaperInfo info = {
428 relative_path,
429 WALLPAPER_LAYOUT_CENTER_CROPPED,
430 User::CUSTOMIZED,
431 base::Time::Now().LocalMidnight()
433 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
435 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
436 WaitAsyncWallpaperLoadStarted();
437 EXPECT_EQ(2, LoadedWallpapers());
438 // Loads the same wallpaper before the initial one finished. It should be
439 // prevented.
440 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
441 WaitAsyncWallpaperLoadStarted();
442 EXPECT_EQ(2, LoadedWallpapers());
443 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
444 WaitAsyncWallpaperLoadFinished();
445 EXPECT_EQ(2, LoadedWallpapers());
448 // Some users have old user profiles which may have legacy wallpapers. And these
449 // lagacy wallpapers should migrate to new wallpaper picker version seamlessly.
450 // This tests make sure we compatible with migrated old wallpapers.
451 // crosbug.com/38429
452 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
453 PRE_UseMigratedWallpaperInfo) {
454 // New user log in, a default wallpaper is loaded.
455 LogIn(kTestUser1, kTestUser1Hash);
456 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
457 // wallpaper info directly to simulate the wallpaper migration. See
458 // crosbug.com/38429 for details about why we modify wallpaper info this way.
459 WallpaperInfo info = {
460 "123",
461 WALLPAPER_LAYOUT_CENTER_CROPPED,
462 User::DEFAULT,
463 base::Time::Now().LocalMidnight()
465 base::FilePath user_data_dir;
466 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
467 SaveUserWallpaperData(user_data_dir.Append("123"),
468 kLargeWallpaperResourceId);
469 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
472 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
473 UseMigratedWallpaperInfo) {
474 LogIn(kTestUser1, kTestUser1Hash);
475 WaitAsyncWallpaperLoadFinished();
476 // This test should finish normally. If timeout, it is probably because
477 // migrated wallpaper is somehow not loaded. Bad things can happen if
478 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
481 // Some users have old user profiles which may never get a chance to migrate.
482 // This tests make sure we compatible with these profiles.
483 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
484 PRE_UsePreMigrationWallpaperInfo) {
485 // New user log in, a default wallpaper is loaded.
486 LogIn(kTestUser1, kTestUser1Hash);
487 // Old wallpaper migration code doesn't exist in codebase anymore. So if
488 // user's profile is not migrated, it is the same as no wallpaper info. To
489 // simulate this, we remove user's wallpaper info here.
490 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1);
493 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
494 UsePreMigrationWallpaperInfo) {
495 LogIn(kTestUser1, kTestUser1Hash);
496 WaitAsyncWallpaperLoadFinished();
497 // This test should finish normally. If timeout, it is probably because chrome
498 // can not handle pre migrated user profile (M21 profile or older).
501 // Test for http://crbug.com/265689. When hooked up a large external monitor,
502 // the default large resolution wallpaper should load.
503 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
504 HotPlugInScreenAtGAIALoginScreen) {
505 UpdateDisplay("800x600");
506 // Set initial wallpaper to the default wallpaper.
507 WallpaperManager::Get()->SetDefaultWallpaperNow(UserManager::kStubUser);
508 WaitAsyncWallpaperLoadFinished();
510 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
511 // be loaded.
512 UpdateDisplay("800x600,2000x2000");
513 WaitAsyncWallpaperLoadFinished();
516 class WallpaperManagerBrowserTestNoAnimation
517 : public WallpaperManagerBrowserTest {
518 public:
519 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
520 command_line->AppendSwitch(switches::kLoginManager);
521 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
522 command_line->AppendSwitch(chromeos::switches::kDisableLoginAnimations);
523 command_line->AppendSwitch(chromeos::switches::kDisableBootAnimation);
527 // Same test as WallpaperManagerBrowserTest.UseMigratedWallpaperInfo. But
528 // disabled boot and login animation.
529 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestNoAnimation,
530 PRE_UseMigratedWallpaperInfo) {
531 // New user log in, a default wallpaper is loaded.
532 LogIn(kTestUser1, kTestUser1Hash);
533 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
534 // wallpaper info directly to simulate the wallpaper migration. See
535 // crosbug.com/38429 for details about why we modify wallpaper info this way.
536 WallpaperInfo info = {
537 "123",
538 WALLPAPER_LAYOUT_CENTER_CROPPED,
539 User::DEFAULT,
540 base::Time::Now().LocalMidnight()
542 base::FilePath user_data_dir;
543 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
544 SaveUserWallpaperData(user_data_dir.Append("123"),
545 kLargeWallpaperResourceId);
546 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
549 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestNoAnimation,
550 UseMigratedWallpaperInfo) {
551 LogIn(kTestUser1, kTestUser1Hash);
552 WaitAsyncWallpaperLoadFinished();
553 // This test should finish normally. If timeout, it is probably because
554 // migrated wallpaper is somehow not loaded. Bad things can happen if
555 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
558 // Same test as WallpaperManagerBrowserTest.UsePreMigrationWallpaperInfo. But
559 // disabled boot and login animation.
560 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestNoAnimation,
561 PRE_UsePreMigrationWallpaperInfo) {
562 // New user log in, a default wallpaper is loaded.
563 LogIn(kTestUser1, kTestUser1Hash);
564 WaitAsyncWallpaperLoadFinished();
565 // Old wallpaper migration code doesn't exist in codebase anymore. So if
566 // user's profile is not migrated, it is the same as no wallpaper info. To
567 // simulate this, we remove user's wallpaper info here.
568 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1);
571 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestNoAnimation,
572 UsePreMigrationWallpaperInfo) {
573 LogIn(kTestUser1, kTestUser1Hash);
574 WaitAsyncWallpaperLoadFinished();
575 // This test should finish normally. If timeout, it is probably because chrome
576 // can not handle pre migrated user profile (M21 profile or older).
579 class WallpaperManagerBrowserTestCrashRestore
580 : public WallpaperManagerBrowserTest {
581 public:
582 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
583 command_line->AppendSwitch(chromeos::switches::kDisableLoginAnimations);
584 command_line->AppendSwitch(chromeos::switches::kDisableBootAnimation);
585 command_line->AppendSwitch(::switches::kMultiProfiles);
586 command_line->AppendSwitchASCII(switches::kLoginUser, kTestUser1);
587 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
591 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestCrashRestore,
592 PRE_RestoreWallpaper) {
593 LogIn(kTestUser1, kTestUser1Hash);
594 WaitAsyncWallpaperLoadFinished();
597 // Test for crbug.com/270278. It simulates a browser crash and verifies if user
598 // wallpaper is loaded.
599 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestCrashRestore,
600 RestoreWallpaper) {
601 EXPECT_EQ(1, LoadedWallpapers());
604 class WallpaperManagerBrowserTestCacheUpdate
605 : public WallpaperManagerBrowserTest {
606 public:
607 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
608 command_line->AppendSwitch(::switches::kMultiProfiles);
609 command_line->AppendSwitchASCII(switches::kLoginUser, kTestUser1);
610 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
612 protected:
613 // Creates a test image of size 1x1.
614 gfx::ImageSkia CreateTestImage(SkColor color) {
615 SkBitmap bitmap;
616 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
617 bitmap.allocPixels();
618 bitmap.eraseColor(color);
619 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
623 // Sets kTestUser1's wallpaper to a custom wallpaper.
624 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestCacheUpdate,
625 PRE_VerifyWallpaperCache) {
626 // Add kTestUser1 to user list. kTestUser1 is the default login profile.
627 LogIn(kTestUser1, kTestUser1Hash);
629 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
630 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
631 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
632 kSmallWallpaperSubDir,
633 kTestUser1Hash,
634 id);
635 base::FilePath large_wallpaper_path = GetCustomWallpaperPath(
636 kLargeWallpaperSubDir,
637 kTestUser1Hash,
638 id);
640 // Saves the small/large resolution wallpapers to small/large custom
641 // wallpaper paths.
642 SaveUserWallpaperData(small_wallpaper_path,
643 kSmallWallpaperResourceId);
644 SaveUserWallpaperData(large_wallpaper_path,
645 kLargeWallpaperResourceId);
647 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
648 // Saves wallpaper info to local state for user |kTestUser1|.
649 WallpaperInfo info = {
650 relative_path,
651 WALLPAPER_LAYOUT_CENTER_CROPPED,
652 User::CUSTOMIZED,
653 base::Time::Now().LocalMidnight()
655 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
656 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
657 WaitAsyncWallpaperLoadFinished();
658 scoped_ptr<WallpaperManager::TestApi> test_api;
659 test_api.reset(new WallpaperManager::TestApi(wallpaper_manager));
660 // Verify SetUserWallpaperNow updates wallpaper cache.
661 gfx::ImageSkia cached_wallpaper;
662 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
665 // Tests for crbug.com/339576. Wallpaper cache should be updated in
666 // multi-profile mode when user:
667 // 1. chooses an online wallpaper from wallpaper
668 // picker (calls SetWallpaperFromImageSkia);
669 // 2. chooses a custom wallpaper from wallpaper
670 // picker (calls SetCustomWallpaper);
671 // 3. reverts to a default wallpaper.
672 // Also, when user login at multi-profile mode, previous logged in users'
673 // wallpaper cache should not be deleted.
674 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTestCacheUpdate,
675 VerifyWallpaperCache) {
676 WaitAsyncWallpaperLoadFinished();
677 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
678 scoped_ptr<WallpaperManager::TestApi> test_api;
679 test_api.reset(new WallpaperManager::TestApi(wallpaper_manager));
680 gfx::ImageSkia cached_wallpaper;
681 // Previous custom wallpaper should be cached after user login.
682 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
684 LogIn(kTestUser2, kTestUser2Hash);
685 WaitAsyncWallpaperLoadFinished();
686 // Login another user should not delete logged in user's wallpaper cache.
687 // Note active user is still kTestUser1.
688 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
690 gfx::ImageSkia red_wallpaper = CreateTestImage(SK_ColorRED);
691 wallpaper_manager->SetWallpaperFromImageSkia(kTestUser1,
692 red_wallpaper,
693 WALLPAPER_LAYOUT_CENTER,
694 true);
695 WaitAsyncWallpaperLoadFinished();
696 // SetWallpaperFromImageSkia should update wallpaper cache when multi-profile
697 // is turned on.
698 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
699 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(red_wallpaper));
701 gfx::ImageSkia green_wallpaper = CreateTestImage(SK_ColorGREEN);
702 wallpaper_manager->SetCustomWallpaper(kTestUser1,
703 kTestUser1Hash,
704 "dummy", // dummy file name
705 WALLPAPER_LAYOUT_CENTER,
706 User::CUSTOMIZED,
707 green_wallpaper,
708 true);
709 WaitAsyncWallpaperLoadFinished();
710 // SetCustomWallpaper should also update wallpaper cache when multi-profile is
711 // turned on.
712 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
713 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(green_wallpaper));
715 wallpaper_manager->SetDefaultWallpaperNow(kTestUser1);
716 WaitAsyncWallpaperLoadFinished();
717 // SetDefaultWallpaper should invalidate the user's wallpaper cache.
718 EXPECT_FALSE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
721 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestInstantiation,
722 WallpaperManagerBrowserTest,
723 testing::Bool());
725 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestNoAnimationInstantiation,
726 WallpaperManagerBrowserTestNoAnimation,
727 testing::Bool());
729 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCrashRestoreInstantiation,
730 WallpaperManagerBrowserTestCrashRestore,
731 testing::Bool());
733 INSTANTIATE_TEST_CASE_P(WallpaperManagerBrowserTestCacheUpdateInstantiation,
734 WallpaperManagerBrowserTestCacheUpdate,
735 testing::Bool());
737 // ----------------------------------------------------------------------
738 // Test default wallpapers.
740 class TestObserver : public WallpaperManager::Observer {
741 public:
742 explicit TestObserver(WallpaperManager* wallpaper_manager)
743 : update_wallpaper_count_(0), wallpaper_manager_(wallpaper_manager) {
744 DCHECK(wallpaper_manager_);
745 wallpaper_manager_->AddObserver(this);
748 virtual ~TestObserver() {
749 wallpaper_manager_->RemoveObserver(this);
752 virtual void OnWallpaperAnimationFinished(const std::string&) OVERRIDE {
755 virtual void OnUpdateWallpaperForTesting() OVERRIDE {
756 ++update_wallpaper_count_;
759 int GetUpdateWallpaperCountAndReset() {
760 const size_t old = update_wallpaper_count_;
761 update_wallpaper_count_ = 0;
762 return old;
765 private:
766 int update_wallpaper_count_;
767 WallpaperManager* wallpaper_manager_;
769 DISALLOW_COPY_AND_ASSIGN(TestObserver);
772 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, DisplayChange) {
773 // TODO(derat|oshima|bshe): Host windows can't be resized on Win8.
774 if (!ash::test::AshTestHelper::SupportsHostWindowResize())
775 return;
777 TestObserver observer(WallpaperManager::Get());
779 // Set the wallpaper to ensure that UpdateWallpaper() will be called when the
780 // display configuration changes.
781 gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor);
782 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
784 // Small wallpaper images should be used for configurations less than or
785 // equal to kSmallWallpaperMaxWidth by kSmallWallpaperMaxHeight, even if
786 // multiple displays are connected.
787 UpdateDisplay("800x600");
788 WaitAsyncWallpaperLoadFinished();
789 WallpaperManager::GetAppropriateResolutionForTesting();
790 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
791 WallpaperManager::Get()->GetAppropriateResolution());
792 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
794 UpdateDisplay("800x600,800x600");
795 WaitAsyncWallpaperLoadFinished();
796 WallpaperManager::GetAppropriateResolutionForTesting();
797 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
798 WallpaperManager::Get()->GetAppropriateResolution());
799 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
801 UpdateDisplay("1366x800");
802 WaitAsyncWallpaperLoadFinished();
803 WallpaperManager::GetAppropriateResolutionForTesting();
804 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
805 WallpaperManager::Get()->GetAppropriateResolution());
806 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
808 // At larger sizes, large wallpapers should be used.
809 UpdateDisplay("1367x800");
810 WaitAsyncWallpaperLoadFinished();
811 WallpaperManager::GetAppropriateResolutionForTesting();
812 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
813 WallpaperManager::Get()->GetAppropriateResolution());
814 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
816 UpdateDisplay("1367x801");
817 WaitAsyncWallpaperLoadFinished();
818 WallpaperManager::GetAppropriateResolutionForTesting();
819 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
820 WallpaperManager::Get()->GetAppropriateResolution());
821 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
823 UpdateDisplay("2560x1700");
824 WaitAsyncWallpaperLoadFinished();
825 WallpaperManager::GetAppropriateResolutionForTesting();
826 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
827 WallpaperManager::Get()->GetAppropriateResolution());
828 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
830 // Rotated smaller screen may use larger image.
831 UpdateDisplay("800x600/r");
832 WaitAsyncWallpaperLoadFinished();
833 WallpaperManager::GetAppropriateResolutionForTesting();
834 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
835 WallpaperManager::Get()->GetAppropriateResolution());
836 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
838 UpdateDisplay("800x600/r,800x600");
839 WaitAsyncWallpaperLoadFinished();
840 WallpaperManager::GetAppropriateResolutionForTesting();
841 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
842 WallpaperManager::Get()->GetAppropriateResolution());
843 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
844 UpdateDisplay("1366x800/r");
845 WallpaperManager::GetAppropriateResolutionForTesting();
846 WaitAsyncWallpaperLoadFinished();
847 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
848 WallpaperManager::Get()->GetAppropriateResolution());
849 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
851 // Max display size didn't chagne.
852 UpdateDisplay("900x800/r,400x1366");
853 WallpaperManager::GetAppropriateResolutionForTesting();
854 WaitAsyncWallpaperLoadFinished();
855 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
858 // Test that WallpaperManager loads the appropriate wallpaper
859 // images as specified via command-line flags in various situations.
860 // Splitting these into separate tests avoids needing to run animations.
861 // TODO(derat): Combine these into a single test
862 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, SmallDefaultWallpaper) {
863 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
864 return;
866 WriteWallpapers();
868 // At 800x600, the small wallpaper should be loaded.
869 UpdateDisplay("800x600");
870 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
871 WaitAsyncWallpaperLoadFinished();
872 EXPECT_TRUE(
873 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor));
876 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, LargeDefaultWallpaper) {
877 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
878 return;
880 WriteWallpapers();
881 UpdateDisplay("1600x1200");
882 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
883 WaitAsyncWallpaperLoadFinished();
884 EXPECT_TRUE(
885 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor));
888 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
889 LargeDefaultWallpaperWhenRotated) {
890 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
891 return;
892 WriteWallpapers();
894 UpdateDisplay("1200x800/r");
895 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
896 WaitAsyncWallpaperLoadFinished();
897 EXPECT_TRUE(
898 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor));
901 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, SmallGuestWallpaper) {
902 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
903 return;
904 WriteWallpapers();
905 UserManager::Get()->UserLoggedIn(
906 UserManager::kGuestUserName, UserManager::kGuestUserName, false);
907 UpdateDisplay("800x600");
908 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
909 WaitAsyncWallpaperLoadFinished();
910 EXPECT_TRUE(
911 ImageIsNearColor(controller_->GetWallpaper(), kSmallGuestWallpaperColor));
914 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest, LargeGuestWallpaper) {
915 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
916 return;
918 WriteWallpapers();
919 UserManager::Get()->UserLoggedIn(
920 UserManager::kGuestUserName, UserManager::kGuestUserName, false);
921 UpdateDisplay("1600x1200");
922 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
923 WaitAsyncWallpaperLoadFinished();
924 EXPECT_TRUE(
925 ImageIsNearColor(controller_->GetWallpaper(), kLargeGuestWallpaperColor));
928 IN_PROC_BROWSER_TEST_P(WallpaperManagerBrowserTest,
929 SwitchBetweenDefaultAndCustom) {
930 // Start loading the default wallpaper.
931 UpdateDisplay("640x480");
932 WriteWallpapers();
933 UserManager::Get()->UserLoggedIn(UserManager::kStubUser, "test_hash", false);
935 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
937 // Custom wallpaper should be applied immediately, canceling the default
938 // wallpaper load task.
939 gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor);
940 WallpaperManager::Get()->SetCustomWallpaper(UserManager::kStubUser,
941 "test_hash",
942 "test-nofile.jpeg",
943 WALLPAPER_LAYOUT_STRETCH,
944 User::CUSTOMIZED,
945 image,
946 true);
947 WaitAsyncWallpaperLoadFinished();
949 EXPECT_TRUE(
950 ImageIsNearColor(controller_->GetWallpaper(), kCustomWallpaperColor));
952 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
953 WaitAsyncWallpaperLoadFinished();
955 EXPECT_TRUE(
956 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor));
959 } // namespace chromeos