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/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" 11 12 namespace autofill { 13 14 TEST(FormFieldTest, Match) { 15 AutofillField field; 16 17 // Empty strings match. 18 EXPECT_TRUE(FormField::Match(&field, base::string16(), 19 FormField::MATCH_LABEL)); 20 21 // Empty pattern matches non-empty string. 22 field.label = ASCIIToUTF16("a"); 23 EXPECT_TRUE(FormField::Match(&field, base::string16(), 24 FormField::MATCH_LABEL)); 25 26 // Strictly empty pattern matches empty string. 27 field.label = base::string16(); 28 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"), 29 FormField::MATCH_LABEL)); 30 31 // Strictly empty pattern does not match non-empty string. 32 field.label = ASCIIToUTF16("a"); 33 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"), 34 FormField::MATCH_LABEL)); 35 36 // Non-empty pattern doesn't match empty string. 37 field.label = base::string16(); 38 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"), 39 FormField::MATCH_LABEL)); 40 41 // Beginning of line. 42 field.label = ASCIIToUTF16("head_tail"); 43 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"), 44 FormField::MATCH_LABEL)); 45 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"), 46 FormField::MATCH_LABEL)); 47 48 // End of line. 49 field.label = ASCIIToUTF16("head_tail"); 50 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"), 51 FormField::MATCH_LABEL)); 52 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"), 53 FormField::MATCH_LABEL)); 54 55 // Exact. 56 field.label = ASCIIToUTF16("head_tail"); 57 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"), 58 FormField::MATCH_LABEL)); 59 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"), 60 FormField::MATCH_LABEL)); 61 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"), 62 FormField::MATCH_LABEL)); 63 64 // Escaped dots. 65 field.label = ASCIIToUTF16("m.i."); 66 // Note: This pattern is misleading as the "." characters are wild cards. 67 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 68 FormField::MATCH_LABEL)); 69 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), 70 FormField::MATCH_LABEL)); 71 field.label = ASCIIToUTF16("mXiX"); 72 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 73 FormField::MATCH_LABEL)); 74 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), 75 FormField::MATCH_LABEL)); 76 77 // Repetition. 78 field.label = ASCIIToUTF16("headtail"); 79 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 80 FormField::MATCH_LABEL)); 81 field.label = ASCIIToUTF16("headXtail"); 82 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 83 FormField::MATCH_LABEL)); 84 field.label = ASCIIToUTF16("headXXXtail"); 85 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 86 FormField::MATCH_LABEL)); 87 field.label = ASCIIToUTF16("headtail"); 88 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 89 FormField::MATCH_LABEL)); 90 field.label = ASCIIToUTF16("headXtail"); 91 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 92 FormField::MATCH_LABEL)); 93 field.label = ASCIIToUTF16("headXXXtail"); 94 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 95 FormField::MATCH_LABEL)); 96 97 // Alternation. 98 field.label = ASCIIToUTF16("head_tail"); 99 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"), 100 FormField::MATCH_LABEL)); 101 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"), 102 FormField::MATCH_LABEL)); 103 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"), 104 FormField::MATCH_LABEL)); 105 106 // Case sensitivity. 107 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx"); 108 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"), 109 FormField::MATCH_LABEL)); 110 111 // Word boundaries. 112 field.label = ASCIIToUTF16("contains word:"); 113 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("\\bword\\b"), 114 FormField::MATCH_LABEL)); 115 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcon\\b"), 116 FormField::MATCH_LABEL)); 117 // Make sure the circumflex in 'crepe' is not treated as a word boundary. 118 field.label = UTF8ToUTF16("cr" "\xC3\xAA" "pe"); 119 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcr\\b"), 120 FormField::MATCH_LABEL)); 121 } 122 123 // Test that we ignore checkable elements. 124 TEST(FormFieldTest, ParseFormFields) { 125 ScopedVector<AutofillField> fields; 126 FormFieldData field_data; 127 field_data.form_control_type = "text"; 128 129 field_data.label = ASCIIToUTF16("Address line1"); 130 fields.push_back(new AutofillField(field_data, field_data.label)); 131 132 field_data.is_checkable = true; 133 field_data.label = ASCIIToUTF16("Is PO Box"); 134 fields.push_back(new AutofillField(field_data, field_data.label)); 135 136 // reset is_checkable to false. 137 field_data.is_checkable = false; 138 field_data.label = ASCIIToUTF16("Address line2"); 139 fields.push_back(new AutofillField(field_data, field_data.label)); 140 141 ServerFieldTypeMap field_type_map; 142 FormField::ParseFormFields(fields.get(), &field_type_map); 143 // Checkable element shouldn't interfere with inference of Address line2. 144 EXPECT_EQ(2U, field_type_map.size()); 145 146 EXPECT_EQ(ADDRESS_HOME_LINE1, 147 field_type_map.find(ASCIIToUTF16("Address line1"))->second); 148 EXPECT_EQ(ADDRESS_HOME_LINE2, 149 field_type_map.find(ASCIIToUTF16("Address line2"))->second); 150 } 151 152 } // namespace autofill 153