1 // Copyright 2013 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.
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/time/time.h"
10 #include "base/values.h"
11 #include "chrome/browser/prefs/pref_service_syncable_util.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/syncable_prefs/synced_pref_change_registrar.h"
17 #include "components/syncable_prefs/testing_pref_service_syncable.h"
18 #include "content/public/test/test_utils.h"
19 #include "sync/api/attachments/attachment_id.h"
20 #include "sync/api/fake_sync_change_processor.h"
21 #include "sync/api/sync_change.h"
22 #include "sync/api/sync_error_factory.h"
23 #include "sync/api/sync_error_factory_mock.h"
24 #include "sync/api/syncable_service.h"
25 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
26 #include "sync/protocol/sync.pb.h"
28 #if defined(ENABLE_CONFIGURATION_POLICY)
29 #include "components/policy/core/browser/browser_policy_connector.h"
30 #include "components/policy/core/common/mock_configuration_policy_provider.h"
31 #include "components/policy/core/common/policy_map.h"
32 #include "policy/policy_constants.h"
37 using testing::Return
;
40 class SyncedPrefChangeRegistrarTest
: public InProcessBrowserTest
{
42 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {}
43 ~SyncedPrefChangeRegistrarTest() override
{}
45 #if defined(ENABLE_CONFIGURATION_POLICY)
46 void UpdateChromePolicy(const policy::PolicyMap
& policies
) {
47 policy_provider_
.UpdateChromePolicy(policies
);
48 DCHECK(base::MessageLoop::current());
54 void SetBooleanPrefValueFromSync(const std::string
& name
, bool value
) {
55 std::string serialized_value
;
56 JSONStringValueSerializer
json(&serialized_value
);
57 json
.Serialize(base::FundamentalValue(value
));
59 sync_pb::EntitySpecifics specifics
;
60 sync_pb::PreferenceSpecifics
* pref_specifics
=
61 specifics
.mutable_preference();
62 pref_specifics
->set_name(name
);
63 pref_specifics
->set_value(serialized_value
);
65 syncer::SyncData change_data
= syncer::SyncData::CreateRemoteData(
69 syncer::AttachmentIdList(),
70 syncer::AttachmentServiceProxyForTest::Create());
71 syncer::SyncChange
change(
72 FROM_HERE
, syncer::SyncChange::ACTION_UPDATE
, change_data
);
74 syncer::SyncChangeList change_list
;
75 change_list
.push_back(change
);
77 syncer_
->ProcessSyncChanges(FROM_HERE
, change_list
);
80 void SetBooleanPrefValueFromLocal(const std::string
& name
, bool value
) {
81 prefs_
->SetBoolean(name
.c_str(), value
);
84 bool GetBooleanPrefValue(const std::string
& name
) {
85 return prefs_
->GetBoolean(name
.c_str());
88 syncable_prefs::PrefServiceSyncable
* prefs() const {
92 syncable_prefs::SyncedPrefChangeRegistrar
* registrar() const {
93 return registrar_
.get();
97 #if defined(ENABLE_CONFIGURATION_POLICY)
98 void SetUpInProcessBrowserTestFixture() override
{
99 EXPECT_CALL(policy_provider_
, IsInitializationComplete(_
))
100 .WillRepeatedly(Return(true));
101 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
106 void SetUpOnMainThread() override
{
107 prefs_
= PrefServiceSyncableFromProfile(browser()->profile());
108 syncer_
= prefs_
->GetSyncableService(syncer::PREFERENCES
);
109 syncer_
->MergeDataAndStartSyncing(
111 syncer::SyncDataList(),
112 scoped_ptr
<syncer::SyncChangeProcessor
>(
113 new syncer::FakeSyncChangeProcessor
),
114 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock
));
115 registrar_
.reset(new syncable_prefs::SyncedPrefChangeRegistrar(prefs_
));
118 void TearDownOnMainThread() override
{ registrar_
.reset(); }
120 syncable_prefs::PrefServiceSyncable
* prefs_
;
121 syncer::SyncableService
* syncer_
;
122 int next_sync_data_id_
;
124 scoped_ptr
<syncable_prefs::SyncedPrefChangeRegistrar
> registrar_
;
125 #if defined(ENABLE_CONFIGURATION_POLICY)
126 policy::MockConfigurationPolicyProvider policy_provider_
;
130 struct TestSyncedPrefObserver
{
131 bool last_seen_value
;
132 bool last_update_is_from_sync
;
133 bool has_been_notified
;
136 void TestPrefChangeCallback(PrefService
* prefs
,
137 TestSyncedPrefObserver
* observer
,
138 const std::string
& path
,
140 observer
->last_seen_value
= prefs
->GetBoolean(path
.c_str());
141 observer
->last_update_is_from_sync
= from_sync
;
142 observer
->has_been_notified
= true;
147 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest
,
148 DifferentiateRemoteAndLocalChanges
) {
149 TestSyncedPrefObserver observer
= {};
150 registrar()->Add(prefs::kShowHomeButton
,
151 base::Bind(&TestPrefChangeCallback
, prefs(), &observer
));
153 EXPECT_FALSE(observer
.has_been_notified
);
155 SetBooleanPrefValueFromSync(prefs::kShowHomeButton
, true);
156 EXPECT_TRUE(observer
.has_been_notified
);
157 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton
));
158 EXPECT_TRUE(observer
.last_update_is_from_sync
);
159 EXPECT_TRUE(observer
.last_seen_value
);
161 observer
.has_been_notified
= false;
162 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton
, false);
163 EXPECT_TRUE(observer
.has_been_notified
);
164 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton
));
165 EXPECT_FALSE(observer
.last_update_is_from_sync
);
166 EXPECT_FALSE(observer
.last_seen_value
);
168 observer
.has_been_notified
= false;
169 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton
, true);
170 EXPECT_TRUE(observer
.has_been_notified
);
171 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton
));
172 EXPECT_FALSE(observer
.last_update_is_from_sync
);
173 EXPECT_TRUE(observer
.last_seen_value
);
175 observer
.has_been_notified
= false;
176 SetBooleanPrefValueFromSync(prefs::kShowHomeButton
, false);
177 EXPECT_TRUE(observer
.has_been_notified
);
178 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton
));
179 EXPECT_TRUE(observer
.last_update_is_from_sync
);
180 EXPECT_FALSE(observer
.last_seen_value
);
183 #if defined(ENABLE_CONFIGURATION_POLICY)
184 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest
,
185 IgnoreLocalChangesToManagedPrefs
) {
186 TestSyncedPrefObserver observer
= {};
187 registrar()->Add(prefs::kShowHomeButton
,
188 base::Bind(&TestPrefChangeCallback
, prefs(), &observer
));
190 policy::PolicyMap policies
;
191 policies
.Set(policy::key::kShowHomeButton
,
192 policy::POLICY_LEVEL_MANDATORY
,
193 policy::POLICY_SCOPE_USER
,
194 new base::FundamentalValue(true),
196 UpdateChromePolicy(policies
);
198 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton
));
200 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton
, false);
201 EXPECT_FALSE(observer
.has_been_notified
);
202 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton
));
205 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest
,
206 IgnoreSyncChangesToManagedPrefs
) {
207 TestSyncedPrefObserver observer
= {};
208 registrar()->Add(prefs::kShowHomeButton
,
209 base::Bind(&TestPrefChangeCallback
, prefs(), &observer
));
211 policy::PolicyMap policies
;
212 policies
.Set(policy::key::kShowHomeButton
,
213 policy::POLICY_LEVEL_MANDATORY
,
214 policy::POLICY_SCOPE_USER
,
215 new base::FundamentalValue(true),
217 UpdateChromePolicy(policies
);
219 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton
));
220 SetBooleanPrefValueFromSync(prefs::kShowHomeButton
, false);
221 EXPECT_FALSE(observer
.has_been_notified
);
222 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton
));