1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/string16.h"
11 #include "base/string_number_conversions.h"
12 #include "base/stringprintf.h"
13 #include "base/tuple.h"
14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/autocomplete_history_manager.h"
16 #include "chrome/browser/autofill/autofill_common_test.h"
17 #include "chrome/browser/autofill/autofill_manager.h"
18 #include "chrome/browser/autofill/autofill_profile.h"
19 #include "chrome/browser/autofill/credit_card.h"
20 #include "chrome/browser/autofill/personal_data_manager.h"
21 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
25 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
26 #include "chrome/common/autofill_messages.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/test/testing_profile.h"
29 #include "content/browser/tab_contents/test_tab_contents.h"
30 #include "googleurl/src/gurl.h"
31 #include "grit/generated_resources.h"
32 #include "ipc/ipc_test_sink.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "webkit/glue/form_data.h"
37 #include "webkit/glue/form_field.h"
38
39 using webkit_glue::FormData;
40 using webkit_glue::FormField;
41
42 namespace {
43
44 // The page ID sent to the AutofillManager from the RenderView, used to send
45 // an IPC message back to the renderer.
46 const int kDefaultPageID = 137;
47
48 typedef Tuple5<int,
49 std::vector<string16>,
50 std::vector<string16>,
51 std::vector<string16>,
52 std::vector<int> > AutofillParam;
53
54 class TestPersonalDataManager : public PersonalDataManager {
55 public:
TestPersonalDataManager()56 TestPersonalDataManager() {
57 CreateTestAutofillProfiles(&web_profiles_);
58 CreateTestCreditCards(&credit_cards_);
59 }
60
61 MOCK_METHOD1(SaveImportedProfile, void(const AutofillProfile&));
62
GetProfileWithGUID(const char * guid)63 AutofillProfile* GetProfileWithGUID(const char* guid) {
64 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin();
65 it != web_profiles_.end(); ++it) {
66 if (!(*it)->guid().compare(guid))
67 return *it;
68 }
69 return NULL;
70 }
71
AddProfile(AutofillProfile * profile)72 void AddProfile(AutofillProfile* profile) {
73 web_profiles_->push_back(profile);
74 }
75
AddCreditCard(CreditCard * credit_card)76 void AddCreditCard(CreditCard* credit_card) {
77 credit_cards_->push_back(credit_card);
78 }
79
ClearAutofillProfiles()80 void ClearAutofillProfiles() {
81 web_profiles_.reset();
82 }
83
ClearCreditCards()84 void ClearCreditCards() {
85 credit_cards_.reset();
86 }
87
CreateTestCreditCardsYearAndMonth(const char * year,const char * month)88 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
89 ClearCreditCards();
90 CreditCard* credit_card = new CreditCard;
91 autofill_test::SetCreditCardInfo(credit_card, "Miku Hatsune",
92 "4234567890654321", // Visa
93 month, year);
94 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
95 credit_cards_->push_back(credit_card);
96 }
97
98 private:
CreateTestAutofillProfiles(ScopedVector<AutofillProfile> * profiles)99 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
100 AutofillProfile* profile = new AutofillProfile;
101 autofill_test::SetProfileInfo(profile, "Elvis", "Aaron",
102 "Presley", "theking@gmail.com", "RCA",
103 "3734 Elvis Presley Blvd.", "Apt. 10",
104 "Memphis", "Tennessee", "38116", "USA",
105 "12345678901", "");
106 profile->set_guid("00000000-0000-0000-0000-000000000001");
107 profiles->push_back(profile);
108 profile = new AutofillProfile;
109 autofill_test::SetProfileInfo(profile, "Charles", "Hardin",
110 "Holley", "buddy@gmail.com", "Decca",
111 "123 Apple St.", "unit 6", "Lubbock",
112 "Texas", "79401", "USA", "23456789012",
113 "");
114 profile->set_guid("00000000-0000-0000-0000-000000000002");
115 profiles->push_back(profile);
116 profile = new AutofillProfile;
117 autofill_test::SetProfileInfo(profile, "", "", "", "", "", "", "",
118 "", "", "", "", "", "");
119 profile->set_guid("00000000-0000-0000-0000-000000000003");
120 profiles->push_back(profile);
121 }
122
CreateTestCreditCards(ScopedVector<CreditCard> * credit_cards)123 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
124 CreditCard* credit_card = new CreditCard;
125 autofill_test::SetCreditCardInfo(credit_card, "Elvis Presley",
126 "4234 5678 9012 3456", // Visa
127 "04", "2012");
128 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
129 credit_cards->push_back(credit_card);
130
131 credit_card = new CreditCard;
132 autofill_test::SetCreditCardInfo(credit_card, "Buddy Holly",
133 "5187654321098765", // Mastercard
134 "10", "2014");
135 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
136 credit_cards->push_back(credit_card);
137
138 credit_card = new CreditCard;
139 autofill_test::SetCreditCardInfo(credit_card, "", "", "", "");
140 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
141 credit_cards->push_back(credit_card);
142 }
143
144 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
145 };
146
147 // Populates |form| with data corresponding to a simple address form.
148 // Note that this actually appends fields to the form data, which can be useful
149 // for building up more complex test forms.
CreateTestAddressFormData(FormData * form)150 void CreateTestAddressFormData(FormData* form) {
151 form->name = ASCIIToUTF16("MyForm");
152 form->method = ASCIIToUTF16("POST");
153 form->origin = GURL("http://myform.com/form.html");
154 form->action = GURL("http://myform.com/submit.html");
155 form->user_submitted = true;
156
157 FormField field;
158 autofill_test::CreateTestFormField(
159 "First Name", "firstname", "", "text", &field);
160 form->fields.push_back(field);
161 autofill_test::CreateTestFormField(
162 "Middle Name", "middlename", "", "text", &field);
163 form->fields.push_back(field);
164 autofill_test::CreateTestFormField(
165 "Last Name", "lastname", "", "text", &field);
166 form->fields.push_back(field);
167 autofill_test::CreateTestFormField(
168 "Address Line 1", "addr1", "", "text", &field);
169 form->fields.push_back(field);
170 autofill_test::CreateTestFormField(
171 "Address Line 2", "addr2", "", "text", &field);
172 form->fields.push_back(field);
173 autofill_test::CreateTestFormField(
174 "City", "city", "", "text", &field);
175 form->fields.push_back(field);
176 autofill_test::CreateTestFormField(
177 "State", "state", "", "text", &field);
178 form->fields.push_back(field);
179 autofill_test::CreateTestFormField(
180 "Postal Code", "zipcode", "", "text", &field);
181 form->fields.push_back(field);
182 autofill_test::CreateTestFormField(
183 "Country", "country", "", "text", &field);
184 form->fields.push_back(field);
185 autofill_test::CreateTestFormField(
186 "Phone Number", "phonenumber", "", "tel", &field);
187 form->fields.push_back(field);
188 autofill_test::CreateTestFormField(
189 "Fax", "fax", "", "text", &field);
190 form->fields.push_back(field);
191 autofill_test::CreateTestFormField(
192 "Email", "email", "", "email", &field);
193 form->fields.push_back(field);
194 }
195
196 // Populates |form| with data corresponding to a simple credit card form.
197 // Note that this actually appends fields to the form data, which can be useful
198 // for building up more complex test forms.
CreateTestCreditCardFormData(FormData * form,bool is_https,bool use_month_type)199 void CreateTestCreditCardFormData(FormData* form,
200 bool is_https,
201 bool use_month_type) {
202 form->name = ASCIIToUTF16("MyForm");
203 form->method = ASCIIToUTF16("POST");
204 if (is_https) {
205 form->origin = GURL("https://myform.com/form.html");
206 form->action = GURL("https://myform.com/submit.html");
207 } else {
208 form->origin = GURL("http://myform.com/form.html");
209 form->action = GURL("http://myform.com/submit.html");
210 }
211 form->user_submitted = true;
212
213 FormField field;
214 autofill_test::CreateTestFormField(
215 "Name on Card", "nameoncard", "", "text", &field);
216 form->fields.push_back(field);
217 autofill_test::CreateTestFormField(
218 "Card Number", "cardnumber", "", "text", &field);
219 form->fields.push_back(field);
220 if (use_month_type) {
221 autofill_test::CreateTestFormField(
222 "Expiration Date", "ccmonth", "", "month", &field);
223 form->fields.push_back(field);
224 } else {
225 autofill_test::CreateTestFormField(
226 "Expiration Date", "ccmonth", "", "text", &field);
227 form->fields.push_back(field);
228 autofill_test::CreateTestFormField(
229 "", "ccyear", "", "text", &field);
230 form->fields.push_back(field);
231 }
232 }
233
ExpectSuggestions(int page_id,const std::vector<string16> & values,const std::vector<string16> & labels,const std::vector<string16> & icons,const std::vector<int> & unique_ids,int expected_page_id,size_t expected_num_suggestions,const string16 expected_values[],const string16 expected_labels[],const string16 expected_icons[],const int expected_unique_ids[])234 void ExpectSuggestions(int page_id,
235 const std::vector<string16>& values,
236 const std::vector<string16>& labels,
237 const std::vector<string16>& icons,
238 const std::vector<int>& unique_ids,
239 int expected_page_id,
240 size_t expected_num_suggestions,
241 const string16 expected_values[],
242 const string16 expected_labels[],
243 const string16 expected_icons[],
244 const int expected_unique_ids[]) {
245 EXPECT_EQ(expected_page_id, page_id);
246 ASSERT_EQ(expected_num_suggestions, values.size());
247 ASSERT_EQ(expected_num_suggestions, labels.size());
248 ASSERT_EQ(expected_num_suggestions, icons.size());
249 ASSERT_EQ(expected_num_suggestions, unique_ids.size());
250 for (size_t i = 0; i < expected_num_suggestions; ++i) {
251 SCOPED_TRACE(StringPrintf("i: %" PRIuS, i));
252 EXPECT_EQ(expected_values[i], values[i]);
253 EXPECT_EQ(expected_labels[i], labels[i]);
254 EXPECT_EQ(expected_icons[i], icons[i]);
255 EXPECT_EQ(expected_unique_ids[i], unique_ids[i]);
256 }
257 }
258
259 // Verifies that the |filled_form| has been filled with the given data.
260 // Verifies address fields if |has_address_fields| is true, and verifies
261 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
262 // are true. |use_month_type| is used for credit card input month type.
ExpectFilledForm(int page_id,const FormData & filled_form,int expected_page_id,const char * first,const char * middle,const char * last,const char * address1,const char * address2,const char * city,const char * state,const char * postal_code,const char * country,const char * phone,const char * fax,const char * email,const char * name_on_card,const char * card_number,const char * expiration_month,const char * expiration_year,bool has_address_fields,bool has_credit_card_fields,bool use_month_type)263 void ExpectFilledForm(int page_id,
264 const FormData& filled_form,
265 int expected_page_id,
266 const char* first,
267 const char* middle,
268 const char* last,
269 const char* address1,
270 const char* address2,
271 const char* city,
272 const char* state,
273 const char* postal_code,
274 const char* country,
275 const char* phone,
276 const char* fax,
277 const char* email,
278 const char* name_on_card,
279 const char* card_number,
280 const char* expiration_month,
281 const char* expiration_year,
282 bool has_address_fields,
283 bool has_credit_card_fields,
284 bool use_month_type) {
285 // The number of fields in the address and credit card forms created above.
286 const size_t kAddressFormSize = 12;
287 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
288
289 EXPECT_EQ(expected_page_id, page_id);
290 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
291 EXPECT_EQ(ASCIIToUTF16("POST"), filled_form.method);
292 if (has_credit_card_fields) {
293 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
294 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
295 } else {
296 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
297 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
298 }
299 EXPECT_TRUE(filled_form.user_submitted);
300
301 size_t form_size = 0;
302 if (has_address_fields)
303 form_size += kAddressFormSize;
304 if (has_credit_card_fields)
305 form_size += kCreditCardFormSize;
306 ASSERT_EQ(form_size, filled_form.fields.size());
307
308 FormField field;
309 if (has_address_fields) {
310 autofill_test::CreateTestFormField(
311 "First Name", "firstname", first, "text", &field);
312 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[0]));
313 autofill_test::CreateTestFormField(
314 "Middle Name", "middlename", middle, "text", &field);
315 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[1]));
316 autofill_test::CreateTestFormField(
317 "Last Name", "lastname", last, "text", &field);
318 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[2]));
319 autofill_test::CreateTestFormField(
320 "Address Line 1", "addr1", address1, "text", &field);
321 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[3]));
322 autofill_test::CreateTestFormField(
323 "Address Line 2", "addr2", address2, "text", &field);
324 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[4]));
325 autofill_test::CreateTestFormField(
326 "City", "city", city, "text", &field);
327 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[5]));
328 autofill_test::CreateTestFormField(
329 "State", "state", state, "text", &field);
330 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[6]));
331 autofill_test::CreateTestFormField(
332 "Postal Code", "zipcode", postal_code, "text", &field);
333 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[7]));
334 autofill_test::CreateTestFormField(
335 "Country", "country", country, "text", &field);
336 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[8]));
337 autofill_test::CreateTestFormField(
338 "Phone Number", "phonenumber", phone, "tel", &field);
339 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[9]));
340 autofill_test::CreateTestFormField(
341 "Fax", "fax", fax, "text", &field);
342 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[10]));
343 autofill_test::CreateTestFormField(
344 "Email", "email", email, "email", &field);
345 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[11]));
346 }
347
348 if (has_credit_card_fields) {
349 size_t offset = has_address_fields? kAddressFormSize : 0;
350 autofill_test::CreateTestFormField(
351 "Name on Card", "nameoncard", name_on_card, "text", &field);
352 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[offset + 0]));
353 autofill_test::CreateTestFormField(
354 "Card Number", "cardnumber", card_number, "text", &field);
355 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[offset + 1]));
356 if (use_month_type) {
357 std::string exp_year = expiration_year;
358 std::string exp_month = expiration_month;
359 std::string date;
360 if (!exp_year.empty() && !exp_month.empty())
361 date = exp_year + "-" + exp_month;
362 autofill_test::CreateTestFormField(
363 "Expiration Date", "ccmonth", date.c_str(), "month", &field);
364 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[offset + 2]));
365 } else {
366 autofill_test::CreateTestFormField(
367 "Expiration Date", "ccmonth", expiration_month, "text", &field);
368 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[offset + 2]));
369 autofill_test::CreateTestFormField(
370 "", "ccyear", expiration_year, "text", &field);
371 EXPECT_TRUE(field.StrictlyEqualsHack(filled_form.fields[offset + 3]));
372 }
373 }
374 }
375
ExpectFilledAddressFormElvis(int page_id,const FormData & filled_form,int expected_page_id,bool has_credit_card_fields)376 void ExpectFilledAddressFormElvis(int page_id,
377 const FormData& filled_form,
378 int expected_page_id,
379 bool has_credit_card_fields) {
380 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
381 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
382 "Tennessee", "38116", "United States", "12345678901", "",
383 "theking@gmail.com", "", "", "", "", true,
384 has_credit_card_fields, false);
385 }
386
ExpectFilledCreditCardFormElvis(int page_id,const FormData & filled_form,int expected_page_id,bool has_address_fields)387 void ExpectFilledCreditCardFormElvis(int page_id,
388 const FormData& filled_form,
389 int expected_page_id,
390 bool has_address_fields) {
391 ExpectFilledForm(page_id, filled_form, expected_page_id,
392 "", "", "", "", "", "", "", "", "", "", "", "",
393 "Elvis Presley", "4234567890123456", "04", "2012",
394 has_address_fields, true, false);
395 }
396
ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,const FormData & filled_form,int expected_page_id,bool has_address_fields,const char * year,const char * month)397 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
398 const FormData& filled_form,
399 int expected_page_id,
400 bool has_address_fields,
401 const char* year,
402 const char* month) {
403 ExpectFilledForm(page_id, filled_form, expected_page_id,
404 "", "", "", "", "", "", "", "", "", "", "", "",
405 "Miku Hatsune", "4234567890654321", month, year,
406 has_address_fields, true, true);
407 }
408
409 class TestAutofillManager : public AutofillManager {
410 public:
TestAutofillManager(TabContents * tab_contents,TestPersonalDataManager * personal_manager)411 TestAutofillManager(TabContents* tab_contents,
412 TestPersonalDataManager* personal_manager)
413 : AutofillManager(tab_contents, personal_manager),
414 autofill_enabled_(true) {
415 test_personal_data_ = personal_manager;
416 }
417
IsAutofillEnabled() const418 virtual bool IsAutofillEnabled() const { return autofill_enabled_; }
419
set_autofill_enabled(bool autofill_enabled)420 void set_autofill_enabled(bool autofill_enabled) {
421 autofill_enabled_ = autofill_enabled;
422 }
423
GetProfileWithGUID(const char * guid)424 AutofillProfile* GetProfileWithGUID(const char* guid) {
425 return test_personal_data_->GetProfileWithGUID(guid);
426 }
427
AddProfile(AutofillProfile * profile)428 void AddProfile(AutofillProfile* profile) {
429 test_personal_data_->AddProfile(profile);
430 }
431
AddCreditCard(CreditCard * credit_card)432 void AddCreditCard(CreditCard* credit_card) {
433 test_personal_data_->AddCreditCard(credit_card);
434 }
435
GetPackedCreditCardID(int credit_card_id)436 int GetPackedCreditCardID(int credit_card_id) {
437 return PackGUIDs(IDToGUID(credit_card_id), GUIDPair(std::string(), 0));
438 }
439
GUIDToID(const GUIDPair & guid)440 virtual int GUIDToID(const GUIDPair& guid) OVERRIDE {
441 if (guid.first.empty())
442 return 0;
443
444 int id;
445 EXPECT_TRUE(base::StringToInt(guid.first.substr(guid.first.rfind("-") + 1),
446 &id));
447 return id;
448 }
449
IDToGUID(int id)450 virtual const GUIDPair IDToGUID(int id) OVERRIDE {
451 EXPECT_TRUE(id >= 0);
452 if (id <= 0)
453 return GUIDPair(std::string(), 0);
454
455 return GUIDPair(base::StringPrintf("00000000-0000-0000-0000-%012d", id), 0);
456 }
457
AddSeenForm(FormStructure * form)458 void AddSeenForm(FormStructure* form) {
459 form_structures()->push_back(form);
460 }
461
462 private:
463 TestPersonalDataManager* test_personal_data_;
464 bool autofill_enabled_;
465
466 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
467 };
468
469 } // namespace
470
471 class AutofillManagerTest : public TabContentsWrapperTestHarness {
472 public:
473 typedef AutofillManager::GUIDPair GUIDPair;
474
AutofillManagerTest()475 AutofillManagerTest() {}
~AutofillManagerTest()476 virtual ~AutofillManagerTest() {
477 // Order of destruction is important as AutofillManager relies on
478 // PersonalDataManager to be around when it gets destroyed.
479 autofill_manager_.reset(NULL);
480 test_personal_data_ = NULL;
481 }
482
SetUp()483 virtual void SetUp() {
484 TabContentsWrapperTestHarness::SetUp();
485 test_personal_data_ = new TestPersonalDataManager();
486 autofill_manager_.reset(new TestAutofillManager(contents(),
487 test_personal_data_.get()));
488 }
489
profile()490 Profile* profile() { return contents()->profile(); }
491
GetAutofillSuggestions(int query_id,const webkit_glue::FormData & form,const webkit_glue::FormField & field)492 void GetAutofillSuggestions(int query_id,
493 const webkit_glue::FormData& form,
494 const webkit_glue::FormField& field) {
495 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field);
496 }
497
GetAutofillSuggestions(const webkit_glue::FormData & form,const webkit_glue::FormField & field)498 void GetAutofillSuggestions(const webkit_glue::FormData& form,
499 const webkit_glue::FormField& field) {
500 GetAutofillSuggestions(kDefaultPageID, form, field);
501 }
502
AutocompleteSuggestionsReturned(const std::vector<string16> & result)503 void AutocompleteSuggestionsReturned(const std::vector<string16>& result) {
504 contents_wrapper()->autocomplete_history_manager()->
505 SendSuggestions(&result);
506 }
507
FormsSeen(const std::vector<webkit_glue::FormData> & forms)508 void FormsSeen(const std::vector<webkit_glue::FormData>& forms) {
509 autofill_manager_->OnFormsSeen(forms);
510 }
511
FormSubmitted(const FormData & form)512 void FormSubmitted(const FormData& form) {
513 autofill_manager_->OnFormSubmitted(form);
514 }
515
FillAutofillFormData(int query_id,const webkit_glue::FormData & form,const webkit_glue::FormField & field,int unique_id)516 void FillAutofillFormData(int query_id,
517 const webkit_glue::FormData& form,
518 const webkit_glue::FormField& field,
519 int unique_id) {
520 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id);
521 }
522
GetAutofillSuggestionsMessage(int * page_id,std::vector<string16> * values,std::vector<string16> * labels,std::vector<string16> * icons,std::vector<int> * unique_ids)523 bool GetAutofillSuggestionsMessage(int* page_id,
524 std::vector<string16>* values,
525 std::vector<string16>* labels,
526 std::vector<string16>* icons,
527 std::vector<int>* unique_ids) {
528 const uint32 kMsgID = AutofillMsg_SuggestionsReturned::ID;
529 const IPC::Message* message =
530 process()->sink().GetFirstMessageMatching(kMsgID);
531 if (!message)
532 return false;
533
534 AutofillParam autofill_param;
535 AutofillMsg_SuggestionsReturned::Read(message, &autofill_param);
536 if (page_id)
537 *page_id = autofill_param.a;
538 if (values)
539 *values = autofill_param.b;
540 if (labels)
541 *labels = autofill_param.c;
542 if (icons)
543 *icons = autofill_param.d;
544 if (unique_ids)
545 *unique_ids = autofill_param.e;
546
547 contents_wrapper()->autocomplete_history_manager()->CancelPendingQuery();
548 process()->sink().ClearMessages();
549 return true;
550 }
551
GetAutofillFormDataFilledMessage(int * page_id,FormData * results)552 bool GetAutofillFormDataFilledMessage(int *page_id, FormData* results) {
553 const uint32 kMsgID = AutofillMsg_FormDataFilled::ID;
554 const IPC::Message* message =
555 process()->sink().GetFirstMessageMatching(kMsgID);
556 if (!message)
557 return false;
558 Tuple2<int, FormData> autofill_param;
559 AutofillMsg_FormDataFilled::Read(message, &autofill_param);
560 if (page_id)
561 *page_id = autofill_param.a;
562 if (results)
563 *results = autofill_param.b;
564
565 process()->sink().ClearMessages();
566 return true;
567 }
568
569 protected:
570 scoped_ptr<TestAutofillManager> autofill_manager_;
571 scoped_refptr<TestPersonalDataManager> test_personal_data_;
572
573 private:
574 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTest);
575 };
576
577 class TestFormStructure : public FormStructure {
578 public:
TestFormStructure(const FormData & form)579 explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
~TestFormStructure()580 virtual ~TestFormStructure() {}
581
SetFieldTypes(const std::vector<AutofillFieldType> & heuristic_types,const std::vector<AutofillFieldType> & server_types)582 void SetFieldTypes(const std::vector<AutofillFieldType>& heuristic_types,
583 const std::vector<AutofillFieldType>& server_types) {
584 ASSERT_EQ(field_count(), heuristic_types.size());
585 ASSERT_EQ(field_count(), server_types.size());
586
587 for (size_t i = 0; i < field_count(); ++i) {
588 AutofillField* field = (*fields())[i];
589 ASSERT_TRUE(field);
590 field->set_heuristic_type(heuristic_types[i]);
591 field->set_server_type(server_types[i]);
592 }
593
594 UpdateAutofillCount();
595 }
596
597 private:
598 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
599 };
600
601 // Test that we return all address profile suggestions when all form fields are
602 // empty.
TEST_F(AutofillManagerTest,GetProfileSuggestionsEmptyValue)603 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
604 // Set up our form data.
605 FormData form;
606 CreateTestAddressFormData(&form);
607 std::vector<FormData> forms(1, form);
608 FormsSeen(forms);
609
610 const FormField& field = form.fields[0];
611 GetAutofillSuggestions(form, field);
612
613 // No suggestions provided, so send an empty vector as the results.
614 // This triggers the combined message send.
615 AutocompleteSuggestionsReturned(std::vector<string16>());
616
617 // Test that we sent the right message to the renderer.
618 int page_id = 0;
619 std::vector<string16> values;
620 std::vector<string16> labels;
621 std::vector<string16> icons;
622 std::vector<int> unique_ids;
623 GetAutofillSuggestionsMessage(
624 &page_id, &values, &labels, &icons, &unique_ids);
625
626 string16 expected_values[] = {
627 ASCIIToUTF16("Elvis"),
628 ASCIIToUTF16("Charles")
629 };
630 // Inferred labels include full first relevant field, which in this case is
631 // the address line 1.
632 string16 expected_labels[] = {
633 ASCIIToUTF16("3734 Elvis Presley Blvd."),
634 ASCIIToUTF16("123 Apple St.")
635 };
636 string16 expected_icons[] = {string16(), string16()};
637 int expected_unique_ids[] = {1, 2};
638 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
639 kDefaultPageID, arraysize(expected_values), expected_values,
640 expected_labels, expected_icons, expected_unique_ids);
641 }
642
643 // Test that we return only matching address profile suggestions when the
644 // selected form field has been partially filled out.
TEST_F(AutofillManagerTest,GetProfileSuggestionsMatchCharacter)645 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
646 // Set up our form data.
647 FormData form;
648 CreateTestAddressFormData(&form);
649 std::vector<FormData> forms(1, form);
650 FormsSeen(forms);
651
652 FormField field;
653 autofill_test::CreateTestFormField("First Name", "firstname", "E", "text",
654 &field);
655 GetAutofillSuggestions(form, field);
656
657 // No suggestions provided, so send an empty vector as the results.
658 // This triggers the combined message send.
659 AutocompleteSuggestionsReturned(std::vector<string16>());
660
661 // Test that we sent the right message to the renderer.
662 int page_id = 0;
663 std::vector<string16> values;
664 std::vector<string16> labels;
665 std::vector<string16> icons;
666 std::vector<int> unique_ids;
667 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
668 &unique_ids));
669
670 string16 expected_values[] = {ASCIIToUTF16("Elvis")};
671 string16 expected_labels[] = {ASCIIToUTF16("3734 Elvis Presley Blvd.")};
672 string16 expected_icons[] = {string16()};
673 int expected_unique_ids[] = {1};
674 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
675 kDefaultPageID, arraysize(expected_values), expected_values,
676 expected_labels, expected_icons, expected_unique_ids);
677 }
678
679 // Test that we return no suggestions when the form has no relevant fields.
TEST_F(AutofillManagerTest,GetProfileSuggestionsUnknownFields)680 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
681 // Set up our form data.
682 FormData form;
683 form.name = ASCIIToUTF16("MyForm");
684 form.method = ASCIIToUTF16("POST");
685 form.origin = GURL("http://myform.com/form.html");
686 form.action = GURL("http://myform.com/submit.html");
687 form.user_submitted = true;
688
689 FormField field;
690 autofill_test::CreateTestFormField("Username", "username", "", "text",
691 &field);
692 form.fields.push_back(field);
693 autofill_test::CreateTestFormField("Password", "password", "", "password",
694 &field);
695 form.fields.push_back(field);
696 autofill_test::CreateTestFormField("Quest", "quest", "", "quest", &field);
697 form.fields.push_back(field);
698 autofill_test::CreateTestFormField("Color", "color", "", "text", &field);
699 form.fields.push_back(field);
700
701 std::vector<FormData> forms(1, form);
702 FormsSeen(forms);
703
704 GetAutofillSuggestions(form, field);
705 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
706 }
707
708 // Test that we cull duplicate profile suggestions.
TEST_F(AutofillManagerTest,GetProfileSuggestionsWithDuplicates)709 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
710 // Set up our form data.
711 FormData form;
712 CreateTestAddressFormData(&form);
713 std::vector<FormData> forms(1, form);
714 FormsSeen(forms);
715
716 // Add a duplicate profile.
717 AutofillProfile* duplicate_profile =
718 new AutofillProfile(
719 *(autofill_manager_->GetProfileWithGUID(
720 "00000000-0000-0000-0000-000000000001")));
721 autofill_manager_->AddProfile(duplicate_profile);
722
723 const FormField& field = form.fields[0];
724 GetAutofillSuggestions(form, field);
725
726 // No suggestions provided, so send an empty vector as the results.
727 // This triggers the combined message send.
728 AutocompleteSuggestionsReturned(std::vector<string16>());
729
730 // Test that we sent the right message to the renderer.
731 int page_id = 0;
732 std::vector<string16> values;
733 std::vector<string16> labels;
734 std::vector<string16> icons;
735 std::vector<int> unique_ids;
736 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
737 &unique_ids));
738
739 string16 expected_values[] = {
740 ASCIIToUTF16("Elvis"),
741 ASCIIToUTF16("Charles")
742 };
743 string16 expected_labels[] = {
744 ASCIIToUTF16("3734 Elvis Presley Blvd."),
745 ASCIIToUTF16("123 Apple St.")
746 };
747 string16 expected_icons[] = {string16(), string16()};
748 int expected_unique_ids[] = {1, 2};
749 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
750 kDefaultPageID, arraysize(expected_values), expected_values,
751 expected_labels, expected_icons, expected_unique_ids);
752 }
753
754 // Test that we return no suggestions when autofill is disabled.
TEST_F(AutofillManagerTest,GetProfileSuggestionsAutofillDisabledByUser)755 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
756 // Set up our form data.
757 FormData form;
758 CreateTestAddressFormData(&form);
759 std::vector<FormData> forms(1, form);
760 FormsSeen(forms);
761
762 // Disable Autofill.
763 autofill_manager_->set_autofill_enabled(false);
764
765 const FormField& field = form.fields[0];
766 GetAutofillSuggestions(form, field);
767 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
768 }
769
770 // Test that we return a warning explaining that autofill suggestions are
771 // unavailable when the form method is GET rather than POST.
TEST_F(AutofillManagerTest,GetProfileSuggestionsMethodGet)772 TEST_F(AutofillManagerTest, GetProfileSuggestionsMethodGet) {
773 // Set up our form data.
774 FormData form;
775 CreateTestAddressFormData(&form);
776 form.method = ASCIIToUTF16("GET");
777 std::vector<FormData> forms(1, form);
778 FormsSeen(forms);
779
780 const FormField& field = form.fields[0];
781 GetAutofillSuggestions(form, field);
782
783 // No suggestions provided, so send an empty vector as the results.
784 // This triggers the combined message send.
785 AutocompleteSuggestionsReturned(std::vector<string16>());
786
787 // Test that we sent the right message to the renderer.
788 int page_id = 0;
789 std::vector<string16> values;
790 std::vector<string16> labels;
791 std::vector<string16> icons;
792 std::vector<int> unique_ids;
793 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
794 &unique_ids));
795
796 string16 expected_values[] = {
797 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)
798 };
799 string16 expected_labels[] = {string16()};
800 string16 expected_icons[] = {string16()};
801 int expected_unique_ids[] = {-1};
802 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
803 kDefaultPageID, arraysize(expected_values), expected_values,
804 expected_labels, expected_icons, expected_unique_ids);
805
806 // Now add some Autocomplete suggestions. We should return the autocomplete
807 // suggestions and the warning; these will be culled by the renderer.
808 const int kPageID2 = 2;
809 GetAutofillSuggestions(kPageID2, form, field);
810
811 std::vector<string16> suggestions;
812 suggestions.push_back(ASCIIToUTF16("Jay"));
813 suggestions.push_back(ASCIIToUTF16("Jason"));
814 AutocompleteSuggestionsReturned(suggestions);
815
816 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
817 &unique_ids));
818
819 string16 expected_values2[] = {
820 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED),
821 ASCIIToUTF16("Jay"),
822 ASCIIToUTF16("Jason")
823 };
824 string16 expected_labels2[] = {string16(), string16(), string16()};
825 string16 expected_icons2[] = {string16(), string16(), string16()};
826 int expected_unique_ids2[] = {-1, 0, 0};
827 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
828 kPageID2, arraysize(expected_values2), expected_values2,
829 expected_labels2, expected_icons2, expected_unique_ids2);
830
831 // Now clear the test profiles and try again -- we shouldn't return a warning.
832 test_personal_data_->ClearAutofillProfiles();
833 GetAutofillSuggestions(form, field);
834 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
835 }
836
837 // Test that we return all credit card profile suggestions when all form fields
838 // are empty.
TEST_F(AutofillManagerTest,GetCreditCardSuggestionsEmptyValue)839 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
840 // Set up our form data.
841 FormData form;
842 CreateTestCreditCardFormData(&form, true, false);
843 std::vector<FormData> forms(1, form);
844 FormsSeen(forms);
845
846 FormField field = form.fields[1];
847 GetAutofillSuggestions(form, field);
848
849 // No suggestions provided, so send an empty vector as the results.
850 // This triggers the combined message send.
851 AutocompleteSuggestionsReturned(std::vector<string16>());
852
853 // Test that we sent the right message to the renderer.
854 int page_id = 0;
855 std::vector<string16> values;
856 std::vector<string16> labels;
857 std::vector<string16> icons;
858 std::vector<int> unique_ids;
859 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
860 &unique_ids));
861
862 string16 expected_values[] = {
863 ASCIIToUTF16("************3456"),
864 ASCIIToUTF16("************8765")
865 };
866 string16 expected_labels[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
867 string16 expected_icons[] = {
868 ASCIIToUTF16("visaCC"),
869 ASCIIToUTF16("genericCC")
870 };
871 int expected_unique_ids[] = {
872 autofill_manager_->GetPackedCreditCardID(4),
873 autofill_manager_->GetPackedCreditCardID(5)
874 };
875 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
876 kDefaultPageID, arraysize(expected_values), expected_values,
877 expected_labels, expected_icons, expected_unique_ids);
878 }
879
880 // Test that we return only matching credit card profile suggestions when the
881 // selected form field has been partially filled out.
TEST_F(AutofillManagerTest,GetCreditCardSuggestionsMatchCharacter)882 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
883 // Set up our form data.
884 FormData form;
885 CreateTestCreditCardFormData(&form, true, false);
886 std::vector<FormData> forms(1, form);
887 FormsSeen(forms);
888
889 FormField field;
890 autofill_test::CreateTestFormField(
891 "Card Number", "cardnumber", "4", "text", &field);
892 GetAutofillSuggestions(form, field);
893
894 // No suggestions provided, so send an empty vector as the results.
895 // This triggers the combined message send.
896 AutocompleteSuggestionsReturned(std::vector<string16>());
897
898 // Test that we sent the right message to the renderer.
899 int page_id = 0;
900 std::vector<string16> values;
901 std::vector<string16> labels;
902 std::vector<string16> icons;
903 std::vector<int> unique_ids;
904 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
905 &unique_ids));
906
907 string16 expected_values[] = {ASCIIToUTF16("************3456")};
908 string16 expected_labels[] = {ASCIIToUTF16("*3456")};
909 string16 expected_icons[] = {ASCIIToUTF16("visaCC")};
910 int expected_unique_ids[] = {autofill_manager_->GetPackedCreditCardID(4)};
911 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
912 kDefaultPageID, arraysize(expected_values), expected_values,
913 expected_labels, expected_icons, expected_unique_ids);
914 }
915
916 // Test that we return credit card profile suggestions when the selected form
917 // field is not the credit card number field.
TEST_F(AutofillManagerTest,GetCreditCardSuggestionsNonCCNumber)918 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
919 // Set up our form data.
920 FormData form;
921 CreateTestCreditCardFormData(&form, true, false);
922 std::vector<FormData> forms(1, form);
923 FormsSeen(forms);
924
925 const FormField& 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 Presley"),
943 ASCIIToUTF16("Buddy Holly")
944 };
945 string16 expected_labels[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
946 string16 expected_icons[] = {
947 ASCIIToUTF16("visaCC"),
948 ASCIIToUTF16("genericCC")
949 };
950 int expected_unique_ids[] = {
951 autofill_manager_->GetPackedCreditCardID(4),
952 autofill_manager_->GetPackedCreditCardID(5)
953 };
954 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
955 kDefaultPageID, arraysize(expected_values), expected_values,
956 expected_labels, expected_icons, expected_unique_ids);
957 }
958
959 // Test that we return a warning explaining that credit card profile suggestions
960 // are unavailable when the form is not https.
TEST_F(AutofillManagerTest,GetCreditCardSuggestionsNonHTTPS)961 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
962 // Set up our form data.
963 FormData form;
964 CreateTestCreditCardFormData(&form, false, false);
965 std::vector<FormData> forms(1, form);
966 FormsSeen(forms);
967
968 const FormField& field = form.fields[0];
969 GetAutofillSuggestions(form, field);
970
971 // No suggestions provided, so send an empty vector as the results.
972 // This triggers the combined message send.
973 AutocompleteSuggestionsReturned(std::vector<string16>());
974
975 // Test that we sent the right message to the renderer.
976 int page_id = 0;
977 std::vector<string16> values;
978 std::vector<string16> labels;
979 std::vector<string16> icons;
980 std::vector<int> unique_ids;
981 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
982 &unique_ids));
983
984 string16 expected_values[] = {
985 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
986 };
987 string16 expected_labels[] = {string16()};
988 string16 expected_icons[] = {string16()};
989 int expected_unique_ids[] = {-1};
990 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
991 kDefaultPageID, arraysize(expected_values), expected_values,
992 expected_labels, expected_icons, expected_unique_ids);
993
994 // Now add some Autocomplete suggestions. We should show the autocomplete
995 // suggestions and the warning.
996 const int kPageID2 = 2;
997 GetAutofillSuggestions(kPageID2, form, field);
998
999 std::vector<string16> suggestions;
1000 suggestions.push_back(ASCIIToUTF16("Jay"));
1001 suggestions.push_back(ASCIIToUTF16("Jason"));
1002 AutocompleteSuggestionsReturned(suggestions);
1003
1004 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1005 &unique_ids));
1006 string16 expected_values2[] = {
1007 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1008 ASCIIToUTF16("Jay"),
1009 ASCIIToUTF16("Jason")
1010 };
1011 string16 expected_labels2[] = {string16(), string16(), string16()};
1012 string16 expected_icons2[] = {string16(), string16(), string16()};
1013 int expected_unique_ids2[] = {-1, 0, 0};
1014 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1015 kPageID2, arraysize(expected_values2), expected_values2,
1016 expected_labels2, expected_icons2, expected_unique_ids2);
1017
1018 // Clear the test credit cards and try again -- we shouldn't return a warning.
1019 test_personal_data_->ClearCreditCards();
1020 GetAutofillSuggestions(form, field);
1021 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
1022 }
1023
1024 // Test that we return all credit card suggestions in the case that two cards
1025 // have the same obfuscated number.
TEST_F(AutofillManagerTest,GetCreditCardSuggestionsRepeatedObfuscatedNumber)1026 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
1027 // Add a credit card with the same obfuscated number as Elvis's.
1028 // |credit_card| will be owned by the mock PersonalDataManager.
1029 CreditCard* credit_card = new CreditCard;
1030 autofill_test::SetCreditCardInfo(credit_card, "Elvis Presley",
1031 "5231567890123456", // Mastercard
1032 "04", "2012");
1033 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
1034 autofill_manager_->AddCreditCard(credit_card);
1035
1036 // Set up our form data.
1037 FormData form;
1038 CreateTestCreditCardFormData(&form, true, false);
1039 std::vector<FormData> forms(1, form);
1040 FormsSeen(forms);
1041
1042 FormField field = form.fields[1];
1043 GetAutofillSuggestions(form, field);
1044
1045 // No suggestions provided, so send an empty vector as the results.
1046 // This triggers the combined message send.
1047 AutocompleteSuggestionsReturned(std::vector<string16>());
1048
1049 // Test that we sent the right message to the renderer.
1050 int page_id = 0;
1051 std::vector<string16> values;
1052 std::vector<string16> labels;
1053 std::vector<string16> icons;
1054 std::vector<int> unique_ids;
1055 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1056 &unique_ids));
1057
1058 string16 expected_values[] = {
1059 ASCIIToUTF16("************3456"),
1060 ASCIIToUTF16("************8765"),
1061 ASCIIToUTF16("************3456")
1062 };
1063 string16 expected_labels[] = {
1064 ASCIIToUTF16("*3456"),
1065 ASCIIToUTF16("*8765"),
1066 ASCIIToUTF16("*3456"),
1067 };
1068 string16 expected_icons[] = {
1069 ASCIIToUTF16("visaCC"),
1070 ASCIIToUTF16("genericCC"),
1071 ASCIIToUTF16("masterCardCC")
1072 };
1073 int expected_unique_ids[] = {
1074 autofill_manager_->GetPackedCreditCardID(4),
1075 autofill_manager_->GetPackedCreditCardID(5),
1076 autofill_manager_->GetPackedCreditCardID(7)
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 profile and credit card suggestions for combined forms.
TEST_F(AutofillManagerTest,GetAddressAndCreditCardSuggestions)1084 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1085 // Set up our form data.
1086 FormData form;
1087 CreateTestAddressFormData(&form);
1088 CreateTestCreditCardFormData(&form, true, false);
1089 std::vector<FormData> forms(1, form);
1090 FormsSeen(forms);
1091
1092 FormField field = form.fields[0];
1093 GetAutofillSuggestions(form, field);
1094
1095 // No suggestions provided, so send an empty vector as the results.
1096 // This triggers the combined message send.
1097 AutocompleteSuggestionsReturned(std::vector<string16>());
1098
1099 // Test that we sent the right address suggestions to the renderer.
1100 int page_id = 0;
1101 std::vector<string16> values;
1102 std::vector<string16> labels;
1103 std::vector<string16> icons;
1104 std::vector<int> unique_ids;
1105 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1106 &unique_ids));
1107
1108 string16 expected_values[] = {
1109 ASCIIToUTF16("Elvis"),
1110 ASCIIToUTF16("Charles")
1111 };
1112 string16 expected_labels[] = {
1113 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1114 ASCIIToUTF16("123 Apple St.")
1115 };
1116 string16 expected_icons[] = {string16(), string16()};
1117 int expected_unique_ids[] = {1, 2};
1118 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1119 kDefaultPageID, arraysize(expected_values), expected_values,
1120 expected_labels, expected_icons, expected_unique_ids);
1121
1122 const int kPageID2 = 2;
1123 autofill_test::CreateTestFormField(
1124 "Card Number", "cardnumber", "", "text", &field);
1125 GetAutofillSuggestions(kPageID2, form, field);
1126
1127 // No suggestions provided, so send an empty vector as the results.
1128 // This triggers the combined message send.
1129 AutocompleteSuggestionsReturned(std::vector<string16>());
1130
1131 // Test that we sent the credit card suggestions to the renderer.
1132 page_id = 0;
1133 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1134 &unique_ids));
1135
1136 string16 expected_values2[] = {
1137 ASCIIToUTF16("************3456"),
1138 ASCIIToUTF16("************8765")
1139 };
1140 string16 expected_labels2[] = {ASCIIToUTF16("*3456"), ASCIIToUTF16("*8765")};
1141 string16 expected_icons2[] = {
1142 ASCIIToUTF16("visaCC"),
1143 ASCIIToUTF16("genericCC")
1144 };
1145 int expected_unique_ids2[] = {
1146 autofill_manager_->GetPackedCreditCardID(4),
1147 autofill_manager_->GetPackedCreditCardID(5)
1148 };
1149 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1150 kPageID2, arraysize(expected_values2), expected_values2,
1151 expected_labels2, expected_icons2, expected_unique_ids2);
1152 }
1153
1154 // Test that for non-https forms with both address and credit card fields, we
1155 // only return address suggestions. Instead of credit card suggestions, we
1156 // should return a warning explaining that credit card profile suggestions are
1157 // unavailable when the form is not https.
TEST_F(AutofillManagerTest,GetAddressAndCreditCardSuggestionsNonHttps)1158 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1159 // Set up our form data.
1160 FormData form;
1161 CreateTestAddressFormData(&form);
1162 CreateTestCreditCardFormData(&form, false, false);
1163 std::vector<FormData> forms(1, form);
1164 FormsSeen(forms);
1165
1166 FormField field = form.fields[0];
1167 GetAutofillSuggestions(form, field);
1168
1169 // No suggestions provided, so send an empty vector as the results.
1170 // This triggers the combined message send.
1171 AutocompleteSuggestionsReturned(std::vector<string16>());
1172
1173 // Test that we sent the right address suggestions to the renderer.
1174 int page_id = 0;
1175 std::vector<string16> values;
1176 std::vector<string16> labels;
1177 std::vector<string16> icons;
1178 std::vector<int> unique_ids;
1179 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1180 &unique_ids));
1181
1182 string16 expected_values[] = {
1183 ASCIIToUTF16("Elvis"),
1184 ASCIIToUTF16("Charles")
1185 };
1186 string16 expected_labels[] = {
1187 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1188 ASCIIToUTF16("123 Apple St.")
1189 };
1190 string16 expected_icons[] = {string16(), string16()};
1191 int expected_unique_ids[] = {1, 2};
1192 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1193 kDefaultPageID, arraysize(expected_values), expected_values,
1194 expected_labels, expected_icons, expected_unique_ids);
1195
1196 autofill_test::CreateTestFormField(
1197 "Card Number", "cardnumber", "", "text", &field);
1198 const int kPageID2 = 2;
1199 GetAutofillSuggestions(kPageID2, form, field);
1200
1201 // No suggestions provided, so send an empty vector as the results.
1202 // This triggers the combined message send.
1203 AutocompleteSuggestionsReturned(std::vector<string16>());
1204
1205 // Test that we sent the right message to the renderer.
1206 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1207 &unique_ids));
1208
1209 string16 expected_values2[] = {
1210 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
1211 };
1212 string16 expected_labels2[] = {string16()};
1213 string16 expected_icons2[] = {string16()};
1214 int expected_unique_ids2[] = {-1};
1215 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1216 kPageID2, arraysize(expected_values2), expected_values2,
1217 expected_labels2, expected_icons2, expected_unique_ids2);
1218
1219 // Clear the test credit cards and try again -- we shouldn't return a warning.
1220 test_personal_data_->ClearCreditCards();
1221 GetAutofillSuggestions(form, field);
1222 EXPECT_FALSE(GetAutofillSuggestionsMessage(NULL, NULL, NULL, NULL, NULL));
1223 }
1224
1225 // Test that we correctly combine autofill and autocomplete suggestions.
TEST_F(AutofillManagerTest,GetCombinedAutofillAndAutocompleteSuggestions)1226 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1227 // Set up our form data.
1228 FormData form;
1229 CreateTestAddressFormData(&form);
1230 std::vector<FormData> forms(1, form);
1231 FormsSeen(forms);
1232
1233 const FormField& field = form.fields[0];
1234 GetAutofillSuggestions(form, field);
1235
1236 // Add some Autocomplete suggestions.
1237 // This triggers the combined message send.
1238 std::vector<string16> suggestions;
1239 suggestions.push_back(ASCIIToUTF16("Jay"));
1240 // This suggestion is a duplicate, and should be trimmed.
1241 suggestions.push_back(ASCIIToUTF16("Elvis"));
1242 suggestions.push_back(ASCIIToUTF16("Jason"));
1243 AutocompleteSuggestionsReturned(suggestions);
1244
1245 // Test that we sent the right message to the renderer.
1246 int page_id = 0;
1247 std::vector<string16> values;
1248 std::vector<string16> labels;
1249 std::vector<string16> icons;
1250 std::vector<int> unique_ids;
1251 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1252 &unique_ids));
1253
1254 string16 expected_values[] = {
1255 ASCIIToUTF16("Elvis"),
1256 ASCIIToUTF16("Charles"),
1257 ASCIIToUTF16("Jay"),
1258 ASCIIToUTF16("Jason")
1259 };
1260 string16 expected_labels[] = {
1261 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1262 ASCIIToUTF16("123 Apple St."),
1263 string16(),
1264 string16()
1265 };
1266 string16 expected_icons[] = {string16(), string16(), string16(), string16()};
1267 int expected_unique_ids[] = {1, 2, 0, 0};
1268 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1269 kDefaultPageID, arraysize(expected_values), expected_values,
1270 expected_labels, expected_icons, expected_unique_ids);
1271 }
1272
1273 // Test that we return autocomplete-like suggestions when trying to autofill
1274 // already filled forms.
TEST_F(AutofillManagerTest,GetFieldSuggestionsWhenFormIsAutofilled)1275 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1276 // Set up our form data.
1277 FormData form;
1278 CreateTestAddressFormData(&form);
1279 std::vector<FormData> forms(1, form);
1280 FormsSeen(forms);
1281
1282 // Mark one of the fields as filled.
1283 form.fields[2].is_autofilled = true;
1284 const FormField& field = form.fields[0];
1285 GetAutofillSuggestions(form, field);
1286
1287 // No suggestions provided, so send an empty vector as the results.
1288 // This triggers the combined message send.
1289 AutocompleteSuggestionsReturned(std::vector<string16>());
1290
1291 // Test that we sent the right message to the renderer.
1292 int page_id = 0;
1293 std::vector<string16> values;
1294 std::vector<string16> labels;
1295 std::vector<string16> icons;
1296 std::vector<int> unique_ids;
1297 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1298 &unique_ids));
1299 string16 expected_values[] = {
1300 ASCIIToUTF16("Elvis"),
1301 ASCIIToUTF16("Charles")
1302 };
1303 string16 expected_labels[] = {string16(), string16()};
1304 string16 expected_icons[] = {string16(), string16()};
1305 int expected_unique_ids[] = {1, 2};
1306 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1307 kDefaultPageID, arraysize(expected_values), expected_values,
1308 expected_labels, expected_icons, expected_unique_ids);
1309 }
1310
1311 // Test that nothing breaks when there are autocomplete suggestions but no
1312 // autofill suggestions.
TEST_F(AutofillManagerTest,GetFieldSuggestionsForAutocompleteOnly)1313 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1314 // Set up our form data.
1315 FormData form;
1316 CreateTestAddressFormData(&form);
1317 FormField field;
1318 autofill_test::CreateTestFormField(
1319 "Some Field", "somefield", "", "text", &field);
1320 form.fields.push_back(field);
1321 std::vector<FormData> forms(1, form);
1322 FormsSeen(forms);
1323
1324 GetAutofillSuggestions(form, field);
1325
1326 // Add some Autocomplete suggestions.
1327 // This triggers the combined message send.
1328 std::vector<string16> suggestions;
1329 suggestions.push_back(ASCIIToUTF16("one"));
1330 suggestions.push_back(ASCIIToUTF16("two"));
1331 AutocompleteSuggestionsReturned(suggestions);
1332
1333 // Test that we sent the right message to the renderer.
1334 int page_id = 0;
1335 std::vector<string16> values;
1336 std::vector<string16> labels;
1337 std::vector<string16> icons;
1338 std::vector<int> unique_ids;
1339 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1340 &unique_ids));
1341
1342 string16 expected_values[] = {
1343 ASCIIToUTF16("one"),
1344 ASCIIToUTF16("two")
1345 };
1346 string16 expected_labels[] = {string16(), string16()};
1347 string16 expected_icons[] = {string16(), string16()};
1348 int expected_unique_ids[] = {0, 0};
1349 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1350 kDefaultPageID, arraysize(expected_values), expected_values,
1351 expected_labels, expected_icons, expected_unique_ids);
1352 }
1353
1354 // Test that we do not return duplicate values drawn from multiple profiles when
1355 // filling an already filled field.
TEST_F(AutofillManagerTest,GetFieldSuggestionsWithDuplicateValues)1356 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1357 // Set up our form data.
1358 FormData form;
1359 CreateTestAddressFormData(&form);
1360 std::vector<FormData> forms(1, form);
1361 FormsSeen(forms);
1362
1363 // |profile| will be owned by the mock PersonalDataManager.
1364 AutofillProfile* profile = new AutofillProfile;
1365 autofill_test::SetProfileInfo(profile, "Elvis", "", "", "", "",
1366 "", "", "", "", "", "", "", "");
1367 profile->set_guid("00000000-0000-0000-0000-000000000101");
1368 autofill_manager_->AddProfile(profile);
1369
1370 FormField& field = form.fields[0];
1371 field.is_autofilled = true;
1372 field.value = ASCIIToUTF16("Elvis");
1373 GetAutofillSuggestions(form, field);
1374
1375 // No suggestions provided, so send an empty vector as the results.
1376 // This triggers the combined message send.
1377 AutocompleteSuggestionsReturned(std::vector<string16>());
1378
1379 // Test that we sent the right message to the renderer.
1380 int page_id = 0;
1381 std::vector<string16> values;
1382 std::vector<string16> labels;
1383 std::vector<string16> icons;
1384 std::vector<int> unique_ids;
1385 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1386 &unique_ids));
1387
1388 string16 expected_values[] = { ASCIIToUTF16("Elvis") };
1389 string16 expected_labels[] = { string16() };
1390 string16 expected_icons[] = { string16() };
1391 int expected_unique_ids[] = { 1 };
1392 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1393 kDefaultPageID, arraysize(expected_values), expected_values,
1394 expected_labels, expected_icons, expected_unique_ids);
1395 }
1396
1397 // Test that a non-default value is suggested for multi-valued profile, on an
1398 // unfilled form.
TEST_F(AutofillManagerTest,GetFieldSuggestionsForMultiValuedProfileUnfilled)1399 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileUnfilled) {
1400 // Set up our form data.
1401 FormData form;
1402 CreateTestAddressFormData(&form);
1403 std::vector<FormData> forms(1, form);
1404 FormsSeen(forms);
1405
1406 // |profile| will be owned by the mock PersonalDataManager.
1407 AutofillProfile* profile = new AutofillProfile;
1408 autofill_test::SetProfileInfo(profile, "Elvis", "", "Presley", "me@x.com", "",
1409 "", "", "", "", "", "", "", "");
1410 profile->set_guid("00000000-0000-0000-0000-000000000101");
1411 std::vector<string16> multi_values(2);
1412 multi_values[0] = ASCIIToUTF16("Elvis Presley");
1413 multi_values[1] = ASCIIToUTF16("Cynthia Love");
1414 profile->SetMultiInfo(NAME_FULL, multi_values);
1415 autofill_manager_->AddProfile(profile);
1416
1417 // Get the first name field. And start out with "Cy", hoping for "Cynthia".
1418 FormField& field = form.fields[0];
1419 field.value = ASCIIToUTF16("Cy");
1420 field.is_autofilled = false;
1421 GetAutofillSuggestions(form, field);
1422
1423 // Trigger the |Send|.
1424 AutocompleteSuggestionsReturned(std::vector<string16>());
1425
1426 // Test that we sent the right message to the renderer.
1427 int page_id = 0;
1428 std::vector<string16> values;
1429 std::vector<string16> labels;
1430 std::vector<string16> icons;
1431 std::vector<int> unique_ids;
1432 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1433 &unique_ids));
1434
1435 string16 expected_values[] = { ASCIIToUTF16("Cynthia") };
1436 string16 expected_labels[] = { ASCIIToUTF16("me@x.com") };
1437 string16 expected_icons[] = { string16() };
1438 int expected_unique_ids[] = { 101 };
1439 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1440 kDefaultPageID, arraysize(expected_values), expected_values,
1441 expected_labels, expected_icons, expected_unique_ids);
1442 }
1443
1444 // Test that all values are suggested for multi-valued profile, on a filled
1445 // form. This is the per-field "override" case.
TEST_F(AutofillManagerTest,GetFieldSuggestionsForMultiValuedProfileFilled)1446 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileFilled) {
1447 // Set up our form data.
1448 FormData form;
1449 CreateTestAddressFormData(&form);
1450 std::vector<FormData> forms(1, form);
1451 FormsSeen(forms);
1452
1453 // |profile| will be owned by the mock PersonalDataManager.
1454 AutofillProfile* profile = new AutofillProfile;
1455 profile->set_guid("00000000-0000-0000-0000-000000000102");
1456 std::vector<string16> multi_values(3);
1457 multi_values[0] = ASCIIToUTF16("Travis Smith");
1458 multi_values[1] = ASCIIToUTF16("Cynthia Love");
1459 multi_values[2] = ASCIIToUTF16("Zac Mango");
1460 profile->SetMultiInfo(NAME_FULL, multi_values);
1461 autofill_manager_->AddProfile(profile);
1462
1463 // Get the first name field. And start out with "Travis", hoping for all the
1464 // multi-valued variants as suggestions.
1465 FormField& field = form.fields[0];
1466 field.value = ASCIIToUTF16("Travis");
1467 field.is_autofilled = true;
1468 GetAutofillSuggestions(form, field);
1469
1470 // Trigger the |Send|.
1471 AutocompleteSuggestionsReturned(std::vector<string16>());
1472
1473 // Test that we sent the right message to the renderer.
1474 int page_id = 0;
1475 std::vector<string16> values;
1476 std::vector<string16> labels;
1477 std::vector<string16> icons;
1478 std::vector<int> unique_ids;
1479 EXPECT_TRUE(GetAutofillSuggestionsMessage(&page_id, &values, &labels, &icons,
1480 &unique_ids));
1481
1482 string16 expected_values[] = {
1483 ASCIIToUTF16("Travis"),
1484 ASCIIToUTF16("Cynthia"),
1485 ASCIIToUTF16("Zac")
1486 };
1487 string16 expected_labels[] = { string16(), string16(), string16() };
1488 string16 expected_icons[] = { string16(), string16(), string16() };
1489 int expected_unique_ids[] = { 102, 102, 102 };
1490 ExpectSuggestions(page_id, values, labels, icons, unique_ids,
1491 kDefaultPageID, arraysize(expected_values), expected_values,
1492 expected_labels, expected_icons, expected_unique_ids);
1493 }
1494
1495 // Test that we correctly fill an address form.
TEST_F(AutofillManagerTest,FillAddressForm)1496 TEST_F(AutofillManagerTest, FillAddressForm) {
1497 // Set up our form data.
1498 FormData form;
1499 CreateTestAddressFormData(&form);
1500 std::vector<FormData> forms(1, form);
1501 FormsSeen(forms);
1502
1503 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1504 GUIDPair empty(std::string(), 0);
1505 FillAutofillFormData(
1506 kDefaultPageID, form, form.fields[0],
1507 autofill_manager_->PackGUIDs(empty, guid));
1508
1509 int page_id = 0;
1510 FormData results;
1511 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1512 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1513 }
1514
1515 // Test that we correctly fill a credit card form.
TEST_F(AutofillManagerTest,FillCreditCardForm)1516 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1517 // Set up our form data.
1518 FormData form;
1519 CreateTestCreditCardFormData(&form, true, false);
1520 std::vector<FormData> forms(1, form);
1521 FormsSeen(forms);
1522
1523 GUIDPair guid("00000000-0000-0000-0000-000000000004", 0);
1524 GUIDPair empty(std::string(), 0);
1525 FillAutofillFormData(
1526 kDefaultPageID, form, *form.fields.begin(),
1527 autofill_manager_->PackGUIDs(guid, empty));
1528
1529 int page_id = 0;
1530 FormData results;
1531 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1532 ExpectFilledCreditCardFormElvis(page_id, results, kDefaultPageID, false);
1533 }
1534
1535 // Test that we correctly fill a credit card form with month input type.
1536 // 1. year empty, month empty
TEST_F(AutofillManagerTest,FillCreditCardFormNoYearNoMonth)1537 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1538 // Same as the SetUp(), but generate 4 credit cards with year month
1539 // combination.
1540 test_personal_data_->CreateTestCreditCardsYearAndMonth("", "");
1541 // Set up our form data.
1542 FormData form;
1543 CreateTestCreditCardFormData(&form, true, true);
1544 std::vector<FormData> forms(1, form);
1545 FormsSeen(forms);
1546
1547 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1548 GUIDPair empty(std::string(), 0);
1549 FillAutofillFormData(
1550 kDefaultPageID, form, *form.fields.begin(),
1551 autofill_manager_->PackGUIDs(guid, empty));
1552
1553 int page_id = 0;
1554 FormData results;
1555 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1556 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1557 kDefaultPageID, false, "", "");
1558 }
1559
1560
1561 // Test that we correctly fill a credit card form with month input type.
1562 // 2. year empty, month non-empty
TEST_F(AutofillManagerTest,FillCreditCardFormNoYearMonth)1563 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1564 // Same as the SetUp(), but generate 4 credit cards with year month
1565 // combination.
1566 test_personal_data_->CreateTestCreditCardsYearAndMonth("", "04");
1567 // Set up our form data.
1568 FormData form;
1569 CreateTestCreditCardFormData(&form, true, true);
1570 std::vector<FormData> forms(1, form);
1571 FormsSeen(forms);
1572
1573 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1574 GUIDPair empty(std::string(), 0);
1575 FillAutofillFormData(
1576 kDefaultPageID, form, *form.fields.begin(),
1577 autofill_manager_->PackGUIDs(guid, empty));
1578
1579 int page_id = 0;
1580 FormData results;
1581 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1582 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1583 kDefaultPageID, false, "", "04");
1584 }
1585
1586 // Test that we correctly fill a credit card form with month input type.
1587 // 3. year non-empty, month empty
TEST_F(AutofillManagerTest,FillCreditCardFormYearNoMonth)1588 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1589 // Same as the SetUp(), but generate 4 credit cards with year month
1590 // combination.
1591 test_personal_data_->CreateTestCreditCardsYearAndMonth("2012", "");
1592 // Set up our form data.
1593 FormData form;
1594 CreateTestCreditCardFormData(&form, true, true);
1595 std::vector<FormData> forms(1, form);
1596 FormsSeen(forms);
1597
1598 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1599 GUIDPair empty(std::string(), 0);
1600 FillAutofillFormData(
1601 kDefaultPageID, form, *form.fields.begin(),
1602 autofill_manager_->PackGUIDs(guid, empty));
1603
1604 int page_id = 0;
1605 FormData results;
1606 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1607 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1608 kDefaultPageID, false, "2012", "");
1609 }
1610
1611 // Test that we correctly fill a credit card form with month input type.
1612 // 4. year non-empty, month empty
TEST_F(AutofillManagerTest,FillCreditCardFormYearMonth)1613 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1614 // Same as the SetUp(), but generate 4 credit cards with year month
1615 // combination.
1616 test_personal_data_->ClearCreditCards();
1617 test_personal_data_->CreateTestCreditCardsYearAndMonth("2012", "04");
1618 // Set up our form data.
1619 FormData form;
1620 CreateTestCreditCardFormData(&form, true, true);
1621 std::vector<FormData> forms(1, form);
1622 FormsSeen(forms);
1623
1624 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1625 GUIDPair empty(std::string(), 0);
1626 FillAutofillFormData(
1627 kDefaultPageID, form, *form.fields.begin(),
1628 autofill_manager_->PackGUIDs(guid, empty));
1629
1630 int page_id = 0;
1631 FormData results;
1632 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1633 ExpectFilledCreditCardYearMonthWithYearMonth(page_id, results,
1634 kDefaultPageID, false, "2012", "04");
1635 }
1636
1637 // Test that we correctly fill a combined address and credit card form.
TEST_F(AutofillManagerTest,FillAddressAndCreditCardForm)1638 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1639 // Set up our form data.
1640 FormData form;
1641 CreateTestAddressFormData(&form);
1642 CreateTestCreditCardFormData(&form, true, false);
1643 std::vector<FormData> forms(1, form);
1644 FormsSeen(forms);
1645
1646 // First fill the address data.
1647 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1648 GUIDPair empty(std::string(), 0);
1649 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1650 autofill_manager_->PackGUIDs(empty, guid));
1651
1652 int page_id = 0;
1653 FormData results;
1654 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1655 {
1656 SCOPED_TRACE("Address");
1657 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, true);
1658 }
1659
1660 // Now fill the credit card data.
1661 const int kPageID2 = 2;
1662 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
1663 FillAutofillFormData(
1664 kPageID2, form, form.fields.back(),
1665 autofill_manager_->PackGUIDs(guid2, empty));
1666
1667 page_id = 0;
1668 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1669 {
1670 SCOPED_TRACE("Credit card");
1671 ExpectFilledCreditCardFormElvis(page_id, results, kPageID2, true);
1672 }
1673 }
1674
1675 // Test that we correctly fill a form that has multiple logical sections, e.g.
1676 // both a billing and a shipping address.
TEST_F(AutofillManagerTest,FillFormWithMultipleSections)1677 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1678 // Set up our form data.
1679 FormData form;
1680 CreateTestAddressFormData(&form);
1681 const size_t kAddressFormSize = form.fields.size();
1682 CreateTestAddressFormData(&form);
1683 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1684 // Make sure the fields have distinct names.
1685 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1686 }
1687 std::vector<FormData> forms(1, form);
1688 FormsSeen(forms);
1689
1690 // Fill the first section.
1691 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1692 GUIDPair empty(std::string(), 0);
1693 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1694 autofill_manager_->PackGUIDs(empty, guid));
1695
1696 int page_id = 0;
1697 FormData results;
1698 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1699 {
1700 SCOPED_TRACE("Address 1");
1701
1702 // The second address section should be empty.
1703 ASSERT_EQ(results.fields.size(), 2*kAddressFormSize);
1704 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1705 EXPECT_EQ(string16(), results.fields[i].value);
1706 }
1707
1708 // The first address section should be filled with Elvis's data.
1709 results.fields.resize(kAddressFormSize);
1710 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1711 }
1712
1713 // Fill the second section, with the initiating field somewhere in the middle
1714 // of the section.
1715 const int kPageID2 = 2;
1716 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0);
1717 ASSERT_LT(9U, kAddressFormSize);
1718 FillAutofillFormData(kPageID2, form, form.fields[kAddressFormSize + 9],
1719 autofill_manager_->PackGUIDs(empty, guid2));
1720
1721 page_id = 0;
1722 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1723 {
1724 SCOPED_TRACE("Address 2");
1725 ASSERT_EQ(results.fields.size(), form.fields.size());
1726
1727 // The first address section should be empty.
1728 ASSERT_EQ(results.fields.size(), 2*kAddressFormSize);
1729 for (size_t i = 0; i < kAddressFormSize; ++i) {
1730 EXPECT_EQ(string16(), results.fields[i].value);
1731 }
1732
1733 // The second address section should be filled with Elvis's data.
1734 FormData secondSection = results;
1735 secondSection.fields.erase(secondSection.fields.begin(),
1736 secondSection.fields.begin() + kAddressFormSize);
1737 for (size_t i = 0; i < kAddressFormSize; ++i) {
1738 // Restore the expected field names.
1739 string16 name = secondSection.fields[i].name;
1740 string16 original_name = name.substr(0, name.size() - 1);
1741 secondSection.fields[i].name = original_name;
1742 }
1743 ExpectFilledAddressFormElvis(page_id, secondSection, kPageID2, false);
1744 }
1745 }
1746
1747 // Test that we correctly fill a form that has a single logical section with
1748 // multiple email address fields.
TEST_F(AutofillManagerTest,FillFormWithMultipleEmails)1749 TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) {
1750 // Set up our form data.
1751 FormData form;
1752 CreateTestAddressFormData(&form);
1753 FormField field;
1754 autofill_test::CreateTestFormField(
1755 "Confirm email", "email2", "", "text", &field);
1756 form.fields.push_back(field);
1757
1758 std::vector<FormData> forms(1, form);
1759 FormsSeen(forms);
1760
1761 // Fill the form.
1762 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1763 GUIDPair empty(std::string(), 0);
1764 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1765 autofill_manager_->PackGUIDs(empty, guid));
1766
1767 int page_id = 0;
1768 FormData results;
1769 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1770
1771 // The second email address should be filled.
1772 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"), results.fields.back().value);
1773
1774 // The remainder of the form should be filled as usual.
1775 results.fields.pop_back();
1776 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1777 }
1778
1779 // Test that we correctly fill a previously auto-filled form.
TEST_F(AutofillManagerTest,FillAutofilledForm)1780 TEST_F(AutofillManagerTest, FillAutofilledForm) {
1781 // Set up our form data.
1782 FormData form;
1783 CreateTestAddressFormData(&form);
1784 // Mark one of the address fields as autofilled.
1785 form.fields[4].is_autofilled = true;
1786 CreateTestCreditCardFormData(&form, true, false);
1787 std::vector<FormData> forms(1, form);
1788 FormsSeen(forms);
1789
1790 // First fill the address data.
1791 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1792 GUIDPair empty(std::string(), 0);
1793 FillAutofillFormData(
1794 kDefaultPageID, form, *form.fields.begin(),
1795 autofill_manager_->PackGUIDs(empty, guid));
1796
1797 int page_id = 0;
1798 FormData results;
1799 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1800 {
1801 SCOPED_TRACE("Address");
1802 ExpectFilledForm(page_id, results, kDefaultPageID,
1803 "Elvis", "", "", "", "", "", "", "", "", "", "", "",
1804 "", "", "", "", true, true, false);
1805 }
1806
1807 // Now fill the credit card data.
1808 const int kPageID2 = 2;
1809 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
1810 FillAutofillFormData(
1811 kPageID2, form, form.fields.back(),
1812 autofill_manager_->PackGUIDs(guid2, empty));
1813
1814 page_id = 0;
1815 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1816 {
1817 SCOPED_TRACE("Credit card 1");
1818 ExpectFilledCreditCardFormElvis(page_id, results, kPageID2, true);
1819 }
1820
1821 // Now set the credit card fields to also be auto-filled, and try again to
1822 // fill the credit card data
1823 for (std::vector<FormField>::iterator iter = form.fields.begin();
1824 iter != form.fields.end();
1825 ++iter) {
1826 iter->is_autofilled = true;
1827 }
1828
1829 const int kPageID3 = 3;
1830 FillAutofillFormData(
1831 kPageID3, form, *form.fields.rbegin(),
1832 autofill_manager_->PackGUIDs(guid2, empty));
1833
1834 page_id = 0;
1835 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1836 {
1837 SCOPED_TRACE("Credit card 2");
1838 ExpectFilledForm(page_id, results, kPageID3,
1839 "", "", "", "", "", "", "", "", "", "", "", "",
1840 "", "", "", "2012", true, true, false);
1841 }
1842 }
1843
1844 // Test that we correctly fill a phone number split across multiple fields.
TEST_F(AutofillManagerTest,FillPhoneNumber)1845 TEST_F(AutofillManagerTest, FillPhoneNumber) {
1846 // Set up our form data.
1847 FormData form;
1848 form.name = ASCIIToUTF16("MyPhoneForm");
1849 form.method = ASCIIToUTF16("POST");
1850 form.origin = GURL("http://myform.com/phone_form.html");
1851 form.action = GURL("http://myform.com/phone_submit.html");
1852 form.user_submitted = true;
1853
1854 FormField field;
1855 autofill_test::CreateTestFormField(
1856 "country code", "country code", "", "text", &field);
1857 field.max_length = 1;
1858 form.fields.push_back(field);
1859 autofill_test::CreateTestFormField(
1860 "area code", "area code", "", "text", &field);
1861 field.max_length = 3;
1862 form.fields.push_back(field);
1863 autofill_test::CreateTestFormField(
1864 "phone", "phone prefix", "1", "text", &field);
1865 field.max_length = 3;
1866 form.fields.push_back(field);
1867 autofill_test::CreateTestFormField(
1868 "-", "phone suffix", "", "text", &field);
1869 field.max_length = 4;
1870 form.fields.push_back(field);
1871 autofill_test::CreateTestFormField(
1872 "Phone Extension", "ext", "", "text", &field);
1873 field.max_length = 3;
1874 form.fields.push_back(field);
1875
1876 std::vector<FormData> forms(1, form);
1877 FormsSeen(forms);
1878
1879 AutofillProfile *work_profile = autofill_manager_->GetProfileWithGUID(
1880 "00000000-0000-0000-0000-000000000002");
1881 ASSERT_TRUE(work_profile != NULL);
1882 string16 saved_phone = work_profile->GetInfo(PHONE_HOME_NUMBER);
1883
1884 GUIDPair guid(work_profile->guid(), 0);
1885 GUIDPair empty(std::string(), 0);
1886
1887 char test_data[] = "1234567890123456";
1888 for (int i = arraysize(test_data) - 1; i >= 0; --i) {
1889 test_data[i] = 0;
1890 SCOPED_TRACE(StringPrintf("Testing phone: %s", test_data));
1891 work_profile->SetInfo(PHONE_HOME_NUMBER, ASCIIToUTF16(test_data));
1892 // The page ID sent to the AutofillManager from the RenderView, used to send
1893 // an IPC message back to the renderer.
1894 int page_id = 100 - i;
1895 FillAutofillFormData(
1896 page_id, form, *form.fields.begin(),
1897 autofill_manager_->PackGUIDs(empty, guid));
1898 page_id = 0;
1899 FormData results;
1900 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1901
1902 if (i != 7) {
1903 EXPECT_EQ(ASCIIToUTF16(test_data), results.fields[2].value);
1904 EXPECT_EQ(ASCIIToUTF16(test_data), results.fields[3].value);
1905 } else {
1906 // The only size that is parsed and split, right now is 7:
1907 EXPECT_EQ(ASCIIToUTF16("123"), results.fields[2].value);
1908 EXPECT_EQ(ASCIIToUTF16("4567"), results.fields[3].value);
1909 }
1910 }
1911
1912 work_profile->SetInfo(PHONE_HOME_NUMBER, saved_phone);
1913 }
1914
1915 // Test that we can still fill a form when a field has been removed from it.
TEST_F(AutofillManagerTest,FormChangesRemoveField)1916 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
1917 // Set up our form data.
1918 FormData form;
1919 CreateTestAddressFormData(&form);
1920
1921 // Add a field -- we'll remove it again later.
1922 FormField field;
1923 autofill_test::CreateTestFormField("Some", "field", "", "text", &field);
1924 form.fields.insert(form.fields.begin() + 3, field);
1925
1926 std::vector<FormData> forms(1, form);
1927 FormsSeen(forms);
1928
1929 // Now, after the call to |FormsSeen|, we remove the field before filling.
1930 form.fields.erase(form.fields.begin() + 3);
1931
1932 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1933 GUIDPair empty(std::string(), 0);
1934 FillAutofillFormData(
1935 kDefaultPageID, form, form.fields[0],
1936 autofill_manager_->PackGUIDs(empty, guid));
1937
1938 int page_id = 0;
1939 FormData results;
1940 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1941 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1942 }
1943
1944 // Test that we can still fill a form when a field has been added to it.
TEST_F(AutofillManagerTest,FormChangesAddField)1945 TEST_F(AutofillManagerTest, FormChangesAddField) {
1946 // The offset of the fax field in the address form.
1947 const int kFaxFieldOffset = 10;
1948
1949 // Set up our form data.
1950 FormData form;
1951 CreateTestAddressFormData(&form);
1952
1953 // Remove the fax field -- we'll add it back later.
1954 std::vector<FormField>::iterator pos = form.fields.begin() + kFaxFieldOffset;
1955 FormField field = *pos;
1956 pos = form.fields.erase(pos);
1957
1958 std::vector<FormData> forms(1, form);
1959 FormsSeen(forms);
1960
1961 // Now, after the call to |FormsSeen|, we restore the field before filling.
1962 form.fields.insert(pos, field);
1963
1964 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1965 GUIDPair empty(std::string(), 0);
1966 FillAutofillFormData(
1967 kDefaultPageID, form, form.fields[0],
1968 autofill_manager_->PackGUIDs(empty, guid));
1969
1970 int page_id = 0;
1971 FormData results;
1972 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1973 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1974 }
1975
1976 // Test that we are able to save form data when forms are submitted.
TEST_F(AutofillManagerTest,FormSubmitted)1977 TEST_F(AutofillManagerTest, FormSubmitted) {
1978 // Set up our form data.
1979 FormData form;
1980 CreateTestAddressFormData(&form);
1981 std::vector<FormData> forms(1, form);
1982 FormsSeen(forms);
1983
1984 // Fill the form.
1985 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1986 GUIDPair empty(std::string(), 0);
1987 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
1988 autofill_manager_->PackGUIDs(empty, guid));
1989
1990 int page_id = 0;
1991 FormData results;
1992 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
1993 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
1994
1995 // Simulate form submission. We should call into the PDM to try to save the
1996 // filled data.
1997 EXPECT_CALL(*test_personal_data_, SaveImportedProfile(::testing::_)).Times(1);
1998 FormSubmitted(results);
1999 }
2000
2001 // Test that we are able to save form data when forms are submitted and we only
2002 // have server data for the field types.
TEST_F(AutofillManagerTest,FormSubmittedServerTypes)2003 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2004 // Set up our form data.
2005 FormData form;
2006 CreateTestAddressFormData(&form);
2007
2008 // Simulate having seen this form on page load.
2009 // |form_structure| will be owned by |autofill_manager_|.
2010 TestFormStructure* form_structure = new TestFormStructure(form);
2011 form_structure->DetermineHeuristicTypes();
2012
2013 // Clear the heuristic types, and instead set the appropriate server types.
2014 std::vector<AutofillFieldType> heuristic_types, server_types;
2015 for (size_t i = 0; i < form.fields.size(); ++i) {
2016 heuristic_types.push_back(UNKNOWN_TYPE);
2017 server_types.push_back(form_structure->field(i)->type());
2018 }
2019 form_structure->SetFieldTypes(heuristic_types, server_types);
2020 autofill_manager_->AddSeenForm(form_structure);
2021
2022 // Fill the form.
2023 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2024 GUIDPair empty(std::string(), 0);
2025 FillAutofillFormData(kDefaultPageID, form, form.fields[0],
2026 autofill_manager_->PackGUIDs(empty, guid));
2027
2028 int page_id = 0;
2029 FormData results;
2030 EXPECT_TRUE(GetAutofillFormDataFilledMessage(&page_id, &results));
2031 ExpectFilledAddressFormElvis(page_id, results, kDefaultPageID, false);
2032
2033 // Simulate form submission. We should call into the PDM to try to save the
2034 // filled data.
2035 EXPECT_CALL(*test_personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2036 FormSubmitted(results);
2037 }
2038
2039 // Checks that resetting the auxiliary profile enabled preference does the right
2040 // thing on all platforms.
TEST_F(AutofillManagerTest,AuxiliaryProfilesReset)2041 TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) {
2042 #if defined(OS_MACOSX)
2043 // Auxiliary profiles is implemented on Mac only. It enables Mac Address
2044 // Book integration.
2045 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
2046 prefs::kAutofillAuxiliaryProfilesEnabled));
2047 profile()->GetPrefs()->SetBoolean(
2048 prefs::kAutofillAuxiliaryProfilesEnabled, false);
2049 profile()->GetPrefs()->ClearPref(prefs::kAutofillAuxiliaryProfilesEnabled);
2050 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
2051 prefs::kAutofillAuxiliaryProfilesEnabled));
2052 #else
2053 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
2054 prefs::kAutofillAuxiliaryProfilesEnabled));
2055 profile()->GetPrefs()->SetBoolean(
2056 prefs::kAutofillAuxiliaryProfilesEnabled, true);
2057 profile()->GetPrefs()->ClearPref(prefs::kAutofillAuxiliaryProfilesEnabled);
2058 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
2059 prefs::kAutofillAuxiliaryProfilesEnabled));
2060 #endif
2061 }
2062