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.
8 #include "ash/desktop_background/desktop_background_controller.h"
9 #include "ash/desktop_background/desktop_background_controller_observer.h"
10 #include "ash/shell.h"
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/json/json_writer.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/run_loop.h"
20 #include "chrome/browser/chromeos/login/login_manager_test.h"
21 #include "chrome/browser/chromeos/login/startup_utils.h"
22 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
23 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
24 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
25 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
26 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
27 #include "chrome/browser/chromeos/profiles/profile_helper.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chromeos/chromeos_paths.h"
31 #include "chromeos/chromeos_switches.h"
32 #include "chromeos/dbus/cryptohome_client.h"
33 #include "chromeos/dbus/dbus_thread_manager.h"
34 #include "chromeos/dbus/fake_session_manager_client.h"
35 #include "chromeos/dbus/session_manager_client.h"
36 #include "components/policy/core/common/cloud/cloud_policy_core.h"
37 #include "components/policy/core/common/cloud/cloud_policy_store.h"
38 #include "components/policy/core/common/cloud/cloud_policy_validator.h"
39 #include "components/policy/core/common/cloud/policy_builder.h"
40 #include "components/user_manager/user.h"
41 #include "components/user_manager/user_manager.h"
42 #include "content/public/test/browser_test_utils.h"
43 #include "crypto/rsa_private_key.h"
44 #include "net/test/embedded_test_server/embedded_test_server.h"
45 #include "policy/proto/cloud_policy.pb.h"
46 #include "testing/gtest/include/gtest/gtest.h"
47 #include "third_party/skia/include/core/SkBitmap.h"
48 #include "third_party/skia/include/core/SkColor.h"
49 #include "ui/gfx/image/image_skia.h"
56 const char kTestUsers[2][19] = { "test-0@example.com", "test-1@example.com" };
58 const char kRedImageFileName[] = "chromeos/wallpapers/red.jpg";
59 const char kGreenImageFileName[] = "chromeos/wallpapers/green.jpg";
60 const char kBlueImageFileName[] = "chromeos/wallpapers/blue.jpg";
62 const SkColor kRedImageColor = SkColorSetARGB(255, 199, 6, 7);
63 const SkColor kGreenImageColor = SkColorSetARGB(255, 38, 196, 15);
65 policy::CloudPolicyStore* GetStoreForUser(const user_manager::User* user) {
66 Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
71 policy::UserCloudPolicyManagerChromeOS* policy_manager =
72 policy::UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
73 if (!policy_manager) {
77 return policy_manager->core()->store();
80 // Compute the average ARGB color of |bitmap|.
81 SkColor ComputeAverageColor(const SkBitmap& bitmap) {
82 if (bitmap.empty() || bitmap.width() < 1 || bitmap.height() < 1) {
83 ADD_FAILURE() << "Empty or invalid bitmap.";
84 return SkColorSetARGB(0, 0, 0, 0);
86 if (bitmap.isNull()) {
87 ADD_FAILURE() << "Bitmap has no pixelref.";
88 return SkColorSetARGB(0, 0, 0, 0);
90 if (bitmap.colorType() == kUnknown_SkColorType) {
91 ADD_FAILURE() << "Bitmap has not been configured.";
92 return SkColorSetARGB(0, 0, 0, 0);
94 uint64 a = 0, r = 0, g = 0, b = 0;
96 for (int x = 0; x < bitmap.width(); ++x) {
97 for (int y = 0; y < bitmap.height(); ++y) {
98 const SkColor color = bitmap.getColor(x, y);
99 a += SkColorGetA(color);
100 r += SkColorGetR(color);
101 g += SkColorGetG(color);
102 b += SkColorGetB(color);
105 bitmap.unlockPixels();
106 uint64 pixel_number = bitmap.width() * bitmap.height();
107 return SkColorSetARGB((a + pixel_number / 2) / pixel_number,
108 (r + pixel_number / 2) / pixel_number,
109 (g + pixel_number / 2) / pixel_number,
110 (b + pixel_number / 2) / pixel_number);
113 // Obtain background image and return its average ARGB color.
114 SkColor GetAverageBackgroundColor() {
115 const gfx::ImageSkia image =
116 ash::Shell::GetInstance()->desktop_background_controller()->
119 const gfx::ImageSkiaRep& representation = image.GetRepresentation(1.);
120 if (representation.is_null()) {
121 ADD_FAILURE() << "No image representation.";
122 return SkColorSetARGB(0, 0, 0, 0);
125 const SkBitmap& bitmap = representation.sk_bitmap();
126 return ComputeAverageColor(bitmap);
131 class WallpaperManagerPolicyTest
132 : public LoginManagerTest,
133 public ash::DesktopBackgroundControllerObserver {
135 WallpaperManagerPolicyTest()
136 : LoginManagerTest(true),
137 wallpaper_change_count_(0),
138 fake_session_manager_client_(new FakeSessionManagerClient) {
141 scoped_ptr<policy::UserPolicyBuilder> GetUserPolicyBuilder(
142 const std::string& user_id) {
143 scoped_ptr<policy::UserPolicyBuilder>
144 user_policy_builder(new policy::UserPolicyBuilder());
145 base::FilePath user_keys_dir;
146 EXPECT_TRUE(PathService::Get(DIR_USER_POLICY_KEYS, &user_keys_dir));
147 const std::string sanitized_user_id =
148 CryptohomeClient::GetStubSanitizedUsername(user_id);
149 const base::FilePath user_key_file =
150 user_keys_dir.AppendASCII(sanitized_user_id)
151 .AppendASCII("policy.pub");
152 std::vector<uint8> user_key_bits;
153 EXPECT_TRUE(user_policy_builder->GetSigningKey()->
154 ExportPublicKey(&user_key_bits));
155 EXPECT_TRUE(base::CreateDirectory(user_key_file.DirName()));
156 EXPECT_EQ(base::WriteFile(
158 reinterpret_cast<const char*>(user_key_bits.data()),
159 user_key_bits.size()),
160 static_cast<int>(user_key_bits.size()));
161 user_policy_builder->policy_data().set_username(user_id);
162 return user_policy_builder.Pass();
166 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
167 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
168 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
170 LoginManagerTest::SetUpInProcessBrowserTestFixture();
171 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
174 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
175 // Set the same switches as LoginManagerTest, except that kMultiProfiles is
176 // only set when GetParam() is true and except that kLoginProfile is set
177 // when GetParam() is false. The latter seems to be required for the sane
178 // start-up of user profiles.
179 command_line->AppendSwitch(switches::kLoginManager);
180 command_line->AppendSwitch(switches::kForceLoginManagerInTests);
183 virtual void SetUpOnMainThread() OVERRIDE {
184 LoginManagerTest::SetUpOnMainThread();
185 ash::Shell::GetInstance()->
186 desktop_background_controller()->AddObserver(this);
188 // Set up policy signing.
189 user_policy_builders_[0] = GetUserPolicyBuilder(kTestUsers[0]);
190 user_policy_builders_[1] = GetUserPolicyBuilder(kTestUsers[1]);
192 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
195 virtual void TearDownOnMainThread() OVERRIDE {
196 ash::Shell::GetInstance()->
197 desktop_background_controller()->RemoveObserver(this);
198 LoginManagerTest::TearDownOnMainThread();
201 // ash::DesktopBackgroundControllerObserver:
202 virtual void OnWallpaperDataChanged() OVERRIDE {
203 ++wallpaper_change_count_;
208 // Runs the loop until wallpaper has changed at least |count| times in total.
209 void RunUntilWallpaperChangeCount(int count) {
210 while (wallpaper_change_count_ < count) {
211 run_loop_.reset(new base::RunLoop);
216 std::string ConstructPolicy(const std::string& relative_path) const {
217 std::string image_data;
218 if (!base::ReadFileToString(test_data_dir_.Append(relative_path),
223 base::JSONWriter::Write(policy::test::ConstructExternalDataReference(
224 embedded_test_server()->GetURL(std::string("/") + relative_path).spec(),
230 // Inject |filename| as wallpaper policy for test user |user_number|. Set
231 // empty |filename| to clear policy.
232 void InjectPolicy(int user_number, const std::string& filename) {
233 ASSERT_TRUE(user_number == 0 || user_number == 1);
234 const std::string user_id = kTestUsers[user_number];
235 policy::UserPolicyBuilder* builder =
236 user_policy_builders_[user_number].get();
237 if (filename != "") {
239 mutable_wallpaperimage()->set_value(ConstructPolicy(filename));
241 builder->payload().Clear();
244 fake_session_manager_client_->set_user_policy(user_id, builder->GetBlob());
245 const user_manager::User* user =
246 user_manager::UserManager::Get()->FindUser(user_id);
248 policy::CloudPolicyStore* store = GetStoreForUser(user);
251 ASSERT_EQ(policy::CloudPolicyStore::STATUS_OK, store->status());
252 ASSERT_EQ(policy::CloudPolicyValidatorBase::VALIDATION_OK,
253 store->validation_status());
256 // Obtain WallpaperInfo for |user_number| from WallpaperManager.
257 void GetUserWallpaperInfo(int user_number, WallpaperInfo* wallpaper_info) {
258 WallpaperManager::Get()->
259 GetUserWallpaperInfo(kTestUsers[user_number], wallpaper_info);
262 base::FilePath test_data_dir_;
263 scoped_ptr<base::RunLoop> run_loop_;
264 int wallpaper_change_count_;
265 scoped_ptr<policy::UserPolicyBuilder> user_policy_builders_[2];
266 FakeSessionManagerClient* fake_session_manager_client_;
269 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerPolicyTest);
272 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest, PRE_SetResetClear) {
273 RegisterUser(kTestUsers[0]);
274 RegisterUser(kTestUsers[1]);
275 StartupUtils::MarkOobeCompleted();
278 // Verifies that the wallpaper can be set and re-set through policy and that
279 // setting policy for a user that is not logged in doesn't affect the current
280 // user. Also verifies that after the policy has been cleared, the wallpaper
281 // reverts to default.
282 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest, SetResetClear) {
284 LoginUser(kTestUsers[0]);
285 base::RunLoop().RunUntilIdle();
287 // First user: Wait until default wallpaper has been loaded (happens
288 // automatically) and store color to recognize it later.
289 RunUntilWallpaperChangeCount(1);
290 const SkColor original_background_color = GetAverageBackgroundColor();
292 // Second user: Set wallpaper policy to blue image. This should not result in
293 // a wallpaper change, which is checked at the very end of this test.
294 InjectPolicy(1, kBlueImageFileName);
296 // First user: Set wallpaper policy to red image and verify average color.
297 InjectPolicy(0, kRedImageFileName);
298 RunUntilWallpaperChangeCount(2);
299 GetUserWallpaperInfo(0, &info);
300 ASSERT_EQ(user_manager::User::POLICY, info.type);
301 ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
303 // First user: Set wallpaper policy to green image and verify average color.
304 InjectPolicy(0, kGreenImageFileName);
305 RunUntilWallpaperChangeCount(3);
306 GetUserWallpaperInfo(0, &info);
307 ASSERT_EQ(user_manager::User::POLICY, info.type);
308 ASSERT_EQ(kGreenImageColor, GetAverageBackgroundColor());
310 // First user: Clear wallpaper policy and verify that the default wallpaper is
313 RunUntilWallpaperChangeCount(4);
314 GetUserWallpaperInfo(0, &info);
315 ASSERT_EQ(user_manager::User::DEFAULT, info.type);
316 ASSERT_EQ(original_background_color, GetAverageBackgroundColor());
318 // Check wallpaper change count to ensure that setting the second user's
319 // wallpaper didn't have any effect.
320 ASSERT_EQ(4, wallpaper_change_count_);
323 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest,
324 DISABLED_PRE_PRE_PRE_WallpaperOnLoginScreen) {
325 RegisterUser(kTestUsers[0]);
326 RegisterUser(kTestUsers[1]);
327 StartupUtils::MarkOobeCompleted();
330 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest,
331 DISABLED_PRE_PRE_WallpaperOnLoginScreen) {
332 LoginUser(kTestUsers[0]);
334 // Wait until default wallpaper has been loaded.
335 RunUntilWallpaperChangeCount(1);
337 // Set wallpaper policy to red image.
338 InjectPolicy(0, kRedImageFileName);
340 // Run until wallpaper has changed.
341 RunUntilWallpaperChangeCount(2);
342 ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
345 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest,
346 DISABLED_PRE_WallpaperOnLoginScreen) {
347 LoginUser(kTestUsers[1]);
349 // Wait until default wallpaper has been loaded.
350 RunUntilWallpaperChangeCount(1);
352 // Set wallpaper policy to green image.
353 InjectPolicy(1, kGreenImageFileName);
355 // Run until wallpaper has changed.
356 RunUntilWallpaperChangeCount(2);
357 ASSERT_EQ(kGreenImageColor, GetAverageBackgroundColor());
360 // Disabled due to flakiness: http://crbug.com/385648.
361 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest,
362 DISABLED_WallpaperOnLoginScreen) {
363 // Wait for active pod's wallpaper to be loaded.
364 RunUntilWallpaperChangeCount(1);
365 ASSERT_EQ(kGreenImageColor, GetAverageBackgroundColor());
367 // Select the second pod (belonging to user 1).
368 ASSERT_TRUE(content::ExecuteScript(
369 static_cast<chromeos::LoginDisplayHostImpl*>(
370 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->
371 web_ui()->GetWebContents(),
372 "document.getElementsByClassName('pod')[1].focus();"));
373 RunUntilWallpaperChangeCount(2);
374 ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
377 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest, PRE_PRE_PersistOverLogout) {
378 RegisterUser(kTestUsers[0]);
379 StartupUtils::MarkOobeCompleted();
382 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest, PRE_PersistOverLogout) {
383 LoginUser(kTestUsers[0]);
385 // Wait until default wallpaper has been loaded.
386 RunUntilWallpaperChangeCount(1);
388 // Set wallpaper policy to red image.
389 InjectPolicy(0, kRedImageFileName);
391 // Run until wallpaper has changed.
392 RunUntilWallpaperChangeCount(2);
393 ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
396 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest, PersistOverLogout) {
397 LoginUser(kTestUsers[0]);
399 // Wait until wallpaper has been loaded.
400 RunUntilWallpaperChangeCount(1);
401 ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
404 } // namespace chromeos