| 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 "chrome/browser/chromeos/ownership/fake_owner_settings_service.h" |
| 8 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "components/ownership/mock_owner_key_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper() { |
| 16 Initialize(true); |
| 17 } |
| 18 |
| 19 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper( |
| 20 bool create_settings_service) { |
| 21 Initialize(create_settings_service); |
| 22 } |
| 23 |
| 24 ScopedCrosSettingsTestHelper::~ScopedCrosSettingsTestHelper() { |
| 25 RestoreProvider(); |
| 26 } |
| 27 |
| 28 FakeOwnerSettingsService* |
| 29 ScopedCrosSettingsTestHelper::CreateOwnerSettingsService(Profile* profile) { |
| 30 return new FakeOwnerSettingsService( |
| 31 profile, new ownership::MockOwnerKeyUtil(), &stub_settings_provider_); |
| 32 } |
| 33 |
| 34 void ScopedCrosSettingsTestHelper::ReplaceProvider(const std::string& path) { |
| 35 // Swap out the DeviceSettingsProvider with our settings provider so we can |
| 36 // set values for the specified path. |
| 37 CrosSettings* const cros_settings = CrosSettings::Get(); |
| 38 device_settings_provider_ = cros_settings->GetProvider(path); |
| 39 EXPECT_TRUE(device_settings_provider_); |
| 40 EXPECT_TRUE(cros_settings->RemoveSettingsProvider(device_settings_provider_)); |
| 41 cros_settings->AddSettingsProvider(&stub_settings_provider_); |
| 42 } |
| 43 |
| 44 void ScopedCrosSettingsTestHelper::RestoreProvider() { |
| 45 if (device_settings_provider_) { |
| 46 // Restore the real DeviceSettingsProvider. |
| 47 CrosSettings* const cros_settings = CrosSettings::Get(); |
| 48 EXPECT_TRUE( |
| 49 cros_settings->RemoveSettingsProvider(&stub_settings_provider_)); |
| 50 cros_settings->AddSettingsProvider(device_settings_provider_); |
| 51 device_settings_provider_ = nullptr; |
| 52 } |
| 53 } |
| 54 |
| 55 void ScopedCrosSettingsTestHelper::SetBoolean(const std::string& path, |
| 56 bool in_value) { |
| 57 base::FundamentalValue value(in_value); |
| 58 stub_settings_provider_.Set(path, value); |
| 59 } |
| 60 |
| 61 void ScopedCrosSettingsTestHelper::SetInteger(const std::string& path, |
| 62 int in_value) { |
| 63 base::FundamentalValue value(in_value); |
| 64 stub_settings_provider_.Set(path, value); |
| 65 } |
| 66 |
| 67 void ScopedCrosSettingsTestHelper::SetDouble(const std::string& path, |
| 68 double in_value) { |
| 69 base::FundamentalValue value(in_value); |
| 70 stub_settings_provider_.Set(path, value); |
| 71 } |
| 72 |
| 73 void ScopedCrosSettingsTestHelper::SetString(const std::string& path, |
| 74 const std::string& in_value) { |
| 75 base::StringValue value(in_value); |
| 76 stub_settings_provider_.Set(path, value); |
| 77 } |
| 78 |
| 79 void ScopedCrosSettingsTestHelper::Initialize(bool create_settings_service) { |
| 80 if (create_settings_service && !DeviceSettingsService::IsInitialized()) { |
| 81 test_device_settings_service_.reset(new ScopedTestDeviceSettingsService()); |
| 82 test_cros_settings_.reset(new ScopedTestCrosSettings()); |
| 83 } |
| 84 } |
| 85 |
| 86 } // namespace chromeos |
| OLD | NEW |