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

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

Issue 11415221: Add support for autofilling radio buttons and checkboxes. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Remove extra line change in autofill_scanner.cc Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/autofill/form_structure.h" 5 #include "chrome/browser/autofill/form_structure.h"
6 6
7 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
8 #include "base/string_util.h" 9 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/autofill/autofill_metrics.h" 11 #include "chrome/browser/autofill/autofill_metrics.h"
12 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/form_data.h" 13 #include "chrome/common/form_data.h"
12 #include "chrome/common/form_field_data.h" 14 #include "chrome/common/form_field_data.h"
13 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
16 18
17 using WebKit::WebInputElement; 19 using WebKit::WebInputElement;
18 20
19 namespace { 21 namespace {
20 22
(...skipping 1402 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 form.fields.push_back(field); 1425 form.fields.push_back(field);
1424 1426
1425 field.label = ASCIIToUTF16("Expiration Date"); 1427 field.label = ASCIIToUTF16("Expiration Date");
1426 field.name = ASCIIToUTF16("expiration_month"); 1428 field.name = ASCIIToUTF16("expiration_month");
1427 form.fields.push_back(field); 1429 form.fields.push_back(field);
1428 1430
1429 field.label = ASCIIToUTF16("Expiration Year"); 1431 field.label = ASCIIToUTF16("Expiration Year");
1430 field.name = ASCIIToUTF16("expiration_year"); 1432 field.name = ASCIIToUTF16("expiration_year");
1431 form.fields.push_back(field); 1433 form.fields.push_back(field);
1432 1434
1435 // Add checkable field.
1436 FormFieldData checkable_field;
1437 checkable_field.is_checkable = true;
1438 checkable_field.label = ASCIIToUTF16("Checkable1");
1439 checkable_field.name = ASCIIToUTF16("Checkable1");
1440 form.fields.push_back(checkable_field);
1441
1433 ScopedVector<FormStructure> forms; 1442 ScopedVector<FormStructure> forms;
1434 forms.push_back(new FormStructure(form)); 1443 forms.push_back(new FormStructure(form));
1435 std::vector<std::string> encoded_signatures; 1444 std::vector<std::string> encoded_signatures;
1436 std::string encoded_xml; 1445 std::string encoded_xml;
1437 const char * const kSignature1 = "11337937696949187602"; 1446 const char * const kSignature1 = "11337937696949187602";
1438 const char * const kResponse1 = 1447 const char * const kResponse1 =
1439 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " 1448 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery "
1440 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " 1449 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form "
1441 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" 1450 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>"
1442 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1451 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1480 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1489 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1481 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" 1490 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>"
1482 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>" 1491 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>"
1483 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1492 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1484 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field " 1493 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field "
1485 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1494 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1486 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1495 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1487 "signature=\"509334676\"/></form></autofillquery>"; 1496 "signature=\"509334676\"/></form></autofillquery>";
1488 EXPECT_EQ(kResponse2, encoded_xml); 1497 EXPECT_EQ(kResponse2, encoded_xml);
1489 1498
1499 FormData malformed_form(form);
1490 // Add 50 address fields - the form is not valid anymore, but previous ones 1500 // Add 50 address fields - the form is not valid anymore, but previous ones
1491 // are. The result should be the same as in previous test. 1501 // are. The result should be the same as in previous test.
1492 for (size_t i = 0; i < 50; ++i) { 1502 for (size_t i = 0; i < 50; ++i) {
1493 field.label = ASCIIToUTF16("Address"); 1503 field.label = ASCIIToUTF16("Address");
1494 field.name = ASCIIToUTF16("address"); 1504 field.name = ASCIIToUTF16("address");
1495 form.fields.push_back(field); 1505 malformed_form.fields.push_back(field);
1496 } 1506 }
1497 1507
1498 forms.push_back(new FormStructure(form)); 1508 forms.push_back(new FormStructure(malformed_form));
1499 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1509 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1500 &encoded_signatures, 1510 &encoded_signatures,
1501 &encoded_xml)); 1511 &encoded_xml));
1502 ASSERT_EQ(2U, encoded_signatures.size()); 1512 ASSERT_EQ(2U, encoded_signatures.size());
1503 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1513 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1504 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1514 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1505 EXPECT_EQ(kResponse2, encoded_xml); 1515 EXPECT_EQ(kResponse2, encoded_xml);
1506 1516
1507 // Check that we fail if there are only bad form(s). 1517 // Check that we fail if there are only bad form(s).
1508 ScopedVector<FormStructure> bad_forms; 1518 ScopedVector<FormStructure> bad_forms;
1509 bad_forms.push_back(new FormStructure(form)); 1519 bad_forms.push_back(new FormStructure(malformed_form));
1510 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(), 1520 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(),
1511 &encoded_signatures, 1521 &encoded_signatures,
1512 &encoded_xml)); 1522 &encoded_xml));
1513 EXPECT_EQ(0U, encoded_signatures.size()); 1523 EXPECT_EQ(0U, encoded_signatures.size());
1514 EXPECT_EQ("", encoded_xml); 1524 EXPECT_EQ("", encoded_xml);
1525
1526 // Check the behaviour with kEnableExperimentalFormFilling switch on.
1527 CommandLine::ForCurrentProcess()->AppendSwitch(
1528 switches::kEnableExperimentalFormFilling);
1529 // Add the previous form but with flag set.
1530 ScopedVector<FormStructure> checkable_forms;
1531 checkable_forms.push_back(new FormStructure(form));
1532
1533 ASSERT_TRUE(FormStructure::EncodeQueryRequest(checkable_forms.get(),
1534 &encoded_signatures,
1535 &encoded_xml));
1536 const char * const kSignature3 = "7747357776717901584";
1537 const char * const kResponse3 =
1538 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery "
1539 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\">"
1540 "<form signature=\"7747357776717901584\"><field signature=\"412125936\"/>"
1541 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/><field"
1542 " signature=\"747221617\"/><field signature=\"4108155786\"/><field "
1543 "signature=\"3410250678\"/><field signature=\"509334676\"/><field "
1544 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1545 "signature=\"509334676\"/><field signature=\"509334676\"/></form>"
1546 "</autofillquery>";
1547 ASSERT_EQ(1U, encoded_signatures.size());
1548 EXPECT_EQ(kSignature3, encoded_signatures[0]);
1549 EXPECT_EQ(kResponse3, encoded_xml);
1515 } 1550 }
1516 1551
1517 TEST(FormStructureTest, EncodeUploadRequest) { 1552 TEST(FormStructureTest, EncodeUploadRequest) {
1518 scoped_ptr<FormStructure> form_structure; 1553 scoped_ptr<FormStructure> form_structure;
1519 std::vector<FieldTypeSet> possible_field_types; 1554 std::vector<FieldTypeSet> possible_field_types;
1520 FormData form; 1555 FormData form;
1521 form.method = ASCIIToUTF16("post"); 1556 form.method = ASCIIToUTF16("post");
1522 form_structure.reset(new FormStructure(form)); 1557 form_structure.reset(new FormStructure(form));
1523 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1558 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1524 1559
(...skipping 25 matching lines...) Expand all
1550 form.fields.push_back(field); 1585 form.fields.push_back(field);
1551 possible_field_types.push_back(FieldTypeSet()); 1586 possible_field_types.push_back(FieldTypeSet());
1552 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1587 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1553 1588
1554 field.label = ASCIIToUTF16("Country"); 1589 field.label = ASCIIToUTF16("Country");
1555 field.name = ASCIIToUTF16("country"); 1590 field.name = ASCIIToUTF16("country");
1556 field.form_control_type = "select-one"; 1591 field.form_control_type = "select-one";
1557 form.fields.push_back(field); 1592 form.fields.push_back(field);
1558 possible_field_types.push_back(FieldTypeSet()); 1593 possible_field_types.push_back(FieldTypeSet());
1559 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1594 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1595
1596 // Add checkable field.
1597 FormFieldData checkable_field;
1598 checkable_field.is_checkable = true;
1599 checkable_field.label = ASCIIToUTF16("Checkable1");
1600 checkable_field.name = ASCIIToUTF16("Checkable1");
1601 form.fields.push_back(checkable_field);
1602 possible_field_types.push_back(FieldTypeSet());
1603 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1604
1560 form_structure.reset(new FormStructure(form)); 1605 form_structure.reset(new FormStructure(form));
1561 1606
1562 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1607 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1563 for (size_t i = 0; i < form_structure->field_count(); ++i) 1608 for (size_t i = 0; i < form_structure->field_count(); ++i)
1564 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1609 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1565 1610
1566 FieldTypeSet available_field_types; 1611 FieldTypeSet available_field_types;
1567 available_field_types.insert(NAME_FIRST); 1612 available_field_types.insert(NAME_FIRST);
1568 available_field_types.insert(NAME_LAST); 1613 available_field_types.insert(NAME_LAST);
1569 available_field_types.insert(ADDRESS_HOME_LINE1); 1614 available_field_types.insert(ADDRESS_HOME_LINE1);
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
2087 field.form_control_type = "submit"; 2132 field.form_control_type = "submit";
2088 form.fields.push_back(field); 2133 form.fields.push_back(field);
2089 2134
2090 EXPECT_EQ(form, FormStructure(form).ToFormData()); 2135 EXPECT_EQ(form, FormStructure(form).ToFormData());
2091 2136
2092 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always 2137 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always
2093 // false. This forces a future author that changes this to update this test. 2138 // false. This forces a future author that changes this to update this test.
2094 form.user_submitted = true; 2139 form.user_submitted = true;
2095 EXPECT_NE(form, FormStructure(form).ToFormData()); 2140 EXPECT_NE(form, FormStructure(form).ToFormData());
2096 } 2141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698