Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / prefs / synced_pref_change_registrar_browsertest.cc
blobaf9a3618807471c29c728c829ce3fe99aaa995be
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/attachments/attachment_id.h"
19 #include "sync/api/fake_sync_change_processor.h"
20 #include "sync/api/sync_change.h"
21 #include "sync/api/sync_error_factory.h"
22 #include "sync/api/sync_error_factory_mock.h"
23 #include "sync/api/syncable_service.h"
24 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
25 #include "sync/protocol/sync.pb.h"
27 #if defined(ENABLE_CONFIGURATION_POLICY)
28 #include "components/policy/core/browser/browser_policy_connector.h"
29 #include "components/policy/core/common/mock_configuration_policy_provider.h"
30 #include "components/policy/core/common/policy_map.h"
31 #include "policy/policy_constants.h"
32 #endif
34 namespace {
36 using testing::Return;
37 using testing::_;
39 class SyncedPrefChangeRegistrarTest : public InProcessBrowserTest {
40 public:
41 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {}
42 ~SyncedPrefChangeRegistrarTest() override {}
44 #if defined(ENABLE_CONFIGURATION_POLICY)
45 void UpdateChromePolicy(const policy::PolicyMap& policies) {
46 policy_provider_.UpdateChromePolicy(policies);
47 DCHECK(base::MessageLoop::current());
48 base::RunLoop loop;
49 loop.RunUntilIdle();
51 #endif
53 void SetBooleanPrefValueFromSync(const std::string& name, bool value) {
54 std::string serialized_value;
55 JSONStringValueSerializer json(&serialized_value);
56 json.Serialize(base::FundamentalValue(value));
58 sync_pb::EntitySpecifics specifics;
59 sync_pb::PreferenceSpecifics* pref_specifics =
60 specifics.mutable_preference();
61 pref_specifics->set_name(name);
62 pref_specifics->set_value(serialized_value);
64 syncer::SyncData change_data = syncer::SyncData::CreateRemoteData(
65 ++next_sync_data_id_,
66 specifics,
67 base::Time(),
68 syncer::AttachmentIdList(),
69 syncer::AttachmentServiceProxyForTest::Create());
70 syncer::SyncChange change(
71 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, change_data);
73 syncer::SyncChangeList change_list;
74 change_list.push_back(change);
76 syncer_->ProcessSyncChanges(FROM_HERE, change_list);
79 void SetBooleanPrefValueFromLocal(const std::string& name, bool value) {
80 prefs_->SetBoolean(name.c_str(), value);
83 bool GetBooleanPrefValue(const std::string& name) {
84 return prefs_->GetBoolean(name.c_str());
87 PrefServiceSyncable* prefs() const {
88 return prefs_;
91 SyncedPrefChangeRegistrar* registrar() const {
92 return registrar_.get();
95 private:
96 #if defined(ENABLE_CONFIGURATION_POLICY)
97 void SetUpInProcessBrowserTestFixture() override {
98 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
99 .WillRepeatedly(Return(true));
100 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
101 &policy_provider_);
103 #endif
105 void SetUpOnMainThread() override {
106 prefs_ = PrefServiceSyncable::FromProfile(browser()->profile());
107 syncer_ = prefs_->GetSyncableService(syncer::PREFERENCES);
108 syncer_->MergeDataAndStartSyncing(
109 syncer::PREFERENCES,
110 syncer::SyncDataList(),
111 scoped_ptr<syncer::SyncChangeProcessor>(
112 new syncer::FakeSyncChangeProcessor),
113 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock));
114 registrar_.reset(new SyncedPrefChangeRegistrar(prefs_));
117 void TearDownOnMainThread() override { registrar_.reset(); }
119 PrefServiceSyncable* prefs_;
120 syncer::SyncableService* syncer_;
121 int next_sync_data_id_;
123 scoped_ptr<SyncedPrefChangeRegistrar> registrar_;
124 #if defined(ENABLE_CONFIGURATION_POLICY)
125 policy::MockConfigurationPolicyProvider policy_provider_;
126 #endif
129 struct TestSyncedPrefObserver {
130 bool last_seen_value;
131 bool last_update_is_from_sync;
132 bool has_been_notified;
135 void TestPrefChangeCallback(PrefService* prefs,
136 TestSyncedPrefObserver* observer,
137 const std::string& path,
138 bool from_sync) {
139 observer->last_seen_value = prefs->GetBoolean(path.c_str());
140 observer->last_update_is_from_sync = from_sync;
141 observer->has_been_notified = true;
144 } // namespace
146 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
147 DifferentiateRemoteAndLocalChanges) {
148 TestSyncedPrefObserver observer = {};
149 registrar()->Add(prefs::kShowHomeButton,
150 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
152 EXPECT_FALSE(observer.has_been_notified);
154 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, true);
155 EXPECT_TRUE(observer.has_been_notified);
156 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
157 EXPECT_TRUE(observer.last_update_is_from_sync);
158 EXPECT_TRUE(observer.last_seen_value);
160 observer.has_been_notified = false;
161 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
162 EXPECT_TRUE(observer.has_been_notified);
163 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
164 EXPECT_FALSE(observer.last_update_is_from_sync);
165 EXPECT_FALSE(observer.last_seen_value);
167 observer.has_been_notified = false;
168 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, true);
169 EXPECT_TRUE(observer.has_been_notified);
170 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
171 EXPECT_FALSE(observer.last_update_is_from_sync);
172 EXPECT_TRUE(observer.last_seen_value);
174 observer.has_been_notified = false;
175 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
176 EXPECT_TRUE(observer.has_been_notified);
177 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton));
178 EXPECT_TRUE(observer.last_update_is_from_sync);
179 EXPECT_FALSE(observer.last_seen_value);
182 #if defined(ENABLE_CONFIGURATION_POLICY)
183 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
184 IgnoreLocalChangesToManagedPrefs) {
185 TestSyncedPrefObserver observer = {};
186 registrar()->Add(prefs::kShowHomeButton,
187 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
189 policy::PolicyMap policies;
190 policies.Set(policy::key::kShowHomeButton,
191 policy::POLICY_LEVEL_MANDATORY,
192 policy::POLICY_SCOPE_USER,
193 new base::FundamentalValue(true),
194 NULL);
195 UpdateChromePolicy(policies);
197 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
199 SetBooleanPrefValueFromLocal(prefs::kShowHomeButton, false);
200 EXPECT_FALSE(observer.has_been_notified);
201 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
204 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest,
205 IgnoreSyncChangesToManagedPrefs) {
206 TestSyncedPrefObserver observer = {};
207 registrar()->Add(prefs::kShowHomeButton,
208 base::Bind(&TestPrefChangeCallback, prefs(), &observer));
210 policy::PolicyMap policies;
211 policies.Set(policy::key::kShowHomeButton,
212 policy::POLICY_LEVEL_MANDATORY,
213 policy::POLICY_SCOPE_USER,
214 new base::FundamentalValue(true),
215 NULL);
216 UpdateChromePolicy(policies);
218 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton));
219 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false);
220 EXPECT_FALSE(observer.has_been_notified);
221 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton));
223 #endif