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

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

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

Powered by Google App Engine
This is Rietveld 408576698