diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-07 21:44:29 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-07 21:44:29 +0000 |
commit | 7465b14cbc710e2cc68af27c62eeed5708bef476 (patch) | |
tree | 4f51960a253d32d44d6a4a6ef9864cbee7a470e8 /chrome | |
parent | b1e4eb506631e71476d10aeeebcd347585b31566 (diff) | |
download | chromium_src-7465b14cbc710e2cc68af27c62eeed5708bef476.zip chromium_src-7465b14cbc710e2cc68af27c62eeed5708bef476.tar.gz chromium_src-7465b14cbc710e2cc68af27c62eeed5708bef476.tar.bz2 |
Add tests for AutoFillField.
BUG=none
TEST=AutoFillFieldTest
Review URL: http://codereview.chromium.org/669203
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40876 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/autofill/autofill_field.cc | 18 | ||||
-rw-r--r-- | chrome/browser/autofill/autofill_field.h | 10 | ||||
-rw-r--r-- | chrome/browser/autofill/autofill_field_unittest.cc | 92 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
4 files changed, 108 insertions, 13 deletions
diff --git a/chrome/browser/autofill/autofill_field.cc b/chrome/browser/autofill/autofill_field.cc index 68dc91e..3ba943a 100644 --- a/chrome/browser/autofill/autofill_field.cc +++ b/chrome/browser/autofill/autofill_field.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. @@ -38,6 +38,14 @@ AutoFillField::AutoFillField(const webkit_glue::FormField& field, heuristic_type_(UNKNOWN_TYPE) { } +void AutoFillField::set_heuristic_type(const AutoFillFieldType& type) { + DCHECK(type >= 0 && type < MAX_VALID_FIELD_TYPE); + if (type >= 0 && type < MAX_VALID_FIELD_TYPE) + heuristic_type_ = type; + else + heuristic_type_ = UNKNOWN_TYPE; +} + AutoFillFieldType AutoFillField::type() const { if (server_type_ != NO_SERVER_DATA) return server_type_; @@ -56,14 +64,6 @@ std::string AutoFillField::FieldSignature() const { return Hash32Bit(field_string); } -void AutoFillField::set_heuristic_type(const AutoFillFieldType& type) { - DCHECK(type >= 0 && type < MAX_VALID_FIELD_TYPE); - if (type >= 0 && type < MAX_VALID_FIELD_TYPE) - heuristic_type_ = type; - else - heuristic_type_ = UNKNOWN_TYPE; -} - bool AutoFillField::IsFieldFillable() const { return type() != UNKNOWN_TYPE; } diff --git a/chrome/browser/autofill/autofill_field.h b/chrome/browser/autofill/autofill_field.h index 2deb239..f28ecf8 100644 --- a/chrome/browser/autofill/autofill_field.h +++ b/chrome/browser/autofill/autofill_field.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. @@ -20,7 +20,12 @@ class AutoFillField : public webkit_glue::FormField { const string16& unique_name() const { return unique_name_; } AutoFillFieldType heuristic_type() const { return heuristic_type_; } + AutoFillFieldType server_type() const { return server_type_; } const FieldTypeSet& possible_types() const { return possible_types_; } + + // Sets the heuristic type of this field, validating the input. + void set_heuristic_type(const AutoFillFieldType& type); + void set_server_type(const AutoFillFieldType& type) { server_type_ = type; } void set_possible_types(const FieldTypeSet& possible_types) { possible_types_ = possible_types; } @@ -36,9 +41,6 @@ class AutoFillField : public webkit_glue::FormField { // input type in a 32-bit hash. std::string FieldSignature() const; - // Sets the heuristic type of this field, validating the input. - void set_heuristic_type(const AutoFillFieldType& type); - // Returns true if the field type has been determined (without the text in the // field). bool IsFieldFillable() const; diff --git a/chrome/browser/autofill/autofill_field_unittest.cc b/chrome/browser/autofill/autofill_field_unittest.cc new file mode 100644 index 0000000..48d29ec --- /dev/null +++ b/chrome/browser/autofill/autofill_field_unittest.cc @@ -0,0 +1,92 @@ +// 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/string_util.h" +#include "chrome/browser/autofill/autofill_field.h" +#include "chrome/browser/autofill/field_types.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +TEST(AutoFillFieldTest, Type) { + AutoFillField field; + ASSERT_EQ(NO_SERVER_DATA, field.server_type()); + ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); + + // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. + EXPECT_EQ(UNKNOWN_TYPE, field.type()); + + // Set the heuristic type and check it. + field.set_heuristic_type(NAME_FIRST); + EXPECT_EQ(NAME_FIRST, field.type()); + + // Set the server type and check it. + field.set_server_type(ADDRESS_BILLING_LINE1); + EXPECT_EQ(ADDRESS_BILLING_LINE1, field.type()); + + // Remove the server type to make sure the heuristic type is preserved. + field.set_server_type(NO_SERVER_DATA); + EXPECT_EQ(NAME_FIRST, field.type()); +} + +TEST(AutoFillFieldTest, IsEmpty) { + AutoFillField field; + ASSERT_EQ(string16(), field.value()); + + // Field value is empty. + EXPECT_TRUE(field.IsEmpty()); + + // Field value is non-empty. + field.set_value(ASCIIToUTF16("Value")); + EXPECT_FALSE(field.IsEmpty()); +} + +TEST(AutoFillFieldTest, FieldSignature) { + AutoFillField field; + ASSERT_EQ(string16(), field.name()); + ASSERT_EQ(string16(), field.form_control_type()); + + // Signature is empty. + EXPECT_EQ("2085434232", field.FieldSignature()); + + // Field name is set. + field.set_name(ASCIIToUTF16("Name")); + EXPECT_EQ("1606968241", field.FieldSignature()); + + // Field form control type is set. + field.set_form_control_type(ASCIIToUTF16("Text")); + EXPECT_EQ("4246049809", field.FieldSignature()); + + // Heuristic type does not affect FieldSignature. + field.set_heuristic_type(NAME_FIRST); + EXPECT_EQ("4246049809", field.FieldSignature()); + + // Server type does not affect FieldSignature. + field.set_server_type(NAME_LAST); + EXPECT_EQ("4246049809", field.FieldSignature()); +} + +TEST(AutoFillFieldTest, IsFieldFillable) { + AutoFillField field; + ASSERT_EQ(UNKNOWN_TYPE, field.type()); + + // Type is unknown. + EXPECT_FALSE(field.IsFieldFillable()); + + // Only heuristic type is set. + field.set_heuristic_type(NAME_FIRST); + EXPECT_TRUE(field.IsFieldFillable()); + + // Only server type is set. + field.set_heuristic_type(UNKNOWN_TYPE); + field.set_server_type(NAME_LAST); + EXPECT_TRUE(field.IsFieldFillable()); + + // Both types set. + field.set_heuristic_type(NAME_FIRST); + field.set_server_type(NAME_LAST); + EXPECT_TRUE(field.IsFieldFillable()); +} + +} // namespace diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index dd3a75f..2648ee7 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -542,6 +542,7 @@ 'browser/autofill/autofill_credit_card_model_mac_unittest.mm', 'browser/autofill/autofill_credit_card_view_controller_mac_unittest.mm', 'browser/autofill/autofill_dialog_controller_mac_unittest.mm', + 'browser/autofill/autofill_field_unittest.cc', 'browser/autofill/autofill_infobar_delegate_unittest.cc', 'browser/autofill/autofill_profile_unittest.cc', 'browser/autofill/billing_address_unittest.cc', |