1 // Copyright 2013 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.
8 #include "ash/ash_resources/grit/ash_resources.h"
9 #include "ash/desktop_background/desktop_background_controller.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "base/command_line.h"
13 #include "base/file_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/prefs/testing_pref_service.h"
17 #include "chrome/browser/chromeos/login/fake_user_manager.h"
18 #include "chrome/browser/chromeos/login/startup_utils.h"
19 #include "chrome/browser/chromeos/login/user_manager_impl.h"
20 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "chrome/browser/chromeos/settings/device_settings_service.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chromeos/chromeos_switches.h"
26 #include "chromeos/settings/cros_settings_names.h"
27 #include "chromeos/settings/cros_settings_provider.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/base/resource/resource_bundle.h"
33 const char kTestUser1[] = "test-user@example.com";
34 const char kTestUser1Hash[] = "test-user@example.com-hash";
38 class WallpaperManagerTest : public test::AshTestBase {
40 WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {}
42 virtual ~WallpaperManagerTest() {}
44 virtual void SetUp() OVERRIDE {
45 test::AshTestBase::SetUp();
47 // Register an in-memory local settings instance.
48 local_state_.reset(new TestingPrefServiceSimple);
49 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get());
50 UserManager::RegisterPrefs(local_state_->registry());
51 // Wallpaper manager and user image managers prefs will be accessed by the
53 UserImageManager::RegisterPrefs(local_state_->registry());
54 WallpaperManager::RegisterPrefs(local_state_->registry());
56 StartupUtils::RegisterPrefs(local_state_->registry());
57 StartupUtils::MarkDeviceRegistered();
62 virtual void TearDown() OVERRIDE {
63 // Unregister the in-memory local settings instance.
64 TestingBrowserProcess::GetGlobal()->SetLocalState(0);
66 // Shut down the DeviceSettingsService.
67 DeviceSettingsService::Get()->UnsetSessionManager();
69 test::AshTestBase::TearDown();
72 void ResetUserManager() {
73 // Reset the UserManager singleton.
74 user_manager_enabler_.reset();
75 // Initialize the UserManager singleton to a fresh UserManagerImpl instance.
76 user_manager_enabler_.reset(
77 new ScopedUserManagerEnabler(new UserManagerImpl));
80 void AppendGuestSwitch() {
81 command_line_.AppendSwitch(switches::kGuestSession);
82 WallpaperManager::Get()->set_command_line_for_testing(&command_line_);
85 void WaitAsyncWallpaperLoad() {
86 base::MessageLoop::current()->RunUntilIdle();
90 CommandLine command_line_;
92 scoped_ptr<TestingPrefServiceSimple> local_state_;
94 ScopedTestDeviceSettingsService test_device_settings_service_;
95 ScopedTestCrosSettings test_cros_settings_;
97 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
100 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest);
103 // Test for crbug.com/260755. If this test fails, it is probably because the
104 // wallpaper of last logged in user is set as guest wallpaper.
105 TEST_F(WallpaperManagerTest, GuestUserUseGuestWallpaper) {
106 UserManager::Get()->UserLoggedIn(kTestUser1, kTestUser1Hash, false);
108 std::string relative_path =
109 base::FilePath(kTestUser1Hash).Append(FILE_PATH_LITERAL("DUMMY")).value();
110 // Saves wallpaper info to local state for user |kTestUser1|.
111 WallpaperInfo info = {
113 WALLPAPER_LAYOUT_CENTER_CROPPED,
115 base::Time::Now().LocalMidnight()
117 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
121 scoped_ptr<WallpaperManager::TestApi> test_api;
122 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get()));
123 // If last logged in user's wallpaper is used in function InitializeWallpaper,
124 // this test will crash. InitializeWallpaper should be a noop after
125 // AppendGuestSwitch being called.
126 WallpaperManager::Get()->InitializeWallpaper();
127 EXPECT_TRUE(test_api->current_wallpaper_path().empty());
128 UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName,
129 UserManager::kGuestUserName, false);
130 WaitAsyncWallpaperLoad();
131 EXPECT_FALSE(ash::Shell::GetInstance()->desktop_background_controller()->
132 SetDefaultWallpaper(true));
135 class WallpaperManagerCacheTest : public test::AshTestBase {
137 WallpaperManagerCacheTest()
138 : fake_user_manager_(new FakeUserManager()),
139 scoped_user_manager_(fake_user_manager_) {
143 virtual ~WallpaperManagerCacheTest() {}
145 FakeUserManager* fake_user_manager() { return fake_user_manager_; }
147 virtual void SetUp() OVERRIDE {
148 CommandLine::ForCurrentProcess()->AppendSwitch(::switches::kMultiProfiles);
149 test::AshTestBase::SetUp();
152 // Creates a test image of size 1x1.
153 gfx::ImageSkia CreateTestImage(SkColor color) {
155 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
156 bitmap.allocPixels();
157 bitmap.eraseColor(color);
158 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
162 FakeUserManager* fake_user_manager_;
163 ScopedUserManagerEnabler scoped_user_manager_;
166 TEST_F(WallpaperManagerCacheTest, VerifyWallpaperCache) {
167 // Add three users to known users.
168 std::string test_user_1 = "test1@example.com";
169 std::string test_user_2 = "test2@example.com";
170 std::string test_user_3 = "test3@example.com";
171 fake_user_manager()->AddUser(test_user_1);
172 fake_user_manager()->AddUser(test_user_2);
173 fake_user_manager()->AddUser(test_user_3);
176 fake_user_manager()->LoginUser(test_user_1);
177 fake_user_manager()->LoginUser(test_user_2);
179 scoped_ptr<WallpaperManager::TestApi> test_api;
180 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get()));
182 gfx::ImageSkia test_user_1_wallpaper = CreateTestImage(SK_ColorRED);
183 gfx::ImageSkia test_user_2_wallpaper = CreateTestImage(SK_ColorGREEN);
184 gfx::ImageSkia test_user_3_wallpaper = CreateTestImage(SK_ColorWHITE);
185 test_api->SetWallpaperCache(test_user_1, test_user_1_wallpaper);
186 test_api->SetWallpaperCache(test_user_2, test_user_2_wallpaper);
187 test_api->SetWallpaperCache(test_user_3, test_user_3_wallpaper);
189 test_api->ClearWallpaperCache();
191 gfx::ImageSkia cached_wallpaper;
192 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_1, &cached_wallpaper));
193 // Logged in users' wallpaper cache should be kept.
194 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper));
195 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper));
196 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper));
198 // Not logged in user's wallpaper cache should be cleared.
199 EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper));
202 } // namespace chromeos