Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / prefs / synced_pref_change_registrar_browsertest.cc
blobe7c0b5eed102d4bb10e13f0c428948768fc2a28e
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.
5 #include <string>
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/synced_pref_change_registrar.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_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/test/test_utils.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_error_factory.h"
20 #include "sync/api/sync_error_factory_mock.h"
21 #include "sync/api/syncable_service.h"
22 #include "sync/protocol/sync.pb.h"
24 #if defined(ENABLE_CONFIGURATION_POLICY)
25 #include "chrome/browser/policy/browser_policy_connector.h"
26 #include "components/policy/core/common/mock_configuration_policy_provider.h"
27 #include "components/policy/core/common/policy_map.h"
28 #include "policy/policy_constants.h"
29 #endif
31 namespace {
33 using testing::Return;
34 using testing::_;
36 class TestSyncProcessorStub : public syncer::SyncChangeProcessor {
37 virtual syncer::SyncError ProcessSyncChanges(
38 const tracked_objects::Location& from_here,
39 const syncer::SyncChangeList& change_list) OVERRIDE {
40 return syncer::SyncError();
43 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const
44 OVERRIDE {
45 return syncer::SyncDataList();
49 class SyncedPrefChangeRegistrarTest : public InProcessBrowserTest {
50 public:
51 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {}
52 virtual ~SyncedPrefChangeRegistrarTest() {}
54 #if defined(ENABLE_CONFIGURATION_POLICY)
55 void UpdateChromePolicy(const policy::PolicyMap& policies) {
56 policy_provider_.UpdateChromePolicy(policies);
57 DCHECK(base::MessageLoop::current());
58 base::RunLoop loop;
59 loop.RunUntilIdle();
61 #endif
63 void SetBooleanPrefValueFromSync(const std::string& name, bool value) {
64 std::string serialized_value;
65 JSONStringValueSerializer json(&serialized_value);
66 json.Serialize(base::FundamentalValue(value));
68 sync_pb::EntitySpecifics specifics;
69 sync_pb::PreferenceSpecifics* pref_specifics =
70 specifics.mutable_preference();
71 pref_specifics->set_name(name);
72 pref_specifics->set_value(serialized_value);
74 syncer::SyncData change_data = syncer::SyncData::CreateRemoteData(
75 ++next_sync_data_id_, specifics, base::Time());
76 syncer::SyncChange change(
77 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, change_data);
79 syncer::SyncChangeList change_list;
80 change_list.push_back(change);
82 syncer_->ProcessSyncChanges(FROM_HERE, change_list);
85 void SetBooleanPrefValueFromLocal(const std::string& name, bool value) {
86 prefs_->SetBoolean(name.c_str(), value);
89 bool GetBooleanPrefValue(const std::string& name) {
90 return prefs_->GetBoolean(name.c_str());
93 PrefServiceSyncable* prefs() const {
94 return prefs_;
97 SyncedPrefChangeRegistrar* registrar() const {
98 return registrar_.get();
101 private:
102 #if defined(ENABLE_CONFIGURATION_POLICY)
103 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
104 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
105 .WillRepeatedly(Return(true));
106 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
107 &policy_provider_);
109 #endif
111 virtual void SetUpOnMainThread() OVERRIDE {
112 prefs_ = PrefServiceSyncable::FromProfile(browser()->profile());
113 syncer_ = prefs_->GetSyncableService(syncer::PREFERENCES);
114 syncer_->MergeDataAndStartSyncing(
115 syncer::PREFERENCES,
116 syncer::SyncDataList(),
117 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
118 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock));
119 registrar_.reset(new SyncedPrefChangeRegistrar(prefs_));
122 virtual void CleanUpOnMainThread() OVERRIDE {
123 registrar_.reset();
126 PrefServiceSyncable* prefs_;
127 syncer::SyncableService* syncer_;
128 int next_sync_data_id_;
130 scoped_ptr<SyncedPrefChangeRegistrar> registrar_;
131 #if defined(ENABLE_CONFIGURATION_POLICY)
132 policy::MockConfigurationPolicyProvider policy_provider_;
133 #endif
136 struct TestSyncedPrefObserver {
137 bool last_seen_value;
138 bool last_update_is_from_sync;
139 bool has_been_notified;
142 void TestPrefChangeCallback(PrefService* prefs,
143 TestSyncedPrefObserver* observer,
144 const std::string& path,
145 bool from_sync) {
146 observer->last_seen_value = prefs->GetBoolean(path.c_str());
147 observer->last_update_is_from_sync = from_sync;
148 observer->has_been_notified = true;
151 } // namespace
153 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
154 DifferentiateRemoteAndLocalChanges) {
155 TestSyncedPrefObserver observer = {};
156 registrar()->Add(prefs::kShowHomeButton,
157 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
159 EXPECT_FALSE(observer.has_been_notified);
161 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, true);
162 EXPECT_TRUE(observer.has_been_notified);
163 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
164 EXPECT_TRUE(observer.last_update_is_from_sync);
165 EXPECT_TRUE(observer.last_seen_value);
167 observer.has_been_notified = false;
168 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
169 EXPECT_TRUE(observer.has_been_notified);
170 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
171 EXPECT_FALSE(observer.last_update_is_from_sync);
172 EXPECT_FALSE(observer.last_seen_value);
174 observer.has_been_notified = false;
175 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, true);
176 EXPECT_TRUE(observer.has_been_notified);
177 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
178 EXPECT_FALSE(observer.last_update_is_from_sync);
179 EXPECT_TRUE(observer.last_seen_value);
181 observer.has_been_notified = false;
182 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
183 EXPECT_TRUE(observer.has_been_notified);
184 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
185 EXPECT_TRUE(observer.last_update_is_from_sync);
186 EXPECT_FALSE(observer.last_seen_value);
189 #if defined(ENABLE_CONFIGURATION_POLICY)
190 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
191 IgnoreLocalChangesToManagedPrefs) {
192 TestSyncedPrefObserver observer = {};
193 registrar()->Add(prefs::kShowHomeButton,
194 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
196 policy::PolicyMap policies;
197 policies.Set(policy::key::kShowHomeButton,
198 policy::POLICY_LEVEL_MANDATORY,
199 policy::POLICY_SCOPE_USER,
200 base::Value::CreateBooleanValue(true),
201 NULL);
202 UpdateChromePolicy(policies);
204 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
206 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
207 EXPECT_FALSE(observer.has_been_notified);
208 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
211 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
212 IgnoreSyncChangesToManagedPrefs) {
213 TestSyncedPrefObserver observer = {};
214 registrar()->Add(prefs::kShowHomeButton,
215 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
217 policy::PolicyMap policies;
218 policies.Set(policy::key::kShowHomeButton,
219 policy::POLICY_LEVEL_MANDATORY,
220 policy::POLICY_SCOPE_USER,
221 base::Value::CreateBooleanValue(true),
222 NULL);
223 UpdateChromePolicy(policies);
225 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
226 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
227 EXPECT_FALSE(observer.has_been_notified);
228 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
230 #endif