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/utf_string_conversions.h" 6 #include "chrome/browser/autofill/autofill_field.h" 7 #include "chrome/browser/autofill/form_field.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace { 11 12 TEST(FormFieldTest, Match) { 13 AutofillField field; 14 15 // Empty strings match. 16 EXPECT_TRUE(FormField::Match(&field, string16(), true)); 17 18 // Empty pattern matches non-empty string. 19 field.label = ASCIIToUTF16("a"); 20 EXPECT_TRUE(FormField::Match(&field, string16(), true)); 21 22 // Strictly empty pattern matches empty string. 23 field.label = ASCIIToUTF16(""); 24 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"), true)); 25 26 // Strictly empty pattern does not match non-empty string. 27 field.label = ASCIIToUTF16("a"); 28 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"), true)); 29 30 // Non-empty pattern doesn't match empty string. 31 field.label = string16(); 32 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"), true)); 33 34 // Beginning of line. 35 field.label = ASCIIToUTF16("head_tail"); 36 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"), true)); 37 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"), true)); 38 39 // End of line. 40 field.label = ASCIIToUTF16("head_tail"); 41 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"), true)); 42 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"), true)); 43 44 // Exact. 45 field.label = ASCIIToUTF16("head_tail"); 46 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"), true)); 47 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"), true)); 48 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"), true)); 49 50 // Escaped dots. 51 field.label = ASCIIToUTF16("m.i."); 52 // Note: This pattern is misleading as the "." characters are wild cards. 53 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), true)); 54 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), true)); 55 field.label = ASCIIToUTF16("mXiX"); 56 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), true)); 57 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), true)); 58 59 // Repetition. 60 field.label = ASCIIToUTF16("headtail"); 61 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true)); 62 field.label = ASCIIToUTF16("headXtail"); 63 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true)); 64 field.label = ASCIIToUTF16("headXXXtail"); 65 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true)); 66 field.label = ASCIIToUTF16("headtail"); 67 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true)); 68 field.label = ASCIIToUTF16("headXtail"); 69 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true)); 70 field.label = ASCIIToUTF16("headXXXtail"); 71 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true)); 72 73 // Alternation. 74 field.label = ASCIIToUTF16("head_tail"); 75 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"), true)); 76 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"), true)); 77 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"), true)); 78 79 // Case sensitivity. 80 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx"); 81 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"), true)); 82 } 83 84 } // namespace 85