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