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