test_profile_sync_service.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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/test_profile_sync_service.h"
6
7#include "chrome/browser/sync/abstract_profile_sync_service_test.h"
8#include "chrome/browser/sync/engine/syncapi.h"
9#include "chrome/browser/sync/glue/data_type_controller.h"
10#include "chrome/browser/sync/glue/sync_backend_host.h"
11#include "chrome/browser/sync/profile_sync_factory.h"
12#include "chrome/browser/sync/sessions/session_state.h"
13#include "chrome/browser/sync/syncable/directory_manager.h"
14#include "chrome/browser/sync/syncable/syncable.h"
15#include "chrome/test/sync/test_http_bridge_factory.h"
16
17using browser_sync::ModelSafeRoutingInfo;
18using browser_sync::sessions::ErrorCounters;
19using browser_sync::sessions::SyncSourceInfo;
20using browser_sync::sessions::SyncerStatus;
21using browser_sync::sessions::SyncSessionSnapshot;
22using syncable::DirectoryManager;
23using syncable::ModelType;
24using syncable::ScopedDirLookup;
25using sync_api::UserShare;
26
27ACTION_P(CallOnPaused, core) {
28  core->OnPaused();
29};
30
31ACTION_P(CallOnResumed, core) {
32  core->OnResumed();
33}
34
35namespace browser_sync {
36
37SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
38    Profile* profile,
39    int num_expected_resumes,
40    int num_expected_pauses,
41    bool set_initial_sync_ended_on_init,
42    bool synchronous_init)
43    : browser_sync::SyncBackendHost(profile),
44      synchronous_init_(synchronous_init) {
45  // By default, the RequestPause and RequestResume methods will
46  // send the confirmation notification and return true.
47  ON_CALL(*this, RequestPause()).
48      WillByDefault(testing::DoAll(CallOnPaused(core_),
49                                   testing::Return(true)));
50  ON_CALL(*this, RequestResume()).
51      WillByDefault(testing::DoAll(CallOnResumed(core_),
52                                   testing::Return(true)));
53  ON_CALL(*this, RequestNudge()).WillByDefault(
54      testing::Invoke(this,
55                      &SyncBackendHostForProfileSyncTest::
56                      SimulateSyncCycleCompletedInitialSyncEnded));
57
58  EXPECT_CALL(*this, RequestPause()).Times(num_expected_pauses);
59  EXPECT_CALL(*this, RequestResume()).Times(num_expected_resumes);
60  EXPECT_CALL(*this,
61              RequestNudge()).Times(set_initial_sync_ended_on_init ? 0 : 1);
62}
63
64SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
65
66void SyncBackendHostForProfileSyncTest::ConfigureDataTypes(
67    const DataTypeController::TypeMap& data_type_controllers,
68    const syncable::ModelTypeSet& types,
69    CancelableTask* ready_task) {
70  SetAutofillMigrationState(syncable::MIGRATED);
71  SyncBackendHost::ConfigureDataTypes(
72      data_type_controllers, types, ready_task);
73}
74
75void SyncBackendHostForProfileSyncTest::
76    SimulateSyncCycleCompletedInitialSyncEnded() {
77  syncable::ModelTypeBitSet sync_ended;
78  ModelSafeRoutingInfo enabled_types;
79  GetModelSafeRoutingInfo(&enabled_types);
80  std::string download_progress_markers[syncable::MODEL_TYPE_COUNT];
81  for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin();
82       i != enabled_types.end(); ++i) {
83    sync_ended.set(i->first);
84  }
85  core_->HandleSyncCycleCompletedOnFrontendLoop(new SyncSessionSnapshot(
86      SyncerStatus(), ErrorCounters(), 0, false,
87      sync_ended, download_progress_markers, false, false, 0, 0, false,
88      SyncSourceInfo()));
89}
90
91sync_api::HttpPostProviderFactory*
92    SyncBackendHostForProfileSyncTest::MakeHttpBridgeFactory(
93        URLRequestContextGetter* getter) {
94  return new browser_sync::TestHttpBridgeFactory;
95}
96
97void SyncBackendHostForProfileSyncTest::InitCore(
98    const Core::DoInitializeOptions& options) {
99  std::wstring user = L"testuser";
100  core_loop()->PostTask(
101      FROM_HERE,
102      NewRunnableMethod(core_.get(),
103                        &SyncBackendHost::Core::DoInitializeForTest,
104                        user,
105                        options.http_bridge_factory,
106                        options.delete_sync_data_folder));
107
108  // TODO(akalin): Figure out a better way to do this.
109  if (synchronous_init_) {
110    // The SyncBackend posts a task to the current loop when
111    // initialization completes.
112    MessageLoop::current()->Run();
113  }
114}
115
116JsBackend* SyncBackendHostForProfileSyncTest::GetJsBackend() {
117  // Return a non-NULL result only when the overridden function does.
118  if (SyncBackendHost::GetJsBackend()) {
119    return this;
120  } else {
121    NOTREACHED();
122    return NULL;
123  }
124}
125
126void SyncBackendHostForProfileSyncTest::SetParentJsEventRouter(
127    JsEventRouter* router) {
128  core_->SetParentJsEventRouter(router);
129}
130
131void SyncBackendHostForProfileSyncTest::RemoveParentJsEventRouter() {
132  core_->RemoveParentJsEventRouter();
133}
134
135const JsEventRouter*
136    SyncBackendHostForProfileSyncTest::GetParentJsEventRouter() const {
137  return core_->GetParentJsEventRouter();
138}
139
140void SyncBackendHostForProfileSyncTest::ProcessMessage(
141    const std::string& name, const JsArgList& args,
142    const JsEventHandler* sender) {
143  if (name.find("delay") != name.npos) {
144    core_->RouteJsEvent(name, args, sender);
145  } else {
146    core_->RouteJsEventOnFrontendLoop(name, args, sender);
147  }
148}
149
150void SyncBackendHostForProfileSyncTest::
151    SetDefaultExpectationsForWorkerCreation(ProfileMock* profile) {
152  EXPECT_CALL(*profile, GetPasswordStore(testing::_)).
153      WillOnce(testing::Return((PasswordStore*)NULL));
154}
155
156void SyncBackendHostForProfileSyncTest::SetHistoryServiceExpectations(
157    ProfileMock* profile) {
158  EXPECT_CALL(*profile, GetHistoryService(testing::_)).
159      WillOnce(testing::Return((HistoryService*)NULL));
160}
161
162}  // namespace browser_sync
163
164browser_sync::TestIdFactory* TestProfileSyncService::id_factory() {
165  return &id_factory_;
166}
167
168browser_sync::SyncBackendHostForProfileSyncTest*
169    TestProfileSyncService::GetBackendForTest() {
170  return static_cast<browser_sync::SyncBackendHostForProfileSyncTest*>(
171      ProfileSyncService::GetBackendForTest());
172}
173
174TestProfileSyncService::TestProfileSyncService(
175    ProfileSyncFactory* factory,
176                       Profile* profile,
177                       const std::string& test_user,
178                       bool synchronous_backend_initialization,
179                       Task* initial_condition_setup_task)
180    : ProfileSyncService(factory, profile, test_user),
181      synchronous_backend_initialization_(
182          synchronous_backend_initialization),
183      synchronous_sync_configuration_(false),
184      num_expected_resumes_(1),
185      num_expected_pauses_(1),
186      initial_condition_setup_task_(initial_condition_setup_task),
187      set_initial_sync_ended_on_init_(true) {
188  RegisterPreferences();
189  SetSyncSetupCompleted();
190}
191
192TestProfileSyncService::~TestProfileSyncService() {}
193
194void TestProfileSyncService::SetInitialSyncEndedForEnabledTypes() {
195  UserShare* user_share = GetUserShare();
196  DirectoryManager* dir_manager = user_share->dir_manager.get();
197
198  ScopedDirLookup dir(dir_manager, user_share->name);
199  if (!dir.good())
200    FAIL();
201
202  ModelSafeRoutingInfo enabled_types;
203  backend_->GetModelSafeRoutingInfo(&enabled_types);
204  for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin();
205       i != enabled_types.end(); ++i) {
206    dir->set_initial_sync_ended_for_type(i->first, true);
207  }
208}
209
210void TestProfileSyncService::OnBackendInitialized() {
211  // Set this so below code can access GetUserShare().
212  backend_initialized_ = true;
213
214  // Set up any nodes the test wants around before model association.
215  if (initial_condition_setup_task_) {
216    initial_condition_setup_task_->Run();
217    initial_condition_setup_task_ = NULL;
218  }
219
220  // Pretend we downloaded initial updates and set initial sync ended bits
221  // if we were asked to.
222  if (set_initial_sync_ended_on_init_) {
223    UserShare* user_share = GetUserShare();
224    DirectoryManager* dir_manager = user_share->dir_manager.get();
225
226    ScopedDirLookup dir(dir_manager, user_share->name);
227    if (!dir.good())
228      FAIL();
229
230    if (!dir->initial_sync_ended_for_type(syncable::NIGORI)) {
231      ProfileSyncServiceTestHelper::CreateRoot(
232          syncable::NIGORI, GetUserShare(),
233          id_factory());
234    }
235
236    SetInitialSyncEndedForEnabledTypes();
237  }
238
239  ProfileSyncService::OnBackendInitialized();
240
241  // TODO(akalin): Figure out a better way to do this.
242  if (synchronous_backend_initialization_) {
243    MessageLoop::current()->Quit();
244  }
245}
246
247void TestProfileSyncService::Observe(NotificationType type,
248                                     const NotificationSource& source,
249                                     const NotificationDetails& details) {
250  ProfileSyncService::Observe(type, source, details);
251  if (type == NotificationType::SYNC_CONFIGURE_DONE &&
252      !synchronous_sync_configuration_) {
253    MessageLoop::current()->Quit();
254  }
255}
256
257void TestProfileSyncService::set_num_expected_resumes(int times) {
258  num_expected_resumes_ = times;
259}
260void TestProfileSyncService::set_num_expected_pauses(int num) {
261  num_expected_pauses_ = num;
262}
263void TestProfileSyncService::dont_set_initial_sync_ended_on_init() {
264  set_initial_sync_ended_on_init_ = false;
265}
266void TestProfileSyncService::set_synchronous_sync_configuration() {
267  synchronous_sync_configuration_ = true;
268}
269
270void TestProfileSyncService::CreateBackend() {
271  backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest(
272      profile(),
273      num_expected_resumes_, num_expected_pauses_,
274      set_initial_sync_ended_on_init_,
275      synchronous_backend_initialization_));
276}
277
278std::string TestProfileSyncService::GetLsidForAuthBootstraping() {
279  return "foo";
280}
281