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

Side by Side Diff: components/autofill/core/browser/form_field_unittest.cc

Issue 1453193002: autofill: switch autofill_regexes to RE2 library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address reviews Created 5 years 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/browser/form_field.h"
6
7 #include <string>
8
5 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
6 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/autofill_field.h" 12 #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" 13 #include "testing/gtest/include/gtest/gtest.h"
11 14
12 using base::ASCIIToUTF16; 15 using base::ASCIIToUTF16;
13 16
14 namespace autofill { 17 namespace autofill {
15 18
16 TEST(FormFieldTest, Match) { 19 TEST(FormFieldTest, Match) {
17 AutofillField field; 20 AutofillField field;
18 21
19 // Empty strings match. 22 // Empty strings match.
20 EXPECT_TRUE(FormField::Match(&field, base::string16(), 23 EXPECT_TRUE(FormField::Match(&field, std::string(), FormField::MATCH_LABEL));
21 FormField::MATCH_LABEL));
22 24
23 // Empty pattern matches non-empty string. 25 // Empty pattern matches non-empty string.
24 field.label = ASCIIToUTF16("a"); 26 field.label = ASCIIToUTF16("a");
25 EXPECT_TRUE(FormField::Match(&field, base::string16(), 27 EXPECT_TRUE(FormField::Match(&field, std::string(), FormField::MATCH_LABEL));
26 FormField::MATCH_LABEL));
27 28
28 // Strictly empty pattern matches empty string. 29 // Strictly empty pattern matches empty string.
29 field.label = base::string16(); 30 field.label = base::string16();
30 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"), 31 EXPECT_TRUE(FormField::Match(&field, "^$", FormField::MATCH_LABEL));
31 FormField::MATCH_LABEL));
32 32
33 // Strictly empty pattern does not match non-empty string. 33 // Strictly empty pattern does not match non-empty string.
34 field.label = ASCIIToUTF16("a"); 34 field.label = ASCIIToUTF16("a");
35 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"), 35 EXPECT_FALSE(FormField::Match(&field, "^$", FormField::MATCH_LABEL));
36 FormField::MATCH_LABEL));
37 36
38 // Non-empty pattern doesn't match empty string. 37 // Non-empty pattern doesn't match empty string.
39 field.label = base::string16(); 38 field.label = base::string16();
40 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"), 39 EXPECT_FALSE(FormField::Match(&field, "a", FormField::MATCH_LABEL));
41 FormField::MATCH_LABEL));
42 40
43 // Beginning of line. 41 // Beginning of line.
44 field.label = ASCIIToUTF16("head_tail"); 42 field.label = ASCIIToUTF16("head_tail");
45 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"), 43 EXPECT_TRUE(FormField::Match(&field, "^head", FormField::MATCH_LABEL));
46 FormField::MATCH_LABEL)); 44 EXPECT_FALSE(FormField::Match(&field, "^tail", FormField::MATCH_LABEL));
47 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"),
48 FormField::MATCH_LABEL));
49 45
50 // End of line. 46 // End of line.
51 field.label = ASCIIToUTF16("head_tail"); 47 field.label = ASCIIToUTF16("head_tail");
52 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"), 48 EXPECT_FALSE(FormField::Match(&field, "head$", FormField::MATCH_LABEL));
53 FormField::MATCH_LABEL)); 49 EXPECT_TRUE(FormField::Match(&field, "tail$", FormField::MATCH_LABEL));
54 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"),
55 FormField::MATCH_LABEL));
56 50
57 // Exact. 51 // Exact.
58 field.label = ASCIIToUTF16("head_tail"); 52 field.label = ASCIIToUTF16("head_tail");
59 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"), 53 EXPECT_FALSE(FormField::Match(&field, "^head$", FormField::MATCH_LABEL));
60 FormField::MATCH_LABEL)); 54 EXPECT_FALSE(FormField::Match(&field, "^tail$", FormField::MATCH_LABEL));
61 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"), 55 EXPECT_TRUE(FormField::Match(&field, "^head_tail$", FormField::MATCH_LABEL));
62 FormField::MATCH_LABEL));
63 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"),
64 FormField::MATCH_LABEL));
65 56
66 // Escaped dots. 57 // Escaped dots.
67 field.label = ASCIIToUTF16("m.i."); 58 field.label = ASCIIToUTF16("m.i.");
68 // Note: This pattern is misleading as the "." characters are wild cards. 59 // Note: This pattern is misleading as the "." characters are wild cards.
69 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 60 EXPECT_TRUE(FormField::Match(&field, "m.i.", FormField::MATCH_LABEL));
70 FormField::MATCH_LABEL)); 61 EXPECT_TRUE(FormField::Match(&field, "m\\.i\\.", FormField::MATCH_LABEL));
71 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
72 FormField::MATCH_LABEL));
73 field.label = ASCIIToUTF16("mXiX"); 62 field.label = ASCIIToUTF16("mXiX");
74 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 63 EXPECT_TRUE(FormField::Match(&field, "m.i.", FormField::MATCH_LABEL));
75 FormField::MATCH_LABEL)); 64 EXPECT_FALSE(FormField::Match(&field, "m\\.i\\.", FormField::MATCH_LABEL));
76 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
77 FormField::MATCH_LABEL));
78 65
79 // Repetition. 66 // Repetition.
80 field.label = ASCIIToUTF16("headtail"); 67 field.label = ASCIIToUTF16("headtail");
81 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 68 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
82 FormField::MATCH_LABEL));
83 field.label = ASCIIToUTF16("headXtail"); 69 field.label = ASCIIToUTF16("headXtail");
84 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 70 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
85 FormField::MATCH_LABEL));
86 field.label = ASCIIToUTF16("headXXXtail"); 71 field.label = ASCIIToUTF16("headXXXtail");
87 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 72 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
88 FormField::MATCH_LABEL));
89 field.label = ASCIIToUTF16("headtail"); 73 field.label = ASCIIToUTF16("headtail");
90 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 74 EXPECT_FALSE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
91 FormField::MATCH_LABEL));
92 field.label = ASCIIToUTF16("headXtail"); 75 field.label = ASCIIToUTF16("headXtail");
93 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 76 EXPECT_TRUE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
94 FormField::MATCH_LABEL));
95 field.label = ASCIIToUTF16("headXXXtail"); 77 field.label = ASCIIToUTF16("headXXXtail");
96 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 78 EXPECT_TRUE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
97 FormField::MATCH_LABEL));
98 79
99 // Alternation. 80 // Alternation.
100 field.label = ASCIIToUTF16("head_tail"); 81 field.label = ASCIIToUTF16("head_tail");
101 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"), 82 EXPECT_TRUE(FormField::Match(&field, "head|other", FormField::MATCH_LABEL));
102 FormField::MATCH_LABEL)); 83 EXPECT_TRUE(FormField::Match(&field, "tail|other", FormField::MATCH_LABEL));
103 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"), 84 EXPECT_FALSE(FormField::Match(&field, "bad|good", FormField::MATCH_LABEL));
104 FormField::MATCH_LABEL));
105 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"),
106 FormField::MATCH_LABEL));
107 85
108 // Case sensitivity. 86 // Case sensitivity.
109 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx"); 87 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx");
110 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"), 88 EXPECT_TRUE(FormField::Match(&field, "head_tail", FormField::MATCH_LABEL));
111 FormField::MATCH_LABEL));
112 89
113 // Word boundaries. 90 // Word boundaries.
91 const std::string kWordBoundary = "(\\A|\\z|\\PL)";
114 field.label = ASCIIToUTF16("contains word:"); 92 field.label = ASCIIToUTF16("contains word:");
115 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("\\bword\\b"), 93 EXPECT_TRUE(FormField::Match(&field, kWordBoundary + "word" + kWordBoundary,
116 FormField::MATCH_LABEL)); 94 FormField::MATCH_LABEL));
117 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcon\\b"), 95 EXPECT_FALSE(FormField::Match(&field, kWordBoundary + "con" + kWordBoundary,
118 FormField::MATCH_LABEL)); 96 FormField::MATCH_LABEL));
119 // Make sure the circumflex in 'crepe' is not treated as a word boundary. 97 // Make sure the circumflex in 'crepe' is not treated as a word boundary.
120 field.label = base::UTF8ToUTF16("cr" "\xC3\xAA" "pe"); 98 field.label = base::UTF8ToUTF16("cr" "\xC3\xAA" "pe");
121 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcr\\b"), 99 EXPECT_FALSE(FormField::Match(&field, kWordBoundary + "cr" + kWordBoundary,
122 FormField::MATCH_LABEL)); 100 FormField::MATCH_LABEL));
123 } 101 }
124 102
125 // Test that we ignore checkable elements. 103 // Test that we ignore checkable elements.
126 TEST(FormFieldTest, ParseFormFields) { 104 TEST(FormFieldTest, ParseFormFields) {
127 ScopedVector<AutofillField> fields; 105 ScopedVector<AutofillField> fields;
128 FormFieldData field_data; 106 FormFieldData field_data;
129 field_data.form_control_type = "text"; 107 field_data.form_control_type = "text";
130 108
131 field_data.label = ASCIIToUTF16("Address line1"); 109 field_data.label = ASCIIToUTF16("Address line1");
(...skipping 22 matching lines...) Expand all
154 FormField::ParseFormFields(fields.get(), true, &field_type_map); 132 FormField::ParseFormFields(fields.get(), true, &field_type_map);
155 ASSERT_EQ(3U, field_type_map.size()); 133 ASSERT_EQ(3U, field_type_map.size());
156 134
157 EXPECT_EQ(ADDRESS_HOME_LINE1, 135 EXPECT_EQ(ADDRESS_HOME_LINE1,
158 field_type_map.find(ASCIIToUTF16("Address line1"))->second); 136 field_type_map.find(ASCIIToUTF16("Address line1"))->second);
159 EXPECT_EQ(ADDRESS_HOME_LINE2, 137 EXPECT_EQ(ADDRESS_HOME_LINE2,
160 field_type_map.find(ASCIIToUTF16("Address line2"))->second); 138 field_type_map.find(ASCIIToUTF16("Address line2"))->second);
161 } 139 }
162 140
163 } // namespace autofill 141 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698