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

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: Fix for android CQ failure. 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 210
209 // We need at least three text fields to be parseable. 211 // We need at least three text fields to be parseable.
210 form.method = ASCIIToUTF16("post"); 212 form.method = ASCIIToUTF16("post");
211 213
212 FormFieldData field; 214 FormFieldData field;
213 field.label = ASCIIToUTF16("username"); 215 field.label = ASCIIToUTF16("username");
214 field.name = ASCIIToUTF16("username"); 216 field.name = ASCIIToUTF16("username");
215 field.form_control_type = "text"; 217 field.form_control_type = "text";
216 form.fields.push_back(field); 218 form.fields.push_back(field);
217 219
220 FormFieldData checkable_field;
221 checkable_field.is_checkable = true;
222 checkable_field.name = ASCIIToUTF16("radiobtn");
223 checkable_field.form_control_type = "radio";
224 form.fields.push_back(checkable_field);
225
226 checkable_field.name = ASCIIToUTF16("checkbox");
227 checkable_field.form_control_type = "checkbox";
228 form.fields.push_back(checkable_field);
229
218 form_structure.reset(new FormStructure(form)); 230 form_structure.reset(new FormStructure(form));
219 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); 231 EXPECT_FALSE(form_structure->ShouldBeParsed(true));
220 232
221 // We now have three text fields, though only two are auto-fillable. 233 // We now have three text fields, though only two are auto-fillable.
222 field.label = ASCIIToUTF16("First Name"); 234 field.label = ASCIIToUTF16("First Name");
223 field.name = ASCIIToUTF16("firstname"); 235 field.name = ASCIIToUTF16("firstname");
224 field.form_control_type = "text"; 236 field.form_control_type = "text";
225 form.fields.push_back(field); 237 form.fields.push_back(field);
226 238
227 field.label = ASCIIToUTF16("Last Name"); 239 field.label = ASCIIToUTF16("Last Name");
(...skipping 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 form.fields.push_back(field); 1435 form.fields.push_back(field);
1424 1436
1425 field.label = ASCIIToUTF16("Expiration Date"); 1437 field.label = ASCIIToUTF16("Expiration Date");
1426 field.name = ASCIIToUTF16("expiration_month"); 1438 field.name = ASCIIToUTF16("expiration_month");
1427 form.fields.push_back(field); 1439 form.fields.push_back(field);
1428 1440
1429 field.label = ASCIIToUTF16("Expiration Year"); 1441 field.label = ASCIIToUTF16("Expiration Year");
1430 field.name = ASCIIToUTF16("expiration_year"); 1442 field.name = ASCIIToUTF16("expiration_year");
1431 form.fields.push_back(field); 1443 form.fields.push_back(field);
1432 1444
1445 // Add checkable field.
1446 FormFieldData checkable_field;
1447 checkable_field.is_checkable = true;
1448 checkable_field.label = ASCIIToUTF16("Checkable1");
1449 checkable_field.name = ASCIIToUTF16("Checkable1");
1450 form.fields.push_back(checkable_field);
1451
1433 ScopedVector<FormStructure> forms; 1452 ScopedVector<FormStructure> forms;
1434 forms.push_back(new FormStructure(form)); 1453 forms.push_back(new FormStructure(form));
1435 std::vector<std::string> encoded_signatures; 1454 std::vector<std::string> encoded_signatures;
1436 std::string encoded_xml; 1455 std::string encoded_xml;
1437 const char * const kSignature1 = "11337937696949187602"; 1456 const char * const kSignature1 = "11337937696949187602";
1438 const char * const kResponse1 = 1457 const char * const kResponse1 =
1439 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " 1458 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery "
1440 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " 1459 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form "
1441 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" 1460 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>"
1442 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1461 "<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\"/>" 1499 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1481 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" 1500 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>"
1482 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>" 1501 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>"
1483 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" 1502 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1484 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field " 1503 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field "
1485 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1504 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1486 "signature=\"509334676\"/><field signature=\"509334676\"/><field " 1505 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1487 "signature=\"509334676\"/></form></autofillquery>"; 1506 "signature=\"509334676\"/></form></autofillquery>";
1488 EXPECT_EQ(kResponse2, encoded_xml); 1507 EXPECT_EQ(kResponse2, encoded_xml);
1489 1508
1509 FormData malformed_form(form);
1490 // Add 50 address fields - the form is not valid anymore, but previous ones 1510 // 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. 1511 // are. The result should be the same as in previous test.
1492 for (size_t i = 0; i < 50; ++i) { 1512 for (size_t i = 0; i < 50; ++i) {
1493 field.label = ASCIIToUTF16("Address"); 1513 field.label = ASCIIToUTF16("Address");
1494 field.name = ASCIIToUTF16("address"); 1514 field.name = ASCIIToUTF16("address");
1495 form.fields.push_back(field); 1515 malformed_form.fields.push_back(field);
1496 } 1516 }
1497 1517
1498 forms.push_back(new FormStructure(form)); 1518 forms.push_back(new FormStructure(malformed_form));
1499 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1519 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1500 &encoded_signatures, 1520 &encoded_signatures,
1501 &encoded_xml)); 1521 &encoded_xml));
1502 ASSERT_EQ(2U, encoded_signatures.size()); 1522 ASSERT_EQ(2U, encoded_signatures.size());
1503 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1523 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1504 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1524 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1505 EXPECT_EQ(kResponse2, encoded_xml); 1525 EXPECT_EQ(kResponse2, encoded_xml);
1506 1526
1507 // Check that we fail if there are only bad form(s). 1527 // Check that we fail if there are only bad form(s).
1508 ScopedVector<FormStructure> bad_forms; 1528 ScopedVector<FormStructure> bad_forms;
1509 bad_forms.push_back(new FormStructure(form)); 1529 bad_forms.push_back(new FormStructure(malformed_form));
1510 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(), 1530 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(),
1511 &encoded_signatures, 1531 &encoded_signatures,
1512 &encoded_xml)); 1532 &encoded_xml));
1513 EXPECT_EQ(0U, encoded_signatures.size()); 1533 EXPECT_EQ(0U, encoded_signatures.size());
1514 EXPECT_EQ("", encoded_xml); 1534 EXPECT_EQ("", encoded_xml);
1535
1536 // Check the behaviour with kEnableExperimentalFormFilling switch on.
1537 CommandLine::ForCurrentProcess()->AppendSwitch(
1538 switches::kEnableExperimentalFormFilling);
1539 // Add the previous form but with flag set.
1540 ScopedVector<FormStructure> checkable_forms;
1541 checkable_forms.push_back(new FormStructure(form));
1542
1543 ASSERT_TRUE(FormStructure::EncodeQueryRequest(checkable_forms.get(),
1544 &encoded_signatures,
1545 &encoded_xml));
1546 const char * const kSignature3 = "7747357776717901584";
1547 const char * const kResponse3 =
1548 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery "
1549 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\">"
1550 "<form signature=\"7747357776717901584\"><field signature=\"412125936\"/>"
1551 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/><field"
1552 " signature=\"747221617\"/><field signature=\"4108155786\"/><field "
1553 "signature=\"3410250678\"/><field signature=\"509334676\"/><field "
1554 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1555 "signature=\"509334676\"/><field signature=\"509334676\"/></form>"
1556 "</autofillquery>";
1557 ASSERT_EQ(1U, encoded_signatures.size());
1558 EXPECT_EQ(kSignature3, encoded_signatures[0]);
1559 EXPECT_EQ(kResponse3, encoded_xml);
1515 } 1560 }
1516 1561
1517 TEST(FormStructureTest, EncodeUploadRequest) { 1562 TEST(FormStructureTest, EncodeUploadRequest) {
1518 scoped_ptr<FormStructure> form_structure; 1563 scoped_ptr<FormStructure> form_structure;
1519 std::vector<FieldTypeSet> possible_field_types; 1564 std::vector<FieldTypeSet> possible_field_types;
1520 FormData form; 1565 FormData form;
1521 form.method = ASCIIToUTF16("post"); 1566 form.method = ASCIIToUTF16("post");
1522 form_structure.reset(new FormStructure(form)); 1567 form_structure.reset(new FormStructure(form));
1523 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1568 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1524 1569
(...skipping 25 matching lines...) Expand all
1550 form.fields.push_back(field); 1595 form.fields.push_back(field);
1551 possible_field_types.push_back(FieldTypeSet()); 1596 possible_field_types.push_back(FieldTypeSet());
1552 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1597 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1553 1598
1554 field.label = ASCIIToUTF16("Country"); 1599 field.label = ASCIIToUTF16("Country");
1555 field.name = ASCIIToUTF16("country"); 1600 field.name = ASCIIToUTF16("country");
1556 field.form_control_type = "select-one"; 1601 field.form_control_type = "select-one";
1557 form.fields.push_back(field); 1602 form.fields.push_back(field);
1558 possible_field_types.push_back(FieldTypeSet()); 1603 possible_field_types.push_back(FieldTypeSet());
1559 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1604 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1605
1606 // Add checkable field.
1607 FormFieldData checkable_field;
1608 checkable_field.is_checkable = true;
1609 checkable_field.label = ASCIIToUTF16("Checkable1");
1610 checkable_field.name = ASCIIToUTF16("Checkable1");
1611 form.fields.push_back(checkable_field);
1612 possible_field_types.push_back(FieldTypeSet());
1613 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1614
1560 form_structure.reset(new FormStructure(form)); 1615 form_structure.reset(new FormStructure(form));
1561 1616
1562 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1617 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1563 for (size_t i = 0; i < form_structure->field_count(); ++i) 1618 for (size_t i = 0; i < form_structure->field_count(); ++i)
1564 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1619 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1565 1620
1566 FieldTypeSet available_field_types; 1621 FieldTypeSet available_field_types;
1567 available_field_types.insert(NAME_FIRST); 1622 available_field_types.insert(NAME_FIRST);
1568 available_field_types.insert(NAME_LAST); 1623 available_field_types.insert(NAME_LAST);
1569 available_field_types.insert(ADDRESS_HOME_LINE1); 1624 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"; 2142 field.form_control_type = "submit";
2088 form.fields.push_back(field); 2143 form.fields.push_back(field);
2089 2144
2090 EXPECT_EQ(form, FormStructure(form).ToFormData()); 2145 EXPECT_EQ(form, FormStructure(form).ToFormData());
2091 2146
2092 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always 2147 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always
2093 // false. This forces a future author that changes this to update this test. 2148 // false. This forces a future author that changes this to update this test.
2094 form.user_submitted = true; 2149 form.user_submitted = true;
2095 EXPECT_NE(form, FormStructure(form).ToFormData()); 2150 EXPECT_NE(form, FormStructure(form).ToFormData());
2096 } 2151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698