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.
5 #include "components/sync_driver/sync_prefs.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_notifier_impl.h"
10 #include "base/prefs/pref_value_store.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/time/time.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "components/sync_driver/pref_names.h"
15 #include "sync/internal_api/public/base/model_type.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 TestingPrefServiceBase
<PrefService
, user_prefs::PrefRegistrySyncable
>::
21 TestingPrefServiceBase(TestingPrefStore
* managed_prefs
,
22 TestingPrefStore
* user_prefs
,
23 TestingPrefStore
* recommended_prefs
,
24 user_prefs::PrefRegistrySyncable
* pref_registry
,
25 PrefNotifierImpl
* pref_notifier
)
28 new PrefValueStore(managed_prefs
,
29 NULL
, // supervised_user_prefs
30 NULL
, // extension_prefs
31 NULL
, // command_line_prefs
34 pref_registry
->defaults().get(),
38 base::Bind(&TestingPrefServiceBase
<
40 user_prefs::PrefRegistrySyncable
>::HandleReadError
),
42 managed_prefs_(managed_prefs
),
43 user_prefs_(user_prefs
),
44 recommended_prefs_(recommended_prefs
) {}
46 namespace sync_driver
{
50 using ::testing::InSequence
;
51 using ::testing::StrictMock
;
53 // Test version of PrefServiceSyncable.
54 class TestingPrefServiceSyncable
55 : public TestingPrefServiceBase
<PrefService
,
56 user_prefs::PrefRegistrySyncable
> {
58 TestingPrefServiceSyncable();
59 virtual ~TestingPrefServiceSyncable();
61 user_prefs::PrefRegistrySyncable
* registry();
64 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSyncable
);
67 TestingPrefServiceSyncable::TestingPrefServiceSyncable()
68 : TestingPrefServiceBase
<PrefService
, user_prefs::PrefRegistrySyncable
>(
69 new TestingPrefStore(),
70 new TestingPrefStore(),
71 new TestingPrefStore(),
72 new user_prefs::PrefRegistrySyncable(),
73 new PrefNotifierImpl()) {}
75 TestingPrefServiceSyncable::~TestingPrefServiceSyncable() {}
77 user_prefs::PrefRegistrySyncable
* TestingPrefServiceSyncable::registry() {
78 return static_cast<user_prefs::PrefRegistrySyncable
*>(
79 DeprecatedGetPrefRegistry());
82 class SyncPrefsTest
: public testing::Test
{
84 virtual void SetUp() OVERRIDE
{
85 SyncPrefs::RegisterProfilePrefs(pref_service_
.registry());
88 TestingPrefServiceSyncable pref_service_
;
91 base::MessageLoop loop_
;
94 TEST_F(SyncPrefsTest
, Basic
) {
95 SyncPrefs
sync_prefs(&pref_service_
);
97 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
98 sync_prefs
.SetSyncSetupCompleted();
99 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
101 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
102 sync_prefs
.SetStartSuppressed(true);
103 EXPECT_TRUE(sync_prefs
.IsStartSuppressed());
104 sync_prefs
.SetStartSuppressed(false);
105 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
107 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
108 const base::Time
& now
= base::Time::Now();
109 sync_prefs
.SetLastSyncedTime(now
);
110 EXPECT_EQ(now
, sync_prefs
.GetLastSyncedTime());
112 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
113 sync_prefs
.SetKeepEverythingSynced(false);
114 EXPECT_FALSE(sync_prefs
.HasKeepEverythingSynced());
115 sync_prefs
.SetKeepEverythingSynced(true);
116 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
118 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
119 sync_prefs
.SetEncryptionBootstrapToken("token");
120 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
123 TEST_F(SyncPrefsTest
, DefaultTypes
) {
124 SyncPrefs
sync_prefs(&pref_service_
);
125 sync_prefs
.SetKeepEverythingSynced(false);
127 // Only bookmarks are enabled by default.
128 syncer::ModelTypeSet preferred_types
=
129 sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
130 EXPECT_TRUE(preferred_types
.Equals(syncer::ModelTypeSet(syncer::BOOKMARKS
)));
132 // Simulate an upgrade to delete directives + proxy tabs support. None of the
133 // new types or their pref group types should be registering, ensuring they
134 // don't have pref values.
135 syncer::ModelTypeSet registered_types
= syncer::UserTypes();
136 registered_types
.Remove(syncer::PROXY_TABS
);
137 registered_types
.Remove(syncer::TYPED_URLS
);
138 registered_types
.Remove(syncer::SESSIONS
);
139 registered_types
.Remove(syncer::HISTORY_DELETE_DIRECTIVES
);
141 // Enable all other types.
142 sync_prefs
.SetPreferredDataTypes(registered_types
, registered_types
);
144 // Manually enable typed urls (to simulate the old world).
145 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, true);
147 // Proxy tabs should not be enabled (since sessions wasn't), but history
148 // delete directives should (since typed urls was).
149 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
150 EXPECT_FALSE(preferred_types
.Has(syncer::PROXY_TABS
));
151 EXPECT_TRUE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
153 // Now manually enable sessions, which should result in proxy tabs also being
154 // enabled. Also, manually disable typed urls, which should mean that history
155 // delete directives are not enabled.
156 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, false);
157 pref_service_
.SetBoolean(prefs::kSyncSessions
, true);
158 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
159 EXPECT_TRUE(preferred_types
.Has(syncer::PROXY_TABS
));
160 EXPECT_FALSE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
163 TEST_F(SyncPrefsTest
, PreferredTypesKeepEverythingSynced
) {
164 SyncPrefs
sync_prefs(&pref_service_
);
166 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
168 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
169 EXPECT_TRUE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
170 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
171 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
174 syncer::ModelTypeSet preferred_types
;
175 preferred_types
.Put(it
.Get());
176 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
178 user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
182 TEST_F(SyncPrefsTest
, PreferredTypesNotKeepEverythingSynced
) {
183 SyncPrefs
sync_prefs(&pref_service_
);
185 sync_prefs
.SetKeepEverythingSynced(false);
187 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
188 EXPECT_FALSE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
189 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
190 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
193 syncer::ModelTypeSet preferred_types
;
194 preferred_types
.Put(it
.Get());
195 syncer::ModelTypeSet
expected_preferred_types(preferred_types
);
196 if (it
.Get() == syncer::AUTOFILL
) {
197 expected_preferred_types
.Put(syncer::AUTOFILL_PROFILE
);
199 if (it
.Get() == syncer::PREFERENCES
) {
200 expected_preferred_types
.Put(syncer::DICTIONARY
);
201 expected_preferred_types
.Put(syncer::PRIORITY_PREFERENCES
);
202 expected_preferred_types
.Put(syncer::SEARCH_ENGINES
);
204 if (it
.Get() == syncer::APPS
) {
205 expected_preferred_types
.Put(syncer::APP_LIST
);
206 expected_preferred_types
.Put(syncer::APP_NOTIFICATIONS
);
207 expected_preferred_types
.Put(syncer::APP_SETTINGS
);
209 if (it
.Get() == syncer::EXTENSIONS
) {
210 expected_preferred_types
.Put(syncer::EXTENSION_SETTINGS
);
212 if (it
.Get() == syncer::TYPED_URLS
) {
213 expected_preferred_types
.Put(syncer::HISTORY_DELETE_DIRECTIVES
);
214 expected_preferred_types
.Put(syncer::SESSIONS
);
215 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
216 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
218 if (it
.Get() == syncer::PROXY_TABS
) {
219 expected_preferred_types
.Put(syncer::SESSIONS
);
220 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
221 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
223 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
224 EXPECT_TRUE(expected_preferred_types
.Equals(
225 sync_prefs
.GetPreferredDataTypes(user_types
)));
229 class MockSyncPrefObserver
: public SyncPrefObserver
{
231 MOCK_METHOD1(OnSyncManagedPrefChange
, void(bool));
234 TEST_F(SyncPrefsTest
, ObservedPrefs
) {
235 SyncPrefs
sync_prefs(&pref_service_
);
237 StrictMock
<MockSyncPrefObserver
> mock_sync_pref_observer
;
239 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(true));
240 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(false));
242 EXPECT_FALSE(sync_prefs
.IsManaged());
244 sync_prefs
.AddSyncPrefObserver(&mock_sync_pref_observer
);
246 sync_prefs
.SetManagedForTest(true);
247 EXPECT_TRUE(sync_prefs
.IsManaged());
248 sync_prefs
.SetManagedForTest(false);
249 EXPECT_FALSE(sync_prefs
.IsManaged());
251 sync_prefs
.RemoveSyncPrefObserver(&mock_sync_pref_observer
);
254 TEST_F(SyncPrefsTest
, AcknowledgeSyncedTypes
) {
255 SyncPrefs
sync_prefs(&pref_service_
);
257 syncer::ModelTypeSet expected_acknowledge_synced_types
=
258 sync_prefs
.GetAcknowledgeSyncedTypesForTest();
259 for (int i
= syncer::EXTENSION_SETTINGS
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
260 const syncer::ModelType type
= syncer::ModelTypeFromInt(i
);
261 syncer::ModelTypeSet
acknowledge_synced_types(type
);
262 expected_acknowledge_synced_types
.Put(type
);
263 sync_prefs
.AcknowledgeSyncedTypes(acknowledge_synced_types
);
264 EXPECT_TRUE(expected_acknowledge_synced_types
.Equals(
265 sync_prefs
.GetAcknowledgeSyncedTypesForTest()));
269 TEST_F(SyncPrefsTest
, ClearPreferences
) {
270 SyncPrefs
sync_prefs(&pref_service_
);
272 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
273 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
274 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
276 sync_prefs
.SetSyncSetupCompleted();
277 sync_prefs
.SetLastSyncedTime(base::Time::Now());
278 sync_prefs
.SetEncryptionBootstrapToken("token");
280 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
281 EXPECT_NE(base::Time(), sync_prefs
.GetLastSyncedTime());
282 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
284 sync_prefs
.ClearPreferences();
286 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
287 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
288 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
293 } // namespace browser_sync