OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "components/sync_driver/sync_prefs.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <map> | |
10 | |
11 #include "base/command_line.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/time/time.h" | |
14 #include "components/pref_registry/testing_pref_service_syncable.h" | |
15 #include "components/prefs/pref_notifier_impl.h" | |
16 #include "components/prefs/pref_value_store.h" | |
17 #include "components/prefs/testing_pref_service.h" | |
18 #include "components/sync/base/model_type.h" | |
19 #include "components/sync_driver/pref_names.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace sync_driver { | |
24 | |
25 namespace { | |
26 | |
27 using ::testing::InSequence; | |
28 using ::testing::StrictMock; | |
29 | |
30 class SyncPrefsTest : public testing::Test { | |
31 protected: | |
32 void SetUp() override { | |
33 SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); | |
34 } | |
35 | |
36 user_prefs::TestingPrefServiceSyncable pref_service_; | |
37 | |
38 private: | |
39 base::MessageLoop loop_; | |
40 }; | |
41 | |
42 TEST_F(SyncPrefsTest, Basic) { | |
43 SyncPrefs sync_prefs(&pref_service_); | |
44 | |
45 EXPECT_FALSE(sync_prefs.IsFirstSetupComplete()); | |
46 sync_prefs.SetFirstSetupComplete(); | |
47 EXPECT_TRUE(sync_prefs.IsFirstSetupComplete()); | |
48 | |
49 EXPECT_TRUE(sync_prefs.IsSyncRequested()); | |
50 sync_prefs.SetSyncRequested(false); | |
51 EXPECT_FALSE(sync_prefs.IsSyncRequested()); | |
52 sync_prefs.SetSyncRequested(true); | |
53 EXPECT_TRUE(sync_prefs.IsSyncRequested()); | |
54 | |
55 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime()); | |
56 const base::Time& now = base::Time::Now(); | |
57 sync_prefs.SetLastSyncedTime(now); | |
58 EXPECT_EQ(now, sync_prefs.GetLastSyncedTime()); | |
59 | |
60 EXPECT_TRUE(sync_prefs.HasKeepEverythingSynced()); | |
61 sync_prefs.SetKeepEverythingSynced(false); | |
62 EXPECT_FALSE(sync_prefs.HasKeepEverythingSynced()); | |
63 sync_prefs.SetKeepEverythingSynced(true); | |
64 EXPECT_TRUE(sync_prefs.HasKeepEverythingSynced()); | |
65 | |
66 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty()); | |
67 sync_prefs.SetEncryptionBootstrapToken("token"); | |
68 EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken()); | |
69 } | |
70 | |
71 TEST_F(SyncPrefsTest, DefaultTypes) { | |
72 SyncPrefs sync_prefs(&pref_service_); | |
73 sync_prefs.SetKeepEverythingSynced(false); | |
74 | |
75 // Only bookmarks and device info are enabled by default. | |
76 syncer::ModelTypeSet expected(syncer::BOOKMARKS, syncer::DEVICE_INFO); | |
77 syncer::ModelTypeSet preferred_types = | |
78 sync_prefs.GetPreferredDataTypes(syncer::UserTypes()); | |
79 EXPECT_EQ(expected, preferred_types); | |
80 | |
81 // Simulate an upgrade to delete directives + proxy tabs support. None of the | |
82 // new types or their pref group types should be registering, ensuring they | |
83 // don't have pref values. | |
84 syncer::ModelTypeSet registered_types = syncer::UserTypes(); | |
85 registered_types.Remove(syncer::PROXY_TABS); | |
86 registered_types.Remove(syncer::TYPED_URLS); | |
87 registered_types.Remove(syncer::SESSIONS); | |
88 registered_types.Remove(syncer::HISTORY_DELETE_DIRECTIVES); | |
89 | |
90 // Enable all other types. | |
91 sync_prefs.SetPreferredDataTypes(registered_types, registered_types); | |
92 | |
93 // Manually enable typed urls (to simulate the old world). | |
94 pref_service_.SetBoolean(prefs::kSyncTypedUrls, true); | |
95 | |
96 // Proxy tabs should not be enabled (since sessions wasn't), but history | |
97 // delete directives should (since typed urls was). | |
98 preferred_types = sync_prefs.GetPreferredDataTypes(syncer::UserTypes()); | |
99 EXPECT_FALSE(preferred_types.Has(syncer::PROXY_TABS)); | |
100 EXPECT_TRUE(preferred_types.Has(syncer::HISTORY_DELETE_DIRECTIVES)); | |
101 | |
102 // Now manually enable sessions, which should result in proxy tabs also being | |
103 // enabled. Also, manually disable typed urls, which should mean that history | |
104 // delete directives are not enabled. | |
105 pref_service_.SetBoolean(prefs::kSyncTypedUrls, false); | |
106 pref_service_.SetBoolean(prefs::kSyncSessions, true); | |
107 preferred_types = sync_prefs.GetPreferredDataTypes(syncer::UserTypes()); | |
108 EXPECT_TRUE(preferred_types.Has(syncer::PROXY_TABS)); | |
109 EXPECT_FALSE(preferred_types.Has(syncer::HISTORY_DELETE_DIRECTIVES)); | |
110 } | |
111 | |
112 TEST_F(SyncPrefsTest, PreferredTypesKeepEverythingSynced) { | |
113 SyncPrefs sync_prefs(&pref_service_); | |
114 | |
115 EXPECT_TRUE(sync_prefs.HasKeepEverythingSynced()); | |
116 | |
117 const syncer::ModelTypeSet user_types = syncer::UserTypes(); | |
118 EXPECT_EQ(user_types, sync_prefs.GetPreferredDataTypes(user_types)); | |
119 const syncer::ModelTypeSet user_visible_types = syncer::UserSelectableTypes(); | |
120 for (syncer::ModelTypeSet::Iterator it = user_visible_types.First(); | |
121 it.Good(); | |
122 it.Inc()) { | |
123 syncer::ModelTypeSet preferred_types; | |
124 preferred_types.Put(it.Get()); | |
125 sync_prefs.SetPreferredDataTypes(user_types, preferred_types); | |
126 EXPECT_EQ(user_types, sync_prefs.GetPreferredDataTypes(user_types)); | |
127 } | |
128 } | |
129 | |
130 TEST_F(SyncPrefsTest, PreferredTypesNotKeepEverythingSynced) { | |
131 SyncPrefs sync_prefs(&pref_service_); | |
132 | |
133 sync_prefs.SetKeepEverythingSynced(false); | |
134 | |
135 const syncer::ModelTypeSet user_types = syncer::UserTypes(); | |
136 EXPECT_NE(user_types, sync_prefs.GetPreferredDataTypes(user_types)); | |
137 const syncer::ModelTypeSet user_visible_types = syncer::UserSelectableTypes(); | |
138 for (syncer::ModelTypeSet::Iterator it = user_visible_types.First(); | |
139 it.Good(); | |
140 it.Inc()) { | |
141 syncer::ModelTypeSet preferred_types; | |
142 preferred_types.Put(it.Get()); | |
143 syncer::ModelTypeSet expected_preferred_types(preferred_types); | |
144 if (it.Get() == syncer::AUTOFILL) { | |
145 expected_preferred_types.Put(syncer::AUTOFILL_PROFILE); | |
146 expected_preferred_types.Put(syncer::AUTOFILL_WALLET_DATA); | |
147 expected_preferred_types.Put(syncer::AUTOFILL_WALLET_METADATA); | |
148 } | |
149 if (it.Get() == syncer::PREFERENCES) { | |
150 expected_preferred_types.Put(syncer::DICTIONARY); | |
151 expected_preferred_types.Put(syncer::PRIORITY_PREFERENCES); | |
152 expected_preferred_types.Put(syncer::SEARCH_ENGINES); | |
153 } | |
154 if (it.Get() == syncer::APPS) { | |
155 expected_preferred_types.Put(syncer::APP_LIST); | |
156 expected_preferred_types.Put(syncer::APP_NOTIFICATIONS); | |
157 expected_preferred_types.Put(syncer::APP_SETTINGS); | |
158 } | |
159 if (it.Get() == syncer::EXTENSIONS) { | |
160 expected_preferred_types.Put(syncer::EXTENSION_SETTINGS); | |
161 } | |
162 if (it.Get() == syncer::TYPED_URLS) { | |
163 expected_preferred_types.Put(syncer::HISTORY_DELETE_DIRECTIVES); | |
164 expected_preferred_types.Put(syncer::SESSIONS); | |
165 expected_preferred_types.Put(syncer::FAVICON_IMAGES); | |
166 expected_preferred_types.Put(syncer::FAVICON_TRACKING); | |
167 } | |
168 if (it.Get() == syncer::PROXY_TABS) { | |
169 expected_preferred_types.Put(syncer::SESSIONS); | |
170 expected_preferred_types.Put(syncer::FAVICON_IMAGES); | |
171 expected_preferred_types.Put(syncer::FAVICON_TRACKING); | |
172 } | |
173 | |
174 // Device info is always preferred. | |
175 expected_preferred_types.Put(syncer::DEVICE_INFO); | |
176 | |
177 sync_prefs.SetPreferredDataTypes(user_types, preferred_types); | |
178 EXPECT_EQ(expected_preferred_types, | |
179 sync_prefs.GetPreferredDataTypes(user_types)); | |
180 } | |
181 } | |
182 | |
183 class MockSyncPrefObserver : public SyncPrefObserver { | |
184 public: | |
185 MOCK_METHOD1(OnSyncManagedPrefChange, void(bool)); | |
186 }; | |
187 | |
188 TEST_F(SyncPrefsTest, ObservedPrefs) { | |
189 SyncPrefs sync_prefs(&pref_service_); | |
190 | |
191 StrictMock<MockSyncPrefObserver> mock_sync_pref_observer; | |
192 InSequence dummy; | |
193 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(true)); | |
194 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(false)); | |
195 | |
196 EXPECT_FALSE(sync_prefs.IsManaged()); | |
197 | |
198 sync_prefs.AddSyncPrefObserver(&mock_sync_pref_observer); | |
199 | |
200 sync_prefs.SetManagedForTest(true); | |
201 EXPECT_TRUE(sync_prefs.IsManaged()); | |
202 sync_prefs.SetManagedForTest(false); | |
203 EXPECT_FALSE(sync_prefs.IsManaged()); | |
204 | |
205 sync_prefs.RemoveSyncPrefObserver(&mock_sync_pref_observer); | |
206 } | |
207 | |
208 TEST_F(SyncPrefsTest, ClearPreferences) { | |
209 SyncPrefs sync_prefs(&pref_service_); | |
210 | |
211 EXPECT_FALSE(sync_prefs.IsFirstSetupComplete()); | |
212 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime()); | |
213 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty()); | |
214 | |
215 sync_prefs.SetFirstSetupComplete(); | |
216 sync_prefs.SetLastSyncedTime(base::Time::Now()); | |
217 sync_prefs.SetEncryptionBootstrapToken("token"); | |
218 | |
219 EXPECT_TRUE(sync_prefs.IsFirstSetupComplete()); | |
220 EXPECT_NE(base::Time(), sync_prefs.GetLastSyncedTime()); | |
221 EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken()); | |
222 | |
223 sync_prefs.ClearPreferences(); | |
224 | |
225 EXPECT_FALSE(sync_prefs.IsFirstSetupComplete()); | |
226 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime()); | |
227 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty()); | |
228 } | |
229 | |
230 // Device info should always be enabled. | |
231 TEST_F(SyncPrefsTest, DeviceInfo) { | |
232 SyncPrefs sync_prefs(&pref_service_); | |
233 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes()) | |
234 .Has(syncer::DEVICE_INFO)); | |
235 sync_prefs.SetKeepEverythingSynced(true); | |
236 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes()) | |
237 .Has(syncer::DEVICE_INFO)); | |
238 sync_prefs.SetKeepEverythingSynced(false); | |
239 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes()) | |
240 .Has(syncer::DEVICE_INFO)); | |
241 } | |
242 | |
243 // Verify that invalidation versions are persisted and loaded correctly. | |
244 TEST_F(SyncPrefsTest, InvalidationVersions) { | |
245 std::map<syncer::ModelType, int64_t> versions; | |
246 versions[syncer::BOOKMARKS] = 10; | |
247 versions[syncer::SESSIONS] = 20; | |
248 versions[syncer::PREFERENCES] = 30; | |
249 | |
250 SyncPrefs sync_prefs(&pref_service_); | |
251 sync_prefs.UpdateInvalidationVersions(versions); | |
252 | |
253 std::map<syncer::ModelType, int64_t> versions2; | |
254 sync_prefs.GetInvalidationVersions(&versions2); | |
255 | |
256 EXPECT_EQ(versions.size(), versions2.size()); | |
257 for (auto map_iter : versions2) { | |
258 EXPECT_EQ(versions[map_iter.first], map_iter.second); | |
259 } | |
260 } | |
261 | |
262 } // namespace | |
263 | |
264 } // namespace sync_driver | |
OLD | NEW |