| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/test/integration/autofill_helper.h" | 5 #include "chrome/browser/sync/test/integration/autofill_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 7 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/sync/profile_sync_service.h" | 9 #include "chrome/browser/sync/profile_sync_service.h" |
| 10 #include "chrome/browser/sync/profile_sync_test_util.h" | 10 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 11 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 11 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 12 #include "chrome/browser/sync/test/integration/sync_test.h" | 12 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 13 #include "chrome/browser/webdata/autofill_entry.h" | 13 #include "chrome/browser/webdata/autofill_entry.h" |
| 14 #include "chrome/browser/webdata/autofill_table.h" | 14 #include "chrome/browser/webdata/autofill_table.h" |
| 15 #include "chrome/browser/webdata/autofill_web_data_service.h" | 15 #include "chrome/browser/webdata/autofill_web_data_service.h" |
| 16 #include "chrome/browser/webdata/web_database.h" | 16 #include "chrome/browser/webdata/web_database.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/test/base/thread_observer_helper.h" | |
| 19 #include "components/autofill/browser/autofill_common_test.h" | 18 #include "components/autofill/browser/autofill_common_test.h" |
| 20 #include "components/autofill/browser/autofill_profile.h" | 19 #include "components/autofill/browser/autofill_profile.h" |
| 21 #include "components/autofill/browser/autofill_type.h" | 20 #include "components/autofill/browser/autofill_type.h" |
| 22 #include "components/autofill/browser/personal_data_manager.h" | 21 #include "components/autofill/browser/personal_data_manager.h" |
| 23 #include "components/autofill/browser/personal_data_manager_observer.h" | 22 #include "components/autofill/browser/personal_data_manager_observer.h" |
| 24 #include "components/autofill/common/form_field_data.h" | 23 #include "components/autofill/common/form_field_data.h" |
| 25 | 24 |
| 26 using base::WaitableEvent; | 25 using base::WaitableEvent; |
| 27 using content::BrowserThread; | 26 using content::BrowserThread; |
| 28 using sync_datatype_helper::test; | 27 using sync_datatype_helper::test; |
| 29 using testing::_; | 28 using testing::_; |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 ACTION_P(SignalEvent, event) { | 32 ACTION_P(SignalEvent, event) { |
| 34 event->Signal(); | 33 event->Signal(); |
| 35 } | 34 } |
| 36 | 35 |
| 37 class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { | 36 class MockWebDataServiceObserver |
| 38 protected: | 37 : public AutofillWebDataServiceObserverOnDBThread { |
| 39 virtual ~AutofillDBThreadObserverHelper() {} | 38 public: |
| 40 | 39 MOCK_METHOD1(AutofillEntriesChanged, |
| 41 virtual void RegisterObservers() OVERRIDE { | 40 void(const AutofillChangeList& changes)); |
| 42 registrar_.Add(&observer_, | |
| 43 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | |
| 44 content::NotificationService::AllSources()); | |
| 45 registrar_.Add(&observer_, | |
| 46 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | |
| 47 content::NotificationService::AllSources()); | |
| 48 } | |
| 49 }; | 41 }; |
| 50 | 42 |
| 51 class MockPersonalDataManagerObserver : public PersonalDataManagerObserver { | 43 class MockPersonalDataManagerObserver : public PersonalDataManagerObserver { |
| 52 public: | 44 public: |
| 53 MOCK_METHOD0(OnPersonalDataChanged, void()); | 45 MOCK_METHOD0(OnPersonalDataChanged, void()); |
| 54 }; | 46 }; |
| 55 | 47 |
| 56 void RemoveKeyDontBlockForSync(int profile, const AutofillKey& key) { | |
| 57 WaitableEvent done_event(false, false); | |
| 58 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | |
| 59 new AutofillDBThreadObserverHelper()); | |
| 60 observer_helper->Init(); | |
| 61 | |
| 62 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | |
| 63 WillOnce(SignalEvent(&done_event)); | |
| 64 scoped_refptr<AutofillWebDataService> wds = | |
| 65 autofill_helper::GetWebDataService(profile); | |
| 66 wds->RemoveFormValueForElementName(key.name(), key.value()); | |
| 67 done_event.Wait(); | |
| 68 } | |
| 69 | |
| 70 void RunOnDBThreadAndSignal(base::Closure task, | 48 void RunOnDBThreadAndSignal(base::Closure task, |
| 71 base::WaitableEvent* done_event) { | 49 base::WaitableEvent* done_event) { |
| 72 if (!task.is_null()) { | 50 if (!task.is_null()) { |
| 73 task.Run(); | 51 task.Run(); |
| 74 } | 52 } |
| 75 done_event->Signal(); | 53 done_event->Signal(); |
| 76 } | 54 } |
| 77 | 55 |
| 78 void RunOnDBThreadAndBlock(base::Closure task) { | 56 void RunOnDBThreadAndBlock(base::Closure task) { |
| 79 WaitableEvent done_event(false, false); | 57 WaitableEvent done_event(false, false); |
| 80 BrowserThread::PostTask(BrowserThread::DB, | 58 BrowserThread::PostTask(BrowserThread::DB, |
| 81 FROM_HERE, | 59 FROM_HERE, |
| 82 Bind(&RunOnDBThreadAndSignal, task, &done_event)); | 60 Bind(&RunOnDBThreadAndSignal, task, &done_event)); |
| 83 done_event.Wait(); | 61 done_event.Wait(); |
| 84 } | 62 } |
| 85 | 63 |
| 64 void RemoveKeyDontBlockForSync(int profile, const AutofillKey& key) { |
| 65 WaitableEvent done_event(false, false); |
| 66 |
| 67 MockWebDataServiceObserver mock_observer; |
| 68 EXPECT_CALL(mock_observer, AutofillEntriesChanged(_)) |
| 69 .WillOnce(SignalEvent(&done_event)); |
| 70 |
| 71 scoped_refptr<AutofillWebDataService> wds = |
| 72 autofill_helper::GetWebDataService(profile); |
| 73 |
| 74 void(AutofillWebDataService::*add_observer_func)( |
| 75 AutofillWebDataServiceObserverOnDBThread*) = |
| 76 &AutofillWebDataService::AddObserver; |
| 77 RunOnDBThreadAndBlock(Bind(add_observer_func, wds, &mock_observer)); |
| 78 |
| 79 wds->RemoveFormValueForElementName(key.name(), key.value()); |
| 80 done_event.Wait(); |
| 81 |
| 82 void(AutofillWebDataService::*remove_observer_func)( |
| 83 AutofillWebDataServiceObserverOnDBThread*) = |
| 84 &AutofillWebDataService::RemoveObserver; |
| 85 RunOnDBThreadAndBlock(Bind(remove_observer_func, wds, &mock_observer)); |
| 86 } |
| 87 |
| 86 void GetAllAutofillEntriesOnDBThread(AutofillWebDataService* wds, | 88 void GetAllAutofillEntriesOnDBThread(AutofillWebDataService* wds, |
| 87 std::vector<AutofillEntry>* entries) { | 89 std::vector<AutofillEntry>* entries) { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 89 AutofillTable::FromWebDatabase( | 91 AutofillTable::FromWebDatabase( |
| 90 wds->GetDatabase())->GetAllAutofillEntries(entries); | 92 wds->GetDatabase())->GetAllAutofillEntries(entries); |
| 91 } | 93 } |
| 92 | 94 |
| 93 std::vector<AutofillEntry> GetAllAutofillEntries(AutofillWebDataService* wds) { | 95 std::vector<AutofillEntry> GetAllAutofillEntries(AutofillWebDataService* wds) { |
| 94 std::vector<AutofillEntry> entries; | 96 std::vector<AutofillEntry> entries; |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 for (std::set<AutofillKey>::const_iterator i = keys.begin(); | 164 for (std::set<AutofillKey>::const_iterator i = keys.begin(); |
| 163 i != keys.end(); | 165 i != keys.end(); |
| 164 ++i) { | 166 ++i) { |
| 165 FormFieldData field; | 167 FormFieldData field; |
| 166 field.name = i->name(); | 168 field.name = i->name(); |
| 167 field.value = i->value(); | 169 field.value = i->value(); |
| 168 form_fields.push_back(field); | 170 form_fields.push_back(field); |
| 169 } | 171 } |
| 170 | 172 |
| 171 WaitableEvent done_event(false, false); | 173 WaitableEvent done_event(false, false); |
| 172 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | 174 MockWebDataServiceObserver mock_observer; |
| 173 new AutofillDBThreadObserverHelper()); | 175 EXPECT_CALL(mock_observer, AutofillEntriesChanged(_)) |
| 174 observer_helper->Init(); | 176 .WillOnce(SignalEvent(&done_event)); |
| 175 | 177 |
| 176 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | |
| 177 WillOnce(SignalEvent(&done_event)); | |
| 178 scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile); | 178 scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile); |
| 179 |
| 180 void(AutofillWebDataService::*add_observer_func)( |
| 181 AutofillWebDataServiceObserverOnDBThread*) = |
| 182 &AutofillWebDataService::AddObserver; |
| 183 RunOnDBThreadAndBlock(Bind(add_observer_func, wds, &mock_observer)); |
| 184 |
| 179 wds->AddFormFields(form_fields); | 185 wds->AddFormFields(form_fields); |
| 180 done_event.Wait(); | 186 done_event.Wait(); |
| 181 BlockForPendingDBThreadTasks(); | 187 BlockForPendingDBThreadTasks(); |
| 188 |
| 189 void(AutofillWebDataService::*remove_observer_func)( |
| 190 AutofillWebDataServiceObserverOnDBThread*) = |
| 191 &AutofillWebDataService::RemoveObserver; |
| 192 RunOnDBThreadAndBlock(Bind(remove_observer_func, wds, &mock_observer)); |
| 182 } | 193 } |
| 183 | 194 |
| 184 void RemoveKey(int profile, const AutofillKey& key) { | 195 void RemoveKey(int profile, const AutofillKey& key) { |
| 185 RemoveKeyDontBlockForSync(profile, key); | 196 RemoveKeyDontBlockForSync(profile, key); |
| 186 BlockForPendingDBThreadTasks(); | 197 BlockForPendingDBThreadTasks(); |
| 187 } | 198 } |
| 188 | 199 |
| 189 void RemoveKeys(int profile) { | 200 void RemoveKeys(int profile) { |
| 190 std::set<AutofillEntry> keys = GetAllKeys(profile); | 201 std::set<AutofillEntry> keys = GetAllKeys(profile); |
| 191 for (std::set<AutofillEntry>::const_iterator it = keys.begin(); | 202 for (std::set<AutofillEntry>::const_iterator it = keys.begin(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 if (!ProfilesMatch(0, i)) { | 337 if (!ProfilesMatch(0, i)) { |
| 327 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " | 338 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " |
| 328 "profiles as profile 0."; | 339 "profiles as profile 0."; |
| 329 return false; | 340 return false; |
| 330 } | 341 } |
| 331 } | 342 } |
| 332 return true; | 343 return true; |
| 333 } | 344 } |
| 334 | 345 |
| 335 } // namespace autofill_helper | 346 } // namespace autofill_helper |
| OLD | NEW |