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 IsSubstringStartingOnTokenBoundary(). |
| 18 struct FieldIsTokenBoundarySubstringCase { | 18 struct TokenBoundarySubstringCase { |
| 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 struct TokenBoundaryPrefixCase { |
|
vabr (Chromium)
2017/06/12 18:56:59
nit: Please move this just before its first use.
melandory
2017/06/19 10:53:48
Done.
| |
| 26 : public testing::TestWithParam<FieldIsTokenBoundarySubstringCase> {}; | 26 const char* const field_suggestion; |
| 27 const char* const field_contents; | |
| 28 const bool expected_result; | |
| 29 }; | |
| 27 | 30 |
| 28 TEST_P(FieldIsTokenBoundarySubstringCaseTest, | 31 class SubstringStartingOnTokenBoundaryTest |
|
vabr (Chromium)
2017/06/12 18:56:59
nit: Similarly to the name of FieldIsSuggestionSub
melandory
2017/06/19 10:53:48
Done.
| |
| 29 FieldIsSuggestionSubstringStartingOnTokenBoundary) { | 32 : public testing::TestWithParam<TokenBoundarySubstringCase> {}; |
| 30 // FieldIsSuggestionSubstringStartingOnTokenBoundary should not work yet | 33 |
| 34 TEST_P(SubstringStartingOnTokenBoundaryTest, | |
| 35 IsSubstringStartingOnTokenBoundary) { | |
| 36 // IsSubstringStartingOnTokenBoundary should not work yet | |
| 31 // without a flag. | 37 // without a flag. |
| 32 EXPECT_FALSE(FieldIsSuggestionSubstringStartingOnTokenBoundary( | 38 EXPECT_FALSE(IsSubstringStartingOnTokenBoundary( |
| 33 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false)); | 39 base::ASCIIToUTF16("ab@cd.b"), base::ASCIIToUTF16("b"), false)); |
| 34 | 40 |
| 35 // Token matching is currently behind a flag. | 41 // Token matching is currently behind a flag. |
| 36 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 42 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 37 switches::kEnableSuggestionsWithSubstringMatch); | 43 switches::kEnableSuggestionsWithSubstringMatch); |
| 38 | 44 |
| 39 auto test_case = GetParam(); | 45 auto test_case = GetParam(); |
| 40 SCOPED_TRACE(testing::Message() | 46 SCOPED_TRACE(testing::Message() |
| 41 << "suggestion = " << test_case.field_suggestion | 47 << "suggestion = " << test_case.field_suggestion |
| 42 << ", contents = " << test_case.field_contents | 48 << ", contents = " << test_case.field_contents |
| 43 << ", case_sensitive = " << test_case.case_sensitive); | 49 << ", case_sensitive = " << test_case.case_sensitive); |
| 44 | 50 |
| 45 EXPECT_EQ(test_case.expected_result, | 51 EXPECT_EQ(test_case.expected_result, |
| 46 FieldIsSuggestionSubstringStartingOnTokenBoundary( | 52 IsSubstringStartingOnTokenBoundary( |
| 47 base::ASCIIToUTF16(test_case.field_suggestion), | 53 base::ASCIIToUTF16(test_case.field_suggestion), |
| 48 base::ASCIIToUTF16(test_case.field_contents), | 54 base::ASCIIToUTF16(test_case.field_contents), |
| 49 test_case.case_sensitive)); | 55 test_case.case_sensitive)); |
| 50 } | 56 } |
| 51 | 57 |
| 52 INSTANTIATE_TEST_CASE_P( | 58 INSTANTIATE_TEST_CASE_P( |
| 53 AutofillUtilTest, | 59 AutofillUtilTest, |
| 54 FieldIsTokenBoundarySubstringCaseTest, | 60 SubstringStartingOnTokenBoundaryTest, |
| 55 testing::Values( | 61 testing::Values(TokenBoundarySubstringCase{"ab@cd.b", "a", false, true}, |
| 56 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "a", false, true}, | 62 TokenBoundarySubstringCase{"ab@cd.b", "b", false, true}, |
| 57 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b", false, true}, | 63 TokenBoundarySubstringCase{"ab@cd.b", "Ab", false, true}, |
| 58 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "Ab", false, true}, | 64 TokenBoundarySubstringCase{"ab@cd.b", "Ab", true, false}, |
| 59 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "Ab", true, false}, | 65 TokenBoundarySubstringCase{"ab@cd.b", "cd", true, true}, |
| 60 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "cd", true, true}, | 66 TokenBoundarySubstringCase{"ab@cd.b", "d", false, false}, |
| 61 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "d", false, false}, | 67 TokenBoundarySubstringCase{"ab@cd.b", "b@", true, false}, |
| 62 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b@", true, false}, | 68 TokenBoundarySubstringCase{"ab@cd.b", "ab", false, true}, |
| 63 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "ab", false, true}, | 69 TokenBoundarySubstringCase{"ab@cd.b", "cd.b", true, true}, |
| 64 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "cd.b", true, true}, | 70 TokenBoundarySubstringCase{"ab@cd.b", "b@cd", false, false}, |
| 65 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "b@cd", false, false}, | 71 TokenBoundarySubstringCase{"ab@cd.b", "ab@c", false, true}, |
| 66 FieldIsTokenBoundarySubstringCase{"ab@cd.b", "ab@c", false, true}, | 72 TokenBoundarySubstringCase{"ba.a.ab", "a.a", false, true}, |
| 67 FieldIsTokenBoundarySubstringCase{"ba.a.ab", "a.a", false, true}, | 73 TokenBoundarySubstringCase{"", "ab", false, false}, |
| 68 FieldIsTokenBoundarySubstringCase{"", "ab", false, false}, | 74 TokenBoundarySubstringCase{"", "ab", true, false}, |
| 69 FieldIsTokenBoundarySubstringCase{"", "ab", true, false}, | 75 TokenBoundarySubstringCase{"ab", "", false, true}, |
| 70 FieldIsTokenBoundarySubstringCase{"ab", "", false, true}, | 76 TokenBoundarySubstringCase{"ab", "", true, true})); |
| 71 FieldIsTokenBoundarySubstringCase{"ab", "", true, true})); | 77 |
| 78 class PrefixEndingOnTokenBoundaryTest | |
| 79 : public testing::TestWithParam<TokenBoundaryPrefixCase> {}; | |
| 80 | |
| 81 TEST_P(PrefixEndingOnTokenBoundaryTest, IsPrefixEndingOnTokenBoundary) { | |
| 82 auto test_case = GetParam(); | |
| 83 SCOPED_TRACE(testing::Message() | |
| 84 << "suggestion = " << test_case.field_suggestion | |
| 85 << ", contents = " << test_case.field_contents); | |
| 86 | |
| 87 EXPECT_EQ(test_case.expected_result, | |
| 88 IsPrefixEndingOnTokenBoundary( | |
| 89 base::ASCIIToUTF16(test_case.field_suggestion), | |
| 90 base::ASCIIToUTF16(test_case.field_contents))); | |
| 91 } | |
| 92 | |
| 93 INSTANTIATE_TEST_CASE_P( | |
| 94 AutofillUtilTest, | |
| 95 PrefixEndingOnTokenBoundaryTest, | |
| 96 testing::Values(TokenBoundaryPrefixCase{"ab@cd.b", "a", false}, | |
| 97 TokenBoundaryPrefixCase{"ab@cd.b", "b", false}, | |
| 98 TokenBoundaryPrefixCase{"ab@cd.b", "Ab", false}, | |
| 99 TokenBoundaryPrefixCase{"ab@cd.b", "cd", false}, | |
| 100 TokenBoundaryPrefixCase{"ab@cd.b", "d", false}, | |
| 101 TokenBoundaryPrefixCase{"ab@cd.b", "b@", false}, | |
| 102 TokenBoundaryPrefixCase{"ab@cd.b", "ab", true}, | |
| 103 TokenBoundaryPrefixCase{"ab@cd.b", "cd.b", false}, | |
| 104 TokenBoundaryPrefixCase{"ab@cd.b", "b@cd", false}, | |
| 105 TokenBoundaryPrefixCase{"ab@cd.b", "ab@c", false}, | |
| 106 TokenBoundaryPrefixCase{"ba.a.ab", "a.a", false}, | |
| 107 TokenBoundaryPrefixCase{"", "ab", false}, | |
| 108 TokenBoundaryPrefixCase{"", "ab", false}, | |
|
vabr (Chromium)
2017/06/12 18:56:59
Let's not duplicate the test cases :).
(Also below
melandory
2017/06/19 10:53:48
Done.
| |
| 109 TokenBoundaryPrefixCase{"ab", "", false}, | |
| 110 TokenBoundaryPrefixCase{"ab", "", false})); | |
|
vabr (Chromium)
2017/06/12 18:56:59
More cases to add:
ab@c, ab@, false
ab@cd@g, ab, f
melandory
2017/06/19 10:53:48
Done.
| |
| 72 | 111 |
| 73 // Tests for GetTextSelectionStart(). | 112 // Tests for GetTextSelectionStart(). |
| 74 struct GetTextSelectionStartCase { | 113 struct GetTextSelectionStartCase { |
| 75 const char* const field_suggestion; | 114 const char* const field_suggestion; |
| 76 const char* const field_contents; | 115 const char* const field_contents; |
| 77 const bool case_sensitive; | 116 const bool case_sensitive; |
| 78 const size_t expected_start; | 117 const size_t expected_start; |
| 79 }; | 118 }; |
| 80 | 119 |
| 81 class GetTextSelectionStartTest | 120 class GetTextSelectionStartTest |
| 82 : public testing::TestWithParam<GetTextSelectionStartCase> {}; | 121 : public testing::TestWithParam<GetTextSelectionStartCase> {}; |
| 83 | 122 |
| 84 TEST_P(GetTextSelectionStartTest, GetTextSelectionStart) { | 123 TEST_P(GetTextSelectionStartTest, GetTextSelectionStart) { |
| 85 auto test_case = GetParam(); | 124 auto test_case = GetParam(); |
| 86 SCOPED_TRACE(testing::Message() | 125 SCOPED_TRACE(testing::Message() |
| 87 << "suggestion = " << test_case.field_suggestion | 126 << "full string = " << test_case.field_suggestion |
|
vabr (Chromium)
2017/06/12 18:56:59
nit: Also here, please do not change the unrelated
melandory
2017/06/19 10:53:48
Done.
| |
| 88 << ", contents = " << test_case.field_contents | 127 << ", prefix = " << test_case.field_contents |
| 89 << ", case_sensitive = " << test_case.case_sensitive); | 128 << ", case_sensitive = " << test_case.case_sensitive); |
| 90 EXPECT_EQ( | 129 EXPECT_EQ( |
| 91 test_case.expected_start, | 130 test_case.expected_start, |
| 92 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion), | 131 GetTextSelectionStart(base::ASCIIToUTF16(test_case.field_suggestion), |
| 93 base::ASCIIToUTF16(test_case.field_contents), | 132 base::ASCIIToUTF16(test_case.field_contents), |
| 94 test_case.case_sensitive)); | 133 test_case.case_sensitive)); |
| 95 } | 134 } |
| 96 | 135 |
| 97 INSTANTIATE_TEST_CASE_P( | 136 INSTANTIATE_TEST_CASE_P( |
| 98 AutofillUtilTest, | 137 AutofillUtilTest, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", | 189 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", |
| 151 {"foo", "bar", "baz"}}, | 190 {"foo", "bar", "baz"}}, |
| 152 | 191 |
| 153 // Test collapsing of multiple whitespace characters in a row | 192 // Test collapsing of multiple whitespace characters in a row |
| 154 LowercaseAndTokenizeAttributeStringCase{" \t\t\n\n ", | 193 LowercaseAndTokenizeAttributeStringCase{" \t\t\n\n ", |
| 155 std::vector<std::string>()}, | 194 std::vector<std::string>()}, |
| 156 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", | 195 LowercaseAndTokenizeAttributeStringCase{"foO baR bAz", |
| 157 {"foo", "bar", "baz"}})); | 196 {"foo", "bar", "baz"}})); |
| 158 | 197 |
| 159 } // namespace autofill | 198 } // namespace autofill |
| OLD | NEW |