summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-22 22:22:01 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-22 22:22:01 +0000
commitc52b0758d6286483215d395c62aad0b333e5d6a4 (patch)
treef52d6c4ec05862d731eb0d6aaedba07ab3eacacf /chrome/browser/autofill
parentc2de68aeea9a829af41f087f2c3b3c347f7f196e (diff)
downloadchromium_src-c52b0758d6286483215d395c62aad0b333e5d6a4.zip
chromium_src-c52b0758d6286483215d395c62aad0b333e5d6a4.tar.gz
chromium_src-c52b0758d6286483215d395c62aad0b333e5d6a4.tar.bz2
Add tests for BillingAddress::GetPossibleFieldTypes. Fix a few issues that popped up as a result of writing the tests. Remove an unnecessary test fixture from the AutoFillProfile unit test.
BUG=none TEST=BillingAddress.GetPossibleFieldTypes Review URL: http://codereview.chromium.org/652057 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39646 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autofill')
-rw-r--r--chrome/browser/autofill/address.cc47
-rw-r--r--chrome/browser/autofill/address.h26
-rw-r--r--chrome/browser/autofill/autofill_profile_unittest.cc11
-rw-r--r--chrome/browser/autofill/billing_address_unittest.cc312
4 files changed, 356 insertions, 40 deletions
diff --git a/chrome/browser/autofill/address.cc b/chrome/browser/autofill/address.cc
index c76f6af..9f2d335 100644
--- a/chrome/browser/autofill/address.cc
+++ b/chrome/browser/autofill/address.cc
@@ -9,6 +9,8 @@
#include "chrome/browser/autofill/autofill_type.h"
#include "chrome/browser/autofill/field_types.h"
+#define U(x) (UTF16ToUTF8(x).c_str())
+
static const string16 kAddressSplitChars = ASCIIToUTF16("-,#. ");
static const AutoFillType::FieldTypeSubGroup kAutoFillAddressTypes[] = {
@@ -24,6 +26,15 @@ static const int kAutoFillAddressLength = arraysize(kAutoFillAddressTypes);
void Address::GetPossibleFieldTypes(const string16& text,
FieldTypeSet* possible_types) const {
+ DCHECK(possible_types);
+ if (!possible_types)
+ return;
+
+ // If the text to match against the field types is empty, then no results will
+ // match.
+ if (text.empty())
+ return;
+
if (IsLine1(text))
possible_types->insert(GetLine1Type());
@@ -134,6 +145,19 @@ void Address::Clone(const Address& address) {
set_zip_code(address.zip_code());
}
+Address::Address(const Address& address)
+ : FormGroup(),
+ line1_tokens_(address.line1_tokens_),
+ line2_tokens_(address.line2_tokens_),
+ line1_(address.line1_),
+ line2_(address.line2_),
+ apt_num_(address.apt_num_),
+ city_(address.city_),
+ state_(address.state_),
+ country_(address.country_),
+ zip_code_(address.zip_code_) {
+}
+
void Address::set_line1(const string16& line1) {
line1_ = line1;
line1_tokens_.clear();
@@ -152,19 +176,6 @@ void Address::set_line2(const string16& line2) {
*iter = StringToLowerASCII(*iter);
}
-Address::Address(const Address& address)
- : FormGroup(),
- line1_tokens_(address.line1_tokens_),
- line2_tokens_(address.line2_tokens_),
- line1_(address.line1_),
- line2_(address.line2_),
- apt_num_(address.apt_num_),
- city_(address.city_),
- state_(address.state_),
- country_(address.country_),
- zip_code_(address.zip_code_) {
-}
-
bool Address::IsLine1(const string16& text) const {
return IsLineMatch(text, line1_tokens_);
}
@@ -174,19 +185,19 @@ bool Address::IsLine2(const string16& text) const {
}
bool Address::IsAptNum(const string16& text) const {
- return (StringToLowerASCII(apt_num_) == text);
+ return (StringToLowerASCII(apt_num_) == StringToLowerASCII(text));
}
bool Address::IsCity(const string16& text) const {
- return (StringToLowerASCII(city_) == text);
+ return (StringToLowerASCII(city_) == StringToLowerASCII(text));
}
bool Address::IsState(const string16& text) const {
- return (StringToLowerASCII(state_) == text);
+ return (StringToLowerASCII(state_) == StringToLowerASCII(text));
}
bool Address::IsCountry(const string16& text) const {
- return (StringToLowerASCII(country_) == text);
+ return (StringToLowerASCII(country_) == StringToLowerASCII(text));
}
bool Address::IsZipCode(const string16& text) const {
@@ -258,7 +269,7 @@ bool Address::IsWordInLine(const string16& word,
const LineTokens& line_tokens) const {
LineTokens::const_iterator iter;
for (iter = line_tokens.begin(); iter != line_tokens.end(); ++iter) {
- if (word == *iter)
+ if (StringToLowerASCII(word) == *iter)
return true;
}
diff --git a/chrome/browser/autofill/address.h b/chrome/browser/autofill/address.h
index a8307f2..191dfa7 100644
--- a/chrome/browser/autofill/address.h
+++ b/chrome/browser/autofill/address.h
@@ -10,8 +10,6 @@
#include "base/string16.h"
#include "chrome/browser/autofill/form_group.h"
-typedef std::vector<string16> LineTokens;
-
// A form group that stores address information.
class Address : public FormGroup {
public:
@@ -34,6 +32,13 @@ class Address : public FormGroup {
// Sets the values of this object to the values in |address|.
void Clone(const Address& address);
+ protected:
+ explicit Address(const Address& address);
+
+ private:
+ // Vector of tokens in an address line.
+ typedef std::vector<string16> LineTokens;
+
string16 line1() const { return line1_; }
string16 line2() const { return line2_; }
string16 apt_num() const { return apt_num_; }
@@ -50,8 +55,6 @@ class Address : public FormGroup {
void set_country(const string16& country) { country_ = country; }
void set_zip_code(const string16& zip_code) { zip_code_ = zip_code; }
- protected:
- explicit Address(const Address& address);
void operator=(const Address& address);
// The following functions match |text| against the various values of the
@@ -80,6 +83,13 @@ class Address : public FormGroup {
const string16& info,
string16* match) const;
+ // Returns true if all of the tokens in |text| match the tokens in
+ // |line_tokens|.
+ bool IsLineMatch(const string16& text, const LineTokens& line_tokens) const;
+
+ // Returns true if |word| is one of the tokens in |line_tokens|.
+ bool IsWordInLine(const string16& word, const LineTokens& line_tokens) const;
+
// List of tokens in each part of |line1_| and |line2_|.
LineTokens line1_tokens_;
LineTokens line2_tokens_;
@@ -92,14 +102,6 @@ class Address : public FormGroup {
string16 state_;
string16 country_;
string16 zip_code_;
-
- private:
- // Returns true if all of the tokens in |text| match the tokens in
- // |line_tokens|.
- bool IsLineMatch(const string16& text, const LineTokens& line_tokens) const;
-
- // Returns true if |word| is one of the tokens in |line_tokens|.
- bool IsWordInLine(const string16& word, const LineTokens& line_tokens) const;
};
#endif // CHROME_BROWSER_AUTOFILL_ADDRESS_H_
diff --git a/chrome/browser/autofill/autofill_profile_unittest.cc b/chrome/browser/autofill/autofill_profile_unittest.cc
index 344cdfe..3a1e086 100644
--- a/chrome/browser/autofill/autofill_profile_unittest.cc
+++ b/chrome/browser/autofill/autofill_profile_unittest.cc
@@ -9,18 +9,9 @@
namespace {
-// Unit tests for autofill |AutoFillProfile| class.
-class AutoFillProfileTest : public testing::Test {
- protected:
- AutoFillProfileTest() {
- }
- private:
- DISALLOW_COPY_AND_ASSIGN(AutoFillProfileTest);
-};
-
// Tests different possibilities for summary string generation.
// Based on existence of first name, last name, and address line 1.
-TEST_F(AutoFillProfileTest, PreviewSummaryString) {
+TEST(AutoFillProfileTest, PreviewSummaryString) {
// Case 0/null: ""
AutoFillProfile profile0(string16(), 0);
string16 summary0 = profile0.PreviewSummary();
diff --git a/chrome/browser/autofill/billing_address_unittest.cc b/chrome/browser/autofill/billing_address_unittest.cc
new file mode 100644
index 0000000..a8dfb7d
--- /dev/null
+++ b/chrome/browser/autofill/billing_address_unittest.cc
@@ -0,0 +1,312 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/autofill/billing_address.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(BillingAddressTest, GetPossibleFieldTypes) {
+ BillingAddress address;
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_LINE1),
+ UTF8ToUTF16("123 Appian Way"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_LINE2), UTF8ToUTF16("Unit 6"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_APT_NUM), UTF8ToUTF16("#6"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_CITY), UTF8ToUTF16("Paris"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_STATE), UTF8ToUTF16("Texas"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_ZIP), UTF8ToUTF16("12345"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_COUNTRY), UTF8ToUTF16("USA"));
+
+ // DCHECK on NULL |possible_types|.
+ ASSERT_DEBUG_DEATH(address.GetPossibleFieldTypes(string16(), NULL), "");
+
+ // Empty string.
+ FieldTypeSet possible_types;
+ address.GetPossibleFieldTypes(string16(), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Only use split-chars for the address.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("-,#. "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // ADDRESS_BILLING_LINE1 =====================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("123 Appian Way"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // Fields are mixed up.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Way 123 Appian"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("123 aPPiaN wAy"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // Case-insensitive match with fields mixed up. The previous test doesn't
+ // do a good job of testing for case-insensitivity because the underlying
+ // algorithm stops search when it matches '123'.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("wAy aPpIAn"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("123 Appian"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("123 Appian Way #6"),
+ &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text in the middle
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("123 Middle Appian Way"),
+ &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace doesn't matter.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" 123 Appian Way "),
+ &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+
+ // ADDRESS_BILLING_LINE2 =====================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Unit 6"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // Fields are mixed up.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("6 Unit"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("uNiT 6"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Unit"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Unit 6 Extra"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text in the middle
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Unit Middle 6"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace doesn't matter.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" Unit 6 "), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // ADDRESS_BILLING_APT_NUM ===================================================
+
+ // Exact match. This matches the apartment number exactly, and also matches
+ // 'Unit 6' because of the 6 and the fact that '#' is an address separator,
+ // which is ignored in the search for an address line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("#6"), &possible_types);
+ ASSERT_EQ(2U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_APT_NUM) !=
+ possible_types.end());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_APT_NUM), UTF8ToUTF16("Num 10"));
+ address.GetPossibleFieldTypes(UTF8ToUTF16("nuM 10"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_APT_NUM) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Num"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Num 10 More"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text in the middle
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Num Middle 10"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace does matter for apartment number.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" Num 10 "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // ADDRESS_BILLING_CITY ======================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Paris"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_CITY) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("pARiS"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_CITY) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Par"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Paris City"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace does matter for apartment number.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" Paris "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // ADDRESS_BILLING_STATE =====================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Texas"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_STATE) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("tExAs"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_STATE) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Tex"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Texas State"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace does matter for apartment number.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" Texas "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // ADDRESS_BILLING_COUNTRY ===================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("USA"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_COUNTRY) !=
+ possible_types.end());
+
+ // The match is case-insensitive.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("uSa"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_COUNTRY) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("US"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("US Country"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace does matter for apartment number.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" US "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // ADDRESS_BILLING_ZIP =======================================================
+
+ // Exact match.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("12345"), &possible_types);
+ ASSERT_EQ(1U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_ZIP) !=
+ possible_types.end());
+
+ // The text is not complete.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("1234"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Extra text on the line.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("12345 678"), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Whitespace does matter for apartment number.
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16(" 12345 "), &possible_types);
+ ASSERT_EQ(0U, possible_types.size());
+
+ // Misc =======================================================
+
+ // More than one type can match.
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_LINE1), UTF8ToUTF16("Same"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_LINE2), UTF8ToUTF16("Same"));
+ address.SetInfo(AutoFillType(ADDRESS_BILLING_APT_NUM), UTF8ToUTF16("Same"));
+ possible_types.clear();
+ address.GetPossibleFieldTypes(UTF8ToUTF16("Same"), &possible_types);
+ ASSERT_EQ(3U, possible_types.size());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE1) !=
+ possible_types.end());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_LINE2) !=
+ possible_types.end());
+ EXPECT_TRUE(possible_types.find(ADDRESS_BILLING_APT_NUM) !=
+ possible_types.end());
+}