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.
5 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
11 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
12 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "chrome/browser/chromeos/settings/device_settings_cache.h"
15 #include "chrome/browser/chromeos/settings/device_settings_service.h"
16 #include "chrome/browser/profiles/profile.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"
23 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper() {
27 ScopedCrosSettingsTestHelper::ScopedCrosSettingsTestHelper(
28 bool create_settings_service
) {
29 Initialize(create_settings_service
);
32 ScopedCrosSettingsTestHelper::~ScopedCrosSettingsTestHelper() {
36 scoped_ptr
<FakeOwnerSettingsService
>
37 ScopedCrosSettingsTestHelper::CreateOwnerSettingsService(Profile
* profile
) {
38 return make_scoped_ptr(new FakeOwnerSettingsService(
39 profile
, new ownership::MockOwnerKeyUtil(), &stub_settings_provider_
));
42 void ScopedCrosSettingsTestHelper::ReplaceProvider(const std::string
& path
) {
43 CHECK(!real_settings_provider_
);
44 // Swap out the DeviceSettingsProvider with our settings provider so we can
45 // set values for the specified path.
46 CrosSettings
* const cros_settings
= CrosSettings::Get();
47 real_settings_provider_
= cros_settings
->GetProvider(path
);
48 EXPECT_TRUE(real_settings_provider_
);
49 EXPECT_TRUE(cros_settings
->RemoveSettingsProvider(real_settings_provider_
));
50 cros_settings
->AddSettingsProvider(&stub_settings_provider_
);
53 void ScopedCrosSettingsTestHelper::RestoreProvider() {
54 if (real_settings_provider_
) {
55 // Restore the real DeviceSettingsProvider.
56 CrosSettings
* const cros_settings
= CrosSettings::Get();
58 cros_settings
->RemoveSettingsProvider(&stub_settings_provider_
));
59 cros_settings
->AddSettingsProvider(real_settings_provider_
);
60 real_settings_provider_
= nullptr;
64 void ScopedCrosSettingsTestHelper::SetTrustedStatus(
65 CrosSettingsProvider::TrustedStatus status
) {
66 stub_settings_provider_
.SetTrustedStatus(status
);
69 void ScopedCrosSettingsTestHelper::SetCurrentUserIsOwner(bool owner
) {
70 stub_settings_provider_
.SetCurrentUserIsOwner(owner
);
73 void ScopedCrosSettingsTestHelper::Set(const std::string
& path
,
74 const base::Value
& in_value
) {
75 stub_settings_provider_
.Set(path
, in_value
);
78 void ScopedCrosSettingsTestHelper::SetBoolean(const std::string
& path
,
80 Set(path
, base::FundamentalValue(in_value
));
83 void ScopedCrosSettingsTestHelper::SetInteger(const std::string
& path
,
85 Set(path
, base::FundamentalValue(in_value
));
88 void ScopedCrosSettingsTestHelper::SetDouble(const std::string
& path
,
90 Set(path
, base::FundamentalValue(in_value
));
93 void ScopedCrosSettingsTestHelper::SetString(const std::string
& path
,
94 const std::string
& in_value
) {
95 Set(path
, base::StringValue(in_value
));
98 void ScopedCrosSettingsTestHelper::StoreCachedDeviceSetting(
99 const std::string
& path
) {
100 const base::Value
* const value
= stub_settings_provider_
.Get(path
);
102 enterprise_management::PolicyData data
;
103 enterprise_management::ChromeDeviceSettingsProto settings
;
104 if (device_settings_cache::Retrieve(&data
,
105 g_browser_process
->local_state())) {
106 CHECK(settings
.ParseFromString(data
.policy_value()));
108 OwnerSettingsServiceChromeOS::UpdateDeviceSettings(path
, *value
, settings
);
109 CHECK(settings
.SerializeToString(data
.mutable_policy_value()));
110 CHECK(device_settings_cache::Store(data
, g_browser_process
->local_state()));
114 void ScopedCrosSettingsTestHelper::CopyStoredValue(const std::string
& path
) {
115 CrosSettingsProvider
* provider
= real_settings_provider_
116 ? real_settings_provider_
117 : CrosSettings::Get()->GetProvider(path
);
118 const base::Value
* const value
= provider
->Get(path
);
120 stub_settings_provider_
.Set(path
, *value
);
124 void ScopedCrosSettingsTestHelper::Initialize(bool create_settings_service
) {
125 if (create_settings_service
) {
126 CHECK(!DeviceSettingsService::IsInitialized());
127 test_device_settings_service_
.reset(new ScopedTestDeviceSettingsService());
128 test_cros_settings_
.reset(new ScopedTestCrosSettings());
132 } // namespace chromeos