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

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

Issue 6731036: Enabled pressing TAB to cycle through the Omnibox results. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 8 years, 11 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string16.h" 8 #include "base/string16.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 11 matching lines...) Expand all
22 #include "content/public/browser/notification_observer.h" 22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h" 23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/notification_source.h" 24 #include "content/public/browser/notification_source.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 static std::ostream& operator<<(std::ostream& os, 27 static std::ostream& operator<<(std::ostream& os,
28 const AutocompleteResult::const_iterator& it) { 28 const AutocompleteResult::const_iterator& it) {
29 return os << static_cast<const AutocompleteMatch*>(&(*it)); 29 return os << static_cast<const AutocompleteMatch*>(&(*it));
30 } 30 }
31 31
32 namespace {
33
34 const size_t num_results_per_provider = 3; 32 const size_t num_results_per_provider = 3;
Peter Kasting 2012/01/11 03:00:16 Nit: Enclosing this constant in a namespace before
35 33
36 // Autocomplete provider that provides known results. Note that this is 34 // Autocomplete provider that provides known results. Note that this is
37 // refcounted so that it can also be a task on the message loop. 35 // refcounted so that it can also be a task on the message loop.
38 class TestProvider : public AutocompleteProvider { 36 class TestProvider : public AutocompleteProvider {
39 public: 37 public:
40 TestProvider(int relevance, const string16& prefix) 38 TestProvider(int relevance, const string16& prefix)
41 : AutocompleteProvider(NULL, NULL, ""), 39 : AutocompleteProvider(NULL, NULL, ""),
42 relevance_(relevance), 40 relevance_(relevance),
43 prefix_(prefix) { 41 prefix_(prefix) {
44 } 42 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 match.description_class.push_back( 99 match.description_class.push_back(
102 ACMatchClassification(0, ACMatchClassification::NONE)); 100 ACMatchClassification(0, ACMatchClassification::NONE));
103 101
104 matches_.push_back(match); 102 matches_.push_back(match);
105 } 103 }
106 } 104 }
107 105
108 class AutocompleteProviderTest : public testing::Test, 106 class AutocompleteProviderTest : public testing::Test,
109 public content::NotificationObserver { 107 public content::NotificationObserver {
110 protected: 108 protected:
109 struct KeywordTestData {
110 const string16 fill_into_edit;
111 const string16 keyword;
112 const bool expected_keyword_result;
113 };
114
115 protected:
111 void ResetControllerWithTestProviders(bool same_destinations); 116 void ResetControllerWithTestProviders(bool same_destinations);
112 117
113 // Runs a query on the input "a", and makes sure both providers' input is 118 // Runs a query on the input "a", and makes sure both providers' input is
114 // properly collected. 119 // properly collected.
115 void RunTest(); 120 void RunTest();
116 121
122 void RunRedundantKeywordTest(const KeywordTestData* match_data, size_t size);
123
124 void RunQuery(const string16 query);
125
117 void ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); 126 void ResetControllerWithTestProvidersWithKeywordAndSearchProviders();
127 void ResetControllerWithKeywordProvider();
118 void RunExactKeymatchTest(bool allow_exact_keyword_match); 128 void RunExactKeymatchTest(bool allow_exact_keyword_match);
119 129
120 // These providers are owned by the controller once it's created. 130 // These providers are owned by the controller once it's created.
121 ACProviders providers_; 131 ACProviders providers_;
122 132
123 AutocompleteResult result_; 133 AutocompleteResult result_;
134 scoped_ptr<AutocompleteController> controller_;
124 135
125 private: 136 private:
126 // content::NotificationObserver 137 // content::NotificationObserver
127 virtual void Observe(int type, 138 virtual void Observe(int type,
128 const content::NotificationSource& source, 139 const content::NotificationSource& source,
129 const content::NotificationDetails& details); 140 const content::NotificationDetails& details);
130 141
131 MessageLoopForUI message_loop_; 142 MessageLoopForUI message_loop_;
132 scoped_ptr<AutocompleteController> controller_;
133 content::NotificationRegistrar registrar_; 143 content::NotificationRegistrar registrar_;
134 TestingProfile profile_; 144 TestingProfile profile_;
135 }; 145 };
136 146
137 void AutocompleteProviderTest::ResetControllerWithTestProviders( 147 void AutocompleteProviderTest::ResetControllerWithTestProviders(
138 bool same_destinations) { 148 bool same_destinations) {
139 // Forget about any existing providers. The controller owns them and will 149 // Forget about any existing providers. The controller owns them and will
140 // Release() them below, when we delete it during the call to reset(). 150 // Release() them below, when we delete it during the call to reset().
141 providers_.clear(); 151 providers_.clear();
142 152
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // Create both a keyword and search provider, and add them in that order. 204 // Create both a keyword and search provider, and add them in that order.
195 // (Order is important; see comments in RunExactKeymatchTest().) 205 // (Order is important; see comments in RunExactKeymatchTest().)
196 AutocompleteProvider* keyword_provider = new KeywordProvider(NULL, 206 AutocompleteProvider* keyword_provider = new KeywordProvider(NULL,
197 &profile_); 207 &profile_);
198 keyword_provider->AddRef(); 208 keyword_provider->AddRef();
199 providers_.push_back(keyword_provider); 209 providers_.push_back(keyword_provider);
200 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_); 210 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_);
201 search_provider->AddRef(); 211 search_provider->AddRef();
202 providers_.push_back(search_provider); 212 providers_.push_back(search_provider);
203 213
214 controller_.reset(new AutocompleteController(providers_, &profile_));
215 }
216
217 void AutocompleteProviderTest::
218 ResetControllerWithKeywordProvider() {
219 profile_.CreateTemplateURLService();
220
221 TemplateURLService* turl_model =
222 TemplateURLServiceFactory::GetForProfile(&profile_);
223
224 // Create a TemplateURL for KeywordProvider.
225 TemplateURL* keyword_t_url = new TemplateURL();
226 keyword_t_url->set_short_name(ASCIIToUTF16("foo.com"));
227 keyword_t_url->set_keyword(ASCIIToUTF16("foo.com"));
228 keyword_t_url->SetURL("http://foo.com/{searchTerms}", 0, 0);
229 turl_model->Add(keyword_t_url);
230 ASSERT_NE(0, keyword_t_url->id());
231
232 // Create another TemplateURL for KeywordProvider.
233 keyword_t_url = new TemplateURL();
234 keyword_t_url->set_short_name(ASCIIToUTF16("bar.com"));
235 keyword_t_url->set_keyword(ASCIIToUTF16("bar.com"));
236 keyword_t_url->SetURL("http://bar.com/{searchTerms}", 0, 0);
237 turl_model->Add(keyword_t_url);
238 ASSERT_NE(0, keyword_t_url->id());
239
240 // Forget about any existing providers. The controller owns them and will
241 // Release() them below, when we delete it during the call to reset().
242 providers_.clear();
243
244 // Create both a keyword and search provider, and add them in that order.
245 // (Order is important; see comments in RunExactKeymatchTest().)
246 KeywordProvider* keyword_provider = new KeywordProvider(NULL,
247 &profile_);
248 keyword_provider->AddRef();
249 providers_.push_back(keyword_provider);
250
204 AutocompleteController* controller = 251 AutocompleteController* controller =
205 new AutocompleteController(providers_, &profile_); 252 new AutocompleteController(providers_, &profile_);
253 controller->set_keyword_provider(keyword_provider);
206 controller_.reset(controller); 254 controller_.reset(controller);
207 } 255 }
208 256
209 void AutocompleteProviderTest::RunTest() { 257 void AutocompleteProviderTest::RunTest() {
258 RunQuery(ASCIIToUTF16("a"));
259 }
260
261 void AutocompleteProviderTest::RunRedundantKeywordTest(
262 const KeywordTestData* match_data,
263 size_t size) {
264 ACMatches matches;
265 for (size_t i = 0; i < size; ++i) {
266 AutocompleteMatch match;
267 match.fill_into_edit = match_data[i].fill_into_edit;
268 match.keyword = match_data[i].keyword;
269 matches.push_back(match);
270 }
271
272 AutocompleteResult result;
273 result.AppendMatches(matches);
274 controller_->UpdateAssociatedKeywords(&result);
275
276 for (size_t j = 0; j < result.size(); ++j) {
277 EXPECT_EQ(match_data[j].expected_keyword_result,
278 result.match_at(j).associated_keyword.get() != NULL);
279 }
280 }
281
282
283 void AutocompleteProviderTest::RunQuery(const string16 query) {
210 result_.Reset(); 284 result_.Reset();
211 controller_->Start(ASCIIToUTF16("a"), string16(), true, false, true, 285 controller_->Start(query, string16(), true, false, true,
212 AutocompleteInput::ALL_MATCHES); 286 AutocompleteInput::ALL_MATCHES);
213 287
214 // The message loop will terminate when all autocomplete input has been 288 if (!controller_->done())
215 // collected. 289 // The message loop will terminate when all autocomplete input has been
216 MessageLoop::current()->Run(); 290 // collected.
291 MessageLoop::current()->Run();
217 } 292 }
218 293
219 void AutocompleteProviderTest::RunExactKeymatchTest( 294 void AutocompleteProviderTest::RunExactKeymatchTest(
220 bool allow_exact_keyword_match) { 295 bool allow_exact_keyword_match) {
221 // Send the controller input which exactly matches the keyword provider we 296 // Send the controller input which exactly matches the keyword provider we
222 // created in ResetControllerWithKeywordAndSearchProviders(). The default 297 // created in ResetControllerWithKeywordAndSearchProviders(). The default
223 // match should thus be a keyword match iff |allow_exact_keyword_match| is 298 // match should thus be a keyword match iff |allow_exact_keyword_match| is
224 // true. 299 // true.
225 controller_->Start(ASCIIToUTF16("k test"), string16(), true, false, 300 controller_->Start(ASCIIToUTF16("k test"), string16(), true, false,
226 allow_exact_keyword_match, 301 allow_exact_keyword_match,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 i != result_.end(); ++i) 341 i != result_.end(); ++i)
267 EXPECT_EQ(providers_[1], i->provider); 342 EXPECT_EQ(providers_[1], i->provider);
268 } 343 }
269 344
270 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { 345 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) {
271 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); 346 ResetControllerWithTestProvidersWithKeywordAndSearchProviders();
272 RunExactKeymatchTest(true); 347 RunExactKeymatchTest(true);
273 RunExactKeymatchTest(false); 348 RunExactKeymatchTest(false);
274 } 349 }
275 350
351 // Test that redundant associated keywords are removed.
352 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) {
353 ResetControllerWithKeywordProvider();
354
355 // Get the controller's internal members in the correct state.
356 RunQuery(ASCIIToUTF16("fo"));
357
358 {
359 KeywordTestData duplicate_url[] = {
360 { ASCIIToUTF16("fo"), string16(), false },
361 { ASCIIToUTF16("foo.com"), string16(), true },
362 { ASCIIToUTF16("foo.com"), string16(), false }
363 };
364
365 SCOPED_TRACE("Duplicate url");
366 RunRedundantKeywordTest(duplicate_url, ARRAYSIZE_UNSAFE(duplicate_url));
367 }
368
369 {
370 KeywordTestData keyword_match[] = {
371 { ASCIIToUTF16("foo.com"), ASCIIToUTF16("foo.com"), false },
372 { ASCIIToUTF16("foo.com"), string16(), false }
373 };
374
375 SCOPED_TRACE("Duplicate url with keyword match");
376 RunRedundantKeywordTest(keyword_match, ARRAYSIZE_UNSAFE(keyword_match));
377 }
378
379 {
380 KeywordTestData multiple_keyword[] = {
381 { ASCIIToUTF16("fo"), string16(), false },
382 { ASCIIToUTF16("foo.com"), string16(), true },
383 { ASCIIToUTF16("foo.com"), string16(), false },
384 { ASCIIToUTF16("bar.com"), string16(), true },
385 };
386
387 SCOPED_TRACE("Duplicate url with multiple keywords");
388 RunRedundantKeywordTest(multiple_keyword,
389 ARRAYSIZE_UNSAFE(multiple_keyword));
390 }
391 }
392
276 typedef testing::Test AutocompleteTest; 393 typedef testing::Test AutocompleteTest;
277 394
278 TEST_F(AutocompleteTest, InputType) { 395 TEST_F(AutocompleteTest, InputType) {
279 struct test_data { 396 struct test_data {
280 const string16 input; 397 const string16 input;
281 const AutocompleteInput::Type type; 398 const AutocompleteInput::Type type;
282 } input_cases[] = { 399 } input_cases[] = {
283 { string16(), AutocompleteInput::INVALID }, 400 { string16(), AutocompleteInput::INVALID },
284 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, 401 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY },
285 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, 402 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY },
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 &scheme, 594 &scheme,
478 &host); 595 &host);
479 AutocompleteInput input(input_cases[i].input, string16(), true, false, 596 AutocompleteInput input(input_cases[i].input, string16(), true, false,
480 true, AutocompleteInput::ALL_MATCHES); 597 true, AutocompleteInput::ALL_MATCHES);
481 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); 598 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin);
482 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); 599 EXPECT_EQ(input_cases[i].scheme.len, scheme.len);
483 EXPECT_EQ(input_cases[i].host.begin, host.begin); 600 EXPECT_EQ(input_cases[i].host.begin, host.begin);
484 EXPECT_EQ(input_cases[i].host.len, host.len); 601 EXPECT_EQ(input_cases[i].host.len, host.len);
485 } 602 }
486 } 603 }
487
488 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698