OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/omnibox/browser/autocomplete_match.h" | 5 #include "components/omnibox/browser/autocomplete_match.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/test/scoped_feature_list.h" |
| 12 #include "components/omnibox/browser/omnibox_field_trial.h" |
11 #include "components/omnibox/browser/test_scheme_classifier.h" | 13 #include "components/omnibox/browser/test_scheme_classifier.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/gurl.h" |
13 | 16 |
14 TEST(AutocompleteMatchTest, MoreRelevant) { | 17 TEST(AutocompleteMatchTest, MoreRelevant) { |
15 struct RelevantCases { | 18 struct RelevantCases { |
16 int r1; | 19 int r1; |
17 int r2; | 20 int r2; |
18 bool expected_result; | 21 bool expected_result; |
19 } cases[] = { | 22 } cases[] = { |
20 { 10, 0, true }, | 23 { 10, 0, true }, |
21 { 10, -5, true }, | 24 { 10, -5, true }, |
22 { -5, 10, false }, | 25 { -5, 10, false }, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // Test an arbitrary complicated case. | 104 // Test an arbitrary complicated case. |
102 EXPECT_EQ("0,2," "1,0," "2,1," "4,3," "5,7," "6,3," "7,7," "15,1," "17,0", | 105 EXPECT_EQ("0,2," "1,0," "2,1," "4,3," "5,7," "6,3," "7,7," "15,1," "17,0", |
103 AutocompleteMatch::ClassificationsToString( | 106 AutocompleteMatch::ClassificationsToString( |
104 AutocompleteMatch::MergeClassifications( | 107 AutocompleteMatch::MergeClassifications( |
105 AutocompleteMatch::ClassificationsFromString( | 108 AutocompleteMatch::ClassificationsFromString( |
106 "0,0," "2,1," "4,3," "7,7," "10,6," "15,0"), | 109 "0,0," "2,1," "4,3," "7,7," "10,6," "15,0"), |
107 AutocompleteMatch::ClassificationsFromString( | 110 AutocompleteMatch::ClassificationsFromString( |
108 "0,2," "1,0," "5,7," "6,1," "17,0")))); | 111 "0,2," "1,0," "5,7," "6,1," "17,0")))); |
109 } | 112 } |
110 | 113 |
| 114 TEST(AutocompleteMatchTest, FormatUrlForSuggestionDisplay) { |
| 115 struct FormatUrlTestData { |
| 116 const std::string url; |
| 117 bool trim_scheme; |
| 118 size_t offset_for_adjustment; |
| 119 const std::string expected_result; |
| 120 size_t expected_adjusted_offset; |
| 121 |
| 122 void Validate() { |
| 123 SCOPED_TRACE(testing::Message() |
| 124 << " url= " << url << " trim_scheme=" << trim_scheme |
| 125 << " offset_for_adjustment=" << offset_for_adjustment |
| 126 << " expected_result=" << expected_result |
| 127 << " expected_adjusted_offset=" << expected_adjusted_offset); |
| 128 |
| 129 size_t offset_result = offset_for_adjustment; |
| 130 base::string16 result = AutocompleteMatch::FormatUrlForSuggestionDisplay( |
| 131 GURL(url), trim_scheme, &offset_result); |
| 132 |
| 133 EXPECT_EQ(expected_result, base::UTF16ToASCII(result)); |
| 134 EXPECT_EQ(expected_adjusted_offset, offset_result); |
| 135 }; |
| 136 }; |
| 137 |
| 138 FormatUrlTestData normal_cases[] = { |
| 139 {"http://google.com", true, 9, "google.com", 2}, |
| 140 {"https://google.com", true, 9, "https://google.com", 9}, |
| 141 {"http://google.com", false, 9, "http://google.com", 9}, |
| 142 {"https://google.com", false, 9, "https://google.com", 9}, |
| 143 }; |
| 144 for (size_t i = 0; i < arraysize(normal_cases); ++i) { |
| 145 normal_cases[i].Validate(); |
| 146 } |
| 147 |
| 148 std::unique_ptr<base::test::ScopedFeatureList> feature_list( |
| 149 new base::test::ScopedFeatureList); |
| 150 feature_list->InitAndEnableFeature( |
| 151 omnibox::kUIExperimentHideSuggestionUrlScheme); |
| 152 |
| 153 FormatUrlTestData omit_scheme_cases[] = { |
| 154 {"http://google.com", true, 9, "google.com", 2}, |
| 155 {"https://google.com", true, 9, "google.com", 1}, |
| 156 {"https://username:password@google.com", true, 9, "google.com", |
| 157 base::string16::npos}, |
| 158 {"https://username:password@google.com", true, 29, "google.com", 3}, |
| 159 {"http://google.com", false, 9, "http://google.com", 9}, |
| 160 {"https://google.com", false, 9, "https://google.com", 9}, |
| 161 {"http://username:password@google.com", false, 9, "http://google.com", |
| 162 base::string16::npos}, |
| 163 }; |
| 164 for (size_t i = 0; i < arraysize(omit_scheme_cases); ++i) { |
| 165 omit_scheme_cases[i].Validate(); |
| 166 } |
| 167 } |
| 168 |
111 TEST(AutocompleteMatchTest, SupportsDeletion) { | 169 TEST(AutocompleteMatchTest, SupportsDeletion) { |
112 // A non-deletable match with no duplicates. | 170 // A non-deletable match with no duplicates. |
113 AutocompleteMatch m(NULL, 0, false, | 171 AutocompleteMatch m(NULL, 0, false, |
114 AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 172 AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
115 EXPECT_FALSE(m.SupportsDeletion()); | 173 EXPECT_FALSE(m.SupportsDeletion()); |
116 | 174 |
117 // A deletable match with no duplicates. | 175 // A deletable match with no duplicates. |
118 AutocompleteMatch m1(NULL, 0, true, | 176 AutocompleteMatch m1(NULL, 0, true, |
119 AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 177 AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
120 EXPECT_TRUE(m1.SupportsDeletion()); | 178 EXPECT_TRUE(m1.SupportsDeletion()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 m1.destination_url = GURL(cases[i].url1); | 252 m1.destination_url = GURL(cases[i].url1); |
195 m1.ComputeStrippedDestinationURL(input, nullptr); | 253 m1.ComputeStrippedDestinationURL(input, nullptr); |
196 AutocompleteMatch m2(nullptr, 100, false, | 254 AutocompleteMatch m2(nullptr, 100, false, |
197 AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 255 AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
198 m2.destination_url = GURL(cases[i].url2); | 256 m2.destination_url = GURL(cases[i].url2); |
199 m2.ComputeStrippedDestinationURL(input, nullptr); | 257 m2.ComputeStrippedDestinationURL(input, nullptr); |
200 EXPECT_EQ(cases[i].expected_duplicate, | 258 EXPECT_EQ(cases[i].expected_duplicate, |
201 AutocompleteMatch::DestinationsEqual(m1, m2)); | 259 AutocompleteMatch::DestinationsEqual(m1, m2)); |
202 } | 260 } |
203 } | 261 } |
OLD | NEW |