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

Side by Side Diff: chrome/browser/autocomplete/keyword_provider_unittest.cc

Issue 18878007: Omnibox: Make the Controller Reorder Matches for Inlining (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove blank line Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/autocomplete/autocomplete_match.h" 8 #include "chrome/browser/autocomplete/autocomplete_match.h"
9 #include "chrome/browser/autocomplete/keyword_provider.h" 9 #include "chrome/browser/autocomplete/keyword_provider.h"
10 #include "chrome/browser/search_engines/template_url.h" 10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/search_engines/template_url_service.h" 11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/testing_browser_process.h" 13 #include "chrome/test/base/testing_browser_process.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h" 15 #include "url/gurl.h"
16 16
17 class KeywordProviderTest : public testing::Test { 17 class KeywordProviderTest : public testing::Test {
18 protected: 18 protected:
19 template<class ResultType> 19 template<class ResultType>
20 struct test_data { 20 struct MatchType {
21 const ResultType member;
22 bool allowed_to_be_default_match;
23 };
24
25 template<class ResultType>
26 struct TestData {
21 const string16 input; 27 const string16 input;
22 const size_t num_results; 28 const size_t num_results;
23 const ResultType output[3]; 29 const MatchType<ResultType> output[3];
24 }; 30 };
25 31
26 KeywordProviderTest() : kw_provider_(NULL) { } 32 KeywordProviderTest() : kw_provider_(NULL) { }
27 virtual ~KeywordProviderTest() { } 33 virtual ~KeywordProviderTest() { }
28 34
29 virtual void SetUp(); 35 virtual void SetUp();
30 virtual void TearDown(); 36 virtual void TearDown();
31 37
32 template<class ResultType> 38 template<class ResultType>
33 void RunTest(test_data<ResultType>* keyword_cases, 39 void RunTest(TestData<ResultType>* keyword_cases,
34 int num_cases, 40 int num_cases,
35 ResultType AutocompleteMatch::* member); 41 ResultType AutocompleteMatch::* member);
36 42
37 protected: 43 protected:
38 static const TemplateURLService::Initializer kTestData[]; 44 static const TemplateURLService::Initializer kTestData[];
39 45
40 scoped_refptr<KeywordProvider> kw_provider_; 46 scoped_refptr<KeywordProvider> kw_provider_;
41 scoped_ptr<TemplateURLService> model_; 47 scoped_ptr<TemplateURLService> model_;
42 }; 48 };
43 49
(...skipping 13 matching lines...) Expand all
57 kw_provider_ = new KeywordProvider(NULL, model_.get()); 63 kw_provider_ = new KeywordProvider(NULL, model_.get());
58 } 64 }
59 65
60 void KeywordProviderTest::TearDown() { 66 void KeywordProviderTest::TearDown() {
61 model_.reset(); 67 model_.reset();
62 kw_provider_ = NULL; 68 kw_provider_ = NULL;
63 } 69 }
64 70
65 template<class ResultType> 71 template<class ResultType>
66 void KeywordProviderTest::RunTest( 72 void KeywordProviderTest::RunTest(
67 test_data<ResultType>* keyword_cases, 73 TestData<ResultType>* keyword_cases,
68 int num_cases, 74 int num_cases,
69 ResultType AutocompleteMatch::* member) { 75 ResultType AutocompleteMatch::* member) {
70 ACMatches matches; 76 ACMatches matches;
71 for (int i = 0; i < num_cases; ++i) { 77 for (int i = 0; i < num_cases; ++i) {
72 AutocompleteInput input(keyword_cases[i].input, string16::npos, string16(), 78 AutocompleteInput input(keyword_cases[i].input, string16::npos, string16(),
73 GURL(), AutocompleteInput::INVALID_SPEC, true, 79 GURL(), AutocompleteInput::INVALID_SPEC, true,
74 false, true, AutocompleteInput::ALL_MATCHES); 80 false, true, AutocompleteInput::ALL_MATCHES);
75 kw_provider_->Start(input, false); 81 kw_provider_->Start(input, false);
76 EXPECT_TRUE(kw_provider_->done()); 82 EXPECT_TRUE(kw_provider_->done());
77 matches = kw_provider_->matches(); 83 matches = kw_provider_->matches();
78 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) << 84 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) <<
79 ASCIIToUTF16("Input was: ") + keyword_cases[i].input; 85 ASCIIToUTF16("Input was: ") + keyword_cases[i].input;
80 if (matches.size() == keyword_cases[i].num_results) { 86 if (matches.size() == keyword_cases[i].num_results) {
81 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) { 87 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) {
82 EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member); 88 EXPECT_EQ(keyword_cases[i].output[j].member, matches[j].*member);
89 EXPECT_EQ(keyword_cases[i].output[j].allowed_to_be_default_match,
90 matches[j].allowed_to_be_default_match);
83 } 91 }
84 } 92 }
85 } 93 }
86 } 94 }
87 95
88 TEST_F(KeywordProviderTest, Edit) { 96 TEST_F(KeywordProviderTest, Edit) {
89 test_data<string16> edit_cases[] = { 97 TestData<string16> edit_cases[] = {
90 // Searching for a nonexistent prefix should give nothing. 98 // Searching for a nonexistent prefix should give nothing.
91 {ASCIIToUTF16("Not Found"), 0, {}}, 99 {ASCIIToUTF16("Not Found"), 0, {}},
92 {ASCIIToUTF16("aaaaaNot Found"), 0, {}}, 100 {ASCIIToUTF16("aaaaaNot Found"), 0, {}},
93 101
94 // Check that tokenization only collapses whitespace between first tokens, 102 // Check that tokenization only collapses whitespace between first tokens,
95 // no-query-input cases have a space appended, and action is not escaped. 103 // no-query-input cases have a space appended, and action is not escaped.
96 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}}, 104 {ASCIIToUTF16("z"), 1, {{ASCIIToUTF16("z "), true}}},
97 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}}, 105 {ASCIIToUTF16("z \t"), 1, {{ASCIIToUTF16("z "), true}}},
98 106
99 // Check that exact, substituting keywords with a verbatim search term 107 // Check that exact, substituting keywords with a verbatim search term
100 // don't generate a result. (These are handled by SearchProvider.) 108 // don't generate a result. (These are handled by SearchProvider.)
101 {ASCIIToUTF16("z foo"), 0, {}}, 109 {ASCIIToUTF16("z foo"), 0, {}},
102 {ASCIIToUTF16("z a b c++"), 0, {}}, 110 {ASCIIToUTF16("z a b c++"), 0, {}},
103 111
104 // Matches should be limited to three, and sorted in quality order, not 112 // Matches should be limited to three, and sorted in quality order, not
105 // alphabetical. 113 // alphabetical.
106 {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "), 114 {ASCIIToUTF16("aaa"), 2, {{ASCIIToUTF16("aaaa "), false},
107 ASCIIToUTF16("aaaaa ")}}, 115 {ASCIIToUTF16("aaaaa "), false}}},
108 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"), 116 {ASCIIToUTF16("a 1 2 3"), 3, {{ASCIIToUTF16("aa 1 2 3"), false},
109 ASCIIToUTF16("ab 1 2 3"), 117 {ASCIIToUTF16("ab 1 2 3"), false},
110 ASCIIToUTF16("aaaa 1 2 3")}}, 118 {ASCIIToUTF16("aaaa 1 2 3"), false}}},
111 {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "), 119 {ASCIIToUTF16("www.a"), 3, {{ASCIIToUTF16("aa "), false},
112 ASCIIToUTF16("ab "), 120 {ASCIIToUTF16("ab "), false},
113 ASCIIToUTF16("aaaa ")}}, 121 {ASCIIToUTF16("aaaa "), false}}},
114 // Exact matches should prevent returning inexact matches. Also, the 122 // Exact matches should prevent returning inexact matches. Also, the
115 // verbatim query for this keyword match should not be returned. (It's 123 // verbatim query for this keyword match should not be returned. (It's
116 // returned by SearchProvider.) 124 // returned by SearchProvider.)
117 {ASCIIToUTF16("aaaa foo"), 0, {}}, 125 {ASCIIToUTF16("aaaa foo"), 0, {}},
118 {ASCIIToUTF16("www.aaaa foo"), 0, {}}, 126 {ASCIIToUTF16("www.aaaa foo"), 0, {}},
119 127
120 // Clean up keyword input properly. "http" and "https" are the only 128 // Clean up keyword input properly. "http" and "https" are the only
121 // allowed schemes. 129 // allowed schemes.
122 {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}}, 130 {ASCIIToUTF16("www"), 1, {{ASCIIToUTF16("www "), true}}},
123 {ASCIIToUTF16("www."), 0, {}}, 131 {ASCIIToUTF16("www."), 0, {}},
124 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"), 132 {ASCIIToUTF16("www.w w"), 2, {{ASCIIToUTF16("www w"), false},
125 ASCIIToUTF16("weasel w")}}, 133 {ASCIIToUTF16("weasel w"), false}}},
126 {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}}, 134 {ASCIIToUTF16("http://www"), 1, {{ASCIIToUTF16("www "), true}}},
127 {ASCIIToUTF16("http://www."), 0, {}}, 135 {ASCIIToUTF16("http://www."), 0, {}},
128 {ASCIIToUTF16("ftp: blah"), 0, {}}, 136 {ASCIIToUTF16("ftp: blah"), 0, {}},
129 {ASCIIToUTF16("mailto:z"), 0, {}}, 137 {ASCIIToUTF16("mailto:z"), 0, {}},
130 {ASCIIToUTF16("ftp://z"), 0, {}}, 138 {ASCIIToUTF16("ftp://z"), 0, {}},
131 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, 139 {ASCIIToUTF16("https://z"), 1, {{ASCIIToUTF16("z "), true}}},
132 }; 140 };
133 141
134 RunTest<string16>(edit_cases, arraysize(edit_cases), 142 RunTest<string16>(edit_cases, arraysize(edit_cases),
135 &AutocompleteMatch::fill_into_edit); 143 &AutocompleteMatch::fill_into_edit);
136 } 144 }
137 145
138 TEST_F(KeywordProviderTest, URL) { 146 TEST_F(KeywordProviderTest, URL) {
139 test_data<GURL> url_cases[] = { 147 TestData<GURL> url_cases[] = {
140 // No query input -> empty destination URL. 148 // No query input -> empty destination URL.
141 {ASCIIToUTF16("z"), 1, {GURL()}}, 149 {ASCIIToUTF16("z"), 1, {{GURL(), true}}},
142 {ASCIIToUTF16("z \t"), 1, {GURL()}}, 150 {ASCIIToUTF16("z \t"), 1, {{GURL(), true}}},
143 151
144 // Check that tokenization only collapses whitespace between first tokens 152 // Check that tokenization only collapses whitespace between first tokens
145 // and query input, but not rest of URL, is escaped. 153 // and query input, but not rest of URL, is escaped.
146 {ASCIIToUTF16("w bar +baz"), 2, {GURL(" +%2B?=bar+%2Bbazfoo "), 154 {ASCIIToUTF16("w bar +baz"), 2, {{GURL(" +%2B?=bar+%2Bbazfoo "), false},
147 GURL("bar+%2Bbaz=z")}}, 155 {GURL("bar+%2Bbaz=z"), false}}},
148 156
149 // Substitution should work with various locations of the "%s". 157 // Substitution should work with various locations of the "%s".
150 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), 158 {ASCIIToUTF16("aaa 1a2b"), 2,
151 GURL("1a2b")}}, 159 {{GURL("http://aaaa/?aaaa=1&b=1a2b&c"), false},
152 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), 160 {GURL("1a2b"), false}}},
153 GURL("bogus URL 1+2+3"), 161 {ASCIIToUTF16("a 1 2 3"), 3,
154 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, 162 {{GURL("aa.com?foo=1+2+3"), false},
155 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), 163 {GURL("bogus URL 1+2+3"), false},
156 GURL("weaselwweasel")}}, 164 {GURL("http://aaaa/?aaaa=1&b=1+2+3&c"), false}}},
165 {ASCIIToUTF16("www.w w"), 2, {{GURL(" +%2B?=wfoo "), false},
166 {GURL("weaselwweasel"), false}}},
157 }; 167 };
158 168
159 RunTest<GURL>(url_cases, arraysize(url_cases), 169 RunTest<GURL>(url_cases, arraysize(url_cases),
160 &AutocompleteMatch::destination_url); 170 &AutocompleteMatch::destination_url);
161 } 171 }
162 172
163 TEST_F(KeywordProviderTest, Contents) { 173 TEST_F(KeywordProviderTest, Contents) {
164 test_data<string16> contents_cases[] = { 174 TestData<string16> contents_cases[] = {
165 // No query input -> substitute "<enter query>" into contents. 175 // No query input -> substitute "<enter query>" into contents.
166 {ASCIIToUTF16("z"), 1, 176 {ASCIIToUTF16("z"), 1,
167 {ASCIIToUTF16("Search z for <enter query>")}}, 177 {{ASCIIToUTF16("Search z for <enter query>"), true}}},
168 {ASCIIToUTF16("z \t"), 1, 178 {ASCIIToUTF16("z \t"), 1,
169 {ASCIIToUTF16("Search z for <enter query>")}}, 179 {{ASCIIToUTF16("Search z for <enter query>"), true}}},
170 180
171 // Exact keyword matches with remaining text should return nothing. 181 // Exact keyword matches with remaining text should return nothing.
172 {ASCIIToUTF16("www.www www"), 0, {}}, 182 {ASCIIToUTF16("www.www www"), 0, {}},
173 {ASCIIToUTF16("z a b c++"), 0, {}}, 183 {ASCIIToUTF16("z a b c++"), 0, {}},
174 184
175 // Exact keyword matches with remaining text when the keyword is an 185 // Exact keyword matches with remaining text when the keyword is an
176 // extension keyword should return something. This is tested in 186 // extension keyword should return something. This is tested in
177 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc's 187 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc's
178 // in OmniboxApiTest's Basic test. 188 // in OmniboxApiTest's Basic test.
179 189
180 // Substitution should work with various locations of the "%s". 190 // Substitution should work with various locations of the "%s".
181 {ASCIIToUTF16("aaa"), 2, 191 {ASCIIToUTF16("aaa"), 2,
182 {ASCIIToUTF16("Search aaaa for <enter query>"), 192 {{ASCIIToUTF16("Search aaaa for <enter query>"), false},
183 ASCIIToUTF16("Search aaaaa for <enter query>")}}, 193 {ASCIIToUTF16("Search aaaaa for <enter query>"), false}}},
184 {ASCIIToUTF16("www.w w"), 2, 194 {ASCIIToUTF16("www.w w"), 2,
185 {ASCIIToUTF16("Search www for w"), 195 {{ASCIIToUTF16("Search www for w"), false},
186 ASCIIToUTF16("Search weasel for w")}}, 196 {ASCIIToUTF16("Search weasel for w"), false}}},
187 // Also, check that tokenization only collapses whitespace between first 197 // Also, check that tokenization only collapses whitespace between first
188 // tokens and contents are not escaped or unescaped. 198 // tokens and contents are not escaped or unescaped.
189 {ASCIIToUTF16("a 1 2+ 3"), 3, 199 {ASCIIToUTF16("a 1 2+ 3"), 3,
190 {ASCIIToUTF16("Search aa for 1 2+ 3"), 200 {{ASCIIToUTF16("Search aa for 1 2+ 3"), false},
191 ASCIIToUTF16("Search ab for 1 2+ 3"), 201 {ASCIIToUTF16("Search ab for 1 2+ 3"), false},
192 ASCIIToUTF16("Search aaaa for 1 2+ 3")}}, 202 {ASCIIToUTF16("Search aaaa for 1 2+ 3"), false}}},
193 }; 203 };
194 204
195 RunTest<string16>(contents_cases, arraysize(contents_cases), 205 RunTest<string16>(contents_cases, arraysize(contents_cases),
196 &AutocompleteMatch::contents); 206 &AutocompleteMatch::contents);
197 } 207 }
198 208
199 TEST_F(KeywordProviderTest, AddKeyword) { 209 TEST_F(KeywordProviderTest, AddKeyword) {
200 TemplateURLData data; 210 TemplateURLData data;
201 data.short_name = ASCIIToUTF16("Test"); 211 data.short_name = ASCIIToUTF16("Test");
202 string16 keyword(ASCIIToUTF16("foo")); 212 string16 keyword(ASCIIToUTF16("foo"));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); 289 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position());
280 } 290 }
281 } 291 }
282 292
283 // If extra query params are specified on the command line, they should be 293 // If extra query params are specified on the command line, they should be
284 // reflected (only) in the default search provider's destination URL. 294 // reflected (only) in the default search provider's destination URL.
285 TEST_F(KeywordProviderTest, ExtraQueryParams) { 295 TEST_F(KeywordProviderTest, ExtraQueryParams) {
286 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 296 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
287 switches::kExtraSearchQueryParams, "a=b"); 297 switches::kExtraSearchQueryParams, "a=b");
288 298
289 test_data<GURL> url_cases[] = { 299 TestData<GURL> url_cases[] = {
290 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?a=b&foo=1+2+3"), 300 {ASCIIToUTF16("a 1 2 3"), 3,
291 GURL("bogus URL 1+2+3"), 301 {{GURL("aa.com?a=b&foo=1+2+3"), false},
292 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, 302 {GURL("bogus URL 1+2+3"), false},
303 {GURL("http://aaaa/?aaaa=1&b=1+2+3&c"), false}}},
293 }; 304 };
294 305
295 RunTest<GURL>(url_cases, arraysize(url_cases), 306 RunTest<GURL>(url_cases, arraysize(url_cases),
296 &AutocompleteMatch::destination_url); 307 &AutocompleteMatch::destination_url);
297 } 308 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698