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 kRedImageFileName
[] = "chromeos/wallpapers/red.jpg";
57 const char kGreenImageFileName
[] = "chromeos/wallpapers/green.jpg";
58 const char kBlueImageFileName
[] = "chromeos/wallpapers/blue.jpg";
60 const SkColor kRedImageColor
= SkColorSetARGB(255, 199, 6, 7);
61 const SkColor kGreenImageColor
= SkColorSetARGB(255, 38, 196, 15);
63 policy::CloudPolicyStore
* GetStoreForUser(const user_manager::User
* user
) {
64 Profile
* profile
= ProfileHelper::Get()->GetProfileByUserUnsafe(user
);
69 policy::UserCloudPolicyManagerChromeOS
* policy_manager
=
70 policy::UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
71 if (!policy_manager
) {
75 return policy_manager
->core()->store();
78 // Compute the average ARGB color of |bitmap|.
79 SkColor
ComputeAverageColor(const SkBitmap
& bitmap
) {
80 if (bitmap
.empty() || bitmap
.width() < 1 || bitmap
.height() < 1) {
81 ADD_FAILURE() << "Empty or invalid bitmap.";
82 return SkColorSetARGB(0, 0, 0, 0);
84 if (bitmap
.isNull()) {
85 ADD_FAILURE() << "Bitmap has no pixelref.";
86 return SkColorSetARGB(0, 0, 0, 0);
88 if (bitmap
.colorType() == kUnknown_SkColorType
) {
89 ADD_FAILURE() << "Bitmap has not been configured.";
90 return SkColorSetARGB(0, 0, 0, 0);
92 uint64 a
= 0, r
= 0, g
= 0, b
= 0;
94 for (int x
= 0; x
< bitmap
.width(); ++x
) {
95 for (int y
= 0; y
< bitmap
.height(); ++y
) {
96 const SkColor color
= bitmap
.getColor(x
, y
);
97 a
+= SkColorGetA(color
);
98 r
+= SkColorGetR(color
);
99 g
+= SkColorGetG(color
);
100 b
+= SkColorGetB(color
);
103 bitmap
.unlockPixels();
104 uint64 pixel_number
= bitmap
.width() * bitmap
.height();
105 return SkColorSetARGB((a
+ pixel_number
/ 2) / pixel_number
,
106 (r
+ pixel_number
/ 2) / pixel_number
,
107 (g
+ pixel_number
/ 2) / pixel_number
,
108 (b
+ pixel_number
/ 2) / pixel_number
);
111 // Obtain background image and return its average ARGB color.
112 SkColor
GetAverageBackgroundColor() {
113 const gfx::ImageSkia image
=
114 ash::Shell::GetInstance()->desktop_background_controller()->
117 const gfx::ImageSkiaRep
& representation
= image
.GetRepresentation(1.);
118 if (representation
.is_null()) {
119 ADD_FAILURE() << "No image representation.";
120 return SkColorSetARGB(0, 0, 0, 0);
123 const SkBitmap
& bitmap
= representation
.sk_bitmap();
124 return ComputeAverageColor(bitmap
);
129 class WallpaperManagerPolicyTest
130 : public LoginManagerTest
,
131 public ash::DesktopBackgroundControllerObserver
{
133 WallpaperManagerPolicyTest()
134 : LoginManagerTest(true),
135 wallpaper_change_count_(0),
136 fake_session_manager_client_(new FakeSessionManagerClient
) {
137 testUsers_
[0] = LoginManagerTest::kEnterpriseUser1
;
138 testUsers_
[1] = LoginManagerTest::kEnterpriseUser2
;
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 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 void SetUpCommandLine(base::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
);
182 LoginManagerTest::SetUpCommandLine(command_line
);
185 void SetUpOnMainThread() override
{
186 LoginManagerTest::SetUpOnMainThread();
187 ash::Shell::GetInstance()->
188 desktop_background_controller()->AddObserver(this);
190 // Set up policy signing.
191 user_policy_builders_
[0] = GetUserPolicyBuilder(testUsers_
[0]);
192 user_policy_builders_
[1] = GetUserPolicyBuilder(testUsers_
[1]);
195 void TearDownOnMainThread() override
{
196 ash::Shell::GetInstance()->
197 desktop_background_controller()->RemoveObserver(this);
198 LoginManagerTest::TearDownOnMainThread();
201 // ash::DesktopBackgroundControllerObserver:
202 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()
225 ->GetURL(std::string("/") + relative_path
)
232 // Inject |filename| as wallpaper policy for test user |user_number|. Set
233 // empty |filename| to clear policy.
234 void InjectPolicy(int user_number
, const std::string
& filename
) {
235 ASSERT_TRUE(user_number
== 0 || user_number
== 1);
236 const std::string user_id
= testUsers_
[user_number
];
237 policy::UserPolicyBuilder
* builder
=
238 user_policy_builders_
[user_number
].get();
239 if (!filename
.empty()) {
241 mutable_wallpaperimage()->set_value(ConstructPolicy(filename
));
243 builder
->payload().Clear();
246 fake_session_manager_client_
->set_user_policy(user_id
, builder
->GetBlob());
247 const user_manager::User
* user
=
248 user_manager::UserManager::Get()->FindUser(user_id
);
250 policy::CloudPolicyStore
* store
= GetStoreForUser(user
);
253 ASSERT_EQ(policy::CloudPolicyStore::STATUS_OK
, store
->status());
254 ASSERT_EQ(policy::CloudPolicyValidatorBase::VALIDATION_OK
,
255 store
->validation_status());
258 // Obtain WallpaperInfo for |user_number| from WallpaperManager.
259 void GetUserWallpaperInfo(int user_number
,
260 wallpaper::WallpaperInfo
* wallpaper_info
) {
261 WallpaperManager::Get()->GetUserWallpaperInfo(testUsers_
[user_number
],
265 base::FilePath test_data_dir_
;
266 scoped_ptr
<base::RunLoop
> run_loop_
;
267 int wallpaper_change_count_
;
268 scoped_ptr
<policy::UserPolicyBuilder
> user_policy_builders_
[2];
269 FakeSessionManagerClient
* fake_session_manager_client_
;
270 const char* testUsers_
[2];
273 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerPolicyTest
);
276 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_SetResetClear
) {
277 RegisterUser(testUsers_
[0]);
278 RegisterUser(testUsers_
[1]);
279 StartupUtils::MarkOobeCompleted();
282 // Verifies that the wallpaper can be set and re-set through policy and that
283 // setting policy for a user that is not logged in doesn't affect the current
284 // user. Also verifies that after the policy has been cleared, the wallpaper
285 // reverts to default.
286 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, SetResetClear
) {
287 wallpaper::WallpaperInfo info
;
288 LoginUser(testUsers_
[0]);
289 base::RunLoop().RunUntilIdle();
291 // First user: Wait until default wallpaper has been loaded (happens
292 // automatically) and store color to recognize it later.
293 RunUntilWallpaperChangeCount(1);
294 const SkColor original_background_color
= GetAverageBackgroundColor();
296 // Second user: Set wallpaper policy to blue image. This should not result in
297 // a wallpaper change, which is checked at the very end of this test.
298 InjectPolicy(1, kBlueImageFileName
);
300 // First user: Set wallpaper policy to red image and verify average color.
301 InjectPolicy(0, kRedImageFileName
);
302 RunUntilWallpaperChangeCount(2);
303 GetUserWallpaperInfo(0, &info
);
304 ASSERT_EQ(user_manager::User::POLICY
, info
.type
);
305 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
307 // First user: Set wallpaper policy to green image and verify average color.
308 InjectPolicy(0, kGreenImageFileName
);
309 RunUntilWallpaperChangeCount(3);
310 GetUserWallpaperInfo(0, &info
);
311 ASSERT_EQ(user_manager::User::POLICY
, info
.type
);
312 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
314 // First user: Clear wallpaper policy and verify that the default wallpaper is
317 RunUntilWallpaperChangeCount(4);
318 GetUserWallpaperInfo(0, &info
);
319 ASSERT_EQ(user_manager::User::DEFAULT
, info
.type
);
320 ASSERT_EQ(original_background_color
, GetAverageBackgroundColor());
322 // Check wallpaper change count to ensure that setting the second user's
323 // wallpaper didn't have any effect.
324 ASSERT_EQ(4, wallpaper_change_count_
);
327 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
328 DISABLED_PRE_PRE_PRE_WallpaperOnLoginScreen
) {
329 RegisterUser(testUsers_
[0]);
330 RegisterUser(testUsers_
[1]);
331 StartupUtils::MarkOobeCompleted();
334 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
335 DISABLED_PRE_PRE_WallpaperOnLoginScreen
) {
336 LoginUser(testUsers_
[0]);
338 // Wait until default wallpaper has been loaded.
339 RunUntilWallpaperChangeCount(1);
341 // Set wallpaper policy to red image.
342 InjectPolicy(0, kRedImageFileName
);
344 // Run until wallpaper has changed.
345 RunUntilWallpaperChangeCount(2);
346 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
349 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
350 DISABLED_PRE_WallpaperOnLoginScreen
) {
351 LoginUser(testUsers_
[1]);
353 // Wait until default wallpaper has been loaded.
354 RunUntilWallpaperChangeCount(1);
356 // Set wallpaper policy to green image.
357 InjectPolicy(1, kGreenImageFileName
);
359 // Run until wallpaper has changed.
360 RunUntilWallpaperChangeCount(2);
361 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
364 // Disabled due to flakiness: http://crbug.com/385648.
365 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
366 DISABLED_WallpaperOnLoginScreen
) {
367 // Wait for active pod's wallpaper to be loaded.
368 RunUntilWallpaperChangeCount(1);
369 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
371 // Select the second pod (belonging to user 1).
372 ASSERT_TRUE(content::ExecuteScript(
373 static_cast<chromeos::LoginDisplayHostImpl
*>(
374 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->
375 web_ui()->GetWebContents(),
376 "document.getElementsByClassName('pod')[1].focus();"));
377 RunUntilWallpaperChangeCount(2);
378 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
381 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_PRE_PersistOverLogout
) {
382 RegisterUser(testUsers_
[0]);
383 StartupUtils::MarkOobeCompleted();
386 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_PersistOverLogout
) {
387 LoginUser(testUsers_
[0]);
389 // Wait until default wallpaper has been loaded.
390 RunUntilWallpaperChangeCount(1);
392 // Set wallpaper policy to red image.
393 InjectPolicy(0, kRedImageFileName
);
395 // Run until wallpaper has changed.
396 RunUntilWallpaperChangeCount(2);
397 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
400 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PersistOverLogout
) {
401 LoginUser(testUsers_
[0]);
403 // Wait until wallpaper has been loaded.
404 RunUntilWallpaperChangeCount(1);
405 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
408 } // namespace chromeos