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 virtual 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 are enabled by default.
72 syncer::ModelTypeSet preferred_types
=
73 sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
74 EXPECT_TRUE(preferred_types
.Equals(syncer::ModelTypeSet(syncer::BOOKMARKS
)));
76 // Simulate an upgrade to delete directives + proxy tabs support. None of the
77 // new types or their pref group types should be registering, ensuring they
78 // don't have pref values.
79 syncer::ModelTypeSet registered_types
= syncer::UserTypes();
80 registered_types
.Remove(syncer::PROXY_TABS
);
81 registered_types
.Remove(syncer::TYPED_URLS
);
82 registered_types
.Remove(syncer::SESSIONS
);
83 registered_types
.Remove(syncer::HISTORY_DELETE_DIRECTIVES
);
85 // Enable all other types.
86 sync_prefs
.SetPreferredDataTypes(registered_types
, registered_types
);
88 // Manually enable typed urls (to simulate the old world).
89 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, true);
91 // Proxy tabs should not be enabled (since sessions wasn't), but history
92 // delete directives should (since typed urls was).
93 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
94 EXPECT_FALSE(preferred_types
.Has(syncer::PROXY_TABS
));
95 EXPECT_TRUE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
97 // Now manually enable sessions, which should result in proxy tabs also being
98 // enabled. Also, manually disable typed urls, which should mean that history
99 // delete directives are not enabled.
100 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, false);
101 pref_service_
.SetBoolean(prefs::kSyncSessions
, true);
102 preferred_types
= sync_prefs
.GetPreferredDataTypes(syncer::UserTypes());
103 EXPECT_TRUE(preferred_types
.Has(syncer::PROXY_TABS
));
104 EXPECT_FALSE(preferred_types
.Has(syncer::HISTORY_DELETE_DIRECTIVES
));
107 TEST_F(SyncPrefsTest
, PreferredTypesKeepEverythingSynced
) {
108 SyncPrefs
sync_prefs(&pref_service_
);
110 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
112 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
113 EXPECT_TRUE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
114 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
115 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
118 syncer::ModelTypeSet preferred_types
;
119 preferred_types
.Put(it
.Get());
120 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
122 user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
126 TEST_F(SyncPrefsTest
, PreferredTypesNotKeepEverythingSynced
) {
127 SyncPrefs
sync_prefs(&pref_service_
);
129 sync_prefs
.SetKeepEverythingSynced(false);
131 const syncer::ModelTypeSet user_types
= syncer::UserTypes();
132 EXPECT_FALSE(user_types
.Equals(sync_prefs
.GetPreferredDataTypes(user_types
)));
133 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
134 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
137 syncer::ModelTypeSet preferred_types
;
138 preferred_types
.Put(it
.Get());
139 syncer::ModelTypeSet
expected_preferred_types(preferred_types
);
140 if (it
.Get() == syncer::AUTOFILL
) {
141 expected_preferred_types
.Put(syncer::AUTOFILL_PROFILE
);
143 if (it
.Get() == syncer::PREFERENCES
) {
144 expected_preferred_types
.Put(syncer::DICTIONARY
);
145 expected_preferred_types
.Put(syncer::PRIORITY_PREFERENCES
);
146 expected_preferred_types
.Put(syncer::SEARCH_ENGINES
);
148 if (it
.Get() == syncer::APPS
) {
149 expected_preferred_types
.Put(syncer::APP_LIST
);
150 expected_preferred_types
.Put(syncer::APP_NOTIFICATIONS
);
151 expected_preferred_types
.Put(syncer::APP_SETTINGS
);
153 if (it
.Get() == syncer::EXTENSIONS
) {
154 expected_preferred_types
.Put(syncer::EXTENSION_SETTINGS
);
156 if (it
.Get() == syncer::TYPED_URLS
) {
157 expected_preferred_types
.Put(syncer::HISTORY_DELETE_DIRECTIVES
);
158 expected_preferred_types
.Put(syncer::SESSIONS
);
159 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
160 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
162 if (it
.Get() == syncer::PROXY_TABS
) {
163 expected_preferred_types
.Put(syncer::SESSIONS
);
164 expected_preferred_types
.Put(syncer::FAVICON_IMAGES
);
165 expected_preferred_types
.Put(syncer::FAVICON_TRACKING
);
167 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
168 EXPECT_TRUE(expected_preferred_types
.Equals(
169 sync_prefs
.GetPreferredDataTypes(user_types
)));
173 class MockSyncPrefObserver
: public SyncPrefObserver
{
175 MOCK_METHOD1(OnSyncManagedPrefChange
, void(bool));
178 TEST_F(SyncPrefsTest
, ObservedPrefs
) {
179 SyncPrefs
sync_prefs(&pref_service_
);
181 StrictMock
<MockSyncPrefObserver
> mock_sync_pref_observer
;
183 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(true));
184 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(false));
186 EXPECT_FALSE(sync_prefs
.IsManaged());
188 sync_prefs
.AddSyncPrefObserver(&mock_sync_pref_observer
);
190 sync_prefs
.SetManagedForTest(true);
191 EXPECT_TRUE(sync_prefs
.IsManaged());
192 sync_prefs
.SetManagedForTest(false);
193 EXPECT_FALSE(sync_prefs
.IsManaged());
195 sync_prefs
.RemoveSyncPrefObserver(&mock_sync_pref_observer
);
198 TEST_F(SyncPrefsTest
, AcknowledgeSyncedTypes
) {
199 SyncPrefs
sync_prefs(&pref_service_
);
201 syncer::ModelTypeSet expected_acknowledge_synced_types
=
202 sync_prefs
.GetAcknowledgeSyncedTypesForTest();
203 for (int i
= syncer::EXTENSION_SETTINGS
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
204 const syncer::ModelType type
= syncer::ModelTypeFromInt(i
);
205 syncer::ModelTypeSet
acknowledge_synced_types(type
);
206 expected_acknowledge_synced_types
.Put(type
);
207 sync_prefs
.AcknowledgeSyncedTypes(acknowledge_synced_types
);
208 EXPECT_TRUE(expected_acknowledge_synced_types
.Equals(
209 sync_prefs
.GetAcknowledgeSyncedTypesForTest()));
213 TEST_F(SyncPrefsTest
, ClearPreferences
) {
214 SyncPrefs
sync_prefs(&pref_service_
);
216 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
217 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
218 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
220 sync_prefs
.SetSyncSetupCompleted();
221 sync_prefs
.SetLastSyncedTime(base::Time::Now());
222 sync_prefs
.SetEncryptionBootstrapToken("token");
224 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
225 EXPECT_NE(base::Time(), sync_prefs
.GetLastSyncedTime());
226 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
228 sync_prefs
.ClearPreferences();
230 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
231 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
232 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
237 } // namespace sync_driver