Roll src/third_party/WebKit a4fcdf5:1a119ff (svn 197351:197359)
[chromium-blink-merge.git] / components / sync_driver / sync_prefs_unittest.cc
blobd87b0117ed0e6559f85f10c6cd2f6a62de601981
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 {
21 namespace {
23 using ::testing::InSequence;
24 using ::testing::StrictMock;
26 class SyncPrefsTest : public testing::Test {
27 protected:
28 void SetUp() override {
29 SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
32 user_prefs::TestingPrefServiceSyncable pref_service_;
34 private:
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_TRUE(sync_prefs.IsSyncRequested());
46 sync_prefs.SetSyncRequested(false);
47 EXPECT_FALSE(sync_prefs.IsSyncRequested());
48 sync_prefs.SetSyncRequested(true);
49 EXPECT_TRUE(sync_prefs.IsSyncRequested());
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();
117 it.Good();
118 it.Inc()) {
119 syncer::ModelTypeSet preferred_types;
120 preferred_types.Put(it.Get());
121 sync_prefs.SetPreferredDataTypes(user_types, preferred_types);
122 EXPECT_TRUE(
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();
136 it.Good();
137 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);
143 expected_preferred_types.Put(syncer::AUTOFILL_WALLET_DATA);
144 expected_preferred_types.Put(syncer::AUTOFILL_WALLET_METADATA);
146 if (it.Get() == syncer::PREFERENCES) {
147 expected_preferred_types.Put(syncer::DICTIONARY);
148 expected_preferred_types.Put(syncer::PRIORITY_PREFERENCES);
149 expected_preferred_types.Put(syncer::SEARCH_ENGINES);
151 if (it.Get() == syncer::APPS) {
152 expected_preferred_types.Put(syncer::APP_LIST);
153 expected_preferred_types.Put(syncer::APP_NOTIFICATIONS);
154 expected_preferred_types.Put(syncer::APP_SETTINGS);
156 if (it.Get() == syncer::EXTENSIONS) {
157 expected_preferred_types.Put(syncer::EXTENSION_SETTINGS);
159 if (it.Get() == syncer::TYPED_URLS) {
160 expected_preferred_types.Put(syncer::HISTORY_DELETE_DIRECTIVES);
161 expected_preferred_types.Put(syncer::SESSIONS);
162 expected_preferred_types.Put(syncer::FAVICON_IMAGES);
163 expected_preferred_types.Put(syncer::FAVICON_TRACKING);
165 if (it.Get() == syncer::PROXY_TABS) {
166 expected_preferred_types.Put(syncer::SESSIONS);
167 expected_preferred_types.Put(syncer::FAVICON_IMAGES);
168 expected_preferred_types.Put(syncer::FAVICON_TRACKING);
171 // Device info is always preferred.
172 expected_preferred_types.Put(syncer::DEVICE_INFO);
174 sync_prefs.SetPreferredDataTypes(user_types, preferred_types);
175 EXPECT_TRUE(expected_preferred_types.Equals(
176 sync_prefs.GetPreferredDataTypes(user_types)));
180 class MockSyncPrefObserver : public SyncPrefObserver {
181 public:
182 MOCK_METHOD1(OnSyncManagedPrefChange, void(bool));
185 TEST_F(SyncPrefsTest, ObservedPrefs) {
186 SyncPrefs sync_prefs(&pref_service_);
188 StrictMock<MockSyncPrefObserver> mock_sync_pref_observer;
189 InSequence dummy;
190 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(true));
191 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(false));
193 EXPECT_FALSE(sync_prefs.IsManaged());
195 sync_prefs.AddSyncPrefObserver(&mock_sync_pref_observer);
197 sync_prefs.SetManagedForTest(true);
198 EXPECT_TRUE(sync_prefs.IsManaged());
199 sync_prefs.SetManagedForTest(false);
200 EXPECT_FALSE(sync_prefs.IsManaged());
202 sync_prefs.RemoveSyncPrefObserver(&mock_sync_pref_observer);
205 TEST_F(SyncPrefsTest, ClearPreferences) {
206 SyncPrefs sync_prefs(&pref_service_);
208 EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
209 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
210 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
212 sync_prefs.SetSyncSetupCompleted();
213 sync_prefs.SetLastSyncedTime(base::Time::Now());
214 sync_prefs.SetEncryptionBootstrapToken("token");
216 EXPECT_TRUE(sync_prefs.HasSyncSetupCompleted());
217 EXPECT_NE(base::Time(), sync_prefs.GetLastSyncedTime());
218 EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken());
220 sync_prefs.ClearPreferences();
222 EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
223 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
224 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
227 // Device info should always be enabled.
228 TEST_F(SyncPrefsTest, DeviceInfo) {
229 SyncPrefs sync_prefs(&pref_service_);
230 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
231 .Has(syncer::DEVICE_INFO));
232 sync_prefs.SetKeepEverythingSynced(true);
233 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
234 .Has(syncer::DEVICE_INFO));
235 sync_prefs.SetKeepEverythingSynced(false);
236 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
237 .Has(syncer::DEVICE_INFO));
240 } // namespace
242 } // namespace sync_driver