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.
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/prefs/pref_service_syncable.h"
26 #include "chrome/browser/signin/account_tracker_service_factory.h"
27 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
28 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
29 #include "chrome/browser/signin/signin_manager_factory.h"
30 #include "chrome/browser/sync/abstract_profile_sync_service_test.h"
31 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
32 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
33 #include "chrome/browser/sync/profile_sync_components_factory.h"
34 #include "chrome/browser/sync/profile_sync_service.h"
35 #include "chrome/browser/sync/profile_sync_service_factory.h"
36 #include "chrome/browser/sync/profile_sync_test_util.h"
37 #include "chrome/browser/sync/test_profile_sync_service.h"
38 #include "chrome/browser/web_data_service_factory.h"
39 #include "chrome/test/base/testing_browser_process.h"
40 #include "chrome/test/base/testing_profile.h"
41 #include "chrome/test/base/testing_profile_manager.h"
42 #include "components/autofill/core/browser/autofill_test_utils.h"
43 #include "components/autofill/core/browser/personal_data_manager.h"
44 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
45 #include "components/autofill/core/browser/webdata/autofill_change.h"
46 #include "components/autofill/core/browser/webdata/autofill_entry.h"
47 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
48 #include "components/autofill/core/browser/webdata/autofill_table.h"
49 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
50 #include "components/signin/core/browser/account_tracker_service.h"
51 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
52 #include "components/signin/core/browser/signin_manager.h"
53 #include "components/sync_driver/data_type_controller.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
;
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
;
102 using testing::DoAll
;
103 using testing::ElementsAre
;
105 using testing::SetArgumentPointee
;
106 using testing::Return
;
114 const char kTestProfileName
[] = "test-profile";
116 void RunAndSignal(const base::Closure
& cb
, WaitableEvent
* event
) {
123 class AutofillTableMock
: public AutofillTable
{
125 AutofillTableMock() : AutofillTable("en-US") {}
126 MOCK_METHOD2(RemoveFormElement
,
127 bool(const base::string16
& name
,
128 const base::string16
& value
)); // NOLINT
129 MOCK_METHOD1(GetAllAutofillEntries
,
130 bool(std::vector
<AutofillEntry
>* entries
)); // NOLINT
131 MOCK_METHOD4(GetAutofillTimestamps
,
132 bool(const base::string16
& name
, // NOLINT
133 const base::string16
& value
,
134 base::Time
* date_created
,
135 base::Time
* date_last_used
));
136 MOCK_METHOD1(UpdateAutofillEntries
,
137 bool(const std::vector
<AutofillEntry
>&)); // NOLINT
138 MOCK_METHOD1(GetAutofillProfiles
,
139 bool(std::vector
<AutofillProfile
*>*)); // NOLINT
140 MOCK_METHOD1(UpdateAutofillProfile
,
141 bool(const AutofillProfile
&)); // NOLINT
142 MOCK_METHOD1(AddAutofillProfile
,
143 bool(const AutofillProfile
&)); // NOLINT
144 MOCK_METHOD1(RemoveAutofillProfile
,
145 bool(const std::string
&)); // NOLINT
148 MATCHER_P(MatchProfiles
, profile
, "") {
149 return (profile
.Compare(arg
) == 0);
152 class WebDatabaseFake
: public WebDatabase
{
154 explicit WebDatabaseFake(AutofillTable
* autofill_table
) {
155 AddTable(autofill_table
);
159 class MockAutofillBackend
: public autofill::AutofillWebDataBackend
{
162 WebDatabase
* web_database
,
163 const base::Closure
& on_changed
)
164 : web_database_(web_database
),
165 on_changed_(on_changed
) {
168 ~MockAutofillBackend() override
{}
169 WebDatabase
* GetDatabase() override
{ return web_database_
; }
171 autofill::AutofillWebDataServiceObserverOnDBThread
* observer
) override
{}
173 autofill::AutofillWebDataServiceObserverOnDBThread
* observer
) override
{}
174 void RemoveExpiredFormElements() override
{}
175 void NotifyOfMultipleAutofillChanges() override
{
176 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
177 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, on_changed_
);
181 WebDatabase
* web_database_
;
182 base::Closure on_changed_
;
185 class ProfileSyncServiceAutofillTest
;
187 template<class AutofillProfile
>
188 syncer::ModelType
GetModelType() {
189 return syncer::UNSPECIFIED
;
193 syncer::ModelType GetModelType
<AutofillEntry
>() {
194 return syncer::AUTOFILL
;
198 syncer::ModelType GetModelType
<AutofillProfile
>() {
199 return syncer::AUTOFILL_PROFILE
;
202 class TokenWebDataServiceFake
: public TokenWebData
{
204 TokenWebDataServiceFake()
206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
207 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
)) {
210 bool IsDatabaseLoaded() override
{ return true; }
212 AutofillWebDataService::Handle
GetAllTokens(
213 WebDataServiceConsumer
* consumer
) override
{
214 // TODO(tim): It would be nice if WebDataService was injected on
215 // construction of ProfileOAuth2TokenService rather than fetched by
216 // Initialize so that this isn't necessary (we could pass a NULL service).
217 // We currently do return it via EXPECT_CALLs, but without depending on
218 // order-of-initialization (which seems way more fragile) we can't tell
219 // which component is asking at what time, and some components in these
220 // Autofill tests require a WebDataService.
225 ~TokenWebDataServiceFake() override
{}
227 DISALLOW_COPY_AND_ASSIGN(TokenWebDataServiceFake
);
230 class WebDataServiceFake
: public AutofillWebDataService
{
233 : AutofillWebDataService(
234 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
235 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
)),
237 autocomplete_syncable_service_(NULL
),
238 autofill_profile_syncable_service_(NULL
),
239 syncable_service_created_or_destroyed_(false, false) {
242 void SetDatabase(WebDatabase
* web_database
) {
243 web_database_
= web_database
;
246 void StartSyncableService() {
247 // The |autofill_profile_syncable_service_| must be constructed on the DB
249 const base::Closure
& on_changed_callback
= base::Bind(
250 &WebDataServiceFake::NotifyAutofillMultipleChangedOnUIThread
,
253 BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
,
254 base::Bind(&WebDataServiceFake::CreateSyncableService
,
255 base::Unretained(this),
256 on_changed_callback
));
257 syncable_service_created_or_destroyed_
.Wait();
260 void ShutdownSyncableService() {
261 // The |autofill_profile_syncable_service_| must be destructed on the DB
263 BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
,
264 base::Bind(&WebDataServiceFake::DestroySyncableService
,
265 base::Unretained(this)));
266 syncable_service_created_or_destroyed_
.Wait();
269 bool IsDatabaseLoaded() override
{ return true; }
271 WebDatabase
* GetDatabase() override
{ return web_database_
; }
273 void OnAutofillEntriesChanged(const AutofillChangeList
& changes
) {
274 WaitableEvent
event(true, false);
276 base::Closure notify_cb
=
277 base::Bind(&AutocompleteSyncableService::AutofillEntriesChanged
,
278 base::Unretained(autocomplete_syncable_service_
),
280 BrowserThread::PostTask(
283 base::Bind(&RunAndSignal
, notify_cb
, &event
));
287 void OnAutofillProfileChanged(const AutofillProfileChange
& changes
) {
288 WaitableEvent
event(true, false);
290 base::Closure notify_cb
=
291 base::Bind(&AutocompleteSyncableService::AutofillProfileChanged
,
292 base::Unretained(autofill_profile_syncable_service_
),
294 BrowserThread::PostTask(
297 base::Bind(&RunAndSignal
, notify_cb
, &event
));
302 ~WebDataServiceFake() override
{}
304 void CreateSyncableService(const base::Closure
& on_changed_callback
) {
305 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
306 // These services are deleted in DestroySyncableService().
307 backend_
.reset(new MockAutofillBackend(
308 GetDatabase(), on_changed_callback
));
309 AutocompleteSyncableService::CreateForWebDataServiceAndBackend(
310 this, backend_
.get());
311 AutofillProfileSyncableService::CreateForWebDataServiceAndBackend(
312 this, backend_
.get(), "en-US");
314 autocomplete_syncable_service_
=
315 AutocompleteSyncableService::FromWebDataService(this);
316 autofill_profile_syncable_service_
=
317 AutofillProfileSyncableService::FromWebDataService(this);
319 syncable_service_created_or_destroyed_
.Signal();
322 void DestroySyncableService() {
323 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
324 autocomplete_syncable_service_
= NULL
;
325 autofill_profile_syncable_service_
= NULL
;
327 syncable_service_created_or_destroyed_
.Signal();
330 WebDatabase
* web_database_
;
331 AutocompleteSyncableService
* autocomplete_syncable_service_
;
332 AutofillProfileSyncableService
* autofill_profile_syncable_service_
;
333 scoped_ptr
<autofill::AutofillWebDataBackend
> backend_
;
335 WaitableEvent syncable_service_created_or_destroyed_
;
337 DISALLOW_COPY_AND_ASSIGN(WebDataServiceFake
);
340 scoped_ptr
<KeyedService
> BuildMockWebDataServiceWrapper(
341 content::BrowserContext
* profile
) {
342 return make_scoped_ptr(new MockWebDataServiceWrapper(
343 new WebDataServiceFake(), new TokenWebDataServiceFake()));
346 ACTION_P(MakeAutocompleteSyncComponents
, wds
) {
347 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
348 if (!BrowserThread::CurrentlyOn(BrowserThread::DB
))
349 return base::WeakPtr
<syncer::SyncableService
>();
350 return AutocompleteSyncableService::FromWebDataService(wds
)->AsWeakPtr();
353 ACTION_P(ReturnNewDataTypeManagerWithDebugListener
, debug_listener
) {
354 return new sync_driver::DataTypeManagerImpl(
363 ACTION_P(MakeAutofillProfileSyncComponents
, wds
) {
364 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
365 if (!BrowserThread::CurrentlyOn(BrowserThread::DB
))
366 return base::WeakPtr
<syncer::SyncableService
>();
367 return AutofillProfileSyncableService::FromWebDataService(wds
)->AsWeakPtr();
370 class AbstractAutofillFactory
{
372 virtual DataTypeController
* CreateDataTypeController(
373 ProfileSyncComponentsFactory
* factory
,
374 TestingProfile
* profile
,
375 ProfileSyncService
* service
) = 0;
376 virtual void SetExpectation(ProfileSyncComponentsFactoryMock
* factory
,
377 ProfileSyncService
* service
,
378 AutofillWebDataService
* wds
,
379 DataTypeController
* dtc
) = 0;
380 virtual ~AbstractAutofillFactory() {}
383 class AutofillEntryFactory
: public AbstractAutofillFactory
{
385 DataTypeController
* CreateDataTypeController(
386 ProfileSyncComponentsFactory
* factory
,
387 TestingProfile
* profile
,
388 ProfileSyncService
* service
) override
{
389 return new AutofillDataTypeController(factory
, profile
);
392 void SetExpectation(ProfileSyncComponentsFactoryMock
* factory
,
393 ProfileSyncService
* service
,
394 AutofillWebDataService
* wds
,
395 DataTypeController
* dtc
) override
{
396 EXPECT_CALL(*factory
, GetSyncableServiceForType(syncer::AUTOFILL
)).
397 WillOnce(MakeAutocompleteSyncComponents(wds
));
401 class AutofillProfileFactory
: public AbstractAutofillFactory
{
403 DataTypeController
* CreateDataTypeController(
404 ProfileSyncComponentsFactory
* factory
,
405 TestingProfile
* profile
,
406 ProfileSyncService
* service
) override
{
407 return new AutofillProfileDataTypeController(factory
, profile
);
410 void SetExpectation(ProfileSyncComponentsFactoryMock
* factory
,
411 ProfileSyncService
* service
,
412 AutofillWebDataService
* wds
,
413 DataTypeController
* dtc
) override
{
414 EXPECT_CALL(*factory
,
415 GetSyncableServiceForType(syncer::AUTOFILL_PROFILE
)).
416 WillOnce(MakeAutofillProfileSyncComponents(wds
));
420 class MockPersonalDataManager
: public PersonalDataManager
{
422 MockPersonalDataManager() : PersonalDataManager("en-US") {}
423 MOCK_CONST_METHOD0(IsDataLoaded
, bool());
424 MOCK_METHOD0(LoadProfiles
, void());
425 MOCK_METHOD0(LoadCreditCards
, void());
426 MOCK_METHOD0(Refresh
, void());
428 static scoped_ptr
<KeyedService
> Build(content::BrowserContext
* profile
) {
429 return make_scoped_ptr(new MockPersonalDataManager());
433 template <class T
> class AddAutofillHelper
;
435 class ProfileSyncServiceAutofillTest
436 : public AbstractProfileSyncServiceTest
,
437 public syncer::DataTypeDebugInfoListener
{
439 // DataTypeDebugInfoListener implementation.
440 void OnDataTypeConfigureComplete(const std::vector
<
441 syncer::DataTypeConfigurationStats
>& configuration_stats
) override
{
442 ASSERT_EQ(1u, configuration_stats
.size());
443 association_stats_
= configuration_stats
[0].association_stats
;
447 ProfileSyncServiceAutofillTest()
448 : profile_manager_(TestingBrowserProcess::GetGlobal()),
449 debug_ptr_factory_(this) {
451 ~ProfileSyncServiceAutofillTest() override
{}
453 AutofillProfileFactory profile_factory_
;
454 AutofillEntryFactory entry_factory_
;
456 AbstractAutofillFactory
* GetFactory(syncer::ModelType type
) {
457 if (type
== syncer::AUTOFILL
) {
458 return &entry_factory_
;
459 } else if (type
== syncer::AUTOFILL_PROFILE
) {
460 return &profile_factory_
;
467 void SetUp() override
{
468 AbstractProfileSyncServiceTest::SetUp();
469 ASSERT_TRUE(profile_manager_
.SetUp());
470 TestingProfile::TestingFactories testing_factories
;
471 testing_factories
.push_back(std::make_pair(
472 ProfileOAuth2TokenServiceFactory::GetInstance(),
473 BuildAutoIssuingFakeProfileOAuth2TokenService
));
474 profile_
= profile_manager_
.CreateTestingProfile(
476 scoped_ptr
<PrefServiceSyncable
>(),
477 base::UTF8ToUTF16(kTestProfileName
),
481 web_database_
.reset(new WebDatabaseFake(&autofill_table_
));
482 MockWebDataServiceWrapper
* wrapper
=
483 static_cast<MockWebDataServiceWrapper
*>(
484 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse(
485 profile_
, BuildMockWebDataServiceWrapper
));
487 static_cast<WebDataServiceFake
*>(wrapper
->GetAutofillWebData().get());
488 web_data_service_
->SetDatabase(web_database_
.get());
490 personal_data_manager_
= static_cast<MockPersonalDataManager
*>(
491 autofill::PersonalDataManagerFactory::GetInstance()
492 ->SetTestingFactoryAndUse(profile_
,
493 MockPersonalDataManager::Build
));
495 EXPECT_CALL(*personal_data_manager_
, LoadProfiles()).Times(1);
496 EXPECT_CALL(*personal_data_manager_
, LoadCreditCards()).Times(1);
498 personal_data_manager_
->Init(
499 WebDataServiceFactory::GetAutofillWebDataForProfile(
500 profile_
, ServiceAccessType::EXPLICIT_ACCESS
),
501 profile_
->GetPrefs(),
502 AccountTrackerServiceFactory::GetForProfile(profile_
),
503 profile_
->IsOffTheRecord());
505 web_data_service_
->StartSyncableService();
507 // When UpdateAutofillEntries() is called with an empty list, the return
508 // value should be |true|, rather than the default of |false|.
509 std::vector
<AutofillEntry
> empty
;
510 EXPECT_CALL(autofill_table_
, UpdateAutofillEntries(empty
))
511 .WillRepeatedly(Return(true));
514 void TearDown() override
{
515 // Note: The tear down order is important.
516 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile_
, NULL
);
517 web_data_service_
->ShutdownOnUIThread();
518 web_data_service_
->ShutdownSyncableService();
519 web_data_service_
= NULL
;
520 // To prevent a leak, fully release TestURLRequestContext to ensure its
521 // destruction on the IO message loop.
523 profile_manager_
.DeleteTestingProfile(kTestProfileName
);
524 AbstractProfileSyncServiceTest::TearDown();
527 int GetSyncCount(syncer::ModelType type
) {
528 syncer::ReadTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
529 syncer::ReadNode
node(&trans
);
530 if (node
.InitTypeRoot(type
) != syncer::BaseNode::INIT_OK
)
532 return node
.GetTotalNodeCount() - 1;
535 void StartSyncService(const base::Closure
& callback
,
536 bool will_fail_association
,
537 syncer::ModelType type
) {
538 AbstractAutofillFactory
* factory
= GetFactory(type
);
539 SigninManagerBase
* signin
= SigninManagerFactory::GetForProfile(profile_
);
540 signin
->SetAuthenticatedAccountInfo("12345", "test_user@gmail.com");
541 sync_service_
= TestProfileSyncService::BuildAutoStartAsyncInit(profile_
,
544 ProfileSyncComponentsFactoryMock
* components
=
545 sync_service_
->components_factory_mock();
546 DataTypeController
* data_type_controller
=
547 factory
->CreateDataTypeController(components
, profile_
, sync_service_
);
548 factory
->SetExpectation(components
,
550 web_data_service_
.get(),
551 data_type_controller
);
553 EXPECT_CALL(*components
, CreateDataTypeManager(_
, _
, _
, _
, _
)).
554 WillOnce(ReturnNewDataTypeManagerWithDebugListener(
555 syncer::MakeWeakHandle(debug_ptr_factory_
.GetWeakPtr())));
557 EXPECT_CALL(*personal_data_manager_
, IsDataLoaded()).
558 WillRepeatedly(Return(true));
560 // We need tokens to get the tests going
561 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
)
562 ->UpdateCredentials(signin
->GetAuthenticatedAccountId(),
563 "oauth2_login_token");
565 sync_service_
->RegisterDataTypeController(data_type_controller
);
566 sync_service_
->Initialize();
567 base::MessageLoop::current()->Run();
569 // It's possible this test triggered an unrecoverable error, in which case
570 // we can't get the sync count.
571 if (sync_service_
->IsSyncActive()) {
572 EXPECT_EQ(GetSyncCount(type
),
573 association_stats_
.num_sync_items_after_association
);
575 EXPECT_EQ(association_stats_
.num_sync_items_after_association
,
576 association_stats_
.num_sync_items_before_association
+
577 association_stats_
.num_sync_items_added
-
578 association_stats_
.num_sync_items_deleted
);
581 bool AddAutofillSyncNode(const AutofillEntry
& entry
) {
582 syncer::WriteTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
583 syncer::WriteNode
node(&trans
);
584 std::string tag
= AutocompleteSyncableService::KeyToTag(
585 base::UTF16ToUTF8(entry
.key().name()),
586 base::UTF16ToUTF8(entry
.key().value()));
587 syncer::WriteNode::InitUniqueByCreationResult result
=
588 node
.InitUniqueByCreation(syncer::AUTOFILL
, tag
);
589 if (result
!= syncer::WriteNode::INIT_SUCCESS
)
592 sync_pb::EntitySpecifics specifics
;
593 AutocompleteSyncableService::WriteAutofillEntry(entry
, &specifics
);
594 node
.SetEntitySpecifics(specifics
);
598 bool AddAutofillSyncNode(const AutofillProfile
& profile
) {
599 syncer::WriteTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
600 syncer::WriteNode
node(&trans
);
601 std::string tag
= profile
.guid();
602 syncer::WriteNode::InitUniqueByCreationResult result
=
603 node
.InitUniqueByCreation(syncer::AUTOFILL_PROFILE
, tag
);
604 if (result
!= syncer::WriteNode::INIT_SUCCESS
)
607 sync_pb::EntitySpecifics specifics
;
608 AutofillProfileSyncableService::WriteAutofillProfile(profile
, &specifics
);
609 node
.SetEntitySpecifics(specifics
);
613 bool GetAutofillEntriesFromSyncDB(std::vector
<AutofillEntry
>* entries
,
614 std::vector
<AutofillProfile
>* profiles
) {
615 syncer::ReadTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
616 syncer::ReadNode
autofill_root(&trans
);
617 if (autofill_root
.InitTypeRoot(syncer::AUTOFILL
) != BaseNode::INIT_OK
) {
621 int64 child_id
= autofill_root
.GetFirstChildId();
622 while (child_id
!= syncer::kInvalidId
) {
623 syncer::ReadNode
child_node(&trans
);
624 if (child_node
.InitByIdLookup(child_id
) != BaseNode::INIT_OK
)
627 const sync_pb::AutofillSpecifics
& autofill(
628 child_node
.GetEntitySpecifics().autofill());
629 if (autofill
.has_value()) {
630 AutofillKey
key(base::UTF8ToUTF16(autofill
.name()),
631 base::UTF8ToUTF16(autofill
.value()));
632 std::vector
<base::Time
> timestamps
;
633 int timestamps_count
= autofill
.usage_timestamp_size();
634 for (int i
= 0; i
< timestamps_count
; ++i
) {
635 timestamps
.push_back(Time::FromInternalValue(
636 autofill
.usage_timestamp(i
)));
639 AutofillEntry(key
, timestamps
.front(), timestamps
.back()));
640 } else if (autofill
.has_profile()) {
642 p
.set_guid(autofill
.profile().guid());
643 AutofillProfileSyncableService::OverwriteProfileWithServerData(
644 autofill
.profile(), &p
, "en-US");
645 profiles
->push_back(p
);
647 child_id
= child_node
.GetSuccessorId();
652 bool GetAutofillProfilesFromSyncDBUnderProfileNode(
653 std::vector
<AutofillProfile
>* profiles
) {
654 syncer::ReadTransaction
trans(FROM_HERE
, sync_service_
->GetUserShare());
655 syncer::ReadNode
autofill_root(&trans
);
656 if (autofill_root
.InitTypeRoot(AUTOFILL_PROFILE
) != BaseNode::INIT_OK
) {
660 int64 child_id
= autofill_root
.GetFirstChildId();
661 while (child_id
!= syncer::kInvalidId
) {
662 syncer::ReadNode
child_node(&trans
);
663 if (child_node
.InitByIdLookup(child_id
) != BaseNode::INIT_OK
)
666 const sync_pb::AutofillProfileSpecifics
& autofill(
667 child_node
.GetEntitySpecifics().autofill_profile());
669 p
.set_guid(autofill
.guid());
670 AutofillProfileSyncableService::OverwriteProfileWithServerData(
671 autofill
, &p
, "en-US");
672 profiles
->push_back(p
);
673 child_id
= child_node
.GetSuccessorId();
678 void SetIdleChangeProcessorExpectations() {
679 EXPECT_CALL(autofill_table_
, RemoveFormElement(_
, _
)).Times(0);
680 EXPECT_CALL(autofill_table_
, GetAutofillTimestamps(_
, _
, _
, _
)).Times(0);
682 // Only permit UpdateAutofillEntries() to be called with an empty list.
683 std::vector
<AutofillEntry
> empty
;
684 EXPECT_CALL(autofill_table_
, UpdateAutofillEntries(Not(empty
))).Times(0);
687 static AutofillEntry
MakeAutofillEntry(const char* name
,
691 // Time deep in the past would cause Autocomplete sync to discard the
693 static Time base_time
= Time::Now().LocalMidnight();
695 base::Time date_created
= base_time
+ TimeDelta::FromSeconds(time_shift0
);
696 base::Time date_last_used
= date_created
;
697 if (time_shift1
>= 0)
698 date_last_used
= base_time
+ TimeDelta::FromSeconds(time_shift1
);
699 return AutofillEntry(
700 AutofillKey(base::ASCIIToUTF16(name
), base::ASCIIToUTF16(value
)),
701 date_created
, date_last_used
);
704 static AutofillEntry
MakeAutofillEntry(const char* name
,
707 return MakeAutofillEntry(name
, value
, time_shift
, -1);
710 friend class AddAutofillHelper
<AutofillEntry
>;
711 friend class AddAutofillHelper
<AutofillProfile
>;
712 friend class FakeServerUpdater
;
714 TestingProfileManager profile_manager_
;
715 TestingProfile
* profile_
;
716 AutofillTableMock autofill_table_
;
717 scoped_ptr
<WebDatabaseFake
> web_database_
;
718 scoped_refptr
<WebDataServiceFake
> web_data_service_
;
719 MockPersonalDataManager
* personal_data_manager_
;
720 syncer::DataTypeAssociationStats association_stats_
;
721 base::WeakPtrFactory
<DataTypeDebugInfoListener
> debug_ptr_factory_
;
725 class AddAutofillHelper
{
727 AddAutofillHelper(ProfileSyncServiceAutofillTest
* test
,
728 const std::vector
<T
>& entries
)
729 : callback_(base::Bind(&AddAutofillHelper::AddAutofillCallback
,
730 base::Unretained(this), test
, entries
)),
734 const base::Closure
& callback() const { return callback_
; }
735 bool success() { return success_
; }
738 void AddAutofillCallback(ProfileSyncServiceAutofillTest
* test
,
739 const std::vector
<T
>& entries
) {
740 if (!test
->CreateRoot(GetModelType
<T
>()))
743 for (size_t i
= 0; i
< entries
.size(); ++i
) {
744 if (!test
->AddAutofillSyncNode(entries
[i
]))
750 base::Closure callback_
;
754 // Overload write transaction to use custom NotifyTransactionComplete
755 class WriteTransactionTest
: public WriteTransaction
{
757 WriteTransactionTest(const tracked_objects::Location
& from_here
,
759 syncer::syncable::Directory
* directory
,
760 scoped_ptr
<WaitableEvent
>* wait_for_syncapi
)
761 : WriteTransaction(from_here
, writer
, directory
),
762 wait_for_syncapi_(wait_for_syncapi
) { }
764 void NotifyTransactionComplete(syncer::ModelTypeSet types
) override
{
765 // This is where we differ. Force a thread change here, giving another
766 // thread a chance to create a WriteTransaction
767 (*wait_for_syncapi_
)->Wait();
769 WriteTransaction::NotifyTransactionComplete(types
);
773 scoped_ptr
<WaitableEvent
>* wait_for_syncapi_
;
776 // Our fake server updater. Needs the RefCountedThreadSafe inheritance so we can
777 // post tasks with it.
778 class FakeServerUpdater
: public base::RefCountedThreadSafe
<FakeServerUpdater
> {
780 FakeServerUpdater(TestProfileSyncService
* service
,
781 scoped_ptr
<WaitableEvent
>* wait_for_start
,
782 scoped_ptr
<WaitableEvent
>* wait_for_syncapi
)
783 : entry_(ProfileSyncServiceAutofillTest::MakeAutofillEntry("0", "0", 0)),
785 wait_for_start_(wait_for_start
),
786 wait_for_syncapi_(wait_for_syncapi
),
787 is_finished_(false, false) { }
790 // This gets called in a modelsafeworker thread.
791 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
793 syncer::UserShare
* user_share
= service_
->GetUserShare();
794 syncer::syncable::Directory
* directory
= user_share
->directory
.get();
796 // Create autofill protobuf.
797 std::string tag
= AutocompleteSyncableService::KeyToTag(
798 base::UTF16ToUTF8(entry_
.key().name()),
799 base::UTF16ToUTF8(entry_
.key().value()));
800 sync_pb::AutofillSpecifics new_autofill
;
801 new_autofill
.set_name(base::UTF16ToUTF8(entry_
.key().name()));
802 new_autofill
.set_value(base::UTF16ToUTF8(entry_
.key().value()));
803 new_autofill
.add_usage_timestamp(entry_
.date_created().ToInternalValue());
804 if (entry_
.date_created() != entry_
.date_last_used()) {
805 new_autofill
.add_usage_timestamp(
806 entry_
.date_last_used().ToInternalValue());
809 sync_pb::EntitySpecifics entity_specifics
;
810 entity_specifics
.mutable_autofill()->CopyFrom(new_autofill
);
813 // Tell main thread we've started
814 (*wait_for_start_
)->Signal();
816 // Create write transaction.
817 WriteTransactionTest
trans(FROM_HERE
, UNITTEST
, directory
,
820 // Create actual entry based on autofill protobuf information.
821 // Simulates effects of UpdateLocalDataFromServerData
822 MutableEntry
parent(&trans
, GET_TYPE_ROOT
, syncer::AUTOFILL
);
823 MutableEntry
item(&trans
, CREATE
, syncer::AUTOFILL
, parent
.GetId(), tag
);
824 ASSERT_TRUE(item
.good());
825 item
.PutSpecifics(entity_specifics
);
826 item
.PutServerSpecifics(entity_specifics
);
827 item
.PutBaseVersion(1);
828 syncer::syncable::Id server_item_id
=
829 service_
->id_factory()->NewServerId();
830 item
.PutId(server_item_id
);
831 syncer::syncable::Id new_predecessor
;
832 ASSERT_TRUE(item
.PutPredecessor(new_predecessor
));
834 DVLOG(1) << "FakeServerUpdater finishing.";
835 is_finished_
.Signal();
838 void CreateNewEntry(const AutofillEntry
& entry
) {
840 ASSERT_FALSE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
841 if (!BrowserThread::PostTask(
842 BrowserThread::DB
, FROM_HERE
,
843 base::Bind(&FakeServerUpdater::Update
, this))) {
844 NOTREACHED() << "Failed to post task to the db thread.";
849 void CreateNewEntryAndWait(const AutofillEntry
& entry
) {
851 ASSERT_FALSE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
852 is_finished_
.Reset();
853 if (!BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
,
854 base::Bind(&FakeServerUpdater::Update
, this))) {
855 NOTREACHED() << "Failed to post task to the db thread.";
862 friend class base::RefCountedThreadSafe
<FakeServerUpdater
>;
863 ~FakeServerUpdater() { }
865 AutofillEntry entry_
;
866 TestProfileSyncService
* service_
;
867 scoped_ptr
<WaitableEvent
>* wait_for_start_
;
868 scoped_ptr
<WaitableEvent
>* wait_for_syncapi_
;
869 WaitableEvent is_finished_
;
870 syncer::syncable::Id parent_id_
;
873 // TODO(skrul): Test abort startup.
874 // TODO(skrul): Test processing of cloud changes.
875 // TODO(tim): Add autofill data type controller test, and a case to cover
876 // waiting for the PersonalDataManager.
877 TEST_F(ProfileSyncServiceAutofillTest
, FailModelAssociation
) {
878 // Don't create the root autofill node so startup fails.
879 StartSyncService(base::Closure(), true, syncer::AUTOFILL
);
880 EXPECT_TRUE(sync_service_
->HasUnrecoverableError());
883 TEST_F(ProfileSyncServiceAutofillTest
, EmptyNativeEmptySync
) {
884 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).WillOnce(Return(true));
885 SetIdleChangeProcessorExpectations();
886 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
887 EXPECT_CALL(*personal_data_manager_
, Refresh());
888 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
889 EXPECT_TRUE(create_root
.success());
890 std::vector
<AutofillEntry
> sync_entries
;
891 std::vector
<AutofillProfile
> sync_profiles
;
892 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries
, &sync_profiles
));
893 EXPECT_EQ(0U, sync_entries
.size());
894 EXPECT_EQ(0U, sync_profiles
.size());
897 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeEntriesEmptySync
) {
898 std::vector
<AutofillEntry
> entries
;
899 entries
.push_back(MakeAutofillEntry("foo", "bar", 1));
900 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
901 WillOnce(DoAll(SetArgumentPointee
<0>(entries
), Return(true)));
902 SetIdleChangeProcessorExpectations();
903 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
904 EXPECT_CALL(*personal_data_manager_
, Refresh());
905 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
906 ASSERT_TRUE(create_root
.success());
907 std::vector
<AutofillEntry
> sync_entries
;
908 std::vector
<AutofillProfile
> sync_profiles
;
909 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries
, &sync_profiles
));
910 ASSERT_EQ(1U, entries
.size());
911 EXPECT_TRUE(entries
[0] == sync_entries
[0]);
912 EXPECT_EQ(0U, sync_profiles
.size());
915 TEST_F(ProfileSyncServiceAutofillTest
, HasProfileEmptySync
) {
916 std::vector
<AutofillProfile
*> profiles
;
917 std::vector
<AutofillProfile
> expected_profiles
;
918 // Owned by GetAutofillProfiles caller.
919 AutofillProfile
* profile0
= new AutofillProfile
;
920 autofill::test::SetProfileInfoWithGuid(profile0
,
921 "54B3F9AA-335E-4F71-A27D-719C41564230", "Billing",
922 "Mitchell", "Morrison",
923 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
924 "91601", "US", "12345678910");
925 profiles
.push_back(profile0
);
926 expected_profiles
.push_back(*profile0
);
927 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).
928 WillOnce(DoAll(SetArgumentPointee
<0>(profiles
), Return(true)));
929 EXPECT_CALL(*personal_data_manager_
, Refresh());
930 SetIdleChangeProcessorExpectations();
931 CreateRootHelper
create_root(this, syncer::AUTOFILL_PROFILE
);
932 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL_PROFILE
);
933 ASSERT_TRUE(create_root
.success());
934 std::vector
<AutofillProfile
> sync_profiles
;
935 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(&sync_profiles
));
936 EXPECT_EQ(1U, sync_profiles
.size());
937 EXPECT_EQ(0, expected_profiles
[0].Compare(sync_profiles
[0]));
940 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeWithDuplicatesEmptySync
) {
941 // There is buggy autofill code that allows duplicate name/value
942 // pairs to exist in the database with separate pair_ids.
943 std::vector
<AutofillEntry
> entries
;
944 entries
.push_back(MakeAutofillEntry("foo", "bar", 1));
945 entries
.push_back(MakeAutofillEntry("dup", "", 2));
946 entries
.push_back(MakeAutofillEntry("dup", "", 3));
947 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
948 WillOnce(DoAll(SetArgumentPointee
<0>(entries
), Return(true)));
949 SetIdleChangeProcessorExpectations();
950 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
951 EXPECT_CALL(*personal_data_manager_
, Refresh());
952 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
953 ASSERT_TRUE(create_root
.success());
954 std::vector
<AutofillEntry
> sync_entries
;
955 std::vector
<AutofillProfile
> sync_profiles
;
956 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries
, &sync_profiles
));
957 EXPECT_EQ(2U, sync_entries
.size());
960 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeHasSyncNoMerge
) {
961 AutofillEntry
native_entry(MakeAutofillEntry("native", "entry", 1));
962 AutofillEntry
sync_entry(MakeAutofillEntry("sync", "entry", 2));
964 std::vector
<AutofillEntry
> native_entries
;
965 native_entries
.push_back(native_entry
);
967 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
968 WillOnce(DoAll(SetArgumentPointee
<0>(native_entries
), Return(true)));
970 std::vector
<AutofillEntry
> sync_entries
;
971 sync_entries
.push_back(sync_entry
);
973 AddAutofillHelper
<AutofillEntry
> add_autofill(this, sync_entries
);
975 EXPECT_CALL(autofill_table_
, UpdateAutofillEntries(ElementsAre(sync_entry
))).
976 WillOnce(Return(true));
978 EXPECT_CALL(*personal_data_manager_
, Refresh());
979 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL
);
980 ASSERT_TRUE(add_autofill
.success());
982 std::set
<AutofillEntry
> expected_entries
;
983 expected_entries
.insert(native_entry
);
984 expected_entries
.insert(sync_entry
);
986 std::vector
<AutofillEntry
> new_sync_entries
;
987 std::vector
<AutofillProfile
> new_sync_profiles
;
988 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries
,
989 &new_sync_profiles
));
990 std::set
<AutofillEntry
> new_sync_entries_set(new_sync_entries
.begin(),
991 new_sync_entries
.end());
993 EXPECT_TRUE(expected_entries
== new_sync_entries_set
);
996 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeHasSyncMergeEntry
) {
997 AutofillEntry
native_entry(MakeAutofillEntry("merge", "entry", 1));
998 AutofillEntry
sync_entry(MakeAutofillEntry("merge", "entry", 2));
999 AutofillEntry
merged_entry(MakeAutofillEntry("merge", "entry", 1, 2));
1001 std::vector
<AutofillEntry
> native_entries
;
1002 native_entries
.push_back(native_entry
);
1003 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
1004 WillOnce(DoAll(SetArgumentPointee
<0>(native_entries
), Return(true)));
1006 std::vector
<AutofillEntry
> sync_entries
;
1007 sync_entries
.push_back(sync_entry
);
1008 AddAutofillHelper
<AutofillEntry
> add_autofill(this, sync_entries
);
1010 EXPECT_CALL(autofill_table_
,
1011 UpdateAutofillEntries(ElementsAre(merged_entry
))).WillOnce(Return(true));
1012 EXPECT_CALL(*personal_data_manager_
, Refresh());
1013 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL
);
1014 ASSERT_TRUE(add_autofill
.success());
1016 std::vector
<AutofillEntry
> new_sync_entries
;
1017 std::vector
<AutofillProfile
> new_sync_profiles
;
1018 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries
,
1019 &new_sync_profiles
));
1020 ASSERT_EQ(1U, new_sync_entries
.size());
1021 EXPECT_TRUE(merged_entry
== new_sync_entries
[0]);
1024 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeHasSyncMergeProfile
) {
1025 AutofillProfile sync_profile
;
1026 autofill::test::SetProfileInfoWithGuid(&sync_profile
,
1027 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1028 "Mitchell", "Morrison",
1029 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1030 "91601", "US", "12345678910");
1032 AutofillProfile
* native_profile
= new AutofillProfile
;
1033 autofill::test::SetProfileInfoWithGuid(native_profile
,
1034 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing", "Alicia", "Saenz",
1035 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1036 "32801", "US", "19482937549");
1038 std::vector
<AutofillProfile
*> native_profiles
;
1039 native_profiles
.push_back(native_profile
);
1040 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).
1041 WillOnce(DoAll(SetArgumentPointee
<0>(native_profiles
), Return(true)));
1043 std::vector
<AutofillProfile
> sync_profiles
;
1044 sync_profiles
.push_back(sync_profile
);
1045 AddAutofillHelper
<AutofillProfile
> add_autofill(this, sync_profiles
);
1047 EXPECT_CALL(autofill_table_
,
1048 UpdateAutofillProfile(MatchProfiles(sync_profile
))).
1049 WillOnce(Return(true));
1050 EXPECT_CALL(*personal_data_manager_
, Refresh());
1051 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL_PROFILE
);
1052 ASSERT_TRUE(add_autofill
.success());
1054 std::vector
<AutofillProfile
> new_sync_profiles
;
1055 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1056 &new_sync_profiles
));
1057 ASSERT_EQ(1U, new_sync_profiles
.size());
1058 EXPECT_EQ(0, sync_profile
.Compare(new_sync_profiles
[0]));
1061 TEST_F(ProfileSyncServiceAutofillTest
, HasNativeHasSyncMergeProfileCombine
) {
1062 AutofillProfile sync_profile
;
1063 autofill::test::SetProfileInfoWithGuid(&sync_profile
,
1064 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1065 "Mitchell", "Morrison",
1066 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1067 "91601", "US", "12345678910");
1069 AutofillProfile
* native_profile
= new AutofillProfile
;
1070 // Same address, but different names, phones and e-mails.
1071 autofill::test::SetProfileInfoWithGuid(native_profile
,
1072 "23355099-1170-4B71-8ED4-144470CC9EBF", "Billing", "Alicia", "Saenz",
1073 "joewayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1074 "91601", "US", "19482937549");
1076 AutofillProfile
expected_profile(sync_profile
);
1077 expected_profile
.OverwriteWith(*native_profile
, "en-US");
1079 std::vector
<AutofillProfile
*> native_profiles
;
1080 native_profiles
.push_back(native_profile
);
1081 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).
1082 WillOnce(DoAll(SetArgumentPointee
<0>(native_profiles
), Return(true)));
1083 EXPECT_CALL(autofill_table_
,
1084 AddAutofillProfile(MatchProfiles(expected_profile
))).
1085 WillOnce(Return(true));
1086 EXPECT_CALL(autofill_table_
,
1087 RemoveAutofillProfile("23355099-1170-4B71-8ED4-144470CC9EBF")).
1088 WillOnce(Return(true));
1089 std::vector
<AutofillProfile
> sync_profiles
;
1090 sync_profiles
.push_back(sync_profile
);
1091 AddAutofillHelper
<AutofillProfile
> add_autofill(this, sync_profiles
);
1093 EXPECT_CALL(*personal_data_manager_
, Refresh());
1094 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL_PROFILE
);
1095 ASSERT_TRUE(add_autofill
.success());
1097 std::vector
<AutofillProfile
> new_sync_profiles
;
1098 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1099 &new_sync_profiles
));
1100 ASSERT_EQ(1U, new_sync_profiles
.size());
1101 // Check that key fields are the same.
1102 EXPECT_TRUE(new_sync_profiles
[0].IsSubsetOf(sync_profile
, "en-US"));
1105 TEST_F(ProfileSyncServiceAutofillTest
, MergeProfileWithDifferentGuid
) {
1106 AutofillProfile sync_profile
;
1108 autofill::test::SetProfileInfoWithGuid(&sync_profile
,
1109 "23355099-1170-4B71-8ED4-144470CC9EBE", "Billing",
1110 "Mitchell", "Morrison",
1111 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1112 "91601", "US", "12345678910");
1114 std::string native_guid
= "EDC609ED-7EEE-4F27-B00C-423242A9C44B";
1115 AutofillProfile
* native_profile
= new AutofillProfile
;
1116 autofill::test::SetProfileInfoWithGuid(native_profile
,
1117 native_guid
.c_str(), "Billing",
1118 "Mitchell", "Morrison",
1119 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1120 "91601", "US", "12345678910");
1122 std::vector
<AutofillProfile
*> native_profiles
;
1123 native_profiles
.push_back(native_profile
);
1124 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).
1125 WillOnce(DoAll(SetArgumentPointee
<0>(native_profiles
), Return(true)));
1127 std::vector
<AutofillProfile
> sync_profiles
;
1128 sync_profiles
.push_back(sync_profile
);
1129 AddAutofillHelper
<AutofillProfile
> add_autofill(this, sync_profiles
);
1131 EXPECT_CALL(autofill_table_
, AddAutofillProfile(_
)).
1132 WillOnce(Return(true));
1133 EXPECT_CALL(autofill_table_
, RemoveAutofillProfile(native_guid
)).
1134 WillOnce(Return(true));
1135 EXPECT_CALL(*personal_data_manager_
, Refresh());
1136 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL_PROFILE
);
1137 ASSERT_TRUE(add_autofill
.success());
1139 std::vector
<AutofillProfile
> new_sync_profiles
;
1140 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1141 &new_sync_profiles
));
1142 ASSERT_EQ(1U, new_sync_profiles
.size());
1143 EXPECT_EQ(0, sync_profile
.Compare(new_sync_profiles
[0]));
1144 EXPECT_EQ(sync_profile
.guid(), new_sync_profiles
[0].guid());
1147 TEST_F(ProfileSyncServiceAutofillTest
, ProcessUserChangeAddEntry
) {
1148 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).WillOnce(Return(true));
1149 EXPECT_CALL(*personal_data_manager_
, Refresh());
1150 SetIdleChangeProcessorExpectations();
1151 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
1152 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
1153 ASSERT_TRUE(create_root
.success());
1155 AutofillEntry
added_entry(MakeAutofillEntry("added", "entry", 1));
1157 EXPECT_CALL(autofill_table_
, GetAutofillTimestamps(_
, _
, _
, _
)).
1158 WillOnce(DoAll(SetArgumentPointee
<2>(added_entry
.date_created()),
1159 SetArgumentPointee
<3>(added_entry
.date_last_used()),
1162 AutofillChangeList changes
;
1163 changes
.push_back(AutofillChange(AutofillChange::ADD
, added_entry
.key()));
1165 web_data_service_
->OnAutofillEntriesChanged(changes
);
1167 std::vector
<AutofillEntry
> new_sync_entries
;
1168 std::vector
<AutofillProfile
> new_sync_profiles
;
1169 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries
,
1170 &new_sync_profiles
));
1171 ASSERT_EQ(1U, new_sync_entries
.size());
1172 EXPECT_TRUE(added_entry
== new_sync_entries
[0]);
1175 TEST_F(ProfileSyncServiceAutofillTest
, ProcessUserChangeAddProfile
) {
1176 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).WillOnce(Return(true));
1177 EXPECT_CALL(*personal_data_manager_
, Refresh());
1178 SetIdleChangeProcessorExpectations();
1179 CreateRootHelper
create_root(this, syncer::AUTOFILL_PROFILE
);
1180 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL_PROFILE
);
1181 ASSERT_TRUE(create_root
.success());
1183 AutofillProfile added_profile
;
1184 autofill::test::SetProfileInfoWithGuid(&added_profile
,
1185 "D6ADA912-D374-4C0A-917D-F5C8EBE43011", "Josephine", "Alicia", "Saenz",
1186 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1187 "32801", "US", "19482937549");
1189 AutofillProfileChange
change(
1190 AutofillProfileChange::ADD
, added_profile
.guid(), &added_profile
);
1191 web_data_service_
->OnAutofillProfileChanged(change
);
1193 std::vector
<AutofillProfile
> new_sync_profiles
;
1194 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1195 &new_sync_profiles
));
1196 ASSERT_EQ(1U, new_sync_profiles
.size());
1197 EXPECT_EQ(0, added_profile
.Compare(new_sync_profiles
[0]));
1200 TEST_F(ProfileSyncServiceAutofillTest
, ProcessUserChangeUpdateEntry
) {
1201 AutofillEntry
original_entry(MakeAutofillEntry("my", "entry", 1));
1202 std::vector
<AutofillEntry
> original_entries
;
1203 original_entries
.push_back(original_entry
);
1205 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
1206 WillOnce(DoAll(SetArgumentPointee
<0>(original_entries
), Return(true)));
1207 EXPECT_CALL(*personal_data_manager_
, Refresh());
1208 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
1209 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
1210 ASSERT_TRUE(create_root
.success());
1212 AutofillEntry
updated_entry(MakeAutofillEntry("my", "entry", 1, 2));
1214 EXPECT_CALL(autofill_table_
, GetAutofillTimestamps(_
, _
, _
, _
)).
1215 WillOnce(DoAll(SetArgumentPointee
<2>(updated_entry
.date_created()),
1216 SetArgumentPointee
<3>(updated_entry
.date_last_used()),
1219 AutofillChangeList changes
;
1220 changes
.push_back(AutofillChange(AutofillChange::UPDATE
,
1221 updated_entry
.key()));
1222 web_data_service_
->OnAutofillEntriesChanged(changes
);
1224 std::vector
<AutofillEntry
> new_sync_entries
;
1225 std::vector
<AutofillProfile
> new_sync_profiles
;
1226 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries
,
1227 &new_sync_profiles
));
1228 ASSERT_EQ(1U, new_sync_entries
.size());
1229 EXPECT_TRUE(updated_entry
== new_sync_entries
[0]);
1233 TEST_F(ProfileSyncServiceAutofillTest
, ProcessUserChangeRemoveEntry
) {
1234 AutofillEntry
original_entry(MakeAutofillEntry("my", "entry", 1));
1235 std::vector
<AutofillEntry
> original_entries
;
1236 original_entries
.push_back(original_entry
);
1238 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
1239 WillOnce(DoAll(SetArgumentPointee
<0>(original_entries
), Return(true)));
1240 EXPECT_CALL(*personal_data_manager_
, Refresh());
1241 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
1242 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
1243 ASSERT_TRUE(create_root
.success());
1245 AutofillChangeList changes
;
1246 changes
.push_back(AutofillChange(AutofillChange::REMOVE
,
1247 original_entry
.key()));
1248 web_data_service_
->OnAutofillEntriesChanged(changes
);
1250 std::vector
<AutofillEntry
> new_sync_entries
;
1251 std::vector
<AutofillProfile
> new_sync_profiles
;
1252 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&new_sync_entries
,
1253 &new_sync_profiles
));
1254 ASSERT_EQ(0U, new_sync_entries
.size());
1257 TEST_F(ProfileSyncServiceAutofillTest
, ProcessUserChangeRemoveProfile
) {
1258 AutofillProfile sync_profile
;
1259 autofill::test::SetProfileInfoWithGuid(&sync_profile
,
1260 "3BA5FA1B-1EC4-4BB3-9B57-EC92BE3C1A09", "Josephine", "Alicia", "Saenz",
1261 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1262 "32801", "US", "19482937549");
1263 AutofillProfile
* native_profile
= new AutofillProfile
;
1264 autofill::test::SetProfileInfoWithGuid(native_profile
,
1265 "3BA5FA1B-1EC4-4BB3-9B57-EC92BE3C1A09", "Josephine", "Alicia", "Saenz",
1266 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1267 "32801", "US", "19482937549");
1269 std::vector
<AutofillProfile
*> native_profiles
;
1270 native_profiles
.push_back(native_profile
);
1271 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).
1272 WillOnce(DoAll(SetArgumentPointee
<0>(native_profiles
), Return(true)));
1274 std::vector
<AutofillProfile
> sync_profiles
;
1275 sync_profiles
.push_back(sync_profile
);
1276 AddAutofillHelper
<AutofillProfile
> add_autofill(this, sync_profiles
);
1277 EXPECT_CALL(*personal_data_manager_
, Refresh());
1278 StartSyncService(add_autofill
.callback(), false, syncer::AUTOFILL_PROFILE
);
1279 ASSERT_TRUE(add_autofill
.success());
1281 AutofillProfileChange
change(
1282 AutofillProfileChange::REMOVE
, sync_profile
.guid(), NULL
);
1283 web_data_service_
->OnAutofillProfileChanged(change
);
1285 std::vector
<AutofillProfile
> new_sync_profiles
;
1286 ASSERT_TRUE(GetAutofillProfilesFromSyncDBUnderProfileNode(
1287 &new_sync_profiles
));
1288 ASSERT_EQ(0U, new_sync_profiles
.size());
1291 // http://crbug.com/57884
1292 TEST_F(ProfileSyncServiceAutofillTest
, DISABLED_ServerChangeRace
) {
1293 // Once for MergeDataAndStartSyncing() and twice for ProcessSyncChanges(), via
1294 // LoadAutofillData().
1295 EXPECT_CALL(autofill_table_
, GetAllAutofillEntries(_
)).
1296 Times(3).WillRepeatedly(Return(true));
1297 // On the other hand Autofill and Autocomplete are separated now, so
1298 // GetAutofillProfiles() should not be called.
1299 EXPECT_CALL(autofill_table_
, GetAutofillProfiles(_
)).Times(0);
1300 EXPECT_CALL(autofill_table_
, UpdateAutofillEntries(_
)).
1301 WillRepeatedly(Return(true));
1302 EXPECT_CALL(*personal_data_manager_
, Refresh()).Times(3);
1303 CreateRootHelper
create_root(this, syncer::AUTOFILL
);
1304 StartSyncService(create_root
.callback(), false, syncer::AUTOFILL
);
1305 ASSERT_TRUE(create_root
.success());
1307 // (true, false) means we have to reset after |Signal|, init to unsignaled.
1308 scoped_ptr
<WaitableEvent
> wait_for_start(new WaitableEvent(true, false));
1309 scoped_ptr
<WaitableEvent
> wait_for_syncapi(new WaitableEvent(true, false));
1310 scoped_refptr
<FakeServerUpdater
> updater(new FakeServerUpdater(
1311 sync_service_
, &wait_for_start
, &wait_for_syncapi
));
1313 // This server side update will stall waiting for CommitWaiter.
1314 updater
->CreateNewEntry(MakeAutofillEntry("server", "entry", 1));
1315 wait_for_start
->Wait();
1317 AutofillEntry
syncapi_entry(MakeAutofillEntry("syncapi", "entry", 2));
1318 ASSERT_TRUE(AddAutofillSyncNode(syncapi_entry
));
1319 DVLOG(1) << "Syncapi update finished.";
1321 // If we reach here, it means syncapi succeeded and we didn't deadlock. Yay!
1322 // Signal FakeServerUpdater that it can complete.
1323 wait_for_syncapi
->Signal();
1325 // Make another entry to ensure nothing broke afterwards and wait for finish
1327 updater
->CreateNewEntryAndWait(MakeAutofillEntry("server2", "entry2", 3));
1329 std::vector
<AutofillEntry
> sync_entries
;
1330 std::vector
<AutofillProfile
> sync_profiles
;
1331 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries
, &sync_profiles
));
1332 EXPECT_EQ(3U, sync_entries
.size());
1333 EXPECT_EQ(0U, sync_profiles
.size());
1334 for (size_t i
= 0; i
< sync_entries
.size(); i
++) {
1335 DVLOG(1) << "Entry " << i
<< ": " << sync_entries
[i
].key().name()
1336 << ", " << sync_entries
[i
].key().value();