OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/prefs/pref_service_syncable_util.h" | 11 #include "chrome/browser/prefs/pref_service_syncable_util.h" |
12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/policy/core/browser/browser_policy_connector.h" |
| 17 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 18 #include "components/policy/core/common/policy_map.h" |
| 19 #include "components/policy/core/common/policy_types.h" |
16 #include "components/syncable_prefs/synced_pref_change_registrar.h" | 20 #include "components/syncable_prefs/synced_pref_change_registrar.h" |
17 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 21 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
18 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
| 23 #include "policy/policy_constants.h" |
19 #include "sync/api/attachments/attachment_id.h" | 24 #include "sync/api/attachments/attachment_id.h" |
20 #include "sync/api/fake_sync_change_processor.h" | 25 #include "sync/api/fake_sync_change_processor.h" |
21 #include "sync/api/sync_change.h" | 26 #include "sync/api/sync_change.h" |
22 #include "sync/api/sync_error_factory.h" | 27 #include "sync/api/sync_error_factory.h" |
23 #include "sync/api/sync_error_factory_mock.h" | 28 #include "sync/api/sync_error_factory_mock.h" |
24 #include "sync/api/syncable_service.h" | 29 #include "sync/api/syncable_service.h" |
25 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" | 30 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" |
26 #include "sync/protocol/sync.pb.h" | 31 #include "sync/protocol/sync.pb.h" |
27 | 32 |
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 | |
35 | |
36 namespace { | 33 namespace { |
37 | 34 |
38 using testing::Return; | 35 using testing::Return; |
39 using testing::_; | 36 using testing::_; |
40 | 37 |
41 class SyncedPrefChangeRegistrarTest : public InProcessBrowserTest { | 38 class SyncedPrefChangeRegistrarTest : public InProcessBrowserTest { |
42 public: | 39 public: |
43 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {} | 40 SyncedPrefChangeRegistrarTest() : next_sync_data_id_(0) {} |
44 ~SyncedPrefChangeRegistrarTest() override {} | 41 ~SyncedPrefChangeRegistrarTest() override {} |
45 | 42 |
46 #if defined(ENABLE_CONFIGURATION_POLICY) | |
47 void UpdateChromePolicy(const policy::PolicyMap& policies) { | 43 void UpdateChromePolicy(const policy::PolicyMap& policies) { |
48 policy_provider_.UpdateChromePolicy(policies); | 44 policy_provider_.UpdateChromePolicy(policies); |
49 DCHECK(base::MessageLoop::current()); | 45 DCHECK(base::MessageLoop::current()); |
50 base::RunLoop loop; | 46 base::RunLoop loop; |
51 loop.RunUntilIdle(); | 47 loop.RunUntilIdle(); |
52 } | 48 } |
53 #endif | |
54 | 49 |
55 void SetBooleanPrefValueFromSync(const std::string& name, bool value) { | 50 void SetBooleanPrefValueFromSync(const std::string& name, bool value) { |
56 std::string serialized_value; | 51 std::string serialized_value; |
57 JSONStringValueSerializer json(&serialized_value); | 52 JSONStringValueSerializer json(&serialized_value); |
58 json.Serialize(base::FundamentalValue(value)); | 53 json.Serialize(base::FundamentalValue(value)); |
59 | 54 |
60 sync_pb::EntitySpecifics specifics; | 55 sync_pb::EntitySpecifics specifics; |
61 sync_pb::PreferenceSpecifics* pref_specifics = | 56 sync_pb::PreferenceSpecifics* pref_specifics = |
62 specifics.mutable_preference(); | 57 specifics.mutable_preference(); |
63 pref_specifics->set_name(name); | 58 pref_specifics->set_name(name); |
(...skipping 24 matching lines...) Expand all Loading... |
88 | 83 |
89 syncable_prefs::PrefServiceSyncable* prefs() const { | 84 syncable_prefs::PrefServiceSyncable* prefs() const { |
90 return prefs_; | 85 return prefs_; |
91 } | 86 } |
92 | 87 |
93 syncable_prefs::SyncedPrefChangeRegistrar* registrar() const { | 88 syncable_prefs::SyncedPrefChangeRegistrar* registrar() const { |
94 return registrar_.get(); | 89 return registrar_.get(); |
95 } | 90 } |
96 | 91 |
97 private: | 92 private: |
98 #if defined(ENABLE_CONFIGURATION_POLICY) | |
99 void SetUpInProcessBrowserTestFixture() override { | 93 void SetUpInProcessBrowserTestFixture() override { |
100 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) | 94 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) |
101 .WillRepeatedly(Return(true)); | 95 .WillRepeatedly(Return(true)); |
102 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( | 96 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( |
103 &policy_provider_); | 97 &policy_provider_); |
104 } | 98 } |
105 #endif | |
106 | 99 |
107 void SetUpOnMainThread() override { | 100 void SetUpOnMainThread() override { |
108 prefs_ = PrefServiceSyncableFromProfile(browser()->profile()); | 101 prefs_ = PrefServiceSyncableFromProfile(browser()->profile()); |
109 syncer_ = prefs_->GetSyncableService(syncer::PREFERENCES); | 102 syncer_ = prefs_->GetSyncableService(syncer::PREFERENCES); |
110 syncer_->MergeDataAndStartSyncing( | 103 syncer_->MergeDataAndStartSyncing( |
111 syncer::PREFERENCES, | 104 syncer::PREFERENCES, |
112 syncer::SyncDataList(), | 105 syncer::SyncDataList(), |
113 scoped_ptr<syncer::SyncChangeProcessor>( | 106 scoped_ptr<syncer::SyncChangeProcessor>( |
114 new syncer::FakeSyncChangeProcessor), | 107 new syncer::FakeSyncChangeProcessor), |
115 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock)); | 108 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock)); |
116 registrar_.reset(new syncable_prefs::SyncedPrefChangeRegistrar(prefs_)); | 109 registrar_.reset(new syncable_prefs::SyncedPrefChangeRegistrar(prefs_)); |
117 } | 110 } |
118 | 111 |
119 void TearDownOnMainThread() override { registrar_.reset(); } | 112 void TearDownOnMainThread() override { registrar_.reset(); } |
120 | 113 |
121 syncable_prefs::PrefServiceSyncable* prefs_; | 114 syncable_prefs::PrefServiceSyncable* prefs_; |
122 syncer::SyncableService* syncer_; | 115 syncer::SyncableService* syncer_; |
123 int next_sync_data_id_; | 116 int next_sync_data_id_; |
124 | 117 |
125 scoped_ptr<syncable_prefs::SyncedPrefChangeRegistrar> registrar_; | 118 scoped_ptr<syncable_prefs::SyncedPrefChangeRegistrar> registrar_; |
126 #if defined(ENABLE_CONFIGURATION_POLICY) | |
127 policy::MockConfigurationPolicyProvider policy_provider_; | 119 policy::MockConfigurationPolicyProvider policy_provider_; |
128 #endif | |
129 }; | 120 }; |
130 | 121 |
131 struct TestSyncedPrefObserver { | 122 struct TestSyncedPrefObserver { |
132 bool last_seen_value; | 123 bool last_seen_value; |
133 bool last_update_is_from_sync; | 124 bool last_update_is_from_sync; |
134 bool has_been_notified; | 125 bool has_been_notified; |
135 }; | 126 }; |
136 | 127 |
137 void TestPrefChangeCallback(PrefService* prefs, | 128 void TestPrefChangeCallback(PrefService* prefs, |
138 TestSyncedPrefObserver* observer, | 129 TestSyncedPrefObserver* observer, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 EXPECT_TRUE(observer.last_seen_value); | 165 EXPECT_TRUE(observer.last_seen_value); |
175 | 166 |
176 observer.has_been_notified = false; | 167 observer.has_been_notified = false; |
177 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false); | 168 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false); |
178 EXPECT_TRUE(observer.has_been_notified); | 169 EXPECT_TRUE(observer.has_been_notified); |
179 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton)); | 170 EXPECT_FALSE(GetBooleanPrefValue(prefs::kShowHomeButton)); |
180 EXPECT_TRUE(observer.last_update_is_from_sync); | 171 EXPECT_TRUE(observer.last_update_is_from_sync); |
181 EXPECT_FALSE(observer.last_seen_value); | 172 EXPECT_FALSE(observer.last_seen_value); |
182 } | 173 } |
183 | 174 |
184 #if defined(ENABLE_CONFIGURATION_POLICY) | |
185 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest, | 175 IN_PROC_BROWSER_TEST_F(SyncedPrefChangeRegistrarTest, |
186 IgnoreLocalChangesToManagedPrefs) { | 176 IgnoreLocalChangesToManagedPrefs) { |
187 TestSyncedPrefObserver observer = {}; | 177 TestSyncedPrefObserver observer = {}; |
188 registrar()->Add(prefs::kShowHomeButton, | 178 registrar()->Add(prefs::kShowHomeButton, |
189 base::Bind(&TestPrefChangeCallback, prefs(), &observer)); | 179 base::Bind(&TestPrefChangeCallback, prefs(), &observer)); |
190 | 180 |
191 policy::PolicyMap policies; | 181 policy::PolicyMap policies; |
192 policies.Set(policy::key::kShowHomeButton, | 182 policies.Set(policy::key::kShowHomeButton, |
193 policy::POLICY_LEVEL_MANDATORY, | 183 policy::POLICY_LEVEL_MANDATORY, |
194 policy::POLICY_SCOPE_USER, | 184 policy::POLICY_SCOPE_USER, |
(...skipping 22 matching lines...) Expand all Loading... |
217 policy::POLICY_SOURCE_CLOUD, | 207 policy::POLICY_SOURCE_CLOUD, |
218 new base::FundamentalValue(true), | 208 new base::FundamentalValue(true), |
219 NULL); | 209 NULL); |
220 UpdateChromePolicy(policies); | 210 UpdateChromePolicy(policies); |
221 | 211 |
222 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton)); | 212 EXPECT_TRUE(prefs()->IsManagedPreference(prefs::kShowHomeButton)); |
223 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false); | 213 SetBooleanPrefValueFromSync(prefs::kShowHomeButton, false); |
224 EXPECT_FALSE(observer.has_been_notified); | 214 EXPECT_FALSE(observer.has_been_notified); |
225 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton)); | 215 EXPECT_TRUE(GetBooleanPrefValue(prefs::kShowHomeButton)); |
226 } | 216 } |
227 #endif | |
OLD | NEW |