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/files/file_path.h"
14 #include "base/files/file_util.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/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.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 void SetUpInProcessBrowserTestFixture() override
;
127 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 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
159 void SetUpOnMainThread() override
;
160 void TearDownOnMainThread() override
;
163 DISALLOW_COPY_AND_ASSIGN(PowerPolicyLoginScreenBrowserTest
);
166 class PowerPolicyInSessionBrowserTest
: public PowerPolicyBrowserTestBase
{
168 PowerPolicyInSessionBrowserTest();
170 // PowerPolicyBrowserTestBase:
171 void SetUpOnMainThread() override
;
174 DISALLOW_COPY_AND_ASSIGN(PowerPolicyInSessionBrowserTest
);
177 PowerPolicyBrowserTestBase::PowerPolicyBrowserTestBase()
178 : power_manager_client_(NULL
) {
181 void PowerPolicyBrowserTestBase::SetUpInProcessBrowserTestFixture() {
182 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
183 power_manager_client_
= new chromeos::FakePowerManagerClient
;
184 dbus_setter()->SetPowerManagerClient(
185 scoped_ptr
<chromeos::PowerManagerClient
>(power_manager_client_
));
187 // Initialize device policy.
189 MarkAsEnterpriseOwned();
192 void PowerPolicyBrowserTestBase::SetUpOnMainThread() {
193 DevicePolicyCrosBrowserTest::SetUpOnMainThread();
195 // Initialize user policy.
197 user_policy_
.policy_data().set_username(chromeos::login::kStubUser
);
200 void PowerPolicyBrowserTestBase::InstallUserKey() {
201 base::FilePath user_keys_dir
;
202 ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS
, &user_keys_dir
));
203 std::string sanitized_username
=
204 chromeos::CryptohomeClient::GetStubSanitizedUsername(
205 chromeos::login::kStubUser
);
206 base::FilePath user_key_file
=
207 user_keys_dir
.AppendASCII(sanitized_username
)
208 .AppendASCII("policy.pub");
209 std::vector
<uint8
> user_key_bits
;
210 ASSERT_TRUE(user_policy_
.GetSigningKey()->ExportPublicKey(&user_key_bits
));
211 ASSERT_TRUE(base::CreateDirectory(user_key_file
.DirName()));
212 ASSERT_EQ(base::WriteFile(
214 reinterpret_cast<const char*>(user_key_bits
.data()),
215 user_key_bits
.size()),
216 static_cast<int>(user_key_bits
.size()));
219 void PowerPolicyBrowserTestBase::StoreAndReloadUserPolicy() {
220 // Install the new user policy blob in session manager client.
221 user_policy_
.Build();
222 session_manager_client()->set_user_policy(
223 user_policy_
.policy_data().username(),
224 user_policy_
.GetBlob());
226 // Reload user policy from session manager client and wait for the update to
228 RunClosureAndWaitForUserPolicyUpdate(
229 base::Bind(&PowerPolicyBrowserTestBase::ReloadUserPolicy
, this,
230 browser()->profile()),
231 browser()->profile());
234 void PowerPolicyBrowserTestBase::
235 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange() {
236 Profile
* profile
= chromeos::ProfileHelper::GetSigninProfile();
237 ASSERT_TRUE(profile
);
239 // Install the new device policy blob in session manager client, reload device
240 // policy from session manager client and wait for a change in the login
241 // profile's policy to be observed.
242 RunClosureAndWaitForUserPolicyUpdate(
243 base::Bind(&PowerPolicyBrowserTestBase::RefreshDevicePolicy
, this),
247 std::string
PowerPolicyBrowserTestBase::GetDebugString(
248 const pm::PowerManagementPolicy
& policy
) {
249 return chromeos::PowerPolicyController::GetPolicyDebugString(policy
);
252 void PowerPolicyBrowserTestBase::RunClosureAndWaitForUserPolicyUpdate(
253 const base::Closure
& closure
,
255 base::RunLoop run_loop
;
256 MockPolicyServiceObserver observer
;
257 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
))
258 .WillOnce(InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
));
259 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(AnyNumber());
260 PolicyService
* policy_service
=
261 ProfilePolicyConnectorFactory::GetForBrowserContext(profile
)
263 ASSERT_TRUE(policy_service
);
264 policy_service
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
267 policy_service
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
270 void PowerPolicyBrowserTestBase::ReloadUserPolicy(Profile
* profile
) {
271 UserCloudPolicyManagerChromeOS
* policy_manager
=
272 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
273 ASSERT_TRUE(policy_manager
);
274 policy_manager
->core()->store()->Load();
277 PowerPolicyLoginScreenBrowserTest::PowerPolicyLoginScreenBrowserTest() {
280 void PowerPolicyLoginScreenBrowserTest::SetUpCommandLine(
281 base::CommandLine
* command_line
) {
282 PowerPolicyBrowserTestBase::SetUpCommandLine(command_line
);
283 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
284 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
287 void PowerPolicyLoginScreenBrowserTest::SetUpOnMainThread() {
288 PowerPolicyBrowserTestBase::SetUpOnMainThread();
290 // Wait for the login screen to be shown.
291 content::WindowedNotificationObserver(
292 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
293 content::NotificationService::AllSources()).Wait();
296 void PowerPolicyLoginScreenBrowserTest::TearDownOnMainThread() {
297 base::MessageLoop::current()->PostTask(FROM_HERE
,
298 base::Bind(&chrome::AttemptExit
));
299 base::RunLoop().RunUntilIdle();
300 PowerPolicyBrowserTestBase::TearDownOnMainThread();
303 PowerPolicyInSessionBrowserTest::PowerPolicyInSessionBrowserTest() {
306 void PowerPolicyInSessionBrowserTest::SetUpOnMainThread() {
307 PowerPolicyBrowserTestBase::SetUpOnMainThread();
310 // Verifies that device policy is applied on the login screen.
311 IN_PROC_BROWSER_TEST_F(PowerPolicyLoginScreenBrowserTest
, SetDevicePolicy
) {
312 pm::PowerManagementPolicy power_management_policy
=
313 power_manager_client_
->policy();
314 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
315 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
316 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
317 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
318 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
319 power_management_policy
.mutable_battery_delays()->set_idle_ms(4000);
320 power_management_policy
.set_ac_idle_action(
321 pm::PowerManagementPolicy::DO_NOTHING
);
322 power_management_policy
.set_battery_idle_action(
323 pm::PowerManagementPolicy::DO_NOTHING
);
324 power_management_policy
.set_lid_closed_action(
325 pm::PowerManagementPolicy::DO_NOTHING
);
326 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
328 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
329 proto
.mutable_login_screen_power_management()->
330 set_login_screen_power_management(kLoginScreenPowerManagementPolicy
);
331 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
332 EXPECT_EQ(GetDebugString(power_management_policy
),
333 GetDebugString(power_manager_client_
->policy()));
336 // Verifies that device policy is ignored during a session.
337 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetDevicePolicy
) {
338 pm::PowerManagementPolicy power_management_policy
=
339 power_manager_client_
->policy();
341 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
342 proto
.mutable_login_screen_power_management()->
343 set_login_screen_power_management(kLoginScreenPowerManagementPolicy
);
344 StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
345 EXPECT_EQ(GetDebugString(power_management_policy
),
346 GetDebugString(power_manager_client_
->policy()));
349 // Verifies that legacy user policy is applied during a session.
350 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetLegacyUserPolicy
) {
351 pm::PowerManagementPolicy power_management_policy
=
352 power_manager_client_
->policy();
353 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
354 power_management_policy
.mutable_ac_delays()->set_screen_lock_ms(6000);
355 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
356 power_management_policy
.mutable_ac_delays()->set_idle_warning_ms(8000);
357 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
358 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
359 power_management_policy
.mutable_battery_delays()->set_screen_lock_ms(2000);
360 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
361 power_management_policy
.mutable_battery_delays()->set_idle_warning_ms(4000);
362 power_management_policy
.mutable_battery_delays()->set_idle_ms(5000);
363 power_management_policy
.set_use_audio_activity(false);
364 power_management_policy
.set_use_video_activity(false);
365 power_management_policy
.set_ac_idle_action(
366 pm::PowerManagementPolicy::STOP_SESSION
);
367 power_management_policy
.set_battery_idle_action(
368 pm::PowerManagementPolicy::STOP_SESSION
);
369 power_management_policy
.set_lid_closed_action(
370 pm::PowerManagementPolicy::STOP_SESSION
);
371 power_management_policy
.set_presentation_screen_dim_delay_factor(3.0);
372 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
373 power_management_policy
.set_wait_for_initial_user_activity(true);
375 user_policy_
.payload().mutable_screendimdelayac()->set_value(5000);
376 user_policy_
.payload().mutable_screenlockdelayac()->set_value(6000);
377 user_policy_
.payload().mutable_screenoffdelayac()->set_value(7000);
378 user_policy_
.payload().mutable_idlewarningdelayac()->set_value(8000);
379 user_policy_
.payload().mutable_idledelayac()->set_value(9000);
380 user_policy_
.payload().mutable_screendimdelaybattery()->set_value(1000);
381 user_policy_
.payload().mutable_screenlockdelaybattery()->set_value(2000);
382 user_policy_
.payload().mutable_screenoffdelaybattery()->set_value(3000);
383 user_policy_
.payload().mutable_idlewarningdelaybattery()->set_value(4000);
384 user_policy_
.payload().mutable_idledelaybattery()->set_value(5000);
385 user_policy_
.payload().mutable_powermanagementusesaudioactivity()->set_value(
387 user_policy_
.payload().mutable_powermanagementusesvideoactivity()->set_value(
389 user_policy_
.payload().mutable_idleactionac()->set_value(
390 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
391 user_policy_
.payload().mutable_idleactionbattery()->set_value(
392 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
393 user_policy_
.payload().mutable_lidcloseaction()->set_value(
394 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
395 user_policy_
.payload().mutable_presentationscreendimdelayscale()->set_value(
397 user_policy_
.payload().mutable_useractivityscreendimdelayscale()->set_value(
399 user_policy_
.payload().mutable_waitforinitialuseractivity()->set_value(true);
400 StoreAndReloadUserPolicy();
401 EXPECT_EQ(GetDebugString(power_management_policy
),
402 GetDebugString(power_manager_client_
->policy()));
405 // Verifies that user policy is applied during a session.
406 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, SetUserPolicy
) {
407 pm::PowerManagementPolicy power_management_policy
=
408 power_manager_client_
->policy();
409 power_management_policy
.mutable_ac_delays()->set_screen_dim_ms(5000);
410 power_management_policy
.mutable_ac_delays()->set_screen_lock_ms(6000);
411 power_management_policy
.mutable_ac_delays()->set_screen_off_ms(7000);
412 power_management_policy
.mutable_ac_delays()->set_idle_warning_ms(8000);
413 power_management_policy
.mutable_ac_delays()->set_idle_ms(9000);
414 power_management_policy
.mutable_battery_delays()->set_screen_dim_ms(1000);
415 power_management_policy
.mutable_battery_delays()->set_screen_lock_ms(2000);
416 power_management_policy
.mutable_battery_delays()->set_screen_off_ms(3000);
417 power_management_policy
.mutable_battery_delays()->set_idle_warning_ms(4000);
418 power_management_policy
.mutable_battery_delays()->set_idle_ms(5000);
419 power_management_policy
.set_use_audio_activity(false);
420 power_management_policy
.set_use_video_activity(false);
421 power_management_policy
.set_ac_idle_action(
422 pm::PowerManagementPolicy::STOP_SESSION
);
423 power_management_policy
.set_battery_idle_action(
424 pm::PowerManagementPolicy::STOP_SESSION
);
425 power_management_policy
.set_lid_closed_action(
426 pm::PowerManagementPolicy::STOP_SESSION
);
427 power_management_policy
.set_presentation_screen_dim_delay_factor(3.0);
428 power_management_policy
.set_user_activity_screen_dim_delay_factor(3.0);
429 power_management_policy
.set_wait_for_initial_user_activity(true);
431 // Set legacy policies which are expected to be ignored.
432 user_policy_
.payload().mutable_screendimdelayac()->set_value(5555);
433 user_policy_
.payload().mutable_screenlockdelayac()->set_value(6666);
434 user_policy_
.payload().mutable_screenoffdelayac()->set_value(7777);
435 user_policy_
.payload().mutable_idlewarningdelayac()->set_value(8888);
436 user_policy_
.payload().mutable_idledelayac()->set_value(9999);
437 user_policy_
.payload().mutable_screendimdelaybattery()->set_value(1111);
438 user_policy_
.payload().mutable_screenlockdelaybattery()->set_value(2222);
439 user_policy_
.payload().mutable_screenoffdelaybattery()->set_value(3333);
440 user_policy_
.payload().mutable_idlewarningdelaybattery()->set_value(4444);
441 user_policy_
.payload().mutable_idledelaybattery()->set_value(5555);
442 user_policy_
.payload().mutable_idleactionac()->set_value(
443 chromeos::PowerPolicyController::ACTION_SHUT_DOWN
);
444 user_policy_
.payload().mutable_idleactionbattery()->set_value(
445 chromeos::PowerPolicyController::ACTION_DO_NOTHING
);
447 // Set current policies which are expected to be honored.
448 user_policy_
.payload().mutable_powermanagementusesaudioactivity()->set_value(
450 user_policy_
.payload().mutable_powermanagementusesvideoactivity()->set_value(
452 user_policy_
.payload().mutable_lidcloseaction()->set_value(
453 chromeos::PowerPolicyController::ACTION_STOP_SESSION
);
454 user_policy_
.payload().mutable_presentationscreendimdelayscale()->set_value(
456 user_policy_
.payload().mutable_useractivityscreendimdelayscale()->set_value(
458 user_policy_
.payload().mutable_waitforinitialuseractivity()->set_value(true);
460 user_policy_
.payload().mutable_powermanagementidlesettings()->set_value(
461 kPowerManagementIdleSettingsPolicy
);
462 user_policy_
.payload().mutable_screenlockdelays()->set_value(
463 kScreenLockDelayPolicy
);
465 StoreAndReloadUserPolicy();
466 EXPECT_EQ(GetDebugString(power_management_policy
),
467 GetDebugString(power_manager_client_
->policy()));
470 // Verifies that screen wake locks can be enabled and disabled by extensions and
471 // user policy during a session.
472 IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest
, AllowScreenWakeLocks
) {
473 pm::PowerManagementPolicy baseline_policy
=
474 power_manager_client_
->policy();
476 // Default settings should have delays.
477 pm::PowerManagementPolicy power_management_policy
= baseline_policy
;
478 EXPECT_NE(0, baseline_policy
.ac_delays().screen_dim_ms());
479 EXPECT_NE(0, baseline_policy
.ac_delays().screen_off_ms());
480 EXPECT_NE(0, baseline_policy
.battery_delays().screen_dim_ms());
481 EXPECT_NE(0, baseline_policy
.battery_delays().screen_off_ms());
483 // Pretend an extension grabs a screen wake lock.
484 const char kExtensionId
[] = "abcdefghijklmnopabcdefghijlkmnop";
485 extensions::PowerAPI::Get(browser()->profile())
486 ->AddRequest(kExtensionId
, extensions::core_api::power::LEVEL_DISPLAY
);
487 base::RunLoop().RunUntilIdle();
489 // Check that the lock is in effect (ignoring ac_idle_action,
490 // battery_idle_action and reason).
491 pm::PowerManagementPolicy policy
= baseline_policy
;
492 policy
.mutable_ac_delays()->set_screen_dim_ms(0);
493 policy
.mutable_ac_delays()->set_screen_off_ms(0);
494 policy
.mutable_battery_delays()->set_screen_dim_ms(0);
495 policy
.mutable_battery_delays()->set_screen_off_ms(0);
496 policy
.set_ac_idle_action(
497 power_manager_client_
->policy().ac_idle_action());
498 policy
.set_battery_idle_action(
499 power_manager_client_
->policy().battery_idle_action());
500 policy
.set_reason(power_manager_client_
->policy().reason());
501 EXPECT_EQ(GetDebugString(policy
),
502 GetDebugString(power_manager_client_
->policy()));
504 // Engage the user policy and verify that the defaults take effect again.
505 user_policy_
.payload().mutable_allowscreenwakelocks()->set_value(false);
506 StoreAndReloadUserPolicy();
507 policy
= baseline_policy
;
508 policy
.set_ac_idle_action(power_manager_client_
->policy().ac_idle_action());
509 policy
.set_battery_idle_action(
510 power_manager_client_
->policy().battery_idle_action());
511 policy
.set_reason(power_manager_client_
->policy().reason());
512 EXPECT_EQ(GetDebugString(policy
),
513 GetDebugString(power_manager_client_
->policy()));
516 } // namespace policy