Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(620)

Side by Side Diff: chrome/browser/sync/glue/new_non_frontend_data_type_controller_unittest.cc

Issue 8274020: Revert 105404 - [Sync] Refactor non-frontend DTC to handle new API properly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/glue/new_non_frontend_data_type_controller.h"
6
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/task.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/tracked_objects.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "chrome/browser/sync/api/syncable_service_mock.h"
15 #include "chrome/browser/sync/engine/model_safe_worker.h"
16 #include "chrome/browser/sync/glue/data_type_controller_mock.h"
17 #include "chrome/browser/sync/glue/new_non_frontend_data_type_controller_mock.h"
18 #include "chrome/browser/sync/glue/shared_change_processor_mock.h"
19 #include "chrome/browser/sync/profile_sync_factory_mock.h"
20 #include "chrome/browser/sync/profile_sync_service_mock.h"
21 #include "chrome/test/base/profile_mock.h"
22 #include "content/browser/browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using base::WaitableEvent;
26 using browser_sync::GenericChangeProcessor;
27 using browser_sync::SharedChangeProcessorMock;
28 using browser_sync::DataTypeController;
29 using browser_sync::GROUP_DB;
30 using browser_sync::NewNonFrontendDataTypeController;
31 using browser_sync::NewNonFrontendDataTypeControllerMock;
32 using browser_sync::StartCallback;
33 using syncable::AUTOFILL_PROFILE;
34 using testing::_;
35 using testing::DoAll;
36 using testing::InvokeWithoutArgs;
37 using testing::Return;
38 using testing::SetArgumentPointee;
39 using testing::StrictMock;
40
41 namespace browser_sync {
42
43 namespace {
44
45 ACTION_P(WaitOnEvent, event) {
46 event->Wait();
47 }
48
49 ACTION_P(SignalEvent, event) {
50 event->Signal();
51 }
52
53 ACTION_P(SaveChangeProcessor, scoped_change_processor) {
54 scoped_change_processor->reset(arg2);
55 }
56
57 ACTION_P(GetWeakPtrToSyncableService, syncable_service) {
58 // Have to do this within an Action to ensure it's not evaluated on the wrong
59 // thread.
60 return syncable_service->AsWeakPtr();
61 }
62
63 class NewNonFrontendDataTypeControllerFake
64 : public NewNonFrontendDataTypeController {
65 public:
66 NewNonFrontendDataTypeControllerFake(
67 ProfileSyncFactory* profile_sync_factory,
68 Profile* profile,
69 NewNonFrontendDataTypeControllerMock* mock)
70 : NewNonFrontendDataTypeController(profile_sync_factory,
71 profile),
72 mock_(mock) {}
73
74 virtual syncable::ModelType type() const OVERRIDE {
75 return AUTOFILL_PROFILE;
76 }
77 virtual ModelSafeGroup model_safe_group() const OVERRIDE {
78 return GROUP_DB;
79 }
80
81 protected:
82 virtual base::WeakPtr<SyncableService>
83 GetWeakPtrToSyncableService() const OVERRIDE {
84 return profile_sync_factory()->GetAutofillProfileSyncableService(NULL);
85 }
86 virtual bool StartAssociationAsync() OVERRIDE {
87 mock_->StartAssociationAsync();
88 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
89 base::Bind(&NewNonFrontendDataTypeControllerFake::StartAssociation,
90 this));
91 }
92
93 // We mock the following methods because their default implementations do
94 // nothing, but we still want to make sure they're called appropriately.
95 virtual bool StartModels() OVERRIDE {
96 return mock_->StartModels();
97 }
98 virtual void StopModels() OVERRIDE {
99 mock_->StopModels();
100 }
101 virtual void StopLocalServiceAsync() OVERRIDE {
102 mock_->StopLocalServiceAsync();
103 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
104 base::Bind(&NewNonFrontendDataTypeControllerFake::StopLocalService,
105 this));
106 }
107 virtual void RecordUnrecoverableError(
108 const tracked_objects::Location& from_here,
109 const std::string& message) OVERRIDE {
110 mock_->RecordUnrecoverableError(from_here, message);
111 }
112 virtual void RecordAssociationTime(base::TimeDelta time) OVERRIDE {
113 mock_->RecordAssociationTime(time);
114 }
115 virtual void RecordStartFailure(DataTypeController::StartResult result)
116 OVERRIDE {
117 mock_->RecordStartFailure(result);
118 }
119 private:
120 NewNonFrontendDataTypeControllerMock* mock_;
121 };
122
123 class NewNonFrontendDataTypeControllerTest : public testing::Test {
124 public:
125 NewNonFrontendDataTypeControllerTest()
126 : ui_thread_(BrowserThread::UI, &message_loop_),
127 db_thread_(BrowserThread::DB) {}
128
129 virtual void SetUp() OVERRIDE {
130 EXPECT_CALL(profile_, GetProfileSyncService()).WillRepeatedly(
131 Return(&service_));
132 EXPECT_CALL(service_, GetUserShare()).WillRepeatedly(
133 Return((sync_api::UserShare*)NULL));
134 db_thread_.Start();
135 profile_sync_factory_.reset(new ProfileSyncFactoryMock());
136 change_processor_ = new SharedChangeProcessorMock();
137
138 // Both of these are refcounted, so don't need to be released.
139 dtc_mock_ = new StrictMock<NewNonFrontendDataTypeControllerMock>();
140 new_non_frontend_dtc_ =
141 new NewNonFrontendDataTypeControllerFake(profile_sync_factory_.get(),
142 &profile_,
143 dtc_mock_.get());
144 }
145
146 virtual void TearDown() OVERRIDE {
147 db_thread_.Stop();
148 }
149
150 void WaitForDTC() {
151 WaitableEvent done(true, false);
152 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
153 base::Bind(&NewNonFrontendDataTypeControllerTest::SignalDone,
154 &done));
155 done.TimedWait(base::TimeDelta::FromMilliseconds(
156 TestTimeouts::action_timeout_ms()));
157 if (!done.IsSignaled()) {
158 ADD_FAILURE() << "Timed out waiting for DB thread to finish.";
159 }
160 MessageLoop::current()->RunAllPending();
161 }
162
163 protected:
164 void SetStartExpectations() {
165 EXPECT_CALL(*dtc_mock_, StartModels()).WillOnce(Return(true));
166 EXPECT_CALL(*profile_sync_factory_,
167 GetAutofillProfileSyncableService(_)).
168 WillOnce(GetWeakPtrToSyncableService(&syncable_service_));
169 EXPECT_CALL(*profile_sync_factory_,
170 CreateSharedChangeProcessor()).
171 WillOnce(Return(change_processor_.get()));
172 }
173
174 void SetAssociateExpectations() {
175 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
176 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
177 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
178 WillOnce(Return(true));
179 EXPECT_CALL(*change_processor_, SyncModelHasUserCreatedNodes(_,_)).
180 WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true)));
181 EXPECT_CALL(*change_processor_, GetSyncDataForType(_,_)).
182 WillOnce(Return(SyncError()));
183 EXPECT_CALL(syncable_service_, MergeDataAndStartSyncing(_,_,_)).
184 WillOnce(DoAll(SaveChangeProcessor(&saved_change_processor_),
185 Return(SyncError())));
186 EXPECT_CALL(*dtc_mock_, RecordAssociationTime(_));
187 }
188
189 void SetActivateExpectations(DataTypeController::StartResult result) {
190 EXPECT_CALL(service_, ActivateDataType(_, _, _));
191 EXPECT_CALL(start_callback_, Run(result,_));
192 }
193
194 void SetStopExpectations() {
195 EXPECT_CALL(*dtc_mock_, StopModels());
196 EXPECT_CALL(*change_processor_, Disconnect()).WillOnce(Return(true));
197 EXPECT_CALL(service_, DeactivateDataType(_));
198 EXPECT_CALL(*dtc_mock_, StopLocalServiceAsync());
199 EXPECT_CALL(syncable_service_, StopSyncing(_));
200 }
201
202 void SetStartFailExpectations(DataTypeController::StartResult result) {
203 EXPECT_CALL(*dtc_mock_, StopModels());
204 EXPECT_CALL(*dtc_mock_, RecordStartFailure(result));
205 EXPECT_CALL(start_callback_, Run(result,_));
206 }
207
208 static void SignalDone(WaitableEvent* done) {
209 done->Signal();
210 }
211
212 MessageLoopForUI message_loop_;
213 BrowserThread ui_thread_;
214 BrowserThread db_thread_;
215 ProfileMock profile_;
216 scoped_ptr<ProfileSyncFactoryMock> profile_sync_factory_;
217 ProfileSyncServiceMock service_;
218 StartCallback start_callback_;
219 // Must be destroyed after new_non_frontend_dtc_.
220 SyncableServiceMock syncable_service_;
221 scoped_refptr<NewNonFrontendDataTypeControllerFake> new_non_frontend_dtc_;
222 scoped_refptr<NewNonFrontendDataTypeControllerMock> dtc_mock_;
223 scoped_refptr<SharedChangeProcessorMock> change_processor_;
224 scoped_ptr<SyncChangeProcessor> saved_change_processor_;
225 };
226
227 TEST_F(NewNonFrontendDataTypeControllerTest, StartOk) {
228 SetStartExpectations();
229 SetAssociateExpectations();
230 SetActivateExpectations(DataTypeController::OK);
231 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
232 new_non_frontend_dtc_->Start(
233 NewCallback(&start_callback_, &StartCallback::Run));
234 WaitForDTC();
235 EXPECT_EQ(DataTypeController::RUNNING, new_non_frontend_dtc_->state());
236 }
237
238 TEST_F(NewNonFrontendDataTypeControllerTest, StartFirstRun) {
239 SetStartExpectations();
240 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
241 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
242 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
243 WillOnce(Return(true));
244 EXPECT_CALL(*change_processor_, SyncModelHasUserCreatedNodes(_,_)).
245 WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true)));
246 EXPECT_CALL(*change_processor_, GetSyncDataForType(_,_)).
247 WillOnce(Return(SyncError()));
248 EXPECT_CALL(syncable_service_, MergeDataAndStartSyncing(_,_,_)).
249 WillOnce(DoAll(SaveChangeProcessor(&saved_change_processor_),
250 Return(SyncError())));
251 EXPECT_CALL(*dtc_mock_, RecordAssociationTime(_));
252 SetActivateExpectations(DataTypeController::OK_FIRST_RUN);
253 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
254 new_non_frontend_dtc_->Start(
255 NewCallback(&start_callback_, &StartCallback::Run));
256 WaitForDTC();
257 EXPECT_EQ(DataTypeController::RUNNING, new_non_frontend_dtc_->state());
258 }
259
260 TEST_F(NewNonFrontendDataTypeControllerTest, AbortDuringStartModels) {
261 EXPECT_CALL(*dtc_mock_, StartModels()).WillOnce(Return(false));
262 SetStartFailExpectations(DataTypeController::ABORTED);
263 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
264 new_non_frontend_dtc_->Start(
265 NewCallback(&start_callback_, &StartCallback::Run));
266 WaitForDTC();
267 EXPECT_EQ(DataTypeController::MODEL_STARTING, new_non_frontend_dtc_->state());
268 new_non_frontend_dtc_->Stop();
269 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
270 }
271
272 TEST_F(NewNonFrontendDataTypeControllerTest, StartAssociationFailed) {
273 SetStartExpectations();
274 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
275 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
276 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
277 WillOnce(Return(true));
278 EXPECT_CALL(*change_processor_, SyncModelHasUserCreatedNodes(_,_)).
279 WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true)));
280 EXPECT_CALL(*change_processor_, GetSyncDataForType(_,_)).
281 WillOnce(Return(SyncError()));
282 EXPECT_CALL(syncable_service_, MergeDataAndStartSyncing(_,_,_)).
283 WillOnce(DoAll(SaveChangeProcessor(&saved_change_processor_),
284 Return(SyncError(FROM_HERE, "failed", AUTOFILL_PROFILE))));
285 EXPECT_CALL(*dtc_mock_, RecordAssociationTime(_));
286 SetStartFailExpectations(DataTypeController::ASSOCIATION_FAILED);
287 EXPECT_CALL(syncable_service_, StopSyncing(_));
288 // Set up association to fail with an association failed error.
289 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
290 new_non_frontend_dtc_->Start(
291 NewCallback(&start_callback_, &StartCallback::Run));
292 WaitForDTC();
293 EXPECT_EQ(DataTypeController::DISABLED, new_non_frontend_dtc_->state());
294 }
295
296 TEST_F(NewNonFrontendDataTypeControllerTest,
297 StartAssociationTriggersUnrecoverableError) {
298 SetStartExpectations();
299 SetStartFailExpectations(DataTypeController::UNRECOVERABLE_ERROR);
300 // Set up association to fail with an unrecoverable error.
301 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
302 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
303 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
304 WillRepeatedly(Return(true));
305 EXPECT_CALL(*change_processor_, SyncModelHasUserCreatedNodes(_,_)).
306 WillRepeatedly(DoAll(SetArgumentPointee<1>(false), Return(false)));
307 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
308 new_non_frontend_dtc_->Start(
309 NewCallback(&start_callback_, &StartCallback::Run));
310 WaitForDTC();
311 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
312 }
313
314 TEST_F(NewNonFrontendDataTypeControllerTest, StartAssociationCryptoNotReady) {
315 SetStartExpectations();
316 SetStartFailExpectations(DataTypeController::NEEDS_CRYPTO);
317 // Set up association to fail with a NEEDS_CRYPTO error.
318 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
319 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
320 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
321 WillRepeatedly(Return(false));
322 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
323 new_non_frontend_dtc_->Start(
324 NewCallback(&start_callback_, &StartCallback::Run));
325 WaitForDTC();
326 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
327 }
328
329 // Trigger a Stop() call when we check if the model associator has user created
330 // nodes.
331 TEST_F(NewNonFrontendDataTypeControllerTest, AbortDuringAssociation) {
332 WaitableEvent wait_for_db_thread_pause(false, false);
333 WaitableEvent pause_db_thread(false, false);
334
335 SetStartExpectations();
336 SetStartFailExpectations(DataTypeController::ABORTED);
337 EXPECT_CALL(*dtc_mock_, StartAssociationAsync());
338 EXPECT_CALL(*change_processor_, Connect(_,_,_,_)).WillOnce(Return(true));
339 EXPECT_CALL(*change_processor_, CryptoReadyIfNecessary(_)).
340 WillOnce(Return(true));
341 EXPECT_CALL(*change_processor_, SyncModelHasUserCreatedNodes(_,_)).
342 WillOnce(DoAll(
343 SignalEvent(&wait_for_db_thread_pause),
344 WaitOnEvent(&pause_db_thread),
345 SetArgumentPointee<1>(true),
346 Return(true)));
347 EXPECT_CALL(*change_processor_, GetSyncDataForType(_,_)).
348 WillOnce(Return(SyncError(FROM_HERE, "Disconnected.", AUTOFILL_PROFILE)));
349 EXPECT_CALL(*change_processor_, Disconnect()).
350 WillOnce(DoAll(SignalEvent(&pause_db_thread), Return(true)));
351 EXPECT_CALL(service_, DeactivateDataType(_));
352 EXPECT_CALL(*dtc_mock_, StopLocalServiceAsync());
353 EXPECT_CALL(syncable_service_, StopSyncing(_));
354 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
355 new_non_frontend_dtc_->Start(
356 NewCallback(&start_callback_, &StartCallback::Run));
357 wait_for_db_thread_pause.Wait();
358 new_non_frontend_dtc_->Stop();
359 WaitForDTC();
360 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
361 }
362
363 TEST_F(NewNonFrontendDataTypeControllerTest, Stop) {
364 SetStartExpectations();
365 SetAssociateExpectations();
366 SetActivateExpectations(DataTypeController::OK);
367 SetStopExpectations();
368 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
369 new_non_frontend_dtc_->Start(
370 NewCallback(&start_callback_, &StartCallback::Run));
371 WaitForDTC();
372 EXPECT_EQ(DataTypeController::RUNNING, new_non_frontend_dtc_->state());
373 new_non_frontend_dtc_->Stop();
374 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
375 }
376
377 TEST_F(NewNonFrontendDataTypeControllerTest, OnUnrecoverableError) {
378 SetStartExpectations();
379 SetAssociateExpectations();
380 SetActivateExpectations(DataTypeController::OK);
381 EXPECT_CALL(*dtc_mock_, RecordUnrecoverableError(_, "Test"));
382 EXPECT_CALL(service_, OnUnrecoverableError(_,_)).WillOnce(
383 InvokeWithoutArgs(new_non_frontend_dtc_.get(),
384 &NewNonFrontendDataTypeController::Stop));
385 SetStopExpectations();
386 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
387 new_non_frontend_dtc_->Start(
388 NewCallback(&start_callback_, &StartCallback::Run));
389 WaitForDTC();
390 EXPECT_EQ(DataTypeController::RUNNING, new_non_frontend_dtc_->state());
391 // This should cause new_non_frontend_dtc_->Stop() to be called.
392 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
393 base::Bind(
394 &NewNonFrontendDataTypeControllerFake::OnUnrecoverableError,
395 new_non_frontend_dtc_.get(),
396 FROM_HERE,
397 std::string("Test")));
398 WaitForDTC();
399 EXPECT_EQ(DataTypeController::NOT_RUNNING, new_non_frontend_dtc_->state());
400 }
401
402 } // namespace
403
404 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698