1 // Copyright (c) 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 "base/basictypes.h"
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/location.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/path_service.h"
18 #include "base/run_loop.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
21 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
22 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
23 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
24 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
25 #include "chrome/browser/chromeos/profiles/profile_helper.h"
26 #include "chrome/browser/lifetime/application_lifetime.h"
27 #include "chrome/browser/policy/profile_policy_connector.h"
28 #include "chrome/browser/policy/profile_policy_connector_factory.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/ui/browser.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "chromeos/chromeos_paths.h"
34 #include "chromeos/chromeos_switches.h"
35 #include "chromeos/dbus/cryptohome_client.h"
36 #include "chromeos/dbus/fake_dbus_thread_manager.h"
37 #include "chromeos/dbus/fake_power_manager_client.h"
38 #include "chromeos/dbus/fake_session_manager_client.h"
39 #include "chromeos/dbus/power_manager/policy.pb.h"
40 #include "chromeos/dbus/power_policy_controller.h"
41 #include "chromeos/login/user_names.h"
42 #include "components/policy/core/common/cloud/cloud_policy_core.h"
43 #include "components/policy/core/common/cloud/cloud_policy_store.h"
44 #include "components/policy/core/common/cloud/policy_builder.h"
45 #include "components/policy/core/common/external_data_fetcher.h"
46 #include "components/policy/core/common/mock_policy_service.h"
47 #include "components/policy/core/common/policy_service.h"
48 #include "content/public/browser/notification_details.h"
49 #include "content/public/browser/notification_service.h"
50 #include "content/public/browser/notification_source.h"
51 #include "content/public/test/test_utils.h"
52 #include "extensions/browser/api/power/power_api_manager.h"
53 #include "extensions/common/api/power.h"
54 #include "policy/proto/device_management_backend.pb.h"
55 #include "testing/gmock/include/gmock/gmock.h"
56 #include "testing/gtest/include/gtest/gtest.h"
58 namespace em
= enterprise_management
;
59 namespace pm
= power_manager
;
61 using ::testing::AnyNumber
;
62 using ::testing::InvokeWithoutArgs
;
69 const char kLoginScreenPowerManagementPolicy
[] =
73 " \"ScreenDim\": 5000,"
74 " \"ScreenOff\": 7000,"
77 " \"IdleAction\": \"DoNothing\""
81 " \"ScreenDim\": 1000,"
82 " \"ScreenOff\": 3000,"
85 " \"IdleAction\": \"DoNothing\""
87 " \"LidCloseAction\": \"DoNothing\","
88 " \"UserActivityScreenDimDelayScale\": 300"
91 const char kPowerManagementIdleSettingsPolicy
[] =
95 " \"ScreenDim\": 5000,"
96 " \"ScreenOff\": 7000,"
97 " \"IdleWarning\": 8000,"
100 " \"IdleAction\": \"Logout\""
104 " \"ScreenDim\": 1000,"
105 " \"ScreenOff\": 3000,"
106 " \"IdleWarning\": 4000,"
109 " \"IdleAction\": \"Logout\""
113 const char kScreenLockDelayPolicy
[] =
121 class PowerPolicyBrowserTestBase
: public DevicePolicyCrosBrowserTest
{
123 PowerPolicyBrowserTestBase();
125 // DevicePolicyCrosBrowserTest:
126 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
;
127 virtual void SetUpOnMainThread() OVERRIDE
;
129 void InstallUserKey();
130 void StoreAndReloadUserPolicy();
132 void StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
134 // Returns a string describing |policy|.
135 std::string
GetDebugString(const pm::PowerManagementPolicy
& policy
);
137 UserPolicyBuilder user_policy_
;
139 chromeos::FakePowerManagerClient
* power_manager_client_
;
142 // Runs |closure| and waits for |profile|'s user policy to be updated as a
144 void RunClosureAndWaitForUserPolicyUpdate(const base::Closure
& closure
,
147 // Reloads user policy for |profile| from session manager client.
148 void ReloadUserPolicy(Profile
* profile
);
150 DISALLOW_COPY_AND_ASSIGN(PowerPolicyBrowserTestBase
);
153 class PowerPolicyLoginScreenBrowserTest
: public PowerPolicyBrowserTestBase
{
155 PowerPolicyLoginScreenBrowserTest();
157 // PowerPolicyBrowserTestBase:
158 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
;
159 virtual void SetUpOnMainThread() OVERRIDE
;
160 virtual void CleanUpOnMainThread() OVERRIDE
;
162 DISALLOW_COPY_AND_ASSIGN(PowerPolicyLoginScreenBrowserTest
);
165 class PowerPolicyInSessionBrowserTest
: public PowerPolicyBrowserTestBase
{
167 PowerPolicyInSessionBrowserTest();
169 // PowerPolicyBrowserTestBase:
170 virtual void SetUpOnMainThread() OVERRIDE
;
172 DISALLOW_COPY_AND_ASSIGN(PowerPolicyInSessionBrowserTest
);
175 PowerPolicyBrowserTestBase::PowerPolicyBrowserTestBase()
176 : power_manager_client_(NULL
) {
179 void PowerPolicyBrowserTestBase::SetUpInProcessBrowserTestFixture() {
180 power_manager_client_
= new chromeos::FakePowerManagerClient
;
181 fake_dbus_thread_manager()->SetPowerManagerClient(
182 scoped_ptr
<chromeos::PowerManagerClient
>(power_manager_client_
));
184 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
186 // Initialize device policy.
188 MarkAsEnterpriseOwned();
191 void PowerPolicyBrowserTestBase::SetUpOnMainThread() {
192 DevicePolicyCrosBrowserTest::SetUpOnMainThread();
194 // Initialize user policy.
196 user_policy_
.policy_data().set_username(chromeos::login::kStubUser
);
199 void PowerPolicyBrowserTestBase::InstallUserKey() {
200 base::FilePath user_keys_dir
;
201 ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS
, &user_keys_dir
));
202 std::string sanitized_username
=
203 chromeos::CryptohomeClient::GetStubSanitizedUsername(
204 chromeos::login::kStubUser
);
205 base::FilePath user_key_file
=
206 user_keys_dir
.AppendASCII(sanitized_username
)
207 .AppendASCII("policy.pub");
208 std::vector
<uint8
> user_key_bits
;
209 ASSERT_TRUE(user_policy_
.GetSigningKey()->ExportPublicKey(&user_key_bits
));
210 ASSERT_TRUE(base::CreateDirectory(user_key_file
.DirName()));
211 ASSERT_EQ(base::WriteFile(
213 reinterpret_cast<const char*>(user_key_bits
.data()),
214 user_key_bits
.size()),
215 static_cast<int>(user_key_bits
.size()));
218 void PowerPolicyBrowserTestBase::StoreAndReloadUserPolicy() {
219 // Install the new user policy blob in session manager client.
220 user_policy_
.Build();
221 session_manager_client()->set_user_policy(
222 user_policy_
.policy_data().username(),
223 user_policy_
.GetBlob());
225 // Reload user policy from session manager client and wait for the update to
227 RunClosureAndWaitForUserPolicyUpdate(
228 base::Bind(&PowerPolicyBrowserTestBase::ReloadUserPolicy
, this,
229 browser()->profile()),
230 browser()->profile());
233 void PowerPolicyBrowserTestBase::
234 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange() {
235 Profile
* profile
= chromeos::ProfileHelper::GetSigninProfile();
236 ASSERT_TRUE(profile
);
238 // Install the new device policy blob in session manager client, reload device
239 // policy from session manager client and wait for a change in the login
240 // profile's policy to be observed.
241 RunClosureAndWaitForUserPolicyUpdate(
242 base::Bind(&PowerPolicyBrowserTestBase::RefreshDevicePolicy
, this),
246 std::string
PowerPolicyBrowserTestBase::GetDebugString(
247 const pm::PowerManagementPolicy
& policy
) {
248 return chromeos::PowerPolicyController::GetPolicyDebugString(policy
);
251 void PowerPolicyBrowserTestBase::RunClosureAndWaitForUserPolicyUpdate(
252 const base::Closure
& closure
,
254 base::RunLoop run_loop
;
255 MockPolicyServiceObserver observer
;
256 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
))
257 .WillOnce(InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
));
258 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(AnyNumber());
259 PolicyService
* policy_service
=
260 ProfilePolicyConnectorFactory::GetForProfile(profile
)->policy_service();
261 ASSERT_TRUE(policy_service
);
262 policy_service
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
265 policy_service
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
268 void PowerPolicyBrowserTestBase::ReloadUserPolicy(Profile
* profile
) {
269 UserCloudPolicyManagerChromeOS
* policy_manager
=
270 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
271 ASSERT_TRUE(policy_manager
);
272 policy_manager
->core()->store()->Load();
275 PowerPolicyLoginScreenBrowserTest::PowerPolicyLoginScreenBrowserTest() {
278 void PowerPolicyLoginScreenBrowserTest::SetUpCommandLine(
279 CommandLine
* command_line
) {
280 PowerPolicyBrowserTestBase::SetUpCommandLine(command_line
);
281 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
282 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
285 void PowerPolicyLoginScreenBrowserTest::SetUpOnMainThread() {
286 PowerPolicyBrowserTestBase::SetUpOnMainThread();
288 // Wait for the login screen to be shown.
289 content::WindowedNotificationObserver(
290 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
291 content::NotificationService::AllSources()).Wait();
294 void PowerPolicyLoginScreenBrowserTest::CleanUpOnMainThread() {
295 base::MessageLoop::current()->PostTask(FROM_HERE
,
296 base::Bind(&chrome::AttemptExit
));
297 base::RunLoop().RunUntilIdle();
298 PowerPolicyBrowserTestBase::CleanUpOnMainThread();
301 PowerPolicyInSessionBrowserTest::PowerPolicyInSessionBrowserTest() {
304 void PowerPolicyInSessionBrowserTest::SetUpOnMainThread() {
305 PowerPolicyBrowserTestBase::SetUpOnMainThread();
308 // Verifies that device policy is applied on the login screen.
309 IN_PROC_BROWSER_TEST_F(PowerPolicyLoginScreenBrowserTest
, SetDevicePolicy
) {
310 pm::PowerManagementPolicy power_management_policy
=
311 power_manager_client_
->policy();
312 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
313 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
314 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
315 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
316 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
317 power_management_policy
.mutable_battery_delays()->set_idle_ms(4000);
318 power_management_policy
.set_ac_idle_action(
319 pm::PowerManagementPolicy::DO_NOTHING
);
320 power_management_policy
.set_battery_idle_action(
321 pm::PowerManagementPolicy::DO_NOTHING
);
322 power_management_policy
.set_lid_closed_action(
323 pm::PowerManagementPolicy::DO_NOTHING
);
324 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
326 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
327 proto
.mutable_login_screen_power_management()->
328 set_login_screen_power_management(kLoginScreenPowerManagementPolicy
);
329 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
330 EXPECT_EQ(GetDebugString(power_management_policy
),
331 GetDebugString(power_manager_client_
->policy()));
334 // Verifies that device policy is ignored during a session.
335 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetDevicePolicy
) {
336 pm::PowerManagementPolicy power_management_policy
=
337 power_manager_client_
->policy();
339 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
340 proto
.mutable_login_screen_power_management()->
341 set_login_screen_power_management(kLoginScreenPowerManagementPolicy
);
342 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
343 EXPECT_EQ(GetDebugString(power_management_policy
),
344 GetDebugString(power_manager_client_
->policy()));
347 // Verifies that legacy user policy is applied during a session.
348 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetLegacyUserPolicy
) {
349 pm::PowerManagementPolicy power_management_policy
=
350 power_manager_client_
->policy();
351 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
352 power_management_policy
.mutable_ac_delays()->set_screen_lock_ms(6000);
353 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
354 power_management_policy
.mutable_ac_delays()->set_idle_warning_ms(8000);
355 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
356 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
357 power_management_policy
.mutable_battery_delays()->set_screen_lock_ms(2000);
358 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
359 power_management_policy
.mutable_battery_delays()->set_idle_warning_ms(4000);
360 power_management_policy
.mutable_battery_delays()->set_idle_ms(5000);
361 power_management_policy
.set_use_audio_activity(false);
362 power_management_policy
.set_use_video_activity(false);
363 power_management_policy
.set_ac_idle_action(
364 pm::PowerManagementPolicy::STOP_SESSION
);
365 power_management_policy
.set_battery_idle_action(
366 pm::PowerManagementPolicy::STOP_SESSION
);
367 power_management_policy
.set_lid_closed_action(
368 pm::PowerManagementPolicy::STOP_SESSION
);
369 power_management_policy
.set_presentation_screen_dim_delay_factor(3.0);
370 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
371 power_management_policy
.set_wait_for_initial_user_activity(true);
373 user_policy_
.payload().mutable_screendimdelayac()->set_value(5000);
374 user_policy_
.payload().mutable_screenlockdelayac()->set_value(6000);
375 user_policy_
.payload().mutable_screenoffdelayac()->set_value(7000);
376 user_policy_
.payload().mutable_idlewarningdelayac()->set_value(8000);
377 user_policy_
.payload().mutable_idledelayac()->set_value(9000);
378 user_policy_
.payload().mutable_screendimdelaybattery()->set_value(1000);
379 user_policy_
.payload().mutable_screenlockdelaybattery()->set_value(2000);
380 user_policy_
.payload().mutable_screenoffdelaybattery()->set_value(3000);
381 user_policy_
.payload().mutable_idlewarningdelaybattery()->set_value(4000);
382 user_policy_
.payload().mutable_idledelaybattery()->set_value(5000);
383 user_policy_
.payload().mutable_powermanagementusesaudioactivity()->set_value(
385 user_policy_
.payload().mutable_powermanagementusesvideoactivity()->set_value(
387 user_policy_
.payload().mutable_idleactionac()->set_value(
388 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
389 user_policy_
.payload().mutable_idleactionbattery()->set_value(
390 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
391 user_policy_
.payload().mutable_lidcloseaction()->set_value(
392 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
393 user_policy_
.payload().mutable_presentationscreendimdelayscale()->set_value(
395 user_policy_
.payload().mutable_useractivityscreendimdelayscale()->set_value(
397 user_policy_
.payload().mutable_waitforinitialuseractivity()->set_value(true);
398 StoreAndReloadUserPolicy();
399 EXPECT_EQ(GetDebugString(power_management_policy
),
400 GetDebugString(power_manager_client_
->policy()));
403 // Verifies that user policy is applied during a session.
404 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetUserPolicy
) {
405 pm::PowerManagementPolicy power_management_policy
=
406 power_manager_client_
->policy();
407 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
408 power_management_policy
.mutable_ac_delays()->set_screen_lock_ms(6000);
409 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
410 power_management_policy
.mutable_ac_delays()->set_idle_warning_ms(8000);
411 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
412 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
413 power_management_policy
.mutable_battery_delays()->set_screen_lock_ms(2000);
414 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
415 power_management_policy
.mutable_battery_delays()->set_idle_warning_ms(4000);
416 power_management_policy
.mutable_battery_delays()->set_idle_ms(5000);
417 power_management_policy
.set_use_audio_activity(false);
418 power_management_policy
.set_use_video_activity(false);
419 power_management_policy
.set_ac_idle_action(
420 pm::PowerManagementPolicy::STOP_SESSION
);
421 power_management_policy
.set_battery_idle_action(
422 pm::PowerManagementPolicy::STOP_SESSION
);
423 power_management_policy
.set_lid_closed_action(
424 pm::PowerManagementPolicy::STOP_SESSION
);
425 power_management_policy
.set_presentation_screen_dim_delay_factor(3.0);
426 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
427 power_management_policy
.set_wait_for_initial_user_activity(true);
429 // Set legacy policies which are expected to be ignored.
430 user_policy_
.payload().mutable_screendimdelayac()->set_value(5555);
431 user_policy_
.payload().mutable_screenlockdelayac()->set_value(6666);
432 user_policy_
.payload().mutable_screenoffdelayac()->set_value(7777);
433 user_policy_
.payload().mutable_idlewarningdelayac()->set_value(8888);
434 user_policy_
.payload().mutable_idledelayac()->set_value(9999);
435 user_policy_
.payload().mutable_screendimdelaybattery()->set_value(1111);
436 user_policy_
.payload().mutable_screenlockdelaybattery()->set_value(2222);
437 user_policy_
.payload().mutable_screenoffdelaybattery()->set_value(3333);
438 user_policy_
.payload().mutable_idlewarningdelaybattery()->set_value(4444);
439 user_policy_
.payload().mutable_idledelaybattery()->set_value(5555);
440 user_policy_
.payload().mutable_idleactionac()->set_value(
441 chromeos::PowerPolicyController::ACTION_SHUT_DOWN
);
442 user_policy_
.payload().mutable_idleactionbattery()->set_value(
443 chromeos::PowerPolicyController::ACTION_DO_NOTHING
);
445 // Set current policies which are expected to be honored.
446 user_policy_
.payload().mutable_powermanagementusesaudioactivity()->set_value(
448 user_policy_
.payload().mutable_powermanagementusesvideoactivity()->set_value(
450 user_policy_
.payload().mutable_lidcloseaction()->set_value(
451 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
452 user_policy_
.payload().mutable_presentationscreendimdelayscale()->set_value(
454 user_policy_
.payload().mutable_useractivityscreendimdelayscale()->set_value(
456 user_policy_
.payload().mutable_waitforinitialuseractivity()->set_value(true);
458 user_policy_
.payload().mutable_powermanagementidlesettings()->set_value(
459 kPowerManagementIdleSettingsPolicy
);
460 user_policy_
.payload().mutable_screenlockdelays()->set_value(
461 kScreenLockDelayPolicy
);
463 StoreAndReloadUserPolicy();
464 EXPECT_EQ(GetDebugString(power_management_policy
),
465 GetDebugString(power_manager_client_
->policy()));
468 // Verifies that screen wake locks can be enabled and disabled by extensions and
469 // user policy during a session.
470 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, AllowScreenWakeLocks
) {
471 pm::PowerManagementPolicy baseline_policy
=
472 power_manager_client_
->policy();
474 // Default settings should have delays.
475 pm::PowerManagementPolicy power_management_policy
= baseline_policy
;
476 EXPECT_NE(0, baseline_policy
.ac_delays().screen_dim_ms());
477 EXPECT_NE(0, baseline_policy
.ac_delays().screen_off_ms());
478 EXPECT_NE(0, baseline_policy
.battery_delays().screen_dim_ms());
479 EXPECT_NE(0, baseline_policy
.battery_delays().screen_off_ms());
481 // Pretend an extension grabs a screen wake lock.
482 const char kExtensionId
[] = "abcdefghijklmnopabcdefghijlkmnop";
483 extensions::PowerApiManager::Get(browser()->profile())->AddRequest(
484 kExtensionId
, extensions::core_api::power::LEVEL_DISPLAY
);
485 base::RunLoop().RunUntilIdle();
487 // Check that the lock is in effect (ignoring ac_idle_action,
488 // battery_idle_action and reason).
489 pm::PowerManagementPolicy policy
= baseline_policy
;
490 policy
.mutable_ac_delays()->set_screen_dim_ms(0);
491 policy
.mutable_ac_delays()->set_screen_off_ms(0);
492 policy
.mutable_battery_delays()->set_screen_dim_ms(0);
493 policy
.mutable_battery_delays()->set_screen_off_ms(0);
494 policy
.set_ac_idle_action(
495 power_manager_client_
->policy().ac_idle_action());
496 policy
.set_battery_idle_action(
497 power_manager_client_
->policy().battery_idle_action());
498 policy
.set_reason(power_manager_client_
->policy().reason());
499 EXPECT_EQ(GetDebugString(policy
),
500 GetDebugString(power_manager_client_
->policy()));
502 // Engage the user policy and verify that the defaults take effect again.
503 user_policy_
.payload().mutable_allowscreenwakelocks()->set_value(false);
504 StoreAndReloadUserPolicy();
505 policy
= baseline_policy
;
506 policy
.set_ac_idle_action(power_manager_client_
->policy().ac_idle_action());
507 policy
.set_battery_idle_action(
508 power_manager_client_
->policy().battery_idle_action());
509 policy
.set_reason(power_manager_client_
->policy().reason());
510 EXPECT_EQ(GetDebugString(policy
),
511 GetDebugString(power_manager_client_
->policy()));
514 } // namespace policy