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