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

Side by Side Diff: chrome/browser/autofill/form_field_unittest.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 months 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/autofill/form_field.cc ('k') | chrome/browser/autofill/form_group.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/memory/scoped_vector.h"
6 #include "base/string16.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/autofill/autofill_field.h"
9 #include "chrome/browser/autofill/form_field.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 TEST(FormFieldTest, Match) {
13 AutofillField field;
14
15 // Empty strings match.
16 EXPECT_TRUE(FormField::Match(&field, string16(), FormField::MATCH_LABEL));
17
18 // Empty pattern matches non-empty string.
19 field.label = ASCIIToUTF16("a");
20 EXPECT_TRUE(FormField::Match(&field, string16(), FormField::MATCH_LABEL));
21
22 // Strictly empty pattern matches empty string.
23 field.label = string16();
24 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"),
25 FormField::MATCH_LABEL));
26
27 // Strictly empty pattern does not match non-empty string.
28 field.label = ASCIIToUTF16("a");
29 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"),
30 FormField::MATCH_LABEL));
31
32 // Non-empty pattern doesn't match empty string.
33 field.label = string16();
34 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"),
35 FormField::MATCH_LABEL));
36
37 // Beginning of line.
38 field.label = ASCIIToUTF16("head_tail");
39 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"),
40 FormField::MATCH_LABEL));
41 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"),
42 FormField::MATCH_LABEL));
43
44 // End of line.
45 field.label = ASCIIToUTF16("head_tail");
46 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"),
47 FormField::MATCH_LABEL));
48 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"),
49 FormField::MATCH_LABEL));
50
51 // Exact.
52 field.label = ASCIIToUTF16("head_tail");
53 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"),
54 FormField::MATCH_LABEL));
55 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"),
56 FormField::MATCH_LABEL));
57 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"),
58 FormField::MATCH_LABEL));
59
60 // Escaped dots.
61 field.label = ASCIIToUTF16("m.i.");
62 // Note: This pattern is misleading as the "." characters are wild cards.
63 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."),
64 FormField::MATCH_LABEL));
65 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
66 FormField::MATCH_LABEL));
67 field.label = ASCIIToUTF16("mXiX");
68 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."),
69 FormField::MATCH_LABEL));
70 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
71 FormField::MATCH_LABEL));
72
73 // Repetition.
74 field.label = ASCIIToUTF16("headtail");
75 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"),
76 FormField::MATCH_LABEL));
77 field.label = ASCIIToUTF16("headXtail");
78 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"),
79 FormField::MATCH_LABEL));
80 field.label = ASCIIToUTF16("headXXXtail");
81 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"),
82 FormField::MATCH_LABEL));
83 field.label = ASCIIToUTF16("headtail");
84 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"),
85 FormField::MATCH_LABEL));
86 field.label = ASCIIToUTF16("headXtail");
87 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"),
88 FormField::MATCH_LABEL));
89 field.label = ASCIIToUTF16("headXXXtail");
90 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"),
91 FormField::MATCH_LABEL));
92
93 // Alternation.
94 field.label = ASCIIToUTF16("head_tail");
95 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"),
96 FormField::MATCH_LABEL));
97 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"),
98 FormField::MATCH_LABEL));
99 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"),
100 FormField::MATCH_LABEL));
101
102 // Case sensitivity.
103 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx");
104 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"),
105 FormField::MATCH_LABEL));
106
107 // Word boundaries.
108 field.label = ASCIIToUTF16("contains word:");
109 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("\\bword\\b"),
110 FormField::MATCH_LABEL));
111 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcon\\b"),
112 FormField::MATCH_LABEL));
113 // Make sure the circumflex in 'crepe' is not treated as a word boundary.
114 field.label = UTF8ToUTF16("cr" "\xC3\xAA" "pe");
115 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcr\\b"),
116 FormField::MATCH_LABEL));
117 }
118
119 // Test that we ignore checkable elements.
120 TEST(FormFieldTest, ParseFormFields) {
121 ScopedVector<AutofillField> fields;
122 FormFieldData field_data;
123 field_data.form_control_type = "text";
124
125 field_data.label = ASCIIToUTF16("Address line1");
126 fields.push_back(new AutofillField(field_data, field_data.label));
127
128 field_data.is_checkable = true;
129 field_data.label = ASCIIToUTF16("Is PO Box");
130 fields.push_back(new AutofillField(field_data, field_data.label));
131
132 // reset is_checkable to false.
133 field_data.is_checkable = false;
134 field_data.label = ASCIIToUTF16("Address line2");
135 fields.push_back(new AutofillField(field_data, field_data.label));
136
137 FieldTypeMap field_type_map;
138 FormField::ParseFormFields(fields.get(), &field_type_map);
139 // Checkable element shouldn't interfere with inference of Address line2.
140 EXPECT_EQ(2U, field_type_map.size());
141
142 EXPECT_EQ(ADDRESS_HOME_LINE1,
143 field_type_map.find(ASCIIToUTF16("Address line1"))->second);
144 EXPECT_EQ(ADDRESS_HOME_LINE2,
145 field_type_map.find(ASCIIToUTF16("Address line2"))->second);
146 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/form_field.cc ('k') | chrome/browser/autofill/form_group.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698