summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/form_field_unittest.cc
blob: ef68eb8c202312f4def60f88810617cacdbfabc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/form_field.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

TEST(FormFieldTest, Match) {
  AutofillField field;

  // Empty strings match.
  EXPECT_TRUE(FormField::Match(&field, string16(), true));

  // Empty pattern matches non-empty string.
  field.set_label(ASCIIToUTF16("a"));
  EXPECT_TRUE(FormField::Match(&field, string16(), true));

  // Strictly empty pattern matches empty string.
  field.set_label(ASCIIToUTF16(""));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"), true));

  // Strictly empty pattern does not match non-empty string.
  field.set_label(ASCIIToUTF16("a"));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"), true));

  // Non-empty pattern doesn't match empty string.
  field.set_label(string16());
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"), true));

  // Beginning of line.
  field.set_label(ASCIIToUTF16("head_tail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"), true));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"), true));

  // End of line.
  field.set_label(ASCIIToUTF16("head_tail"));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"), true));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"), true));

  // Exact.
  field.set_label(ASCIIToUTF16("head_tail"));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"), true));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"), true));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"), true));

  // Escaped dots.
  field.set_label(ASCIIToUTF16("m.i."));
  // Note: This pattern is misleading as the "." characters are wild cards.
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), true));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), true));
  field.set_label(ASCIIToUTF16("mXiX"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), true));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."), true));

  // Repetition.
  field.set_label(ASCIIToUTF16("headtail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true));
  field.set_label(ASCIIToUTF16("headXtail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true));
  field.set_label(ASCIIToUTF16("headXXXtail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), true));
  field.set_label(ASCIIToUTF16("headtail"));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true));
  field.set_label(ASCIIToUTF16("headXtail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true));
  field.set_label(ASCIIToUTF16("headXXXtail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), true));

  // Alternation.
  field.set_label(ASCIIToUTF16("head_tail"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"), true));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"), true));
  EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"), true));

  // Case sensitivity.
  field.set_label(ASCIIToUTF16("xxxHeAd_tAiLxxx"));
  EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"), true));
}

}  // namespace