power_policy_browsertest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// Use of this source code is governed by a BSD-style license that can be
3cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// found in the LICENSE file.
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include <string>
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include <vector>
7cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "base/basictypes.h"
9cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#include "base/bind.h"
108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/callback.h"
118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/command_line.h"
128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/compiler_specific.h"
138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/file_util.h"
148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/files/file_path.h"
158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/location.h"
168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/message_loop/message_loop.h"
178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/path_service.h"
188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "base/run_loop.h"
198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/browser_process.h"
208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chrome_notification_types.h"
218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/login/user_manager.h"
228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/policy/device_policy_builder.h"
238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/profiles/profile_helper.h"
278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/chromeos/settings/device_settings_service.h"
288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/extensions/api/power/power_api_manager.h"
298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/lifetime/application_lifetime.h"
308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/cloud/cloud_policy_core.h"
318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/cloud/cloud_policy_store.h"
328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/cloud/policy_builder.h"
338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/external_data_fetcher.h"
348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/mock_policy_service.h"
358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/policy_service.h"
368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/profile_policy_connector.h"
378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/profile_policy_connector_factory.h"
388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h"
398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/profiles/profile.h"
418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/browser/profiles/profile_manager.h"
428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/common/extensions/api/power.h"
438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chrome/test/base/testing_profile.h"
448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/chromeos_paths.h"
458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/chromeos_switches.h"
468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/dbus/fake_power_manager_client.h"
478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/dbus/fake_session_manager_client.h"
488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/dbus/power_manager/policy.pb.h"
508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "chromeos/dbus/power_policy_controller.h"
518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "content/public/browser/notification_details.h"
528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "content/public/browser/notification_service.h"
538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "content/public/browser/notification_source.h"
548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "content/public/test/test_utils.h"
558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "crypto/rsa_private_key.h"
568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "testing/gmock/include/gmock/gmock.h"
578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "testing/gtest/include/gtest/gtest.h"
588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
598cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace em = enterprise_management;
608cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace pm = power_manager;
618cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
628cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comusing ::testing::AnyNumber;
638cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comusing ::testing::InvokeWithoutArgs;
648cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comusing ::testing::_;
658cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
668cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace policy {
678cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
688cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace {
698cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
708cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comconst char kLoginScreenPowerManagementPolicy[] =
718cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "{"
728cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  \"AC\": {"
738cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    \"Delays\": {"
748cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"ScreenDim\": 5000,"
758cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"ScreenOff\": 7000,"
768cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"Idle\": 9000"
778cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    },"
788cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    \"IdleAction\": \"DoNothing\""
798cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  },"
808cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  \"Battery\": {"
818cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    \"Delays\": {"
828cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"ScreenDim\": 1000,"
838cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"ScreenOff\": 3000,"
848cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "      \"Idle\": 4000"
858cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    },"
868cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "    \"IdleAction\": \"DoNothing\""
878cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  },"
888cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  \"LidCloseAction\": \"DoNothing\","
898cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "  \"UserActivityScreenDimDelayScale\": 300"
908cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    "}";
918cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
928cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}  // namespace
938cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
948cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass PowerPolicyBrowserTestBase : public DevicePolicyCrosBrowserTest {
958cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com protected:
968cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyBrowserTestBase();
978cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
988cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // DevicePolicyCrosBrowserTest:
998cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
1008cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUpOnMainThread() OVERRIDE;
1018cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1028cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  void InstallUserKey();
1038cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  void StoreAndReloadUserPolicy();
1048cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1058cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  void StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
1068cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1078cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Returns a string describing |policy|.
1088cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  std::string GetDebugString(const pm::PowerManagementPolicy& policy);
1098cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  UserPolicyBuilder user_policy_;
1118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  chromeos::FakePowerManagerClient* power_manager_client_;
1138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com private:
1158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Runs |closure| and waits for |profile|'s user policy to be updated as a
1168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // result.
1178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  void RunClosureAndWaitForUserPolicyUpdate(const base::Closure& closure,
1188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                                            Profile* profile);
1198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Reloads user policy for |profile| from session manager client.
1218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  void ReloadUserPolicy(Profile* profile);
1228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  DISALLOW_COPY_AND_ASSIGN(PowerPolicyBrowserTestBase);
1248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
1258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass PowerPolicyLoginScreenBrowserTest : public PowerPolicyBrowserTestBase {
1278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com protected:
1288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyLoginScreenBrowserTest();
1298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // PowerPolicyBrowserTestBase:
1318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
1328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUpOnMainThread() OVERRIDE;
1338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void CleanUpOnMainThread() OVERRIDE;
1348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  DISALLOW_COPY_AND_ASSIGN(PowerPolicyLoginScreenBrowserTest);
1368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
1378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass PowerPolicyInSessionBrowserTest : public PowerPolicyBrowserTestBase {
1398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com protected:
1408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyInSessionBrowserTest();
1418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // PowerPolicyBrowserTestBase:
1438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual void SetUpOnMainThread() OVERRIDE;
1448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  DISALLOW_COPY_AND_ASSIGN(PowerPolicyInSessionBrowserTest);
1468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
1478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comPowerPolicyBrowserTestBase::PowerPolicyBrowserTestBase()
1498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    : power_manager_client_(NULL) {
1508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
1518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::SetUpInProcessBrowserTestFixture() {
1538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
1548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Initialize device policy.
1568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  InstallOwnerKey();
1578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  MarkAsEnterpriseOwned();
1588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1598cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_manager_client_ =
1608cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      mock_dbus_thread_manager()->fake_power_manager_client();
1618cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
1628cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1638cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::SetUpOnMainThread() {
1648cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  DevicePolicyCrosBrowserTest::SetUpOnMainThread();
1658cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1668cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Initialize user policy.
1678cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  InstallUserKey();
1688cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.policy_data().set_username(chromeos::UserManager::kStubUser);
1698cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
1708cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1718cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::InstallUserKey() {
1728cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::FilePath user_keys_dir;
1738cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &user_keys_dir));
1748cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::FilePath user_key_file =
1758cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      user_keys_dir.AppendASCII(chromeos::UserManager::kStubUser)
1768cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                   .AppendASCII("policy.pub");
1778cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  std::vector<uint8> user_key_bits;
1788cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(user_policy_.signing_key()->ExportPublicKey(&user_key_bits));
1798cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(file_util::CreateDirectory(user_key_file.DirName()));
1808cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_EQ(file_util::WriteFile(
1818cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                user_key_file,
1828cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                reinterpret_cast<const char*>(user_key_bits.data()),
1838cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                user_key_bits.size()),
1848cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            static_cast<int>(user_key_bits.size()));
1858cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
1868cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1878cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::StoreAndReloadUserPolicy() {
1888cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ProfileManager* profile_manager = g_browser_process->profile_manager();
1898cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  Profile* profile = profile_manager->GetProfileByPath(
1908cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      profile_manager->user_data_dir().Append(
1918cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com          TestingProfile::kTestUserProfileDir));
1928cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(profile);
1938cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1948cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Install the new user policy blob in session manager client.
1958cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.Build();
1968cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  session_manager_client()->set_user_policy(
1978cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      user_policy_.policy_data().username(),
1988cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      user_policy_.GetBlob());
1998cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2008cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Reload user policy from session manager client and wait for the update to
2018cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // take effect.
2028cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  RunClosureAndWaitForUserPolicyUpdate(
2038cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      base::Bind(&PowerPolicyBrowserTestBase::ReloadUserPolicy, this, profile),
2048cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      profile);
2058cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2068cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2078cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::
2088cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    StoreAndReloadDevicePolicyAndWaitForLoginProfileChange() {
2098cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  Profile* profile = chromeos::ProfileHelper::GetSigninProfile();
2108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(profile);
2118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Install the new device policy blob in session manager client, reload device
2138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // policy from session manager client and wait for a change in the login
2148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // profile's policy to be observed.
2158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  RunClosureAndWaitForUserPolicyUpdate(
2168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      base::Bind(&PowerPolicyBrowserTestBase::RefreshDevicePolicy, this),
2178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      profile);
2188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comstd::string PowerPolicyBrowserTestBase::GetDebugString(
2218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    const pm::PowerManagementPolicy& policy) {
2228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  return chromeos::PowerPolicyController::GetPolicyDebugString(policy);
2238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::RunClosureAndWaitForUserPolicyUpdate(
2268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    const base::Closure& closure,
2278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    Profile* profile) {
2288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::RunLoop run_loop;
2298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  MockPolicyServiceObserver observer;
2308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_CALL(observer, OnPolicyUpdated(_, _, _))
2318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
2328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(AnyNumber());
2338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PolicyService* policy_service =
2348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      ProfilePolicyConnectorFactory::GetForProfile(profile)->policy_service();
2358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(policy_service);
2368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy_service->AddObserver(POLICY_DOMAIN_CHROME, &observer);
2378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  closure.Run();
2388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  run_loop.Run();
2398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy_service->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
2408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyBrowserTestBase::ReloadUserPolicy(Profile* profile) {
2438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  UserCloudPolicyManagerChromeOS* policy_manager =
2448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
2458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  ASSERT_TRUE(policy_manager);
2468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy_manager->core()->store()->Load();
2478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comPowerPolicyLoginScreenBrowserTest::PowerPolicyLoginScreenBrowserTest() {
2508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyLoginScreenBrowserTest::SetUpCommandLine(
2538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    CommandLine* command_line) {
2548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyBrowserTestBase::SetUpCommandLine(command_line);
2558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  command_line->AppendSwitch(chromeos::switches::kLoginManager);
2568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
2578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2598cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyLoginScreenBrowserTest::SetUpOnMainThread() {
2608cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyBrowserTestBase::SetUpOnMainThread();
2618cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2628cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Wait for the login screen to be shown.
2638cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  content::WindowedNotificationObserver(
2648cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE,
2658cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      content::NotificationService::AllSources()).Wait();
2668cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2678cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2688cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyLoginScreenBrowserTest::CleanUpOnMainThread() {
2698cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::MessageLoop::current()->PostTask(FROM_HERE,
2708cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                                         base::Bind(&chrome::AttemptExit));
2718cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::RunLoop().RunUntilIdle();
2728cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyBrowserTestBase::CleanUpOnMainThread();
2738cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2748cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2758cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comPowerPolicyInSessionBrowserTest::PowerPolicyInSessionBrowserTest() {
2768cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2778cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2788cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid PowerPolicyInSessionBrowserTest::SetUpOnMainThread() {
2798cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  PowerPolicyBrowserTestBase::SetUpOnMainThread();
2808cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2818cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Tell the DeviceSettingsService that there is no local owner.
2828cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  chromeos::DeviceSettingsService::Get()->SetUsername(std::string());
2838cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
2848cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
2858cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Verifies that device policy is applied on the login screen.
2868cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comIN_PROC_BROWSER_TEST_F(PowerPolicyLoginScreenBrowserTest, SetDevicePolicy) {
2878cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy power_management_policy =
2888cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy();
2898cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_screen_dim_ms(5000);
2908cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_screen_off_ms(7000);
2918cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_idle_ms(9000);
2928cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_screen_dim_ms(1000);
2938cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_screen_off_ms(3000);
2948cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_idle_ms(4000);
2958cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_ac_idle_action(
2968cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::DO_NOTHING);
2978cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_battery_idle_action(
2988cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::DO_NOTHING);
2998cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_lid_closed_action(
3008cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::DO_NOTHING);
3018cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_user_activity_screen_dim_delay_factor(3.0);
3028cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3038cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
3048cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  proto.mutable_login_screen_power_management()->
3058cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      set_login_screen_power_management(kLoginScreenPowerManagementPolicy);
3068cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
3078cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_EQ(GetDebugString(power_management_policy),
3088cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            GetDebugString(power_manager_client_->get_policy()));
3098cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
3108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Verifies that device policy is ignored during a session.
3128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comIN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest, SetDevicePolicy) {
3138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy power_management_policy =
3148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy();
3158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
3178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  proto.mutable_login_screen_power_management()->
3188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      set_login_screen_power_management(kLoginScreenPowerManagementPolicy);
3198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  StoreAndReloadDevicePolicyAndWaitForLoginProfileChange();
3208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_EQ(GetDebugString(power_management_policy),
3218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            GetDebugString(power_manager_client_->get_policy()));
3228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
3238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Verifies that user policy is applied during a session.
3258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comIN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest, SetUserPolicy) {
3268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy power_management_policy =
3278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy();
3288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_screen_dim_ms(5000);
3298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_screen_lock_ms(6000);
3308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_screen_off_ms(7000);
3318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_idle_warning_ms(8000);
3328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_ac_delays()->set_idle_ms(9000);
3338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_screen_dim_ms(1000);
3348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_screen_lock_ms(2000);
3358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_screen_off_ms(3000);
3368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_idle_warning_ms(4000);
3378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.mutable_battery_delays()->set_idle_ms(5000);
3388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_use_audio_activity(false);
3398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_use_video_activity(false);
3408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_ac_idle_action(
3418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::STOP_SESSION);
3428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_battery_idle_action(
3438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::STOP_SESSION);
3448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_lid_closed_action(
3458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pm::PowerManagementPolicy::STOP_SESSION);
3468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_presentation_screen_dim_delay_factor(3.0);
3478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  power_management_policy.set_user_activity_screen_dim_delay_factor(3.0);
3488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screendimdelayac()->set_value(5000);
3508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screenlockdelayac()->set_value(6000);
3518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screenoffdelayac()->set_value(7000);
3528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idlewarningdelayac()->set_value(8000);
3538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idledelayac()->set_value(9000);
3548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screendimdelaybattery()->set_value(1000);
3558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screenlockdelaybattery()->set_value(2000);
3568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_screenoffdelaybattery()->set_value(3000);
3578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idlewarningdelaybattery()->set_value(4000);
3588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idledelaybattery()->set_value(5000);
3598cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_powermanagementusesaudioactivity()->set_value(
3608cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      false);
3618cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_powermanagementusesvideoactivity()->set_value(
3628cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      false);
3638cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idleactionac()->set_value(
3648cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      chromeos::PowerPolicyController::ACTION_STOP_SESSION);
3658cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_idleactionbattery()->set_value(
3668cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      chromeos::PowerPolicyController::ACTION_STOP_SESSION);
3678cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_lidcloseaction()->set_value(
3688cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      chromeos::PowerPolicyController::ACTION_STOP_SESSION);
3698cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_presentationscreendimdelayscale()->set_value(
3708cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      300);
3718cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_useractivityscreendimdelayscale()->set_value(
3728cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      300);
3738cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  StoreAndReloadUserPolicy();
3748cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_EQ(GetDebugString(power_management_policy),
3758cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            GetDebugString(power_manager_client_->get_policy()));
3768cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
3778cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3788cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Verifies that screen wake locks can be enabled and disabled by extensions and
3798cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// user policy during a session.
3808cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comIN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest, AllowScreenWakeLocks) {
3818cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy baseline_policy =
3828cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy();
3838cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3848cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Default settings should have delays.
3858cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy power_management_policy = baseline_policy;
3868cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_NE(0, baseline_policy.ac_delays().screen_dim_ms());
3878cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_NE(0, baseline_policy.ac_delays().screen_off_ms());
3888cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_NE(0, baseline_policy.battery_delays().screen_dim_ms());
3898cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_NE(0, baseline_policy.battery_delays().screen_off_ms());
3908cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3918cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Pretend an extension grabs a screen wake lock.
3928cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  const char kExtensionId[] = "abcdefghijklmnopabcdefghijlkmnop";
3938cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  extensions::PowerApiManager::GetInstance()->AddRequest(
3948cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      kExtensionId, extensions::api::power::LEVEL_DISPLAY);
3958cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  base::RunLoop().RunUntilIdle();
3968cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
3978cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Check that the lock is in effect (ignoring ac_idle_action,
3988cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // battery_idle_action and reason).
3998cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  pm::PowerManagementPolicy policy = baseline_policy;
4008cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.mutable_ac_delays()->set_screen_dim_ms(0);
4018cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.mutable_ac_delays()->set_screen_off_ms(0);
4028cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.mutable_battery_delays()->set_screen_dim_ms(0);
4038cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.mutable_battery_delays()->set_screen_off_ms(0);
4048cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_ac_idle_action(
4058cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy().ac_idle_action());
4068cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_battery_idle_action(
4078cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy().battery_idle_action());
4088cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_reason(power_manager_client_->get_policy().reason());
4098cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_EQ(GetDebugString(policy),
4108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            GetDebugString(power_manager_client_->get_policy()));
4118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
4128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  // Engage the user policy and verify that the defaults take effect again.
4138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  user_policy_.payload().mutable_allowscreenwakelocks()->set_value(false);
4148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  StoreAndReloadUserPolicy();
4158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy = baseline_policy;
4168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_ac_idle_action(
4178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy().ac_idle_action());
4188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_battery_idle_action(
4198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      power_manager_client_->get_policy().battery_idle_action());
4208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  policy.set_reason(power_manager_client_->get_policy().reason());
4218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  EXPECT_EQ(GetDebugString(policy),
4228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com            GetDebugString(power_manager_client_->get_policy()));
4238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
4248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
4258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}  // namespace policy
4268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com