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

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

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

Powered by Google App Engine
This is Rietveld 408576698