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.
4
5#include "chrome/browser/sync/sync_prefs.h"
6
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"
16
17namespace browser_sync {
18
19namespace {
20
21using ::testing::InSequence;
22using ::testing::StrictMock;
23
24class SyncPrefsTest : public testing::Test {
25 protected:
26  virtual void SetUp() OVERRIDE {
27    SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
28  }
29
30  TestingPrefServiceSyncable pref_service_;
31
32 private:
33  base::MessageLoop loop_;
34};
35
36TEST_F(SyncPrefsTest, Basic) {
37  SyncPrefs sync_prefs(&pref_service_);
38
39  EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
40  sync_prefs.SetSyncSetupCompleted();
41  EXPECT_TRUE(sync_prefs.HasSyncSetupCompleted());
42
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());
48
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());
53
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());
59
60  EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
61  sync_prefs.SetEncryptionBootstrapToken("token");
62  EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken());
63}
64
65TEST_F(SyncPrefsTest, DefaultTypes) {
66  SyncPrefs sync_prefs(&pref_service_);
67  sync_prefs.SetKeepEverythingSynced(false);
68
69  // Only bookmarks are enabled by default.
70  syncer::ModelTypeSet preferred_types = sync_prefs.GetPreferredDataTypes(
71      syncer::UserTypes());
72  EXPECT_TRUE(preferred_types.Equals(syncer::ModelTypeSet(syncer::BOOKMARKS)));
73
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);
82
83  // Enable all other types.
84  sync_prefs.SetPreferredDataTypes(registered_types,
85                                   registered_types);
86
87  // Manually enable typed urls (to simulate the old world).
88  pref_service_.SetBoolean(prefs::kSyncTypedUrls, true);
89
90  // Proxy tabs should not be enabled (since sessions wasn't), but history
91  // delete directives should (since typed urls was).
92  preferred_types =
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));
96
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 =
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));
106}
107
108TEST_F(SyncPrefsTest, PreferredTypesKeepEverythingSynced) {
109  SyncPrefs sync_prefs(&pref_service_);
110
111  EXPECT_TRUE(sync_prefs.HasKeepEverythingSynced());
112
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)));
124  }
125}
126
127TEST_F(SyncPrefsTest, PreferredTypesNotKeepEverythingSynced) {
128  SyncPrefs sync_prefs(&pref_service_);
129
130  sync_prefs.SetKeepEverythingSynced(false);
131
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);
143    }
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);
148    }
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);
153    }
154    if (it.Get() == syncer::EXTENSIONS) {
155      expected_preferred_types.Put(syncer::EXTENSION_SETTINGS);
156    }
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);
162    }
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);
167    }
168    sync_prefs.SetPreferredDataTypes(user_types, preferred_types);
169    EXPECT_TRUE(expected_preferred_types.Equals(
170        sync_prefs.GetPreferredDataTypes(user_types)));
171  }
172}
173
174class MockSyncPrefObserver : public SyncPrefObserver {
175 public:
176  MOCK_METHOD1(OnSyncManagedPrefChange, void(bool));
177};
178
179TEST_F(SyncPrefsTest, ObservedPrefs) {
180  SyncPrefs sync_prefs(&pref_service_);
181
182  StrictMock<MockSyncPrefObserver> mock_sync_pref_observer;
183  InSequence dummy;
184  EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(true));
185  EXPECT_CALL(mock_sync_pref_observer, OnSyncManagedPrefChange(false));
186
187  EXPECT_FALSE(sync_prefs.IsManaged());
188
189  sync_prefs.AddSyncPrefObserver(&mock_sync_pref_observer);
190
191  sync_prefs.SetManagedForTest(true);
192  EXPECT_TRUE(sync_prefs.IsManaged());
193  sync_prefs.SetManagedForTest(false);
194  EXPECT_FALSE(sync_prefs.IsManaged());
195
196  sync_prefs.RemoveSyncPrefObserver(&mock_sync_pref_observer);
197}
198
199TEST_F(SyncPrefsTest, AcknowledgeSyncedTypes) {
200  SyncPrefs sync_prefs(&pref_service_);
201
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()));
211  }
212}
213
214TEST_F(SyncPrefsTest, ClearPreferences) {
215  SyncPrefs sync_prefs(&pref_service_);
216
217  EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
218  EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
219  EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
220
221  sync_prefs.SetSyncSetupCompleted();
222  sync_prefs.SetLastSyncedTime(base::Time::Now());
223  sync_prefs.SetEncryptionBootstrapToken("token");
224
225  EXPECT_TRUE(sync_prefs.HasSyncSetupCompleted());
226  EXPECT_NE(base::Time(), sync_prefs.GetLastSyncedTime());
227  EXPECT_EQ("token", sync_prefs.GetEncryptionBootstrapToken());
228
229  sync_prefs.ClearPreferences();
230
231  EXPECT_FALSE(sync_prefs.HasSyncSetupCompleted());
232  EXPECT_EQ(base::Time(), sync_prefs.GetLastSyncedTime());
233  EXPECT_TRUE(sync_prefs.GetEncryptionBootstrapToken().empty());
234}
235
236}  // namespace
237
238}  // namespace browser_sync
239