OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/system/device_disabling_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/browser_process_platform_part.h" |
| 15 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 16 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
| 17 #include "chrome/browser/chromeos/policy/server_backed_device_state.h" |
| 18 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/test/base/testing_browser_process.h" |
| 21 #include "chromeos/chromeos_switches.h" |
| 22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace chromeos { |
| 26 namespace system { |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kDisabledMessage[] = "Device disabled."; |
| 31 |
| 32 } |
| 33 |
| 34 class DeviceDisablingManagerTest : public testing::Test { |
| 35 public: |
| 36 DeviceDisablingManagerTest(); |
| 37 ~DeviceDisablingManagerTest(); |
| 38 |
| 39 // testing::Test: |
| 40 void SetUp() override; |
| 41 void TearDown() override; |
| 42 |
| 43 bool IsDeviceDisabled() const; |
| 44 const std::string& GetDisabledMessage() const; |
| 45 |
| 46 void CheckWhetherDeviceDisabledDuringOOBE(); |
| 47 |
| 48 void SetDeviceDisabled(bool disabled); |
| 49 void SetDeviceMode(policy::DeviceMode device_mode); |
| 50 |
| 51 private: |
| 52 void OnDeviceDisabledChecked(bool device_disabled); |
| 53 |
| 54 policy::ScopedStubEnterpriseInstallAttributes install_attributes_; |
| 55 TestingPrefServiceSimple local_state_; |
| 56 |
| 57 scoped_ptr<DeviceDisablingManager> device_disabling_manager_; |
| 58 |
| 59 base::RunLoop run_loop_; |
| 60 bool device_disabled_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(DeviceDisablingManagerTest); |
| 63 }; |
| 64 |
| 65 DeviceDisablingManagerTest::DeviceDisablingManagerTest() |
| 66 : install_attributes_("", "", "", policy::DEVICE_MODE_NOT_SET), |
| 67 device_disabled_(false) { |
| 68 } |
| 69 |
| 70 DeviceDisablingManagerTest::~DeviceDisablingManagerTest() { |
| 71 } |
| 72 |
| 73 void DeviceDisablingManagerTest::SetUp() { |
| 74 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); |
| 75 policy::DeviceCloudPolicyManagerChromeOS::RegisterPrefs( |
| 76 local_state_.registry()); |
| 77 |
| 78 device_disabling_manager_.reset(new DeviceDisablingManager( |
| 79 TestingBrowserProcess::GetGlobal()->platform_part()-> |
| 80 browser_policy_connector_chromeos())); |
| 81 } |
| 82 |
| 83 void DeviceDisablingManagerTest::TearDown() { |
| 84 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); |
| 85 } |
| 86 |
| 87 bool DeviceDisablingManagerTest::IsDeviceDisabled() const { |
| 88 return device_disabled_; |
| 89 } |
| 90 |
| 91 const std::string& DeviceDisablingManagerTest::GetDisabledMessage() const { |
| 92 return device_disabling_manager_->GetDisabledMessage(); |
| 93 |
| 94 } |
| 95 |
| 96 void DeviceDisablingManagerTest::CheckWhetherDeviceDisabledDuringOOBE() { |
| 97 device_disabling_manager_->CheckWhetherDeviceDisabledDuringOOBE( |
| 98 base::Bind(&DeviceDisablingManagerTest::OnDeviceDisabledChecked, |
| 99 base::Unretained(this))); |
| 100 run_loop_.Run(); |
| 101 } |
| 102 |
| 103 void DeviceDisablingManagerTest::SetDeviceDisabled(bool disabled) { |
| 104 DictionaryPrefUpdate dict(&local_state_, prefs::kServerBackedDeviceState); |
| 105 if (disabled) { |
| 106 dict->SetString(policy::kDeviceStateRestoreMode, |
| 107 policy::kDeviceStateRestoreModeDisabled); |
| 108 } else { |
| 109 dict->Remove(policy::kDeviceStateRestoreMode, nullptr); |
| 110 } |
| 111 dict->SetString(policy::kDeviceStateDisabledMessage, kDisabledMessage); |
| 112 } |
| 113 |
| 114 void DeviceDisablingManagerTest::SetDeviceMode(policy::DeviceMode device_mode) { |
| 115 reinterpret_cast<policy::StubEnterpriseInstallAttributes*>( |
| 116 TestingBrowserProcess::GetGlobal()->platform_part()-> |
| 117 browser_policy_connector_chromeos()->GetInstallAttributes())-> |
| 118 SetMode(device_mode); |
| 119 } |
| 120 |
| 121 void DeviceDisablingManagerTest::OnDeviceDisabledChecked(bool device_disabled) { |
| 122 device_disabled_ = device_disabled; |
| 123 run_loop_.Quit(); |
| 124 } |
| 125 |
| 126 // Verifies that the device is not considered disabled during OOBE by default. |
| 127 TEST_F(DeviceDisablingManagerTest, NotDisabledByDefault) { |
| 128 CheckWhetherDeviceDisabledDuringOOBE(); |
| 129 EXPECT_FALSE(IsDeviceDisabled()); |
| 130 } |
| 131 |
| 132 // Verifies that the device is not considered disabled during OOBE when it is |
| 133 // explicitly marked as not disabled. |
| 134 TEST_F(DeviceDisablingManagerTest, NotDisabledWhenExplicitlyNotDisabled) { |
| 135 SetDeviceDisabled(false); |
| 136 CheckWhetherDeviceDisabledDuringOOBE(); |
| 137 EXPECT_FALSE(IsDeviceDisabled()); |
| 138 } |
| 139 |
| 140 // Verifies that the device is not considered disabled during OOBE when device |
| 141 // disabling is turned off by flag, even if the device is marked as disabled. |
| 142 TEST_F(DeviceDisablingManagerTest, NotDisabledWhenTurnedOffByFlag) { |
| 143 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 144 switches::kDisableDeviceDisabling); |
| 145 SetDeviceDisabled(true); |
| 146 CheckWhetherDeviceDisabledDuringOOBE(); |
| 147 EXPECT_FALSE(IsDeviceDisabled()); |
| 148 } |
| 149 |
| 150 // Verifies that the device is not considered disabled during OOBE when it is |
| 151 // already enrolled, even if the device is marked as disabled. |
| 152 TEST_F(DeviceDisablingManagerTest, DoNotShowWhenEnterpriseOwned) { |
| 153 SetDeviceMode(policy::DEVICE_MODE_ENTERPRISE); |
| 154 SetDeviceDisabled(true); |
| 155 CheckWhetherDeviceDisabledDuringOOBE(); |
| 156 EXPECT_FALSE(IsDeviceDisabled()); |
| 157 } |
| 158 |
| 159 // Verifies that the device is not considered disabled during OOBE when it is |
| 160 // already owned by a consumer, even if the device is marked as disabled. |
| 161 TEST_F(DeviceDisablingManagerTest, DoNotShowWhenConsumerOwned) { |
| 162 SetDeviceMode(policy::DEVICE_MODE_CONSUMER); |
| 163 SetDeviceDisabled(true); |
| 164 CheckWhetherDeviceDisabledDuringOOBE(); |
| 165 EXPECT_FALSE(IsDeviceDisabled()); |
| 166 } |
| 167 |
| 168 // Verifies that the device is considered disabled during OOBE when it is marked |
| 169 // as disabled, device disabling is not turned off by flag and the device is not |
| 170 // owned yet. |
| 171 TEST_F(DeviceDisablingManagerTest, ShowWhenDisabledAndNotOwned) { |
| 172 SetDeviceDisabled(true); |
| 173 CheckWhetherDeviceDisabledDuringOOBE(); |
| 174 EXPECT_TRUE(IsDeviceDisabled()); |
| 175 EXPECT_EQ(kDisabledMessage, GetDisabledMessage()); |
| 176 } |
| 177 |
| 178 } // namespace system |
| 179 } // namespace chromeos |
OLD | NEW |