Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / wallpaper / wallpaper_manager_browsertest.cc
blob86561757eb383a44ae85a80c9f520d3aa6d4dc82
1 // Copyright 2014 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/users/wallpaper/wallpaper_manager.h"
7 #include "ash/desktop_background/desktop_background_controller.h"
8 #include "ash/desktop_background/desktop_background_controller_observer.h"
9 #include "ash/desktop_background/desktop_background_controller_test_api.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/ash_test_helper.h"
14 #include "ash/test/display_manager_test_api.h"
15 #include "ash/test/test_user_wallpaper_delegate.h"
16 #include "base/command_line.h"
17 #include "base/compiler_specific.h"
18 #include "base/files/file_path.h"
19 #include "base/files/file_util.h"
20 #include "base/macros.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/path_service.h"
23 #include "base/prefs/scoped_user_pref_update.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/time/time.h"
26 #include "base/values.h"
27 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager_test_utils.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/testing_browser_process.h"
31 #include "chromeos/chromeos_switches.h"
32 #include "chromeos/login/user_names.h"
33 #include "components/user_manager/user.h"
34 #include "components/user_manager/user_manager.h"
35 #include "content/public/test/test_utils.h"
36 #include "ui/aura/env.h"
37 #include "ui/gfx/geometry/point.h"
38 #include "ui/gfx/geometry/rect.h"
39 #include "ui/gfx/image/image_skia.h"
41 using wallpaper::WallpaperLayout;
42 using wallpaper::WallpaperInfo;
43 using wallpaper::WALLPAPER_LAYOUT_CENTER;
44 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
45 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
46 using wallpaper::WALLPAPER_LAYOUT_TILE;
48 namespace chromeos {
50 namespace {
52 int kLargeWallpaperWidth = 256;
53 int kLargeWallpaperHeight = wallpaper::kLargeWallpaperMaxHeight;
54 int kSmallWallpaperWidth = 256;
55 int kSmallWallpaperHeight = wallpaper::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:
66 WallpaperManagerBrowserTest () : controller_(NULL),
67 local_state_(NULL) {
70 ~WallpaperManagerBrowserTest() override {}
72 void SetUpOnMainThread() override {
73 controller_ = ash::Shell::GetInstance()->desktop_background_controller();
74 local_state_ = g_browser_process->local_state();
75 ash::DesktopBackgroundController::TestAPI(controller_)
76 .set_wallpaper_reload_delay_for_test(0);
77 UpdateDisplay("800x600");
80 void SetUpCommandLine(base::CommandLine* command_line) override {
81 command_line->AppendSwitch(switches::kLoginManager);
82 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
85 void TearDownOnMainThread() override { controller_ = NULL; }
87 // Update the display configuration as given in |display_specs|. See
88 // ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
89 void UpdateDisplay(const std::string& display_specs) {
90 ash::test::DisplayManagerTestApi display_manager_test_api(
91 ash::Shell::GetInstance()->display_manager());
92 display_manager_test_api.UpdateDisplay(display_specs);
95 void WaitAsyncWallpaperLoadStarted() {
96 base::RunLoop().RunUntilIdle();
99 protected:
101 // Return custom wallpaper path. Create directory if not exist.
102 base::FilePath GetCustomWallpaperPath(const char* sub_dir,
103 const std::string& username_hash,
104 const std::string& id) {
105 base::FilePath wallpaper_path =
106 WallpaperManager::Get()->GetCustomWallpaperPath(sub_dir,
107 username_hash,
108 id);
109 if (!base::DirectoryExists(wallpaper_path.DirName()))
110 base::CreateDirectory(wallpaper_path.DirName());
112 return wallpaper_path;
115 // Logs in |username|.
116 void LogIn(const std::string& username, const std::string& username_hash) {
117 user_manager::UserManager::Get()->UserLoggedIn(
118 username, username_hash, false);
119 WaitAsyncWallpaperLoadStarted();
122 // Logs in |username| and sets it as child account.
123 void LogInAsChild(
124 const std::string& username, const std::string& username_hash) {
125 user_manager::UserManager::Get()->UserLoggedIn(
126 username, username_hash, false);
127 user_manager::User* user =
128 user_manager::UserManager::Get()->FindUserAndModify(username);
129 user_manager::UserManager::Get()->ChangeUserChildStatus(
130 user, true /* is_child */);
133 int LoadedWallpapers() {
134 return WallpaperManager::Get()->loaded_wallpapers_for_test();
137 void CacheUserWallpaper(const std::string& user) {
138 WallpaperManager::Get()->CacheUserWallpaper(user);
141 void ClearDisposableWallpaperCache() {
142 WallpaperManager::Get()->ClearDisposableWallpaperCache();
145 // Initializes default wallpaper paths "*default_*file" and writes JPEG
146 // wallpaper images to them.
147 // Only needs to be called (once) by tests that want to test loading of
148 // default wallpapers.
149 void CreateCmdlineWallpapers() {
150 wallpaper_dir_.reset(new base::ScopedTempDir);
151 ASSERT_TRUE(wallpaper_dir_->CreateUniqueTempDir());
152 wallpaper_manager_test_utils::CreateCmdlineWallpapers(
153 *wallpaper_dir_, &wallpaper_manager_command_line_);
156 ash::DesktopBackgroundController* controller_;
157 PrefService* local_state_;
158 scoped_ptr<base::CommandLine> wallpaper_manager_command_line_;
160 // Directory created by CreateCmdlineWallpapers () to store default
161 // wallpaper images.
162 scoped_ptr<base::ScopedTempDir> wallpaper_dir_;
164 private:
165 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerBrowserTest);
168 // Tests that the appropriate custom wallpaper (large vs. small) is loaded
169 // depending on the desktop resolution.
170 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
171 LoadCustomLargeWallpaperForLargeExternalScreen) {
172 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
173 LogIn(kTestUser1, kTestUser1Hash);
174 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
175 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
176 wallpaper::kSmallWallpaperSubDir, kTestUser1Hash, id);
177 base::FilePath large_wallpaper_path = GetCustomWallpaperPath(
178 wallpaper::kLargeWallpaperSubDir, kTestUser1Hash, id);
180 // Saves the small/large resolution wallpapers to small/large custom
181 // wallpaper paths.
182 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
183 small_wallpaper_path,
184 kSmallWallpaperWidth,
185 kSmallWallpaperHeight,
186 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor));
187 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
188 large_wallpaper_path,
189 kLargeWallpaperWidth,
190 kLargeWallpaperHeight,
191 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
193 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
194 // Saves wallpaper info to local state for user |kTestUser1|.
195 WallpaperInfo info = {relative_path, WALLPAPER_LAYOUT_CENTER_CROPPED,
196 user_manager::User::CUSTOMIZED,
197 base::Time::Now().LocalMidnight()};
198 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
200 // Set the wallpaper for |kTestUser1|.
201 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
202 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
203 gfx::ImageSkia wallpaper = controller_->GetWallpaper();
205 // Display is initialized to 800x600. The small resolution custom wallpaper is
206 // expected.
207 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width());
208 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height());
210 // Hook up another 800x600 display. This shouldn't trigger a reload.
211 UpdateDisplay("800x600,800x600");
212 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
213 // The small resolution custom wallpaper is expected.
214 EXPECT_EQ(kSmallWallpaperWidth, wallpaper.width());
215 EXPECT_EQ(kSmallWallpaperHeight, wallpaper.height());
217 // Detach the secondary display.
218 UpdateDisplay("800x600");
219 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
220 // be loaded.
221 UpdateDisplay("800x600,2000x2000");
222 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
223 wallpaper = controller_->GetWallpaper();
225 // The large resolution custom wallpaper is expected.
226 EXPECT_EQ(kLargeWallpaperWidth, wallpaper.width());
227 EXPECT_EQ(kLargeWallpaperHeight, wallpaper.height());
229 // Detach the secondary display.
230 UpdateDisplay("800x600");
231 // Hook up the 2000x2000 display again. The large resolution default wallpaper
232 // should persist. Test for crbug/165788.
233 UpdateDisplay("800x600,2000x2000");
234 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
235 wallpaper = controller_->GetWallpaper();
237 // The large resolution custom wallpaper is expected.
238 EXPECT_EQ(kLargeWallpaperWidth, wallpaper.width());
239 EXPECT_EQ(kLargeWallpaperHeight, wallpaper.height());
242 // If chrome tries to reload the same wallpaper twice, the latter request should
243 // be prevented. Otherwise, there are some strange animation issues as
244 // described in crbug.com/158383.
245 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
246 PreventReloadingSameWallpaper) {
247 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
248 // New user log in, a default wallpaper is loaded.
249 LogIn(kTestUser1, kTestUser1Hash);
250 EXPECT_EQ(1, LoadedWallpapers());
251 // Loads the same wallpaper before the initial one finished. It should be
252 // prevented.
253 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
254 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
255 EXPECT_EQ(1, LoadedWallpapers());
256 // Loads the same wallpaper after the initial one finished. It should be
257 // prevented.
258 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
259 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
260 EXPECT_EQ(1, LoadedWallpapers());
261 ClearDisposableWallpaperCache();
263 // Change wallpaper to a custom wallpaper.
264 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
265 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
266 wallpaper::kSmallWallpaperSubDir, kTestUser1Hash, id);
267 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
268 small_wallpaper_path,
269 kSmallWallpaperWidth,
270 kSmallWallpaperHeight,
271 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor));
273 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
274 // Saves wallpaper info to local state for user |kTestUser1|.
275 WallpaperInfo info = {relative_path, WALLPAPER_LAYOUT_CENTER_CROPPED,
276 user_manager::User::CUSTOMIZED,
277 base::Time::Now().LocalMidnight()};
278 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
280 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
281 WaitAsyncWallpaperLoadStarted();
282 EXPECT_EQ(2, LoadedWallpapers());
283 // Loads the same wallpaper before the initial one finished. It should be
284 // prevented.
285 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
286 WaitAsyncWallpaperLoadStarted();
287 EXPECT_EQ(2, LoadedWallpapers());
288 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
289 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
290 EXPECT_EQ(2, LoadedWallpapers());
293 // Some users have old user profiles which may have legacy wallpapers. And these
294 // lagacy wallpapers should migrate to new wallpaper picker version seamlessly.
295 // This tests make sure we compatible with migrated old wallpapers.
296 // crosbug.com/38429
297 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
298 PRE_UseMigratedWallpaperInfo) {
299 // New user log in, a default wallpaper is loaded.
300 LogIn(kTestUser1, kTestUser1Hash);
301 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
302 // wallpaper info directly to simulate the wallpaper migration. See
303 // crosbug.com/38429 for details about why we modify wallpaper info this way.
304 WallpaperInfo info = {"123", WALLPAPER_LAYOUT_CENTER_CROPPED,
305 user_manager::User::DEFAULT,
306 base::Time::Now().LocalMidnight()};
307 base::FilePath user_data_dir;
308 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
309 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
310 user_data_dir.Append("123"),
311 wallpaper_manager_test_utils::kWallpaperSize,
312 wallpaper_manager_test_utils::kWallpaperSize,
313 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
314 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
317 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
318 UseMigratedWallpaperInfo) {
319 LogIn(kTestUser1, kTestUser1Hash);
320 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
321 // This test should finish normally. If timeout, it is probably because
322 // migrated wallpaper is somehow not loaded. Bad things can happen if
323 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
326 // Some users have old user profiles which may never get a chance to migrate.
327 // This tests make sure we compatible with these profiles.
328 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
329 PRE_UsePreMigrationWallpaperInfo) {
330 // New user log in, a default wallpaper is loaded.
331 LogIn(kTestUser1, kTestUser1Hash);
332 // Old wallpaper migration code doesn't exist in codebase anymore. So if
333 // user's profile is not migrated, it is the same as no wallpaper info. To
334 // simulate this, we remove user's wallpaper info here.
335 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1);
338 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
339 UsePreMigrationWallpaperInfo) {
340 LogIn(kTestUser1, kTestUser1Hash);
341 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
342 // This test should finish normally. If timeout, it is probably because chrome
343 // can not handle pre migrated user profile (M21 profile or older).
346 // Test for http://crbug.com/265689. When hooked up a large external monitor,
347 // the default large resolution wallpaper should load.
348 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
349 HotPlugInScreenAtGAIALoginScreen) {
350 UpdateDisplay("800x600");
351 // Set initial wallpaper to the default wallpaper.
352 WallpaperManager::Get()->SetDefaultWallpaperNow(chromeos::login::kStubUser);
353 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
355 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
356 // be loaded.
357 UpdateDisplay("800x600,2000x2000");
358 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
361 class WallpaperManagerBrowserTestNoAnimation
362 : public WallpaperManagerBrowserTest {
363 public:
364 void SetUpCommandLine(base::CommandLine* command_line) override {
365 command_line->AppendSwitch(switches::kLoginManager);
366 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
367 command_line->AppendSwitch(chromeos::switches::kDisableLoginAnimations);
368 command_line->AppendSwitch(chromeos::switches::kDisableBootAnimation);
372 // Same test as WallpaperManagerBrowserTest.UseMigratedWallpaperInfo. But
373 // disabled boot and login animation.
374 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation,
375 PRE_UseMigratedWallpaperInfo) {
376 // New user log in, a default wallpaper is loaded.
377 LogIn(kTestUser1, kTestUser1Hash);
378 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
379 // wallpaper info directly to simulate the wallpaper migration. See
380 // crosbug.com/38429 for details about why we modify wallpaper info this way.
381 WallpaperInfo info = {"123", WALLPAPER_LAYOUT_CENTER_CROPPED,
382 user_manager::User::DEFAULT,
383 base::Time::Now().LocalMidnight()};
384 base::FilePath user_data_dir;
385 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
386 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
387 user_data_dir.Append("123"),
388 wallpaper_manager_test_utils::kWallpaperSize,
389 wallpaper_manager_test_utils::kWallpaperSize,
390 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
391 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
394 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation,
395 UseMigratedWallpaperInfo) {
396 LogIn(kTestUser1, kTestUser1Hash);
397 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
398 // This test should finish normally. If timeout, it is probably because
399 // migrated wallpaper is somehow not loaded. Bad things can happen if
400 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
403 // Same test as WallpaperManagerBrowserTest.UsePreMigrationWallpaperInfo. But
404 // disabled boot and login animation.
405 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation,
406 PRE_UsePreMigrationWallpaperInfo) {
407 // New user log in, a default wallpaper is loaded.
408 LogIn(kTestUser1, kTestUser1Hash);
409 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
410 // Old wallpaper migration code doesn't exist in codebase anymore. So if
411 // user's profile is not migrated, it is the same as no wallpaper info. To
412 // simulate this, we remove user's wallpaper info here.
413 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1);
416 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation,
417 UsePreMigrationWallpaperInfo) {
418 LogIn(kTestUser1, kTestUser1Hash);
419 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
420 // This test should finish normally. If timeout, it is probably because chrome
421 // can not handle pre migrated user profile (M21 profile or older).
424 class WallpaperManagerBrowserTestCrashRestore
425 : public WallpaperManagerBrowserTest {
426 public:
427 void SetUpCommandLine(base::CommandLine* command_line) override {
428 command_line->AppendSwitch(chromeos::switches::kDisableLoginAnimations);
429 command_line->AppendSwitch(chromeos::switches::kDisableBootAnimation);
430 command_line->AppendSwitchASCII(switches::kLoginUser, kTestUser1);
431 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
435 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCrashRestore,
436 PRE_RestoreWallpaper) {
437 LogIn(kTestUser1, kTestUser1Hash);
438 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
441 // Test for crbug.com/270278. It simulates a browser crash and verifies if user
442 // wallpaper is loaded.
443 // Fails on the MSAN bots. See http://crbug.com/444477
444 #if defined(MEMORY_SANITIZER)
445 #define MAYBE_RestoreWallpaper DISABLED_RestoreWallpaper
446 #else
447 #define MAYBE_RestoreWallpaper RestoreWallpaper
448 #endif
449 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCrashRestore,
450 MAYBE_RestoreWallpaper) {
451 EXPECT_EQ(1, LoadedWallpapers());
454 class WallpaperManagerBrowserTestCacheUpdate
455 : public WallpaperManagerBrowserTest {
456 public:
457 void SetUpCommandLine(base::CommandLine* command_line) override {
458 command_line->AppendSwitchASCII(switches::kLoginUser, kTestUser1);
459 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
461 protected:
462 // Creates a test image of size 1x1.
463 gfx::ImageSkia CreateTestImage(SkColor color) {
464 return wallpaper_manager_test_utils::CreateTestImage(1, 1, color);
468 // Sets kTestUser1's wallpaper to a custom wallpaper.
469 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCacheUpdate,
470 PRE_VerifyWallpaperCache) {
471 // Add kTestUser1 to user list. kTestUser1 is the default login profile.
472 LogIn(kTestUser1, kTestUser1Hash);
474 std::string id = base::Int64ToString(base::Time::Now().ToInternalValue());
475 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
476 base::FilePath small_wallpaper_path = GetCustomWallpaperPath(
477 wallpaper::kSmallWallpaperSubDir, kTestUser1Hash, id);
478 base::FilePath large_wallpaper_path = GetCustomWallpaperPath(
479 wallpaper::kLargeWallpaperSubDir, kTestUser1Hash, id);
481 // Saves the small/large resolution wallpapers to small/large custom
482 // wallpaper paths.
483 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
484 small_wallpaper_path,
485 kSmallWallpaperWidth,
486 kSmallWallpaperHeight,
487 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor));
488 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
489 large_wallpaper_path,
490 kLargeWallpaperWidth,
491 kLargeWallpaperHeight,
492 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
494 std::string relative_path = base::FilePath(kTestUser1Hash).Append(id).value();
495 // Saves wallpaper info to local state for user |kTestUser1|.
496 WallpaperInfo info = {relative_path, WALLPAPER_LAYOUT_CENTER_CROPPED,
497 user_manager::User::CUSTOMIZED,
498 base::Time::Now().LocalMidnight()};
499 wallpaper_manager->SetUserWallpaperInfo(kTestUser1, info, true);
500 wallpaper_manager->SetUserWallpaperNow(kTestUser1);
501 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
502 scoped_ptr<WallpaperManager::TestApi> test_api;
503 test_api.reset(new WallpaperManager::TestApi(wallpaper_manager));
504 // Verify SetUserWallpaperNow updates wallpaper cache.
505 gfx::ImageSkia cached_wallpaper;
506 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
507 base::FilePath path;
508 EXPECT_TRUE(test_api->GetPathFromCache(kTestUser1, &path));
509 EXPECT_FALSE(path.empty());
512 // Tests for crbug.com/339576. Wallpaper cache should be updated in
513 // multi-profile mode when user:
514 // 1. chooses an online wallpaper from wallpaper
515 // picker (calls SetWallpaperFromImageSkia);
516 // 2. chooses a custom wallpaper from wallpaper
517 // picker (calls SetCustomWallpaper);
518 // 3. reverts to a default wallpaper.
519 // Also, when user login at multi-profile mode, previous logged in users'
520 // wallpaper cache should not be deleted.
521 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCacheUpdate,
522 VerifyWallpaperCache) {
523 WallpaperManager* wallpaper_manager = WallpaperManager::Get();
525 // Force load initial wallpaper
526 // (simulate DesktopBackgroundController::UpdateDisplay()).
527 wallpaper_manager->UpdateWallpaper(true);
528 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
529 scoped_ptr<WallpaperManager::TestApi> test_api;
530 test_api.reset(new WallpaperManager::TestApi(wallpaper_manager));
531 gfx::ImageSkia cached_wallpaper;
532 // Previous custom wallpaper should be cached after user login.
533 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
534 base::FilePath original_path;
535 EXPECT_TRUE(test_api->GetPathFromCache(kTestUser1, &original_path));
536 EXPECT_FALSE(original_path.empty());
538 LogIn(kTestUser2, kTestUser2Hash);
539 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
540 // Login another user should not delete logged in user's wallpaper cache.
541 // Note active user is still kTestUser1.
542 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
543 base::FilePath path;
544 EXPECT_TRUE(test_api->GetPathFromCache(kTestUser1, &path));
545 EXPECT_EQ(original_path, path);
547 gfx::ImageSkia red_wallpaper = CreateTestImage(SK_ColorRED);
548 wallpaper_manager->SetWallpaperFromImageSkia(kTestUser1,
549 red_wallpaper,
550 WALLPAPER_LAYOUT_CENTER,
551 true);
552 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
553 // SetWallpaperFromImageSkia should update wallpaper cache when multi-profile
554 // is turned on.
555 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
556 EXPECT_TRUE(test_api->GetPathFromCache(kTestUser1, &path));
557 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(red_wallpaper));
559 gfx::ImageSkia green_wallpaper = CreateTestImage(SK_ColorGREEN);
560 wallpaper_manager->SetCustomWallpaper(kTestUser1,
561 kTestUser1Hash,
562 "dummy", // dummy file name
563 WALLPAPER_LAYOUT_CENTER,
564 user_manager::User::CUSTOMIZED,
565 green_wallpaper,
566 true);
567 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
568 // SetCustomWallpaper should also update wallpaper cache when multi-profile is
569 // turned on.
570 EXPECT_TRUE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
571 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(green_wallpaper));
572 EXPECT_TRUE(test_api->GetPathFromCache(kTestUser1, &path));
573 EXPECT_NE(original_path, path);
575 wallpaper_manager->SetDefaultWallpaperNow(kTestUser1);
576 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
577 // SetDefaultWallpaper should invalidate the user's wallpaper cache.
578 EXPECT_FALSE(test_api->GetWallpaperFromCache(kTestUser1, &cached_wallpaper));
581 // ----------------------------------------------------------------------
582 // Test default wallpapers.
584 class TestObserver : public WallpaperManager::Observer {
585 public:
586 explicit TestObserver(WallpaperManager* wallpaper_manager)
587 : update_wallpaper_count_(0), wallpaper_manager_(wallpaper_manager) {
588 DCHECK(wallpaper_manager_);
589 wallpaper_manager_->AddObserver(this);
592 ~TestObserver() override { wallpaper_manager_->RemoveObserver(this); }
594 void OnWallpaperAnimationFinished(const std::string&) override {}
596 void OnUpdateWallpaperForTesting() override { ++update_wallpaper_count_; }
598 int GetUpdateWallpaperCountAndReset() {
599 const size_t old = update_wallpaper_count_;
600 update_wallpaper_count_ = 0;
601 return old;
604 private:
605 int update_wallpaper_count_;
606 WallpaperManager* wallpaper_manager_;
608 DISALLOW_COPY_AND_ASSIGN(TestObserver);
611 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, DisplayChange) {
612 // TODO(derat|oshima|bshe): Host windows can't be resized on Win8.
613 if (!ash::test::AshTestHelper::SupportsHostWindowResize())
614 return;
616 TestObserver observer(WallpaperManager::Get());
618 // Set the wallpaper to ensure that UpdateWallpaper() will be called when the
619 // display configuration changes.
620 gfx::ImageSkia image = wallpaper_manager_test_utils::CreateTestImage(
621 640, 480, wallpaper_manager_test_utils::kCustomWallpaperColor);
622 controller_->SetWallpaperImage(image, WALLPAPER_LAYOUT_STRETCH);
624 // Small wallpaper images should be used for configurations less than or
625 // equal to kSmallWallpaperMaxWidth by kSmallWallpaperMaxHeight, even if
626 // multiple displays are connected.
627 UpdateDisplay("800x600");
628 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
629 base::RunLoop().RunUntilIdle();
630 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
631 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
632 WallpaperManager::Get()->GetAppropriateResolution());
633 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
635 UpdateDisplay("800x600,800x600");
636 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
637 base::RunLoop().RunUntilIdle();
638 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
639 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
640 WallpaperManager::Get()->GetAppropriateResolution());
641 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
643 UpdateDisplay("1366x800");
644 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
645 base::RunLoop().RunUntilIdle();
646 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
647 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
648 WallpaperManager::Get()->GetAppropriateResolution());
649 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
651 // At larger sizes, large wallpapers should be used.
652 UpdateDisplay("1367x800");
653 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
654 base::RunLoop().RunUntilIdle();
655 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
656 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
657 WallpaperManager::Get()->GetAppropriateResolution());
658 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
660 UpdateDisplay("1367x801");
661 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
662 base::RunLoop().RunUntilIdle();
663 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
664 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
665 WallpaperManager::Get()->GetAppropriateResolution());
666 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
668 UpdateDisplay("2560x1700");
669 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
670 base::RunLoop().RunUntilIdle();
671 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
672 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
673 WallpaperManager::Get()->GetAppropriateResolution());
674 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
676 // Rotated smaller screen may use larger image.
677 UpdateDisplay("800x600/r");
678 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
679 base::RunLoop().RunUntilIdle();
680 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
681 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
682 WallpaperManager::Get()->GetAppropriateResolution());
683 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
685 UpdateDisplay("800x600/r,800x600");
686 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
687 base::RunLoop().RunUntilIdle();
688 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
689 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL,
690 WallpaperManager::Get()->GetAppropriateResolution());
691 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
692 UpdateDisplay("1366x800/r");
693 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
694 base::RunLoop().RunUntilIdle();
695 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
696 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE,
697 WallpaperManager::Get()->GetAppropriateResolution());
698 EXPECT_EQ(1, observer.GetUpdateWallpaperCountAndReset());
700 // Max display size didn't change.
701 UpdateDisplay("900x800/r,400x1366");
702 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
703 base::RunLoop().RunUntilIdle();
704 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
705 EXPECT_EQ(0, observer.GetUpdateWallpaperCountAndReset());
708 // Test that WallpaperManager loads the appropriate wallpaper
709 // images as specified via command-line flags in various situations.
710 // Splitting these into separate tests avoids needing to run animations.
711 // TODO(derat): Combine these into a single test
712 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, SmallDefaultWallpaper) {
713 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
714 return;
716 CreateCmdlineWallpapers();
718 // At 800x600, the small wallpaper should be loaded.
719 UpdateDisplay("800x600");
720 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
721 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
722 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
723 controller_->GetWallpaper(),
724 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor));
727 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, LargeDefaultWallpaper) {
728 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
729 return;
731 CreateCmdlineWallpapers();
732 UpdateDisplay("1600x1200");
733 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
734 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
735 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
736 controller_->GetWallpaper(),
737 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
740 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
741 LargeDefaultWallpaperWhenRotated) {
742 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
743 return;
744 CreateCmdlineWallpapers();
746 UpdateDisplay("1200x800/r");
747 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
748 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
749 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
750 controller_->GetWallpaper(),
751 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor));
754 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, SmallGuestWallpaper) {
755 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
756 return;
757 CreateCmdlineWallpapers();
758 user_manager::UserManager::Get()->UserLoggedIn(
759 chromeos::login::kGuestUserName, chromeos::login::kGuestUserName, false);
760 UpdateDisplay("800x600");
761 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
762 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
763 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
764 controller_->GetWallpaper(),
765 wallpaper_manager_test_utils::kSmallGuestWallpaperColor));
768 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, LargeGuestWallpaper) {
769 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
770 return;
772 CreateCmdlineWallpapers();
773 user_manager::UserManager::Get()->UserLoggedIn(
774 chromeos::login::kGuestUserName, chromeos::login::kGuestUserName, false);
775 UpdateDisplay("1600x1200");
776 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
777 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
778 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
779 controller_->GetWallpaper(),
780 wallpaper_manager_test_utils::kLargeGuestWallpaperColor));
783 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, SmallChildWallpaper) {
784 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
785 return;
786 CreateCmdlineWallpapers();
787 LogInAsChild(kTestUser1, kTestUser1Hash);
788 UpdateDisplay("800x600");
789 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
790 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
791 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
792 controller_->GetWallpaper(),
793 wallpaper_manager_test_utils::kSmallChildWallpaperColor));
796 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest, LargeChildWallpaper) {
797 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
798 return;
800 CreateCmdlineWallpapers();
801 LogInAsChild(kTestUser1, kTestUser1Hash);
802 UpdateDisplay("1600x1200");
803 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
804 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
805 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
806 controller_->GetWallpaper(),
807 wallpaper_manager_test_utils::kLargeChildWallpaperColor));
810 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest,
811 SwitchBetweenDefaultAndCustom) {
812 // Start loading the default wallpaper.
813 UpdateDisplay("640x480");
814 CreateCmdlineWallpapers();
815 user_manager::UserManager::Get()->UserLoggedIn(
816 chromeos::login::kStubUser, "test_hash", false);
818 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
820 // Custom wallpaper should be applied immediately, canceling the default
821 // wallpaper load task.
822 gfx::ImageSkia image = wallpaper_manager_test_utils::CreateTestImage(
823 640, 480, wallpaper_manager_test_utils::kCustomWallpaperColor);
824 WallpaperManager::Get()->SetCustomWallpaper(chromeos::login::kStubUser,
825 "test_hash",
826 "test-nofile.jpeg",
827 WALLPAPER_LAYOUT_STRETCH,
828 user_manager::User::CUSTOMIZED,
829 image,
830 true);
831 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
833 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
834 controller_->GetWallpaper(),
835 wallpaper_manager_test_utils::kCustomWallpaperColor));
837 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
838 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
840 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
841 controller_->GetWallpaper(),
842 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor));
845 } // namespace chromeos