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 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
);
183 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 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
= kTestUsers
[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()->
262 GetUserWallpaperInfo(kTestUsers
[user_number
], wallpaper_info
);
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_
;
272 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerPolicyTest
);
275 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_SetResetClear
) {
276 RegisterUser(kTestUsers
[0]);
277 RegisterUser(kTestUsers
[1]);
278 StartupUtils::MarkOobeCompleted();
281 // Verifies that the wallpaper can be set and re-set through policy and that
282 // setting policy for a user that is not logged in doesn't affect the current
283 // user. Also verifies that after the policy has been cleared, the wallpaper
284 // reverts to default.
285 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, SetResetClear
) {
286 wallpaper::WallpaperInfo info
;
287 LoginUser(kTestUsers
[0]);
288 base::RunLoop().RunUntilIdle();
290 // First user: Wait until default wallpaper has been loaded (happens
291 // automatically) and store color to recognize it later.
292 RunUntilWallpaperChangeCount(1);
293 const SkColor original_background_color
= GetAverageBackgroundColor();
295 // Second user: Set wallpaper policy to blue image. This should not result in
296 // a wallpaper change, which is checked at the very end of this test.
297 InjectPolicy(1, kBlueImageFileName
);
299 // First user: Set wallpaper policy to red image and verify average color.
300 InjectPolicy(0, kRedImageFileName
);
301 RunUntilWallpaperChangeCount(2);
302 GetUserWallpaperInfo(0, &info
);
303 ASSERT_EQ(user_manager::User::POLICY
, info
.type
);
304 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
306 // First user: Set wallpaper policy to green image and verify average color.
307 InjectPolicy(0, kGreenImageFileName
);
308 RunUntilWallpaperChangeCount(3);
309 GetUserWallpaperInfo(0, &info
);
310 ASSERT_EQ(user_manager::User::POLICY
, info
.type
);
311 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
313 // First user: Clear wallpaper policy and verify that the default wallpaper is
316 RunUntilWallpaperChangeCount(4);
317 GetUserWallpaperInfo(0, &info
);
318 ASSERT_EQ(user_manager::User::DEFAULT
, info
.type
);
319 ASSERT_EQ(original_background_color
, GetAverageBackgroundColor());
321 // Check wallpaper change count to ensure that setting the second user's
322 // wallpaper didn't have any effect.
323 ASSERT_EQ(4, wallpaper_change_count_
);
326 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
327 DISABLED_PRE_PRE_PRE_WallpaperOnLoginScreen
) {
328 RegisterUser(kTestUsers
[0]);
329 RegisterUser(kTestUsers
[1]);
330 StartupUtils::MarkOobeCompleted();
333 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
334 DISABLED_PRE_PRE_WallpaperOnLoginScreen
) {
335 LoginUser(kTestUsers
[0]);
337 // Wait until default wallpaper has been loaded.
338 RunUntilWallpaperChangeCount(1);
340 // Set wallpaper policy to red image.
341 InjectPolicy(0, kRedImageFileName
);
343 // Run until wallpaper has changed.
344 RunUntilWallpaperChangeCount(2);
345 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
348 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
349 DISABLED_PRE_WallpaperOnLoginScreen
) {
350 LoginUser(kTestUsers
[1]);
352 // Wait until default wallpaper has been loaded.
353 RunUntilWallpaperChangeCount(1);
355 // Set wallpaper policy to green image.
356 InjectPolicy(1, kGreenImageFileName
);
358 // Run until wallpaper has changed.
359 RunUntilWallpaperChangeCount(2);
360 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
363 // Disabled due to flakiness: http://crbug.com/385648.
364 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
,
365 DISABLED_WallpaperOnLoginScreen
) {
366 // Wait for active pod's wallpaper to be loaded.
367 RunUntilWallpaperChangeCount(1);
368 ASSERT_EQ(kGreenImageColor
, GetAverageBackgroundColor());
370 // Select the second pod (belonging to user 1).
371 ASSERT_TRUE(content::ExecuteScript(
372 static_cast<chromeos::LoginDisplayHostImpl
*>(
373 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->
374 web_ui()->GetWebContents(),
375 "document.getElementsByClassName('pod')[1].focus();"));
376 RunUntilWallpaperChangeCount(2);
377 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
380 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_PRE_PersistOverLogout
) {
381 RegisterUser(kTestUsers
[0]);
382 StartupUtils::MarkOobeCompleted();
385 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PRE_PersistOverLogout
) {
386 LoginUser(kTestUsers
[0]);
388 // Wait until default wallpaper has been loaded.
389 RunUntilWallpaperChangeCount(1);
391 // Set wallpaper policy to red image.
392 InjectPolicy(0, kRedImageFileName
);
394 // Run until wallpaper has changed.
395 RunUntilWallpaperChangeCount(2);
396 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
399 IN_PROC_BROWSER_TEST_F(WallpaperManagerPolicyTest
, PersistOverLogout
) {
400 LoginUser(kTestUsers
[0]);
402 // Wait until wallpaper has been loaded.
403 RunUntilWallpaperChangeCount(1);
404 ASSERT_EQ(kRedImageColor
, GetAverageBackgroundColor());
407 } // namespace chromeos