Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 FieldIsSuggestionSubstringStartingOnTokenBoundary(). |
| 18 TEST(AutofillUtilTest, FieldIsSuggestionSubstringStartingOnTokenBoundary) { | 18 struct FieldIsTokenBoundarySubstringCase { |
| 19 FieldIsTokenBoundarySubstringCase(const char* field_suggestion, | |
| 20 const char* field_contents, | |
| 21 bool case_sensitive, | |
| 22 bool expected_result) | |
| 23 : field_suggestion(field_suggestion), | |
| 24 field_contents(field_contents), | |
| 25 case_sensitive(case_sensitive), | |
| 26 expected_result(expected_result) {} | |
| 27 ~FieldIsTokenBoundarySubstringCase() {} | |
| 28 | |
| 29 const char* const field_suggestion; | |
| 30 const char* const field_contents; | |
| 31 const bool case_sensitive; | |
| 32 const bool expected_result; | |
| 33 }; | |
| 34 | |
| 35 class FieldIsTokenBoundarySubstringCaseTest | |
| 36 : public testing::TestWithParam<FieldIsTokenBoundarySubstringCase> {}; | |
| 37 | |
| 38 TEST_P(FieldIsTokenBoundarySubstringCaseTest, | |
| 39 FieldIsSuggestionSubstringStartingOnTokenBoundary) { | |
| 19 // FieldIsSuggestionSubstringStartingOnTokenBoundary should not work yet | 40 // FieldIsSuggestionSubstringStartingOnTokenBoundary should not work yet |
| 20 // without a flag. | 41 // without a flag. |
| 21 EXPECT_FALSE(FieldIsSuggestionSubstringStartingOnTokenBoundary( | 42 EXPECT_FALSE(FieldIsSuggestionSubstringStartingOnTokenBoundary( |
| 22 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false)); | 43 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false)); |
| 23 | 44 |
| 24 // Token matching is currently behind a flag. | 45 // Token matching is currently behind a flag. |
| 25 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 46 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 26 switches::kEnableSuggestionsWithSubstringMatch); | 47 switches::kEnableSuggestionsWithSubstringMatch); |
| 27 | 48 |
| 28 const struct { | 49 auto test_case = GetParam(); |
| 29 const char* const field_suggestion; | 50 SCOPED_TRACE(testing::Message() |
| 30 const char* const field_contents; | 51 << "suggestion = " << test_case.field_suggestion |
| 31 bool case_sensitive; | 52 << ", contents = " << test_case.field_contents |
| 32 bool expected_result; | 53 << ", case_sensitive = " << test_case.case_sensitive); |
| 33 } kTestCases[] = { | |
| 34 {"ab@cd.b", "a", false, true}, | |
| 35 {"ab@cd.b", "b", false, true}, | |
| 36 {"ab@cd.b", "Ab", false, true}, | |
| 37 {"ab@cd.b", "Ab", true, false}, | |
| 38 {"ab@cd.b", "cd", true, true}, | |
| 39 {"ab@cd.b", "d", false, false}, | |
| 40 {"ab@cd.b", "b@", true, false}, | |
| 41 {"ab@cd.b", "ab", false, true}, | |
| 42 {"ab@cd.b", "cd.b", true, true}, | |
| 43 {"ab@cd.b", "b@cd", false, false}, | |
| 44 {"ab@cd.b", "ab@c", false, true}, | |
| 45 {"ba.a.ab", "a.a", false, true}, | |
| 46 {"", "ab", false, false}, | |
| 47 {"", "ab", true, false}, | |
| 48 {"ab", "", false, true}, | |
| 49 {"ab", "", true, true}, | |
| 50 }; | |
| 51 | 54 |
| 52 for (const auto& test_case : kTestCases) { | 55 EXPECT_EQ(test_case.expected_result, |
| 53 SCOPED_TRACE(testing::Message() | 56 FieldIsSuggestionSubstringStartingOnTokenBoundary( |
| 54 << "suggestion = " << test_case.field_suggestion | 57 base::ASCIIToUTF16(test_case.field_suggestion), |
| 55 << ", contents = " << test_case.field_contents | 58 base::ASCIIToUTF16(test_case.field_contents), |
| 56 << ", case_sensitive = " << test_case.case_sensitive); | 59 test_case.case_sensitive)); |
| 57 | |
| 58 EXPECT_EQ(test_case.expected_result, | |
| 59 FieldIsSuggestionSubstringStartingOnTokenBoundary( | |
| 60 base::ASCIIToUTF16(test_case.field_suggestion), | |
| 61 base::ASCIIToUTF16(test_case.field_contents), | |
| 62 test_case.case_sensitive)); | |
| 63 } | |
| 64 } | 60 } |
| 65 | 61 |
| 62 INSTANTIATE_TEST_CASE_P( | |
| 63 AutofillUtil, | |
|
sebsg
2017/03/13 14:56:11
AutofillUtilTest here and in the rest of the file.
wuandy
2017/03/14 19:05:36
Done.
| |
| 64 FieldIsTokenBoundarySubstringCaseTest, | |
| 65 testing::Values( | |
| 66 FieldIsTokenBoundarySubstringCase("ab@cd.b", "a", false, true), | |
| 67 FieldIsTokenBoundarySubstringCase("ab@cd.b", "b", false, true), | |
| 68 FieldIsTokenBoundarySubstringCase("ab@cd.b", "Ab", false, true), | |
| 69 FieldIsTokenBoundarySubstringCase("ab@cd.b", "Ab", true, false), | |
| 70 FieldIsTokenBoundarySubstringCase("ab@cd.b", "cd", true, true), | |
| 71 FieldIsTokenBoundarySubstringCase("ab@cd.b", "d", false, false), | |
| 72 FieldIsTokenBoundarySubstringCase("ab@cd.b", "b@", true, false), | |
| 73 FieldIsTokenBoundarySubstringCase("ab@cd.b", "ab", false, true), | |
| 74 FieldIsTokenBoundarySubstringCase("ab@cd.b", "cd.b", true, true), | |
| 75 FieldIsTokenBoundarySubstringCase("ab@cd.b", "b@cd", false, false), | |
| 76 FieldIsTokenBoundarySubstringCase("ab@cd.b", "ab@c", false, true), | |
| 77 FieldIsTokenBoundarySubstringCase("ba.a.ab", "a.a", false, true), | |
| 78 FieldIsTokenBoundarySubstringCase("", "ab", false, false), | |
| 79 FieldIsTokenBoundarySubstringCase("", "ab", true, false), | |
| 80 FieldIsTokenBoundarySubstringCase("ab", "", false, true), | |
| 81 FieldIsTokenBoundarySubstringCase("ab", "", true, true))); | |
| 82 | |
| 66 // Tests for GetTextSelectionStart(). | 83 // Tests for GetTextSelectionStart(). |
| 67 TEST(AutofillUtilTest, GetTextSelectionStart) { | 84 struct GetTextSelectionStartCase { |
| 68 const size_t kInvalid = base::string16::npos; | 85 GetTextSelectionStartCase(const char* field_suggestion, |
| 69 const struct { | 86 const char* field_contents, |
| 70 const char* const field_suggestion; | 87 bool case_sensitive, |
| 71 const char* const field_contents; | 88 size_t expected_start) |
| 72 bool case_sensitive; | 89 : field_suggestion(field_suggestion), |
| 73 size_t expected_start; | 90 field_contents(field_contents), |
| 74 } kTestCases[] = { | 91 case_sensitive(case_sensitive), |
| 75 {"ab@cd.b", "a", false, 1}, | 92 expected_start(expected_start) {} |
| 76 {"ab@cd.b", "A", true, kInvalid}, | 93 ~GetTextSelectionStartCase() {} |
| 77 {"ab@cd.b", "Ab", false, 2}, | |
| 78 {"ab@cd.b", "Ab", true, kInvalid}, | |
| 79 {"ab@cd.b", "cd", false, 5}, | |
| 80 {"ab@cd.b", "ab@c", false, 4}, | |
| 81 {"ab@cd.b", "cd.b", false, 7}, | |
| 82 {"ab@cd.b", "b@cd", false, kInvalid}, | |
| 83 {"ab@cd.b", "b", false, 7}, | |
| 84 {"ba.a.ab", "a.a", false, 6}, | |
| 85 {"texample@example.com", "example", false, 16}, | |
| 86 }; | |
| 87 | 94 |
| 88 for (const auto& test_case : kTestCases) { | 95 const char* const field_suggestion; |
| 89 SCOPED_TRACE(testing::Message() | 96 const char* const field_contents; |
| 90 << "suggestion = " << test_case.field_suggestion | 97 const bool case_sensitive; |
| 91 << ", contents = " << test_case.field_contents | 98 const size_t expected_start; |
| 92 << ", case_sensitive = " << test_case.case_sensitive); | 99 }; |
| 93 | 100 |
| 94 EXPECT_EQ( | 101 class GetTextSelectionStartTest |
| 95 test_case.expected_start, | 102 : public testing::TestWithParam<GetTextSelectionStartCase> {}; |
| 96 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion), | 103 |
| 97 base::ASCIIToUTF16(test_case.field_contents), | 104 TEST_P(GetTextSelectionStartTest, GetTextSelectionStart) { |
| 98 test_case.case_sensitive)); | 105 auto test_case = GetParam(); |
| 99 } | 106 SCOPED_TRACE(testing::Message() |
| 107 << "suggestion = " << test_case.field_suggestion | |
| 108 << ", contents = " << test_case.field_contents | |
| 109 << ", case_sensitive = " << test_case.case_sensitive); | |
| 110 EXPECT_EQ( | |
| 111 test_case.expected_start, | |
| 112 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion), | |
| 113 base::ASCIIToUTF16(test_case.field_contents), | |
| 114 test_case.case_sensitive)); | |
| 100 } | 115 } |
| 101 | 116 |
| 117 INSTANTIATE_TEST_CASE_P( | |
| 118 AutofillUtil, | |
| 119 GetTextSelectionStartTest, | |
| 120 testing::Values( | |
| 121 GetTextSelectionStartCase("ab@cd.b", "a", false, 1), | |
| 122 GetTextSelectionStartCase("ab@cd.b", "A", true, base::string16::npos), | |
| 123 GetTextSelectionStartCase("ab@cd.b", "Ab", false, 2), | |
| 124 GetTextSelectionStartCase("ab@cd.b", "Ab", true, base::string16::npos), | |
| 125 GetTextSelectionStartCase("ab@cd.b", "cd", false, 5), | |
| 126 GetTextSelectionStartCase("ab@cd.b", "ab@c", false, 4), | |
| 127 GetTextSelectionStartCase("ab@cd.b", "cd.b", false, 7), | |
| 128 GetTextSelectionStartCase("ab@cd.b", | |
| 129 "b@cd", | |
| 130 false, | |
| 131 base::string16::npos), | |
| 132 GetTextSelectionStartCase("ab@cd.b", "b", false, 7), | |
| 133 GetTextSelectionStartCase("ba.a.ab", "a.a", false, 6), | |
| 134 GetTextSelectionStartCase("texample@example.com", | |
| 135 "example", | |
| 136 false, | |
| 137 16))); | |
| 138 | |
| 102 // Tests for LowercaseAndTokenizeAttributeString | 139 // Tests for LowercaseAndTokenizeAttributeString |
| 103 TEST(AutofillUtilTest, LowercaseAndTokenizeAttributeString) { | 140 struct LowercaseAndTokenizeAttributeStringCase { |
| 104 const struct { | 141 LowercaseAndTokenizeAttributeStringCase(const char* const attribute, |
| 105 const char* const attribute; | 142 std::vector<std::string> tokens) |
| 106 std::vector<std::string> tokens; | 143 : attribute(attribute), tokens(tokens) {} |
| 107 } kTestCases[] = { | 144 ~LowercaseAndTokenizeAttributeStringCase() {} |
| 108 // Test leading and trailing whitespace, test tabs and newlines | |
| 109 {"foo bar baz", {"foo", "bar", "baz"}}, | |
| 110 {" foo bar baz ", {"foo", "bar", "baz"}}, | |
| 111 {"foo\tbar baz ", {"foo", "bar", "baz"}}, | |
| 112 {"foo\nbar baz ", {"foo", "bar", "baz"}}, | |
| 113 | 145 |
| 114 // Test different forms of capitalization | 146 const char* const attribute; |
| 115 {"FOO BAR BAZ", {"foo", "bar", "baz"}}, | 147 std::vector<std::string> tokens; |
| 116 {"foO baR bAz", {"foo", "bar", "baz"}}, | 148 }; |
| 117 | 149 |
| 118 // Test collapsing of multiple whitespace characters in a row | 150 class LowercaseAndTokenizeAttributeStringTest |
| 119 {" \t\t\n\n ", std::vector<std::string>()}, | 151 : public testing::TestWithParam<LowercaseAndTokenizeAttributeStringCase> {}; |
| 120 {"foO baR bAz", {"foo", "bar", "baz"}}, | |
| 121 }; | |
| 122 | 152 |
| 123 for (const auto& test_case : kTestCases) { | 153 TEST_P(LowercaseAndTokenizeAttributeStringTest, |
| 124 SCOPED_TRACE(testing::Message() << "attribute = " << test_case.attribute); | 154 LowercaseAndTokenizeAttributeStringTest) { |
| 155 auto test_case = GetParam(); | |
| 156 SCOPED_TRACE(testing::Message() << "attribute = " << test_case.attribute); | |
| 125 | 157 |
| 126 EXPECT_EQ(test_case.tokens, | 158 EXPECT_EQ(test_case.tokens, |
| 127 LowercaseAndTokenizeAttributeString(test_case.attribute)); | 159 LowercaseAndTokenizeAttributeString(test_case.attribute)); |
| 128 } | |
| 129 } | 160 } |
| 161 | |
| 162 INSTANTIATE_TEST_CASE_P( | |
| 163 AutofillUtil, | |
| 164 LowercaseAndTokenizeAttributeStringTest, | |
| 165 testing::Values( | |
| 166 // Test leading and trailing whitespace, test tabs and newlines | |
| 167 LowercaseAndTokenizeAttributeStringCase("foo bar baz", | |
| 168 {"foo", "bar", "baz"}), | |
| 169 LowercaseAndTokenizeAttributeStringCase(" foo bar baz ", | |
| 170 {"foo", "bar", "baz"}), | |
| 171 LowercaseAndTokenizeAttributeStringCase("foo\tbar baz ", | |
| 172 {"foo", "bar", "baz"}), | |
| 173 LowercaseAndTokenizeAttributeStringCase("foo\nbar baz ", | |
| 174 {"foo", "bar", "baz"}), | |
| 175 | |
| 176 // Test different forms of capitalization | |
| 177 LowercaseAndTokenizeAttributeStringCase("FOO BAR BAZ", | |
| 178 {"foo", "bar", "baz"}), | |
| 179 LowercaseAndTokenizeAttributeStringCase("foO baR bAz", | |
| 180 {"foo", "bar", "baz"}), | |
| 181 | |
| 182 // Test collapsing of multiple whitespace characters in a row | |
| 183 LowercaseAndTokenizeAttributeStringCase(" \t\t\n\n ", | |
| 184 std::vector<std::string>()), | |
| 185 LowercaseAndTokenizeAttributeStringCase("foO baR bAz", | |
| 186 {"foo", "bar", "baz"}))); | |
| 187 | |
| 130 } // namespace autofill | 188 } // namespace autofill |
| OLD | NEW |