Reland [Sync] Refactoring polling to be reliable.
[chromium-blink-merge.git] / components / sync_driver / sync_prefs_unittest.cc
blobbcfbc7b32b7ad0980f267c9cf21f8caafaff1526
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_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();
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);
145 if (it.Get() == syncer::PREFERENCES) {
146 expected_preferred_types.Put(syncer::DICTIONARY);
147 expected_preferred_types.Put(syncer::PRIORITY_PREFERENCES);
148 expected_preferred_types.Put(syncer::SEARCH_ENGINES);
150 if (it.Get() == syncer::APPS) {
151 expected_preferred_types.Put(syncer::APP_LIST);
152 expected_preferred_types.Put(syncer::APP_NOTIFICATIONS);
153 expected_preferred_types.Put(syncer::APP_SETTINGS);
155 if (it.Get() == syncer::EXTENSIONS) {
156 expected_preferred_types.Put(syncer::EXTENSION_SETTINGS);
158 if (it.Get() == syncer::TYPED_URLS) {
159 expected_preferred_types.Put(syncer::HISTORY_DELETE_DIRECTIVES);
160 expected_preferred_types.Put(syncer::SESSIONS);
161 expected_preferred_types.Put(syncer::FAVICON_IMAGES);
162 expected_preferred_types.Put(syncer::FAVICON_TRACKING);
164 if (it.Get() == syncer::PROXY_TABS) {
165 expected_preferred_types.Put(syncer::SESSIONS);
166 expected_preferred_types.Put(syncer::FAVICON_IMAGES);
167 expected_preferred_types.Put(syncer::FAVICON_TRACKING);
170 // Device info is always preferred.
171 expected_preferred_types.Put(syncer::DEVICE_INFO);
173 sync_prefs.SetPreferredDataTypes(user_types, preferred_types);
174 EXPECT_TRUE(expected_preferred_types.Equals(
175 sync_prefs.GetPreferredDataTypes(user_types)));
179 class MockSyncPrefObserver : public SyncPrefObserver {
180 public:
181 MOCK_METHOD1(OnSyncManagedPrefChange, void(bool));
184 TEST_F(SyncPrefsTest, ObservedPrefs) {
185 SyncPrefs sync_prefs(&pref_service_);
187 StrictMock<MockSyncPrefObserver> mock_sync_pref_observer;
188 InSequence dummy;
189 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(true));
190 EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(false));
192 EXPECT_FALSE(sync_prefs.IsManaged());
194 sync_prefs.AddSyncPrefObserver(&mock_sync_pref_observer);
196 sync_prefs.SetManagedForTest(true);
197 EXPECT_TRUE(sync_prefs.IsManaged());
198 sync_prefs.SetManagedForTest(false);
199 EXPECT_FALSE(sync_prefs.IsManaged());
201 sync_prefs.RemoveSyncPrefObserver(&mock_sync_pref_observer);
204 TEST_F(SyncPrefsTest, ClearPreferences) {
205 SyncPrefs sync_prefs(&pref_service_);
207 EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
208 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
209 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
211 sync_prefs.SetSyncSetupCompleted();
212 sync_prefs.SetLastSyncedTime(base::Time::Now());
213 sync_prefs.SetEncryptionBootstrapToken("token");
215 EXPECT_TRUE(sync_prefs.HasSyncSetupCompleted());
216 EXPECT_NE(base::Time(), sync_prefs.GetLastSyncedTime());
217 EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken());
219 sync_prefs.ClearPreferences();
221 EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
222 EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
223 EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
226 // Device info should always be enabled.
227 TEST_F(SyncPrefsTest, DeviceInfo) {
228 SyncPrefs sync_prefs(&pref_service_);
229 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
230 .Has(syncer::DEVICE_INFO));
231 sync_prefs.SetKeepEverythingSynced(true);
232 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
233 .Has(syncer::DEVICE_INFO));
234 sync_prefs.SetKeepEverythingSynced(false);
235 EXPECT_TRUE(sync_prefs.GetPreferredDataTypes(syncer::UserTypes())
236 .Has(syncer::DEVICE_INFO));
239 } // namespace
241 } // namespace sync_driver