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.
8 #include "base/macros.h"
9 #include "base/memory/linked_ptr.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/test/scoped_path_override.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
15 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
16 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
17 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "chromeos/settings/cros_settings_names.h"
23 #include "testing/gtest/include/gtest/gtest.h"
29 void OnPrefChanged(const std::string
& /* setting */) {
32 class PrefsChecker
: public ownership::OwnerSettingsService::Observer
{
34 PrefsChecker(OwnerSettingsServiceChromeOS
* service
,
35 DeviceSettingsProvider
* provider
)
36 : service_(service
), provider_(provider
) {
39 service_
->AddObserver(this);
42 virtual ~PrefsChecker() { service_
->RemoveObserver(this); }
44 // OwnerSettingsService::Observer implementation:
45 virtual void OnSignedPolicyStored(bool success
) override
{
46 if (service_
->has_pending_changes())
49 while (!set_requests_
.empty()) {
50 SetRequest request
= set_requests_
.front();
52 const base::Value
* value
= provider_
->Get(request
.first
);
53 ASSERT_TRUE(request
.second
->Equals(value
));
58 bool Set(const std::string
& setting
, const base::Value
& value
) {
59 if (!service_
->Set(setting
, value
))
62 SetRequest(setting
, linked_ptr
<base::Value
>(value
.DeepCopy())));
66 void Wait() { loop_
.Run(); }
69 OwnerSettingsServiceChromeOS
* service_
;
70 DeviceSettingsProvider
* provider_
;
73 typedef std::pair
<std::string
, linked_ptr
<base::Value
>> SetRequest
;
74 std::queue
<SetRequest
> set_requests_
;
76 DISALLOW_COPY_AND_ASSIGN(PrefsChecker
);
81 class OwnerSettingsServiceChromeOSTest
: public DeviceSettingsTestBase
{
83 OwnerSettingsServiceChromeOSTest()
85 local_state_(TestingBrowserProcess::GetGlobal()),
86 user_data_dir_override_(chrome::DIR_USER_DATA
) {}
88 virtual void SetUp() override
{
89 DeviceSettingsTestBase::SetUp();
90 provider_
.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged
),
91 &device_settings_service_
));
92 owner_key_util_
->SetPrivateKey(device_policy_
.GetSigningKey());
93 InitOwner(device_policy_
.policy_data().username(), true);
94 FlushDeviceSettings();
96 service_
= OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
98 ASSERT_TRUE(service_
);
99 ASSERT_TRUE(service_
->IsOwner());
102 virtual void TearDown() override
{ DeviceSettingsTestBase::TearDown(); }
104 void TestSingleSet(OwnerSettingsServiceChromeOS
* service
,
105 const std::string
& setting
,
106 const base::Value
& in_value
) {
107 PrefsChecker
checker(service
, provider_
.get());
108 checker
.Set(setting
, in_value
);
109 FlushDeviceSettings();
113 OwnerSettingsServiceChromeOS
* service_
;
114 ScopedTestingLocalState local_state_
;
115 scoped_ptr
<DeviceSettingsProvider
> provider_
;
116 base::ScopedPathOverride user_data_dir_override_
;
119 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSTest
);
122 TEST_F(OwnerSettingsServiceChromeOSTest
, SingleSetTest
) {
123 TestSingleSet(service_
, kReleaseChannel
, base::StringValue("dev-channel"));
124 TestSingleSet(service_
, kReleaseChannel
, base::StringValue("beta-channel"));
125 TestSingleSet(service_
, kReleaseChannel
, base::StringValue("stable-channel"));
128 TEST_F(OwnerSettingsServiceChromeOSTest
, MultipleSetTest
) {
129 base::FundamentalValue
allow_guest(false);
130 base::StringValue
release_channel("stable-channel");
131 base::FundamentalValue
show_user_names(true);
133 PrefsChecker
checker(service_
, provider_
.get());
135 checker
.Set(kAccountsPrefAllowGuest
, allow_guest
);
136 checker
.Set(kReleaseChannel
, release_channel
);
137 checker
.Set(kAccountsPrefShowUserNamesOnSignIn
, show_user_names
);
139 FlushDeviceSettings();
143 TEST_F(OwnerSettingsServiceChromeOSTest
, FailedSetRequest
) {
144 device_settings_test_helper_
.set_store_result(false);
145 std::string current_channel
;
146 ASSERT_TRUE(provider_
->Get(kReleaseChannel
)->GetAsString(¤t_channel
));
147 ASSERT_NE(current_channel
, "stable-channel");
149 // Check that DeviceSettingsProvider's cache is updated.
150 PrefsChecker
checker(service_
, provider_
.get());
151 checker
.Set(kReleaseChannel
, base::StringValue("stable-channel"));
152 FlushDeviceSettings();
155 // Check that DeviceSettingsService's policy isn't updated.
156 ASSERT_EQ(current_channel
, device_settings_service_
.device_settings()
161 class OwnerSettingsServiceChromeOSNoOwnerTest
162 : public OwnerSettingsServiceChromeOSTest
{
164 OwnerSettingsServiceChromeOSNoOwnerTest() {}
165 virtual ~OwnerSettingsServiceChromeOSNoOwnerTest() {}
167 virtual void SetUp() override
{
168 DeviceSettingsTestBase::SetUp();
169 provider_
.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged
),
170 &device_settings_service_
));
171 FlushDeviceSettings();
172 service_
= OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
174 ASSERT_TRUE(service_
);
175 ASSERT_FALSE(service_
->IsOwner());
178 virtual void TearDown() override
{ DeviceSettingsTestBase::TearDown(); }
181 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSNoOwnerTest
);
184 TEST_F(OwnerSettingsServiceChromeOSNoOwnerTest
, SingleSetTest
) {
185 ASSERT_FALSE(service_
->SetBoolean(kAccountsPrefAllowGuest
, false));
188 } // namespace chromeos