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