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 |
| 38 // testing::Test: |
| 39 void SetUp() override; |
| 40 void TearDown() override; |
| 41 |
| 42 bool device_disabled() const { return device_disabled_; } |
| 43 const std::string& GetDisabledMessage() const; |
| 44 |
| 45 void CheckWhetherDeviceDisabledDuringOOBE(); |
| 46 |
| 47 void SetDeviceDisabled(bool disabled); |
| 48 void SetDeviceMode(policy::DeviceMode device_mode); |
| 49 |
| 50 private: |
| 51 void OnDeviceDisabledChecked(bool device_disabled); |
| 52 |
| 53 policy::ScopedStubEnterpriseInstallAttributes install_attributes_; |
| 54 TestingPrefServiceSimple local_state_; |
| 55 |
| 56 scoped_ptr<DeviceDisablingManager> device_disabling_manager_; |
| 57 |
| 58 base::RunLoop run_loop_; |
| 59 bool device_disabled_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(DeviceDisablingManagerTest); |
| 62 }; |
| 63 |
| 64 DeviceDisablingManagerTest::DeviceDisablingManagerTest() |
| 65 : install_attributes_("", "", "", policy::DEVICE_MODE_NOT_SET), |
| 66 device_disabled_(false) { |
| 67 } |
| 68 |
| 69 void DeviceDisablingManagerTest::SetUp() { |
| 70 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); |
| 71 policy::DeviceCloudPolicyManagerChromeOS::RegisterPrefs( |
| 72 local_state_.registry()); |
| 73 |
| 74 device_disabling_manager_.reset(new DeviceDisablingManager( |
| 75 TestingBrowserProcess::GetGlobal()->platform_part()-> |
| 76 browser_policy_connector_chromeos())); |
| 77 } |
| 78 |
| 79 void DeviceDisablingManagerTest::TearDown() { |
| 80 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); |
| 81 } |
| 82 |
| 83 const std::string& DeviceDisablingManagerTest::GetDisabledMessage() const { |
| 84 return device_disabling_manager_->disabled_message(); |
| 85 |
| 86 } |
| 87 |
| 88 void DeviceDisablingManagerTest::CheckWhetherDeviceDisabledDuringOOBE() { |
| 89 device_disabling_manager_->CheckWhetherDeviceDisabledDuringOOBE( |
| 90 base::Bind(&DeviceDisablingManagerTest::OnDeviceDisabledChecked, |
| 91 base::Unretained(this))); |
| 92 run_loop_.Run(); |
| 93 } |
| 94 |
| 95 void DeviceDisablingManagerTest::SetDeviceDisabled(bool disabled) { |
| 96 DictionaryPrefUpdate dict(&local_state_, prefs::kServerBackedDeviceState); |
| 97 if (disabled) { |
| 98 dict->SetString(policy::kDeviceStateRestoreMode, |
| 99 policy::kDeviceStateRestoreModeDisabled); |
| 100 } else { |
| 101 dict->Remove(policy::kDeviceStateRestoreMode, nullptr); |
| 102 } |
| 103 dict->SetString(policy::kDeviceStateDisabledMessage, kDisabledMessage); |
| 104 } |
| 105 |
| 106 void DeviceDisablingManagerTest::SetDeviceMode(policy::DeviceMode device_mode) { |
| 107 reinterpret_cast<policy::StubEnterpriseInstallAttributes*>( |
| 108 TestingBrowserProcess::GetGlobal()->platform_part()-> |
| 109 browser_policy_connector_chromeos()->GetInstallAttributes())-> |
| 110 SetMode(device_mode); |
| 111 } |
| 112 |
| 113 void DeviceDisablingManagerTest::OnDeviceDisabledChecked(bool device_disabled) { |
| 114 device_disabled_ = device_disabled; |
| 115 run_loop_.Quit(); |
| 116 } |
| 117 |
| 118 // Verifies that the device is not considered disabled during OOBE by default. |
| 119 TEST_F(DeviceDisablingManagerTest, NotDisabledByDefault) { |
| 120 CheckWhetherDeviceDisabledDuringOOBE(); |
| 121 EXPECT_FALSE(device_disabled()); |
| 122 } |
| 123 |
| 124 // Verifies that the device is not considered disabled during OOBE when it is |
| 125 // explicitly marked as not disabled. |
| 126 TEST_F(DeviceDisablingManagerTest, NotDisabledWhenExplicitlyNotDisabled) { |
| 127 SetDeviceDisabled(false); |
| 128 CheckWhetherDeviceDisabledDuringOOBE(); |
| 129 EXPECT_FALSE(device_disabled()); |
| 130 } |
| 131 |
| 132 // Verifies that the device is not considered disabled during OOBE when device |
| 133 // disabling is turned off by flag, even if the device is marked as disabled. |
| 134 TEST_F(DeviceDisablingManagerTest, NotDisabledWhenTurnedOffByFlag) { |
| 135 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 136 switches::kDisableDeviceDisabling); |
| 137 SetDeviceDisabled(true); |
| 138 CheckWhetherDeviceDisabledDuringOOBE(); |
| 139 EXPECT_FALSE(device_disabled()); |
| 140 } |
| 141 |
| 142 // Verifies that the device is not considered disabled during OOBE when it is |
| 143 // already enrolled, even if the device is marked as disabled. |
| 144 TEST_F(DeviceDisablingManagerTest, DoNotShowWhenEnterpriseOwned) { |
| 145 SetDeviceMode(policy::DEVICE_MODE_ENTERPRISE); |
| 146 SetDeviceDisabled(true); |
| 147 CheckWhetherDeviceDisabledDuringOOBE(); |
| 148 EXPECT_FALSE(device_disabled()); |
| 149 } |
| 150 |
| 151 // Verifies that the device is not considered disabled during OOBE when it is |
| 152 // already owned by a consumer, even if the device is marked as disabled. |
| 153 TEST_F(DeviceDisablingManagerTest, DoNotShowWhenConsumerOwned) { |
| 154 SetDeviceMode(policy::DEVICE_MODE_CONSUMER); |
| 155 SetDeviceDisabled(true); |
| 156 CheckWhetherDeviceDisabledDuringOOBE(); |
| 157 EXPECT_FALSE(device_disabled()); |
| 158 } |
| 159 |
| 160 // Verifies that the device is considered disabled during OOBE when it is marked |
| 161 // as disabled, device disabling is not turned off by flag and the device is not |
| 162 // owned yet. |
| 163 TEST_F(DeviceDisablingManagerTest, ShowWhenDisabledAndNotOwned) { |
| 164 SetDeviceDisabled(true); |
| 165 CheckWhetherDeviceDisabledDuringOOBE(); |
| 166 EXPECT_TRUE(device_disabled()); |
| 167 EXPECT_EQ(kDisabledMessage, GetDisabledMessage()); |
| 168 } |
| 169 |
| 170 } // namespace system |
| 171 } // namespace chromeos |
OLD | NEW |