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 "chrome/browser/sync/sync_prefs.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_pref_service_syncable.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace browser_sync
{
21 using ::testing::InSequence
;
22 using ::testing::StrictMock
;
24 class SyncPrefsTest
: public testing::Test
{
26 virtual void SetUp() OVERRIDE
{
27 SyncPrefs::RegisterProfilePrefs(pref_service_
.registry());
30 TestingPrefServiceSyncable pref_service_
;
33 base::MessageLoop loop_
;
36 TEST_F(SyncPrefsTest
, Basic
) {
37 SyncPrefs
sync_prefs(&pref_service_
);
39 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
40 sync_prefs
.SetSyncSetupCompleted();
41 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
43 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
44 sync_prefs
.SetStartSuppressed(true);
45 EXPECT_TRUE(sync_prefs
.IsStartSuppressed());
46 sync_prefs
.SetStartSuppressed(false);
47 EXPECT_FALSE(sync_prefs
.IsStartSuppressed());
49 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
50 const base::Time
& now
= base::Time::Now();
51 sync_prefs
.SetLastSyncedTime(now
);
52 EXPECT_EQ(now
, sync_prefs
.GetLastSyncedTime());
54 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
55 sync_prefs
.SetKeepEverythingSynced(false);
56 EXPECT_FALSE(sync_prefs
.HasKeepEverythingSynced());
57 sync_prefs
.SetKeepEverythingSynced(true);
58 EXPECT_TRUE(sync_prefs
.HasKeepEverythingSynced());
60 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
61 sync_prefs
.SetEncryptionBootstrapToken("token");
62 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
65 TEST_F(SyncPrefsTest
, DefaultTypes
) {
66 SyncPrefs
sync_prefs(&pref_service_
);
67 sync_prefs
.SetKeepEverythingSynced(false);
69 // Only bookmarks are enabled by default.
70 syncer::ModelTypeSet preferred_types
= sync_prefs
.GetPreferredDataTypes(
72 EXPECT_TRUE(preferred_types
.Equals(syncer::ModelTypeSet(syncer::BOOKMARKS
)));
74 // Simulate an upgrade to delete directives + proxy tabs support. None of the
75 // new types or their pref group types should be registering, ensuring they
76 // don't have pref values.
77 syncer::ModelTypeSet registered_types
= syncer::UserTypes();
78 registered_types
.Remove(syncer::PROXY_TABS
);
79 registered_types
.Remove(syncer::TYPED_URLS
);
80 registered_types
.Remove(syncer::SESSIONS
);
81 registered_types
.Remove(syncer::HISTORY_DELETE_DIRECTIVES
);
83 // Enable all other types.
84 sync_prefs
.SetPreferredDataTypes(registered_types
,
87 // Manually enable typed urls (to simulate the old world).
88 pref_service_
.SetBoolean(prefs::kSyncTypedUrls
, true);
90 // Proxy tabs should not be enabled (since sessions wasn't), but history
91 // delete directives should (since typed urls was).
93 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);
103 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(
115 sync_prefs
.GetPreferredDataTypes(user_types
)));
116 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
117 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
118 it
.Good(); it
.Inc()) {
119 syncer::ModelTypeSet preferred_types
;
120 preferred_types
.Put(it
.Get());
121 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
122 EXPECT_TRUE(user_types
.Equals(
123 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(
134 sync_prefs
.GetPreferredDataTypes(user_types
)));
135 const syncer::ModelTypeSet user_visible_types
= syncer::UserSelectableTypes();
136 for (syncer::ModelTypeSet::Iterator it
= user_visible_types
.First();
137 it
.Good(); it
.Inc()) {
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
);
168 sync_prefs
.SetPreferredDataTypes(user_types
, preferred_types
);
169 EXPECT_TRUE(expected_preferred_types
.Equals(
170 sync_prefs
.GetPreferredDataTypes(user_types
)));
174 class MockSyncPrefObserver
: public SyncPrefObserver
{
176 MOCK_METHOD1(OnSyncManagedPrefChange
, void(bool));
179 TEST_F(SyncPrefsTest
, ObservedPrefs
) {
180 SyncPrefs
sync_prefs(&pref_service_
);
182 StrictMock
<MockSyncPrefObserver
> mock_sync_pref_observer
;
184 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(true));
185 EXPECT_CALL(mock_sync_pref_observer
, OnSyncManagedPrefChange(false));
187 EXPECT_FALSE(sync_prefs
.IsManaged());
189 sync_prefs
.AddSyncPrefObserver(&mock_sync_pref_observer
);
191 sync_prefs
.SetManagedForTest(true);
192 EXPECT_TRUE(sync_prefs
.IsManaged());
193 sync_prefs
.SetManagedForTest(false);
194 EXPECT_FALSE(sync_prefs
.IsManaged());
196 sync_prefs
.RemoveSyncPrefObserver(&mock_sync_pref_observer
);
199 TEST_F(SyncPrefsTest
, AcknowledgeSyncedTypes
) {
200 SyncPrefs
sync_prefs(&pref_service_
);
202 syncer::ModelTypeSet expected_acknowledge_synced_types
=
203 sync_prefs
.GetAcknowledgeSyncedTypesForTest();
204 for (int i
= syncer::EXTENSION_SETTINGS
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
205 const syncer::ModelType type
= syncer::ModelTypeFromInt(i
);
206 syncer::ModelTypeSet
acknowledge_synced_types(type
);
207 expected_acknowledge_synced_types
.Put(type
);
208 sync_prefs
.AcknowledgeSyncedTypes(acknowledge_synced_types
);
209 EXPECT_TRUE(expected_acknowledge_synced_types
.Equals(
210 sync_prefs
.GetAcknowledgeSyncedTypesForTest()));
214 TEST_F(SyncPrefsTest
, ClearPreferences
) {
215 SyncPrefs
sync_prefs(&pref_service_
);
217 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
218 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
219 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
221 sync_prefs
.SetSyncSetupCompleted();
222 sync_prefs
.SetLastSyncedTime(base::Time::Now());
223 sync_prefs
.SetEncryptionBootstrapToken("token");
225 EXPECT_TRUE(sync_prefs
.HasSyncSetupCompleted());
226 EXPECT_NE(base::Time(), sync_prefs
.GetLastSyncedTime());
227 EXPECT_EQ("token", sync_prefs
.GetEncryptionBootstrapToken());
229 sync_prefs
.ClearPreferences();
231 EXPECT_FALSE(sync_prefs
.HasSyncSetupCompleted());
232 EXPECT_EQ(base::Time(), sync_prefs
.GetLastSyncedTime());
233 EXPECT_TRUE(sync_prefs
.GetEncryptionBootstrapToken().empty());
238 } // namespace browser_sync