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
;
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";
64 class WallpaperManagerBrowserTest
: public InProcessBrowserTest
{
66 WallpaperManagerBrowserTest () : controller_(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().UpdateDisplay(display_specs
);
93 void WaitAsyncWallpaperLoadStarted() {
94 base::RunLoop().RunUntilIdle();
99 // Return custom wallpaper path. Create directory if not exist.
100 base::FilePath
GetCustomWallpaperPath(const char* sub_dir
,
101 const std::string
& username_hash
,
102 const std::string
& id
) {
103 base::FilePath wallpaper_path
=
104 WallpaperManager::Get()->GetCustomWallpaperPath(sub_dir
,
107 if (!base::DirectoryExists(wallpaper_path
.DirName()))
108 base::CreateDirectory(wallpaper_path
.DirName());
110 return wallpaper_path
;
113 // Logs in |username|.
114 void LogIn(const std::string
& username
, const std::string
& username_hash
) {
115 user_manager::UserManager::Get()->UserLoggedIn(
116 username
, username_hash
, false);
117 WaitAsyncWallpaperLoadStarted();
120 // Logs in |username| and sets it as child account.
122 const std::string
& username
, const std::string
& username_hash
) {
123 user_manager::UserManager::Get()->UserLoggedIn(
124 username
, username_hash
, false);
125 user_manager::User
* user
=
126 user_manager::UserManager::Get()->FindUserAndModify(username
);
127 user_manager::UserManager::Get()->ChangeUserChildStatus(
128 user
, true /* is_child */);
131 int LoadedWallpapers() {
132 return WallpaperManager::Get()->loaded_wallpapers_for_test();
135 void CacheUserWallpaper(const std::string
& user
) {
136 WallpaperManager::Get()->CacheUserWallpaper(user
);
139 void ClearDisposableWallpaperCache() {
140 WallpaperManager::Get()->ClearDisposableWallpaperCache();
143 // Initializes default wallpaper paths "*default_*file" and writes JPEG
144 // wallpaper images to them.
145 // Only needs to be called (once) by tests that want to test loading of
146 // default wallpapers.
147 void CreateCmdlineWallpapers() {
148 wallpaper_dir_
.reset(new base::ScopedTempDir
);
149 ASSERT_TRUE(wallpaper_dir_
->CreateUniqueTempDir());
150 wallpaper_manager_test_utils::CreateCmdlineWallpapers(
151 *wallpaper_dir_
, &wallpaper_manager_command_line_
);
154 ash::DesktopBackgroundController
* controller_
;
155 PrefService
* local_state_
;
156 scoped_ptr
<base::CommandLine
> wallpaper_manager_command_line_
;
158 // Directory created by CreateCmdlineWallpapers () to store default
160 scoped_ptr
<base::ScopedTempDir
> wallpaper_dir_
;
163 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerBrowserTest
);
166 // Tests that the appropriate custom wallpaper (large vs. small) is loaded
167 // depending on the desktop resolution.
168 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
169 LoadCustomLargeWallpaperForLargeExternalScreen
) {
170 WallpaperManager
* wallpaper_manager
= WallpaperManager::Get();
171 LogIn(kTestUser1
, kTestUser1Hash
);
172 std::string id
= base::Int64ToString(base::Time::Now().ToInternalValue());
173 base::FilePath small_wallpaper_path
= GetCustomWallpaperPath(
174 wallpaper::kSmallWallpaperSubDir
, kTestUser1Hash
, id
);
175 base::FilePath large_wallpaper_path
= GetCustomWallpaperPath(
176 wallpaper::kLargeWallpaperSubDir
, kTestUser1Hash
, id
);
178 // Saves the small/large resolution wallpapers to small/large custom
180 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
181 small_wallpaper_path
,
182 kSmallWallpaperWidth
,
183 kSmallWallpaperHeight
,
184 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor
));
185 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
186 large_wallpaper_path
,
187 kLargeWallpaperWidth
,
188 kLargeWallpaperHeight
,
189 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
191 std::string relative_path
= base::FilePath(kTestUser1Hash
).Append(id
).value();
192 // Saves wallpaper info to local state for user |kTestUser1|.
193 WallpaperInfo info
= {relative_path
, WALLPAPER_LAYOUT_CENTER_CROPPED
,
194 user_manager::User::CUSTOMIZED
,
195 base::Time::Now().LocalMidnight()};
196 wallpaper_manager
->SetUserWallpaperInfo(kTestUser1
, info
, true);
198 // Set the wallpaper for |kTestUser1|.
199 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
200 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
201 gfx::ImageSkia wallpaper
= controller_
->GetWallpaper();
203 // Display is initialized to 800x600. The small resolution custom wallpaper is
205 EXPECT_EQ(kSmallWallpaperWidth
, wallpaper
.width());
206 EXPECT_EQ(kSmallWallpaperHeight
, wallpaper
.height());
208 // Hook up another 800x600 display. This shouldn't trigger a reload.
209 UpdateDisplay("800x600,800x600");
210 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
211 // The small resolution custom wallpaper is expected.
212 EXPECT_EQ(kSmallWallpaperWidth
, wallpaper
.width());
213 EXPECT_EQ(kSmallWallpaperHeight
, wallpaper
.height());
215 // Detach the secondary display.
216 UpdateDisplay("800x600");
217 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
219 UpdateDisplay("800x600,2000x2000");
220 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
221 wallpaper
= controller_
->GetWallpaper();
223 // The large resolution custom wallpaper is expected.
224 EXPECT_EQ(kLargeWallpaperWidth
, wallpaper
.width());
225 EXPECT_EQ(kLargeWallpaperHeight
, wallpaper
.height());
227 // Detach the secondary display.
228 UpdateDisplay("800x600");
229 // Hook up the 2000x2000 display again. The large resolution default wallpaper
230 // should persist. Test for crbug/165788.
231 UpdateDisplay("800x600,2000x2000");
232 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
233 wallpaper
= controller_
->GetWallpaper();
235 // The large resolution custom wallpaper is expected.
236 EXPECT_EQ(kLargeWallpaperWidth
, wallpaper
.width());
237 EXPECT_EQ(kLargeWallpaperHeight
, wallpaper
.height());
240 // If chrome tries to reload the same wallpaper twice, the latter request should
241 // be prevented. Otherwise, there are some strange animation issues as
242 // described in crbug.com/158383.
243 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
244 PreventReloadingSameWallpaper
) {
245 WallpaperManager
* wallpaper_manager
= WallpaperManager::Get();
246 // New user log in, a default wallpaper is loaded.
247 LogIn(kTestUser1
, kTestUser1Hash
);
248 EXPECT_EQ(1, LoadedWallpapers());
249 // Loads the same wallpaper before the initial one finished. It should be
251 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
252 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
253 EXPECT_EQ(1, LoadedWallpapers());
254 // Loads the same wallpaper after the initial one finished. It should be
256 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
257 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
258 EXPECT_EQ(1, LoadedWallpapers());
259 ClearDisposableWallpaperCache();
261 // Change wallpaper to a custom wallpaper.
262 std::string id
= base::Int64ToString(base::Time::Now().ToInternalValue());
263 base::FilePath small_wallpaper_path
= GetCustomWallpaperPath(
264 wallpaper::kSmallWallpaperSubDir
, kTestUser1Hash
, id
);
265 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
266 small_wallpaper_path
,
267 kSmallWallpaperWidth
,
268 kSmallWallpaperHeight
,
269 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor
));
271 std::string relative_path
= base::FilePath(kTestUser1Hash
).Append(id
).value();
272 // Saves wallpaper info to local state for user |kTestUser1|.
273 WallpaperInfo info
= {relative_path
, WALLPAPER_LAYOUT_CENTER_CROPPED
,
274 user_manager::User::CUSTOMIZED
,
275 base::Time::Now().LocalMidnight()};
276 wallpaper_manager
->SetUserWallpaperInfo(kTestUser1
, info
, true);
278 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
279 WaitAsyncWallpaperLoadStarted();
280 EXPECT_EQ(2, LoadedWallpapers());
281 // Loads the same wallpaper before the initial one finished. It should be
283 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
284 WaitAsyncWallpaperLoadStarted();
285 EXPECT_EQ(2, LoadedWallpapers());
286 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
287 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
288 EXPECT_EQ(2, LoadedWallpapers());
291 // Some users have old user profiles which may have legacy wallpapers. And these
292 // lagacy wallpapers should migrate to new wallpaper picker version seamlessly.
293 // This tests make sure we compatible with migrated old wallpapers.
295 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
296 PRE_UseMigratedWallpaperInfo
) {
297 // New user log in, a default wallpaper is loaded.
298 LogIn(kTestUser1
, kTestUser1Hash
);
299 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
300 // wallpaper info directly to simulate the wallpaper migration. See
301 // crosbug.com/38429 for details about why we modify wallpaper info this way.
302 WallpaperInfo info
= {"123", WALLPAPER_LAYOUT_CENTER_CROPPED
,
303 user_manager::User::DEFAULT
,
304 base::Time::Now().LocalMidnight()};
305 base::FilePath user_data_dir
;
306 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
));
307 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
308 user_data_dir
.Append("123"),
309 wallpaper_manager_test_utils::kWallpaperSize
,
310 wallpaper_manager_test_utils::kWallpaperSize
,
311 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
312 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1
, info
, true);
315 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
316 UseMigratedWallpaperInfo
) {
317 LogIn(kTestUser1
, kTestUser1Hash
);
318 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
319 // This test should finish normally. If timeout, it is probably because
320 // migrated wallpaper is somehow not loaded. Bad things can happen if
321 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
324 // Some users have old user profiles which may never get a chance to migrate.
325 // This tests make sure we compatible with these profiles.
326 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
327 PRE_UsePreMigrationWallpaperInfo
) {
328 // New user log in, a default wallpaper is loaded.
329 LogIn(kTestUser1
, kTestUser1Hash
);
330 // Old wallpaper migration code doesn't exist in codebase anymore. So if
331 // user's profile is not migrated, it is the same as no wallpaper info. To
332 // simulate this, we remove user's wallpaper info here.
333 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1
);
336 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
337 UsePreMigrationWallpaperInfo
) {
338 LogIn(kTestUser1
, kTestUser1Hash
);
339 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
340 // This test should finish normally. If timeout, it is probably because chrome
341 // can not handle pre migrated user profile (M21 profile or older).
344 // Test for http://crbug.com/265689. When hooked up a large external monitor,
345 // the default large resolution wallpaper should load.
346 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
347 HotPlugInScreenAtGAIALoginScreen
) {
348 UpdateDisplay("800x600");
349 // Set initial wallpaper to the default wallpaper.
350 WallpaperManager::Get()->SetDefaultWallpaperNow(chromeos::login::kStubUser
);
351 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
353 // Hook up a 2000x2000 display. The large resolution custom wallpaper should
355 UpdateDisplay("800x600,2000x2000");
356 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
359 class WallpaperManagerBrowserTestNoAnimation
360 : public WallpaperManagerBrowserTest
{
362 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
363 command_line
->AppendSwitch(switches::kLoginManager
);
364 command_line
->AppendSwitchASCII(switches::kLoginProfile
, "user");
365 command_line
->AppendSwitch(chromeos::switches::kDisableLoginAnimations
);
366 command_line
->AppendSwitch(chromeos::switches::kDisableBootAnimation
);
370 // Same test as WallpaperManagerBrowserTest.UseMigratedWallpaperInfo. But
371 // disabled boot and login animation.
372 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation
,
373 PRE_UseMigratedWallpaperInfo
) {
374 // New user log in, a default wallpaper is loaded.
375 LogIn(kTestUser1
, kTestUser1Hash
);
376 // Old wallpaper migration code doesn't exist in codebase anymore. Modify user
377 // wallpaper info directly to simulate the wallpaper migration. See
378 // crosbug.com/38429 for details about why we modify wallpaper info this way.
379 WallpaperInfo info
= {"123", WALLPAPER_LAYOUT_CENTER_CROPPED
,
380 user_manager::User::DEFAULT
,
381 base::Time::Now().LocalMidnight()};
382 base::FilePath user_data_dir
;
383 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
));
384 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
385 user_data_dir
.Append("123"),
386 wallpaper_manager_test_utils::kWallpaperSize
,
387 wallpaper_manager_test_utils::kWallpaperSize
,
388 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
389 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1
, info
, true);
392 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation
,
393 UseMigratedWallpaperInfo
) {
394 LogIn(kTestUser1
, kTestUser1Hash
);
395 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
396 // This test should finish normally. If timeout, it is probably because
397 // migrated wallpaper is somehow not loaded. Bad things can happen if
398 // wallpaper is not loaded at login screen. One example is: crosbug.com/38429.
401 // Same test as WallpaperManagerBrowserTest.UsePreMigrationWallpaperInfo. But
402 // disabled boot and login animation.
403 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation
,
404 PRE_UsePreMigrationWallpaperInfo
) {
405 // New user log in, a default wallpaper is loaded.
406 LogIn(kTestUser1
, kTestUser1Hash
);
407 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
408 // Old wallpaper migration code doesn't exist in codebase anymore. So if
409 // user's profile is not migrated, it is the same as no wallpaper info. To
410 // simulate this, we remove user's wallpaper info here.
411 WallpaperManager::Get()->RemoveUserWallpaperInfo(kTestUser1
);
414 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestNoAnimation
,
415 UsePreMigrationWallpaperInfo
) {
416 LogIn(kTestUser1
, kTestUser1Hash
);
417 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
418 // This test should finish normally. If timeout, it is probably because chrome
419 // can not handle pre migrated user profile (M21 profile or older).
422 class WallpaperManagerBrowserTestCrashRestore
423 : public WallpaperManagerBrowserTest
{
425 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
426 command_line
->AppendSwitch(chromeos::switches::kDisableLoginAnimations
);
427 command_line
->AppendSwitch(chromeos::switches::kDisableBootAnimation
);
428 command_line
->AppendSwitchASCII(switches::kLoginUser
, kTestUser1
);
429 command_line
->AppendSwitchASCII(switches::kLoginProfile
, "user");
433 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCrashRestore
,
434 PRE_RestoreWallpaper
) {
435 LogIn(kTestUser1
, kTestUser1Hash
);
436 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
439 // Test for crbug.com/270278. It simulates a browser crash and verifies if user
440 // wallpaper is loaded.
441 // Fails on the MSAN bots. See http://crbug.com/444477
442 #if defined(MEMORY_SANITIZER)
443 #define MAYBE_RestoreWallpaper DISABLED_RestoreWallpaper
445 #define MAYBE_RestoreWallpaper RestoreWallpaper
447 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCrashRestore
,
448 MAYBE_RestoreWallpaper
) {
449 EXPECT_EQ(1, LoadedWallpapers());
452 class WallpaperManagerBrowserTestCacheUpdate
453 : public WallpaperManagerBrowserTest
{
455 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
456 command_line
->AppendSwitchASCII(switches::kLoginUser
, kTestUser1
);
457 command_line
->AppendSwitchASCII(switches::kLoginProfile
, "user");
460 // Creates a test image of size 1x1.
461 gfx::ImageSkia
CreateTestImage(SkColor color
) {
462 return wallpaper_manager_test_utils::CreateTestImage(1, 1, color
);
466 // Sets kTestUser1's wallpaper to a custom wallpaper.
467 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCacheUpdate
,
468 PRE_VerifyWallpaperCache
) {
469 // Add kTestUser1 to user list. kTestUser1 is the default login profile.
470 LogIn(kTestUser1
, kTestUser1Hash
);
472 std::string id
= base::Int64ToString(base::Time::Now().ToInternalValue());
473 WallpaperManager
* wallpaper_manager
= WallpaperManager::Get();
474 base::FilePath small_wallpaper_path
= GetCustomWallpaperPath(
475 wallpaper::kSmallWallpaperSubDir
, kTestUser1Hash
, id
);
476 base::FilePath large_wallpaper_path
= GetCustomWallpaperPath(
477 wallpaper::kLargeWallpaperSubDir
, kTestUser1Hash
, id
);
479 // Saves the small/large resolution wallpapers to small/large custom
481 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
482 small_wallpaper_path
,
483 kSmallWallpaperWidth
,
484 kSmallWallpaperHeight
,
485 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor
));
486 ASSERT_TRUE(wallpaper_manager_test_utils::WriteJPEGFile(
487 large_wallpaper_path
,
488 kLargeWallpaperWidth
,
489 kLargeWallpaperHeight
,
490 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
492 std::string relative_path
= base::FilePath(kTestUser1Hash
).Append(id
).value();
493 // Saves wallpaper info to local state for user |kTestUser1|.
494 WallpaperInfo info
= {relative_path
, WALLPAPER_LAYOUT_CENTER_CROPPED
,
495 user_manager::User::CUSTOMIZED
,
496 base::Time::Now().LocalMidnight()};
497 wallpaper_manager
->SetUserWallpaperInfo(kTestUser1
, info
, true);
498 wallpaper_manager
->SetUserWallpaperNow(kTestUser1
);
499 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
500 scoped_ptr
<WallpaperManager::TestApi
> test_api
;
501 test_api
.reset(new WallpaperManager::TestApi(wallpaper_manager
));
502 // Verify SetUserWallpaperNow updates wallpaper cache.
503 gfx::ImageSkia cached_wallpaper
;
504 EXPECT_TRUE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
506 EXPECT_TRUE(test_api
->GetPathFromCache(kTestUser1
, &path
));
507 EXPECT_FALSE(path
.empty());
510 // Tests for crbug.com/339576. Wallpaper cache should be updated in
511 // multi-profile mode when user:
512 // 1. chooses an online wallpaper from wallpaper
513 // picker (calls SetWallpaperFromImageSkia);
514 // 2. chooses a custom wallpaper from wallpaper
515 // picker (calls SetCustomWallpaper);
516 // 3. reverts to a default wallpaper.
517 // Also, when user login at multi-profile mode, previous logged in users'
518 // wallpaper cache should not be deleted.
519 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTestCacheUpdate
,
520 VerifyWallpaperCache
) {
521 WallpaperManager
* wallpaper_manager
= WallpaperManager::Get();
523 // Force load initial wallpaper
524 // (simulate DesktopBackgroundController::UpdateDisplay()).
525 wallpaper_manager
->UpdateWallpaper(true);
526 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
527 scoped_ptr
<WallpaperManager::TestApi
> test_api
;
528 test_api
.reset(new WallpaperManager::TestApi(wallpaper_manager
));
529 gfx::ImageSkia cached_wallpaper
;
530 // Previous custom wallpaper should be cached after user login.
531 EXPECT_TRUE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
532 base::FilePath original_path
;
533 EXPECT_TRUE(test_api
->GetPathFromCache(kTestUser1
, &original_path
));
534 EXPECT_FALSE(original_path
.empty());
536 LogIn(kTestUser2
, kTestUser2Hash
);
537 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
538 // Login another user should not delete logged in user's wallpaper cache.
539 // Note active user is still kTestUser1.
540 EXPECT_TRUE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
542 EXPECT_TRUE(test_api
->GetPathFromCache(kTestUser1
, &path
));
543 EXPECT_EQ(original_path
, path
);
545 gfx::ImageSkia red_wallpaper
= CreateTestImage(SK_ColorRED
);
546 wallpaper_manager
->SetWallpaperFromImageSkia(kTestUser1
,
548 WALLPAPER_LAYOUT_CENTER
,
550 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
551 // SetWallpaperFromImageSkia should update wallpaper cache when multi-profile
553 EXPECT_TRUE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
554 EXPECT_TRUE(test_api
->GetPathFromCache(kTestUser1
, &path
));
555 EXPECT_TRUE(cached_wallpaper
.BackedBySameObjectAs(red_wallpaper
));
557 gfx::ImageSkia green_wallpaper
= CreateTestImage(SK_ColorGREEN
);
558 wallpaper_manager
->SetCustomWallpaper(kTestUser1
,
560 "dummy", // dummy file name
561 WALLPAPER_LAYOUT_CENTER
,
562 user_manager::User::CUSTOMIZED
,
565 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
566 // SetCustomWallpaper should also update wallpaper cache when multi-profile is
568 EXPECT_TRUE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
569 EXPECT_TRUE(cached_wallpaper
.BackedBySameObjectAs(green_wallpaper
));
570 EXPECT_TRUE(test_api
->GetPathFromCache(kTestUser1
, &path
));
571 EXPECT_NE(original_path
, path
);
573 wallpaper_manager
->SetDefaultWallpaperNow(kTestUser1
);
574 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
575 // SetDefaultWallpaper should invalidate the user's wallpaper cache.
576 EXPECT_FALSE(test_api
->GetWallpaperFromCache(kTestUser1
, &cached_wallpaper
));
579 // ----------------------------------------------------------------------
580 // Test default wallpapers.
582 class TestObserver
: public WallpaperManager::Observer
{
584 explicit TestObserver(WallpaperManager
* wallpaper_manager
)
585 : update_wallpaper_count_(0), wallpaper_manager_(wallpaper_manager
) {
586 DCHECK(wallpaper_manager_
);
587 wallpaper_manager_
->AddObserver(this);
590 ~TestObserver() override
{ wallpaper_manager_
->RemoveObserver(this); }
592 void OnWallpaperAnimationFinished(const std::string
&) override
{}
594 void OnUpdateWallpaperForTesting() override
{ ++update_wallpaper_count_
; }
596 int GetUpdateWallpaperCountAndReset() {
597 const size_t old
= update_wallpaper_count_
;
598 update_wallpaper_count_
= 0;
603 int update_wallpaper_count_
;
604 WallpaperManager
* wallpaper_manager_
;
606 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
609 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, DisplayChange
) {
610 // TODO(derat|oshima|bshe): Host windows can't be resized on Win8.
611 if (!ash::test::AshTestHelper::SupportsHostWindowResize())
614 TestObserver
observer(WallpaperManager::Get());
616 // Set the wallpaper to ensure that UpdateWallpaper() will be called when the
617 // display configuration changes.
618 gfx::ImageSkia image
= wallpaper_manager_test_utils::CreateTestImage(
619 640, 480, wallpaper_manager_test_utils::kCustomWallpaperColor
);
620 controller_
->SetWallpaperImage(image
, WALLPAPER_LAYOUT_STRETCH
);
622 // Small wallpaper images should be used for configurations less than or
623 // equal to kSmallWallpaperMaxWidth by kSmallWallpaperMaxHeight, even if
624 // multiple displays are connected.
625 UpdateDisplay("800x600");
626 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
627 base::RunLoop().RunUntilIdle();
628 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
629 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL
,
630 WallpaperManager::Get()->GetAppropriateResolution());
631 EXPECT_EQ(0, observer
.GetUpdateWallpaperCountAndReset());
633 UpdateDisplay("800x600,800x600");
634 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
635 base::RunLoop().RunUntilIdle();
636 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
637 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL
,
638 WallpaperManager::Get()->GetAppropriateResolution());
639 EXPECT_EQ(0, observer
.GetUpdateWallpaperCountAndReset());
641 UpdateDisplay("1366x800");
642 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
643 base::RunLoop().RunUntilIdle();
644 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
645 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL
,
646 WallpaperManager::Get()->GetAppropriateResolution());
647 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
649 // At larger sizes, large wallpapers should be used.
650 UpdateDisplay("1367x800");
651 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
652 base::RunLoop().RunUntilIdle();
653 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
654 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE
,
655 WallpaperManager::Get()->GetAppropriateResolution());
656 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
658 UpdateDisplay("1367x801");
659 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
660 base::RunLoop().RunUntilIdle();
661 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
662 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE
,
663 WallpaperManager::Get()->GetAppropriateResolution());
664 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
666 UpdateDisplay("2560x1700");
667 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
668 base::RunLoop().RunUntilIdle();
669 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
670 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE
,
671 WallpaperManager::Get()->GetAppropriateResolution());
672 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
674 // Rotated smaller screen may use larger image.
675 UpdateDisplay("800x600/r");
676 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
677 base::RunLoop().RunUntilIdle();
678 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
679 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL
,
680 WallpaperManager::Get()->GetAppropriateResolution());
681 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
683 UpdateDisplay("800x600/r,800x600");
684 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
685 base::RunLoop().RunUntilIdle();
686 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
687 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_SMALL
,
688 WallpaperManager::Get()->GetAppropriateResolution());
689 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
690 UpdateDisplay("1366x800/r");
691 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
692 base::RunLoop().RunUntilIdle();
693 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
694 EXPECT_EQ(WallpaperManager::WALLPAPER_RESOLUTION_LARGE
,
695 WallpaperManager::Get()->GetAppropriateResolution());
696 EXPECT_EQ(1, observer
.GetUpdateWallpaperCountAndReset());
698 // Max display size didn't change.
699 UpdateDisplay("900x800/r,400x1366");
700 // Wait for asynchronous DisplayBackgroundController::UpdateDisplay() call.
701 base::RunLoop().RunUntilIdle();
702 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
703 EXPECT_EQ(0, observer
.GetUpdateWallpaperCountAndReset());
706 // Test that WallpaperManager loads the appropriate wallpaper
707 // images as specified via command-line flags in various situations.
708 // Splitting these into separate tests avoids needing to run animations.
709 // TODO(derat): Combine these into a single test
710 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, SmallDefaultWallpaper
) {
711 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
714 CreateCmdlineWallpapers();
716 // At 800x600, the small wallpaper should be loaded.
717 UpdateDisplay("800x600");
718 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
719 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
720 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
721 controller_
->GetWallpaper(),
722 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor
));
725 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, LargeDefaultWallpaper
) {
726 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
729 CreateCmdlineWallpapers();
730 UpdateDisplay("1600x1200");
731 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
732 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
733 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
734 controller_
->GetWallpaper(),
735 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
738 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
739 LargeDefaultWallpaperWhenRotated
) {
740 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
742 CreateCmdlineWallpapers();
744 UpdateDisplay("1200x800/r");
745 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
746 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
747 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
748 controller_
->GetWallpaper(),
749 wallpaper_manager_test_utils::kLargeDefaultWallpaperColor
));
752 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, SmallGuestWallpaper
) {
753 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
755 CreateCmdlineWallpapers();
756 user_manager::UserManager::Get()->UserLoggedIn(
757 chromeos::login::kGuestUserName
, chromeos::login::kGuestUserName
, false);
758 UpdateDisplay("800x600");
759 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
760 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
761 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
762 controller_
->GetWallpaper(),
763 wallpaper_manager_test_utils::kSmallGuestWallpaperColor
));
766 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, LargeGuestWallpaper
) {
767 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
770 CreateCmdlineWallpapers();
771 user_manager::UserManager::Get()->UserLoggedIn(
772 chromeos::login::kGuestUserName
, chromeos::login::kGuestUserName
, false);
773 UpdateDisplay("1600x1200");
774 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
775 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
776 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
777 controller_
->GetWallpaper(),
778 wallpaper_manager_test_utils::kLargeGuestWallpaperColor
));
781 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, SmallChildWallpaper
) {
782 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
784 CreateCmdlineWallpapers();
785 LogInAsChild(kTestUser1
, kTestUser1Hash
);
786 UpdateDisplay("800x600");
787 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
788 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
789 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
790 controller_
->GetWallpaper(),
791 wallpaper_manager_test_utils::kSmallChildWallpaperColor
));
794 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
, LargeChildWallpaper
) {
795 if (!ash::test::AshTestHelper::SupportsMultipleDisplays())
798 CreateCmdlineWallpapers();
799 LogInAsChild(kTestUser1
, kTestUser1Hash
);
800 UpdateDisplay("1600x1200");
801 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
802 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
803 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
804 controller_
->GetWallpaper(),
805 wallpaper_manager_test_utils::kLargeChildWallpaperColor
));
808 IN_PROC_BROWSER_TEST_F(WallpaperManagerBrowserTest
,
809 SwitchBetweenDefaultAndCustom
) {
810 // Start loading the default wallpaper.
811 UpdateDisplay("640x480");
812 CreateCmdlineWallpapers();
813 user_manager::UserManager::Get()->UserLoggedIn(
814 chromeos::login::kStubUser
, "test_hash", false);
816 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
818 // Custom wallpaper should be applied immediately, canceling the default
819 // wallpaper load task.
820 gfx::ImageSkia image
= wallpaper_manager_test_utils::CreateTestImage(
821 640, 480, wallpaper_manager_test_utils::kCustomWallpaperColor
);
822 WallpaperManager::Get()->SetCustomWallpaper(chromeos::login::kStubUser
,
825 WALLPAPER_LAYOUT_STRETCH
,
826 user_manager::User::CUSTOMIZED
,
829 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
831 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
832 controller_
->GetWallpaper(),
833 wallpaper_manager_test_utils::kCustomWallpaperColor
));
835 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
836 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished();
838 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor(
839 controller_
->GetWallpaper(),
840 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor
));
843 } // namespace chromeos