Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 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 "base/strings/string_util.h" |
| 6 | #include "base/strings/utf_string_conversions.h" |
| 7 | #include "components/autofill/core/browser/autofill_field.h" |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 8 | #include "components/autofill/core/browser/autofill_type.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 9 | #include "components/autofill/core/browser/field_types.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace autofill { |
| 13 | namespace { |
| 14 | |
| 15 | TEST(AutofillFieldTest, Type) { |
| 16 | AutofillField field; |
| 17 | ASSERT_EQ(NO_SERVER_DATA, field.server_type()); |
| 18 | ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); |
| 19 | |
| 20 | // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 21 | EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 22 | |
| 23 | // Set the heuristic type and check it. |
| 24 | field.set_heuristic_type(NAME_FIRST); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 25 | EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType()); |
| 26 | EXPECT_EQ(NAME, field.Type().group()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 27 | |
| 28 | // Set the server type and check it. |
| 29 | field.set_server_type(ADDRESS_BILLING_LINE1); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 30 | EXPECT_EQ(ADDRESS_HOME_LINE1, field.Type().GetStorableType()); |
| 31 | EXPECT_EQ(ADDRESS_BILLING, field.Type().group()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 32 | |
| 33 | // Remove the server type to make sure the heuristic type is preserved. |
| 34 | field.set_server_type(NO_SERVER_DATA); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 35 | EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType()); |
| 36 | EXPECT_EQ(NAME, field.Type().group()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TEST(AutofillFieldTest, IsEmpty) { |
| 40 | AutofillField field; |
| 41 | ASSERT_EQ(base::string16(), field.value); |
| 42 | |
| 43 | // Field value is empty. |
| 44 | EXPECT_TRUE(field.IsEmpty()); |
| 45 | |
| 46 | // Field value is non-empty. |
| 47 | field.value = ASCIIToUTF16("Value"); |
| 48 | EXPECT_FALSE(field.IsEmpty()); |
| 49 | } |
| 50 | |
| 51 | TEST(AutofillFieldTest, FieldSignature) { |
| 52 | AutofillField field; |
| 53 | ASSERT_EQ(base::string16(), field.name); |
| 54 | ASSERT_EQ(std::string(), field.form_control_type); |
| 55 | |
| 56 | // Signature is empty. |
| 57 | EXPECT_EQ("2085434232", field.FieldSignature()); |
| 58 | |
| 59 | // Field name is set. |
| 60 | field.name = ASCIIToUTF16("Name"); |
| 61 | EXPECT_EQ("1606968241", field.FieldSignature()); |
| 62 | |
| 63 | // Field form control type is set. |
| 64 | field.form_control_type = "text"; |
| 65 | EXPECT_EQ("502192749", field.FieldSignature()); |
| 66 | |
| 67 | // Heuristic type does not affect FieldSignature. |
| 68 | field.set_heuristic_type(NAME_FIRST); |
| 69 | EXPECT_EQ("502192749", field.FieldSignature()); |
| 70 | |
| 71 | // Server type does not affect FieldSignature. |
| 72 | field.set_server_type(NAME_LAST); |
| 73 | EXPECT_EQ("502192749", field.FieldSignature()); |
| 74 | } |
| 75 | |
| 76 | TEST(AutofillFieldTest, IsFieldFillable) { |
| 77 | AutofillField field; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 78 | ASSERT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 79 | |
| 80 | // Type is unknown. |
| 81 | EXPECT_FALSE(field.IsFieldFillable()); |
| 82 | |
| 83 | // Only heuristic type is set. |
| 84 | field.set_heuristic_type(NAME_FIRST); |
| 85 | EXPECT_TRUE(field.IsFieldFillable()); |
| 86 | |
| 87 | // Only server type is set. |
| 88 | field.set_heuristic_type(UNKNOWN_TYPE); |
| 89 | field.set_server_type(NAME_LAST); |
| 90 | EXPECT_TRUE(field.IsFieldFillable()); |
| 91 | |
| 92 | // Both types set. |
| 93 | field.set_heuristic_type(NAME_FIRST); |
| 94 | field.set_server_type(NAME_LAST); |
| 95 | EXPECT_TRUE(field.IsFieldFillable()); |
| 96 | } |
| 97 | |
| 98 | } // namespace |
| 99 | } // namespace autofill |