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/autofill_helper.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/live_sync/live_sync_test.h" |
| 18 #include "chrome/test/live_sync/sync_datatype_helper.h" |
18 #include "chrome/test/base/thread_observer_helper.h" | 19 #include "chrome/test/base/thread_observer_helper.h" |
19 #include "webkit/glue/form_field.h" | 20 #include "webkit/glue/form_field.h" |
20 | 21 |
21 using base::WaitableEvent; | 22 using base::WaitableEvent; |
| 23 using sync_datatype_helper::test; |
22 using testing::_; | 24 using testing::_; |
23 | 25 |
24 namespace { | 26 namespace { |
| 27 |
25 class GetAllAutofillEntries | 28 class GetAllAutofillEntries |
26 : public base::RefCountedThreadSafe<GetAllAutofillEntries> { | 29 : public base::RefCountedThreadSafe<GetAllAutofillEntries> { |
27 public: | 30 public: |
28 explicit GetAllAutofillEntries(WebDataService* web_data_service) | 31 explicit GetAllAutofillEntries(WebDataService* web_data_service) |
29 : web_data_service_(web_data_service), | 32 : web_data_service_(web_data_service), |
30 done_event_(false, false) {} | 33 done_event_(false, false) {} |
31 | 34 |
32 void Init() { | 35 void Init() { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 BrowserThread::PostTask( | 37 BrowserThread::PostTask( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 76 } |
74 }; | 77 }; |
75 | 78 |
76 class MockPersonalDataManagerObserver : public PersonalDataManager::Observer { | 79 class MockPersonalDataManagerObserver : public PersonalDataManager::Observer { |
77 public: | 80 public: |
78 MOCK_METHOD0(OnPersonalDataChanged, void()); | 81 MOCK_METHOD0(OnPersonalDataChanged, void()); |
79 }; | 82 }; |
80 | 83 |
81 } // namespace | 84 } // namespace |
82 | 85 |
83 AutofillHelper::AutofillHelper() {} | 86 namespace autofill_helper { |
84 | 87 |
85 AutofillHelper::~AutofillHelper() {} | 88 AutofillProfile CreateAutofillProfile(ProfileType type) { |
86 | |
87 // static | |
88 AutofillProfile AutofillHelper::CreateAutofillProfile(ProfileType type) { | |
89 AutofillProfile profile; | 89 AutofillProfile profile; |
90 switch (type) { | 90 switch (type) { |
91 case PROFILE_MARION: | 91 case PROFILE_MARION: |
92 autofill_test::SetProfileInfoWithGuid(&profile, | 92 autofill_test::SetProfileInfoWithGuid(&profile, |
93 "C837507A-6C3B-4872-AC14-5113F157D668", | 93 "C837507A-6C3B-4872-AC14-5113F157D668", |
94 "Marion", "Mitchell", "Morrison", | 94 "Marion", "Mitchell", "Morrison", |
95 "johnwayne@me.xyz", "Fox", | 95 "johnwayne@me.xyz", "Fox", |
96 "123 Zoo St.", "unit 5", "Hollywood", "CA", | 96 "123 Zoo St.", "unit 5", "Hollywood", "CA", |
97 "91601", "US", "12345678910", "01987654321"); | 97 "91601", "US", "12345678910", "01987654321"); |
98 break; | 98 break; |
(...skipping 14 matching lines...) Expand all Loading... |
113 break; | 113 break; |
114 case PROFILE_NULL: | 114 case PROFILE_NULL: |
115 autofill_test::SetProfileInfoWithGuid(&profile, | 115 autofill_test::SetProfileInfoWithGuid(&profile, |
116 "FE461507-7E13-4198-8E66-74C7DB6D8322", | 116 "FE461507-7E13-4198-8E66-74C7DB6D8322", |
117 "", "", "", "", "", "", "", "", "", "", "", "", ""); | 117 "", "", "", "", "", "", "", "", "", "", "", "", ""); |
118 break; | 118 break; |
119 } | 119 } |
120 return profile; | 120 return profile; |
121 } | 121 } |
122 | 122 |
123 // static | 123 WebDataService* GetWebDataService(int index) { |
124 WebDataService* AutofillHelper::GetWebDataService(int index) { | |
125 return test()->GetProfile(index)->GetWebDataService(Profile::EXPLICIT_ACCESS); | 124 return test()->GetProfile(index)->GetWebDataService(Profile::EXPLICIT_ACCESS); |
126 } | 125 } |
127 | 126 |
128 // static | 127 PersonalDataManager* GetPersonalDataManager(int index) { |
129 PersonalDataManager* AutofillHelper::GetPersonalDataManager(int index) { | |
130 return test()->GetProfile(index)->GetPersonalDataManager(); | 128 return test()->GetProfile(index)->GetPersonalDataManager(); |
131 } | 129 } |
132 | 130 |
133 // static | 131 void AddKeys(int profile, |
134 void AutofillHelper::AddKeys(int profile, | |
135 const std::set<AutofillKey>& keys) { | 132 const std::set<AutofillKey>& keys) { |
136 std::vector<webkit_glue::FormField> form_fields; | 133 std::vector<webkit_glue::FormField> form_fields; |
137 for (std::set<AutofillKey>::const_iterator i = keys.begin(); | 134 for (std::set<AutofillKey>::const_iterator i = keys.begin(); |
138 i != keys.end(); | 135 i != keys.end(); |
139 ++i) { | 136 ++i) { |
140 form_fields.push_back(webkit_glue::FormField(string16(), | 137 form_fields.push_back(webkit_glue::FormField(string16(), |
141 (*i).name(), | 138 (*i).name(), |
142 (*i).value(), | 139 (*i).value(), |
143 string16(), | 140 string16(), |
144 0, | 141 0, |
145 false)); | 142 false)); |
146 } | 143 } |
147 | 144 |
148 WaitableEvent done_event(false, false); | 145 WaitableEvent done_event(false, false); |
149 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | 146 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( |
150 new AutofillDBThreadObserverHelper()); | 147 new AutofillDBThreadObserverHelper()); |
151 observer_helper->Init(); | 148 observer_helper->Init(); |
152 | 149 |
153 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | 150 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). |
154 WillOnce(SignalEvent(&done_event)); | 151 WillOnce(SignalEvent(&done_event)); |
155 WebDataService* wds = GetWebDataService(profile); | 152 WebDataService* wds = GetWebDataService(profile); |
156 wds->AddFormFields(form_fields); | 153 wds->AddFormFields(form_fields); |
157 done_event.Wait(); | 154 done_event.Wait(); |
158 } | 155 } |
159 | 156 |
160 // static | 157 void RemoveKey(int profile, const AutofillKey& key) { |
161 void AutofillHelper::RemoveKey(int profile, const AutofillKey& key) { | |
162 WaitableEvent done_event(false, false); | 158 WaitableEvent done_event(false, false); |
163 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( | 159 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper( |
164 new AutofillDBThreadObserverHelper()); | 160 new AutofillDBThreadObserverHelper()); |
165 observer_helper->Init(); | 161 observer_helper->Init(); |
166 | 162 |
167 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). | 163 EXPECT_CALL(*observer_helper->observer(), Observe(_, _, _)). |
168 WillOnce(SignalEvent(&done_event)); | 164 WillOnce(SignalEvent(&done_event)); |
169 WebDataService* wds = GetWebDataService(profile); | 165 WebDataService* wds = GetWebDataService(profile); |
170 wds->RemoveFormValueForElementName(key.name(), key.value()); | 166 wds->RemoveFormValueForElementName(key.name(), key.value()); |
171 done_event.Wait(); | 167 done_event.Wait(); |
172 } | 168 } |
173 | 169 |
174 // static | 170 std::set<AutofillEntry> GetAllKeys(int profile) { |
175 std::set<AutofillEntry> AutofillHelper::GetAllKeys(int profile) { | |
176 WebDataService* wds = GetWebDataService(profile); | 171 WebDataService* wds = GetWebDataService(profile); |
177 scoped_refptr<GetAllAutofillEntries> get_all_entries = | 172 scoped_refptr<GetAllAutofillEntries> get_all_entries = |
178 new GetAllAutofillEntries(wds); | 173 new GetAllAutofillEntries(wds); |
179 get_all_entries->Init(); | 174 get_all_entries->Init(); |
180 const std::vector<AutofillEntry>& all_entries = get_all_entries->entries(); | 175 const std::vector<AutofillEntry>& all_entries = get_all_entries->entries(); |
181 std::set<AutofillEntry> all_keys; | 176 std::set<AutofillEntry> all_keys; |
182 for (std::vector<AutofillEntry>::const_iterator it = all_entries.begin(); | 177 for (std::vector<AutofillEntry>::const_iterator it = all_entries.begin(); |
183 it != all_entries.end(); ++it) { | 178 it != all_entries.end(); ++it) { |
184 all_keys.insert(*it); | 179 all_keys.insert(*it); |
185 } | 180 } |
186 return all_keys; | 181 return all_keys; |
187 } | 182 } |
188 | 183 |
189 // static | 184 bool KeysMatch(int profile_a, int profile_b) { |
190 bool AutofillHelper::KeysMatch(int profile_a, int profile_b) { | |
191 return GetAllKeys(profile_a) == GetAllKeys(profile_b); | 185 return GetAllKeys(profile_a) == GetAllKeys(profile_b); |
192 } | 186 } |
193 | 187 |
194 // static | 188 void SetProfiles(int profile, std::vector<AutofillProfile>* autofill_profiles) { |
195 void AutofillHelper::SetProfiles( | |
196 int profile, std::vector<AutofillProfile>* autofill_profiles) { | |
197 MockPersonalDataManagerObserver observer; | 189 MockPersonalDataManagerObserver observer; |
198 EXPECT_CALL(observer, OnPersonalDataChanged()). | 190 EXPECT_CALL(observer, OnPersonalDataChanged()). |
199 WillOnce(QuitUIMessageLoop()); | 191 WillOnce(QuitUIMessageLoop()); |
200 PersonalDataManager* pdm = GetPersonalDataManager(profile); | 192 PersonalDataManager* pdm = GetPersonalDataManager(profile); |
201 pdm->SetObserver(&observer); | 193 pdm->SetObserver(&observer); |
202 pdm->SetProfiles(autofill_profiles); | 194 pdm->SetProfiles(autofill_profiles); |
203 MessageLoop::current()->Run(); | 195 MessageLoop::current()->Run(); |
204 pdm->RemoveObserver(&observer); | 196 pdm->RemoveObserver(&observer); |
205 } | 197 } |
206 | 198 |
207 // static | 199 void AddProfile(int profile, const AutofillProfile& autofill_profile) { |
208 void AutofillHelper::AddProfile(int profile, | |
209 const AutofillProfile& autofill_profile) { | |
210 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | 200 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); |
211 std::vector<AutofillProfile> autofill_profiles; | 201 std::vector<AutofillProfile> autofill_profiles; |
212 for (size_t i = 0; i < all_profiles.size(); ++i) | 202 for (size_t i = 0; i < all_profiles.size(); ++i) |
213 autofill_profiles.push_back(*all_profiles[i]); | 203 autofill_profiles.push_back(*all_profiles[i]); |
214 autofill_profiles.push_back(autofill_profile); | 204 autofill_profiles.push_back(autofill_profile); |
215 SetProfiles(profile, &autofill_profiles); | 205 autofill_helper::SetProfiles(profile, &autofill_profiles); |
216 } | 206 } |
217 | 207 |
218 // static | 208 void RemoveProfile(int profile, const std::string& guid) { |
219 void AutofillHelper::RemoveProfile(int profile, const std::string& guid) { | |
220 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | 209 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); |
221 std::vector<AutofillProfile> autofill_profiles; | 210 std::vector<AutofillProfile> autofill_profiles; |
222 for (size_t i = 0; i < all_profiles.size(); ++i) { | 211 for (size_t i = 0; i < all_profiles.size(); ++i) { |
223 if (all_profiles[i]->guid() != guid) | 212 if (all_profiles[i]->guid() != guid) |
224 autofill_profiles.push_back(*all_profiles[i]); | 213 autofill_profiles.push_back(*all_profiles[i]); |
225 } | 214 } |
226 SetProfiles(profile, &autofill_profiles); | 215 autofill_helper::SetProfiles(profile, &autofill_profiles); |
227 } | 216 } |
228 | 217 |
229 // static | 218 void UpdateProfile(int profile, |
230 void AutofillHelper::UpdateProfile(int profile, | |
231 const std::string& guid, | 219 const std::string& guid, |
232 const AutofillType& type, | 220 const AutofillType& type, |
233 const string16& value) { | 221 const string16& value) { |
234 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); | 222 const std::vector<AutofillProfile*>& all_profiles = GetAllProfiles(profile); |
235 std::vector<AutofillProfile> profiles; | 223 std::vector<AutofillProfile> profiles; |
236 for (size_t i = 0; i < all_profiles.size(); ++i) { | 224 for (size_t i = 0; i < all_profiles.size(); ++i) { |
237 profiles.push_back(*all_profiles[i]); | 225 profiles.push_back(*all_profiles[i]); |
238 if (all_profiles[i]->guid() == guid) | 226 if (all_profiles[i]->guid() == guid) |
239 profiles.back().SetInfo(type.field_type(), value); | 227 profiles.back().SetInfo(type.field_type(), value); |
240 } | 228 } |
241 SetProfiles(profile, &profiles); | 229 autofill_helper::SetProfiles(profile, &profiles); |
242 } | 230 } |
243 | 231 |
244 // static | 232 const std::vector<AutofillProfile*>& GetAllProfiles( |
245 const std::vector<AutofillProfile*>& AutofillHelper::GetAllProfiles( | |
246 int profile) { | 233 int profile) { |
247 MockPersonalDataManagerObserver observer; | 234 MockPersonalDataManagerObserver observer; |
248 EXPECT_CALL(observer, OnPersonalDataChanged()). | 235 EXPECT_CALL(observer, OnPersonalDataChanged()). |
249 WillOnce(QuitUIMessageLoop()); | 236 WillOnce(QuitUIMessageLoop()); |
250 PersonalDataManager* pdm = GetPersonalDataManager(profile); | 237 PersonalDataManager* pdm = GetPersonalDataManager(profile); |
251 pdm->SetObserver(&observer); | 238 pdm->SetObserver(&observer); |
252 pdm->Refresh(); | 239 pdm->Refresh(); |
253 MessageLoop::current()->Run(); | 240 MessageLoop::current()->Run(); |
254 pdm->RemoveObserver(&observer); | 241 pdm->RemoveObserver(&observer); |
255 return pdm->web_profiles(); | 242 return pdm->web_profiles(); |
256 } | 243 } |
257 | 244 |
258 // static | 245 int GetProfileCount(int profile) { |
259 int AutofillHelper::GetProfileCount(int profile) { | |
260 return GetAllProfiles(profile).size(); | 246 return GetAllProfiles(profile).size(); |
261 } | 247 } |
262 | 248 |
263 // static | 249 bool ProfilesMatch(int profile_a, int profile_b) { |
264 bool AutofillHelper::ProfilesMatch(int profile_a, int profile_b) { | |
265 const std::vector<AutofillProfile*>& autofill_profiles_a = | 250 const std::vector<AutofillProfile*>& autofill_profiles_a = |
266 GetAllProfiles(profile_a); | 251 GetAllProfiles(profile_a); |
267 std::map<std::string, AutofillProfile> autofill_profiles_a_map; | 252 std::map<std::string, AutofillProfile> autofill_profiles_a_map; |
268 for (size_t i = 0; i < autofill_profiles_a.size(); ++i) { | 253 for (size_t i = 0; i < autofill_profiles_a.size(); ++i) { |
269 const AutofillProfile* p = autofill_profiles_a[i]; | 254 const AutofillProfile* p = autofill_profiles_a[i]; |
270 autofill_profiles_a_map[p->guid()] = *p; | 255 autofill_profiles_a_map[p->guid()] = *p; |
271 } | 256 } |
272 | 257 |
273 const std::vector<AutofillProfile*>& autofill_profiles_b = | 258 const std::vector<AutofillProfile*>& autofill_profiles_b = |
274 GetAllProfiles(profile_b); | 259 GetAllProfiles(profile_b); |
(...skipping 14 matching lines...) Expand all Loading... |
289 } | 274 } |
290 | 275 |
291 if (autofill_profiles_a_map.size()) { | 276 if (autofill_profiles_a_map.size()) { |
292 LOG(ERROR) << "Entries present in Profile " << profile_a | 277 LOG(ERROR) << "Entries present in Profile " << profile_a |
293 << " but not in " << profile_b << "."; | 278 << " but not in " << profile_b << "."; |
294 return false; | 279 return false; |
295 } | 280 } |
296 return true; | 281 return true; |
297 } | 282 } |
298 | 283 |
299 // static | 284 bool AllProfilesMatch() { |
300 bool AutofillHelper::AllProfilesMatch() { | |
301 for (int i = 1; i < test()->num_clients(); ++i) { | 285 for (int i = 1; i < test()->num_clients(); ++i) { |
302 if (!ProfilesMatch(0, i)) { | 286 if (!ProfilesMatch(0, i)) { |
303 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " | 287 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " |
304 "profiles as profile 0."; | 288 "profiles as profile 0."; |
305 return false; | 289 return false; |
306 } | 290 } |
307 } | 291 } |
308 return true; | 292 return true; |
309 } | 293 } |
| 294 |
| 295 } // namespace autofill_helper |
OLD | NEW |