| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/test/live_sync/live_autofill_sync_test.h" | |
| 6 | |
| 7 #include "chrome/browser/autofill/autofill_common_test.h" | |
| 8 #include "chrome/browser/autofill/autofill_profile.h" | |
| 9 #include "chrome/browser/autofill/autofill_type.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/sync/profile_sync_service.h" | |
| 12 #include "chrome/browser/sync/profile_sync_test_util.h" | |
| 13 #include "chrome/browser/webdata/autofill_entry.h" | |
| 14 #include "chrome/browser/webdata/autofill_table.h" | |
| 15 #include "chrome/browser/webdata/web_database.h" | |
| 16 #include "chrome/common/chrome_notification_types.h" | |
| 17 #include "chrome/test/base/thread_observer_helper.h" | |
| 18 #include "webkit/glue/form_field.h" | |
| 19 | |
| 20 using base::WaitableEvent; | |
| 21 using testing::_; | |
| 22 | |
| 23 namespace { | |
| 24 class GetAllAutofillEntries | |
| 25 : public base::RefCountedThreadSafe<GetAllAutofillEntries> { | |
| 26 public: | |
| 27 explicit GetAllAutofillEntries(WebDataService* web_data_service) | |
| 28 : web_data_service_(web_data_service), | |
| 29 done_event_(false, false) {} | |
| 30 | |
| 31 void Init() { | |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 33 BrowserThread::PostTask( | |
| 34 BrowserThread::DB, | |
| 35 FROM_HERE, | |
| 36 NewRunnableMethod(this, &GetAllAutofillEntries::Run)); | |
| 37 done_event_.Wait(); | |
| 38 } | |
| 39 | |
| 40 const std::vector<AutofillEntry>& entries() const { | |
| 41 return entries_; | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 friend class base::RefCountedThreadSafe<GetAllAutofillEntries>; | |
| 46 | |
| 47 void Run() { | |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 49 web_data_service_->GetDatabase()->GetAutofillTable()->GetAllAutofillEntries( | |
| 50 &entries_); | |
| 51 done_event_.Signal(); | |
| 52 } | |
| 53 | |
| 54 WebDataService* web_data_service_; | |
| 55 base::WaitableEvent done_event_; | |
| 56 std::vector<AutofillEntry> entries_; | |
| 57 }; | |
| 58 | |
| 59 ACTION_P(SignalEvent, event) { | |
| 60 event->Signal(); | |
| 61 } | |
| 62 | |
| 63 class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { | |
| 64 protected: | |
| 65 virtual void RegisterObservers() { | |
| 66 registrar_.Add(&observer_, | |
| 67 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | |
| 68 NotificationService::AllSources()); | |
| 69 registrar_.Add(&observer_, | |
| 70 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | |
| 71 NotificationService::AllSources()); | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 class MockPersonalDataManagerObserver : public PersonalDataManager::Observer { | |
| 76 public: | |
| 77 MOCK_METHOD0(OnPersonalDataChanged, void()); | |
| 78 }; | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 AutofillProfile CreateAutofillProfile(LiveAutofillSyncTest::ProfileType type) { | |
| 83 AutofillProfile profile; | |
| 84 switch (type) { | |
| 85 case LiveAutofillSyncTest::PROFILE_MARION: | |
| 86 autofill_test::SetProfileInfoWithGuid(&profile, | |
| 87 "C837507A-6C3B-4872-AC14-5113F157D668", | |
| 88 "Marion", "Mitchell", "Morrison", | |
| 89 "johnwayne@me.xyz", "Fox", | |
| 90 "123 Zoo St.", "unit 5", "Hollywood", "CA", | |
| 91 "91601", "US", "12345678910", "01987654321"); | |
| 92 break; | |
| 93 case LiveAutofillSyncTest::PROFILE_HOMER: | |
| 94 autofill_test::SetProfileInfoWithGuid(&profile, | |
| 95 "137DE1C3-6A30-4571-AC86-109B1ECFBE7F", | |
| 96 "Homer", "J.", "Simpson", | |
| 97 "homer@abc.com", "SNPP", | |
| 98 "1 Main St", "PO Box 1", "Springfield", "MA", | |
| 99 "94101", "US", "14155551212", "14155551313"); | |
| 100 break; | |
| 101 case LiveAutofillSyncTest::PROFILE_FRASIER: | |
| 102 autofill_test::SetProfileInfoWithGuid(&profile, | |
| 103 "9A5E6872-6198-4688-BF75-0016E781BB0A", | |
| 104 "Frasier", "Winslow", "Crane", | |
| 105 "", "randomness", "", "Apt. 4", "Seattle", "WA", | |
| 106 "99121", "US", "0000000000", "ABCDEFGHIJK"); | |
| 107 break; | |
| 108 case LiveAutofillSyncTest::PROFILE_NULL: | |
| 109 autofill_test::SetProfileInfoWithGuid(&profile, | |
| 110 "FE461507-7E13-4198-8E66-74C7DB6D8322", | |
| 111 "", "", "", "", "", "", "", "", "", "", "", "", ""); | |
| 112 break; | |
| 113 } | |
| 114 return profile; | |
| 115 } | |
| 116 | |
| 117 LiveAutofillSyncTest::LiveAutofillSyncTest(TestType test_type) | |
| 118 : LiveSyncTest(test_type) {} | |
| 119 | |
| 120 LiveAutofillSyncTest::~LiveAutofillSyncTest() {} | |
| 121 | |
| 122 WebDataService* LiveAutofillSyncTest::GetWebDataService(int index) { | |
| 123 return GetProfile(index)->GetWebDataService(Profile::EXPLICIT_ACCESS); | |
| 124 } | |
| 125 | |
| 126 PersonalDataManager* LiveAutofillSyncTest::GetPersonalDataManager(int index) { | |
| 127 return GetProfile(index)->GetPersonalDataManager(); | |
| 128 } | |
| 129 | |
| 130 void LiveAutofillSyncTest::AddKeys(int profile, | |
| 131 const std::set<AutofillKey>& keys) { | |
| 132 std::vector<webkit_glue::FormField> form_fields; | |
| 133 for (std::set<AutofillKey>::const_iterator i = keys.begin(); | |
| 134 i != keys.end(); | |
| 135 ++i) { | |
| 136 form_fields.push_back(webkit_glue::FormField(string16(), | |
| 137 (*i).name(), | |
| 138 (*i).value(), | |
| 139 string16(), | |
| 140 0, | |
| 141 false)); | |
| 142 } | |
| 143 | |
| 144 WaitableEvent done_event(false, false); | |
| 145 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | |
| 146 new AutofillDBThreadObserverHelper()); | |
| 147 observer_helper->Init(); | |
| 148 | |
| 149 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | |
| 150 WillOnce(SignalEvent(&done_event)); | |
| 151 WebDataService* wds = GetWebDataService(profile); | |
| 152 wds->AddFormFields(form_fields); | |
| 153 done_event.Wait(); | |
| 154 } | |
| 155 | |
| 156 void LiveAutofillSyncTest::RemoveKey(int profile, const AutofillKey& key) { | |
| 157 WaitableEvent done_event(false, false); | |
| 158 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | |
| 159 new AutofillDBThreadObserverHelper()); | |
| 160 observer_helper->Init(); | |
| 161 | |
| 162 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | |
| 163 WillOnce(SignalEvent(&done_event)); | |
| 164 WebDataService* wds = GetWebDataService(profile); | |
| 165 wds->RemoveFormValueForElementName(key.name(), key.value()); | |
| 166 done_event.Wait(); | |
| 167 } | |
| 168 | |
| 169 std::set<AutofillEntry> LiveAutofillSyncTest::GetAllKeys(int profile) { | |
| 170 WebDataService* wds = GetWebDataService(profile); | |
| 171 scoped_refptr<GetAllAutofillEntries> get_all_entries = | |
| 172 new GetAllAutofillEntries(wds); | |
| 173 get_all_entries->Init(); | |
| 174 const std::vector<AutofillEntry>& all_entries = get_all_entries->entries(); | |
| 175 std::set<AutofillEntry> all_keys; | |
| 176 for (std::vector<AutofillEntry>::const_iterator it = all_entries.begin(); | |
| 177 it != all_entries.end(); ++it) { | |
| 178 all_keys.insert(*it); | |
| 179 } | |
| 180 return all_keys; | |
| 181 } | |
| 182 | |
| 183 bool LiveAutofillSyncTest::KeysMatch(int profile_a, int profile_b) { | |
| 184 return GetAllKeys(profile_a) == GetAllKeys(profile_b); | |
| 185 } | |
| 186 | |
| 187 void LiveAutofillSyncTest::SetProfiles( | |
| 188 int profile, std::vector<AutofillProfile>* autofill_profiles) { | |
| 189 MockPersonalDataManagerObserver observer; | |
| 190 EXPECT_CALL(observer, OnPersonalDataChanged()). | |
| 191 WillOnce(QuitUIMessageLoop()); | |
| 192 PersonalDataManager* pdm = GetPersonalDataManager(profile); | |
| 193 pdm->SetObserver(&observer); | |
| 194 pdm->SetProfiles(autofill_profiles); | |
| 195 MessageLoop::current()->Run(); | |
| 196 pdm->RemoveObserver(&observer); | |
| 197 } | |
| 198 | |
| 199 void LiveAutofillSyncTest::AddProfile(int profile, | |
| 200 const AutofillProfile& autofill_profile) { | |
| 201 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | |
| 202 std::vector<AutofillProfile> autofill_profiles; | |
| 203 for (size_t i = 0; i < all_profiles.size(); ++i) | |
| 204 autofill_profiles.push_back(*all_profiles[i]); | |
| 205 autofill_profiles.push_back(autofill_profile); | |
| 206 SetProfiles(profile, &autofill_profiles); | |
| 207 } | |
| 208 | |
| 209 void LiveAutofillSyncTest::RemoveProfile(int profile, const std::string& guid) { | |
| 210 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | |
| 211 std::vector<AutofillProfile> autofill_profiles; | |
| 212 for (size_t i = 0; i < all_profiles.size(); ++i) { | |
| 213 if (all_profiles[i]->guid() != guid) | |
| 214 autofill_profiles.push_back(*all_profiles[i]); | |
| 215 } | |
| 216 SetProfiles(profile, &autofill_profiles); | |
| 217 } | |
| 218 | |
| 219 void LiveAutofillSyncTest::UpdateProfile(int profile, | |
| 220 const std::string& guid, | |
| 221 const AutofillType& type, | |
| 222 const string16& value) { | |
| 223 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | |
| 224 std::vector<AutofillProfile> profiles; | |
| 225 for (size_t i = 0; i < all_profiles.size(); ++i) { | |
| 226 profiles.push_back(*all_profiles[i]); | |
| 227 if (all_profiles[i]->guid() == guid) | |
| 228 profiles.back().SetInfo(type.field_type(), value); | |
| 229 } | |
| 230 SetProfiles(profile, &profiles); | |
| 231 } | |
| 232 | |
| 233 const std::vector<AutofillProfile*>& LiveAutofillSyncTest::GetAllProfiles( | |
| 234 int profile) { | |
| 235 MockPersonalDataManagerObserver observer; | |
| 236 EXPECT_CALL(observer, OnPersonalDataChanged()). | |
| 237 WillOnce(QuitUIMessageLoop()); | |
| 238 PersonalDataManager* pdm = GetPersonalDataManager(profile); | |
| 239 pdm->SetObserver(&observer); | |
| 240 pdm->Refresh(); | |
| 241 MessageLoop::current()->Run(); | |
| 242 pdm->RemoveObserver(&observer); | |
| 243 return pdm->web_profiles(); | |
| 244 } | |
| 245 | |
| 246 int LiveAutofillSyncTest::GetProfileCount(int profile) { | |
| 247 return GetAllProfiles(profile).size(); | |
| 248 } | |
| 249 | |
| 250 bool LiveAutofillSyncTest::ProfilesMatch(int profile_a, int profile_b) { | |
| 251 const std::vector<AutofillProfile*>& autofill_profiles_a = | |
| 252 GetAllProfiles(profile_a); | |
| 253 std::map<std::string, AutofillProfile> autofill_profiles_a_map; | |
| 254 for (size_t i = 0; i < autofill_profiles_a.size(); ++i) { | |
| 255 const AutofillProfile* p = autofill_profiles_a[i]; | |
| 256 autofill_profiles_a_map[p->guid()] = *p; | |
| 257 } | |
| 258 | |
| 259 const std::vector<AutofillProfile*>& autofill_profiles_b = | |
| 260 GetAllProfiles(profile_b); | |
| 261 for (size_t i = 0; i < autofill_profiles_b.size(); ++i) { | |
| 262 const AutofillProfile* p = autofill_profiles_b[i]; | |
| 263 if (!autofill_profiles_a_map.count(p->guid())) { | |
| 264 LOG(ERROR) << "GUID " << p->guid() << " not found in profile " | |
| 265 << profile_b << "."; | |
| 266 return false; | |
| 267 } | |
| 268 AutofillProfile* expected_profile = &autofill_profiles_a_map[p->guid()]; | |
| 269 expected_profile->set_guid(p->guid()); | |
| 270 if (*expected_profile != *p) { | |
| 271 LOG(ERROR) << "Mismatch in profile with GUID " << p->guid() << "."; | |
| 272 return false; | |
| 273 } | |
| 274 autofill_profiles_a_map.erase(p->guid()); | |
| 275 } | |
| 276 | |
| 277 if (autofill_profiles_a_map.size()) { | |
| 278 LOG(ERROR) << "Entries present in Profile " << profile_a | |
| 279 << " but not in " << profile_b << "."; | |
| 280 return false; | |
| 281 } | |
| 282 return true; | |
| 283 } | |
| 284 | |
| 285 bool LiveAutofillSyncTest::AllProfilesMatch() { | |
| 286 for (int i = 1; i < num_clients(); ++i) { | |
| 287 if (!ProfilesMatch(0, i)) { | |
| 288 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " | |
| 289 "profiles as profile 0."; | |
| 290 return false; | |
| 291 } | |
| 292 } | |
| 293 return true; | |
| 294 } | |
| OLD | NEW |