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/testing_pref_service_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"
19 namespace sync_driver
{
23 using ::testing::InSequence
;
24 using ::testing::StrictMock
;
26 class SyncPrefsTest
: public testing::Test
{
28 void SetUp() override
{
29 SyncPrefs::RegisterProfilePrefs(pref_service_
.registry());
32 user_prefs::TestingPrefServiceSyncable pref_service_
;
35 base::MessageLoop loop_
;
38 TEST_F(SyncPrefsTest
, Basic
) {
39 SyncPrefs
sync_prefs(&pref_service_
);
41 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
42 sync_prefs
.SetSyncSetupCompleted();
43 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
45 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
46 sync_prefs
.SetStartSuppressed(true);
47 EXPECT_TRUE(sync_prefs
.IsStartSuppressed());
48 sync_prefs
.SetStartSuppressed(false);
49 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
51 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
52 const base::Time
& now
= base::Time::Now();
53 sync_prefs
.SetLastSyncedTime(now
);
54 EXPECT_EQ(now
, sync_prefs
.GetLastSyncedTime());
56 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
57 sync_prefs
.SetKeepEverythingSynced(false);
58 EXPECT_FALSE(sync_prefs
.HasKeepEverythingSynced());
59 sync_prefs
.SetKeepEverythingSynced(true);
60 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
62 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
63 sync_prefs
.SetEncryptionBootstrapToken("token");
64 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
67 TEST_F(SyncPrefsTest
, DefaultTypes
) {
68 SyncPrefs
sync_prefs(&pref_service_
);
69 sync_prefs
.SetKeepEverythingSynced(false);
71 // Only bookmarks and device info are enabled by default.
72 syncer::ModelTypeSet
expected(syncer::BOOKMARKS
, syncer::DEVICE_INFO
);
73 syncer::ModelTypeSet preferred_types
=
74 sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
75 EXPECT_TRUE(preferred_types
.Equals(expected
));
77 // Simulate an upgrade to delete directives + proxy tabs support. None of the
78 // new types or their pref group types should be registering, ensuring they
79 // don't have pref values.
80 syncer::ModelTypeSet registered_types
= syncer::UserTypes();
81 registered_types
.Remove(syncer::PROXY_TABS
);
82 registered_types
.Remove(syncer::TYPED_URLS
);
83 registered_types
.Remove(syncer::SESSIONS
);
84 registered_types
.Remove(syncer::HISTORY_DELETE_DIRECTIVES
);
86 // Enable all other types.
87 sync_prefs
.SetPreferredDataTypes(registered_types
, registered_types
);
89 // Manually enable typed urls (to simulate the old world).
90 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, true);
92 // Proxy tabs should not be enabled (since sessions wasn't), but history
93 // delete directives should (since typed urls was).
94 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
95 EXPECT_FALSE(preferred_types
.Has(syncer::PROXY_TABS
));
96 EXPECT_TRUE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
98 // Now manually enable sessions, which should result in proxy tabs also being
99 // enabled. Also, manually disable typed urls, which should mean that history
100 // delete directives are not enabled.
101 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, false);
102 pref_service_
.SetBoolean(prefs::kSyncSessions
, true);
103 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
104 EXPECT_TRUE(preferred_types
.Has(syncer::PROXY_TABS
));
105 EXPECT_FALSE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
108 TEST_F(SyncPrefsTest
, PreferredTypesKeepEverythingSynced
) {
109 SyncPrefs
sync_prefs(&pref_service_
);
111 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
113 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
114 EXPECT_TRUE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
115 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
116 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
119 syncer::ModelTypeSet preferred_types
;
120 preferred_types
.Put(it
.Get());
121 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
123 user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
127 TEST_F(SyncPrefsTest
, PreferredTypesNotKeepEverythingSynced
) {
128 SyncPrefs
sync_prefs(&pref_service_
);
130 sync_prefs
.SetKeepEverythingSynced(false);
132 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
133 EXPECT_FALSE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
134 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
135 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
138 syncer::ModelTypeSet preferred_types
;
139 preferred_types
.Put(it
.Get());
140 syncer::ModelTypeSet
expected_preferred_types(preferred_types
);
141 if (it
.Get() == syncer::AUTOFILL
) {
142 expected_preferred_types
.Put(syncer::AUTOFILL_PROFILE
);
144 if (it
.Get() == syncer::PREFERENCES
) {
145 expected_preferred_types
.Put(syncer::DICTIONARY
);
146 expected_preferred_types
.Put(syncer::PRIORITY_PREFERENCES
);
147 expected_preferred_types
.Put(syncer::SEARCH_ENGINES
);
149 if (it
.Get() == syncer::APPS
) {
150 expected_preferred_types
.Put(syncer::APP_LIST
);
151 expected_preferred_types
.Put(syncer::APP_NOTIFICATIONS
);
152 expected_preferred_types
.Put(syncer::APP_SETTINGS
);
154 if (it
.Get() == syncer::EXTENSIONS
) {
155 expected_preferred_types
.Put(syncer::EXTENSION_SETTINGS
);
157 if (it
.Get() == syncer::TYPED_URLS
) {
158 expected_preferred_types
.Put(syncer::HISTORY_DELETE_DIRECTIVES
);
159 expected_preferred_types
.Put(syncer::SESSIONS
);
160 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
161 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
163 if (it
.Get() == syncer::PROXY_TABS
) {
164 expected_preferred_types
.Put(syncer::SESSIONS
);
165 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
166 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
169 // Device info is always preferred.
170 expected_preferred_types
.Put(syncer::DEVICE_INFO
);
172 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
173 EXPECT_TRUE(expected_preferred_types
.Equals(
174 sync_prefs
.GetPreferredDataTypes(user_types
)));
178 class MockSyncPrefObserver
: public SyncPrefObserver
{
180 MOCK_METHOD1(OnSyncManagedPrefChange
, void(bool));
183 TEST_F(SyncPrefsTest
, ObservedPrefs
) {
184 SyncPrefs
sync_prefs(&pref_service_
);
186 StrictMock
<MockSyncPrefObserver
> mock_sync_pref_observer
;
188 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(true));
189 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(false));
191 EXPECT_FALSE(sync_prefs
.IsManaged());
193 sync_prefs
.AddSyncPrefObserver(&mock_sync_pref_observer
);
195 sync_prefs
.SetManagedForTest(true);
196 EXPECT_TRUE(sync_prefs
.IsManaged());
197 sync_prefs
.SetManagedForTest(false);
198 EXPECT_FALSE(sync_prefs
.IsManaged());
200 sync_prefs
.RemoveSyncPrefObserver(&mock_sync_pref_observer
);
203 TEST_F(SyncPrefsTest
, AcknowledgeSyncedTypes
) {
204 SyncPrefs
sync_prefs(&pref_service_
);
206 syncer::ModelTypeSet expected_acknowledge_synced_types
=
207 sync_prefs
.GetAcknowledgeSyncedTypesForTest();
208 for (int i
= syncer::EXTENSION_SETTINGS
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
209 const syncer::ModelType type
= syncer::ModelTypeFromInt(i
);
210 syncer::ModelTypeSet
acknowledge_synced_types(type
);
211 expected_acknowledge_synced_types
.Put(type
);
212 sync_prefs
.AcknowledgeSyncedTypes(acknowledge_synced_types
);
213 EXPECT_TRUE(expected_acknowledge_synced_types
.Equals(
214 sync_prefs
.GetAcknowledgeSyncedTypesForTest()));
218 TEST_F(SyncPrefsTest
, ClearPreferences
) {
219 SyncPrefs
sync_prefs(&pref_service_
);
221 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
222 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
223 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
225 sync_prefs
.SetSyncSetupCompleted();
226 sync_prefs
.SetLastSyncedTime(base::Time::Now());
227 sync_prefs
.SetEncryptionBootstrapToken("token");
229 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
230 EXPECT_NE(base::Time(), sync_prefs
.GetLastSyncedTime());
231 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
233 sync_prefs
.ClearPreferences();
235 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
236 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
237 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
240 // Device info should always be enabled.
241 TEST_F(SyncPrefsTest
, DeviceInfo
) {
242 SyncPrefs
sync_prefs(&pref_service_
);
243 EXPECT_TRUE(sync_prefs
.GetPreferredDataTypes(syncer::UserTypes())
244 .Has(syncer::DEVICE_INFO
));
245 sync_prefs
.SetKeepEverythingSynced(true);
246 EXPECT_TRUE(sync_prefs
.GetPreferredDataTypes(syncer::UserTypes())
247 .Has(syncer::DEVICE_INFO
));
248 sync_prefs
.SetKeepEverythingSynced(false);
249 EXPECT_TRUE(sync_prefs
.GetPreferredDataTypes(syncer::UserTypes())
250 .Has(syncer::DEVICE_INFO
));
255 } // namespace sync_driver