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.
5 #include "base/memory/scoped_vector.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/autofill_field.h"
9 #include "components/autofill/core/browser/form_field.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using base::ASCIIToUTF16
;
16 TEST(FormFieldTest
, Match
) {
19 // Empty strings match.
20 EXPECT_TRUE(FormField::Match(&field
, base::string16(),
21 FormField::MATCH_LABEL
));
23 // Empty pattern matches non-empty string.
24 field
.label
= ASCIIToUTF16("a");
25 EXPECT_TRUE(FormField::Match(&field
, base::string16(),
26 FormField::MATCH_LABEL
));
28 // Strictly empty pattern matches empty string.
29 field
.label
= base::string16();
30 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("^$"),
31 FormField::MATCH_LABEL
));
33 // Strictly empty pattern does not match non-empty string.
34 field
.label
= ASCIIToUTF16("a");
35 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("^$"),
36 FormField::MATCH_LABEL
));
38 // Non-empty pattern doesn't match empty string.
39 field
.label
= base::string16();
40 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("a"),
41 FormField::MATCH_LABEL
));
44 field
.label
= ASCIIToUTF16("head_tail");
45 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("^head"),
46 FormField::MATCH_LABEL
));
47 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("^tail"),
48 FormField::MATCH_LABEL
));
51 field
.label
= ASCIIToUTF16("head_tail");
52 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("head$"),
53 FormField::MATCH_LABEL
));
54 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("tail$"),
55 FormField::MATCH_LABEL
));
58 field
.label
= ASCIIToUTF16("head_tail");
59 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("^head$"),
60 FormField::MATCH_LABEL
));
61 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("^tail$"),
62 FormField::MATCH_LABEL
));
63 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("^head_tail$"),
64 FormField::MATCH_LABEL
));
67 field
.label
= ASCIIToUTF16("m.i.");
68 // Note: This pattern is misleading as the "." characters are wild cards.
69 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("m.i."),
70 FormField::MATCH_LABEL
));
71 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("m\\.i\\."),
72 FormField::MATCH_LABEL
));
73 field
.label
= ASCIIToUTF16("mXiX");
74 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("m.i."),
75 FormField::MATCH_LABEL
));
76 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("m\\.i\\."),
77 FormField::MATCH_LABEL
));
80 field
.label
= ASCIIToUTF16("headtail");
81 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head.*tail"),
82 FormField::MATCH_LABEL
));
83 field
.label
= ASCIIToUTF16("headXtail");
84 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head.*tail"),
85 FormField::MATCH_LABEL
));
86 field
.label
= ASCIIToUTF16("headXXXtail");
87 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head.*tail"),
88 FormField::MATCH_LABEL
));
89 field
.label
= ASCIIToUTF16("headtail");
90 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("head.+tail"),
91 FormField::MATCH_LABEL
));
92 field
.label
= ASCIIToUTF16("headXtail");
93 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head.+tail"),
94 FormField::MATCH_LABEL
));
95 field
.label
= ASCIIToUTF16("headXXXtail");
96 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head.+tail"),
97 FormField::MATCH_LABEL
));
100 field
.label
= ASCIIToUTF16("head_tail");
101 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head|other"),
102 FormField::MATCH_LABEL
));
103 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("tail|other"),
104 FormField::MATCH_LABEL
));
105 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("bad|good"),
106 FormField::MATCH_LABEL
));
109 field
.label
= ASCIIToUTF16("xxxHeAd_tAiLxxx");
110 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("head_tail"),
111 FormField::MATCH_LABEL
));
114 field
.label
= ASCIIToUTF16("contains word:");
115 EXPECT_TRUE(FormField::Match(&field
, ASCIIToUTF16("\\bword\\b"),
116 FormField::MATCH_LABEL
));
117 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("\\bcon\\b"),
118 FormField::MATCH_LABEL
));
119 // Make sure the circumflex in 'crepe' is not treated as a word boundary.
120 field
.label
= base::UTF8ToUTF16("cr" "\xC3\xAA" "pe");
121 EXPECT_FALSE(FormField::Match(&field
, ASCIIToUTF16("\\bcr\\b"),
122 FormField::MATCH_LABEL
));
125 // Test that we ignore checkable elements.
126 TEST(FormFieldTest
, ParseFormFields
) {
127 ScopedVector
<AutofillField
> fields
;
128 FormFieldData field_data
;
129 field_data
.form_control_type
= "text";
131 field_data
.label
= ASCIIToUTF16("Address line1");
132 fields
.push_back(new AutofillField(field_data
, field_data
.label
));
134 field_data
.is_checkable
= true;
135 field_data
.label
= ASCIIToUTF16("Is PO Box");
136 fields
.push_back(new AutofillField(field_data
, field_data
.label
));
138 // reset |is_checkable| to false.
139 field_data
.is_checkable
= false;
141 field_data
.label
= ASCIIToUTF16("Address line2");
142 fields
.push_back(new AutofillField(field_data
, field_data
.label
));
144 ServerFieldTypeMap field_type_map
;
145 FormField::ParseFormFields(fields
.get(), true, &field_type_map
);
146 // Does not parse since there are only 2 recognized fields.
147 ASSERT_EQ(0U, field_type_map
.size());
149 field_data
.label
= ASCIIToUTF16("City");
150 fields
.push_back(new AutofillField(field_data
, field_data
.label
));
152 // Checkable element shouldn't interfere with inference of Address line2.
153 field_type_map
.clear();
154 FormField::ParseFormFields(fields
.get(), true, &field_type_map
);
155 ASSERT_EQ(3U, field_type_map
.size());
157 EXPECT_EQ(ADDRESS_HOME_LINE1
,
158 field_type_map
.find(ASCIIToUTF16("Address line1"))->second
);
159 EXPECT_EQ(ADDRESS_HOME_LINE2
,
160 field_type_map
.find(ASCIIToUTF16("Address line2"))->second
);
163 } // namespace autofill