Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/chromeos/login/wallpaper_manager_unittest.cc

Issue 208273005: If customization includes default wallpaper, download and apply it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after-review. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cstdlib> 5 #include <cstdlib>
6 #include <cstring> 6 #include <cstring>
7 7
8 #include "ash/ash_resources/grit/ash_resources.h" 8 #include "ash/ash_resources/grit/ash_resources.h"
9 #include "ash/desktop_background/desktop_background_controller.h" 9 #include "ash/desktop_background/desktop_background_controller.h"
10 #include "ash/desktop_background/desktop_background_controller_observer.h"
10 #include "ash/shell.h" 11 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h" 12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/display_manager_test_api.h"
12 #include "base/command_line.h" 14 #include "base/command_line.h"
13 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/files/file_path.h"
17 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_service.h" 19 #include "base/prefs/pref_service.h"
16 #include "base/prefs/testing_pref_service.h" 20 #include "base/prefs/testing_pref_service.h"
17 #include "chrome/browser/chromeos/login/fake_user_manager.h" 21 #include "chrome/browser/chromeos/login/fake_user_manager.h"
18 #include "chrome/browser/chromeos/login/startup_utils.h" 22 #include "chrome/browser/chromeos/login/startup_utils.h"
19 #include "chrome/browser/chromeos/login/user_manager_impl.h" 23 #include "chrome/browser/chromeos/login/user_manager_impl.h"
20 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 24 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h" 25 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "chrome/browser/chromeos/settings/device_settings_service.h" 26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
27 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/testing_browser_process.h" 29 #include "chrome/test/base/testing_browser_process.h"
25 #include "chromeos/chromeos_switches.h" 30 #include "chromeos/chromeos_switches.h"
26 #include "chromeos/settings/cros_settings_names.h" 31 #include "chromeos/settings/cros_settings_names.h"
27 #include "chromeos/settings/cros_settings_provider.h" 32 #include "chromeos/settings/cros_settings_provider.h"
28 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/base/resource/resource_bundle.h" 34 #include "ui/base/resource/resource_bundle.h"
35 #include "ui/gfx/point.h"
36 #include "ui/gfx/rect.h"
30 37
31 using namespace ash; 38 using namespace ash;
32 39
33 const char kTestUser1[] = "test-user@example.com"; 40 const char kTestUser1[] = "test-user@example.com";
34 const char kTestUser1Hash[] = "test-user@example.com-hash"; 41 const char kTestUser1Hash[] = "test-user@example.com-hash";
35 42
36 namespace chromeos { 43 namespace chromeos {
37 44
45 class TestObserver : public DesktopBackgroundControllerObserver {
46 public:
47 explicit TestObserver(DesktopBackgroundController* controller)
48 : controller_(controller) {
49 DCHECK(controller_);
50 controller_->AddObserver(this);
51 }
52
53 virtual ~TestObserver() {
54 controller_->RemoveObserver(this);
55 }
56
57 void WaitForWallpaperDataChanged() {
58 base::MessageLoop::current()->Run();
59 }
60
61 // DesktopBackgroundControllerObserver overrides:
62 virtual void OnWallpaperDataChanged() OVERRIDE {
63 base::MessageLoop::current()->Quit();
64 }
65
66 private:
67 DesktopBackgroundController* controller_;
68 };
69
38 class WallpaperManagerTest : public test::AshTestBase { 70 class WallpaperManagerTest : public test::AshTestBase {
39 public: 71 public:
40 WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {} 72 WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {}
41 73
42 virtual ~WallpaperManagerTest() {} 74 virtual ~WallpaperManagerTest() {}
43 75
44 virtual void SetUp() OVERRIDE { 76 virtual void SetUp() OVERRIDE {
45 test::AshTestBase::SetUp(); 77 test::AshTestBase::SetUp();
46 78
47 // Register an in-memory local settings instance. 79 // Register an in-memory local settings instance.
48 local_state_.reset(new TestingPrefServiceSimple); 80 local_state_.reset(new TestingPrefServiceSimple);
49 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get()); 81 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get());
50 UserManager::RegisterPrefs(local_state_->registry()); 82 chrome::RegisterLocalState(local_state_->registry());
51 // Wallpaper manager and user image managers prefs will be accessed by the
52 // unit-test as well.
53 UserImageManager::RegisterPrefs(local_state_->registry());
54 WallpaperManager::RegisterPrefs(local_state_->registry());
55 83
56 StartupUtils::RegisterPrefs(local_state_->registry());
57 StartupUtils::MarkDeviceRegistered(); 84 StartupUtils::MarkDeviceRegistered();
58 85
59 ResetUserManager(); 86 ResetUserManager();
87 controller_ = ash::Shell::GetInstance()->desktop_background_controller();
60 } 88 }
61 89
62 virtual void TearDown() OVERRIDE { 90 virtual void TearDown() OVERRIDE {
63 // Unregister the in-memory local settings instance. 91 // Unregister the in-memory local settings instance.
64 TestingBrowserProcess::GetGlobal()->SetLocalState(0); 92 TestingBrowserProcess::GetGlobal()->SetLocalState(0);
65 93
66 // Shut down the DeviceSettingsService. 94 // Shut down the DeviceSettingsService.
67 DeviceSettingsService::Get()->UnsetSessionManager(); 95 DeviceSettingsService::Get()->UnsetSessionManager();
68 96
69 test::AshTestBase::TearDown(); 97 test::AshTestBase::TearDown();
70 } 98 }
71 99
72 void ResetUserManager() { 100 void ResetUserManager() {
73 // Reset the UserManager singleton. 101 // Reset the UserManager singleton.
74 user_manager_enabler_.reset(); 102 user_manager_enabler_.reset();
75 // Initialize the UserManager singleton to a fresh UserManagerImpl instance. 103 // Initialize the UserManager singleton to a fresh UserManagerImpl instance.
76 user_manager_enabler_.reset( 104 user_manager_enabler_.reset(
77 new ScopedUserManagerEnabler(new UserManagerImpl)); 105 new ScopedUserManagerEnabler(new UserManagerImpl));
78 } 106 }
79 107
80 void AppendGuestSwitch() { 108 void AppendGuestSwitch() {
81 command_line_.AppendSwitch(switches::kGuestSession); 109 command_line_.AppendSwitch(switches::kGuestSession);
82 WallpaperManager::Get()->set_command_line_for_testing(&command_line_); 110 WallpaperManager::Get()->set_command_line_for_testing(&command_line_);
83 } 111 }
84 112
85 void WaitAsyncWallpaperLoad() { 113 void WaitAsyncWallpaperLoad() {
86 base::MessageLoop::current()->RunUntilIdle(); 114 base::MessageLoop::current()->RunUntilIdle();
87 } 115 }
88 116
117 const base::FilePath& GetWallpaperManagerGuestSmallFile() const {
118 return WallpaperManager::Get()->guest_default_small_wallpaper_file_;
119 }
120
121 const base::FilePath& GetWallpaperManagerGuestLargeFile() const {
122 return WallpaperManager::Get()->guest_default_large_wallpaper_file_;
123 }
124
89 protected: 125 protected:
126 // Colors used for different default wallpapers by
127 // InitWallpapers().
128 static const SkColor kLargeWallpaperColor = SK_ColorRED;
129 static const SkColor kSmallWallpaperColor = SK_ColorGREEN;
130 static const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE;
131 static const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW;
132
133 // A color that can be passed to CreateImage(). Specifically chosen to not
134 // conflict with any of the default wallpaper colors.
135 static const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
136
137 // Dimension used for width and height of default wallpaper images. A
138 // small value is used to minimize the amount of time spent compressing
139 // and writing images.
140 static const int kWallpaperSize = 2;
141
142 // Creates a test image of size 1x1.
143 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) {
144 SkBitmap bitmap;
145 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
146 bitmap.allocPixels();
147 bitmap.eraseColor(color);
148 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
149 }
150
151 // Initializes default wallpaper paths "*default_*file" and writes JPEG
152 // wallpaper images to them.
153 // Only needs to be called (once) by tests that want to test loading of
154 // default wallpapers.
155 void InitWallpapers() {
156 WallpaperManager::Get()->default_small_wallpaper_file_ =
157 base::FilePath(FILE_PATH_LITERAL("small.jpg"));
158 WallpaperManager::Get()->default_large_wallpaper_file_ =
159 base::FilePath(FILE_PATH_LITERAL("large.jpg"));
160 WallpaperManager::Get()->guest_default_small_wallpaper_file_ =
161 base::FilePath(FILE_PATH_LITERAL("guest_small.jpg"));
162 WallpaperManager::Get()->guest_default_large_wallpaper_file_ =
163 base::FilePath(FILE_PATH_LITERAL("guest_large.jpg"));
164 WallpaperManager::Get()->default_small_wallpaper_image_.reset(
165 new gfx::ImageSkia(CreateTestImage(
166 kWallpaperSize, kWallpaperSize, kSmallWallpaperColor)));
167 WallpaperManager::Get()->default_large_wallpaper_image_.reset(
168 new gfx::ImageSkia(CreateTestImage(
169 kWallpaperSize, kWallpaperSize, kLargeWallpaperColor)));
170 WallpaperManager::Get()->guest_default_small_wallpaper_image_.reset(
171 new gfx::ImageSkia(CreateTestImage(
172 kWallpaperSize, kWallpaperSize, kSmallGuestWallpaperColor)));
173 WallpaperManager::Get()->guest_default_large_wallpaper_image_.reset(
174 new gfx::ImageSkia(CreateTestImage(
175 kWallpaperSize, kWallpaperSize, kLargeGuestWallpaperColor)));
176 }
177
178 // Returns true if the color at the center of |image| is close to
179 // |expected_color|. (The center is used so small wallpaper images can be
180 // used.)
181 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) {
182 if (image.size().IsEmpty()) {
183 LOG(ERROR) << "Image is empty";
184 return false;
185 }
186
187 const SkBitmap* bitmap = image.bitmap();
188 if (!bitmap) {
189 LOG(ERROR) << "Unable to get bitmap from image";
190 return false;
191 }
192
193 bitmap->lockPixels();
194 gfx::Point center = gfx::Rect(image.size()).CenterPoint();
195 SkColor image_color = bitmap->getColor(center.x(), center.y());
196 bitmap->unlockPixels();
197
198 const int kDiff = 3;
199 if (std::abs(static_cast<int>(SkColorGetA(image_color)) -
200 static_cast<int>(SkColorGetA(expected_color))) > kDiff ||
201 std::abs(static_cast<int>(SkColorGetR(image_color)) -
202 static_cast<int>(SkColorGetR(expected_color))) > kDiff ||
203 std::abs(static_cast<int>(SkColorGetG(image_color)) -
204 static_cast<int>(SkColorGetG(expected_color))) > kDiff ||
205 std::abs(static_cast<int>(SkColorGetB(image_color)) -
206 static_cast<int>(SkColorGetB(expected_color))) > kDiff) {
207 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color
208 << " but got 0x" << image_color;
209 return false;
210 }
211
212 return true;
213 }
214
90 CommandLine command_line_; 215 CommandLine command_line_;
91 216
92 scoped_ptr<TestingPrefServiceSimple> local_state_; 217 scoped_ptr<TestingPrefServiceSimple> local_state_;
93 218
94 ScopedTestDeviceSettingsService test_device_settings_service_; 219 ScopedTestDeviceSettingsService test_device_settings_service_;
95 ScopedTestCrosSettings test_cros_settings_; 220 ScopedTestCrosSettings test_cros_settings_;
96 221
97 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; 222 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
98 223
224 DesktopBackgroundController* controller_;
225
99 private: 226 private:
100 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest); 227 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest);
101 }; 228 };
102 229
103 // Test for crbug.com/260755. If this test fails, it is probably because the 230 // Test that WallpaperManager loads the appropriate wallpaper
104 // wallpaper of last logged in user is set as guest wallpaper. 231 // images as specified via command-line flags in various situations.
105 TEST_F(WallpaperManagerTest, GuestUserUseGuestWallpaper) { 232 // Splitting these into separate tests avoids needing to run animations.
106 UserManager::Get()->UserLoggedIn(kTestUser1, kTestUser1Hash, false); 233 // TODO(derat): Combine these into a single test
234 TEST_F(WallpaperManagerTest, SmallDefaultWallpaper) {
235 if (!SupportsMultipleDisplays())
236 return;
107 237
108 std::string relative_path = 238 InitWallpapers();
109 base::FilePath(kTestUser1Hash).Append(FILE_PATH_LITERAL("DUMMY")).value(); 239 TestObserver observer(controller_);
110 // Saves wallpaper info to local state for user |kTestUser1|.
111 WallpaperInfo info = {
112 relative_path,
113 WALLPAPER_LAYOUT_CENTER_CROPPED,
114 User::CUSTOMIZED,
115 base::Time::Now().LocalMidnight()
116 };
117 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
118 ResetUserManager();
119 240
120 AppendGuestSwitch(); 241 // At 800x600, the small wallpaper should be loaded.
121 scoped_ptr<WallpaperManager::TestApi> test_api; 242 test::DisplayManagerTestApi display_manager_test_api(
122 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get())); 243 Shell::GetInstance()->display_manager());
123 // If last logged in user's wallpaper is used in function InitializeWallpaper, 244 display_manager_test_api.UpdateDisplay("800x600");
124 // this test will crash. InitializeWallpaper should be a noop after 245 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
125 // AppendGuestSwitch being called. 246 observer.WaitForWallpaperDataChanged();
126 WallpaperManager::Get()->InitializeWallpaper(); 247 EXPECT_TRUE(
127 EXPECT_TRUE(test_api->current_wallpaper_path().empty()); 248 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor));
128 UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, 249 }
129 UserManager::kGuestUserName, false); 250
130 WaitAsyncWallpaperLoad(); 251 TEST_F(WallpaperManagerTest, LargeDefaultWallpaper) {
131 EXPECT_FALSE(ash::Shell::GetInstance()->desktop_background_controller()-> 252 if (!SupportsMultipleDisplays())
132 SetDefaultWallpaper(true)); 253 return;
254
255 InitWallpapers();
256 TestObserver observer(controller_);
257 test::DisplayManagerTestApi display_manager_test_api(
258 Shell::GetInstance()->display_manager());
259 display_manager_test_api.UpdateDisplay("1600x1200");
260 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
261 observer.WaitForWallpaperDataChanged();
262 EXPECT_TRUE(
263 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor));
264 }
265
266 TEST_F(WallpaperManagerTest, LargeDefaultWallpaperWhenRotated) {
267 if (!SupportsMultipleDisplays())
268 return;
269 InitWallpapers();
270 TestObserver observer(controller_);
271 test::DisplayManagerTestApi display_manager_test_api(
272 Shell::GetInstance()->display_manager());
273
274 display_manager_test_api.UpdateDisplay("1200x800/r");
275 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
276 observer.WaitForWallpaperDataChanged();
277 EXPECT_TRUE(
278 ImageIsNearColor(controller_->GetWallpaper(), kLargeWallpaperColor));
279 }
280
281 TEST_F(WallpaperManagerTest, SmallGuestWallpaper) {
282 if (!SupportsMultipleDisplays())
283 return;
284 InitWallpapers();
285 UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, "", false);
286 TestObserver observer(controller_);
287 test::DisplayManagerTestApi display_manager_test_api(
288 Shell::GetInstance()->display_manager());
289 display_manager_test_api.UpdateDisplay("800x600");
290 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
291 observer.WaitForWallpaperDataChanged();
292 EXPECT_TRUE(
293 ImageIsNearColor(controller_->GetWallpaper(), kSmallGuestWallpaperColor));
294 }
295
296 TEST_F(WallpaperManagerTest, LargeGuestWallpaper) {
297 if (!SupportsMultipleDisplays())
298 return;
299
300 InitWallpapers();
301 UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, "", false);
302 TestObserver observer(controller_);
303 test::DisplayManagerTestApi display_manager_test_api(
304 Shell::GetInstance()->display_manager());
305 display_manager_test_api.UpdateDisplay("1600x1200");
306 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
307 observer.WaitForWallpaperDataChanged();
308 EXPECT_TRUE(
309 ImageIsNearColor(controller_->GetWallpaper(), kLargeGuestWallpaperColor));
310 }
311
312 TEST_F(WallpaperManagerTest, SwitchBetweenDefaultAndCustom) {
313 // Start loading the default wallpaper.
314 UpdateDisplay("640x480");
315 InitWallpapers();
316 UserManager::Get()->UserLoggedIn(UserManager::kStubUser, "test_hash", false);
317
318 TestObserver observer(controller_);
319 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
320
321 // Custom wallpaper should be applied immediately, canceling the default
322 // wallpaper load task.
323 gfx::ImageSkia image = CreateTestImage(640, 480, kCustomWallpaperColor);
324 UserImage wallpaper(image);
325 WallpaperManager::Get()->SetCustomWallpaper(UserManager::kStubUser,
326 "test_hash",
327 "test-nofile.jpeg",
328 WALLPAPER_LAYOUT_STRETCH,
329 User::CUSTOMIZED,
330 wallpaper,
331 true);
332 observer.WaitForWallpaperDataChanged();
333
334 EXPECT_TRUE(
335 ImageIsNearColor(controller_->GetWallpaper(), kCustomWallpaperColor));
336
337 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
338 observer.WaitForWallpaperDataChanged();
339 EXPECT_TRUE(
340 ImageIsNearColor(controller_->GetWallpaper(), kSmallWallpaperColor));
133 } 341 }
134 342
135 class WallpaperManagerCacheTest : public test::AshTestBase { 343 class WallpaperManagerCacheTest : public test::AshTestBase {
136 public: 344 public:
137 WallpaperManagerCacheTest() 345 WallpaperManagerCacheTest()
138 : fake_user_manager_(new FakeUserManager()), 346 : fake_user_manager_(new FakeUserManager()),
139 scoped_user_manager_(fake_user_manager_) { 347 scoped_user_manager_(fake_user_manager_) {
140 } 348 }
141 349
142 protected: 350 protected:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // Logged in users' wallpaper cache should be kept. 401 // Logged in users' wallpaper cache should be kept.
194 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper)); 402 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper));
195 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper)); 403 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper));
196 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper)); 404 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper));
197 405
198 // Not logged in user's wallpaper cache should be cleared. 406 // Not logged in user's wallpaper cache should be cleared.
199 EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper)); 407 EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper));
200 } 408 }
201 409
202 } // namespace chromeos 410 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/wallpaper_manager.cc ('k') | chrome/browser/chromeos/login/wizard_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698