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