Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / prefs / synced_pref_change_registrar_browsertest.cc
blob3c443103430ca8c1f2a62173e5d5447d9a277f5b
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/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 "components/policy/core/common/policy_types.h"
33 #include "policy/policy_constants.h"
34 #endif
36 namespace {
38 using testing::Return;
39 using testing::_;
41 class SyncedPrefChangeRegistrarTest : public InProcessBrowserTest {
42 public:
43 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {}
44 ~SyncedPrefChangeRegistrarTest() override {}
46 #if defined(ENABLE_CONFIGURATION_POLICY)
47 void UpdateChromePolicy(const policy::PolicyMap& policies) {
48 policy_provider_.UpdateChromePolicy(policies);
49 DCHECK(base::MessageLoop::current());
50 base::RunLoop loop;
51 loop.RunUntilIdle();
53 #endif
55 void SetBooleanPrefValueFromSync(const std::string& name, bool value) {
56 std::string serialized_value;
57 JSONStringValueSerializer json(&serialized_value);
58 json.Serialize(base::FundamentalValue(value));
60 sync_pb::EntitySpecifics specifics;
61 sync_pb::PreferenceSpecifics* pref_specifics =
62 specifics.mutable_preference();
63 pref_specifics->set_name(name);
64 pref_specifics->set_value(serialized_value);
66 syncer::SyncData change_data = syncer::SyncData::CreateRemoteData(
67 ++next_sync_data_id_,
68 specifics,
69 base::Time(),
70 syncer::AttachmentIdList(),
71 syncer::AttachmentServiceProxyForTest::Create());
72 syncer::SyncChange change(
73 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, change_data);
75 syncer::SyncChangeList change_list;
76 change_list.push_back(change);
78 syncer_->ProcessSyncChanges(FROM_HERE, change_list);
81 void SetBooleanPrefValueFromLocal(const std::string& name, bool value) {
82 prefs_->SetBoolean(name.c_str(), value);
85 bool GetBooleanPrefValue(const std::string& name) {
86 return prefs_->GetBoolean(name.c_str());
89 syncable_prefs::PrefServiceSyncable* prefs() const {
90 return prefs_;
93 syncable_prefs::SyncedPrefChangeRegistrar* registrar() const {
94 return registrar_.get();
97 private:
98 #if defined(ENABLE_CONFIGURATION_POLICY)
99 void SetUpInProcessBrowserTestFixture() override {
100 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
101 .WillRepeatedly(Return(true));
102 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
103 &policy_provider_);
105 #endif
107 void SetUpOnMainThread() override {
108 prefs_ = PrefServiceSyncableFromProfile(browser()->profile());
109 syncer_ = prefs_->GetSyncableService(syncer::PREFERENCES);
110 syncer_->MergeDataAndStartSyncing(
111 syncer::PREFERENCES,
112 syncer::SyncDataList(),
113 scoped_ptr<syncer::SyncChangeProcessor>(
114 new syncer::FakeSyncChangeProcessor),
115 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock));
116 registrar_.reset(new syncable_prefs::SyncedPrefChangeRegistrar(prefs_));
119 void TearDownOnMainThread() override { registrar_.reset(); }
121 syncable_prefs::PrefServiceSyncable* prefs_;
122 syncer::SyncableService* syncer_;
123 int next_sync_data_id_;
125 scoped_ptr<syncable_prefs::SyncedPrefChangeRegistrar> registrar_;
126 #if defined(ENABLE_CONFIGURATION_POLICY)
127 policy::MockConfigurationPolicyProvider policy_provider_;
128 #endif
131 struct TestSyncedPrefObserver {
132 bool last_seen_value;
133 bool last_update_is_from_sync;
134 bool has_been_notified;
137 void TestPrefChangeCallback(PrefService* prefs,
138 TestSyncedPrefObserver* observer,
139 const std::string& path,
140 bool from_sync) {
141 observer->last_seen_value = prefs->GetBoolean(path.c_str());
142 observer->last_update_is_from_sync = from_sync;
143 observer->has_been_notified = true;
146 } // namespace
148 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
149 DifferentiateRemoteAndLocalChanges) {
150 TestSyncedPrefObserver observer = {};
151 registrar()->Add(prefs::kShowHomeButton,
152 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
154 EXPECT_FALSE(observer.has_been_notified);
156 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, true);
157 EXPECT_TRUE(observer.has_been_notified);
158 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
159 EXPECT_TRUE(observer.last_update_is_from_sync);
160 EXPECT_TRUE(observer.last_seen_value);
162 observer.has_been_notified = false;
163 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
164 EXPECT_TRUE(observer.has_been_notified);
165 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
166 EXPECT_FALSE(observer.last_update_is_from_sync);
167 EXPECT_FALSE(observer.last_seen_value);
169 observer.has_been_notified = false;
170 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, true);
171 EXPECT_TRUE(observer.has_been_notified);
172 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
173 EXPECT_FALSE(observer.last_update_is_from_sync);
174 EXPECT_TRUE(observer.last_seen_value);
176 observer.has_been_notified = false;
177 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
178 EXPECT_TRUE(observer.has_been_notified);
179 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
180 EXPECT_TRUE(observer.last_update_is_from_sync);
181 EXPECT_FALSE(observer.last_seen_value);
184 #if defined(ENABLE_CONFIGURATION_POLICY)
185 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
186 IgnoreLocalChangesToManagedPrefs) {
187 TestSyncedPrefObserver observer = {};
188 registrar()->Add(prefs::kShowHomeButton,
189 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
191 policy::PolicyMap policies;
192 policies.Set(policy::key::kShowHomeButton,
193 policy::POLICY_LEVEL_MANDATORY,
194 policy::POLICY_SCOPE_USER,
195 policy::POLICY_SOURCE_CLOUD,
196 new base::FundamentalValue(true),
197 NULL);
198 UpdateChromePolicy(policies);
200 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
202 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
203 EXPECT_FALSE(observer.has_been_notified);
204 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
207 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
208 IgnoreSyncChangesToManagedPrefs) {
209 TestSyncedPrefObserver observer = {};
210 registrar()->Add(prefs::kShowHomeButton,
211 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
213 policy::PolicyMap policies;
214 policies.Set(policy::key::kShowHomeButton,
215 policy::POLICY_LEVEL_MANDATORY,
216 policy::POLICY_SCOPE_USER,
217 policy::POLICY_SOURCE_CLOUD,
218 new base::FundamentalValue(true),
219 NULL);
220 UpdateChromePolicy(policies);
222 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
223 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
224 EXPECT_FALSE(observer.has_been_notified);
225 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
227 #endif