Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: chrome/browser/autofill/autofill_manager_unittest.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <vector>
7
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/string16.h"
13 #include "base/stringprintf.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/time.h"
16 #include "base/tuple.h"
17 #include "base/utf_string_conversions.h"
18 #include "chrome/browser/autofill/autocomplete_history_manager.h"
19 #include "chrome/browser/autofill/autofill_common_test.h"
20 #include "chrome/browser/autofill/autofill_manager.h"
21 #include "chrome/browser/autofill/autofill_metrics.h"
22 #include "chrome/browser/autofill/autofill_profile.h"
23 #include "chrome/browser/autofill/credit_card.h"
24 #include "chrome/browser/autofill/personal_data_manager.h"
25 #include "chrome/browser/autofill/personal_data_manager_factory.h"
26 #include "chrome/browser/autofill/test_autofill_external_delegate.h"
27 #include "chrome/browser/password_manager/password_manager.h"
28 #include "chrome/browser/password_manager/password_manager_delegate_impl.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/sync/profile_sync_service.h"
31 #include "chrome/browser/sync/profile_sync_service_factory.h"
32 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
33 #include "chrome/browser/ui/browser.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
36 #include "chrome/test/base/testing_profile.h"
37 #include "components/autofill/common/autofill_messages.h"
38 #include "components/autofill/common/form_data.h"
39 #include "components/autofill/common/form_field_data.h"
40 #include "components/user_prefs/user_prefs.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/common/ssl_status.h"
43 #include "content/public/test/mock_render_process_host.h"
44 #include "content/public/test/test_browser_thread.h"
45 #include "googleurl/src/gurl.h"
46 #include "grit/generated_resources.h"
47 #include "ipc/ipc_test_sink.h"
48 #include "testing/gmock/include/gmock/gmock.h"
49 #include "testing/gtest/include/gtest/gtest.h"
50 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h"
51 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
52 #include "ui/base/l10n/l10n_util.h"
53 #include "ui/gfx/rect.h"
54
55 typedef PersonalDataManager::GUIDPair GUIDPair;
56 using content::BrowserThread;
57 using content::WebContents;
58 using testing::_;
59 using WebKit::WebFormElement;
60
61 namespace {
62
63 // The page ID sent to the AutofillManager from the RenderView, used to send
64 // an IPC message back to the renderer.
65 const int kDefaultPageID = 137;
66
67 typedef Tuple5<int,
68 std::vector<string16>,
69 std::vector<string16>,
70 std::vector<string16>,
71 std::vector<int> > AutofillParam;
72
73 class TestPersonalDataManager : public PersonalDataManager {
74 public:
75 TestPersonalDataManager() {
76 CreateTestAutofillProfiles(&web_profiles_);
77 CreateTestCreditCards(&credit_cards_);
78 }
79
80 void SetBrowserContext(content::BrowserContext* context) {
81 set_browser_context(context);
82 }
83
84 // Factory method for keyed service. PersonalDataManager is NULL for testing.
85 static ProfileKeyedService* Build(Profile* profile) {
86 return NULL;
87 }
88
89 MOCK_METHOD1(SaveImportedProfile, void(const AutofillProfile&));
90
91 AutofillProfile* GetProfileWithGUID(const char* guid) {
92 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin();
93 it != web_profiles_.end(); ++it) {
94 if (!(*it)->guid().compare(guid))
95 return *it;
96 }
97 return NULL;
98 }
99
100 CreditCard* GetCreditCardWithGUID(const char* guid) {
101 for (std::vector<CreditCard *>::iterator it = credit_cards_.begin();
102 it != credit_cards_.end(); ++it){
103 if (!(*it)->guid().compare(guid))
104 return *it;
105 }
106 return NULL;
107 }
108
109 void AddProfile(AutofillProfile* profile) {
110 web_profiles_.push_back(profile);
111 }
112
113 void AddCreditCard(CreditCard* credit_card) {
114 credit_cards_.push_back(credit_card);
115 }
116
117 virtual void RemoveByGUID(const std::string& guid) OVERRIDE {
118 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str());
119 if (credit_card) {
120 credit_cards_.erase(
121 std::remove(credit_cards_.begin(), credit_cards_.end(), credit_card),
122 credit_cards_.end());
123 }
124
125 AutofillProfile* profile = GetProfileWithGUID(guid.c_str());
126 if (profile) {
127 web_profiles_.erase(
128 std::remove(web_profiles_.begin(), web_profiles_.end(), profile),
129 web_profiles_.end());
130 }
131 }
132
133 // Do nothing (auxiliary profiles will be created in
134 // CreateTestAuxiliaryProfile).
135 virtual void LoadAuxiliaryProfiles() OVERRIDE {}
136
137 void ClearAutofillProfiles() {
138 web_profiles_.clear();
139 }
140
141 void ClearCreditCards() {
142 credit_cards_.clear();
143 }
144
145 void CreateTestAuxiliaryProfiles() {
146 CreateTestAutofillProfiles(&auxiliary_profiles_);
147 }
148
149 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
150 ClearCreditCards();
151 CreditCard* credit_card = new CreditCard;
152 autofill_test::SetCreditCardInfo(credit_card, "Miku Hatsune",
153 "4234567890654321", // Visa
154 month, year);
155 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
156 credit_cards_.push_back(credit_card);
157 }
158
159 private:
160 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
161 AutofillProfile* profile = new AutofillProfile;
162 autofill_test::SetProfileInfo(profile, "Elvis", "Aaron",
163 "Presley", "theking@gmail.com", "RCA",
164 "3734 Elvis Presley Blvd.", "Apt. 10",
165 "Memphis", "Tennessee", "38116", "US",
166 "12345678901");
167 profile->set_guid("00000000-0000-0000-0000-000000000001");
168 profiles->push_back(profile);
169 profile = new AutofillProfile;
170 autofill_test::SetProfileInfo(profile, "Charles", "Hardin",
171 "Holley", "buddy@gmail.com", "Decca",
172 "123 Apple St.", "unit 6", "Lubbock",
173 "Texas", "79401", "US", "23456789012");
174 profile->set_guid("00000000-0000-0000-0000-000000000002");
175 profiles->push_back(profile);
176 profile = new AutofillProfile;
177 autofill_test::SetProfileInfo(profile, "", "", "", "", "", "", "", "", "",
178 "", "", "");
179 profile->set_guid("00000000-0000-0000-0000-000000000003");
180 profiles->push_back(profile);
181 }
182
183 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
184 CreditCard* credit_card = new CreditCard;
185 autofill_test::SetCreditCardInfo(credit_card, "Elvis Presley",
186 "4234 5678 9012 3456", // Visa
187 "04", "2012");
188 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
189 credit_cards->push_back(credit_card);
190
191 credit_card = new CreditCard;
192 autofill_test::SetCreditCardInfo(credit_card, "Buddy Holly",
193 "5187654321098765", // Mastercard
194 "10", "2014");
195 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
196 credit_cards->push_back(credit_card);
197
198 credit_card = new CreditCard;
199 autofill_test::SetCreditCardInfo(credit_card, "", "", "", "");
200 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
201 credit_cards->push_back(credit_card);
202 }
203
204 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
205 };
206
207 // Populates |form| with data corresponding to a simple address form.
208 // Note that this actually appends fields to the form data, which can be useful
209 // for building up more complex test forms.
210 void CreateTestAddressFormData(FormData* form) {
211 form->name = ASCIIToUTF16("MyForm");
212 form->method = ASCIIToUTF16("POST");
213 form->origin = GURL("http://myform.com/form.html");
214 form->action = GURL("http://myform.com/submit.html");
215 form->user_submitted = true;
216
217 FormFieldData field;
218 autofill_test::CreateTestFormField(
219 "First Name", "firstname", "", "text", &field);
220 form->fields.push_back(field);
221 autofill_test::CreateTestFormField(
222 "Middle Name", "middlename", "", "text", &field);
223 form->fields.push_back(field);
224 autofill_test::CreateTestFormField(
225 "Last Name", "lastname", "", "text", &field);
226 form->fields.push_back(field);
227 autofill_test::CreateTestFormField(
228 "Address Line 1", "addr1", "", "text", &field);
229 form->fields.push_back(field);
230 autofill_test::CreateTestFormField(
231 "Address Line 2", "addr2", "", "text", &field);
232 form->fields.push_back(field);
233 autofill_test::CreateTestFormField(
234 "City", "city", "", "text", &field);
235 form->fields.push_back(field);
236 autofill_test::CreateTestFormField(
237 "State", "state", "", "text", &field);
238 form->fields.push_back(field);
239 autofill_test::CreateTestFormField(
240 "Postal Code", "zipcode", "", "text", &field);
241 form->fields.push_back(field);
242 autofill_test::CreateTestFormField(
243 "Country", "country", "", "text", &field);
244 form->fields.push_back(field);
245 autofill_test::CreateTestFormField(
246 "Phone Number", "phonenumber", "", "tel", &field);
247 form->fields.push_back(field);
248 autofill_test::CreateTestFormField(
249 "Email", "email", "", "email", &field);
250 form->fields.push_back(field);
251 }
252
253 // Populates |form| with data corresponding to a simple credit card form.
254 // Note that this actually appends fields to the form data, which can be useful
255 // for building up more complex test forms.
256 void CreateTestCreditCardFormData(FormData* form,
257 bool is_https,
258 bool use_month_type) {
259 form->name = ASCIIToUTF16("MyForm");
260 form->method = ASCIIToUTF16("POST");
261 if (is_https) {
262 form->origin = GURL("https://myform.com/form.html");
263 form->action = GURL("https://myform.com/submit.html");
264 } else {
265 form->origin = GURL("http://myform.com/form.html");
266 form->action = GURL("http://myform.com/submit.html");
267 }
268 form->user_submitted = true;
269
270 FormFieldData field;
271 autofill_test::CreateTestFormField(
272 "Name on Card", "nameoncard", "", "text", &field);
273 form->fields.push_back(field);
274 autofill_test::CreateTestFormField(
275 "Card Number", "cardnumber", "", "text", &field);
276 form->fields.push_back(field);
277 if (use_month_type) {
278 autofill_test::CreateTestFormField(
279 "Expiration Date", "ccmonth", "", "month", &field);
280 form->fields.push_back(field);
281 } else {
282 autofill_test::CreateTestFormField(
283 "Expiration Date", "ccmonth", "", "text", &field);
284 form->fields.push_back(field);
285 autofill_test::CreateTestFormField(
286 "", "ccyear", "", "text", &field);
287 form->fields.push_back(field);
288 }
289 }
290
291 void ExpectSuggestions(int page_id,
292 const std::vector<string16>& values,
293 const std::vector<string16>& labels,
294 const std::vector<string16>& icons,
295 const std::vector<int>& unique_ids,
296 int expected_page_id,
297 size_t expected_num_suggestions,
298 const string16 expected_values[],
299 const string16 expected_labels[],
300 const string16 expected_icons[],
301 const int expected_unique_ids[]) {
302 EXPECT_EQ(expected_page_id, page_id);
303 ASSERT_EQ(expected_num_suggestions, values.size());
304 ASSERT_EQ(expected_num_suggestions, labels.size());
305 ASSERT_EQ(expected_num_suggestions, icons.size());
306 ASSERT_EQ(expected_num_suggestions, unique_ids.size());
307 for (size_t i = 0; i < expected_num_suggestions; ++i) {
308 SCOPED_TRACE(StringPrintf("i: %" PRIuS, i));
309 EXPECT_EQ(expected_values[i], values[i]);
310 EXPECT_EQ(expected_labels[i], labels[i]);
311 EXPECT_EQ(expected_icons[i], icons[i]);
312 EXPECT_EQ(expected_unique_ids[i], unique_ids[i]);
313 }
314 }
315
316 void ExpectFilledField(const char* expected_label,
317 const char* expected_name,
318 const char* expected_value,
319 const char* expected_form_control_type,
320 const FormFieldData& field) {
321 SCOPED_TRACE(expected_label);
322 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label);
323 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name);
324 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value);
325 EXPECT_EQ(expected_form_control_type, field.form_control_type);
326 }
327
328 // Verifies that the |filled_form| has been filled with the given data.
329 // Verifies address fields if |has_address_fields| is true, and verifies
330 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
331 // are true. |use_month_type| is used for credit card input month type.
332 void ExpectFilledForm(int page_id,
333 const FormData& filled_form,
334 int expected_page_id,
335 const char* first,
336 const char* middle,
337 const char* last,
338 const char* address1,
339 const char* address2,
340 const char* city,
341 const char* state,
342 const char* postal_code,
343 const char* country,
344 const char* phone,
345 const char* email,
346 const char* name_on_card,
347 const char* card_number,
348 const char* expiration_month,
349 const char* expiration_year,
350 bool has_address_fields,
351 bool has_credit_card_fields,
352 bool use_month_type) {
353 // The number of fields in the address and credit card forms created above.
354 const size_t kAddressFormSize = 11;
355 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
356
357 EXPECT_EQ(expected_page_id, page_id);
358 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
359 EXPECT_EQ(ASCIIToUTF16("POST"), filled_form.method);
360 if (has_credit_card_fields) {
361 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
362 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
363 } else {
364 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
365 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
366 }
367 EXPECT_TRUE(filled_form.user_submitted);
368
369 size_t form_size = 0;
370 if (has_address_fields)
371 form_size += kAddressFormSize;
372 if (has_credit_card_fields)
373 form_size += kCreditCardFormSize;
374 ASSERT_EQ(form_size, filled_form.fields.size());
375
376 if (has_address_fields) {
377 ExpectFilledField("First Name", "firstname", first, "text",
378 filled_form.fields[0]);
379 ExpectFilledField("Middle Name", "middlename", middle, "text",
380 filled_form.fields[1]);
381 ExpectFilledField("Last Name", "lastname", last, "text",
382 filled_form.fields[2]);
383 ExpectFilledField("Address Line 1", "addr1", address1, "text",
384 filled_form.fields[3]);
385 ExpectFilledField("Address Line 2", "addr2", address2, "text",
386 filled_form.fields[4]);
387 ExpectFilledField("City", "city", city, "text",
388 filled_form.fields[5]);
389 ExpectFilledField("State", "state", state, "text",
390 filled_form.fields[6]);
391 ExpectFilledField("Postal Code", "zipcode", postal_code, "text",
392 filled_form.fields[7]);
393 ExpectFilledField("Country", "country", country, "text",
394 filled_form.fields[8]);
395 ExpectFilledField("Phone Number", "phonenumber", phone, "tel",
396 filled_form.fields[9]);
397 ExpectFilledField("Email", "email", email, "email",
398 filled_form.fields[10]);
399 }
400
401 if (has_credit_card_fields) {
402 size_t offset = has_address_fields? kAddressFormSize : 0;
403 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text",
404 filled_form.fields[offset + 0]);
405 ExpectFilledField("Card Number", "cardnumber", card_number, "text",
406 filled_form.fields[offset + 1]);
407 if (use_month_type) {
408 std::string exp_year = expiration_year;
409 std::string exp_month = expiration_month;
410 std::string date;
411 if (!exp_year.empty() && !exp_month.empty())
412 date = exp_year + "-" + exp_month;
413
414 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month",
415 filled_form.fields[offset + 2]);
416 } else {
417 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text",
418 filled_form.fields[offset + 2]);
419 ExpectFilledField("", "ccyear", expiration_year, "text",
420 filled_form.fields[offset + 3]);
421 }
422 }
423 }
424
425 void ExpectFilledAddressFormElvis(int page_id,
426 const FormData& filled_form,
427 int expected_page_id,
428 bool has_credit_card_fields) {
429 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
430 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
431 "Tennessee", "38116", "United States", "12345678901",
432 "theking@gmail.com", "", "", "", "", true,
433 has_credit_card_fields, false);
434 }
435
436 void ExpectFilledCreditCardFormElvis(int page_id,
437 const FormData& filled_form,
438 int expected_page_id,
439 bool has_address_fields) {
440 ExpectFilledForm(page_id, filled_form, expected_page_id,
441 "", "", "", "", "", "", "", "", "", "", "",
442 "Elvis Presley", "4234567890123456", "04", "2012",
443 has_address_fields, true, false);
444 }
445
446 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
447 const FormData& filled_form,
448 int expected_page_id,
449 bool has_address_fields,
450 const char* year,
451 const char* month) {
452 ExpectFilledForm(page_id, filled_form, expected_page_id,
453 "", "", "", "", "", "", "", "", "", "", "",
454 "Miku Hatsune", "4234567890654321", month, year,
455 has_address_fields, true, true);
456 }
457
458 class TestAutofillManager : public AutofillManager {
459 public:
460 TestAutofillManager(content::WebContents* web_contents,
461 autofill::AutofillManagerDelegate* delegate,
462 TestPersonalDataManager* personal_data)
463 : AutofillManager(web_contents, delegate, personal_data),
464 personal_data_(personal_data),
465 autofill_enabled_(true),
466 did_finish_async_form_submit_(false),
467 message_loop_is_running_(false) {
468 }
469 virtual ~TestAutofillManager() {}
470
471 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
472
473 void set_autofill_enabled(bool autofill_enabled) {
474 autofill_enabled_ = autofill_enabled;
475 }
476
477 const std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> >&
478 request_autocomplete_results() const {
479 return request_autocomplete_results_;
480 }
481
482
483 void set_expected_submitted_field_types(
484 const std::vector<FieldTypeSet>& expected_types) {
485 expected_submitted_field_types_ = expected_types;
486 }
487
488 virtual void UploadFormDataAsyncCallback(
489 const FormStructure* submitted_form,
490 const base::TimeTicks& load_time,
491 const base::TimeTicks& interaction_time,
492 const base::TimeTicks& submission_time) OVERRIDE {
493 if (message_loop_is_running_) {
494 MessageLoop::current()->Quit();
495 message_loop_is_running_ = false;
496 } else {
497 did_finish_async_form_submit_ = true;
498 }
499
500 // If we have expected field types set, make sure they match.
501 if (!expected_submitted_field_types_.empty()) {
502 ASSERT_EQ(expected_submitted_field_types_.size(),
503 submitted_form->field_count());
504 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
505 SCOPED_TRACE(
506 StringPrintf("Field %d with value %s", static_cast<int>(i),
507 UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
508 const FieldTypeSet& possible_types =
509 submitted_form->field(i)->possible_types();
510 EXPECT_EQ(expected_submitted_field_types_[i].size(),
511 possible_types.size());
512 for (FieldTypeSet::const_iterator it =
513 expected_submitted_field_types_[i].begin();
514 it != expected_submitted_field_types_[i].end(); ++it) {
515 EXPECT_TRUE(possible_types.count(*it))
516 << "Expected type: " << AutofillType::FieldTypeToString(*it);
517 }
518 }
519 }
520
521 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
522 load_time,
523 interaction_time,
524 submission_time);
525 }
526
527 // Wait for the asynchronous OnFormSubmitted() call to complete.
528 void WaitForAsyncFormSubmit() {
529 if (!did_finish_async_form_submit_) {
530 // TODO(isherman): It seems silly to need this variable. Is there some
531 // way I can just query the message loop's state?
532 message_loop_is_running_ = true;
533 MessageLoop::current()->Run();
534 } else {
535 did_finish_async_form_submit_ = false;
536 }
537 }
538
539 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE {
540 submitted_form_signature_ = submitted_form.FormSignature();
541 }
542
543 virtual void SendPasswordGenerationStateToRenderer(
544 content::RenderViewHost* host, bool enabled) OVERRIDE {
545 sent_states_.push_back(enabled);
546 }
547
548 const std::vector<bool>& GetSentStates() {
549 return sent_states_;
550 }
551
552 void ClearSentStates() {
553 sent_states_.clear();
554 }
555
556 const std::string GetSubmittedFormSignature() {
557 return submitted_form_signature_;
558 }
559
560 AutofillProfile* GetProfileWithGUID(const char* guid) {
561 return personal_data_->GetProfileWithGUID(guid);
562 }
563
564 CreditCard* GetCreditCardWithGUID(const char* guid) {
565 return personal_data_->GetCreditCardWithGUID(guid);
566 }
567
568 void AddProfile(AutofillProfile* profile) {
569 personal_data_->AddProfile(profile);
570 }
571
572 void AddCreditCard(CreditCard* credit_card) {
573 personal_data_->AddCreditCard(credit_card);
574 }
575
576 int GetPackedCreditCardID(int credit_card_id) {
577 std::string credit_card_guid =
578 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
579
580 return PackGUIDs(GUIDPair(credit_card_guid, 0), GUIDPair(std::string(), 0));
581 }
582
583 void AddSeenForm(FormStructure* form) {
584 form_structures()->push_back(form);
585 }
586
587 virtual void ReturnAutocompleteResult(
588 WebFormElement::AutocompleteResult result,
589 const FormData& form_data) OVERRIDE {
590 request_autocomplete_results_.push_back(std::make_pair(result, form_data));
591 }
592
593 private:
594 // Weak reference.
595 TestPersonalDataManager* personal_data_;
596
597 bool autofill_enabled_;
598 std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> >
599 request_autocomplete_results_;
600
601 bool did_finish_async_form_submit_;
602 bool message_loop_is_running_;
603
604 std::string submitted_form_signature_;
605 std::vector<FieldTypeSet> expected_submitted_field_types_;
606 std::vector<bool> sent_states_;
607
608 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
609 };
610
611 } // namespace
612
613 class AutofillManagerTest : public ChromeRenderViewHostTestHarness {
614 public:
615 AutofillManagerTest()
616 : ChromeRenderViewHostTestHarness(),
617 ui_thread_(BrowserThread::UI, &message_loop_),
618 file_thread_(BrowserThread::FILE),
619 io_thread_(BrowserThread::IO) {
620 }
621
622 virtual ~AutofillManagerTest() {
623 }
624
625 virtual void SetUp() OVERRIDE {
626 TestingProfile* profile = CreateProfile();
627 profile->CreateRequestContext();
628 browser_context_.reset(profile);
629 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(
630 profile, TestPersonalDataManager::Build);
631
632 ChromeRenderViewHostTestHarness::SetUp();
633 io_thread_.StartIOThread();
634 autofill::TabAutofillManagerDelegate::CreateForWebContents(web_contents());
635 personal_data_.SetBrowserContext(profile);
636 autofill_manager_.reset(new TestAutofillManager(
637 web_contents(),
638 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()),
639 &personal_data_));
640
641 file_thread_.Start();
642 }
643
644 virtual void TearDown() OVERRIDE {
645 // Order of destruction is important as AutofillManager relies on
646 // PersonalDataManager to be around when it gets destroyed. Also, a real
647 // AutofillManager is tied to the lifetime of the WebContents, so it must
648 // be destroyed at the destruction of the WebContents.
649 autofill_manager_.reset();
650 file_thread_.Stop();
651 ChromeRenderViewHostTestHarness::TearDown();
652 io_thread_.Stop();
653 }
654
655 virtual TestingProfile* CreateProfile() {
656 return new TestingProfile();
657 }
658
659 void UpdatePasswordGenerationState(bool new_renderer) {
660 autofill_manager_->UpdatePasswordGenerationState(NULL, new_renderer);
661 }
662
663 void GetAutofillSuggestions(int query_id,
664 const FormData& form,
665 const FormFieldData& field) {
666 autofill_manager_->OnQueryFormFieldAutofill(query_id,
667 form,
668 field,
669 gfx::Rect(),
670 false);
671 }
672
673 void GetAutofillSuggestions(const FormData& form,
674 const FormFieldData& field) {
675 GetAutofillSuggestions(kDefaultPageID, form, field);
676 }
677
678 void AutocompleteSuggestionsReturned(const std::vector<string16>& result) {
679 autofill_manager_->autocomplete_history_manager_.SendSuggestions(&result);
680 }
681
682 void FormsSeen(const std::vector<FormData>& forms) {
683 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
684 }
685
686 void FormSubmitted(const FormData& form) {
687 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now()))
688 autofill_manager_->WaitForAsyncFormSubmit();
689 }
690
691 void FillAutofillFormData(int query_id,
692 const FormData& form,
693 const FormFieldData& field,
694 int unique_id) {
695 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id);
696 }
697
698 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid) const {
699 return autofill_manager_->PackGUIDs(cc_guid, profile_guid);
700 }
701
702 bool GetAutofillSuggestionsMessage(int* page_id,
703 std::vector<string16>* values,
704 std::vector<string16>* labels,
705 std::vector<string16>* icons,
706 std::vector<int>* unique_ids) {
707 const uint32 kMsgID = AutofillMsg_SuggestionsReturned::ID;
708 const IPC::Message* message =
709 process()->sink().GetFirstMessageMatching(kMsgID);
710 if (!message)
711 return false;
712
713 AutofillParam autofill_param;
714 AutofillMsg_SuggestionsReturned::Read(message, &autofill_param);
715 if (page_id)
716 *page_id = autofill_param.a;
717 if (values)
718 *values = autofill_param.b;
719 if (labels)
720 *labels = autofill_param.c;
721 if (icons)
722 *icons = autofill_param.d;
723 if (unique_ids)
724 *unique_ids = autofill_param.e;
725
726 autofill_manager_->autocomplete_history_manager_.CancelPendingQuery();
727 process()->sink().ClearMessages();
728 return true;
729 }
730
731 bool GetAutofillFormDataFilledMessage(int* page_id, FormData* results) {
732 const uint32 kMsgID = AutofillMsg_FormDataFilled::ID;
733 const IPC::Message* message =
734 process()->sink().GetFirstMessageMatching(kMsgID);
735 if (!message)
736 return false;
737 Tuple2<int, FormData> autofill_param;
738 AutofillMsg_FormDataFilled::Read(message, &autofill_param);
739 if (page_id)
740 *page_id = autofill_param.a;
741 if (results)
742 *results = autofill_param.b;
743
744 process()->sink().ClearMessages();
745 return true;
746 }
747
748 protected:
749 content::TestBrowserThread ui_thread_;
750 content::TestBrowserThread file_thread_;
751 content::TestBrowserThread io_thread_;
752
753 scoped_ptr<TestAutofillManager> autofill_manager_;
754 TestPersonalDataManager personal_data_;
755
756 // Used when we want an off the record profile. This will store the original
757 // profile from which the off the record profile is derived.
758 scoped_ptr<Profile> other_browser_context_;
759
760 private:
761 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTest);
762 };
763
764 class IncognitoAutofillManagerTest : public AutofillManagerTest {
765 public:
766 IncognitoAutofillManagerTest() {}
767 virtual ~IncognitoAutofillManagerTest() {}
768
769 virtual TestingProfile* CreateProfile() OVERRIDE {
770 // Create an incognito profile.
771 TestingProfile::Builder builder;
772 scoped_ptr<TestingProfile> profile = builder.Build();
773 profile->set_incognito(true);
774 return profile.release();
775 }
776 };
777
778 class TestFormStructure : public FormStructure {
779 public:
780 explicit TestFormStructure(const FormData& form)
781 : FormStructure(form, std::string()) {}
782 virtual ~TestFormStructure() {}
783
784 void SetFieldTypes(const std::vector<AutofillFieldType>& heuristic_types,
785 const std::vector<AutofillFieldType>& server_types) {
786 ASSERT_EQ(field_count(), heuristic_types.size());
787 ASSERT_EQ(field_count(), server_types.size());
788
789 for (size_t i = 0; i < field_count(); ++i) {
790 AutofillField* form_field = field(i);
791 ASSERT_TRUE(form_field);
792 form_field->set_heuristic_type(heuristic_types[i]);
793 form_field->set_server_type(server_types[i]);
794 }
795
796 UpdateAutofillCount();
797 }
798
799 private:
800 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
801 };
802
803 // Test that we return all address profile suggestions when all form fields are
804 // empty.
805 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
806 // Set up our form data.
807 FormData form;
808 CreateTestAddressFormData(&form);
809 std::vector<FormData> forms(1, form);
810 FormsSeen(forms);
811
812 const FormFieldData& field = form.fields[0];
813 GetAutofillSuggestions(form, field);
814
815 // No suggestions provided, so send an empty vector as the results.
816 // This triggers the combined message send.
817 AutocompleteSuggestionsReturned(std::vector<string16>());
818
819 // Test that we sent the right message to the renderer.
820 int page_id = 0;
821 std::vector<string16> values;
822 std::vector<string16> labels;
823 std::vector<string16> icons;
824 std::vector<int> unique_ids;
825 GetAutofillSuggestionsMessage(
826 &page_id, &values, &labels, &icons, &unique_ids);
827
828 string16 expected_values[] = {
829 ASCIIToUTF16("Elvis"),
830 ASCIIToUTF16("Charles")
831 };
832 // Inferred labels include full first relevant field, which in this case is
833 // the address line 1.
834 string16 expected_labels[] = {
835 ASCIIToUTF16("3734 Elvis Presley Blvd."),
836 ASCIIToUTF16("123 Apple St.")
837 };
838 string16 expected_icons[] = {string16(), string16()};
839 int expected_unique_ids[] = {1, 2};
840 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
841 kDefaultPageID, arraysize(expected_values), expected_values,
842 expected_labels, expected_icons, expected_unique_ids);
843 }
844
845 // Test that we return only matching address profile suggestions when the
846 // selected form field has been partially filled out.
847 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
848 // Set up our form data.
849 FormData form;
850 CreateTestAddressFormData(&form);
851 std::vector<FormData> forms(1, form);
852 FormsSeen(forms);
853
854 FormFieldData field;
855 autofill_test::CreateTestFormField("First Name", "firstname", "E", "text",
856 &field);
857 GetAutofillSuggestions(form, field);
858
859 // No suggestions provided, so send an empty vector as the results.
860 // This triggers the combined message send.
861 AutocompleteSuggestionsReturned(std::vector<string16>());
862
863 // Test that we sent the right message to the renderer.
864 int page_id = 0;
865 std::vector<string16> values;
866 std::vector<string16> labels;
867 std::vector<string16> icons;
868 std::vector<int> unique_ids;
869 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
870 &unique_ids));
871
872 string16 expected_values[] = {ASCIIToUTF16("Elvis")};
873 string16 expected_labels[] = {ASCIIToUTF16("3734 Elvis Presley Blvd.")};
874 string16 expected_icons[] = {string16()};
875 int expected_unique_ids[] = {1};
876 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
877 kDefaultPageID, arraysize(expected_values), expected_values,
878 expected_labels, expected_icons, expected_unique_ids);
879 }
880
881 // Test that we return no suggestions when the form has no relevant fields.
882 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
883 // Set up our form data.
884 FormData form;
885 form.name = ASCIIToUTF16("MyForm");
886 form.method = ASCIIToUTF16("POST");
887 form.origin = GURL("http://myform.com/form.html");
888 form.action = GURL("http://myform.com/submit.html");
889 form.user_submitted = true;
890
891 FormFieldData field;
892 autofill_test::CreateTestFormField("Username", "username", "", "text",
893 &field);
894 form.fields.push_back(field);
895 autofill_test::CreateTestFormField("Password", "password", "", "password",
896 &field);
897 form.fields.push_back(field);
898 autofill_test::CreateTestFormField("Quest", "quest", "", "quest", &field);
899 form.fields.push_back(field);
900 autofill_test::CreateTestFormField("Color", "color", "", "text", &field);
901 form.fields.push_back(field);
902
903 std::vector<FormData> forms(1, form);
904 FormsSeen(forms);
905
906 GetAutofillSuggestions(form, field);
907 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
908 }
909
910 // Test that we cull duplicate profile suggestions.
911 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
912 // Set up our form data.
913 FormData form;
914 CreateTestAddressFormData(&form);
915 std::vector<FormData> forms(1, form);
916 FormsSeen(forms);
917
918 // Add a duplicate profile.
919 AutofillProfile* duplicate_profile =
920 new AutofillProfile(
921 *(autofill_manager_->GetProfileWithGUID(
922 "00000000-0000-0000-0000-000000000001")));
923 autofill_manager_->AddProfile(duplicate_profile);
924
925 const FormFieldData& field = form.fields[0];
926 GetAutofillSuggestions(form, field);
927
928 // No suggestions provided, so send an empty vector as the results.
929 // This triggers the combined message send.
930 AutocompleteSuggestionsReturned(std::vector<string16>());
931
932 // Test that we sent the right message to the renderer.
933 int page_id = 0;
934 std::vector<string16> values;
935 std::vector<string16> labels;
936 std::vector<string16> icons;
937 std::vector<int> unique_ids;
938 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
939 &unique_ids));
940
941 string16 expected_values[] = {
942 ASCIIToUTF16("Elvis"),
943 ASCIIToUTF16("Charles")
944 };
945 string16 expected_labels[] = {
946 ASCIIToUTF16("3734 Elvis Presley Blvd."),
947 ASCIIToUTF16("123 Apple St.")
948 };
949 string16 expected_icons[] = {string16(), string16()};
950 int expected_unique_ids[] = {1, 2};
951 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
952 kDefaultPageID, arraysize(expected_values), expected_values,
953 expected_labels, expected_icons, expected_unique_ids);
954 }
955
956 // Test that we return no suggestions when autofill is disabled.
957 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
958 // Set up our form data.
959 FormData form;
960 CreateTestAddressFormData(&form);
961 std::vector<FormData> forms(1, form);
962 FormsSeen(forms);
963
964 // Disable Autofill.
965 autofill_manager_->set_autofill_enabled(false);
966
967 const FormFieldData& field = form.fields[0];
968 GetAutofillSuggestions(form, field);
969 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
970 }
971
972 // Test that we return a warning explaining that autofill suggestions are
973 // unavailable when the form method is GET rather than POST.
974 TEST_F(AutofillManagerTest, GetProfileSuggestionsMethodGet) {
975 // Set up our form data.
976 FormData form;
977 CreateTestAddressFormData(&form);
978 form.method = ASCIIToUTF16("GET");
979 std::vector<FormData> forms(1, form);
980 FormsSeen(forms);
981
982 const FormFieldData& field = form.fields[0];
983 GetAutofillSuggestions(form, field);
984
985 // No suggestions provided, so send an empty vector as the results.
986 // This triggers the combined message send.
987 AutocompleteSuggestionsReturned(std::vector<string16>());
988
989 // Test that we sent the right message to the renderer.
990 int page_id = 0;
991 std::vector<string16> values;
992 std::vector<string16> labels;
993 std::vector<string16> icons;
994 std::vector<int> unique_ids;
995 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
996 &unique_ids));
997
998 string16 expected_values[] = {
999 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)
1000 };
1001 string16 expected_labels[] = {string16()};
1002 string16 expected_icons[] = {string16()};
1003 int expected_unique_ids[] =
1004 {WebKit::WebAutofillClient::MenuItemIDWarningMessage};
1005 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1006 kDefaultPageID, arraysize(expected_values), expected_values,
1007 expected_labels, expected_icons, expected_unique_ids);
1008
1009 // Now add some Autocomplete suggestions. We should return the autocomplete
1010 // suggestions and the warning; these will be culled by the renderer.
1011 const int kPageID2 = 2;
1012 GetAutofillSuggestions(kPageID2, form, field);
1013
1014 std::vector<string16> suggestions;
1015 suggestions.push_back(ASCIIToUTF16("Jay"));
1016 suggestions.push_back(ASCIIToUTF16("Jason"));
1017 AutocompleteSuggestionsReturned(suggestions);
1018
1019 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1020 &unique_ids));
1021
1022 string16 expected_values2[] = {
1023 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED),
1024 ASCIIToUTF16("Jay"),
1025 ASCIIToUTF16("Jason")
1026 };
1027 string16 expected_labels2[] = {string16(), string16(), string16()};
1028 string16 expected_icons2[] = {string16(), string16(), string16()};
1029 int expected_unique_ids2[] = {-1, 0, 0};
1030 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1031 kPageID2, arraysize(expected_values2), expected_values2,
1032 expected_labels2, expected_icons2, expected_unique_ids2);
1033
1034 // Now clear the test profiles and try again -- we shouldn't return a warning.
1035 personal_data_.ClearAutofillProfiles();
1036 GetAutofillSuggestions(form, field);
1037 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
1038 }
1039
1040 // Test that we return all credit card profile suggestions when all form fields
1041 // are empty.
1042 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
1043 // Set up our form data.
1044 FormData form;
1045 CreateTestCreditCardFormData(&form, true, false);
1046 std::vector<FormData> forms(1, form);
1047 FormsSeen(forms);
1048
1049 FormFieldData field = form.fields[1];
1050 GetAutofillSuggestions(form, field);
1051
1052 // No suggestions provided, so send an empty vector as the results.
1053 // This triggers the combined message send.
1054 AutocompleteSuggestionsReturned(std::vector<string16>());
1055
1056 // Test that we sent the right message to the renderer.
1057 int page_id = 0;
1058 std::vector<string16> values;
1059 std::vector<string16> labels;
1060 std::vector<string16> icons;
1061 std::vector<int> unique_ids;
1062 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1063 &unique_ids));
1064
1065 string16 expected_values[] = {
1066 ASCIIToUTF16("************3456"),
1067 ASCIIToUTF16("************8765")
1068 };
1069 string16 expected_labels[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
1070 string16 expected_icons[] = {
1071 ASCIIToUTF16("visaCC"),
1072 ASCIIToUTF16("genericCC")
1073 };
1074 int expected_unique_ids[] = {
1075 autofill_manager_->GetPackedCreditCardID(4),
1076 autofill_manager_->GetPackedCreditCardID(5)
1077 };
1078 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1079 kDefaultPageID, arraysize(expected_values), expected_values,
1080 expected_labels, expected_icons, expected_unique_ids);
1081 }
1082
1083 // Test that we return only matching credit card profile suggestions when the
1084 // selected form field has been partially filled out.
1085 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
1086 // Set up our form data.
1087 FormData form;
1088 CreateTestCreditCardFormData(&form, true, false);
1089 std::vector<FormData> forms(1, form);
1090 FormsSeen(forms);
1091
1092 FormFieldData field;
1093 autofill_test::CreateTestFormField(
1094 "Card Number", "cardnumber", "4", "text", &field);
1095 GetAutofillSuggestions(form, field);
1096
1097 // No suggestions provided, so send an empty vector as the results.
1098 // This triggers the combined message send.
1099 AutocompleteSuggestionsReturned(std::vector<string16>());
1100
1101 // Test that we sent the right message to the renderer.
1102 int page_id = 0;
1103 std::vector<string16> values;
1104 std::vector<string16> labels;
1105 std::vector<string16> icons;
1106 std::vector<int> unique_ids;
1107 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1108 &unique_ids));
1109
1110 string16 expected_values[] = {ASCIIToUTF16("************3456")};
1111 string16 expected_labels[] = {ASCIIToUTF16("*3456")};
1112 string16 expected_icons[] = {ASCIIToUTF16("visaCC")};
1113 int expected_unique_ids[] = {autofill_manager_->GetPackedCreditCardID(4)};
1114 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1115 kDefaultPageID, arraysize(expected_values), expected_values,
1116 expected_labels, expected_icons, expected_unique_ids);
1117 }
1118
1119 // Test that we return credit card profile suggestions when the selected form
1120 // field is not the credit card number field.
1121 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
1122 // Set up our form data.
1123 FormData form;
1124 CreateTestCreditCardFormData(&form, true, false);
1125 std::vector<FormData> forms(1, form);
1126 FormsSeen(forms);
1127
1128 const FormFieldData& field = form.fields[0];
1129 GetAutofillSuggestions(form, field);
1130
1131 // No suggestions provided, so send an empty vector as the results.
1132 // This triggers the combined message send.
1133 AutocompleteSuggestionsReturned(std::vector<string16>());
1134
1135 // Test that we sent the right message to the renderer.
1136 int page_id = 0;
1137 std::vector<string16> values;
1138 std::vector<string16> labels;
1139 std::vector<string16> icons;
1140 std::vector<int> unique_ids;
1141 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1142 &unique_ids));
1143
1144 string16 expected_values[] = {
1145 ASCIIToUTF16("Elvis Presley"),
1146 ASCIIToUTF16("Buddy Holly")
1147 };
1148 string16 expected_labels[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
1149 string16 expected_icons[] = {
1150 ASCIIToUTF16("visaCC"),
1151 ASCIIToUTF16("genericCC")
1152 };
1153 int expected_unique_ids[] = {
1154 autofill_manager_->GetPackedCreditCardID(4),
1155 autofill_manager_->GetPackedCreditCardID(5)
1156 };
1157 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1158 kDefaultPageID, arraysize(expected_values), expected_values,
1159 expected_labels, expected_icons, expected_unique_ids);
1160 }
1161
1162 // Test that we return a warning explaining that credit card profile suggestions
1163 // are unavailable when the form is not https.
1164 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
1165 // Set up our form data.
1166 FormData form;
1167 CreateTestCreditCardFormData(&form, false, false);
1168 std::vector<FormData> forms(1, form);
1169 FormsSeen(forms);
1170
1171 const FormFieldData& field = form.fields[0];
1172 GetAutofillSuggestions(form, field);
1173
1174 // No suggestions provided, so send an empty vector as the results.
1175 // This triggers the combined message send.
1176 AutocompleteSuggestionsReturned(std::vector<string16>());
1177
1178 // Test that we sent the right message to the renderer.
1179 int page_id = 0;
1180 std::vector<string16> values;
1181 std::vector<string16> labels;
1182 std::vector<string16> icons;
1183 std::vector<int> unique_ids;
1184 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1185 &unique_ids));
1186
1187 string16 expected_values[] = {
1188 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
1189 };
1190 string16 expected_labels[] = {string16()};
1191 string16 expected_icons[] = {string16()};
1192 int expected_unique_ids[] = {-1};
1193 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1194 kDefaultPageID, arraysize(expected_values), expected_values,
1195 expected_labels, expected_icons, expected_unique_ids);
1196
1197 // Now add some Autocomplete suggestions. We should show the autocomplete
1198 // suggestions and the warning.
1199 const int kPageID2 = 2;
1200 GetAutofillSuggestions(kPageID2, form, field);
1201
1202 std::vector<string16> suggestions;
1203 suggestions.push_back(ASCIIToUTF16("Jay"));
1204 suggestions.push_back(ASCIIToUTF16("Jason"));
1205 AutocompleteSuggestionsReturned(suggestions);
1206
1207 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1208 &unique_ids));
1209 string16 expected_values2[] = {
1210 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1211 ASCIIToUTF16("Jay"),
1212 ASCIIToUTF16("Jason")
1213 };
1214 string16 expected_labels2[] = {string16(), string16(), string16()};
1215 string16 expected_icons2[] = {string16(), string16(), string16()};
1216 int expected_unique_ids2[] = {-1, 0, 0};
1217 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1218 kPageID2, arraysize(expected_values2), expected_values2,
1219 expected_labels2, expected_icons2, expected_unique_ids2);
1220
1221 // Clear the test credit cards and try again -- we shouldn't return a warning.
1222 personal_data_.ClearCreditCards();
1223 GetAutofillSuggestions(form, field);
1224 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
1225 }
1226
1227 // Test that we return all credit card suggestions in the case that two cards
1228 // have the same obfuscated number.
1229 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
1230 // Add a credit card with the same obfuscated number as Elvis's.
1231 // |credit_card| will be owned by the mock PersonalDataManager.
1232 CreditCard* credit_card = new CreditCard;
1233 autofill_test::SetCreditCardInfo(credit_card, "Elvis Presley",
1234 "5231567890123456", // Mastercard
1235 "04", "2012");
1236 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
1237 autofill_manager_->AddCreditCard(credit_card);
1238
1239 // Set up our form data.
1240 FormData form;
1241 CreateTestCreditCardFormData(&form, true, false);
1242 std::vector<FormData> forms(1, form);
1243 FormsSeen(forms);
1244
1245 FormFieldData field = form.fields[1];
1246 GetAutofillSuggestions(form, field);
1247
1248 // No suggestions provided, so send an empty vector as the results.
1249 // This triggers the combined message send.
1250 AutocompleteSuggestionsReturned(std::vector<string16>());
1251
1252 // Test that we sent the right message to the renderer.
1253 int page_id = 0;
1254 std::vector<string16> values;
1255 std::vector<string16> labels;
1256 std::vector<string16> icons;
1257 std::vector<int> unique_ids;
1258 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1259 &unique_ids));
1260
1261 string16 expected_values[] = {
1262 ASCIIToUTF16("************3456"),
1263 ASCIIToUTF16("************8765"),
1264 ASCIIToUTF16("************3456")
1265 };
1266 string16 expected_labels[] = {
1267 ASCIIToUTF16("*3456"),
1268 ASCIIToUTF16("*8765"),
1269 ASCIIToUTF16("*3456"),
1270 };
1271 string16 expected_icons[] = {
1272 ASCIIToUTF16("visaCC"),
1273 ASCIIToUTF16("genericCC"),
1274 ASCIIToUTF16("masterCardCC")
1275 };
1276 int expected_unique_ids[] = {
1277 autofill_manager_->GetPackedCreditCardID(4),
1278 autofill_manager_->GetPackedCreditCardID(5),
1279 autofill_manager_->GetPackedCreditCardID(7)
1280 };
1281 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1282 kDefaultPageID, arraysize(expected_values), expected_values,
1283 expected_labels, expected_icons, expected_unique_ids);
1284 }
1285
1286 // Test that we return profile and credit card suggestions for combined forms.
1287 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1288 // Set up our form data.
1289 FormData form;
1290 CreateTestAddressFormData(&form);
1291 CreateTestCreditCardFormData(&form, true, false);
1292 std::vector<FormData> forms(1, form);
1293 FormsSeen(forms);
1294
1295 FormFieldData field = form.fields[0];
1296 GetAutofillSuggestions(form, field);
1297
1298 // No suggestions provided, so send an empty vector as the results.
1299 // This triggers the combined message send.
1300 AutocompleteSuggestionsReturned(std::vector<string16>());
1301
1302 // Test that we sent the right address suggestions to the renderer.
1303 int page_id = 0;
1304 std::vector<string16> values;
1305 std::vector<string16> labels;
1306 std::vector<string16> icons;
1307 std::vector<int> unique_ids;
1308 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1309 &unique_ids));
1310
1311 string16 expected_values[] = {
1312 ASCIIToUTF16("Elvis"),
1313 ASCIIToUTF16("Charles")
1314 };
1315 string16 expected_labels[] = {
1316 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1317 ASCIIToUTF16("123 Apple St.")
1318 };
1319 string16 expected_icons[] = {string16(), string16()};
1320 int expected_unique_ids[] = {1, 2};
1321 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1322 kDefaultPageID, arraysize(expected_values), expected_values,
1323 expected_labels, expected_icons, expected_unique_ids);
1324
1325 const int kPageID2 = 2;
1326 autofill_test::CreateTestFormField(
1327 "Card Number", "cardnumber", "", "text", &field);
1328 GetAutofillSuggestions(kPageID2, form, field);
1329
1330 // No suggestions provided, so send an empty vector as the results.
1331 // This triggers the combined message send.
1332 AutocompleteSuggestionsReturned(std::vector<string16>());
1333
1334 // Test that we sent the credit card suggestions to the renderer.
1335 page_id = 0;
1336 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1337 &unique_ids));
1338
1339 string16 expected_values2[] = {
1340 ASCIIToUTF16("************3456"),
1341 ASCIIToUTF16("************8765")
1342 };
1343 string16 expected_labels2[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
1344 string16 expected_icons2[] = {
1345 ASCIIToUTF16("visaCC"),
1346 ASCIIToUTF16("genericCC")
1347 };
1348 int expected_unique_ids2[] = {
1349 autofill_manager_->GetPackedCreditCardID(4),
1350 autofill_manager_->GetPackedCreditCardID(5)
1351 };
1352 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1353 kPageID2, arraysize(expected_values2), expected_values2,
1354 expected_labels2, expected_icons2, expected_unique_ids2);
1355 }
1356
1357 // Test that for non-https forms with both address and credit card fields, we
1358 // only return address suggestions. Instead of credit card suggestions, we
1359 // should return a warning explaining that credit card profile suggestions are
1360 // unavailable when the form is not https.
1361 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1362 // Set up our form data.
1363 FormData form;
1364 CreateTestAddressFormData(&form);
1365 CreateTestCreditCardFormData(&form, false, false);
1366 std::vector<FormData> forms(1, form);
1367 FormsSeen(forms);
1368
1369 FormFieldData field = form.fields[0];
1370 GetAutofillSuggestions(form, field);
1371
1372 // No suggestions provided, so send an empty vector as the results.
1373 // This triggers the combined message send.
1374 AutocompleteSuggestionsReturned(std::vector<string16>());
1375
1376 // Test that we sent the right address suggestions to the renderer.
1377 int page_id = 0;
1378 std::vector<string16> values;
1379 std::vector<string16> labels;
1380 std::vector<string16> icons;
1381 std::vector<int> unique_ids;
1382 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1383 &unique_ids));
1384
1385 string16 expected_values[] = {
1386 ASCIIToUTF16("Elvis"),
1387 ASCIIToUTF16("Charles")
1388 };
1389 string16 expected_labels[] = {
1390 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1391 ASCIIToUTF16("123 Apple St.")
1392 };
1393 string16 expected_icons[] = {string16(), string16()};
1394 int expected_unique_ids[] = {1, 2};
1395 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1396 kDefaultPageID, arraysize(expected_values), expected_values,
1397 expected_labels, expected_icons, expected_unique_ids);
1398
1399 autofill_test::CreateTestFormField(
1400 "Card Number", "cardnumber", "", "text", &field);
1401 const int kPageID2 = 2;
1402 GetAutofillSuggestions(kPageID2, form, field);
1403
1404 // No suggestions provided, so send an empty vector as the results.
1405 // This triggers the combined message send.
1406 AutocompleteSuggestionsReturned(std::vector<string16>());
1407
1408 // Test that we sent the right message to the renderer.
1409 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1410 &unique_ids));
1411
1412 string16 expected_values2[] = {
1413 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
1414 };
1415 string16 expected_labels2[] = {string16()};
1416 string16 expected_icons2[] = {string16()};
1417 int expected_unique_ids2[] = {-1};
1418 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1419 kPageID2, arraysize(expected_values2), expected_values2,
1420 expected_labels2, expected_icons2, expected_unique_ids2);
1421
1422 // Clear the test credit cards and try again -- we shouldn't return a warning.
1423 personal_data_.ClearCreditCards();
1424 GetAutofillSuggestions(form, field);
1425 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
1426 }
1427
1428 // Test that we correctly combine autofill and autocomplete suggestions.
1429 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1430 // Set up our form data.
1431 FormData form;
1432 CreateTestAddressFormData(&form);
1433 std::vector<FormData> forms(1, form);
1434 FormsSeen(forms);
1435
1436 const FormFieldData& field = form.fields[0];
1437 GetAutofillSuggestions(form, field);
1438
1439 // Add some Autocomplete suggestions.
1440 // This triggers the combined message send.
1441 std::vector<string16> suggestions;
1442 suggestions.push_back(ASCIIToUTF16("Jay"));
1443 // This suggestion is a duplicate, and should be trimmed.
1444 suggestions.push_back(ASCIIToUTF16("Elvis"));
1445 suggestions.push_back(ASCIIToUTF16("Jason"));
1446 AutocompleteSuggestionsReturned(suggestions);
1447
1448 // Test that we sent the right message to the renderer.
1449 int page_id = 0;
1450 std::vector<string16> values;
1451 std::vector<string16> labels;
1452 std::vector<string16> icons;
1453 std::vector<int> unique_ids;
1454 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1455 &unique_ids));
1456
1457 string16 expected_values[] = {
1458 ASCIIToUTF16("Elvis"),
1459 ASCIIToUTF16("Charles"),
1460 ASCIIToUTF16("Jay"),
1461 ASCIIToUTF16("Jason")
1462 };
1463 string16 expected_labels[] = {
1464 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1465 ASCIIToUTF16("123 Apple St."),
1466 string16(),
1467 string16()
1468 };
1469 string16 expected_icons[] = {string16(), string16(), string16(), string16()};
1470 int expected_unique_ids[] = {1, 2, 0, 0};
1471 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1472 kDefaultPageID, arraysize(expected_values), expected_values,
1473 expected_labels, expected_icons, expected_unique_ids);
1474 }
1475
1476 // Test that we return autocomplete-like suggestions when trying to autofill
1477 // already filled forms.
1478 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1479 // Set up our form data.
1480 FormData form;
1481 CreateTestAddressFormData(&form);
1482 std::vector<FormData> forms(1, form);
1483 FormsSeen(forms);
1484
1485 // Mark one of the fields as filled.
1486 form.fields[2].is_autofilled = true;
1487 const FormFieldData& field = form.fields[0];
1488 GetAutofillSuggestions(form, field);
1489
1490 // No suggestions provided, so send an empty vector as the results.
1491 // This triggers the combined message send.
1492 AutocompleteSuggestionsReturned(std::vector<string16>());
1493
1494 // Test that we sent the right message to the renderer.
1495 int page_id = 0;
1496 std::vector<string16> values;
1497 std::vector<string16> labels;
1498 std::vector<string16> icons;
1499 std::vector<int> unique_ids;
1500 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1501 &unique_ids));
1502 string16 expected_values[] = {
1503 ASCIIToUTF16("Elvis"),
1504 ASCIIToUTF16("Charles")
1505 };
1506 string16 expected_labels[] = {string16(), string16()};
1507 string16 expected_icons[] = {string16(), string16()};
1508 int expected_unique_ids[] = {1, 2};
1509 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1510 kDefaultPageID, arraysize(expected_values), expected_values,
1511 expected_labels, expected_icons, expected_unique_ids);
1512 }
1513
1514 // Test that nothing breaks when there are autocomplete suggestions but no
1515 // autofill suggestions.
1516 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1517 // Set up our form data.
1518 FormData form;
1519 CreateTestAddressFormData(&form);
1520 FormFieldData field;
1521 autofill_test::CreateTestFormField(
1522 "Some Field", "somefield", "", "text", &field);
1523 form.fields.push_back(field);
1524 std::vector<FormData> forms(1, form);
1525 FormsSeen(forms);
1526
1527 GetAutofillSuggestions(form, field);
1528
1529 // Add some Autocomplete suggestions.
1530 // This triggers the combined message send.
1531 std::vector<string16> suggestions;
1532 suggestions.push_back(ASCIIToUTF16("one"));
1533 suggestions.push_back(ASCIIToUTF16("two"));
1534 AutocompleteSuggestionsReturned(suggestions);
1535
1536 // Test that we sent the right message to the renderer.
1537 int page_id = 0;
1538 std::vector<string16> values;
1539 std::vector<string16> labels;
1540 std::vector<string16> icons;
1541 std::vector<int> unique_ids;
1542 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1543 &unique_ids));
1544
1545 string16 expected_values[] = {
1546 ASCIIToUTF16("one"),
1547 ASCIIToUTF16("two")
1548 };
1549 string16 expected_labels[] = {string16(), string16()};
1550 string16 expected_icons[] = {string16(), string16()};
1551 int expected_unique_ids[] = {0, 0};
1552 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1553 kDefaultPageID, arraysize(expected_values), expected_values,
1554 expected_labels, expected_icons, expected_unique_ids);
1555 }
1556
1557 // Test that we do not return duplicate values drawn from multiple profiles when
1558 // filling an already filled field.
1559 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1560 // Set up our form data.
1561 FormData form;
1562 CreateTestAddressFormData(&form);
1563 std::vector<FormData> forms(1, form);
1564 FormsSeen(forms);
1565
1566 // |profile| will be owned by the mock PersonalDataManager.
1567 AutofillProfile* profile = new AutofillProfile;
1568 autofill_test::SetProfileInfo(profile, "Elvis", "", "", "", "", "", "", "",
1569 "", "", "", "");
1570 profile->set_guid("00000000-0000-0000-0000-000000000101");
1571 autofill_manager_->AddProfile(profile);
1572
1573 FormFieldData& field = form.fields[0];
1574 field.is_autofilled = true;
1575 field.value = ASCIIToUTF16("Elvis");
1576 GetAutofillSuggestions(form, field);
1577
1578 // No suggestions provided, so send an empty vector as the results.
1579 // This triggers the combined message send.
1580 AutocompleteSuggestionsReturned(std::vector<string16>());
1581
1582 // Test that we sent the right message to the renderer.
1583 int page_id = 0;
1584 std::vector<string16> values;
1585 std::vector<string16> labels;
1586 std::vector<string16> icons;
1587 std::vector<int> unique_ids;
1588 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1589 &unique_ids));
1590
1591 string16 expected_values[] = { ASCIIToUTF16("Elvis") };
1592 string16 expected_labels[] = { string16() };
1593 string16 expected_icons[] = { string16() };
1594 int expected_unique_ids[] = { 1 };
1595 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1596 kDefaultPageID, arraysize(expected_values), expected_values,
1597 expected_labels, expected_icons, expected_unique_ids);
1598 }
1599
1600 // Test that a non-default value is suggested for multi-valued profile, on an
1601 // unfilled form.
1602 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileUnfilled) {
1603 // Set up our form data.
1604 FormData form;
1605 CreateTestAddressFormData(&form);
1606 std::vector<FormData> forms(1, form);
1607 FormsSeen(forms);
1608
1609 // |profile| will be owned by the mock PersonalDataManager.
1610 AutofillProfile* profile = new AutofillProfile;
1611 autofill_test::SetProfileInfo(profile, "Elvis", "", "Presley", "me@x.com", "",
1612 "", "", "", "", "", "", "");
1613 profile->set_guid("00000000-0000-0000-0000-000000000101");
1614 std::vector<string16> multi_values(2);
1615 multi_values[0] = ASCIIToUTF16("Elvis Presley");
1616 multi_values[1] = ASCIIToUTF16("Elena Love");
1617 profile->SetRawMultiInfo(NAME_FULL, multi_values);
1618 personal_data_.ClearAutofillProfiles();
1619 autofill_manager_->AddProfile(profile);
1620
1621 {
1622 // Get the first name field.
1623 // Start out with "E", hoping for either "Elvis" or "Elena.
1624 FormFieldData& field = form.fields[0];
1625 field.value = ASCIIToUTF16("E");
1626 field.is_autofilled = false;
1627 GetAutofillSuggestions(form, field);
1628
1629 // Trigger the |Send|.
1630 AutocompleteSuggestionsReturned(std::vector<string16>());
1631
1632 // Test that we sent the right message to the renderer.
1633 int page_id = 0;
1634 std::vector<string16> values;
1635 std::vector<string16> labels;
1636 std::vector<string16> icons;
1637 std::vector<int> unique_ids;
1638 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels,
1639 &icons, &unique_ids));
1640 string16 expected_values[] = {
1641 ASCIIToUTF16("Elvis"),
1642 ASCIIToUTF16("Elena")
1643 };
1644 string16 expected_labels[] = {
1645 ASCIIToUTF16("me@x.com"),
1646 ASCIIToUTF16("me@x.com")
1647 };
1648 string16 expected_icons[] = { string16(), string16() };
1649 int expected_unique_ids[] = { 1, 2 };
1650 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1651 kDefaultPageID, arraysize(expected_values),
1652 expected_values, expected_labels, expected_icons,
1653 expected_unique_ids);
1654 }
1655
1656 {
1657 // Get the first name field.
1658 // This time, start out with "Ele", hoping for "Elena".
1659 FormFieldData& field = form.fields[0];
1660 field.value = ASCIIToUTF16("Ele");
1661 field.is_autofilled = false;
1662 GetAutofillSuggestions(form, field);
1663
1664 // Trigger the |Send|.
1665 AutocompleteSuggestionsReturned(std::vector<string16>());
1666
1667 // Test that we sent the right message to the renderer.
1668 int page_id = 0;
1669 std::vector<string16> values;
1670 std::vector<string16> labels;
1671 std::vector<string16> icons;
1672 std::vector<int> unique_ids;
1673 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels,
1674 &icons, &unique_ids));
1675
1676 string16 expected_values[] = { ASCIIToUTF16("Elena") };
1677 string16 expected_labels[] = { ASCIIToUTF16("me@x.com") };
1678 string16 expected_icons[] = { string16() };
1679 int expected_unique_ids[] = { 2 };
1680 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1681 kDefaultPageID, arraysize(expected_values),
1682 expected_values, expected_labels, expected_icons,
1683 expected_unique_ids);
1684 }
1685 }
1686
1687 // Test that all values are suggested for multi-valued profile, on a filled
1688 // form. This is the per-field "override" case.
1689 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileFilled) {
1690 // Set up our form data.
1691 FormData form;
1692 CreateTestAddressFormData(&form);
1693 std::vector<FormData> forms(1, form);
1694 FormsSeen(forms);
1695
1696 // |profile| will be owned by the mock PersonalDataManager.
1697 AutofillProfile* profile = new AutofillProfile;
1698 profile->set_guid("00000000-0000-0000-0000-000000000102");
1699 std::vector<string16> multi_values(3);
1700 multi_values[0] = ASCIIToUTF16("Travis Smith");
1701 multi_values[1] = ASCIIToUTF16("Cynthia Love");
1702 multi_values[2] = ASCIIToUTF16("Zac Mango");
1703 profile->SetRawMultiInfo(NAME_FULL, multi_values);
1704 autofill_manager_->AddProfile(profile);
1705
1706 // Get the first name field. And start out with "Travis", hoping for all the
1707 // multi-valued variants as suggestions.
1708 FormFieldData& field = form.fields[0];
1709 field.value = ASCIIToUTF16("Travis");
1710 field.is_autofilled = true;
1711 GetAutofillSuggestions(form, field);
1712
1713 // Trigger the |Send|.
1714 AutocompleteSuggestionsReturned(std::vector<string16>());
1715
1716 // Test that we sent the right message to the renderer.
1717 int page_id = 0;
1718 std::vector<string16> values;
1719 std::vector<string16> labels;
1720 std::vector<string16> icons;
1721 std::vector<int> unique_ids;
1722 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1723 &unique_ids));
1724
1725 string16 expected_values[] = {
1726 ASCIIToUTF16("Travis"),
1727 ASCIIToUTF16("Cynthia"),
1728 ASCIIToUTF16("Zac")
1729 };
1730 string16 expected_labels[] = { string16(), string16(), string16() };
1731 string16 expected_icons[] = { string16(), string16(), string16() };
1732 int expected_unique_ids[] = { 1, 2, 3 };
1733 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1734 kDefaultPageID, arraysize(expected_values), expected_values,
1735 expected_labels, expected_icons, expected_unique_ids);
1736 }
1737
1738 TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
1739 // Set up our form data.
1740 FormData form;
1741 CreateTestAddressFormData(&form);
1742 std::vector<FormData> forms(1, form);
1743 FormsSeen(forms);
1744
1745 AutofillProfile* profile = new AutofillProfile;
1746 profile->set_guid("00000000-0000-0000-0000-000000000103");
1747 std::vector<string16> multi_values(1);
1748 multi_values[0] = ASCIIToUTF16("Natty Bumppo");
1749 profile->SetRawMultiInfo(NAME_FULL, multi_values);
1750 multi_values[0] = ASCIIToUTF16("1800PRAIRIE");
1751 profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, multi_values);
1752 autofill_manager_->AddProfile(profile);
1753
1754 const FormFieldData& field = form.fields[9];
1755 GetAutofillSuggestions(form, field);
1756
1757 // No suggestions provided, so send an empty vector as the results.
1758 // This triggers the combined message send.
1759 AutocompleteSuggestionsReturned(std::vector<string16>());
1760
1761 // Test that we sent the right message to the renderer.
1762 int page_id = 0;
1763 std::vector<string16> values;
1764 std::vector<string16> labels;
1765 std::vector<string16> icons;
1766 std::vector<int> unique_ids;
1767 GetAutofillSuggestionsMessage(
1768 &page_id, &values, &labels, &icons, &unique_ids);
1769
1770 string16 expected_values[] = {
1771 ASCIIToUTF16("12345678901"),
1772 ASCIIToUTF16("23456789012"),
1773 ASCIIToUTF16("18007724743"), // 1800PRAIRIE
1774 };
1775 // Inferred labels include full first relevant field, which in this case is
1776 // the address line 1.
1777 string16 expected_labels[] = {
1778 ASCIIToUTF16("Elvis Aaron Presley"),
1779 ASCIIToUTF16("Charles Hardin Holley"),
1780 ASCIIToUTF16("Natty Bumppo"),
1781 };
1782 string16 expected_icons[] = {string16(), string16(), string16()};
1783 int expected_unique_ids[] = {1, 2, 3};
1784 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1785 kDefaultPageID, arraysize(expected_values), expected_values,
1786 expected_labels, expected_icons, expected_unique_ids);
1787 }
1788
1789 // Test that we correctly fill an address form.
1790 TEST_F(AutofillManagerTest, FillAddressForm) {
1791 // Set up our form data.
1792 FormData form;
1793 CreateTestAddressFormData(&form);
1794 std::vector<FormData> forms(1, form);
1795 FormsSeen(forms);
1796
1797 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1798 GUIDPair empty(std::string(), 0);
1799 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1800 PackGUIDs(empty, guid));
1801
1802 int page_id = 0;
1803 FormData results;
1804 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1805 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1806 }
1807
1808 // Test that we correctly fill an address form from an auxiliary profile.
1809 TEST_F(AutofillManagerTest, FillAddressFormFromAuxiliaryProfile) {
1810 personal_data_.ClearAutofillProfiles();
1811 PrefService* prefs = components::UserPrefs::Get(profile());
1812 prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, true);
1813 personal_data_.CreateTestAuxiliaryProfiles();
1814
1815 // Set up our form data.
1816 FormData form;
1817 CreateTestAddressFormData(&form);
1818 std::vector<FormData> forms(1, form);
1819 FormsSeen(forms);
1820
1821 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1822 GUIDPair empty(std::string(), 0);
1823 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1824 PackGUIDs(empty, guid));
1825
1826 int page_id = 0;
1827 FormData results;
1828 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1829 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1830 }
1831
1832 // Test that we correctly fill a credit card form.
1833 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1834 // Set up our form data.
1835 FormData form;
1836 CreateTestCreditCardFormData(&form, true, false);
1837 std::vector<FormData> forms(1, form);
1838 FormsSeen(forms);
1839
1840 GUIDPair guid("00000000-0000-0000-0000-000000000004", 0);
1841 GUIDPair empty(std::string(), 0);
1842 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
1843 PackGUIDs(guid, empty));
1844
1845 int page_id = 0;
1846 FormData results;
1847 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1848 ExpectFilledCreditCardFormElvis(page_id, results, kDefaultPageID, false);
1849 }
1850
1851 // Test that we correctly fill a credit card form with month input type.
1852 // 1. year empty, month empty
1853 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1854 // Same as the SetUp(), but generate 4 credit cards with year month
1855 // combination.
1856 personal_data_.CreateTestCreditCardsYearAndMonth("", "");
1857 // Set up our form data.
1858 FormData form;
1859 CreateTestCreditCardFormData(&form, true, true);
1860 std::vector<FormData> forms(1, form);
1861 FormsSeen(forms);
1862
1863 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1864 GUIDPair empty(std::string(), 0);
1865 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
1866 PackGUIDs(guid, empty));
1867
1868 int page_id = 0;
1869 FormData results;
1870 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1871 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1872 kDefaultPageID, false, "", "");
1873 }
1874
1875
1876 // Test that we correctly fill a credit card form with month input type.
1877 // 2. year empty, month non-empty
1878 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1879 // Same as the SetUp(), but generate 4 credit cards with year month
1880 // combination.
1881 personal_data_.CreateTestCreditCardsYearAndMonth("", "04");
1882 // Set up our form data.
1883 FormData form;
1884 CreateTestCreditCardFormData(&form, true, true);
1885 std::vector<FormData> forms(1, form);
1886 FormsSeen(forms);
1887
1888 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1889 GUIDPair empty(std::string(), 0);
1890 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
1891 PackGUIDs(guid, empty));
1892
1893 int page_id = 0;
1894 FormData results;
1895 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1896 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1897 kDefaultPageID, false, "", "04");
1898 }
1899
1900 // Test that we correctly fill a credit card form with month input type.
1901 // 3. year non-empty, month empty
1902 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1903 // Same as the SetUp(), but generate 4 credit cards with year month
1904 // combination.
1905 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "");
1906 // Set up our form data.
1907 FormData form;
1908 CreateTestCreditCardFormData(&form, true, true);
1909 std::vector<FormData> forms(1, form);
1910 FormsSeen(forms);
1911
1912 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1913 GUIDPair empty(std::string(), 0);
1914 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
1915 PackGUIDs(guid, empty));
1916
1917 int page_id = 0;
1918 FormData results;
1919 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1920 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1921 kDefaultPageID, false, "2012", "");
1922 }
1923
1924 // Test that we correctly fill a credit card form with month input type.
1925 // 4. year non-empty, month empty
1926 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1927 // Same as the SetUp(), but generate 4 credit cards with year month
1928 // combination.
1929 personal_data_.ClearCreditCards();
1930 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
1931 // Set up our form data.
1932 FormData form;
1933 CreateTestCreditCardFormData(&form, true, true);
1934 std::vector<FormData> forms(1, form);
1935 FormsSeen(forms);
1936
1937 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1938 GUIDPair empty(std::string(), 0);
1939 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
1940 PackGUIDs(guid, empty));
1941
1942 int page_id = 0;
1943 FormData results;
1944 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1945 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1946 kDefaultPageID, false, "2012", "04");
1947 }
1948
1949 // Test that we correctly fill a combined address and credit card form.
1950 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1951 // Set up our form data.
1952 FormData form;
1953 CreateTestAddressFormData(&form);
1954 CreateTestCreditCardFormData(&form, true, false);
1955 std::vector<FormData> forms(1, form);
1956 FormsSeen(forms);
1957
1958 // First fill the address data.
1959 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1960 GUIDPair empty(std::string(), 0);
1961 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1962 PackGUIDs(empty, guid));
1963
1964 int page_id = 0;
1965 FormData results;
1966 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1967 {
1968 SCOPED_TRACE("Address");
1969 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, true);
1970 }
1971
1972 // Now fill the credit card data.
1973 const int kPageID2 = 2;
1974 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
1975 FillAutofillFormData(kPageID2, form, form.fields.back(),
1976 PackGUIDs(guid2, empty));
1977
1978 page_id = 0;
1979 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1980 {
1981 SCOPED_TRACE("Credit card");
1982 ExpectFilledCreditCardFormElvis(page_id, results, kPageID2, true);
1983 }
1984 }
1985
1986 // Test that we correctly fill a form that has multiple logical sections, e.g.
1987 // both a billing and a shipping address.
1988 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1989 // Set up our form data.
1990 FormData form;
1991 CreateTestAddressFormData(&form);
1992 const size_t kAddressFormSize = form.fields.size();
1993 CreateTestAddressFormData(&form);
1994 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1995 // Make sure the fields have distinct names.
1996 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1997 }
1998 std::vector<FormData> forms(1, form);
1999 FormsSeen(forms);
2000
2001 // Fill the first section.
2002 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2003 GUIDPair empty(std::string(), 0);
2004 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2005 PackGUIDs(empty, guid));
2006
2007 int page_id = 0;
2008 FormData results;
2009 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2010 {
2011 SCOPED_TRACE("Address 1");
2012
2013 // The second address section should be empty.
2014 ASSERT_EQ(results.fields.size(), 2*kAddressFormSize);
2015 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
2016 EXPECT_EQ(string16(), results.fields[i].value);
2017 }
2018
2019 // The first address section should be filled with Elvis's data.
2020 results.fields.resize(kAddressFormSize);
2021 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2022 }
2023
2024 // Fill the second section, with the initiating field somewhere in the middle
2025 // of the section.
2026 const int kPageID2 = 2;
2027 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0);
2028 ASSERT_LT(9U, kAddressFormSize);
2029 FillAutofillFormData(kPageID2, form, form.fields[kAddressFormSize + 9],
2030 PackGUIDs(empty, guid2));
2031
2032 page_id = 0;
2033 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2034 {
2035 SCOPED_TRACE("Address 2");
2036 ASSERT_EQ(results.fields.size(), form.fields.size());
2037
2038 // The first address section should be empty.
2039 ASSERT_EQ(results.fields.size(), 2*kAddressFormSize);
2040 for (size_t i = 0; i < kAddressFormSize; ++i) {
2041 EXPECT_EQ(string16(), results.fields[i].value);
2042 }
2043
2044 // The second address section should be filled with Elvis's data.
2045 FormData secondSection = results;
2046 secondSection.fields.erase(secondSection.fields.begin(),
2047 secondSection.fields.begin() + kAddressFormSize);
2048 for (size_t i = 0; i < kAddressFormSize; ++i) {
2049 // Restore the expected field names.
2050 string16 name = secondSection.fields[i].name;
2051 string16 original_name = name.substr(0, name.size() - 1);
2052 secondSection.fields[i].name = original_name;
2053 }
2054 ExpectFilledAddressFormElvis(page_id, secondSection, kPageID2, false);
2055 }
2056 }
2057
2058 // Test that we correctly fill a form that has author-specified sections, which
2059 // might not match our expected section breakdown.
2060 TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) {
2061 // Create a form with a billing section and an unnamed section, interleaved.
2062 // The billing section includes both address and credit card fields.
2063 FormData form;
2064 form.name = ASCIIToUTF16("MyForm");
2065 form.method = ASCIIToUTF16("POST");
2066 form.origin = GURL("https://myform.com/form.html");
2067 form.action = GURL("https://myform.com/submit.html");
2068 form.user_submitted = true;
2069
2070 FormFieldData field;
2071
2072 autofill_test::CreateTestFormField("", "country", "", "text", &field);
2073 field.autocomplete_attribute = "section-billing country";
2074 form.fields.push_back(field);
2075
2076 autofill_test::CreateTestFormField("", "firstname", "", "text", &field);
2077 field.autocomplete_attribute = "given-name";
2078 form.fields.push_back(field);
2079
2080 autofill_test::CreateTestFormField("", "lastname", "", "text", &field);
2081 field.autocomplete_attribute = "family-name";
2082 form.fields.push_back(field);
2083
2084 autofill_test::CreateTestFormField("", "address", "", "text", &field);
2085 field.autocomplete_attribute = "section-billing street-address";
2086 form.fields.push_back(field);
2087
2088 autofill_test::CreateTestFormField("", "city", "", "text", &field);
2089 field.autocomplete_attribute = "section-billing locality";
2090 form.fields.push_back(field);
2091
2092 autofill_test::CreateTestFormField("", "state", "", "text", &field);
2093 field.autocomplete_attribute = "section-billing region";
2094 form.fields.push_back(field);
2095
2096 autofill_test::CreateTestFormField("", "zip", "", "text", &field);
2097 field.autocomplete_attribute = "section-billing postal-code";
2098 form.fields.push_back(field);
2099
2100 autofill_test::CreateTestFormField("", "ccname", "", "text", &field);
2101 field.autocomplete_attribute = "section-billing cc-name";
2102 form.fields.push_back(field);
2103
2104 autofill_test::CreateTestFormField("", "ccnumber", "", "text", &field);
2105 field.autocomplete_attribute = "section-billing cc-number";
2106 form.fields.push_back(field);
2107
2108 autofill_test::CreateTestFormField("", "ccexp", "", "text", &field);
2109 field.autocomplete_attribute = "section-billing cc-exp";
2110 form.fields.push_back(field);
2111
2112 autofill_test::CreateTestFormField("", "email", "", "text", &field);
2113 field.autocomplete_attribute = "email";
2114 form.fields.push_back(field);
2115
2116 std::vector<FormData> forms(1, form);
2117 FormsSeen(forms);
2118
2119 // Fill the unnamed section.
2120 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2121 GUIDPair empty(std::string(), 0);
2122 FillAutofillFormData(kDefaultPageID, form, form.fields[1],
2123 PackGUIDs(empty, guid));
2124
2125 int page_id = 0;
2126 FormData results;
2127 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2128 {
2129 SCOPED_TRACE("Unnamed section");
2130 EXPECT_EQ(kDefaultPageID, page_id);
2131 EXPECT_EQ(ASCIIToUTF16("MyForm"), results.name);
2132 EXPECT_EQ(ASCIIToUTF16("POST"), results.method);
2133 EXPECT_EQ(GURL("https://myform.com/form.html"), results.origin);
2134 EXPECT_EQ(GURL("https://myform.com/submit.html"), results.action);
2135 EXPECT_TRUE(results.user_submitted);
2136 ASSERT_EQ(11U, results.fields.size());
2137
2138 ExpectFilledField("", "country", "", "text", results.fields[0]);
2139 ExpectFilledField("", "firstname", "Elvis", "text", results.fields[1]);
2140 ExpectFilledField("", "lastname", "Presley", "text", results.fields[2]);
2141 ExpectFilledField("", "address", "", "text", results.fields[3]);
2142 ExpectFilledField("", "city", "", "text", results.fields[4]);
2143 ExpectFilledField("", "state", "", "text", results.fields[5]);
2144 ExpectFilledField("", "zip", "", "text", results.fields[6]);
2145 ExpectFilledField("", "ccname", "", "text", results.fields[7]);
2146 ExpectFilledField("", "ccnumber", "", "text", results.fields[8]);
2147 ExpectFilledField("", "ccexp", "", "text", results.fields[9]);
2148 ExpectFilledField("", "email", "theking@gmail.com", "text",
2149 results.fields[10]);
2150 }
2151
2152 // Fill the address portion of the billing section.
2153 const int kPageID2 = 2;
2154 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0);
2155 FillAutofillFormData(kPageID2, form, form.fields[0],
2156 PackGUIDs(empty, guid2));
2157
2158 page_id = 0;
2159 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2160 {
2161 SCOPED_TRACE("Billing address");
2162 EXPECT_EQ(kPageID2, page_id);
2163 EXPECT_EQ(ASCIIToUTF16("MyForm"), results.name);
2164 EXPECT_EQ(ASCIIToUTF16("POST"), results.method);
2165 EXPECT_EQ(GURL("https://myform.com/form.html"), results.origin);
2166 EXPECT_EQ(GURL("https://myform.com/submit.html"), results.action);
2167 EXPECT_TRUE(results.user_submitted);
2168 ASSERT_EQ(11U, results.fields.size());
2169
2170 ExpectFilledField("", "country", "United States", "text",
2171 results.fields[0]);
2172 ExpectFilledField("", "firstname", "", "text", results.fields[1]);
2173 ExpectFilledField("", "lastname", "", "text", results.fields[2]);
2174 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text",
2175 results.fields[3]);
2176 ExpectFilledField("", "city", "Memphis", "text", results.fields[4]);
2177 ExpectFilledField("", "state", "Tennessee", "text", results.fields[5]);
2178 ExpectFilledField("", "zip", "38116", "text", results.fields[6]);
2179 ExpectFilledField("", "ccname", "", "text", results.fields[7]);
2180 ExpectFilledField("", "ccnumber", "", "text", results.fields[8]);
2181 ExpectFilledField("", "ccexp", "", "text", results.fields[9]);
2182 ExpectFilledField("", "email", "", "text", results.fields[10]);
2183 }
2184
2185 // Fill the credit card portion of the billing section.
2186 const int kPageID3 = 3;
2187 GUIDPair guid3("00000000-0000-0000-0000-000000000004", 0);
2188 FillAutofillFormData(kPageID3, form, form.fields[form.fields.size() - 2],
2189 PackGUIDs(guid3, empty));
2190
2191 page_id = 0;
2192 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2193 {
2194 SCOPED_TRACE("Credit card");
2195 EXPECT_EQ(kPageID3, page_id);
2196 EXPECT_EQ(ASCIIToUTF16("MyForm"), results.name);
2197 EXPECT_EQ(ASCIIToUTF16("POST"), results.method);
2198 EXPECT_EQ(GURL("https://myform.com/form.html"), results.origin);
2199 EXPECT_EQ(GURL("https://myform.com/submit.html"), results.action);
2200 EXPECT_TRUE(results.user_submitted);
2201 ASSERT_EQ(11U, results.fields.size());
2202
2203 ExpectFilledField("", "country", "", "text", results.fields[0]);
2204 ExpectFilledField("", "firstname", "", "text", results.fields[1]);
2205 ExpectFilledField("", "lastname", "", "text", results.fields[2]);
2206 ExpectFilledField("", "address", "", "text", results.fields[3]);
2207 ExpectFilledField("", "city", "", "text", results.fields[4]);
2208 ExpectFilledField("", "state", "", "text", results.fields[5]);
2209 ExpectFilledField("", "zip", "", "text", results.fields[6]);
2210 ExpectFilledField("", "ccname", "Elvis Presley", "text", results.fields[7]);
2211 ExpectFilledField("", "ccnumber", "4234567890123456", "text",
2212 results.fields[8]);
2213 ExpectFilledField("", "ccexp", "04/2012", "text", results.fields[9]);
2214 ExpectFilledField("", "email", "", "text", results.fields[10]);
2215 }
2216 }
2217
2218 // Test that we correctly fill a form that has a single logical section with
2219 // multiple email address fields.
2220 TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) {
2221 // Set up our form data.
2222 FormData form;
2223 CreateTestAddressFormData(&form);
2224 FormFieldData field;
2225 autofill_test::CreateTestFormField(
2226 "Confirm email", "email2", "", "text", &field);
2227 form.fields.push_back(field);
2228
2229 std::vector<FormData> forms(1, form);
2230 FormsSeen(forms);
2231
2232 // Fill the form.
2233 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2234 GUIDPair empty(std::string(), 0);
2235 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2236 PackGUIDs(empty, guid));
2237
2238 int page_id = 0;
2239 FormData results;
2240 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2241
2242 // The second email address should be filled.
2243 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"), results.fields.back().value);
2244
2245 // The remainder of the form should be filled as usual.
2246 results.fields.pop_back();
2247 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2248 }
2249
2250 // Test that we correctly fill a previously auto-filled form.
2251 TEST_F(AutofillManagerTest, FillAutofilledForm) {
2252 // Set up our form data.
2253 FormData form;
2254 CreateTestAddressFormData(&form);
2255 // Mark one of the address fields as autofilled.
2256 form.fields[4].is_autofilled = true;
2257 CreateTestCreditCardFormData(&form, true, false);
2258 std::vector<FormData> forms(1, form);
2259 FormsSeen(forms);
2260
2261 // First fill the address data.
2262 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2263 GUIDPair empty(std::string(), 0);
2264 FillAutofillFormData(kDefaultPageID, form, *form.fields.begin(),
2265 PackGUIDs(empty, guid));
2266
2267 int page_id = 0;
2268 FormData results;
2269 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2270 {
2271 SCOPED_TRACE("Address");
2272 ExpectFilledForm(page_id, results, kDefaultPageID,
2273 "Elvis", "", "", "", "", "", "", "", "", "", "", "", "",
2274 "", "", true, true, false);
2275 }
2276
2277 // Now fill the credit card data.
2278 const int kPageID2 = 2;
2279 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
2280 FillAutofillFormData(kPageID2, form, form.fields.back(),
2281 PackGUIDs(guid2, empty));
2282
2283 page_id = 0;
2284 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2285 {
2286 SCOPED_TRACE("Credit card 1");
2287 ExpectFilledCreditCardFormElvis(page_id, results, kPageID2, true);
2288 }
2289
2290 // Now set the credit card fields to also be auto-filled, and try again to
2291 // fill the credit card data
2292 for (std::vector<FormFieldData>::iterator iter = form.fields.begin();
2293 iter != form.fields.end();
2294 ++iter) {
2295 iter->is_autofilled = true;
2296 }
2297
2298 const int kPageID3 = 3;
2299 FillAutofillFormData(kPageID3, form, *form.fields.rbegin(),
2300 PackGUIDs(guid2, empty));
2301
2302 page_id = 0;
2303 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2304 {
2305 SCOPED_TRACE("Credit card 2");
2306 ExpectFilledForm(page_id, results, kPageID3,
2307 "", "", "", "", "", "", "", "", "", "", "", "", "", "",
2308 "2012", true, true, false);
2309 }
2310 }
2311
2312 // Test that we correctly fill an address form with a non-default variant for a
2313 // multi-valued field.
2314 TEST_F(AutofillManagerTest, FillAddressFormWithVariantType) {
2315 // Set up our form data.
2316 FormData form;
2317 CreateTestAddressFormData(&form);
2318 std::vector<FormData> forms(1, form);
2319 FormsSeen(forms);
2320
2321 // Add a name variant to the Elvis profile.
2322 AutofillProfile* profile = autofill_manager_->GetProfileWithGUID(
2323 "00000000-0000-0000-0000-000000000001");
2324 const string16 elvis_name = profile->GetRawInfo(NAME_FULL);
2325
2326 std::vector<string16> name_variants;
2327 name_variants.push_back(ASCIIToUTF16("Some Other Guy"));
2328 name_variants.push_back(elvis_name);
2329 profile->SetRawMultiInfo(NAME_FULL, name_variants);
2330
2331 GUIDPair guid(profile->guid(), 1);
2332 GUIDPair empty(std::string(), 0);
2333 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2334 PackGUIDs(empty, guid));
2335
2336 int page_id = 0;
2337 FormData results1;
2338 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results1));
2339 {
2340 SCOPED_TRACE("Valid variant");
2341 ExpectFilledAddressFormElvis(page_id, results1, kDefaultPageID, false);
2342 }
2343
2344 // Try filling with a variant that doesn't exist. The fields to which this
2345 // variant would normally apply should not be filled.
2346 const int kPageID2 = 2;
2347 GUIDPair guid2(profile->guid(), 2);
2348 FillAutofillFormData(kPageID2, form, form.fields[0],
2349 PackGUIDs(empty, guid2));
2350
2351 page_id = 0;
2352 FormData results2;
2353 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results2));
2354 {
2355 SCOPED_TRACE("Invalid variant");
2356 ExpectFilledForm(page_id, results2, kPageID2, "", "", "",
2357 "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
2358 "Tennessee", "38116", "United States", "12345678901",
2359 "theking@gmail.com", "", "", "", "", true, false, false);
2360 }
2361 }
2362
2363 // Test that we correctly fill a phone number split across multiple fields.
2364 TEST_F(AutofillManagerTest, FillPhoneNumber) {
2365 // In one form, rely on the maxlength attribute to imply phone number parts.
2366 // In the other form, rely on the autocompletetype attribute.
2367 FormData form_with_maxlength;
2368 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm");
2369 form_with_maxlength.method = ASCIIToUTF16("POST");
2370 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html");
2371 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html");
2372 form_with_maxlength.user_submitted = true;
2373 FormData form_with_autocompletetype = form_with_maxlength;
2374 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm");
2375
2376 struct {
2377 const char* label;
2378 const char* name;
2379 size_t max_length;
2380 const char* autocomplete_attribute;
2381 } test_fields[] = {
2382 { "country code", "country_code", 1, "tel-country-code" },
2383 { "area code", "area_code", 3, "tel-area-code" },
2384 { "phone", "phone_prefix", 3, "tel-local-prefix" },
2385 { "-", "phone_suffix", 4, "tel-local-suffix" },
2386 { "Phone Extension", "ext", 3, "tel-extension" }
2387 };
2388
2389 FormFieldData field;
2390 const size_t default_max_length = field.max_length;
2391 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_fields); ++i) {
2392 autofill_test::CreateTestFormField(
2393 test_fields[i].label, test_fields[i].name, "", "text", &field);
2394 field.max_length = test_fields[i].max_length;
2395 field.autocomplete_attribute = std::string();
2396 form_with_maxlength.fields.push_back(field);
2397
2398 field.max_length = default_max_length;
2399 field.autocomplete_attribute = test_fields[i].autocomplete_attribute;
2400 form_with_autocompletetype.fields.push_back(field);
2401 }
2402
2403 std::vector<FormData> forms;
2404 forms.push_back(form_with_maxlength);
2405 forms.push_back(form_with_autocompletetype);
2406 FormsSeen(forms);
2407
2408 // We should be able to fill prefix and suffix fields for US numbers.
2409 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID(
2410 "00000000-0000-0000-0000-000000000002");
2411 ASSERT_TRUE(work_profile != NULL);
2412 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2413 ASCIIToUTF16("16505554567"));
2414
2415 GUIDPair guid(work_profile->guid(), 0);
2416 GUIDPair empty(std::string(), 0);
2417
2418 int page_id = 1;
2419 FillAutofillFormData(page_id, form_with_maxlength,
2420 *form_with_maxlength.fields.begin(),
2421 PackGUIDs(empty, guid));
2422 page_id = 0;
2423 FormData results1;
2424 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results1));
2425 EXPECT_EQ(1, page_id);
2426
2427 ASSERT_EQ(5U, results1.fields.size());
2428 EXPECT_EQ(ASCIIToUTF16("1"), results1.fields[0].value);
2429 EXPECT_EQ(ASCIIToUTF16("650"), results1.fields[1].value);
2430 EXPECT_EQ(ASCIIToUTF16("555"), results1.fields[2].value);
2431 EXPECT_EQ(ASCIIToUTF16("4567"), results1.fields[3].value);
2432 EXPECT_EQ(string16(), results1.fields[4].value);
2433
2434 page_id = 2;
2435 FillAutofillFormData(page_id, form_with_autocompletetype,
2436 *form_with_autocompletetype.fields.begin(),
2437 PackGUIDs(empty, guid));
2438 page_id = 0;
2439 FormData results2;
2440 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results2));
2441 EXPECT_EQ(2, page_id);
2442
2443 ASSERT_EQ(5U, results2.fields.size());
2444 EXPECT_EQ(ASCIIToUTF16("1"), results2.fields[0].value);
2445 EXPECT_EQ(ASCIIToUTF16("650"), results2.fields[1].value);
2446 EXPECT_EQ(ASCIIToUTF16("555"), results2.fields[2].value);
2447 EXPECT_EQ(ASCIIToUTF16("4567"), results2.fields[3].value);
2448 EXPECT_EQ(string16(), results2.fields[4].value);
2449
2450 // We should not be able to fill prefix and suffix fields for international
2451 // numbers.
2452 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
2453 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2454 ASCIIToUTF16("447700954321"));
2455 page_id = 3;
2456 FillAutofillFormData(page_id, form_with_maxlength,
2457 *form_with_maxlength.fields.begin(),
2458 PackGUIDs(empty, guid));
2459 page_id = 0;
2460 FormData results3;
2461 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results3));
2462 EXPECT_EQ(3, page_id);
2463
2464 ASSERT_EQ(5U, results3.fields.size());
2465 EXPECT_EQ(ASCIIToUTF16("44"), results3.fields[0].value);
2466 EXPECT_EQ(ASCIIToUTF16("7700"), results3.fields[1].value);
2467 EXPECT_EQ(ASCIIToUTF16("954321"), results3.fields[2].value);
2468 EXPECT_EQ(ASCIIToUTF16("954321"), results3.fields[3].value);
2469 EXPECT_EQ(string16(), results3.fields[4].value);
2470
2471 page_id = 4;
2472 FillAutofillFormData(page_id, form_with_autocompletetype,
2473 *form_with_autocompletetype.fields.begin(),
2474 PackGUIDs(empty, guid));
2475 page_id = 0;
2476 FormData results4;
2477 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results4));
2478 EXPECT_EQ(4, page_id);
2479
2480 ASSERT_EQ(5U, results4.fields.size());
2481 EXPECT_EQ(ASCIIToUTF16("44"), results4.fields[0].value);
2482 EXPECT_EQ(ASCIIToUTF16("7700"), results4.fields[1].value);
2483 EXPECT_EQ(ASCIIToUTF16("954321"), results4.fields[2].value);
2484 EXPECT_EQ(ASCIIToUTF16("954321"), results4.fields[3].value);
2485 EXPECT_EQ(string16(), results4.fields[4].value);
2486
2487 // We should fill all phone fields with the same phone number variant.
2488 std::vector<string16> phone_variants;
2489 phone_variants.push_back(ASCIIToUTF16("16505554567"));
2490 phone_variants.push_back(ASCIIToUTF16("18887771234"));
2491 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
2492 work_profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_variants);
2493
2494 page_id = 5;
2495 GUIDPair variant_guid(work_profile->guid(), 1);
2496 FillAutofillFormData(page_id, form_with_maxlength,
2497 *form_with_maxlength.fields.begin(),
2498 PackGUIDs(empty, variant_guid));
2499 page_id = 0;
2500 FormData results5;
2501 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results5));
2502 EXPECT_EQ(5, page_id);
2503
2504 ASSERT_EQ(5U, results5.fields.size());
2505 EXPECT_EQ(ASCIIToUTF16("1"), results5.fields[0].value);
2506 EXPECT_EQ(ASCIIToUTF16("888"), results5.fields[1].value);
2507 EXPECT_EQ(ASCIIToUTF16("777"), results5.fields[2].value);
2508 EXPECT_EQ(ASCIIToUTF16("1234"), results5.fields[3].value);
2509 EXPECT_EQ(string16(), results5.fields[4].value);
2510 }
2511
2512 // Test that we can still fill a form when a field has been removed from it.
2513 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
2514 // Set up our form data.
2515 FormData form;
2516 CreateTestAddressFormData(&form);
2517
2518 // Add a field -- we'll remove it again later.
2519 FormFieldData field;
2520 autofill_test::CreateTestFormField("Some", "field", "", "text", &field);
2521 form.fields.insert(form.fields.begin() + 3, field);
2522
2523 std::vector<FormData> forms(1, form);
2524 FormsSeen(forms);
2525
2526 // Now, after the call to |FormsSeen|, we remove the field before filling.
2527 form.fields.erase(form.fields.begin() + 3);
2528
2529 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2530 GUIDPair empty(std::string(), 0);
2531 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2532 PackGUIDs(empty, guid));
2533
2534 int page_id = 0;
2535 FormData results;
2536 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2537 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2538 }
2539
2540 // Test that we can still fill a form when a field has been added to it.
2541 TEST_F(AutofillManagerTest, FormChangesAddField) {
2542 // The offset of the phone field in the address form.
2543 const int kPhoneFieldOffset = 9;
2544
2545 // Set up our form data.
2546 FormData form;
2547 CreateTestAddressFormData(&form);
2548
2549 // Remove the phone field -- we'll add it back later.
2550 std::vector<FormFieldData>::iterator pos =
2551 form.fields.begin() + kPhoneFieldOffset;
2552 FormFieldData field = *pos;
2553 pos = form.fields.erase(pos);
2554
2555 std::vector<FormData> forms(1, form);
2556 FormsSeen(forms);
2557
2558 // Now, after the call to |FormsSeen|, we restore the field before filling.
2559 form.fields.insert(pos, field);
2560
2561 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2562 GUIDPair empty(std::string(), 0);
2563 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2564 PackGUIDs(empty, guid));
2565
2566 int page_id = 0;
2567 FormData results;
2568 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2569 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2570 }
2571
2572 // Test that we are able to save form data when forms are submitted.
2573 TEST_F(AutofillManagerTest, FormSubmitted) {
2574 // Set up our form data.
2575 FormData form;
2576 CreateTestAddressFormData(&form);
2577 std::vector<FormData> forms(1, form);
2578 FormsSeen(forms);
2579
2580 // Fill the form.
2581 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2582 GUIDPair empty(std::string(), 0);
2583 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2584 PackGUIDs(empty, guid));
2585
2586 int page_id = 0;
2587 FormData results;
2588 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2589 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2590
2591 // Simulate form submission. We should call into the PDM to try to save the
2592 // filled data.
2593 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2594 FormSubmitted(results);
2595 }
2596
2597 // Test that we are able to save form data when forms are submitted and we only
2598 // have server data for the field types.
2599 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2600 // Set up our form data.
2601 FormData form;
2602 CreateTestAddressFormData(&form);
2603
2604 // Simulate having seen this form on page load.
2605 // |form_structure| will be owned by |autofill_manager_|.
2606 TestFormStructure* form_structure = new TestFormStructure(form);
2607 AutofillMetrics metrics_logger; // ignored
2608 form_structure->DetermineHeuristicTypes(metrics_logger);
2609
2610 // Clear the heuristic types, and instead set the appropriate server types.
2611 std::vector<AutofillFieldType> heuristic_types, server_types;
2612 for (size_t i = 0; i < form.fields.size(); ++i) {
2613 heuristic_types.push_back(UNKNOWN_TYPE);
2614 server_types.push_back(form_structure->field(i)->type());
2615 }
2616 form_structure->SetFieldTypes(heuristic_types, server_types);
2617 autofill_manager_->AddSeenForm(form_structure);
2618
2619 // Fill the form.
2620 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2621 GUIDPair empty(std::string(), 0);
2622 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2623 PackGUIDs(empty, guid));
2624
2625 int page_id = 0;
2626 FormData results;
2627 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2628 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2629
2630 // Simulate form submission. We should call into the PDM to try to save the
2631 // filled data.
2632 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2633 FormSubmitted(results);
2634 }
2635
2636 // Test that the form signature for an uploaded form always matches the form
2637 // signature from the query.
2638 TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) {
2639 // Set up our form data.
2640 FormData form;
2641 CreateTestAddressFormData(&form);
2642 std::vector<FormData> forms(1, form);
2643 FormsSeen(forms);
2644
2645 // Cache the expected form signature.
2646 std::string signature = FormStructure(form, std::string()).FormSignature();
2647
2648 // Change the structure of the form prior to submission.
2649 // Websites would typically invoke JavaScript either on page load or on form
2650 // submit to achieve this.
2651 form.fields.pop_back();
2652 FormFieldData field = form.fields[3];
2653 form.fields[3] = form.fields[7];
2654 form.fields[7] = field;
2655
2656 // Simulate form submission.
2657 FormSubmitted(form);
2658 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
2659 }
2660
2661 // Test that we do not save form data when submitted fields contain default
2662 // values.
2663 TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) {
2664 // Set up our form data.
2665 FormData form;
2666 CreateTestAddressFormData(&form);
2667 form.fields[3].value = ASCIIToUTF16("Enter your address");
2668
2669 // Convert the state field to a <select> popup, to make sure that we only
2670 // reject default values for text fields.
2671 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state"));
2672 form.fields[6].form_control_type = "select-one";
2673 form.fields[6].value = ASCIIToUTF16("Tennessee");
2674
2675 std::vector<FormData> forms(1, form);
2676 FormsSeen(forms);
2677
2678 // Fill the form.
2679 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2680 GUIDPair empty(std::string(), 0);
2681 FillAutofillFormData(kDefaultPageID, form, form.fields[3],
2682 PackGUIDs(empty, guid));
2683
2684 int page_id = 0;
2685 FormData results;
2686 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2687
2688 // Simulate form submission. We should call into the PDM to try to save the
2689 // filled data.
2690 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2691 FormSubmitted(results);
2692
2693 // Set the address field's value back to the default value.
2694 results.fields[3].value = ASCIIToUTF16("Enter your address");
2695
2696 // Simulate form submission. We should not call into the PDM to try to save
2697 // the filled data, since the filled form is effectively missing an address.
2698 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(0);
2699 FormSubmitted(results);
2700 }
2701
2702 // Checks that resetting the auxiliary profile enabled preference does the right
2703 // thing on all platforms.
2704 TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) {
2705 PrefService* prefs = components::UserPrefs::Get(profile());
2706 #if defined(OS_MACOSX)
2707 // Auxiliary profiles is implemented on Mac only. It enables Mac Address
2708 // Book integration.
2709 ASSERT_TRUE(prefs->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled));
2710 prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, false);
2711 prefs->ClearPref(prefs::kAutofillAuxiliaryProfilesEnabled);
2712 ASSERT_TRUE(prefs->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled));
2713 #else
2714 ASSERT_FALSE(prefs->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled));
2715 prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, true);
2716 prefs->ClearPref(prefs::kAutofillAuxiliaryProfilesEnabled);
2717 ASSERT_FALSE(prefs->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled));
2718 #endif
2719 }
2720
2721 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) {
2722 FormData form;
2723 form.name = ASCIIToUTF16("MyForm");
2724 form.method = ASCIIToUTF16("POST");
2725 form.origin = GURL("http://myform.com/form.html");
2726 form.action = GURL("http://myform.com/submit.html");
2727 form.user_submitted = true;
2728
2729 std::vector<FieldTypeSet> expected_types;
2730
2731 // These fields should all match.
2732 FormFieldData field;
2733 FieldTypeSet types;
2734 autofill_test::CreateTestFormField("", "1", "Elvis", "text", &field);
2735 types.clear();
2736 types.insert(NAME_FIRST);
2737 form.fields.push_back(field);
2738 expected_types.push_back(types);
2739
2740 autofill_test::CreateTestFormField("", "2", "Aaron", "text", &field);
2741 types.clear();
2742 types.insert(NAME_MIDDLE);
2743 form.fields.push_back(field);
2744 expected_types.push_back(types);
2745
2746 autofill_test::CreateTestFormField("", "3", "A", "text", &field);
2747 types.clear();
2748 types.insert(NAME_MIDDLE_INITIAL);
2749 form.fields.push_back(field);
2750 expected_types.push_back(types);
2751
2752 autofill_test::CreateTestFormField("", "4", "Presley", "text", &field);
2753 types.clear();
2754 types.insert(NAME_LAST);
2755 form.fields.push_back(field);
2756 expected_types.push_back(types);
2757
2758 autofill_test::CreateTestFormField("", "5", "Elvis Presley", "text", &field);
2759 types.clear();
2760 types.insert(CREDIT_CARD_NAME);
2761 form.fields.push_back(field);
2762 expected_types.push_back(types);
2763
2764 autofill_test::CreateTestFormField("", "6", "Elvis Aaron Presley", "text",
2765 &field);
2766 types.clear();
2767 types.insert(NAME_FULL);
2768 form.fields.push_back(field);
2769 expected_types.push_back(types);
2770
2771 autofill_test::CreateTestFormField("", "7", "theking@gmail.com", "email",
2772 &field);
2773 types.clear();
2774 types.insert(EMAIL_ADDRESS);
2775 form.fields.push_back(field);
2776 expected_types.push_back(types);
2777
2778 autofill_test::CreateTestFormField("", "8", "RCA", "text", &field);
2779 types.clear();
2780 types.insert(COMPANY_NAME);
2781 form.fields.push_back(field);
2782 expected_types.push_back(types);
2783
2784 autofill_test::CreateTestFormField("", "9", "3734 Elvis Presley Blvd.",
2785 "text", &field);
2786 types.clear();
2787 types.insert(ADDRESS_HOME_LINE1);
2788 form.fields.push_back(field);
2789 expected_types.push_back(types);
2790
2791 autofill_test::CreateTestFormField("", "10", "Apt. 10", "text", &field);
2792 types.clear();
2793 types.insert(ADDRESS_HOME_LINE2);
2794 form.fields.push_back(field);
2795 expected_types.push_back(types);
2796
2797 autofill_test::CreateTestFormField("", "11", "Memphis", "text", &field);
2798 types.clear();
2799 types.insert(ADDRESS_HOME_CITY);
2800 form.fields.push_back(field);
2801 expected_types.push_back(types);
2802
2803 autofill_test::CreateTestFormField("", "12", "Tennessee", "text", &field);
2804 types.clear();
2805 types.insert(ADDRESS_HOME_STATE);
2806 form.fields.push_back(field);
2807 expected_types.push_back(types);
2808
2809 autofill_test::CreateTestFormField("", "13", "38116", "text", &field);
2810 types.clear();
2811 types.insert(ADDRESS_HOME_ZIP);
2812 form.fields.push_back(field);
2813 expected_types.push_back(types);
2814
2815 autofill_test::CreateTestFormField("", "14", "USA", "text", &field);
2816 types.clear();
2817 types.insert(ADDRESS_HOME_COUNTRY);
2818 form.fields.push_back(field);
2819 expected_types.push_back(types);
2820
2821 autofill_test::CreateTestFormField("", "15", "United States", "text", &field);
2822 types.clear();
2823 types.insert(ADDRESS_HOME_COUNTRY);
2824 form.fields.push_back(field);
2825 expected_types.push_back(types);
2826
2827 autofill_test::CreateTestFormField("", "16", "+1 (234) 567-8901", "text",
2828 &field);
2829 types.clear();
2830 types.insert(PHONE_HOME_WHOLE_NUMBER);
2831 form.fields.push_back(field);
2832 expected_types.push_back(types);
2833
2834 autofill_test::CreateTestFormField("", "17", "2345678901", "text", &field);
2835 types.clear();
2836 types.insert(PHONE_HOME_CITY_AND_NUMBER);
2837 form.fields.push_back(field);
2838 expected_types.push_back(types);
2839
2840 autofill_test::CreateTestFormField("", "18", "1", "text", &field);
2841 types.clear();
2842 types.insert(PHONE_HOME_COUNTRY_CODE);
2843 form.fields.push_back(field);
2844 expected_types.push_back(types);
2845
2846 autofill_test::CreateTestFormField("", "19", "234", "text", &field);
2847 types.clear();
2848 types.insert(PHONE_HOME_CITY_CODE);
2849 form.fields.push_back(field);
2850 expected_types.push_back(types);
2851
2852 autofill_test::CreateTestFormField("", "20", "5678901", "text", &field);
2853 types.clear();
2854 types.insert(PHONE_HOME_NUMBER);
2855 form.fields.push_back(field);
2856 expected_types.push_back(types);
2857
2858 autofill_test::CreateTestFormField("", "21", "567", "text", &field);
2859 types.clear();
2860 types.insert(PHONE_HOME_NUMBER);
2861 form.fields.push_back(field);
2862 expected_types.push_back(types);
2863
2864 autofill_test::CreateTestFormField("", "22", "8901", "text", &field);
2865 types.clear();
2866 types.insert(PHONE_HOME_NUMBER);
2867 form.fields.push_back(field);
2868 expected_types.push_back(types);
2869
2870 autofill_test::CreateTestFormField("", "23", "4234-5678-9012-3456", "text",
2871 &field);
2872 types.clear();
2873 types.insert(CREDIT_CARD_NUMBER);
2874 form.fields.push_back(field);
2875 expected_types.push_back(types);
2876
2877 autofill_test::CreateTestFormField("", "24", "04", "text", &field);
2878 types.clear();
2879 types.insert(CREDIT_CARD_EXP_MONTH);
2880 form.fields.push_back(field);
2881 expected_types.push_back(types);
2882
2883 autofill_test::CreateTestFormField("", "25", "April", "text", &field);
2884 types.clear();
2885 types.insert(CREDIT_CARD_EXP_MONTH);
2886 form.fields.push_back(field);
2887 expected_types.push_back(types);
2888
2889 autofill_test::CreateTestFormField("", "26", "2012", "text", &field);
2890 types.clear();
2891 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2892 form.fields.push_back(field);
2893 expected_types.push_back(types);
2894
2895 autofill_test::CreateTestFormField("", "27", "12", "text", &field);
2896 types.clear();
2897 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2898 form.fields.push_back(field);
2899 expected_types.push_back(types);
2900
2901 autofill_test::CreateTestFormField("", "28", "04/2012", "text", &field);
2902 types.clear();
2903 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2904 form.fields.push_back(field);
2905 expected_types.push_back(types);
2906
2907 // Make sure that we trim whitespace properly.
2908 autofill_test::CreateTestFormField("", "29", "", "text", &field);
2909 types.clear();
2910 types.insert(EMPTY_TYPE);
2911 form.fields.push_back(field);
2912 expected_types.push_back(types);
2913
2914 autofill_test::CreateTestFormField("", "30", " ", "text", &field);
2915 types.clear();
2916 types.insert(EMPTY_TYPE);
2917 form.fields.push_back(field);
2918 expected_types.push_back(types);
2919
2920 autofill_test::CreateTestFormField("", "31", " Elvis", "text", &field);
2921 types.clear();
2922 types.insert(NAME_FIRST);
2923 form.fields.push_back(field);
2924 expected_types.push_back(types);
2925
2926 autofill_test::CreateTestFormField("", "32", "Elvis ", "text", &field);
2927 types.clear();
2928 types.insert(NAME_FIRST);
2929 form.fields.push_back(field);
2930 expected_types.push_back(types);
2931
2932 // These fields should not match, as they differ by case.
2933 autofill_test::CreateTestFormField("", "33", "elvis", "text", &field);
2934 types.clear();
2935 types.insert(UNKNOWN_TYPE);
2936 form.fields.push_back(field);
2937 expected_types.push_back(types);
2938
2939 autofill_test::CreateTestFormField("", "34", "3734 Elvis Presley BLVD",
2940 "text", &field);
2941 types.clear();
2942 types.insert(UNKNOWN_TYPE);
2943 form.fields.push_back(field);
2944 expected_types.push_back(types);
2945
2946 // These fields should not match, as they are unsupported variants.
2947 autofill_test::CreateTestFormField("", "35", "Elvis Aaron", "text", &field);
2948 types.clear();
2949 types.insert(UNKNOWN_TYPE);
2950 form.fields.push_back(field);
2951 expected_types.push_back(types);
2952
2953 autofill_test::CreateTestFormField("", "36", "Mr. Presley", "text", &field);
2954 types.clear();
2955 types.insert(UNKNOWN_TYPE);
2956 form.fields.push_back(field);
2957 expected_types.push_back(types);
2958
2959 autofill_test::CreateTestFormField("", "37", "3734 Elvis Presley", "text",
2960 &field);
2961 types.clear();
2962 types.insert(UNKNOWN_TYPE);
2963 form.fields.push_back(field);
2964 expected_types.push_back(types);
2965
2966 autofill_test::CreateTestFormField("", "38", "TN", "text", &field);
2967 types.clear();
2968 types.insert(UNKNOWN_TYPE);
2969 form.fields.push_back(field);
2970 expected_types.push_back(types);
2971
2972 autofill_test::CreateTestFormField("", "39", "38116-1023", "text", &field);
2973 types.clear();
2974 types.insert(UNKNOWN_TYPE);
2975 form.fields.push_back(field);
2976 expected_types.push_back(types);
2977
2978 autofill_test::CreateTestFormField("", "20", "5", "text", &field);
2979 types.clear();
2980 types.insert(UNKNOWN_TYPE);
2981 form.fields.push_back(field);
2982 expected_types.push_back(types);
2983
2984 autofill_test::CreateTestFormField("", "20", "56", "text", &field);
2985 types.clear();
2986 types.insert(UNKNOWN_TYPE);
2987 form.fields.push_back(field);
2988 expected_types.push_back(types);
2989
2990 autofill_test::CreateTestFormField("", "20", "901", "text", &field);
2991 types.clear();
2992 types.insert(UNKNOWN_TYPE);
2993 form.fields.push_back(field);
2994 expected_types.push_back(types);
2995
2996 autofill_manager_->set_expected_submitted_field_types(expected_types);
2997 FormSubmitted(form);
2998 }
2999
3000 TEST_F(AutofillManagerTest, UpdatePasswordSyncState) {
3001 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
3002 PasswordManager::CreateForWebContentsAndDelegate(
3003 web_contents(),
3004 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
3005
3006 PrefService* prefs = components::UserPrefs::Get(profile());
3007
3008 // Allow this test to control what should get synced.
3009 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false);
3010 // Always set password generation enabled check box so we can test the
3011 // behavior of password sync.
3012 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true);
3013
3014 // Sync some things, but not passwords. Shouldn't send anything since
3015 // password generation is disabled by default.
3016 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile(
3017 profile());
3018 sync_service->SetSyncSetupCompleted();
3019 syncer::ModelTypeSet preferred_set;
3020 preferred_set.Put(syncer::EXTENSIONS);
3021 preferred_set.Put(syncer::PREFERENCES);
3022 sync_service->ChangePreferredDataTypes(preferred_set);
3023 syncer::ModelTypeSet new_set = sync_service->GetPreferredDataTypes();
3024 UpdatePasswordGenerationState(false);
3025 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size());
3026
3027 // Now sync passwords.
3028 preferred_set.Put(syncer::PASSWORDS);
3029 sync_service->ChangePreferredDataTypes(preferred_set);
3030 UpdatePasswordGenerationState(false);
3031 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3032 EXPECT_TRUE(autofill_manager_->GetSentStates()[0]);
3033 autofill_manager_->ClearSentStates();
3034
3035 // Add some additional synced state. Nothing should be sent.
3036 preferred_set.Put(syncer::THEMES);
3037 sync_service->ChangePreferredDataTypes(preferred_set);
3038 UpdatePasswordGenerationState(false);
3039 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size());
3040
3041 // Disable syncing. This should disable the feature.
3042 sync_service->DisableForUser();
3043 UpdatePasswordGenerationState(false);
3044 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3045 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]);
3046 autofill_manager_->ClearSentStates();
3047
3048 // When a new render_view is created, we send the state even if it's the
3049 // same.
3050 UpdatePasswordGenerationState(true);
3051 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3052 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]);
3053 autofill_manager_->ClearSentStates();
3054 }
3055
3056 TEST_F(IncognitoAutofillManagerTest, UpdatePasswordSyncStateIncognito) {
3057 // Disable password manager by going incognito, and enable syncing. The
3058 // feature should still be disabled, and nothing will be sent.
3059 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
3060 PasswordManager::CreateForWebContentsAndDelegate(
3061 web_contents(),
3062 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
3063
3064 PrefService* prefs = components::UserPrefs::Get(profile());
3065
3066 // Allow this test to control what should get synced.
3067 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false);
3068 // Always set password generation enabled check box so we can test the
3069 // behavior of password sync.
3070 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true);
3071
3072 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs());
3073 sync_prefs.SetSyncSetupCompleted();
3074 UpdatePasswordGenerationState(false);
3075 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size());
3076 }
3077
3078 TEST_F(AutofillManagerTest, UpdatePasswordGenerationState) {
3079 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
3080 PasswordManager::CreateForWebContentsAndDelegate(
3081 web_contents(),
3082 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
3083
3084 PrefService* prefs = components::UserPrefs::Get(profile());
3085
3086 // Always set password sync enabled so we can test the behavior of password
3087 // generation.
3088 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false);
3089 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile(
3090 profile());
3091 sync_service->SetSyncSetupCompleted();
3092 syncer::ModelTypeSet preferred_set;
3093 preferred_set.Put(syncer::PASSWORDS);
3094 sync_service->ChangePreferredDataTypes(preferred_set);
3095
3096 // Enabled state remains false, should not sent.
3097 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, false);
3098 UpdatePasswordGenerationState(false);
3099 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size());
3100
3101 // Enabled state from false to true, should sent true.
3102 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true);
3103 UpdatePasswordGenerationState(false);
3104 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3105 EXPECT_TRUE(autofill_manager_->GetSentStates()[0]);
3106 autofill_manager_->ClearSentStates();
3107
3108 // Enabled states remains true, should not sent.
3109 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true);
3110 UpdatePasswordGenerationState(false);
3111 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size());
3112
3113 // Enabled states from true to false, should sent false.
3114 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, false);
3115 UpdatePasswordGenerationState(false);
3116 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3117 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]);
3118 autofill_manager_->ClearSentStates();
3119
3120 // When a new render_view is created, we send the state even if it's the
3121 // same.
3122 UpdatePasswordGenerationState(true);
3123 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size());
3124 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]);
3125 autofill_manager_->ClearSentStates();
3126 }
3127
3128 TEST_F(AutofillManagerTest, RemoveProfile) {
3129 // Add and remove an Autofill profile.
3130 AutofillProfile* profile = new AutofillProfile;
3131 std::string guid = "00000000-0000-0000-0000-000000000102";
3132 profile->set_guid(guid.c_str());
3133 autofill_manager_->AddProfile(profile);
3134
3135 GUIDPair guid_pair(guid, 0);
3136 GUIDPair empty(std::string(), 0);
3137 int id = PackGUIDs(empty, guid_pair);
3138
3139 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
3140
3141 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
3142 }
3143
3144 TEST_F(AutofillManagerTest, RemoveCreditCard){
3145 // Add and remove an Autofill credit card.
3146 CreditCard* credit_card = new CreditCard;
3147 std::string guid = "00000000-0000-0000-0000-000000100007";
3148 credit_card->set_guid(guid.c_str());
3149 autofill_manager_->AddCreditCard(credit_card);
3150
3151 GUIDPair guid_pair(guid, 0);
3152 GUIDPair empty(std::string(), 0);
3153 int id = PackGUIDs(guid_pair, empty);
3154
3155 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
3156
3157 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str()));
3158 }
3159
3160 TEST_F(AutofillManagerTest, RemoveProfileVariant) {
3161 // Add and remove an Autofill profile.
3162 AutofillProfile* profile = new AutofillProfile;
3163 std::string guid = "00000000-0000-0000-0000-000000000102";
3164 profile->set_guid(guid.c_str());
3165 autofill_manager_->AddProfile(profile);
3166
3167 GUIDPair guid_pair(guid, 1);
3168 GUIDPair empty(std::string(), 0);
3169 int id = PackGUIDs(empty, guid_pair);
3170
3171 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
3172
3173 // TODO(csharp): Currently variants should not be deleted, but once they are
3174 // update these expectations.
3175 // http://crbug.com/124211
3176 EXPECT_TRUE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
3177 }
3178
3179 TEST_F(AutofillManagerTest, DisabledAutofillDispatchesError) {
3180 EXPECT_TRUE(autofill_manager_->request_autocomplete_results().empty());
3181
3182 autofill_manager_->set_autofill_enabled(false);
3183 autofill_manager_->OnRequestAutocomplete(FormData(),
3184 GURL(),
3185 content::SSLStatus());
3186
3187 EXPECT_EQ(1U, autofill_manager_->request_autocomplete_results().size());
3188 EXPECT_EQ(WebFormElement::AutocompleteResultErrorDisabled,
3189 autofill_manager_->request_autocomplete_results()[0].first);
3190 }
3191
3192 namespace {
3193
3194 class MockAutofillExternalDelegate : public AutofillExternalDelegate {
3195 public:
3196 explicit MockAutofillExternalDelegate(content::WebContents* web_contents,
3197 AutofillManager* autofill_manager)
3198 : AutofillExternalDelegate(web_contents, autofill_manager) {}
3199 virtual ~MockAutofillExternalDelegate() {}
3200
3201 MOCK_METHOD5(OnQuery, void(int query_id,
3202 const FormData& form,
3203 const FormFieldData& field,
3204 const gfx::RectF& bounds,
3205 bool display_warning));
3206
3207 private:
3208 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate);
3209 };
3210
3211 } // namespace
3212
3213 // Test our external delegate is called at the right time.
3214 TEST_F(AutofillManagerTest, TestExternalDelegate) {
3215 MockAutofillExternalDelegate external_delegate(web_contents(),
3216 autofill_manager_.get());
3217 EXPECT_CALL(external_delegate, OnQuery(_, _, _, _, _));
3218 autofill_manager_->SetExternalDelegate(&external_delegate);
3219
3220 FormData form;
3221 CreateTestAddressFormData(&form);
3222 std::vector<FormData> forms(1, form);
3223 FormsSeen(forms);
3224 const FormFieldData& field = form.fields[0];
3225 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
3226
3227 autofill_manager_->SetExternalDelegate(NULL);
3228 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autofill_manager_delegate.h ('k') | chrome/browser/autofill/autofill_merge_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698