Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_service_autofill_unittest.cc
blob74075bfbdcfe45409dfd46b39525314f0383f927
1 // Copyright 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 <set>
6 #include <string>
7 #include <utility>
8 #include <vector>
10 #include "testing/gtest/include/gtest/gtest.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/location.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/synchronization/waitable_event.h"
23 #include "base/time/time.h"
24 #include "chrome/browser/autofill/personal_data_manager_factory.h"
25 #include "chrome/browser/signin/account_tracker_service_factory.h"
26 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
28 #include "chrome/browser/signin/signin_manager_factory.h"
29 #include "chrome/browser/sync/abstract_profile_sync_service_test.h"
30 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
31 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
32 #include "chrome/browser/sync/profile_sync_service.h"
33 #include "chrome/browser/sync/profile_sync_service_factory.h"
34 #include "chrome/browser/sync/profile_sync_test_util.h"
35 #include "chrome/browser/sync/test_profile_sync_service.h"
36 #include "chrome/browser/web_data_service_factory.h"
37 #include "chrome/test/base/testing_browser_process.h"
38 #include "chrome/test/base/testing_profile.h"
39 #include "chrome/test/base/testing_profile_manager.h"
40 #include "components/autofill/core/browser/autofill_test_utils.h"
41 #include "components/autofill/core/browser/personal_data_manager.h"
42 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
43 #include "components/autofill/core/browser/webdata/autofill_change.h"
44 #include "components/autofill/core/browser/webdata/autofill_entry.h"
45 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
46 #include "components/autofill/core/browser/webdata/autofill_table.h"
47 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
48 #include "components/signin/core/browser/account_tracker_service.h"
49 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
50 #include "components/signin/core/browser/signin_manager.h"
51 #include "components/sync_driver/data_type_controller.h"
52 #include "components/sync_driver/fake_sync_client.h"
53 #include "components/syncable_prefs/pref_service_syncable.h"
54 #include "components/webdata/common/web_database.h"
55 #include "components/webdata_services/web_data_service_test_util.h"
56 #include "content/public/test/test_browser_thread.h"
57 #include "google_apis/gaia/gaia_constants.h"
58 #include "sync/internal_api/public/base/model_type.h"
59 #include "sync/internal_api/public/data_type_debug_info_listener.h"
60 #include "sync/internal_api/public/read_node.h"
61 #include "sync/internal_api/public/read_transaction.h"
62 #include "sync/internal_api/public/write_node.h"
63 #include "sync/internal_api/public/write_transaction.h"
64 #include "sync/protocol/autofill_specifics.pb.h"
65 #include "sync/syncable/mutable_entry.h"
66 #include "sync/syncable/syncable_write_transaction.h"
67 #include "sync/test/engine/test_id_factory.h"
68 #include "testing/gmock/include/gmock/gmock.h"
70 using autofill::AutocompleteSyncableService;
71 using autofill::AutofillChange;
72 using autofill::AutofillChangeList;
73 using autofill::AutofillEntry;
74 using autofill::ServerFieldType;
75 using autofill::AutofillKey;
76 using autofill::AutofillProfile;
77 using autofill::AutofillProfileChange;
78 using autofill::AutofillProfileSyncableService;
79 using autofill::AutofillTable;
80 using autofill::AutofillWebDataService;
81 using autofill::PersonalDataManager;
82 using base::Time;
83 using base::TimeDelta;
84 using base::WaitableEvent;
85 using browser_sync::AutofillDataTypeController;
86 using browser_sync::AutofillProfileDataTypeController;
87 using content::BrowserThread;
88 using syncer::AUTOFILL;
89 using syncer::AUTOFILL_PROFILE;
90 using syncer::BaseNode;
91 using syncer::syncable::BASE_VERSION;
92 using syncer::syncable::CREATE;
93 using syncer::syncable::GET_TYPE_ROOT;
94 using syncer::syncable::MutableEntry;
95 using syncer::syncable::SERVER_SPECIFICS;
96 using syncer::syncable::SPECIFICS;
97 using syncer::syncable::UNITTEST;
98 using syncer::syncable::WriterTag;
99 using syncer::syncable::WriteTransaction;
100 using sync_driver::DataTypeController;
101 using testing::_;
102 using testing::DoAll;
103 using testing::ElementsAre;
104 using testing::Not;
105 using testing::SetArgumentPointee;
106 using testing::Return;
108 namespace {
110 const char kTestProfileName[] = "test-profile";
112 void RunAndSignal(const base::Closure& cb, WaitableEvent* event) {
113 cb.Run();
114 event->Signal();
117 } // namespace
119 class AutofillTableMock : public AutofillTable {
120 public:
121 AutofillTableMock() {}
122 MOCK_METHOD2(RemoveFormElement,
123 bool(const base::string16& name,
124 const base::string16& value)); // NOLINT
125 MOCK_METHOD1(GetAllAutofillEntries,
126 bool(std::vector<AutofillEntry>* entries)); // NOLINT
127 MOCK_METHOD4(GetAutofillTimestamps,
128 bool(const base::string16& name, // NOLINT
129 const base::string16& value,
130 base::Time* date_created,
131 base::Time* date_last_used));
132 MOCK_METHOD1(UpdateAutofillEntries,
133 bool(const std::vector<AutofillEntry>&)); // NOLINT
134 MOCK_METHOD1(GetAutofillProfiles,
135 bool(std::vector<AutofillProfile*>*)); // NOLINT
136 MOCK_METHOD1(UpdateAutofillProfile,
137 bool(const AutofillProfile&)); // NOLINT
138 MOCK_METHOD1(AddAutofillProfile,
139 bool(const AutofillProfile&)); // NOLINT
140 MOCK_METHOD1(RemoveAutofillProfile,
141 bool(const std::string&)); // NOLINT
144 MATCHER_P(MatchProfiles, profile, "") {
145 return (profile.Compare(arg) == 0);
148 class TestSyncClient : public sync_driver::FakeSyncClient {
149 public:
150 TestSyncClient(PersonalDataManager* pdm,
151 const scoped_refptr<AutofillWebDataService>& web_data_service)
152 : pdm_(pdm),
153 sync_service_(nullptr),
154 web_data_service_(web_data_service) {}
155 ~TestSyncClient() override {}
157 // FakeSyncClient overrides.
158 autofill::PersonalDataManager* GetPersonalDataManager() override {
159 return pdm_;
161 sync_driver::SyncService* GetSyncService() override {
162 DCHECK(sync_service_);
163 return sync_service_;
165 scoped_refptr<autofill::AutofillWebDataService> GetWebDataService() override {
166 return web_data_service_;
168 base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType(
169 syncer::ModelType type) override {
170 DCHECK(type == AUTOFILL || type == AUTOFILL_PROFILE);
171 if (type == AUTOFILL) {
172 return AutocompleteSyncableService::FromWebDataService(
173 web_data_service_.get())->AsWeakPtr();
174 } else {
175 return AutofillProfileSyncableService::FromWebDataService(
176 web_data_service_.get())->AsWeakPtr();
180 void SetSyncService(sync_driver::SyncService* sync_service) {
181 sync_service_ = sync_service;
184 private:
185 PersonalDataManager* pdm_;
186 sync_driver::SyncService* sync_service_;
187 scoped_refptr<AutofillWebDataService> web_data_service_;
190 class WebDatabaseFake : public WebDatabase {
191 public:
192 explicit WebDatabaseFake(AutofillTable* autofill_table) {
193 AddTable(autofill_table);
197 class MockAutofillBackend : public autofill::AutofillWebDataBackend {
198 public:
199 MockAutofillBackend(
200 WebDatabase* web_database,
201 const base::Closure& on_changed)
202 : web_database_(web_database),
203 on_changed_(on_changed) {
206 ~MockAutofillBackend() override {}
207 WebDatabase* GetDatabase() override { return web_database_; }
208 void AddObserver(
209 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
210 void RemoveObserver(
211 autofill::AutofillWebDataServiceObserverOnDBThread* observer) override {}
212 void RemoveExpiredFormElements() override {}
213 void NotifyOfMultipleAutofillChanges() override {
214 DCHECK_CURRENTLY_ON(BrowserThread::DB);
215 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, on_changed_);
218 private:
219 WebDatabase* web_database_;
220 base::Closure on_changed_;
223 class ProfileSyncServiceAutofillTest;
225 template<class AutofillProfile>
226 syncer::ModelType GetModelType() {
227 return syncer::UNSPECIFIED;
230 template<>
231 syncer::ModelType GetModelType<AutofillEntry>() {
232 return syncer::AUTOFILL;
235 template<>
236 syncer::ModelType GetModelType<AutofillProfile>() {
237 return syncer::AUTOFILL_PROFILE;
240 class TokenWebDataServiceFake : public TokenWebData {
241 public:
242 TokenWebDataServiceFake()
243 : TokenWebData(
244 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
245 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)) {
248 bool IsDatabaseLoaded() override { return true; }
250 AutofillWebDataService::Handle GetAllTokens(
251 WebDataServiceConsumer* consumer) override {
252 // TODO(tim): It would be nice if WebDataService was injected on
253 // construction of ProfileOAuth2TokenService rather than fetched by
254 // Initialize so that this isn't necessary (we could pass a NULL service).
255 // We currently do return it via EXPECT_CALLs, but without depending on
256 // order-of-initialization (which seems way more fragile) we can't tell
257 // which component is asking at what time, and some components in these
258 // Autofill tests require a WebDataService.
259 return 0;
262 private:
263 ~TokenWebDataServiceFake() override {}
265 DISALLOW_COPY_AND_ASSIGN(TokenWebDataServiceFake);
268 class WebDataServiceFake : public AutofillWebDataService {
269 public:
270 WebDataServiceFake()
271 : AutofillWebDataService(
272 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
273 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)),
274 web_database_(NULL),
275 autocomplete_syncable_service_(NULL),
276 autofill_profile_syncable_service_(NULL),
277 syncable_service_created_or_destroyed_(false, false) {
280 void SetDatabase(WebDatabase* web_database) {
281 web_database_ = web_database;
284 void StartSyncableService() {
285 // The |autofill_profile_syncable_service_| must be constructed on the DB
286 // thread.
287 const base::Closure& on_changed_callback = base::Bind(
288 &WebDataServiceFake::NotifyAutofillMultipleChangedOnUIThread,
289 AsWeakPtr());
291 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
292 base::Bind(&WebDataServiceFake::CreateSyncableService,
293 base::Unretained(this),
294 on_changed_callback));
295 syncable_service_created_or_destroyed_.Wait();
298 void ShutdownSyncableService() {
299 // The |autofill_profile_syncable_service_| must be destructed on the DB
300 // thread.
301 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
302 base::Bind(&WebDataServiceFake::DestroySyncableService,
303 base::Unretained(this)));
304 syncable_service_created_or_destroyed_.Wait();
307 bool IsDatabaseLoaded() override { return true; }
309 WebDatabase* GetDatabase() override { return web_database_; }
311 void OnAutofillEntriesChanged(const AutofillChangeList& changes) {
312 WaitableEvent event(true, false);
314 base::Closure notify_cb =
315 base::Bind(&AutocompleteSyncableService::AutofillEntriesChanged,
316 base::Unretained(autocomplete_syncable_service_),
317 changes);
318 BrowserThread::PostTask(
319 BrowserThread::DB,
320 FROM_HERE,
321 base::Bind(&RunAndSignal, notify_cb, &event));
322 event.Wait();
325 void OnAutofillProfileChanged(const AutofillProfileChange& changes) {
326 WaitableEvent event(true, false);
328 base::Closure notify_cb =
329 base::Bind(&AutocompleteSyncableService::AutofillProfileChanged,
330 base::Unretained(autofill_profile_syncable_service_),
331 changes);
332 BrowserThread::PostTask(
333 BrowserThread::DB,
334 FROM_HERE,
335 base::Bind(&RunAndSignal, notify_cb, &event));
336 event.Wait();
339 private:
340 ~WebDataServiceFake() override {}
342 void CreateSyncableService(const base::Closure& on_changed_callback) {
343 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB));
344 // These services are deleted in DestroySyncableService().
345 backend_.reset(new MockAutofillBackend(
346 GetDatabase(), on_changed_callback));
347 AutocompleteSyncableService::CreateForWebDataServiceAndBackend(
348 this, backend_.get());
349 AutofillProfileSyncableService::CreateForWebDataServiceAndBackend(
350 this, backend_.get(), "en-US");
352 autocomplete_syncable_service_ =
353 AutocompleteSyncableService::FromWebDataService(this);
354 autofill_profile_syncable_service_ =
355 AutofillProfileSyncableService::FromWebDataService(this);
357 syncable_service_created_or_destroyed_.Signal();
360 void DestroySyncableService() {
361 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB));
362 autocomplete_syncable_service_ = NULL;
363 autofill_profile_syncable_service_ = NULL;
364 backend_.reset();
365 syncable_service_created_or_destroyed_.Signal();
368 WebDatabase* web_database_;
369 AutocompleteSyncableService* autocomplete_syncable_service_;
370 AutofillProfileSyncableService* autofill_profile_syncable_service_;
371 scoped_ptr<autofill::AutofillWebDataBackend> backend_;
373 WaitableEvent syncable_service_created_or_destroyed_;
375 DISALLOW_COPY_AND_ASSIGN(WebDataServiceFake);
378 scoped_ptr<KeyedService> BuildMockWebDataServiceWrapper(
379 content::BrowserContext* profile) {
380 return make_scoped_ptr(new MockWebDataServiceWrapper(
381 new WebDataServiceFake(), new TokenWebDataServiceFake()));
384 ACTION_P(MakeAutocompleteSyncComponents, wds) {
385 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB));
386 if (!BrowserThread::CurrentlyOn(BrowserThread::DB))
387 return base::WeakPtr<syncer::SyncableService>();
388 return AutocompleteSyncableService::FromWebDataService(wds)->AsWeakPtr();
391 ACTION_P(ReturnNewDataTypeManagerWithDebugListener, debug_listener) {
392 return new sync_driver::DataTypeManagerImpl(
393 base::Closure(),
394 debug_listener,
395 arg1,
396 arg2,
397 arg3,
398 arg4);
401 class MockPersonalDataManager : public PersonalDataManager {
402 public:
403 MockPersonalDataManager() : PersonalDataManager("en-US") {}
404 MOCK_CONST_METHOD0(IsDataLoaded, bool());
405 MOCK_METHOD0(LoadProfiles, void());
406 MOCK_METHOD0(LoadCreditCards, void());
407 MOCK_METHOD0(Refresh, void());
409 static scoped_ptr<KeyedService> Build(content::BrowserContext* profile) {
410 return make_scoped_ptr(new MockPersonalDataManager());
414 template <class T> class AddAutofillHelper;
416 class ProfileSyncServiceAutofillTest
417 : public AbstractProfileSyncServiceTest,
418 public syncer::DataTypeDebugInfoListener {
419 public:
420 // DataTypeDebugInfoListener implementation.
421 void OnDataTypeConfigureComplete(const std::vector<
422 syncer::DataTypeConfigurationStats>& configuration_stats) override {
423 ASSERT_EQ(1u, configuration_stats.size());
424 association_stats_ = configuration_stats[0].association_stats;
427 protected:
428 ProfileSyncServiceAutofillTest()
429 : profile_manager_(TestingBrowserProcess::GetGlobal()),
430 debug_ptr_factory_(this) {
432 ~ProfileSyncServiceAutofillTest() override {}
434 void SetUp() override {
435 AbstractProfileSyncServiceTest::SetUp();
436 ASSERT_TRUE(profile_manager_.SetUp());
437 TestingProfile::TestingFactories testing_factories;
438 testing_factories.push_back(std::make_pair(
439 ProfileOAuth2TokenServiceFactory::GetInstance(),
440 BuildAutoIssuingFakeProfileOAuth2TokenService));
441 profile_ = profile_manager_.CreateTestingProfile(
442 kTestProfileName,
443 scoped_ptr<syncable_prefs::PrefServiceSyncable>(),
444 base::UTF8ToUTF16(kTestProfileName),
446 std::string(),
447 testing_factories);
448 web_database_.reset(new WebDatabaseFake(&autofill_table_));
449 MockWebDataServiceWrapper* wrapper =
450 static_cast<MockWebDataServiceWrapper*>(
451 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse(
452 profile_, BuildMockWebDataServiceWrapper));
453 web_data_service_ =
454 static_cast<WebDataServiceFake*>(wrapper->GetAutofillWebData().get());
455 web_data_service_->SetDatabase(web_database_.get());
457 personal_data_manager_ = static_cast<MockPersonalDataManager*>(
458 autofill::PersonalDataManagerFactory::GetInstance()
459 ->SetTestingFactoryAndUse(profile_,
460 MockPersonalDataManager::Build));
462 EXPECT_CALL(*personal_data_manager_, LoadProfiles()).Times(1);
463 EXPECT_CALL(*personal_data_manager_, LoadCreditCards()).Times(1);
465 personal_data_manager_->Init(
466 WebDataServiceFactory::GetAutofillWebDataForProfile(
467 profile_, ServiceAccessType::EXPLICIT_ACCESS),
468 profile_->GetPrefs(),
469 AccountTrackerServiceFactory::GetForProfile(profile_),
470 profile_->IsOffTheRecord());
472 web_data_service_->StartSyncableService();
474 sync_client_.reset(new TestSyncClient(personal_data_manager_,
475 web_data_service_));
477 // When UpdateAutofillEntries() is called with an empty list, the return
478 // value should be |true|, rather than the default of |false|.
479 std::vector<AutofillEntry> empty;
480 EXPECT_CALL(autofill_table_, UpdateAutofillEntries(empty))
481 .WillRepeatedly(Return(true));
484 void TearDown() override {
485 // Note: The tear down order is important.
486 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL);
487 web_data_service_->ShutdownOnUIThread();
488 web_data_service_->ShutdownSyncableService();
489 web_data_service_ = NULL;
490 // To prevent a leak, fully release TestURLRequestContext to ensure its
491 // destruction on the IO message loop.
492 profile_ = NULL;
493 profile_manager_.DeleteTestingProfile(kTestProfileName);
494 AbstractProfileSyncServiceTest::TearDown();
497 int GetSyncCount(syncer::ModelType type) {
498 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
499 syncer::ReadNode node(&trans);
500 if (node.InitTypeRoot(type) != syncer::BaseNode::INIT_OK)
501 return 0;
502 return node.GetTotalNodeCount() - 1;
505 void StartSyncService(const base::Closure& callback,
506 bool will_fail_association,
507 syncer::ModelType type) {
508 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile_);
509 signin->SetAuthenticatedAccountInfo("12345", "test_user@gmail.com");
510 sync_service_ = TestProfileSyncService::BuildAutoStartAsyncInit(profile_,
511 callback);
512 sync_client_->SetSyncService(sync_service_);
514 ProfileSyncComponentsFactoryMock* components =
515 sync_service_->components_factory_mock();
517 EXPECT_CALL(*components, CreateDataTypeManager(_, _, _, _, _)).
518 WillOnce(ReturnNewDataTypeManagerWithDebugListener(
519 syncer::MakeWeakHandle(debug_ptr_factory_.GetWeakPtr())));
521 EXPECT_CALL(*personal_data_manager_, IsDataLoaded()).
522 WillRepeatedly(Return(true));
524 // We need tokens to get the tests going
525 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)
526 ->UpdateCredentials(signin->GetAuthenticatedAccountId(),
527 "oauth2_login_token");
529 sync_service_->RegisterDataTypeController(CreateDataTypeController(type));
530 sync_service_->Initialize();
531 base::MessageLoop::current()->Run();
533 // It's possible this test triggered an unrecoverable error, in which case
534 // we can't get the sync count.
535 if (sync_service_->IsSyncActive()) {
536 EXPECT_EQ(GetSyncCount(type),
537 association_stats_.num_sync_items_after_association);
539 EXPECT_EQ(association_stats_.num_sync_items_after_association,
540 association_stats_.num_sync_items_before_association +
541 association_stats_.num_sync_items_added -
542 association_stats_.num_sync_items_deleted);
545 bool AddAutofillSyncNode(const AutofillEntry& entry) {
546 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare());
547 syncer::WriteNode node(&trans);
548 std::string tag = AutocompleteSyncableService::KeyToTag(
549 base::UTF16ToUTF8(entry.key().name()),
550 base::UTF16ToUTF8(entry.key().value()));
551 syncer::WriteNode::InitUniqueByCreationResult result =
552 node.InitUniqueByCreation(syncer::AUTOFILL, tag);
553 if (result != syncer::WriteNode::INIT_SUCCESS)
554 return false;
556 sync_pb::EntitySpecifics specifics;
557 AutocompleteSyncableService::WriteAutofillEntry(entry, &specifics);
558 node.SetEntitySpecifics(specifics);
559 return true;
562 bool AddAutofillSyncNode(const AutofillProfile& profile) {
563 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare());
564 syncer::WriteNode node(&trans);
565 std::string tag = profile.guid();
566 syncer::WriteNode::InitUniqueByCreationResult result =
567 node.InitUniqueByCreation(syncer::AUTOFILL_PROFILE, tag);
568 if (result != syncer::WriteNode::INIT_SUCCESS)
569 return false;
571 sync_pb::EntitySpecifics specifics;
572 AutofillProfileSyncableService::WriteAutofillProfile(profile, &specifics);
573 node.SetEntitySpecifics(specifics);
574 return true;
577 bool GetAutofillEntriesFromSyncDB(std::vector<AutofillEntry>* entries,
578 std::vector<AutofillProfile>* profiles) {
579 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
580 syncer::ReadNode autofill_root(&trans);
581 if (autofill_root.InitTypeRoot(syncer::AUTOFILL) != BaseNode::INIT_OK) {
582 return false;
585 int64 child_id = autofill_root.GetFirstChildId();
586 while (child_id != syncer::kInvalidId) {
587 syncer::ReadNode child_node(&trans);
588 if (child_node.InitByIdLookup(child_id) != BaseNode::INIT_OK)
589 return false;
591 const sync_pb::AutofillSpecifics& autofill(
592 child_node.GetEntitySpecifics().autofill());
593 if (autofill.has_value()) {
594 AutofillKey key(base::UTF8ToUTF16(autofill.name()),
595 base::UTF8ToUTF16(autofill.value()));
596 std::vector<base::Time> timestamps;
597 int timestamps_count = autofill.usage_timestamp_size();
598 for (int i = 0; i < timestamps_count; ++i) {
599 timestamps.push_back(Time::FromInternalValue(
600 autofill.usage_timestamp(i)));
602 entries->push_back(
603 AutofillEntry(key, timestamps.front(), timestamps.back()));
604 } else if (autofill.has_profile()) {
605 AutofillProfile p;
606 p.set_guid(autofill.profile().guid());
607 AutofillProfileSyncableService::OverwriteProfileWithServerData(
608 autofill.profile(), &p, "en-US");
609 profiles->push_back(p);
611 child_id = child_node.GetSuccessorId();
613 return true;
616 bool GetAutofillProfilesFromSyncDBUnderProfileNode(
617 std::vector<AutofillProfile>* profiles) {
618 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
619 syncer::ReadNode autofill_root(&trans);
620 if (autofill_root.InitTypeRoot(AUTOFILL_PROFILE) != BaseNode::INIT_OK) {
621 return false;
624 int64 child_id = autofill_root.GetFirstChildId();
625 while (child_id != syncer::kInvalidId) {
626 syncer::ReadNode child_node(&trans);
627 if (child_node.InitByIdLookup(child_id) != BaseNode::INIT_OK)
628 return false;
630 const sync_pb::AutofillProfileSpecifics& autofill(
631 child_node.GetEntitySpecifics().autofill_profile());
632 AutofillProfile p;
633 p.set_guid(autofill.guid());
634 AutofillProfileSyncableService::OverwriteProfileWithServerData(
635 autofill, &p, "en-US");
636 profiles->push_back(p);
637 child_id = child_node.GetSuccessorId();
639 return true;
642 void SetIdleChangeProcessorExpectations() {
643 EXPECT_CALL(autofill_table_, RemoveFormElement(_, _)).Times(0);
644 EXPECT_CALL(autofill_table_, GetAutofillTimestamps(_, _, _, _)).Times(0);
646 // Only permit UpdateAutofillEntries() to be called with an empty list.
647 std::vector<AutofillEntry> empty;
648 EXPECT_CALL(autofill_table_, UpdateAutofillEntries(Not(empty))).Times(0);
651 static AutofillEntry MakeAutofillEntry(const char* name,
652 const char* value,
653 int time_shift0,
654 int time_shift1) {
655 // Time deep in the past would cause Autocomplete sync to discard the
656 // entries.
657 static Time base_time = Time::Now().LocalMidnight();
659 base::Time date_created = base_time + TimeDelta::FromSeconds(time_shift0);
660 base::Time date_last_used = date_created;
661 if (time_shift1 >= 0)
662 date_last_used = base_time + TimeDelta::FromSeconds(time_shift1);
663 return AutofillEntry(
664 AutofillKey(base::ASCIIToUTF16(name), base::ASCIIToUTF16(value)),
665 date_created, date_last_used);
668 static AutofillEntry MakeAutofillEntry(const char* name,
669 const char* value,
670 int time_shift) {
671 return MakeAutofillEntry(name, value, time_shift, -1);
674 DataTypeController* CreateDataTypeController(syncer::ModelType type) {
675 DCHECK(type == AUTOFILL || type == AUTOFILL_PROFILE);
676 if (type == AUTOFILL)
677 return new AutofillDataTypeController(sync_client_.get());
678 else
679 return new AutofillProfileDataTypeController(sync_client_.get());
682 friend class AddAutofillHelper<AutofillEntry>;
683 friend class AddAutofillHelper<AutofillProfile>;
684 friend class FakeServerUpdater;
686 TestingProfileManager profile_manager_;
687 TestingProfile* profile_;
688 AutofillTableMock autofill_table_;
689 scoped_ptr<WebDatabaseFake> web_database_;
690 scoped_refptr<WebDataServiceFake> web_data_service_;
691 MockPersonalDataManager* personal_data_manager_;
692 syncer::DataTypeAssociationStats association_stats_;
693 base::WeakPtrFactory<DataTypeDebugInfoListener> debug_ptr_factory_;
694 scoped_ptr<TestSyncClient> sync_client_;
697 template <class T>
698 class AddAutofillHelper {
699 public:
700 AddAutofillHelper(ProfileSyncServiceAutofillTest* test,
701 const std::vector<T>& entries)
702 : callback_(base::Bind(&AddAutofillHelper::AddAutofillCallback,
703 base::Unretained(this), test, entries)),
704 success_(false) {
707 const base::Closure& callback() const { return callback_; }
708 bool success() { return success_; }
710 private:
711 void AddAutofillCallback(ProfileSyncServiceAutofillTest* test,
712 const std::vector<T>& entries) {
713 if (!test->CreateRoot(GetModelType<T>()))
714 return;
716 for (size_t i = 0; i < entries.size(); ++i) {
717 if (!test->AddAutofillSyncNode(entries[i]))
718 return;
720 success_ = true;
723 base::Closure callback_;
724 bool success_;
727 // Overload write transaction to use custom NotifyTransactionComplete
728 class WriteTransactionTest: public WriteTransaction {
729 public:
730 WriteTransactionTest(const tracked_objects::Location& from_here,
731 WriterTag writer,
732 syncer::syncable::Directory* directory,
733 scoped_ptr<WaitableEvent>* wait_for_syncapi)
734 : WriteTransaction(from_here, writer, directory),
735 wait_for_syncapi_(wait_for_syncapi) { }
737 void NotifyTransactionComplete(syncer::ModelTypeSet types) override {
738 // This is where we differ. Force a thread change here, giving another
739 // thread a chance to create a WriteTransaction
740 (*wait_for_syncapi_)->Wait();
742 WriteTransaction::NotifyTransactionComplete(types);
745 private:
746 scoped_ptr<WaitableEvent>* wait_for_syncapi_;
749 // Our fake server updater. Needs the RefCountedThreadSafe inheritance so we can
750 // post tasks with it.
751 class FakeServerUpdater : public base::RefCountedThreadSafe<FakeServerUpdater> {
752 public:
753 FakeServerUpdater(TestProfileSyncService* service,
754 scoped_ptr<WaitableEvent>* wait_for_start,
755 scoped_ptr<WaitableEvent>* wait_for_syncapi)
756 : entry_(ProfileSyncServiceAutofillTest::MakeAutofillEntry("0", "0", 0)),
757 service_(service),
758 wait_for_start_(wait_for_start),
759 wait_for_syncapi_(wait_for_syncapi),
760 is_finished_(false, false) { }
762 void Update() {
763 // This gets called in a modelsafeworker thread.
764 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB));
766 syncer::UserShare* user_share = service_->GetUserShare();
767 syncer::syncable::Directory* directory = user_share->directory.get();
769 // Create autofill protobuf.
770 std::string tag = AutocompleteSyncableService::KeyToTag(
771 base::UTF16ToUTF8(entry_.key().name()),
772 base::UTF16ToUTF8(entry_.key().value()));
773 sync_pb::AutofillSpecifics new_autofill;
774 new_autofill.set_name(base::UTF16ToUTF8(entry_.key().name()));
775 new_autofill.set_value(base::UTF16ToUTF8(entry_.key().value()));
776 new_autofill.add_usage_timestamp(entry_.date_created().ToInternalValue());
777 if (entry_.date_created() != entry_.date_last_used()) {
778 new_autofill.add_usage_timestamp(
779 entry_.date_last_used().ToInternalValue());
782 sync_pb::EntitySpecifics entity_specifics;
783 entity_specifics.mutable_autofill()->CopyFrom(new_autofill);
786 // Tell main thread we've started
787 (*wait_for_start_)->Signal();
789 // Create write transaction.
790 WriteTransactionTest trans(FROM_HERE, UNITTEST, directory,
791 wait_for_syncapi_);
793 // Create actual entry based on autofill protobuf information.
794 // Simulates effects of UpdateLocalDataFromServerData
795 MutableEntry parent(&trans, GET_TYPE_ROOT, syncer::AUTOFILL);
796 MutableEntry item(&trans, CREATE, syncer::AUTOFILL, parent.GetId(), tag);
797 ASSERT_TRUE(item.good());
798 item.PutSpecifics(entity_specifics);
799 item.PutServerSpecifics(entity_specifics);
800 item.PutBaseVersion(1);
801 syncer::syncable::Id server_item_id =
802 service_->id_factory()->NewServerId();
803 item.PutId(server_item_id);
804 syncer::syncable::Id new_predecessor;
805 ASSERT_TRUE(item.PutPredecessor(new_predecessor));
807 DVLOG(1) << "FakeServerUpdater finishing.";
808 is_finished_.Signal();
811 void CreateNewEntry(const AutofillEntry& entry) {
812 entry_ = entry;
813 ASSERT_FALSE(BrowserThread::CurrentlyOn(BrowserThread::DB));
814 if (!BrowserThread::PostTask(
815 BrowserThread::DB, FROM_HERE,
816 base::Bind(&FakeServerUpdater::Update, this))) {
817 NOTREACHED() << "Failed to post task to the db thread.";
818 return;
822 void CreateNewEntryAndWait(const AutofillEntry& entry) {
823 entry_ = entry;
824 ASSERT_FALSE(BrowserThread::CurrentlyOn(BrowserThread::DB));
825 is_finished_.Reset();
826 if (!BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
827 base::Bind(&FakeServerUpdater::Update, this))) {
828 NOTREACHED() << "Failed to post task to the db thread.";
829 return;
831 is_finished_.Wait();
834 private:
835 friend class base::RefCountedThreadSafe<FakeServerUpdater>;
836 ~FakeServerUpdater() { }
838 AutofillEntry entry_;
839 TestProfileSyncService* service_;
840 scoped_ptr<WaitableEvent>* wait_for_start_;
841 scoped_ptr<WaitableEvent>* wait_for_syncapi_;
842 WaitableEvent is_finished_;
843 syncer::syncable::Id parent_id_;
846 // TODO(skrul): Test abort startup.
847 // TODO(skrul): Test processing of cloud changes.
848 // TODO(tim): Add autofill data type controller test, and a case to cover
849 // waiting for the PersonalDataManager.
850 TEST_F(ProfileSyncServiceAutofillTest, FailModelAssociation) {
851 // Don't create the root autofill node so startup fails.
852 StartSyncService(base::Closure(), true, syncer::AUTOFILL);
853 EXPECT_TRUE(sync_service_->HasUnrecoverableError());
856 TEST_F(ProfileSyncServiceAutofillTest, EmptyNativeEmptySync) {
857 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).WillOnce(Return(true));
858 SetIdleChangeProcessorExpectations();
859 CreateRootHelper create_root(this, syncer::AUTOFILL);
860 EXPECT_CALL(*personal_data_manager_, Refresh());
861 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
862 EXPECT_TRUE(create_root.success());
863 std::vector<AutofillEntry> sync_entries;
864 std::vector<AutofillProfile> sync_profiles;
865 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles));
866 EXPECT_EQ(0U, sync_entries.size());
867 EXPECT_EQ(0U, sync_profiles.size());
870 TEST_F(ProfileSyncServiceAutofillTest, HasNativeEntriesEmptySync) {
871 std::vector<AutofillEntry> entries;
872 entries.push_back(MakeAutofillEntry("foo", "bar", 1));
873 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
874 WillOnce(DoAll(SetArgumentPointee<0>(entries), Return(true)));
875 SetIdleChangeProcessorExpectations();
876 CreateRootHelper create_root(this, syncer::AUTOFILL);
877 EXPECT_CALL(*personal_data_manager_, Refresh());
878 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
879 ASSERT_TRUE(create_root.success());
880 std::vector<AutofillEntry> sync_entries;
881 std::vector<AutofillProfile> sync_profiles;
882 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles));
883 ASSERT_EQ(1U, entries.size());
884 EXPECT_TRUE(entries[0] == sync_entries[0]);
885 EXPECT_EQ(0U, sync_profiles.size());
888 TEST_F(ProfileSyncServiceAutofillTest, HasProfileEmptySync) {
889 std::vector<AutofillProfile*> profiles;
890 std::vector<AutofillProfile> expected_profiles;
891 // Owned by GetAutofillProfiles caller.
892 AutofillProfile* profile0 = new AutofillProfile;
893 autofill::test::SetProfileInfoWithGuid(profile0,
894 "54B3F9AA-335E-4F71-A27D-719C41564230", "Billing",
895 "Mitchell", "Morrison",
896 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
897 "91601", "US", "12345678910");
898 profiles.push_back(profile0);
899 expected_profiles.push_back(*profile0);
900 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).
901 WillOnce(DoAll(SetArgumentPointee<0>(profiles), Return(true)));
902 EXPECT_CALL(*personal_data_manager_, Refresh());
903 SetIdleChangeProcessorExpectations();
904 CreateRootHelper create_root(this, syncer::AUTOFILL_PROFILE);
905 StartSyncService(create_root.callback(), false, syncer::AUTOFILL_PROFILE);
906 ASSERT_TRUE(create_root.success());
907 std::vector<AutofillProfile> sync_profiles;
908 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(&sync_profiles));
909 EXPECT_EQ(1U, sync_profiles.size());
910 EXPECT_EQ(0, expected_profiles[0].Compare(sync_profiles[0]));
913 TEST_F(ProfileSyncServiceAutofillTest, HasNativeWithDuplicatesEmptySync) {
914 // There is buggy autofill code that allows duplicate name/value
915 // pairs to exist in the database with separate pair_ids.
916 std::vector<AutofillEntry> entries;
917 entries.push_back(MakeAutofillEntry("foo", "bar", 1));
918 entries.push_back(MakeAutofillEntry("dup", "", 2));
919 entries.push_back(MakeAutofillEntry("dup", "", 3));
920 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
921 WillOnce(DoAll(SetArgumentPointee<0>(entries), Return(true)));
922 SetIdleChangeProcessorExpectations();
923 CreateRootHelper create_root(this, syncer::AUTOFILL);
924 EXPECT_CALL(*personal_data_manager_, Refresh());
925 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
926 ASSERT_TRUE(create_root.success());
927 std::vector<AutofillEntry> sync_entries;
928 std::vector<AutofillProfile> sync_profiles;
929 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles));
930 EXPECT_EQ(2U, sync_entries.size());
933 TEST_F(ProfileSyncServiceAutofillTest, HasNativeHasSyncNoMerge) {
934 AutofillEntry native_entry(MakeAutofillEntry("native", "entry", 1));
935 AutofillEntry sync_entry(MakeAutofillEntry("sync", "entry", 2));
937 std::vector<AutofillEntry> native_entries;
938 native_entries.push_back(native_entry);
940 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
941 WillOnce(DoAll(SetArgumentPointee<0>(native_entries), Return(true)));
943 std::vector<AutofillEntry> sync_entries;
944 sync_entries.push_back(sync_entry);
946 AddAutofillHelper<AutofillEntry> add_autofill(this, sync_entries);
948 EXPECT_CALL(autofill_table_, UpdateAutofillEntries(ElementsAre(sync_entry))).
949 WillOnce(Return(true));
951 EXPECT_CALL(*personal_data_manager_, Refresh());
952 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL);
953 ASSERT_TRUE(add_autofill.success());
955 std::set<AutofillEntry> expected_entries;
956 expected_entries.insert(native_entry);
957 expected_entries.insert(sync_entry);
959 std::vector<AutofillEntry> new_sync_entries;
960 std::vector<AutofillProfile> new_sync_profiles;
961 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries,
962 &new_sync_profiles));
963 std::set<AutofillEntry> new_sync_entries_set(new_sync_entries.begin(),
964 new_sync_entries.end());
966 EXPECT_TRUE(expected_entries == new_sync_entries_set);
969 TEST_F(ProfileSyncServiceAutofillTest, HasNativeHasSyncMergeEntry) {
970 AutofillEntry native_entry(MakeAutofillEntry("merge", "entry", 1));
971 AutofillEntry sync_entry(MakeAutofillEntry("merge", "entry", 2));
972 AutofillEntry merged_entry(MakeAutofillEntry("merge", "entry", 1, 2));
974 std::vector<AutofillEntry> native_entries;
975 native_entries.push_back(native_entry);
976 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
977 WillOnce(DoAll(SetArgumentPointee<0>(native_entries), Return(true)));
979 std::vector<AutofillEntry> sync_entries;
980 sync_entries.push_back(sync_entry);
981 AddAutofillHelper<AutofillEntry> add_autofill(this, sync_entries);
983 EXPECT_CALL(autofill_table_,
984 UpdateAutofillEntries(ElementsAre(merged_entry))).WillOnce(Return(true));
985 EXPECT_CALL(*personal_data_manager_, Refresh());
986 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL);
987 ASSERT_TRUE(add_autofill.success());
989 std::vector<AutofillEntry> new_sync_entries;
990 std::vector<AutofillProfile> new_sync_profiles;
991 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries,
992 &new_sync_profiles));
993 ASSERT_EQ(1U, new_sync_entries.size());
994 EXPECT_TRUE(merged_entry == new_sync_entries[0]);
997 TEST_F(ProfileSyncServiceAutofillTest, HasNativeHasSyncMergeProfile) {
998 AutofillProfile sync_profile;
999 autofill::test::SetProfileInfoWithGuid(&sync_profile,
1000 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1001 "Mitchell", "Morrison",
1002 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1003 "91601", "US", "12345678910");
1005 AutofillProfile* native_profile = new AutofillProfile;
1006 autofill::test::SetProfileInfoWithGuid(native_profile,
1007 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing", "Alicia", "Saenz",
1008 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1009 "32801", "US", "19482937549");
1011 std::vector<AutofillProfile*> native_profiles;
1012 native_profiles.push_back(native_profile);
1013 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).
1014 WillOnce(DoAll(SetArgumentPointee<0>(native_profiles), Return(true)));
1016 std::vector<AutofillProfile> sync_profiles;
1017 sync_profiles.push_back(sync_profile);
1018 AddAutofillHelper<AutofillProfile> add_autofill(this, sync_profiles);
1020 EXPECT_CALL(autofill_table_,
1021 UpdateAutofillProfile(MatchProfiles(sync_profile))).
1022 WillOnce(Return(true));
1023 EXPECT_CALL(*personal_data_manager_, Refresh());
1024 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL_PROFILE);
1025 ASSERT_TRUE(add_autofill.success());
1027 std::vector<AutofillProfile> new_sync_profiles;
1028 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1029 &new_sync_profiles));
1030 ASSERT_EQ(1U, new_sync_profiles.size());
1031 EXPECT_EQ(0, sync_profile.Compare(new_sync_profiles[0]));
1034 TEST_F(ProfileSyncServiceAutofillTest, HasNativeHasSyncMergeProfileCombine) {
1035 AutofillProfile sync_profile;
1036 autofill::test::SetProfileInfoWithGuid(&sync_profile,
1037 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1038 "Mitchell", "Morrison",
1039 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1040 "91601", "US", "12345678910");
1042 AutofillProfile* native_profile = new AutofillProfile;
1043 // Same address, but different names, phones and e-mails.
1044 autofill::test::SetProfileInfoWithGuid(native_profile,
1045 "23355099-1170-4B71-8ED4-144470CC9EBF", "Billing", "Alicia", "Saenz",
1046 "joewayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1047 "91601", "US", "19482937549");
1049 AutofillProfile expected_profile(sync_profile);
1050 expected_profile.OverwriteWith(*native_profile, "en-US");
1052 std::vector<AutofillProfile*> native_profiles;
1053 native_profiles.push_back(native_profile);
1054 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).
1055 WillOnce(DoAll(SetArgumentPointee<0>(native_profiles), Return(true)));
1056 EXPECT_CALL(autofill_table_,
1057 AddAutofillProfile(MatchProfiles(expected_profile))).
1058 WillOnce(Return(true));
1059 EXPECT_CALL(autofill_table_,
1060 RemoveAutofillProfile("23355099-1170-4B71-8ED4-144470CC9EBF")).
1061 WillOnce(Return(true));
1062 std::vector<AutofillProfile> sync_profiles;
1063 sync_profiles.push_back(sync_profile);
1064 AddAutofillHelper<AutofillProfile> add_autofill(this, sync_profiles);
1066 EXPECT_CALL(*personal_data_manager_, Refresh());
1067 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL_PROFILE);
1068 ASSERT_TRUE(add_autofill.success());
1070 std::vector<AutofillProfile> new_sync_profiles;
1071 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1072 &new_sync_profiles));
1073 ASSERT_EQ(1U, new_sync_profiles.size());
1074 // Check that key fields are the same.
1075 EXPECT_TRUE(new_sync_profiles[0].IsSubsetOf(sync_profile, "en-US"));
1078 TEST_F(ProfileSyncServiceAutofillTest, MergeProfileWithDifferentGuid) {
1079 AutofillProfile sync_profile;
1081 autofill::test::SetProfileInfoWithGuid(&sync_profile,
1082 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1083 "Mitchell", "Morrison",
1084 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1085 "91601", "US", "12345678910");
1087 std::string native_guid = "EDC609ED-7EEE-4F27-B00C-423242A9C44B";
1088 AutofillProfile* native_profile = new AutofillProfile;
1089 autofill::test::SetProfileInfoWithGuid(native_profile,
1090 native_guid.c_str(), "Billing",
1091 "Mitchell", "Morrison",
1092 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1093 "91601", "US", "12345678910");
1095 std::vector<AutofillProfile*> native_profiles;
1096 native_profiles.push_back(native_profile);
1097 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).
1098 WillOnce(DoAll(SetArgumentPointee<0>(native_profiles), Return(true)));
1100 std::vector<AutofillProfile> sync_profiles;
1101 sync_profiles.push_back(sync_profile);
1102 AddAutofillHelper<AutofillProfile> add_autofill(this, sync_profiles);
1104 EXPECT_CALL(autofill_table_, AddAutofillProfile(_)).
1105 WillOnce(Return(true));
1106 EXPECT_CALL(autofill_table_, RemoveAutofillProfile(native_guid)).
1107 WillOnce(Return(true));
1108 EXPECT_CALL(*personal_data_manager_, Refresh());
1109 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL_PROFILE);
1110 ASSERT_TRUE(add_autofill.success());
1112 std::vector<AutofillProfile> new_sync_profiles;
1113 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1114 &new_sync_profiles));
1115 ASSERT_EQ(1U, new_sync_profiles.size());
1116 EXPECT_EQ(0, sync_profile.Compare(new_sync_profiles[0]));
1117 EXPECT_EQ(sync_profile.guid(), new_sync_profiles[0].guid());
1120 TEST_F(ProfileSyncServiceAutofillTest, ProcessUserChangeAddEntry) {
1121 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).WillOnce(Return(true));
1122 EXPECT_CALL(*personal_data_manager_, Refresh());
1123 SetIdleChangeProcessorExpectations();
1124 CreateRootHelper create_root(this, syncer::AUTOFILL);
1125 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
1126 ASSERT_TRUE(create_root.success());
1128 AutofillEntry added_entry(MakeAutofillEntry("added", "entry", 1));
1130 EXPECT_CALL(autofill_table_, GetAutofillTimestamps(_, _, _, _)).
1131 WillOnce(DoAll(SetArgumentPointee<2>(added_entry.date_created()),
1132 SetArgumentPointee<3>(added_entry.date_last_used()),
1133 Return(true)));
1135 AutofillChangeList changes;
1136 changes.push_back(AutofillChange(AutofillChange::ADD, added_entry.key()));
1138 web_data_service_->OnAutofillEntriesChanged(changes);
1140 std::vector<AutofillEntry> new_sync_entries;
1141 std::vector<AutofillProfile> new_sync_profiles;
1142 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries,
1143 &new_sync_profiles));
1144 ASSERT_EQ(1U, new_sync_entries.size());
1145 EXPECT_TRUE(added_entry == new_sync_entries[0]);
1148 TEST_F(ProfileSyncServiceAutofillTest, ProcessUserChangeAddProfile) {
1149 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).WillOnce(Return(true));
1150 EXPECT_CALL(*personal_data_manager_, Refresh());
1151 SetIdleChangeProcessorExpectations();
1152 CreateRootHelper create_root(this, syncer::AUTOFILL_PROFILE);
1153 StartSyncService(create_root.callback(), false, syncer::AUTOFILL_PROFILE);
1154 ASSERT_TRUE(create_root.success());
1156 AutofillProfile added_profile;
1157 autofill::test::SetProfileInfoWithGuid(&added_profile,
1158 "D6ADA912-D374-4C0A-917D-F5C8EBE43011", "Josephine", "Alicia", "Saenz",
1159 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1160 "32801", "US", "19482937549");
1162 AutofillProfileChange change(
1163 AutofillProfileChange::ADD, added_profile.guid(), &added_profile);
1164 web_data_service_->OnAutofillProfileChanged(change);
1166 std::vector<AutofillProfile> new_sync_profiles;
1167 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1168 &new_sync_profiles));
1169 ASSERT_EQ(1U, new_sync_profiles.size());
1170 EXPECT_EQ(0, added_profile.Compare(new_sync_profiles[0]));
1173 TEST_F(ProfileSyncServiceAutofillTest, ProcessUserChangeUpdateEntry) {
1174 AutofillEntry original_entry(MakeAutofillEntry("my", "entry", 1));
1175 std::vector<AutofillEntry> original_entries;
1176 original_entries.push_back(original_entry);
1178 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
1179 WillOnce(DoAll(SetArgumentPointee<0>(original_entries), Return(true)));
1180 EXPECT_CALL(*personal_data_manager_, Refresh());
1181 CreateRootHelper create_root(this, syncer::AUTOFILL);
1182 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
1183 ASSERT_TRUE(create_root.success());
1185 AutofillEntry updated_entry(MakeAutofillEntry("my", "entry", 1, 2));
1187 EXPECT_CALL(autofill_table_, GetAutofillTimestamps(_, _, _, _)).
1188 WillOnce(DoAll(SetArgumentPointee<2>(updated_entry.date_created()),
1189 SetArgumentPointee<3>(updated_entry.date_last_used()),
1190 Return(true)));
1192 AutofillChangeList changes;
1193 changes.push_back(AutofillChange(AutofillChange::UPDATE,
1194 updated_entry.key()));
1195 web_data_service_->OnAutofillEntriesChanged(changes);
1197 std::vector<AutofillEntry> new_sync_entries;
1198 std::vector<AutofillProfile> new_sync_profiles;
1199 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries,
1200 &new_sync_profiles));
1201 ASSERT_EQ(1U, new_sync_entries.size());
1202 EXPECT_TRUE(updated_entry == new_sync_entries[0]);
1206 TEST_F(ProfileSyncServiceAutofillTest, ProcessUserChangeRemoveEntry) {
1207 AutofillEntry original_entry(MakeAutofillEntry("my", "entry", 1));
1208 std::vector<AutofillEntry> original_entries;
1209 original_entries.push_back(original_entry);
1211 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
1212 WillOnce(DoAll(SetArgumentPointee<0>(original_entries), Return(true)));
1213 EXPECT_CALL(*personal_data_manager_, Refresh());
1214 CreateRootHelper create_root(this, syncer::AUTOFILL);
1215 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
1216 ASSERT_TRUE(create_root.success());
1218 AutofillChangeList changes;
1219 changes.push_back(AutofillChange(AutofillChange::REMOVE,
1220 original_entry.key()));
1221 web_data_service_->OnAutofillEntriesChanged(changes);
1223 std::vector<AutofillEntry> new_sync_entries;
1224 std::vector<AutofillProfile> new_sync_profiles;
1225 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries,
1226 &new_sync_profiles));
1227 ASSERT_EQ(0U, new_sync_entries.size());
1230 TEST_F(ProfileSyncServiceAutofillTest, ProcessUserChangeRemoveProfile) {
1231 AutofillProfile sync_profile;
1232 autofill::test::SetProfileInfoWithGuid(&sync_profile,
1233 "3BA5FA1B-1EC4-4BB3-9B57-EC92BE3C1A09", "Josephine", "Alicia", "Saenz",
1234 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1235 "32801", "US", "19482937549");
1236 AutofillProfile* native_profile = new AutofillProfile;
1237 autofill::test::SetProfileInfoWithGuid(native_profile,
1238 "3BA5FA1B-1EC4-4BB3-9B57-EC92BE3C1A09", "Josephine", "Alicia", "Saenz",
1239 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1240 "32801", "US", "19482937549");
1242 std::vector<AutofillProfile*> native_profiles;
1243 native_profiles.push_back(native_profile);
1244 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).
1245 WillOnce(DoAll(SetArgumentPointee<0>(native_profiles), Return(true)));
1247 std::vector<AutofillProfile> sync_profiles;
1248 sync_profiles.push_back(sync_profile);
1249 AddAutofillHelper<AutofillProfile> add_autofill(this, sync_profiles);
1250 EXPECT_CALL(*personal_data_manager_, Refresh());
1251 StartSyncService(add_autofill.callback(), false, syncer::AUTOFILL_PROFILE);
1252 ASSERT_TRUE(add_autofill.success());
1254 AutofillProfileChange change(
1255 AutofillProfileChange::REMOVE, sync_profile.guid(), NULL);
1256 web_data_service_->OnAutofillProfileChanged(change);
1258 std::vector<AutofillProfile> new_sync_profiles;
1259 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1260 &new_sync_profiles));
1261 ASSERT_EQ(0U, new_sync_profiles.size());
1264 // http://crbug.com/57884
1265 TEST_F(ProfileSyncServiceAutofillTest, DISABLED_ServerChangeRace) {
1266 // Once for MergeDataAndStartSyncing() and twice for ProcessSyncChanges(), via
1267 // LoadAutofillData().
1268 EXPECT_CALL(autofill_table_, GetAllAutofillEntries(_)).
1269 Times(3).WillRepeatedly(Return(true));
1270 // On the other hand Autofill and Autocomplete are separated now, so
1271 // GetAutofillProfiles() should not be called.
1272 EXPECT_CALL(autofill_table_, GetAutofillProfiles(_)).Times(0);
1273 EXPECT_CALL(autofill_table_, UpdateAutofillEntries(_)).
1274 WillRepeatedly(Return(true));
1275 EXPECT_CALL(*personal_data_manager_, Refresh()).Times(3);
1276 CreateRootHelper create_root(this, syncer::AUTOFILL);
1277 StartSyncService(create_root.callback(), false, syncer::AUTOFILL);
1278 ASSERT_TRUE(create_root.success());
1280 // (true, false) means we have to reset after |Signal|, init to unsignaled.
1281 scoped_ptr<WaitableEvent> wait_for_start(new WaitableEvent(true, false));
1282 scoped_ptr<WaitableEvent> wait_for_syncapi(new WaitableEvent(true, false));
1283 scoped_refptr<FakeServerUpdater> updater(new FakeServerUpdater(
1284 sync_service_, &wait_for_start, &wait_for_syncapi));
1286 // This server side update will stall waiting for CommitWaiter.
1287 updater->CreateNewEntry(MakeAutofillEntry("server", "entry", 1));
1288 wait_for_start->Wait();
1290 AutofillEntry syncapi_entry(MakeAutofillEntry("syncapi", "entry", 2));
1291 ASSERT_TRUE(AddAutofillSyncNode(syncapi_entry));
1292 DVLOG(1) << "Syncapi update finished.";
1294 // If we reach here, it means syncapi succeeded and we didn't deadlock. Yay!
1295 // Signal FakeServerUpdater that it can complete.
1296 wait_for_syncapi->Signal();
1298 // Make another entry to ensure nothing broke afterwards and wait for finish
1299 // to clean up.
1300 updater->CreateNewEntryAndWait(MakeAutofillEntry("server2", "entry2", 3));
1302 std::vector<AutofillEntry> sync_entries;
1303 std::vector<AutofillProfile> sync_profiles;
1304 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles));
1305 EXPECT_EQ(3U, sync_entries.size());
1306 EXPECT_EQ(0U, sync_profiles.size());
1307 for (size_t i = 0; i < sync_entries.size(); i++) {
1308 DVLOG(1) << "Entry " << i << ": " << sync_entries[i].key().name()
1309 << ", " << sync_entries[i].key().value();