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

Side by Side Diff: components/autofill/core/common/autofill_util_unittest.cc

Issue 2906383003: Teach PasswordAutofillAgent sometimes match prefixes of usernames (Closed)
Patch Set: . Created 3 years, 6 months 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/common/autofill_util.h" 5 #include "components/autofill/core/common/autofill_util.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/common/autofill_switches.h" 12 #include "components/autofill/core/common/autofill_switches.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace autofill { 15 namespace autofill {
16 16
17 // Tests for FieldIsSuggestionSubstringStartingOnTokenBoundary(). 17 // Tests for IsSubstringStartingOnTokenBoundary().
18 struct FieldIsTokenBoundarySubstringCase { 18 struct TokenBoundaryMatchersCase {
19 const char* const field_suggestion; 19 const char* const field_suggestion;
20 const char* const field_contents; 20 const char* const field_contents;
21 const bool case_sensitive; 21 const bool case_sensitive;
22 const bool expected_result; 22 const bool expected_result;
23 }; 23 };
24 24
25 class FieldIsTokenBoundarySubstringCaseTest 25 class SubstringStartingOnTokenBoundaryTest
26 : public testing::TestWithParam<FieldIsTokenBoundarySubstringCase> {}; 26 : public testing::TestWithParam<TokenBoundaryMatchersCase> {};
27 27
28 TEST_P(FieldIsTokenBoundarySubstringCaseTest, 28 TEST_P(SubstringStartingOnTokenBoundaryTest,
29 FieldIsSuggestionSubstringStartingOnTokenBoundary) { 29 IsSubstringStartingOnTokenBoundary) {
30 // FieldIsSuggestionSubstringStartingOnTokenBoundary should not work yet 30 // IsSubstringStartingOnTokenBoundary should not work yet
31 // without a flag. 31 // without a flag.
32 EXPECT_FALSE(FieldIsSuggestionSubstringStartingOnTokenBoundary( 32 EXPECT_FALSE(IsSubstringStartingOnTokenBoundary(
33 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false)); 33 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false));
34 34
35 // Token matching is currently behind a flag. 35 // Token matching is currently behind a flag.
36 base::CommandLine::ForCurrentProcess()->AppendSwitch( 36 base::CommandLine::ForCurrentProcess()->AppendSwitch(
37 switches::kEnableSuggestionsWithSubstringMatch); 37 switches::kEnableSuggestionsWithSubstringMatch);
38 38
39 auto test_case = GetParam(); 39 auto test_case = GetParam();
40 SCOPED_TRACE(testing::Message() 40 SCOPED_TRACE(testing::Message()
41 << "suggestion = " << test_case.field_suggestion 41 << "suggestion = " << test_case.field_suggestion
42 << ", contents = " << test_case.field_contents 42 << ", contents = " << test_case.field_contents
43 << ", case_sensitive = " << test_case.case_sensitive); 43 << ", case_sensitive = " << test_case.case_sensitive);
44 44
45 EXPECT_EQ(test_case.expected_result, 45 EXPECT_EQ(test_case.expected_result,
46 FieldIsSuggestionSubstringStartingOnTokenBoundary( 46 IsSubstringStartingOnTokenBoundary(
47 base::ASCIIToUTF16(test_case.field_suggestion), 47 base::ASCIIToUTF16(test_case.field_suggestion),
48 base::ASCIIToUTF16(test_case.field_contents), 48 base::ASCIIToUTF16(test_case.field_contents),
49 test_case.case_sensitive)); 49 test_case.case_sensitive));
50 } 50 }
51 51
52 INSTANTIATE_TEST_CASE_P( 52 INSTANTIATE_TEST_CASE_P(
53 AutofillUtilTest, 53 AutofillUtilTest,
54 FieldIsTokenBoundarySubstringCaseTest, 54 SubstringStartingOnTokenBoundaryTest,
55 testing::Values( 55 testing::Values(TokenBoundaryMatchersCase{"ab@cd.b", "a", false, true},
56 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "a", false, true}, 56 TokenBoundaryMatchersCase{"ab@cd.b", "b", false, true},
57 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b", false, true}, 57 TokenBoundaryMatchersCase{"ab@cd.b", "Ab", false, true},
58 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "Ab", false, true}, 58 TokenBoundaryMatchersCase{"ab@cd.b", "Ab", true, false},
59 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "Ab", true, false}, 59 TokenBoundaryMatchersCase{"ab@cd.b", "cd", true, true},
60 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "cd", true, true}, 60 TokenBoundaryMatchersCase{"ab@cd.b", "d", false, false},
61 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "d", false, false}, 61 TokenBoundaryMatchersCase{"ab@cd.b", "b@", true, false},
62 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b@", true, false}, 62 TokenBoundaryMatchersCase{"ab@cd.b", "ab", false, true},
63 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "ab", false, true}, 63 TokenBoundaryMatchersCase{"ab@cd.b", "cd.b", true, true},
64 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "cd.b", true, true}, 64 TokenBoundaryMatchersCase{"ab@cd.b", "b@cd", false, false},
65 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b@cd", false, false}, 65 TokenBoundaryMatchersCase{"ab@cd.b", "ab@c", false, true},
66 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "ab@c", false, true}, 66 TokenBoundaryMatchersCase{"ba.a.ab", "a.a", false, true},
67 FieldIsTokenBoundarySubstringCase{"ba.a.ab", "a.a", false, true}, 67 TokenBoundaryMatchersCase{"", "ab", false, false},
68 FieldIsTokenBoundarySubstringCase{"", "ab", false, false}, 68 TokenBoundaryMatchersCase{"", "ab", true, false},
69 FieldIsTokenBoundarySubstringCase{"", "ab", true, false}, 69 TokenBoundaryMatchersCase{"ab", "", false, true},
70 FieldIsTokenBoundarySubstringCase{"ab", "", false, true}, 70 TokenBoundaryMatchersCase{"ab", "", true, true}));
71 FieldIsTokenBoundarySubstringCase{"ab", "", true, true})); 71
72 class PrefixStartingOnTokenBoundaryTest
73 : public testing::TestWithParam<TokenBoundaryMatchersCase> {};
74
75 TEST_P(PrefixStartingOnTokenBoundaryTest, IsPrefixStartingOnTokenBoundary) {
76 auto test_case = GetParam();
77 SCOPED_TRACE(testing::Message()
78 << "suggestion = " << test_case.field_suggestion
79 << ", contents = " << test_case.field_contents
80 << ", case_sensitive = " << test_case.case_sensitive);
81
82 EXPECT_EQ(test_case.expected_result,
83 IsPrefixStartingOnTokenBoundary(
84 base::ASCIIToUTF16(test_case.field_suggestion),
85 base::ASCIIToUTF16(test_case.field_contents),
86 test_case.case_sensitive));
87 }
88
89 INSTANTIATE_TEST_CASE_P(
90 AutofillUtilTest,
91 PrefixStartingOnTokenBoundaryTest,
92 testing::Values(TokenBoundaryMatchersCase{"ab@cd.b", "a", false, false},
93 TokenBoundaryMatchersCase{"ab@cd.b", "b", false, false},
94 TokenBoundaryMatchersCase{"ab@cd.b", "Ab", false, true},
95 TokenBoundaryMatchersCase{"ab@cd.b", "Ab", true, false},
96 TokenBoundaryMatchersCase{"ab@cd.b", "cd", true, false},
97 TokenBoundaryMatchersCase{"ab@cd.b", "d", false, false},
98 TokenBoundaryMatchersCase{"ab@cd.b", "b@", true, false},
99 TokenBoundaryMatchersCase{"ab@cd.b", "ab", false, true},
100 TokenBoundaryMatchersCase{"ab@cd.b", "cd.b", true, false},
101 TokenBoundaryMatchersCase{"ab@cd.b", "b@cd", false, false},
102 TokenBoundaryMatchersCase{"ab@cd.b", "ab@c", false, false},
103 TokenBoundaryMatchersCase{"ba.a.ab", "a.a", false, false},
104 TokenBoundaryMatchersCase{"", "ab", false, false},
105 TokenBoundaryMatchersCase{"", "ab", true, false},
106 TokenBoundaryMatchersCase{"ab", "", false, false},
107 TokenBoundaryMatchersCase{"ab", "", true, false}));
72 108
73 // Tests for GetTextSelectionStart(). 109 // Tests for GetTextSelectionStart().
74 struct GetTextSelectionStartCase { 110 struct GetTextSelectionStartCase {
75 const char* const field_suggestion; 111 const char* const field_suggestion;
76 const char* const field_contents; 112 const char* const field_contents;
77 const bool case_sensitive; 113 const bool case_sensitive;
78 const size_t expected_start; 114 const size_t expected_start;
79 }; 115 };
80 116
81 class GetTextSelectionStartTest 117 class GetTextSelectionStartTest
82 : public testing::TestWithParam<GetTextSelectionStartCase> {}; 118 : public testing::TestWithParam<GetTextSelectionStartCase> {};
83 119
84 TEST_P(GetTextSelectionStartTest, GetTextSelectionStart) { 120 TEST_P(GetTextSelectionStartTest, GetTextSelectionStart) {
85 auto test_case = GetParam(); 121 auto test_case = GetParam();
86 SCOPED_TRACE(testing::Message() 122 SCOPED_TRACE(testing::Message()
87 << "suggestion = " << test_case.field_suggestion 123 << "full string = " << test_case.field_suggestion
88 << ", contents = " << test_case.field_contents 124 << ", prefix = " << test_case.field_contents
89 << ", case_sensitive = " << test_case.case_sensitive); 125 << ", case_sensitive = " << test_case.case_sensitive);
90 EXPECT_EQ( 126 EXPECT_EQ(
91 test_case.expected_start, 127 test_case.expected_start,
92 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion), 128 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion),
93 base::ASCIIToUTF16(test_case.field_contents), 129 base::ASCIIToUTF16(test_case.field_contents),
94 test_case.case_sensitive)); 130 test_case.case_sensitive));
95 } 131 }
96 132
97 INSTANTIATE_TEST_CASE_P( 133 INSTANTIATE_TEST_CASE_P(
98 AutofillUtilTest, 134 AutofillUtilTest,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", 186 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz",
151 {"foo", "bar", "baz"}}, 187 {"foo", "bar", "baz"}},
152 188
153 // Test collapsing of multiple whitespace characters in a row 189 // Test collapsing of multiple whitespace characters in a row
154 LowercaseAndTokenizeAttributeStringCase{" \t\t\n\n ", 190 LowercaseAndTokenizeAttributeStringCase{" \t\t\n\n ",
155 std::vector<std::string>()}, 191 std::vector<std::string>()},
156 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", 192 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz",
157 {"foo", "bar", "baz"}})); 193 {"foo", "bar", "baz"}}));
158 194
159 } // namespace autofill 195 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698