OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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/settings/scoped_cros_settings_test_helper.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h" |
| 10 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h" |
| 11 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
| 12 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 13 #include "chrome/browser/chromeos/settings/device_settings_cache.h" |
| 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chromeos/settings/cros_settings_provider.h" |
| 17 #include "components/ownership/mock_owner_key_util.h" |
| 18 #include "policy/proto/device_management_backend.pb.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper() { |
| 24 Initialize(true); |
| 25 } |
| 26 |
| 27 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper( |
| 28 bool create_settings_service) { |
| 29 Initialize(create_settings_service); |
| 30 } |
| 31 |
| 32 ScopedCrosSettingsTestHelper::~ScopedCrosSettingsTestHelper() { |
| 33 RestoreProvider(); |
| 34 } |
| 35 |
| 36 FakeOwnerSettingsService* |
| 37 ScopedCrosSettingsTestHelper::CreateOwnerSettingsService(Profile* profile) { |
| 38 return new FakeOwnerSettingsService( |
| 39 profile, new ownership::MockOwnerKeyUtil(), &stub_settings_provider_); |
| 40 } |
| 41 |
| 42 void ScopedCrosSettingsTestHelper::ReplaceProvider(const std::string& path) { |
| 43 // Swap out the DeviceSettingsProvider with our settings provider so we can |
| 44 // set values for the specified path. |
| 45 CrosSettings* const cros_settings = CrosSettings::Get(); |
| 46 real_settings_provider_ = cros_settings->GetProvider(path); |
| 47 EXPECT_TRUE(real_settings_provider_); |
| 48 EXPECT_TRUE(cros_settings->RemoveSettingsProvider(real_settings_provider_)); |
| 49 cros_settings->AddSettingsProvider(&stub_settings_provider_); |
| 50 } |
| 51 |
| 52 void ScopedCrosSettingsTestHelper::RestoreProvider() { |
| 53 if (real_settings_provider_) { |
| 54 // Restore the real DeviceSettingsProvider. |
| 55 CrosSettings* const cros_settings = CrosSettings::Get(); |
| 56 EXPECT_TRUE( |
| 57 cros_settings->RemoveSettingsProvider(&stub_settings_provider_)); |
| 58 cros_settings->AddSettingsProvider(real_settings_provider_); |
| 59 real_settings_provider_ = nullptr; |
| 60 } |
| 61 } |
| 62 |
| 63 void ScopedCrosSettingsTestHelper::SetTrustedStatus( |
| 64 CrosSettingsProvider::TrustedStatus status) { |
| 65 stub_settings_provider_.SetTrustedStatus(status); |
| 66 } |
| 67 |
| 68 void ScopedCrosSettingsTestHelper::SetCurrentUserIsOwner(bool owner) { |
| 69 stub_settings_provider_.SetCurrentUserIsOwner(owner); |
| 70 } |
| 71 |
| 72 void ScopedCrosSettingsTestHelper::Set(const std::string& path, |
| 73 const base::Value& in_value) { |
| 74 stub_settings_provider_.Set(path, in_value); |
| 75 } |
| 76 |
| 77 void ScopedCrosSettingsTestHelper::SetBoolean(const std::string& path, |
| 78 bool in_value) { |
| 79 Set(path, base::FundamentalValue(in_value)); |
| 80 } |
| 81 |
| 82 void ScopedCrosSettingsTestHelper::SetInteger(const std::string& path, |
| 83 int in_value) { |
| 84 Set(path, base::FundamentalValue(in_value)); |
| 85 } |
| 86 |
| 87 void ScopedCrosSettingsTestHelper::SetDouble(const std::string& path, |
| 88 double in_value) { |
| 89 Set(path, base::FundamentalValue(in_value)); |
| 90 } |
| 91 |
| 92 void ScopedCrosSettingsTestHelper::SetString(const std::string& path, |
| 93 const std::string& in_value) { |
| 94 Set(path, base::StringValue(in_value)); |
| 95 } |
| 96 |
| 97 void ScopedCrosSettingsTestHelper::StoreCachedDeviceSetting( |
| 98 const std::string& path) { |
| 99 const base::Value* value = stub_settings_provider_.Get(path); |
| 100 if (value) { |
| 101 enterprise_management::ChromeDeviceSettingsProto settings; |
| 102 OwnerSettingsServiceChromeOS::UpdateDeviceSettings(path, *value, settings); |
| 103 enterprise_management::PolicyData data; |
| 104 CHECK(settings.SerializeToString(data.mutable_policy_value())); |
| 105 CHECK(device_settings_cache::Store(data, g_browser_process->local_state())); |
| 106 } |
| 107 } |
| 108 |
| 109 void ScopedCrosSettingsTestHelper::CopyStoredValue(const std::string& path) { |
| 110 CrosSettingsProvider* provider = real_settings_provider_ |
| 111 ? real_settings_provider_ |
| 112 : CrosSettings::Get()->GetProvider(path); |
| 113 const base::Value* value = provider->Get(path); |
| 114 if (value) { |
| 115 stub_settings_provider_.Set(path, *value); |
| 116 } |
| 117 } |
| 118 |
| 119 void ScopedCrosSettingsTestHelper::Initialize(bool create_settings_service) { |
| 120 if (create_settings_service && !DeviceSettingsService::IsInitialized()) { |
| 121 test_device_settings_service_.reset(new ScopedTestDeviceSettingsService()); |
| 122 test_cros_settings_.reset(new ScopedTestCrosSettings()); |
| 123 } |
| 124 } |
| 125 |
| 126 } // namespace chromeos |
OLD | NEW |