form_structure_unittest.cc revision a3f7b4e666c476898878fa745f637129375cd889
1// 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 "components/autofill/core/browser/form_structure.h" 6 7#include "base/memory/scoped_ptr.h" 8#include "base/strings/string_util.h" 9#include "base/strings/utf_string_conversions.h" 10#include "components/autofill/content/browser/autocheckout_page_meta_data.h" 11#include "components/autofill/core/browser/autofill_metrics.h" 12#include "components/autofill/core/common/form_data.h" 13#include "components/autofill/core/common/form_field_data.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "third_party/WebKit/public/web/WebInputElement.h" 16#include "url/gurl.h" 17 18using WebKit::WebInputElement; 19 20namespace autofill { 21namespace { 22 23// Unlike the base AutofillMetrics, exposes copy and assignment constructors, 24// which are handy for briefer test code. The AutofillMetrics class is 25// stateless, so this is safe. 26class TestAutofillMetrics : public AutofillMetrics { 27 public: 28 TestAutofillMetrics() {} 29 virtual ~TestAutofillMetrics() {} 30}; 31 32} // anonymous namespace 33 34 35namespace content { 36 37std::ostream& operator<<(std::ostream& os, const FormData& form) { 38 os << UTF16ToUTF8(form.name) 39 << " " 40 << UTF16ToUTF8(form.method) 41 << " " 42 << form.origin.spec() 43 << " " 44 << form.action.spec() 45 << " "; 46 47 for (std::vector<FormFieldData>::const_iterator iter = 48 form.fields.begin(); 49 iter != form.fields.end(); ++iter) { 50 os << *iter 51 << " "; 52 } 53 54 return os; 55} 56 57} // namespace content 58 59class FormStructureTest { 60 public: 61 static std::string Hash64Bit(const std::string& str) { 62 return FormStructure::Hash64Bit(str); 63 } 64}; 65 66TEST(FormStructureTest, FieldCount) { 67 scoped_ptr<FormStructure> form_structure; 68 FormData form; 69 form.method = ASCIIToUTF16("post"); 70 71 FormFieldData field; 72 field.label = ASCIIToUTF16("username"); 73 field.name = ASCIIToUTF16("username"); 74 field.form_control_type = "text"; 75 form.fields.push_back(field); 76 77 field.label = ASCIIToUTF16("password"); 78 field.name = ASCIIToUTF16("password"); 79 field.form_control_type = "password"; 80 form.fields.push_back(field); 81 82 field.label = base::string16(); 83 field.name = ASCIIToUTF16("Submit"); 84 field.form_control_type = "submit"; 85 form.fields.push_back(field); 86 87 field.label = ASCIIToUTF16("address1"); 88 field.name = ASCIIToUTF16("address1"); 89 field.form_control_type = "text"; 90 field.should_autocomplete = false; 91 form.fields.push_back(field); 92 93 // The render process sends all fields to browser including fields with 94 // autocomplete=off 95 form_structure.reset(new FormStructure(form, std::string())); 96 EXPECT_EQ(4U, form_structure->field_count()); 97 98 // We expect the same count when autocheckout is enabled. 99 form_structure.reset(new FormStructure(form, "http://fake_url")); 100 EXPECT_EQ(4U, form_structure->field_count()); 101} 102 103TEST(FormStructureTest, AutofillCount) { 104 scoped_ptr<FormStructure> form_structure; 105 FormData form; 106 form.method = ASCIIToUTF16("post"); 107 108 FormFieldData field; 109 field.label = ASCIIToUTF16("username"); 110 field.name = ASCIIToUTF16("username"); 111 field.form_control_type = "text"; 112 form.fields.push_back(field); 113 114 field.label = ASCIIToUTF16("password"); 115 field.name = ASCIIToUTF16("password"); 116 field.form_control_type = "password"; 117 form.fields.push_back(field); 118 119 field.label = ASCIIToUTF16("state"); 120 field.name = ASCIIToUTF16("state"); 121 field.form_control_type = "select-one"; 122 form.fields.push_back(field); 123 124 field.label = base::string16(); 125 field.name = ASCIIToUTF16("Submit"); 126 field.form_control_type = "submit"; 127 form.fields.push_back(field); 128 129 // Only text and select fields that are heuristically matched are counted. 130 form_structure.reset(new FormStructure(form, std::string())); 131 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 132 EXPECT_EQ(1U, form_structure->autofill_count()); 133 134 // Add a field with should_autocomplete=false. 135 field.label = ASCIIToUTF16("address1"); 136 field.name = ASCIIToUTF16("address1"); 137 field.form_control_type = "text"; 138 field.should_autocomplete = false; 139 form.fields.push_back(field); 140 141 form_structure.reset(new FormStructure(form, std::string())); 142 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 143 // DetermineHeuristicTypes also assign field type for fields with 144 // autocomplete=off thus autofill_count includes them. This is a bug, 145 // and they should not be counted. See http://crbug.com/176432 for details. 146 // TODO(benquan): change it to EXPECT_EQ(1U, ... when the bug is fixed. 147 EXPECT_EQ(2U, form_structure->autofill_count()); 148 149 // All fields should be counted when Autocheckout is enabled. 150 form_structure.reset(new FormStructure(form, "http://fake_url")); 151 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 152 EXPECT_EQ(2U, form_structure->autofill_count()); 153} 154 155TEST(FormStructureTest, SourceURL) { 156 FormData form; 157 form.origin = GURL("http://www.foo.com/"); 158 form.method = ASCIIToUTF16("post"); 159 FormStructure form_structure(form, std::string()); 160 161 EXPECT_EQ(form.origin, form_structure.source_url()); 162} 163 164TEST(FormStructureTest, IsAutofillable) { 165 scoped_ptr<FormStructure> form_structure; 166 FormData form; 167 168 // We need at least three text fields to be auto-fillable. 169 form.method = ASCIIToUTF16("post"); 170 171 FormFieldData field; 172 // When autocheckout is enabled, we enable autofill even the form has 173 // no fields 174 form_structure.reset(new FormStructure(form, "http://fake_url")); 175 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 176 EXPECT_TRUE(form_structure->IsAutofillable(true)); 177 178 field.label = ASCIIToUTF16("username"); 179 field.name = ASCIIToUTF16("username"); 180 field.form_control_type = "text"; 181 form.fields.push_back(field); 182 183 field.label = ASCIIToUTF16("password"); 184 field.name = ASCIIToUTF16("password"); 185 field.form_control_type = "password"; 186 form.fields.push_back(field); 187 188 field.label = base::string16(); 189 field.name = ASCIIToUTF16("Submit"); 190 field.form_control_type = "submit"; 191 form.fields.push_back(field); 192 193 form_structure.reset(new FormStructure(form, std::string())); 194 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 195 EXPECT_FALSE(form_structure->IsAutofillable(true)); 196 197 // We do not limit to three text fields when autocheckout is enabled. 198 form_structure.reset(new FormStructure(form, "http://fake_url")); 199 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 200 EXPECT_TRUE(form_structure->IsAutofillable(true)); 201 202 // We now have three text fields, but only two auto-fillable fields. 203 field.label = ASCIIToUTF16("First Name"); 204 field.name = ASCIIToUTF16("firstname"); 205 field.form_control_type = "text"; 206 form.fields.push_back(field); 207 208 field.label = ASCIIToUTF16("Last Name"); 209 field.name = ASCIIToUTF16("lastname"); 210 field.form_control_type = "text"; 211 form.fields.push_back(field); 212 213 form_structure.reset(new FormStructure(form, std::string())); 214 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 215 EXPECT_FALSE(form_structure->IsAutofillable(true)); 216 217 // We now have three auto-fillable fields. 218 field.label = ASCIIToUTF16("Email"); 219 field.name = ASCIIToUTF16("email"); 220 field.form_control_type = "email"; 221 form.fields.push_back(field); 222 223 form_structure.reset(new FormStructure(form, std::string())); 224 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 225 EXPECT_TRUE(form_structure->IsAutofillable(true)); 226 227 // The method must be 'post', though we can intentionally ignore this 228 // criterion for the sake of providing a helpful warning message to the user. 229 form.method = ASCIIToUTF16("get"); 230 form_structure.reset(new FormStructure(form, std::string())); 231 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 232 EXPECT_FALSE(form_structure->IsAutofillable(true)); 233 EXPECT_TRUE(form_structure->IsAutofillable(false)); 234 235 // The target cannot include http(s)://*/search... 236 form.method = ASCIIToUTF16("post"); 237 form.action = GURL("http://google.com/search?q=hello"); 238 form_structure.reset(new FormStructure(form, std::string())); 239 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 240 EXPECT_FALSE(form_structure->IsAutofillable(true)); 241 242 // But search can be in the URL. 243 form.action = GURL("http://search.com/?q=hello"); 244 form_structure.reset(new FormStructure(form, std::string())); 245 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 246 EXPECT_TRUE(form_structure->IsAutofillable(true)); 247} 248 249TEST(FormStructureTest, ShouldBeParsed) { 250 scoped_ptr<FormStructure> form_structure; 251 FormData form; 252 253 // We need at least three text fields to be parseable. 254 form.method = ASCIIToUTF16("post"); 255 256 FormFieldData field; 257 field.label = ASCIIToUTF16("username"); 258 field.name = ASCIIToUTF16("username"); 259 field.form_control_type = "text"; 260 form.fields.push_back(field); 261 262 FormFieldData checkable_field; 263 checkable_field.is_checkable = true; 264 checkable_field.name = ASCIIToUTF16("radiobtn"); 265 checkable_field.form_control_type = "radio"; 266 form.fields.push_back(checkable_field); 267 268 checkable_field.name = ASCIIToUTF16("checkbox"); 269 checkable_field.form_control_type = "checkbox"; 270 form.fields.push_back(checkable_field); 271 272 // We have only one text field, should not be parsed. 273 form_structure.reset(new FormStructure(form, std::string())); 274 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); 275 276 // The form should be parsed for autocheckout even it has less than three 277 // text fields. 278 form_structure.reset(new FormStructure(form, "http://fake_url")); 279 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); 280 281 // We now have three text fields, though only two are auto-fillable. 282 field.label = ASCIIToUTF16("First Name"); 283 field.name = ASCIIToUTF16("firstname"); 284 field.form_control_type = "text"; 285 form.fields.push_back(field); 286 287 field.label = ASCIIToUTF16("Last Name"); 288 field.name = ASCIIToUTF16("lastname"); 289 field.form_control_type = "text"; 290 form.fields.push_back(field); 291 292 form_structure.reset(new FormStructure(form, std::string())); 293 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); 294 295 // The method must be 'post', though we can intentionally ignore this 296 // criterion for the sake of providing a helpful warning message to the user. 297 form.method = ASCIIToUTF16("get"); 298 form_structure.reset(new FormStructure(form, std::string())); 299 EXPECT_FALSE(form_structure->IsAutofillable(true)); 300 EXPECT_TRUE(form_structure->ShouldBeParsed(false)); 301 302 // The target cannot include http(s)://*/search... 303 form.method = ASCIIToUTF16("post"); 304 form.action = GURL("http://google.com/search?q=hello"); 305 form_structure.reset(new FormStructure(form, std::string())); 306 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); 307 308 // But search can be in the URL. 309 form.action = GURL("http://search.com/?q=hello"); 310 form_structure.reset(new FormStructure(form, std::string())); 311 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); 312 313 // The form need only have three fields, but at least one must be a text 314 // field. 315 form.fields.clear(); 316 317 field.label = ASCIIToUTF16("Email"); 318 field.name = ASCIIToUTF16("email"); 319 field.form_control_type = "email"; 320 form.fields.push_back(field); 321 322 field.label = ASCIIToUTF16("State"); 323 field.name = ASCIIToUTF16("state"); 324 field.form_control_type = "select-one"; 325 form.fields.push_back(field); 326 327 field.label = ASCIIToUTF16("Country"); 328 field.name = ASCIIToUTF16("country"); 329 field.form_control_type = "select-one"; 330 form.fields.push_back(field); 331 332 form_structure.reset(new FormStructure(form, std::string())); 333 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); 334 335 form.fields[0].form_control_type = "select-one"; 336 // Now, no text fields. 337 form_structure.reset(new FormStructure(form, std::string())); 338 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); 339 340 // It should be parsed when autocheckout is enabled. 341 form_structure.reset(new FormStructure(form, "http://fake_url")); 342 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); 343} 344 345TEST(FormStructureTest, HeuristicsContactInfo) { 346 scoped_ptr<FormStructure> form_structure; 347 FormData form; 348 form.method = ASCIIToUTF16("post"); 349 350 FormFieldData field; 351 field.form_control_type = "text"; 352 353 field.label = ASCIIToUTF16("First Name"); 354 field.name = ASCIIToUTF16("firstname"); 355 form.fields.push_back(field); 356 357 field.label = ASCIIToUTF16("Last Name"); 358 field.name = ASCIIToUTF16("lastname"); 359 form.fields.push_back(field); 360 361 field.label = ASCIIToUTF16("Email"); 362 field.name = ASCIIToUTF16("email"); 363 form.fields.push_back(field); 364 365 field.label = ASCIIToUTF16("Phone"); 366 field.name = ASCIIToUTF16("phone"); 367 form.fields.push_back(field); 368 369 field.label = ASCIIToUTF16("Address"); 370 field.name = ASCIIToUTF16("address"); 371 form.fields.push_back(field); 372 373 field.label = ASCIIToUTF16("City"); 374 field.name = ASCIIToUTF16("city"); 375 form.fields.push_back(field); 376 377 field.label = ASCIIToUTF16("Zip code"); 378 field.name = ASCIIToUTF16("zipcode"); 379 form.fields.push_back(field); 380 381 field.label = base::string16(); 382 field.name = ASCIIToUTF16("Submit"); 383 field.form_control_type = "submit"; 384 form.fields.push_back(field); 385 386 form_structure.reset(new FormStructure(form, std::string())); 387 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 388 EXPECT_TRUE(form_structure->IsAutofillable(true)); 389 390 // Expect the correct number of fields. 391 ASSERT_EQ(8U, form_structure->field_count()); 392 ASSERT_EQ(7U, form_structure->autofill_count()); 393 394 // First name. 395 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 396 // Last name. 397 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 398 // Email. 399 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 400 // Phone. 401 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 402 form_structure->field(3)->heuristic_type()); 403 // Address. 404 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); 405 // City. 406 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type()); 407 // Zip. 408 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); 409 // Submit. 410 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); 411} 412 413// Verify that we can correctly process the |autocomplete| attribute. 414TEST(FormStructureTest, HeuristicsAutocompleteAttribute) { 415 scoped_ptr<FormStructure> form_structure; 416 FormData form; 417 form.method = ASCIIToUTF16("post"); 418 419 FormFieldData field; 420 field.form_control_type = "text"; 421 422 field.label = base::string16(); 423 field.name = ASCIIToUTF16("field1"); 424 field.autocomplete_attribute = "given-name"; 425 form.fields.push_back(field); 426 427 field.label = base::string16(); 428 field.name = ASCIIToUTF16("field2"); 429 field.autocomplete_attribute = "family-name"; 430 form.fields.push_back(field); 431 432 field.label = base::string16(); 433 field.name = ASCIIToUTF16("field3"); 434 field.autocomplete_attribute = "email"; 435 form.fields.push_back(field); 436 437 form_structure.reset(new FormStructure(form, std::string())); 438 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 439 EXPECT_TRUE(form_structure->IsAutofillable(true)); 440 441 // Expect the correct number of fields. 442 ASSERT_EQ(3U, form_structure->field_count()); 443 ASSERT_EQ(3U, form_structure->autofill_count()); 444 445 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 446 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 447 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 448} 449 450// Verify that we can correctly process the 'autocomplete' attribute for phone 451// number types (especially phone prefixes and suffixes). 452TEST(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) { 453 scoped_ptr<FormStructure> form_structure; 454 FormData form; 455 form.method = ASCIIToUTF16("post"); 456 457 FormFieldData field; 458 field.form_control_type = "text"; 459 460 field.label = base::string16(); 461 field.name = ASCIIToUTF16("field1"); 462 field.autocomplete_attribute = "tel-local"; 463 form.fields.push_back(field); 464 465 field.label = base::string16(); 466 field.name = ASCIIToUTF16("field2"); 467 field.autocomplete_attribute = "tel-local-prefix"; 468 form.fields.push_back(field); 469 470 field.label = base::string16(); 471 field.name = ASCIIToUTF16("field3"); 472 field.autocomplete_attribute = "tel-local-suffix"; 473 form.fields.push_back(field); 474 475 form_structure.reset(new FormStructure(form, std::string())); 476 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 477 EXPECT_TRUE(form_structure->IsAutofillable(true)); 478 479 // Expect the correct number of fields. 480 ASSERT_EQ(3U, form_structure->field_count()); 481 EXPECT_EQ(3U, form_structure->autofill_count()); 482 483 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(0)->heuristic_type()); 484 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part()); 485 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(1)->heuristic_type()); 486 EXPECT_EQ(AutofillField::PHONE_PREFIX, 487 form_structure->field(1)->phone_part()); 488 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(2)->heuristic_type()); 489 EXPECT_EQ(AutofillField::PHONE_SUFFIX, 490 form_structure->field(2)->phone_part()); 491} 492 493// If at least one field includes type hints in the 'autocomplete' attribute, we 494// should not try to apply any other heuristics. 495TEST(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) { 496 scoped_ptr<FormStructure> form_structure; 497 FormData form; 498 form.method = ASCIIToUTF16("post"); 499 500 // Start with a regular contact form. 501 FormFieldData field; 502 field.form_control_type = "text"; 503 504 field.label = ASCIIToUTF16("First Name"); 505 field.name = ASCIIToUTF16("firstname"); 506 form.fields.push_back(field); 507 508 field.label = ASCIIToUTF16("Last Name"); 509 field.name = ASCIIToUTF16("lastname"); 510 form.fields.push_back(field); 511 512 field.label = ASCIIToUTF16("Email"); 513 field.name = ASCIIToUTF16("email"); 514 form.fields.push_back(field); 515 516 form_structure.reset(new FormStructure(form, std::string())); 517 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 518 EXPECT_TRUE(form_structure->IsAutofillable(true)); 519 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced()); 520 521 ASSERT_EQ(3U, form_structure->field_count()); 522 ASSERT_EQ(3U, form_structure->autofill_count()); 523 524 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 525 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 526 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 527 528 // Now update the first form field to include an 'autocomplete' attribute. 529 form.fields.front().autocomplete_attribute = "x-other"; 530 form_structure.reset(new FormStructure(form, std::string())); 531 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 532 EXPECT_FALSE(form_structure->IsAutofillable(true)); 533 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); 534 535 ASSERT_EQ(3U, form_structure->field_count()); 536 ASSERT_EQ(0U, form_structure->autofill_count()); 537 538 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); 539 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 540 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 541 542 // When Autocheckout is enabled, we should ignore 'autocomplete' attribute 543 // when deciding to crowdsource. 544 form_structure.reset(new FormStructure(form, "http://fake.url")); 545 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 546 EXPECT_TRUE(form_structure->IsAutofillable(true)); 547 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced()); 548 549 ASSERT_EQ(3U, form_structure->field_count()); 550 ASSERT_EQ(0U, form_structure->autofill_count()); 551 552 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); 553 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 554 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 555} 556 557// Verify that we can correctly process sections listed in the |autocomplete| 558// attribute. 559TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) { 560 FormData form; 561 form.method = ASCIIToUTF16("post"); 562 563 FormFieldData field; 564 field.form_control_type = "text"; 565 566 // Some fields will have no section specified. These fall into the default 567 // section. 568 field.autocomplete_attribute = "email"; 569 form.fields.push_back(field); 570 571 // We allow arbitrary section names. 572 field.autocomplete_attribute = "section-foo email"; 573 form.fields.push_back(field); 574 575 // "shipping" and "billing" are special section tokens that don't require the 576 // "section-" prefix. 577 field.autocomplete_attribute = "shipping email"; 578 form.fields.push_back(field); 579 field.autocomplete_attribute = "billing email"; 580 form.fields.push_back(field); 581 582 // "shipping" and "billing" can be combined with other section names. 583 field.autocomplete_attribute = "section-foo shipping email"; 584 form.fields.push_back(field); 585 field.autocomplete_attribute = "section-foo billing email"; 586 form.fields.push_back(field); 587 588 // We don't do anything clever to try to coalesce sections; it's up to site 589 // authors to avoid typos. 590 field.autocomplete_attribute = "section--foo email"; 591 form.fields.push_back(field); 592 593 // "shipping email" and "section--shipping" email should be parsed as 594 // different sections. This is only an interesting test due to how we 595 // implement implicit section names from attributes like "shipping email"; see 596 // the implementation for more details. 597 field.autocomplete_attribute = "section--shipping email"; 598 form.fields.push_back(field); 599 600 // Credit card fields are implicitly in a separate section from other fields. 601 field.autocomplete_attribute = "section-foo cc-number"; 602 form.fields.push_back(field); 603 604 FormStructure form_structure(form, std::string()); 605 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 606 EXPECT_TRUE(form_structure.IsAutofillable(true)); 607 608 // Expect the correct number of fields. 609 ASSERT_EQ(9U, form_structure.field_count()); 610 EXPECT_EQ(9U, form_structure.autofill_count()); 611 612 // All of the fields in this form should be parsed as belonging to different 613 // sections. 614 std::set<std::string> section_names; 615 for (size_t i = 0; i < 9; ++i) { 616 section_names.insert(form_structure.field(i)->section()); 617 } 618 EXPECT_EQ(9U, section_names.size()); 619} 620 621// Verify that we can correctly process a degenerate section listed in the 622// |autocomplete| attribute. 623TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsDegenerate) { 624 FormData form; 625 form.method = ASCIIToUTF16("post"); 626 627 FormFieldData field; 628 field.form_control_type = "text"; 629 630 // Some fields will have no section specified. These fall into the default 631 // section. 632 field.autocomplete_attribute = "email"; 633 form.fields.push_back(field); 634 635 // Specifying "section-" is equivalent to not specifying a section. 636 field.autocomplete_attribute = "section- email"; 637 form.fields.push_back(field); 638 639 // Invalid tokens should prevent us from setting a section name. 640 field.autocomplete_attribute = "garbage section-foo email"; 641 form.fields.push_back(field); 642 field.autocomplete_attribute = "garbage section-bar email"; 643 form.fields.push_back(field); 644 field.autocomplete_attribute = "garbage shipping email"; 645 form.fields.push_back(field); 646 field.autocomplete_attribute = "garbage billing email"; 647 form.fields.push_back(field); 648 649 FormStructure form_structure(form, std::string()); 650 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 651 652 // Expect the correct number of fields. 653 ASSERT_EQ(6U, form_structure.field_count()); 654 EXPECT_EQ(2U, form_structure.autofill_count()); 655 656 // All of the fields in this form should be parsed as belonging to the same 657 // section. 658 std::set<std::string> section_names; 659 for (size_t i = 0; i < 6; ++i) { 660 section_names.insert(form_structure.field(i)->section()); 661 } 662 EXPECT_EQ(1U, section_names.size()); 663} 664 665// Verify that we can correctly process repeated sections listed in the 666// |autocomplete| attribute. 667TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) { 668 FormData form; 669 form.method = ASCIIToUTF16("post"); 670 671 FormFieldData field; 672 field.form_control_type = "text"; 673 674 field.autocomplete_attribute = "section-foo email"; 675 form.fields.push_back(field); 676 field.autocomplete_attribute = "section-foo address-line1"; 677 form.fields.push_back(field); 678 679 FormStructure form_structure(form, std::string()); 680 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 681 682 // Expect the correct number of fields. 683 ASSERT_EQ(2U, form_structure.field_count()); 684 EXPECT_EQ(2U, form_structure.autofill_count()); 685 686 // All of the fields in this form should be parsed as belonging to the same 687 // section. 688 std::set<std::string> section_names; 689 for (size_t i = 0; i < 2; ++i) { 690 section_names.insert(form_structure.field(i)->section()); 691 } 692 EXPECT_EQ(1U, section_names.size()); 693} 694 695// Verify that we do not override the author-specified sections from a form with 696// local heuristics. 697TEST(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) { 698 FormData form; 699 form.method = ASCIIToUTF16("post"); 700 701 FormFieldData field; 702 field.form_control_type = "text"; 703 704 field.name = ASCIIToUTF16("one"); 705 field.autocomplete_attribute = "address-line1"; 706 form.fields.push_back(field); 707 field.name = base::string16(); 708 field.autocomplete_attribute = "section-foo email"; 709 form.fields.push_back(field); 710 field.name = base::string16(); 711 field.autocomplete_attribute = "name"; 712 form.fields.push_back(field); 713 field.name = ASCIIToUTF16("two"); 714 field.autocomplete_attribute = "address-line1"; 715 form.fields.push_back(field); 716 717 FormStructure form_structure(form, std::string()); 718 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 719 720 // Expect the correct number of fields. 721 ASSERT_EQ(4U, form_structure.field_count()); 722 EXPECT_EQ(4U, form_structure.autofill_count()); 723 724 // Normally, the two separate address fields would cause us to detect two 725 // separate sections; but because there is an author-specified section in this 726 // form, we do not apply these usual heuristics. 727 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name); 728 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name); 729 EXPECT_EQ(form_structure.field(0)->section(), 730 form_structure.field(3)->section()); 731} 732 733TEST(FormStructureTest, HeuristicsSample8) { 734 scoped_ptr<FormStructure> form_structure; 735 FormData form; 736 form.method = ASCIIToUTF16("post"); 737 738 FormFieldData field; 739 field.form_control_type = "text"; 740 741 field.label = ASCIIToUTF16("Your First Name:"); 742 field.name = ASCIIToUTF16("bill.first"); 743 form.fields.push_back(field); 744 745 field.label = ASCIIToUTF16("Your Last Name:"); 746 field.name = ASCIIToUTF16("bill.last"); 747 form.fields.push_back(field); 748 749 field.label = ASCIIToUTF16("Street Address Line 1:"); 750 field.name = ASCIIToUTF16("bill.street1"); 751 form.fields.push_back(field); 752 753 field.label = ASCIIToUTF16("Street Address Line 2:"); 754 field.name = ASCIIToUTF16("bill.street2"); 755 form.fields.push_back(field); 756 757 field.label = ASCIIToUTF16("City"); 758 field.name = ASCIIToUTF16("bill.city"); 759 form.fields.push_back(field); 760 761 field.label = ASCIIToUTF16("State (U.S.):"); 762 field.name = ASCIIToUTF16("bill.state"); 763 form.fields.push_back(field); 764 765 field.label = ASCIIToUTF16("Zip/Postal Code:"); 766 field.name = ASCIIToUTF16("BillTo.PostalCode"); 767 form.fields.push_back(field); 768 769 field.label = ASCIIToUTF16("Country:"); 770 field.name = ASCIIToUTF16("bill.country"); 771 form.fields.push_back(field); 772 773 field.label = ASCIIToUTF16("Phone Number:"); 774 field.name = ASCIIToUTF16("BillTo.Phone"); 775 form.fields.push_back(field); 776 777 field.label = base::string16(); 778 field.name = ASCIIToUTF16("Submit"); 779 field.form_control_type = "submit"; 780 form.fields.push_back(field); 781 782 form_structure.reset(new FormStructure(form, std::string())); 783 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 784 EXPECT_TRUE(form_structure->IsAutofillable(true)); 785 ASSERT_EQ(10U, form_structure->field_count()); 786 ASSERT_EQ(9U, form_structure->autofill_count()); 787 788 // First name. 789 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 790 // Last name. 791 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 792 // Address. 793 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type()); 794 // Address. 795 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type()); 796 // City. 797 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(4)->heuristic_type()); 798 // State. 799 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(5)->heuristic_type()); 800 // Zip. 801 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(6)->heuristic_type()); 802 // Country. 803 EXPECT_EQ(ADDRESS_BILLING_COUNTRY, 804 form_structure->field(7)->heuristic_type()); 805 // Phone. 806 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 807 form_structure->field(8)->heuristic_type()); 808 // Submit. 809 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type()); 810} 811 812TEST(FormStructureTest, HeuristicsSample6) { 813 scoped_ptr<FormStructure> form_structure; 814 FormData form; 815 form.method = ASCIIToUTF16("post"); 816 817 FormFieldData field; 818 field.form_control_type = "text"; 819 820 field.label = ASCIIToUTF16("E-mail address"); 821 field.name = ASCIIToUTF16("email"); 822 form.fields.push_back(field); 823 824 field.label = ASCIIToUTF16("Full name"); 825 field.name = ASCIIToUTF16("name"); 826 form.fields.push_back(field); 827 828 field.label = ASCIIToUTF16("Company"); 829 field.name = ASCIIToUTF16("company"); 830 form.fields.push_back(field); 831 832 field.label = ASCIIToUTF16("Address"); 833 field.name = ASCIIToUTF16("address"); 834 form.fields.push_back(field); 835 836 field.label = ASCIIToUTF16("City"); 837 field.name = ASCIIToUTF16("city"); 838 form.fields.push_back(field); 839 840 field.label = ASCIIToUTF16("Zip Code"); 841 field.name = ASCIIToUTF16("Home.PostalCode"); 842 form.fields.push_back(field); 843 844 field.label = base::string16(); 845 field.name = ASCIIToUTF16("Submit"); 846 field.value = ASCIIToUTF16("continue"); 847 field.form_control_type = "submit"; 848 form.fields.push_back(field); 849 850 form_structure.reset(new FormStructure(form, std::string())); 851 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 852 EXPECT_TRUE(form_structure->IsAutofillable(true)); 853 ASSERT_EQ(7U, form_structure->field_count()); 854 ASSERT_EQ(6U, form_structure->autofill_count()); 855 856 // Email. 857 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type()); 858 // Full name. 859 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type()); 860 // Company 861 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); 862 // Address. 863 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type()); 864 // City. 865 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); 866 // Zip. 867 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type()); 868 // Submit. 869 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); 870} 871 872// Tests a sequence of FormFields where only labels are supplied to heuristics 873// for matching. This works because FormFieldData labels are matched in the 874// case that input element ids (or |name| fields) are missing. 875TEST(FormStructureTest, HeuristicsLabelsOnly) { 876 scoped_ptr<FormStructure> form_structure; 877 FormData form; 878 form.method = ASCIIToUTF16("post"); 879 880 FormFieldData field; 881 field.form_control_type = "text"; 882 883 field.label = ASCIIToUTF16("First Name"); 884 field.name = base::string16(); 885 form.fields.push_back(field); 886 887 field.label = ASCIIToUTF16("Last Name"); 888 field.name = base::string16(); 889 form.fields.push_back(field); 890 891 field.label = ASCIIToUTF16("Email"); 892 field.name = base::string16(); 893 form.fields.push_back(field); 894 895 field.label = ASCIIToUTF16("Phone"); 896 field.name = base::string16(); 897 form.fields.push_back(field); 898 899 field.label = ASCIIToUTF16("Address"); 900 field.name = base::string16(); 901 form.fields.push_back(field); 902 903 field.label = ASCIIToUTF16("Address"); 904 field.name = base::string16(); 905 form.fields.push_back(field); 906 907 field.label = ASCIIToUTF16("Zip code"); 908 field.name = base::string16(); 909 form.fields.push_back(field); 910 911 field.label = base::string16(); 912 field.name = ASCIIToUTF16("Submit"); 913 field.form_control_type = "submit"; 914 form.fields.push_back(field); 915 916 form_structure.reset(new FormStructure(form, std::string())); 917 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 918 EXPECT_TRUE(form_structure->IsAutofillable(true)); 919 ASSERT_EQ(8U, form_structure->field_count()); 920 ASSERT_EQ(7U, form_structure->autofill_count()); 921 922 // First name. 923 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 924 // Last name. 925 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 926 // Email. 927 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 928 // Phone. 929 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 930 form_structure->field(3)->heuristic_type()); 931 // Address. 932 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); 933 // Address Line 2. 934 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type()); 935 // Zip. 936 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); 937 // Submit. 938 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); 939} 940 941TEST(FormStructureTest, HeuristicsCreditCardInfo) { 942 scoped_ptr<FormStructure> form_structure; 943 FormData form; 944 form.method = ASCIIToUTF16("post"); 945 946 FormFieldData field; 947 field.form_control_type = "text"; 948 949 field.label = ASCIIToUTF16("Name on Card"); 950 field.name = ASCIIToUTF16("name_on_card"); 951 form.fields.push_back(field); 952 953 field.label = ASCIIToUTF16("Card Number"); 954 field.name = ASCIIToUTF16("card_number"); 955 form.fields.push_back(field); 956 957 field.label = ASCIIToUTF16("Exp Month"); 958 field.name = ASCIIToUTF16("ccmonth"); 959 form.fields.push_back(field); 960 961 field.label = ASCIIToUTF16("Exp Year"); 962 field.name = ASCIIToUTF16("ccyear"); 963 form.fields.push_back(field); 964 965 field.label = ASCIIToUTF16("Verification"); 966 field.name = ASCIIToUTF16("verification"); 967 form.fields.push_back(field); 968 969 field.label = base::string16(); 970 field.name = ASCIIToUTF16("Submit"); 971 field.form_control_type = "submit"; 972 form.fields.push_back(field); 973 974 form_structure.reset(new FormStructure(form, std::string())); 975 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 976 EXPECT_TRUE(form_structure->IsAutofillable(true)); 977 ASSERT_EQ(6U, form_structure->field_count()); 978 ASSERT_EQ(5U, form_structure->autofill_count()); 979 980 // Credit card name. 981 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 982 // Credit card number. 983 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type()); 984 // Credit card expiration month. 985 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type()); 986 // Credit card expiration year. 987 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 988 form_structure->field(3)->heuristic_type()); 989 // CVV. 990 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 991 form_structure->field(4)->heuristic_type()); 992 // Submit. 993 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type()); 994} 995 996TEST(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) { 997 scoped_ptr<FormStructure> form_structure; 998 FormData form; 999 form.method = ASCIIToUTF16("post"); 1000 1001 FormFieldData field; 1002 field.form_control_type = "text"; 1003 1004 field.label = ASCIIToUTF16("Name on Card"); 1005 field.name = ASCIIToUTF16("name_on_card"); 1006 form.fields.push_back(field); 1007 1008 // This is not a field we know how to process. But we should skip over it 1009 // and process the other fields in the card block. 1010 field.label = ASCIIToUTF16("Card image"); 1011 field.name = ASCIIToUTF16("card_image"); 1012 form.fields.push_back(field); 1013 1014 field.label = ASCIIToUTF16("Card Number"); 1015 field.name = ASCIIToUTF16("card_number"); 1016 form.fields.push_back(field); 1017 1018 field.label = ASCIIToUTF16("Exp Month"); 1019 field.name = ASCIIToUTF16("ccmonth"); 1020 form.fields.push_back(field); 1021 1022 field.label = ASCIIToUTF16("Exp Year"); 1023 field.name = ASCIIToUTF16("ccyear"); 1024 form.fields.push_back(field); 1025 1026 field.label = ASCIIToUTF16("Verification"); 1027 field.name = ASCIIToUTF16("verification"); 1028 form.fields.push_back(field); 1029 1030 field.label = base::string16(); 1031 field.name = ASCIIToUTF16("Submit"); 1032 field.form_control_type = "submit"; 1033 form.fields.push_back(field); 1034 1035 form_structure.reset(new FormStructure(form, std::string())); 1036 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1037 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1038 ASSERT_EQ(7U, form_structure->field_count()); 1039 ASSERT_EQ(5U, form_structure->autofill_count()); 1040 1041 // Credit card name. 1042 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 1043 // Credit card type. This is an unknown type but related to the credit card. 1044 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 1045 // Credit card number. 1046 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); 1047 // Credit card expiration month. 1048 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 1049 // Credit card expiration year. 1050 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 1051 form_structure->field(4)->heuristic_type()); 1052 // CVV. 1053 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 1054 form_structure->field(5)->heuristic_type()); 1055 // Submit. 1056 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); 1057} 1058 1059TEST(FormStructureTest, ThreeAddressLines) { 1060 scoped_ptr<FormStructure> form_structure; 1061 FormData form; 1062 form.method = ASCIIToUTF16("post"); 1063 1064 FormFieldData field; 1065 field.form_control_type = "text"; 1066 1067 field.label = ASCIIToUTF16("Address Line1"); 1068 field.name = ASCIIToUTF16("Address"); 1069 form.fields.push_back(field); 1070 1071 field.label = ASCIIToUTF16("Address Line2"); 1072 field.name = ASCIIToUTF16("Address"); 1073 form.fields.push_back(field); 1074 1075 field.label = ASCIIToUTF16("Address Line3"); 1076 field.name = ASCIIToUTF16("Address"); 1077 form.fields.push_back(field); 1078 1079 field.label = ASCIIToUTF16("City"); 1080 field.name = ASCIIToUTF16("city"); 1081 form.fields.push_back(field); 1082 1083 form_structure.reset(new FormStructure(form, std::string())); 1084 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1085 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1086 ASSERT_EQ(4U, form_structure->field_count()); 1087 ASSERT_EQ(3U, form_structure->autofill_count()); 1088 1089 // Address Line 1. 1090 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1091 // Address Line 2. 1092 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1093 // Address Line 3. 1094 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1095 // City. 1096 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); 1097} 1098 1099// This test verifies that "addressLine1" and "addressLine2" matches heuristics. 1100// This occured in https://www.gorillaclothing.com/. http://crbug.com/52126. 1101TEST(FormStructureTest, BillingAndShippingAddresses) { 1102 scoped_ptr<FormStructure> form_structure; 1103 FormData form; 1104 form.method = ASCIIToUTF16("post"); 1105 1106 FormFieldData field; 1107 field.form_control_type = "text"; 1108 1109 field.label = ASCIIToUTF16("Address Line1"); 1110 field.name = ASCIIToUTF16("shipping.address.addressLine1"); 1111 form.fields.push_back(field); 1112 1113 field.label = ASCIIToUTF16("Address Line2"); 1114 field.name = ASCIIToUTF16("shipping.address.addressLine2"); 1115 form.fields.push_back(field); 1116 1117 field.label = ASCIIToUTF16("Address Line1"); 1118 field.name = ASCIIToUTF16("billing.address.addressLine1"); 1119 form.fields.push_back(field); 1120 1121 field.label = ASCIIToUTF16("Address Line2"); 1122 field.name = ASCIIToUTF16("billing.address.addressLine2"); 1123 form.fields.push_back(field); 1124 1125 form_structure.reset(new FormStructure(form, std::string())); 1126 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1127 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1128 ASSERT_EQ(4U, form_structure->field_count()); 1129 ASSERT_EQ(4U, form_structure->autofill_count()); 1130 1131 // Address Line 1. 1132 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1133 // Address Line 2. 1134 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1135 // Address Line 1. 1136 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type()); 1137 // Address Line 2. 1138 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type()); 1139} 1140 1141// Numbered address lines after line two are ignored. 1142TEST(FormStructureTest, SurplusAddressLinesIgnored) { 1143 scoped_ptr<FormStructure> form_structure; 1144 FormData form; 1145 form.method = ASCIIToUTF16("post"); 1146 1147 FormFieldData field; 1148 field.form_control_type = "text"; 1149 1150 field.label = ASCIIToUTF16("Address Line1"); 1151 field.name = ASCIIToUTF16("shipping.address.addressLine1"); 1152 form.fields.push_back(field); 1153 1154 field.label = ASCIIToUTF16("Address Line2"); 1155 field.name = ASCIIToUTF16("shipping.address.addressLine2"); 1156 form.fields.push_back(field); 1157 1158 field.label = ASCIIToUTF16("Address Line3"); 1159 field.name = ASCIIToUTF16("billing.address.addressLine3"); 1160 form.fields.push_back(field); 1161 1162 field.label = ASCIIToUTF16("Address Line4"); 1163 field.name = ASCIIToUTF16("billing.address.addressLine4"); 1164 form.fields.push_back(field); 1165 1166 form_structure.reset(new FormStructure(form, std::string())); 1167 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1168 ASSERT_EQ(4U, form_structure->field_count()); 1169 ASSERT_EQ(2U, form_structure->autofill_count()); 1170 1171 // Address Line 1. 1172 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1173 // Address Line 2. 1174 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1175 // Address Line 3 (ignored). 1176 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1177 // Address Line 4 (ignored). 1178 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type()); 1179} 1180 1181// This example comes from expedia.com where they use a "Suite" label to 1182// indicate a suite or apartment number. We interpret this as address line 2. 1183// And the following "Street address second line" we interpret as address line 1184// 3 and discard. 1185// See http://crbug.com/48197 for details. 1186TEST(FormStructureTest, ThreeAddressLinesExpedia) { 1187 scoped_ptr<FormStructure> form_structure; 1188 FormData form; 1189 form.method = ASCIIToUTF16("post"); 1190 1191 FormFieldData field; 1192 field.form_control_type = "text"; 1193 1194 field.label = ASCIIToUTF16("Street:"); 1195 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1"); 1196 form.fields.push_back(field); 1197 1198 field.label = ASCIIToUTF16("Suite or Apt:"); 1199 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap"); 1200 form.fields.push_back(field); 1201 1202 field.label = ASCIIToUTF16("Street address second line"); 1203 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2"); 1204 form.fields.push_back(field); 1205 1206 field.label = ASCIIToUTF16("City:"); 1207 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct"); 1208 form.fields.push_back(field); 1209 1210 form_structure.reset(new FormStructure(form, std::string())); 1211 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1212 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1213 ASSERT_EQ(4U, form_structure->field_count()); 1214 EXPECT_EQ(3U, form_structure->autofill_count()); 1215 1216 // Address Line 1. 1217 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1218 // Suite / Apt. 1219 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1220 // Address Line 3. 1221 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1222 // City. 1223 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); 1224} 1225 1226// This example comes from ebay.com where the word "suite" appears in the label 1227// and the name "address2" clearly indicates that this is the address line 2. 1228// See http://crbug.com/48197 for details. 1229TEST(FormStructureTest, TwoAddressLinesEbay) { 1230 scoped_ptr<FormStructure> form_structure; 1231 FormData form; 1232 form.method = ASCIIToUTF16("post"); 1233 1234 FormFieldData field; 1235 field.form_control_type = "text"; 1236 1237 field.label = ASCIIToUTF16("Address Line1"); 1238 field.name = ASCIIToUTF16("address1"); 1239 form.fields.push_back(field); 1240 1241 field.label = ASCIIToUTF16("Floor number, suite number, etc"); 1242 field.name = ASCIIToUTF16("address2"); 1243 form.fields.push_back(field); 1244 1245 field.label = ASCIIToUTF16("City:"); 1246 field.name = ASCIIToUTF16("city"); 1247 form.fields.push_back(field); 1248 1249 form_structure.reset(new FormStructure(form, std::string())); 1250 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1251 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1252 ASSERT_EQ(3U, form_structure->field_count()); 1253 ASSERT_EQ(3U, form_structure->autofill_count()); 1254 1255 // Address Line 1. 1256 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1257 // Address Line 2. 1258 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1259 // City. 1260 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type()); 1261} 1262 1263TEST(FormStructureTest, HeuristicsStateWithProvince) { 1264 scoped_ptr<FormStructure> form_structure; 1265 FormData form; 1266 form.method = ASCIIToUTF16("post"); 1267 1268 FormFieldData field; 1269 field.form_control_type = "text"; 1270 1271 field.label = ASCIIToUTF16("Address Line1"); 1272 field.name = ASCIIToUTF16("Address"); 1273 form.fields.push_back(field); 1274 1275 field.label = ASCIIToUTF16("Address Line2"); 1276 field.name = ASCIIToUTF16("Address"); 1277 form.fields.push_back(field); 1278 1279 field.label = ASCIIToUTF16("State/Province/Region"); 1280 field.name = ASCIIToUTF16("State"); 1281 form.fields.push_back(field); 1282 1283 form_structure.reset(new FormStructure(form, std::string())); 1284 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1285 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1286 ASSERT_EQ(3U, form_structure->field_count()); 1287 ASSERT_EQ(3U, form_structure->autofill_count()); 1288 1289 // Address Line 1. 1290 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1291 // Address Line 2. 1292 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1293 // State. 1294 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type()); 1295} 1296 1297// This example comes from lego.com's checkout page. 1298TEST(FormStructureTest, HeuristicsWithBilling) { 1299 scoped_ptr<FormStructure> form_structure; 1300 FormData form; 1301 form.method = ASCIIToUTF16("post"); 1302 1303 FormFieldData field; 1304 field.form_control_type = "text"; 1305 1306 field.label = ASCIIToUTF16("First Name*:"); 1307 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox"); 1308 form.fields.push_back(field); 1309 1310 field.label = ASCIIToUTF16("Last Name*:"); 1311 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox"); 1312 form.fields.push_back(field); 1313 1314 field.label = ASCIIToUTF16("Company Name:"); 1315 field.name = ASCIIToUTF16("editBillingAddress$companyBox"); 1316 form.fields.push_back(field); 1317 1318 field.label = ASCIIToUTF16("Address*:"); 1319 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box"); 1320 form.fields.push_back(field); 1321 1322 field.label = ASCIIToUTF16("Apt/Suite :"); 1323 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box"); 1324 form.fields.push_back(field); 1325 1326 field.label = ASCIIToUTF16("City*:"); 1327 field.name = ASCIIToUTF16("editBillingAddress$cityBox"); 1328 form.fields.push_back(field); 1329 1330 field.label = ASCIIToUTF16("State/Province*:"); 1331 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown"); 1332 form.fields.push_back(field); 1333 1334 field.label = ASCIIToUTF16("Country*:"); 1335 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown"); 1336 form.fields.push_back(field); 1337 1338 field.label = ASCIIToUTF16("Postal Code*:"); 1339 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox"); 1340 form.fields.push_back(field); 1341 1342 field.label = ASCIIToUTF16("Phone*:"); 1343 field.name = ASCIIToUTF16("editBillingAddress$phoneBox"); 1344 form.fields.push_back(field); 1345 1346 field.label = ASCIIToUTF16("Email Address*:"); 1347 field.name = ASCIIToUTF16("email$emailBox"); 1348 form.fields.push_back(field); 1349 1350 form_structure.reset(new FormStructure(form, std::string())); 1351 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1352 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1353 ASSERT_EQ(11U, form_structure->field_count()); 1354 ASSERT_EQ(11U, form_structure->autofill_count()); 1355 1356 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 1357 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 1358 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); 1359 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(3)->heuristic_type()); 1360 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(4)->heuristic_type()); 1361 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(5)->heuristic_type()); 1362 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(6)->heuristic_type()); 1363 EXPECT_EQ(ADDRESS_BILLING_COUNTRY, 1364 form_structure->field(7)->heuristic_type()); 1365 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(8)->heuristic_type()); 1366 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 1367 form_structure->field(9)->heuristic_type()); 1368 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type()); 1369} 1370 1371TEST(FormStructureTest, ThreePartPhoneNumber) { 1372 scoped_ptr<FormStructure> form_structure; 1373 FormData form; 1374 form.method = ASCIIToUTF16("post"); 1375 1376 FormFieldData field; 1377 field.form_control_type = "text"; 1378 1379 field.label = ASCIIToUTF16("Phone:"); 1380 field.name = ASCIIToUTF16("dayphone1"); 1381 field.max_length = 0; 1382 form.fields.push_back(field); 1383 1384 field.label = ASCIIToUTF16("-"); 1385 field.name = ASCIIToUTF16("dayphone2"); 1386 field.max_length = 3; // Size of prefix is 3. 1387 form.fields.push_back(field); 1388 1389 field.label = ASCIIToUTF16("-"); 1390 field.name = ASCIIToUTF16("dayphone3"); 1391 field.max_length = 4; // Size of suffix is 4. If unlimited size is 1392 // passed, phone will be parsed as 1393 // <country code> - <area code> - <phone>. 1394 form.fields.push_back(field); 1395 1396 field.label = ASCIIToUTF16("ext.:"); 1397 field.name = ASCIIToUTF16("dayphone4"); 1398 field.max_length = 0; 1399 form.fields.push_back(field); 1400 1401 form_structure.reset(new FormStructure(form, std::string())); 1402 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1403 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1404 ASSERT_EQ(4U, form_structure->field_count()); 1405 ASSERT_EQ(3U, form_structure->autofill_count()); 1406 1407 // Area code. 1408 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type()); 1409 // Phone number suffix. 1410 EXPECT_EQ(PHONE_HOME_NUMBER, 1411 form_structure->field(1)->heuristic_type()); 1412 // Phone number suffix. 1413 EXPECT_EQ(PHONE_HOME_NUMBER, 1414 form_structure->field(2)->heuristic_type()); 1415 // Unknown. 1416 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type()); 1417} 1418 1419TEST(FormStructureTest, HeuristicsInfernoCC) { 1420 scoped_ptr<FormStructure> form_structure; 1421 FormData form; 1422 form.method = ASCIIToUTF16("post"); 1423 1424 FormFieldData field; 1425 field.form_control_type = "text"; 1426 1427 field.label = ASCIIToUTF16("Name on Card"); 1428 field.name = ASCIIToUTF16("name_on_card"); 1429 form.fields.push_back(field); 1430 1431 field.label = ASCIIToUTF16("Address"); 1432 field.name = ASCIIToUTF16("billing_address"); 1433 form.fields.push_back(field); 1434 1435 field.label = ASCIIToUTF16("Card Number"); 1436 field.name = ASCIIToUTF16("card_number"); 1437 form.fields.push_back(field); 1438 1439 field.label = ASCIIToUTF16("Expiration Date"); 1440 field.name = ASCIIToUTF16("expiration_month"); 1441 form.fields.push_back(field); 1442 1443 field.label = ASCIIToUTF16("Expiration Year"); 1444 field.name = ASCIIToUTF16("expiration_year"); 1445 form.fields.push_back(field); 1446 1447 form_structure.reset(new FormStructure(form, std::string())); 1448 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1449 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1450 1451 // Expect the correct number of fields. 1452 ASSERT_EQ(5U, form_structure->field_count()); 1453 EXPECT_EQ(5U, form_structure->autofill_count()); 1454 1455 // Name on Card. 1456 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 1457 // Address. 1458 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(1)->heuristic_type()); 1459 // Card Number. 1460 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); 1461 // Expiration Date. 1462 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 1463 // Expiration Year. 1464 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 1465 form_structure->field(4)->heuristic_type()); 1466} 1467 1468TEST(FormStructureTest, CVCCodeClash) { 1469 scoped_ptr<FormStructure> form_structure; 1470 FormData form; 1471 form.method = ASCIIToUTF16("post"); 1472 1473 FormFieldData field; 1474 field.form_control_type = "text"; 1475 1476 field.label = ASCIIToUTF16("Card number"); 1477 field.name = ASCIIToUTF16("ccnumber"); 1478 form.fields.push_back(field); 1479 1480 field.label = ASCIIToUTF16("First name"); 1481 field.name = ASCIIToUTF16("first_name"); 1482 form.fields.push_back(field); 1483 1484 field.label = ASCIIToUTF16("Last name"); 1485 field.name = ASCIIToUTF16("last_name"); 1486 form.fields.push_back(field); 1487 1488 field.label = ASCIIToUTF16("Expiration date"); 1489 field.name = ASCIIToUTF16("ccexpiresmonth"); 1490 form.fields.push_back(field); 1491 1492 field.label = base::string16(); 1493 field.name = ASCIIToUTF16("ccexpiresyear"); 1494 form.fields.push_back(field); 1495 1496 field.label = ASCIIToUTF16("cvc number"); 1497 field.name = ASCIIToUTF16("csc"); 1498 form.fields.push_back(field); 1499 1500 form_structure.reset(new FormStructure(form, std::string())); 1501 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1502 EXPECT_TRUE(form_structure->IsAutofillable(true)); 1503 1504 // Expect the correct number of fields. 1505 ASSERT_EQ(6U, form_structure->field_count()); 1506 ASSERT_EQ(5U, form_structure->autofill_count()); 1507 1508 // Card Number. 1509 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type()); 1510 // First name, taken as name on card. 1511 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type()); 1512 // Last name is not merged. 1513 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1514 // Expiration Date. 1515 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 1516 // Expiration Year. 1517 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 1518 form_structure->field(4)->heuristic_type()); 1519 // CVC code. 1520 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 1521 form_structure->field(5)->heuristic_type()); 1522} 1523 1524TEST(FormStructureTest, EncodeQueryRequest) { 1525 FormData form; 1526 form.method = ASCIIToUTF16("post"); 1527 1528 FormFieldData field; 1529 field.form_control_type = "text"; 1530 1531 field.label = ASCIIToUTF16("Name on Card"); 1532 field.name = ASCIIToUTF16("name_on_card"); 1533 form.fields.push_back(field); 1534 1535 field.label = ASCIIToUTF16("Address"); 1536 field.name = ASCIIToUTF16("billing_address"); 1537 form.fields.push_back(field); 1538 1539 field.label = ASCIIToUTF16("Card Number"); 1540 field.name = ASCIIToUTF16("card_number"); 1541 form.fields.push_back(field); 1542 1543 field.label = ASCIIToUTF16("Expiration Date"); 1544 field.name = ASCIIToUTF16("expiration_month"); 1545 form.fields.push_back(field); 1546 1547 field.label = ASCIIToUTF16("Expiration Year"); 1548 field.name = ASCIIToUTF16("expiration_year"); 1549 form.fields.push_back(field); 1550 1551 // Add checkable field. 1552 FormFieldData checkable_field; 1553 checkable_field.is_checkable = true; 1554 checkable_field.label = ASCIIToUTF16("Checkable1"); 1555 checkable_field.name = ASCIIToUTF16("Checkable1"); 1556 form.fields.push_back(checkable_field); 1557 1558 ScopedVector<FormStructure> forms; 1559 forms.push_back(new FormStructure(form, std::string())); 1560 std::vector<std::string> encoded_signatures; 1561 std::string encoded_xml; 1562 const char * const kSignature1 = "11337937696949187602"; 1563 const char * const kResponse1 = 1564 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " 1565 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " 1566 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" 1567 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1568 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" 1569 "</autofillquery>"; 1570 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1571 &encoded_signatures, 1572 &encoded_xml)); 1573 ASSERT_EQ(1U, encoded_signatures.size()); 1574 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1575 EXPECT_EQ(kResponse1, encoded_xml); 1576 1577 // Add the same form, only one will be encoded, so EncodeQueryRequest() should 1578 // return the same data. 1579 forms.push_back(new FormStructure(form, std::string())); 1580 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1581 &encoded_signatures, 1582 &encoded_xml)); 1583 ASSERT_EQ(1U, encoded_signatures.size()); 1584 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1585 EXPECT_EQ(kResponse1, encoded_xml); 1586 // Add 5 address fields - this should be still a valid form. 1587 for (size_t i = 0; i < 5; ++i) { 1588 field.label = ASCIIToUTF16("Address"); 1589 field.name = ASCIIToUTF16("address"); 1590 form.fields.push_back(field); 1591 } 1592 1593 forms.push_back(new FormStructure(form, std::string())); 1594 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1595 &encoded_signatures, 1596 &encoded_xml)); 1597 ASSERT_EQ(2U, encoded_signatures.size()); 1598 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1599 const char * const kSignature2 = "8308881815906226214"; 1600 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1601 const char * const kResponse2 = 1602 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " 1603 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " 1604 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" 1605 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1606 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" 1607 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>" 1608 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1609 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field " 1610 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1611 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1612 "signature=\"509334676\"/></form></autofillquery>"; 1613 EXPECT_EQ(kResponse2, encoded_xml); 1614 1615 FormData malformed_form(form); 1616 // Add 50 address fields - the form is not valid anymore, but previous ones 1617 // are. The result should be the same as in previous test. 1618 for (size_t i = 0; i < 50; ++i) { 1619 field.label = ASCIIToUTF16("Address"); 1620 field.name = ASCIIToUTF16("address"); 1621 malformed_form.fields.push_back(field); 1622 } 1623 1624 forms.push_back(new FormStructure(malformed_form, std::string())); 1625 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1626 &encoded_signatures, 1627 &encoded_xml)); 1628 ASSERT_EQ(2U, encoded_signatures.size()); 1629 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1630 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1631 EXPECT_EQ(kResponse2, encoded_xml); 1632 1633 // Check that we fail if there are only bad form(s). 1634 ScopedVector<FormStructure> bad_forms; 1635 bad_forms.push_back(new FormStructure(malformed_form, std::string())); 1636 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(), 1637 &encoded_signatures, 1638 &encoded_xml)); 1639 EXPECT_EQ(0U, encoded_signatures.size()); 1640 EXPECT_EQ("", encoded_xml); 1641 1642 // Check the behaviour with autocheckout enabled. 1643 ScopedVector<FormStructure> checkable_forms; 1644 checkable_forms.push_back( 1645 new FormStructure(form, "https://www.sample1.com/query/path")); 1646 1647 ASSERT_TRUE(FormStructure::EncodeQueryRequest(checkable_forms.get(), 1648 &encoded_signatures, 1649 &encoded_xml)); 1650 const char * const kSignature3 = "7747357776717901584"; 1651 const char * const kResponse3 = 1652 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery " 1653 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"a,e\" " 1654 "urlprefixsignature=\"7648393911063090788\">" 1655 "<form signature=\"7747357776717901584\">" 1656 "<field signature=\"412125936\"/>" 1657 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/><field" 1658 " signature=\"747221617\"/><field signature=\"4108155786\"/><field " 1659 "signature=\"3410250678\"/><field signature=\"509334676\"/><field " 1660 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1661 "signature=\"509334676\"/><field signature=\"509334676\"/></form>" 1662 "</autofillquery>"; 1663 ASSERT_EQ(1U, encoded_signatures.size()); 1664 EXPECT_EQ(kSignature3, encoded_signatures[0]); 1665 EXPECT_EQ(kResponse3, encoded_xml); 1666} 1667 1668TEST(FormStructureTest, EncodeUploadRequest) { 1669 scoped_ptr<FormStructure> form_structure; 1670 std::vector<FieldTypeSet> possible_field_types; 1671 FormData form; 1672 form.method = ASCIIToUTF16("post"); 1673 form_structure.reset(new FormStructure(form, std::string())); 1674 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1675 1676 FormFieldData field; 1677 field.form_control_type = "text"; 1678 1679 field.label = ASCIIToUTF16("First Name"); 1680 field.name = ASCIIToUTF16("firstname"); 1681 form.fields.push_back(field); 1682 possible_field_types.push_back(FieldTypeSet()); 1683 possible_field_types.back().insert(NAME_FIRST); 1684 1685 field.label = ASCIIToUTF16("Last Name"); 1686 field.name = ASCIIToUTF16("lastname"); 1687 form.fields.push_back(field); 1688 possible_field_types.push_back(FieldTypeSet()); 1689 possible_field_types.back().insert(NAME_LAST); 1690 1691 field.label = ASCIIToUTF16("Email"); 1692 field.name = ASCIIToUTF16("email"); 1693 field.form_control_type = "email"; 1694 form.fields.push_back(field); 1695 possible_field_types.push_back(FieldTypeSet()); 1696 possible_field_types.back().insert(EMAIL_ADDRESS); 1697 1698 field.label = ASCIIToUTF16("Phone"); 1699 field.name = ASCIIToUTF16("phone"); 1700 field.form_control_type = "number"; 1701 form.fields.push_back(field); 1702 possible_field_types.push_back(FieldTypeSet()); 1703 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1704 1705 field.label = ASCIIToUTF16("Country"); 1706 field.name = ASCIIToUTF16("country"); 1707 field.form_control_type = "select-one"; 1708 form.fields.push_back(field); 1709 possible_field_types.push_back(FieldTypeSet()); 1710 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1711 1712 // Add checkable field. 1713 FormFieldData checkable_field; 1714 checkable_field.is_checkable = true; 1715 checkable_field.label = ASCIIToUTF16("Checkable1"); 1716 checkable_field.name = ASCIIToUTF16("Checkable1"); 1717 form.fields.push_back(checkable_field); 1718 possible_field_types.push_back(FieldTypeSet()); 1719 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1720 1721 form_structure.reset(new FormStructure(form, std::string())); 1722 1723 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1724 for (size_t i = 0; i < form_structure->field_count(); ++i) 1725 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1726 1727 FieldTypeSet available_field_types; 1728 available_field_types.insert(NAME_FIRST); 1729 available_field_types.insert(NAME_LAST); 1730 available_field_types.insert(ADDRESS_HOME_LINE1); 1731 available_field_types.insert(ADDRESS_HOME_LINE2); 1732 available_field_types.insert(ADDRESS_HOME_COUNTRY); 1733 available_field_types.insert(ADDRESS_BILLING_LINE1); 1734 available_field_types.insert(ADDRESS_BILLING_LINE2); 1735 available_field_types.insert(EMAIL_ADDRESS); 1736 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 1737 1738 std::string encoded_xml; 1739 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 1740 &encoded_xml)); 1741 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1742 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1743 "formsignature=\"8736493185895608956\" autofillused=\"false\" " 1744 "datapresent=\"144200030e\">" 1745 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1746 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1747 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1748 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1749 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1750 "</autofillupload>", 1751 encoded_xml); 1752 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true, 1753 &encoded_xml)); 1754 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1755 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1756 "formsignature=\"8736493185895608956\" autofillused=\"true\" " 1757 "datapresent=\"144200030e\">" 1758 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1759 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1760 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1761 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1762 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1763 "</autofillupload>", 1764 encoded_xml); 1765 1766 // Add 2 address fields - this should be still a valid form. 1767 for (size_t i = 0; i < 2; ++i) { 1768 field.label = ASCIIToUTF16("Address"); 1769 field.name = ASCIIToUTF16("address"); 1770 field.form_control_type = "text"; 1771 form.fields.push_back(field); 1772 possible_field_types.push_back(FieldTypeSet()); 1773 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1774 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1775 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1776 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1777 } 1778 1779 form_structure.reset(new FormStructure(form, std::string())); 1780 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1781 for (size_t i = 0; i < form_structure->field_count(); ++i) 1782 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1783 1784 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 1785 &encoded_xml)); 1786 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1787 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1788 "formsignature=\"7816485729218079147\" autofillused=\"false\" " 1789 "datapresent=\"144200030e\">" 1790 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1791 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1792 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1793 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1794 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1795 "<field signature=\"509334676\" autofilltype=\"30\"/>" 1796 "<field signature=\"509334676\" autofilltype=\"31\"/>" 1797 "<field signature=\"509334676\" autofilltype=\"37\"/>" 1798 "<field signature=\"509334676\" autofilltype=\"38\"/>" 1799 "<field signature=\"509334676\" autofilltype=\"30\"/>" 1800 "<field signature=\"509334676\" autofilltype=\"31\"/>" 1801 "<field signature=\"509334676\" autofilltype=\"37\"/>" 1802 "<field signature=\"509334676\" autofilltype=\"38\"/>" 1803 "</autofillupload>", 1804 encoded_xml); 1805 1806 // Add 50 address fields - now the form is invalid, as it has too many fields. 1807 for (size_t i = 0; i < 50; ++i) { 1808 field.label = ASCIIToUTF16("Address"); 1809 field.name = ASCIIToUTF16("address"); 1810 field.form_control_type = "text"; 1811 form.fields.push_back(field); 1812 possible_field_types.push_back(FieldTypeSet()); 1813 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1814 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1815 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1816 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1817 } 1818 form_structure.reset(new FormStructure(form, std::string())); 1819 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1820 for (size_t i = 0; i < form_structure->field_count(); ++i) 1821 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1822 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false, 1823 &encoded_xml)); 1824} 1825 1826TEST(FormStructureTest, EncodeFieldAssignments) { 1827 scoped_ptr<FormStructure> form_structure; 1828 std::vector<FieldTypeSet> possible_field_types; 1829 FormData form; 1830 form.method = ASCIIToUTF16("post"); 1831 form_structure.reset(new FormStructure(form, std::string())); 1832 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1833 1834 FormFieldData field; 1835 field.form_control_type = "text"; 1836 1837 field.label = ASCIIToUTF16("First Name"); 1838 field.name = ASCIIToUTF16("firstname"); 1839 form.fields.push_back(field); 1840 possible_field_types.push_back(FieldTypeSet()); 1841 possible_field_types.back().insert(NAME_FIRST); 1842 1843 field.label = ASCIIToUTF16("Last Name"); 1844 field.name = ASCIIToUTF16("lastname"); 1845 form.fields.push_back(field); 1846 possible_field_types.push_back(FieldTypeSet()); 1847 possible_field_types.back().insert(NAME_LAST); 1848 1849 field.label = ASCIIToUTF16("Email"); 1850 field.name = ASCIIToUTF16("email"); 1851 field.form_control_type = "email"; 1852 form.fields.push_back(field); 1853 possible_field_types.push_back(FieldTypeSet()); 1854 possible_field_types.back().insert(EMAIL_ADDRESS); 1855 1856 field.label = ASCIIToUTF16("Phone"); 1857 field.name = ASCIIToUTF16("phone"); 1858 field.form_control_type = "number"; 1859 form.fields.push_back(field); 1860 possible_field_types.push_back(FieldTypeSet()); 1861 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1862 1863 field.label = ASCIIToUTF16("Country"); 1864 field.name = ASCIIToUTF16("country"); 1865 field.form_control_type = "select-one"; 1866 form.fields.push_back(field); 1867 possible_field_types.push_back(FieldTypeSet()); 1868 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1869 1870 // Add checkable field. 1871 FormFieldData checkable_field; 1872 checkable_field.is_checkable = true; 1873 checkable_field.label = ASCIIToUTF16("Checkable1"); 1874 checkable_field.name = ASCIIToUTF16("Checkable1"); 1875 form.fields.push_back(checkable_field); 1876 possible_field_types.push_back(FieldTypeSet()); 1877 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1878 1879 form_structure.reset(new FormStructure(form, std::string())); 1880 1881 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1882 for (size_t i = 0; i < form_structure->field_count(); ++i) 1883 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1884 1885 FieldTypeSet available_field_types; 1886 available_field_types.insert(NAME_FIRST); 1887 available_field_types.insert(NAME_LAST); 1888 available_field_types.insert(ADDRESS_HOME_LINE1); 1889 available_field_types.insert(ADDRESS_HOME_LINE2); 1890 available_field_types.insert(ADDRESS_HOME_COUNTRY); 1891 available_field_types.insert(ADDRESS_BILLING_LINE1); 1892 available_field_types.insert(ADDRESS_BILLING_LINE2); 1893 available_field_types.insert(EMAIL_ADDRESS); 1894 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 1895 1896 std::string encoded_xml; 1897 EXPECT_TRUE(form_structure->EncodeFieldAssignments( 1898 available_field_types, &encoded_xml)); 1899 EXPECT_EQ( 1900 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 1901 "<fieldassignments formsignature=\"8736493185895608956\">" 1902 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>" 1903 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>" 1904 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>" 1905 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>" 1906 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>" 1907 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>" 1908 "</fieldassignments>", 1909 encoded_xml); 1910 1911 // Add 2 address fields - this should be still a valid form. 1912 for (size_t i = 0; i < 2; ++i) { 1913 field.label = ASCIIToUTF16("Address"); 1914 field.name = ASCIIToUTF16("address"); 1915 field.form_control_type = "text"; 1916 form.fields.push_back(field); 1917 possible_field_types.push_back(FieldTypeSet()); 1918 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1919 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1920 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1921 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1922 } 1923 1924 form_structure.reset(new FormStructure(form, std::string())); 1925 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1926 for (size_t i = 0; i < form_structure->field_count(); ++i) 1927 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1928 1929 EXPECT_TRUE(form_structure->EncodeFieldAssignments( 1930 available_field_types, &encoded_xml)); 1931 EXPECT_EQ( 1932 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 1933 "<fieldassignments formsignature=\"7816485729218079147\">" 1934 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>" 1935 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>" 1936 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>" 1937 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>" 1938 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>" 1939 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>" 1940 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>" 1941 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>" 1942 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>" 1943 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>" 1944 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>" 1945 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>" 1946 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>" 1947 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>" 1948 "</fieldassignments>", 1949 encoded_xml); 1950} 1951 1952// Check that we compute the "datapresent" string correctly for the given 1953// |available_types|. 1954TEST(FormStructureTest, CheckDataPresence) { 1955 FormData form; 1956 form.method = ASCIIToUTF16("post"); 1957 1958 FormFieldData field; 1959 field.form_control_type = "text"; 1960 1961 field.label = ASCIIToUTF16("First Name"); 1962 field.name = ASCIIToUTF16("first"); 1963 form.fields.push_back(field); 1964 1965 field.label = ASCIIToUTF16("Last Name"); 1966 field.name = ASCIIToUTF16("last"); 1967 form.fields.push_back(field); 1968 1969 field.label = ASCIIToUTF16("Email"); 1970 field.name = ASCIIToUTF16("email"); 1971 form.fields.push_back(field); 1972 1973 FormStructure form_structure(form, std::string()); 1974 1975 FieldTypeSet unknown_type; 1976 unknown_type.insert(UNKNOWN_TYPE); 1977 for (size_t i = 0; i < form_structure.field_count(); ++i) 1978 form_structure.field(i)->set_possible_types(unknown_type); 1979 1980 // No available types. 1981 // datapresent should be "" == trimmmed(0x0000000000000000) == 1982 // 0b0000000000000000000000000000000000000000000000000000000000000000 1983 FieldTypeSet available_field_types; 1984 1985 std::string encoded_xml; 1986 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 1987 &encoded_xml)); 1988 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1989 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 1990 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 1991 " datapresent=\"\">" 1992 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 1993 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 1994 "<field signature=\"420638584\" autofilltype=\"1\"/>" 1995 "</autofillupload>", 1996 encoded_xml); 1997 1998 // Only a few types available. 1999 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) == 2000 // 0b0001010101000000000000000000001001000000000000000000000000000000 2001 // The set bits are: 2002 // 3 == NAME_FIRST 2003 // 5 == NAME_LAST 2004 // 7 == NAME_FULL 2005 // 9 == EMAIL_ADDRESS 2006 // 30 == ADDRESS_HOME_LINE1 2007 // 33 == ADDRESS_HOME_CITY 2008 available_field_types.clear(); 2009 available_field_types.insert(NAME_FIRST); 2010 available_field_types.insert(NAME_LAST); 2011 available_field_types.insert(NAME_FULL); 2012 available_field_types.insert(EMAIL_ADDRESS); 2013 available_field_types.insert(ADDRESS_HOME_LINE1); 2014 available_field_types.insert(ADDRESS_HOME_CITY); 2015 2016 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 2017 &encoded_xml)); 2018 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2019 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2020 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 2021 " datapresent=\"1540000240\">" 2022 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 2023 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 2024 "<field signature=\"420638584\" autofilltype=\"1\"/>" 2025 "</autofillupload>", 2026 encoded_xml); 2027 2028 // All supported non-credit card types available. 2029 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) == 2030 // 0b0001111101111110000000000000001101111000000000000000000000001000 2031 // The set bits are: 2032 // 3 == NAME_FIRST 2033 // 4 == NAME_MIDDLE 2034 // 5 == NAME_LAST 2035 // 6 == NAME_MIDDLE_INITIAL 2036 // 7 == NAME_FULL 2037 // 9 == EMAIL_ADDRESS 2038 // 10 == PHONE_HOME_NUMBER, 2039 // 11 == PHONE_HOME_CITY_CODE, 2040 // 12 == PHONE_HOME_COUNTRY_CODE, 2041 // 13 == PHONE_HOME_CITY_AND_NUMBER, 2042 // 14 == PHONE_HOME_WHOLE_NUMBER, 2043 // 30 == ADDRESS_HOME_LINE1 2044 // 31 == ADDRESS_HOME_LINE2 2045 // 33 == ADDRESS_HOME_CITY 2046 // 34 == ADDRESS_HOME_STATE 2047 // 35 == ADDRESS_HOME_ZIP 2048 // 36 == ADDRESS_HOME_COUNTRY 2049 // 60 == COMPANY_NAME 2050 available_field_types.clear(); 2051 available_field_types.insert(NAME_FIRST); 2052 available_field_types.insert(NAME_MIDDLE); 2053 available_field_types.insert(NAME_LAST); 2054 available_field_types.insert(NAME_MIDDLE_INITIAL); 2055 available_field_types.insert(NAME_FULL); 2056 available_field_types.insert(EMAIL_ADDRESS); 2057 available_field_types.insert(PHONE_HOME_NUMBER); 2058 available_field_types.insert(PHONE_HOME_CITY_CODE); 2059 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); 2060 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); 2061 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 2062 available_field_types.insert(ADDRESS_HOME_LINE1); 2063 available_field_types.insert(ADDRESS_HOME_LINE2); 2064 available_field_types.insert(ADDRESS_HOME_CITY); 2065 available_field_types.insert(ADDRESS_HOME_STATE); 2066 available_field_types.insert(ADDRESS_HOME_ZIP); 2067 available_field_types.insert(ADDRESS_HOME_COUNTRY); 2068 available_field_types.insert(COMPANY_NAME); 2069 2070 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 2071 &encoded_xml)); 2072 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2073 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2074 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 2075 " datapresent=\"1f7e000378000008\">" 2076 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 2077 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 2078 "<field signature=\"420638584\" autofilltype=\"1\"/>" 2079 "</autofillupload>", 2080 encoded_xml); 2081 2082 // All supported credit card types available. 2083 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) == 2084 // 0b0000000000000000000000000000000000000000000000000001111111000000 2085 // The set bits are: 2086 // 51 == CREDIT_CARD_NAME 2087 // 52 == CREDIT_CARD_NUMBER 2088 // 53 == CREDIT_CARD_EXP_MONTH 2089 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR 2090 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR 2091 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR 2092 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR 2093 available_field_types.clear(); 2094 available_field_types.insert(CREDIT_CARD_NAME); 2095 available_field_types.insert(CREDIT_CARD_NUMBER); 2096 available_field_types.insert(CREDIT_CARD_EXP_MONTH); 2097 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 2098 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 2099 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); 2100 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 2101 2102 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 2103 &encoded_xml)); 2104 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2105 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2106 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 2107 " datapresent=\"0000000000001fc0\">" 2108 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 2109 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 2110 "<field signature=\"420638584\" autofilltype=\"1\"/>" 2111 "</autofillupload>", 2112 encoded_xml); 2113 2114 // All supported types available. 2115 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) == 2116 // 0b0001111101111110000000000000001101111000000000000001111111001000 2117 // The set bits are: 2118 // 3 == NAME_FIRST 2119 // 4 == NAME_MIDDLE 2120 // 5 == NAME_LAST 2121 // 6 == NAME_MIDDLE_INITIAL 2122 // 7 == NAME_FULL 2123 // 9 == EMAIL_ADDRESS 2124 // 10 == PHONE_HOME_NUMBER, 2125 // 11 == PHONE_HOME_CITY_CODE, 2126 // 12 == PHONE_HOME_COUNTRY_CODE, 2127 // 13 == PHONE_HOME_CITY_AND_NUMBER, 2128 // 14 == PHONE_HOME_WHOLE_NUMBER, 2129 // 30 == ADDRESS_HOME_LINE1 2130 // 31 == ADDRESS_HOME_LINE2 2131 // 33 == ADDRESS_HOME_CITY 2132 // 34 == ADDRESS_HOME_STATE 2133 // 35 == ADDRESS_HOME_ZIP 2134 // 36 == ADDRESS_HOME_COUNTRY 2135 // 51 == CREDIT_CARD_NAME 2136 // 52 == CREDIT_CARD_NUMBER 2137 // 53 == CREDIT_CARD_EXP_MONTH 2138 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR 2139 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR 2140 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR 2141 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR 2142 // 60 == COMPANY_NAME 2143 available_field_types.clear(); 2144 available_field_types.insert(NAME_FIRST); 2145 available_field_types.insert(NAME_MIDDLE); 2146 available_field_types.insert(NAME_LAST); 2147 available_field_types.insert(NAME_MIDDLE_INITIAL); 2148 available_field_types.insert(NAME_FULL); 2149 available_field_types.insert(EMAIL_ADDRESS); 2150 available_field_types.insert(PHONE_HOME_NUMBER); 2151 available_field_types.insert(PHONE_HOME_CITY_CODE); 2152 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); 2153 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); 2154 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 2155 available_field_types.insert(ADDRESS_HOME_LINE1); 2156 available_field_types.insert(ADDRESS_HOME_LINE2); 2157 available_field_types.insert(ADDRESS_HOME_CITY); 2158 available_field_types.insert(ADDRESS_HOME_STATE); 2159 available_field_types.insert(ADDRESS_HOME_ZIP); 2160 available_field_types.insert(ADDRESS_HOME_COUNTRY); 2161 available_field_types.insert(CREDIT_CARD_NAME); 2162 available_field_types.insert(CREDIT_CARD_NUMBER); 2163 available_field_types.insert(CREDIT_CARD_EXP_MONTH); 2164 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 2165 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 2166 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); 2167 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 2168 available_field_types.insert(COMPANY_NAME); 2169 2170 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 2171 &encoded_xml)); 2172 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2173 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2174 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 2175 " datapresent=\"1f7e000378001fc8\">" 2176 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 2177 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 2178 "<field signature=\"420638584\" autofilltype=\"1\"/>" 2179 "</autofillupload>", 2180 encoded_xml); 2181} 2182 2183TEST(FormStructureTest, CheckMultipleTypes) { 2184 // Throughout this test, datapresent should be 2185 // 0x1440000360000008 == 2186 // 0b0001010001000000000000000000001101100000000000000000000000001000 2187 // The set bits are: 2188 // 3 == NAME_FIRST 2189 // 5 == NAME_LAST 2190 // 9 == EMAIL_ADDRESS 2191 // 30 == ADDRESS_HOME_LINE1 2192 // 31 == ADDRESS_HOME_LINE2 2193 // 33 == ADDRESS_HOME_CITY 2194 // 34 == ADDRESS_HOME_STATE 2195 // 60 == COMPANY_NAME 2196 FieldTypeSet available_field_types; 2197 available_field_types.insert(NAME_FIRST); 2198 available_field_types.insert(NAME_LAST); 2199 available_field_types.insert(EMAIL_ADDRESS); 2200 available_field_types.insert(ADDRESS_HOME_LINE1); 2201 available_field_types.insert(ADDRESS_HOME_LINE2); 2202 available_field_types.insert(ADDRESS_HOME_CITY); 2203 available_field_types.insert(ADDRESS_HOME_STATE); 2204 available_field_types.insert(COMPANY_NAME); 2205 2206 // Check that multiple types for the field are processed correctly. 2207 scoped_ptr<FormStructure> form_structure; 2208 std::vector<FieldTypeSet> possible_field_types; 2209 FormData form; 2210 form.method = ASCIIToUTF16("post"); 2211 2212 FormFieldData field; 2213 field.form_control_type = "text"; 2214 2215 field.label = ASCIIToUTF16("email"); 2216 field.name = ASCIIToUTF16("email"); 2217 form.fields.push_back(field); 2218 possible_field_types.push_back(FieldTypeSet()); 2219 possible_field_types.back().insert(EMAIL_ADDRESS); 2220 2221 field.label = ASCIIToUTF16("First Name"); 2222 field.name = ASCIIToUTF16("first"); 2223 form.fields.push_back(field); 2224 possible_field_types.push_back(FieldTypeSet()); 2225 possible_field_types.back().insert(NAME_FIRST); 2226 2227 field.label = ASCIIToUTF16("Last Name"); 2228 field.name = ASCIIToUTF16("last"); 2229 form.fields.push_back(field); 2230 possible_field_types.push_back(FieldTypeSet()); 2231 possible_field_types.back().insert(NAME_LAST); 2232 2233 field.label = ASCIIToUTF16("Address"); 2234 field.name = ASCIIToUTF16("address"); 2235 form.fields.push_back(field); 2236 possible_field_types.push_back(FieldTypeSet()); 2237 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 2238 2239 form_structure.reset(new FormStructure(form, std::string())); 2240 2241 for (size_t i = 0; i < form_structure->field_count(); ++i) 2242 form_structure->field(i)->set_possible_types(possible_field_types[i]); 2243 std::string encoded_xml; 2244 2245 // Now we matched both fields singularly. 2246 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2247 &encoded_xml)); 2248 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2249 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2250 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2251 " datapresent=\"1440000360000008\">" 2252 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2253 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2254 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2255 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2256 "</autofillupload>", 2257 encoded_xml); 2258 // Match third field as both first and last. 2259 possible_field_types[2].insert(NAME_FIRST); 2260 form_structure->field(2)->set_possible_types(possible_field_types[2]); 2261 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2262 &encoded_xml)); 2263 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2264 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2265 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2266 " datapresent=\"1440000360000008\">" 2267 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2268 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2269 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2270 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2271 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2272 "</autofillupload>", 2273 encoded_xml); 2274 possible_field_types[3].insert(ADDRESS_HOME_LINE2); 2275 form_structure->field(form_structure->field_count() - 1)->set_possible_types( 2276 possible_field_types[form_structure->field_count() - 1]); 2277 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2278 &encoded_xml)); 2279 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2280 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2281 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2282 " datapresent=\"1440000360000008\">" 2283 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2284 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2285 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2286 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2287 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2288 "<field signature=\"509334676\" autofilltype=\"31\"/>" 2289 "</autofillupload>", 2290 encoded_xml); 2291 possible_field_types[3].clear(); 2292 possible_field_types[3].insert(ADDRESS_HOME_LINE1); 2293 possible_field_types[3].insert(COMPANY_NAME); 2294 form_structure->field(form_structure->field_count() - 1)->set_possible_types( 2295 possible_field_types[form_structure->field_count() - 1]); 2296 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2297 &encoded_xml)); 2298 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2299 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2300 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2301 " datapresent=\"1440000360000008\">" 2302 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2303 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2304 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2305 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2306 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2307 "<field signature=\"509334676\" autofilltype=\"60\"/>" 2308 "</autofillupload>", 2309 encoded_xml); 2310} 2311 2312TEST(FormStructureTest, CheckFormSignature) { 2313 // Check that form signature is created correctly. 2314 scoped_ptr<FormStructure> form_structure; 2315 FormData form; 2316 form.method = ASCIIToUTF16("post"); 2317 2318 FormFieldData field; 2319 field.form_control_type = "text"; 2320 2321 field.label = ASCIIToUTF16("email"); 2322 field.name = ASCIIToUTF16("email"); 2323 form.fields.push_back(field); 2324 2325 field.label = ASCIIToUTF16("First Name"); 2326 field.name = ASCIIToUTF16("first"); 2327 form.fields.push_back(field); 2328 2329 // Password fields shouldn't affect the signature. 2330 field.label = ASCIIToUTF16("Password"); 2331 field.name = ASCIIToUTF16("password"); 2332 field.form_control_type = "password"; 2333 form.fields.push_back(field); 2334 2335 form_structure.reset(new FormStructure(form, std::string())); 2336 2337 EXPECT_EQ(FormStructureTest::Hash64Bit( 2338 std::string("://&&email&first")), 2339 form_structure->FormSignature()); 2340 2341 form.origin = GURL(std::string("http://www.facebook.com")); 2342 form_structure.reset(new FormStructure(form, std::string())); 2343 EXPECT_EQ(FormStructureTest::Hash64Bit( 2344 std::string("http://www.facebook.com&&email&first")), 2345 form_structure->FormSignature()); 2346 2347 form.action = GURL(std::string("https://login.facebook.com/path")); 2348 form_structure.reset(new FormStructure(form, std::string())); 2349 EXPECT_EQ(FormStructureTest::Hash64Bit( 2350 std::string("https://login.facebook.com&&email&first")), 2351 form_structure->FormSignature()); 2352 2353 form.name = ASCIIToUTF16("login_form"); 2354 form_structure.reset(new FormStructure(form, std::string())); 2355 EXPECT_EQ(FormStructureTest::Hash64Bit( 2356 std::string("https://login.facebook.com&login_form&email&first")), 2357 form_structure->FormSignature()); 2358} 2359 2360TEST(FormStructureTest, ToFormData) { 2361 FormData form; 2362 form.name = ASCIIToUTF16("the-name"); 2363 form.method = ASCIIToUTF16("POST"); 2364 form.origin = GURL("http://cool.com"); 2365 form.action = form.origin.Resolve("/login"); 2366 2367 FormFieldData field; 2368 field.label = ASCIIToUTF16("username"); 2369 field.name = ASCIIToUTF16("username"); 2370 field.form_control_type = "text"; 2371 form.fields.push_back(field); 2372 2373 field.label = ASCIIToUTF16("password"); 2374 field.name = ASCIIToUTF16("password"); 2375 field.form_control_type = "password"; 2376 form.fields.push_back(field); 2377 2378 field.label = base::string16(); 2379 field.name = ASCIIToUTF16("Submit"); 2380 field.form_control_type = "submit"; 2381 form.fields.push_back(field); 2382 2383 EXPECT_EQ(form, FormStructure(form, std::string()).ToFormData()); 2384 2385 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always 2386 // false. This forces a future author that changes this to update this test. 2387 form.user_submitted = true; 2388 EXPECT_NE(form, FormStructure(form, std::string()).ToFormData()); 2389} 2390 2391TEST(FormStructureTest, SkipFieldTest) { 2392 FormData form; 2393 form.name = ASCIIToUTF16("the-name"); 2394 form.method = ASCIIToUTF16("POST"); 2395 form.origin = GURL("http://cool.com"); 2396 form.action = form.origin.Resolve("/login"); 2397 2398 FormFieldData field; 2399 field.label = ASCIIToUTF16("username"); 2400 field.name = ASCIIToUTF16("username"); 2401 field.form_control_type = "text"; 2402 form.fields.push_back(field); 2403 2404 field.label = ASCIIToUTF16("password"); 2405 field.name = ASCIIToUTF16("password"); 2406 field.form_control_type = "password"; 2407 form.fields.push_back(field); 2408 2409 field.label = base::string16(); 2410 field.name = ASCIIToUTF16("email"); 2411 field.form_control_type = "text"; 2412 form.fields.push_back(field); 2413 2414 ScopedVector<FormStructure> forms; 2415 forms.push_back(new FormStructure(form, std::string())); 2416 std::vector<std::string> encoded_signatures; 2417 std::string encoded_xml; 2418 2419 const char * const kSignature = "18006745212084723782"; 2420 const char * const kResponse = 2421 "<\?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery " 2422 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " 2423 "signature=\"18006745212084723782\"><field signature=\"239111655\"/>" 2424 "<field signature=\"420638584\"/></form></autofillquery>"; 2425 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 2426 &encoded_signatures, 2427 &encoded_xml)); 2428 ASSERT_EQ(1U, encoded_signatures.size()); 2429 EXPECT_EQ(kSignature, encoded_signatures[0]); 2430 EXPECT_EQ(kResponse, encoded_xml); 2431 2432 AutocheckoutPageMetaData page_meta_data; 2433 const char * const kServerResponse = 2434 "<autofillqueryresponse><field autofilltype=\"3\" />" 2435 "<field autofilltype=\"9\" /></autofillqueryresponse>"; 2436 FormStructure::ParseQueryResponse(kServerResponse, forms.get(), 2437 &page_meta_data, TestAutofillMetrics()); 2438 ASSERT_EQ(NAME_FIRST, forms[0]->field(0)->server_type()); 2439 ASSERT_EQ(NO_SERVER_DATA, forms[0]->field(1)->server_type()); 2440 ASSERT_EQ(EMAIL_ADDRESS, forms[0]->field(2)->server_type()); 2441} 2442 2443} // namespace autofill 2444