OLD | NEW |
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 "chrome/browser/autocomplete/history_url_provider.h" | 5 #include "chrome/browser/autocomplete/history_url_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 Time::Now() - | 238 Time::Now() - |
239 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1), | 239 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1), |
240 false, history::SOURCE_BROWSED); | 240 false, history::SOURCE_BROWSED); |
241 } | 241 } |
242 | 242 |
243 void HistoryURLProviderTest::RunTest(const string16 text, | 243 void HistoryURLProviderTest::RunTest(const string16 text, |
244 const string16& desired_tld, | 244 const string16& desired_tld, |
245 bool prevent_inline_autocomplete, | 245 bool prevent_inline_autocomplete, |
246 const std::string* expected_urls, | 246 const std::string* expected_urls, |
247 size_t num_results) { | 247 size_t num_results) { |
248 AutocompleteInput input(text, string16::npos, desired_tld, | 248 AutocompleteInput input(text, string16::npos, desired_tld, GURL(), |
249 prevent_inline_autocomplete, false, true, | 249 prevent_inline_autocomplete, false, true, |
250 AutocompleteInput::ALL_MATCHES); | 250 AutocompleteInput::ALL_MATCHES); |
251 autocomplete_->Start(input, false); | 251 autocomplete_->Start(input, false); |
252 if (!autocomplete_->done()) | 252 if (!autocomplete_->done()) |
253 MessageLoop::current()->Run(); | 253 MessageLoop::current()->Run(); |
254 | 254 |
255 matches_ = autocomplete_->matches(); | 255 matches_ = autocomplete_->matches(); |
256 if (sort_matches_) { | 256 if (sort_matches_) { |
257 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) | 257 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) |
258 i->ComputeStrippedDestinationURL(profile_.get()); | 258 i->ComputeStrippedDestinationURL(profile_.get()); |
259 std::sort(matches_.begin(), matches_.end(), | 259 std::sort(matches_.begin(), matches_.end(), |
260 &AutocompleteMatch::DestinationSortFunc); | 260 &AutocompleteMatch::DestinationSortFunc); |
261 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 261 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
262 &AutocompleteMatch::DestinationsEqual), | 262 &AutocompleteMatch::DestinationsEqual), |
263 matches_.end()); | 263 matches_.end()); |
264 std::sort(matches_.begin(), matches_.end(), | 264 std::sort(matches_.begin(), matches_.end(), |
265 &AutocompleteMatch::MoreRelevant); | 265 &AutocompleteMatch::MoreRelevant); |
266 } | 266 } |
267 ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text | 267 ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text |
268 << "\nTLD: \"" << desired_tld << "\""; | 268 << "\nTLD: \"" << desired_tld << "\""; |
269 for (size_t i = 0; i < num_results; ++i) | 269 for (size_t i = 0; i < num_results; ++i) |
270 EXPECT_EQ(expected_urls[i], matches_[i].destination_url.spec()); | 270 EXPECT_EQ(expected_urls[i], matches_[i].destination_url.spec()); |
271 } | 271 } |
272 | 272 |
273 void HistoryURLProviderTest::RunAdjustOffsetTest(const string16 text, | 273 void HistoryURLProviderTest::RunAdjustOffsetTest(const string16 text, |
274 size_t expected_offset) { | 274 size_t expected_offset) { |
275 AutocompleteInput input(text, string16::npos, string16(), false, false, true, | 275 AutocompleteInput input(text, string16::npos, string16(), GURL(), false, |
276 AutocompleteInput::ALL_MATCHES); | 276 false, true, AutocompleteInput::ALL_MATCHES); |
277 autocomplete_->Start(input, false); | 277 autocomplete_->Start(input, false); |
278 if (!autocomplete_->done()) | 278 if (!autocomplete_->done()) |
279 MessageLoop::current()->Run(); | 279 MessageLoop::current()->Run(); |
280 | 280 |
281 matches_ = autocomplete_->matches(); | 281 matches_ = autocomplete_->matches(); |
282 ASSERT_GE(matches_.size(), 1U) << "Input text: " << text; | 282 ASSERT_GE(matches_.size(), 1U) << "Input text: " << text; |
283 EXPECT_EQ(expected_offset, matches_[0].inline_autocomplete_offset); | 283 EXPECT_EQ(expected_offset, matches_[0].inline_autocomplete_offset); |
284 } | 284 } |
285 | 285 |
286 TEST_F(HistoryURLProviderTest, PromoteShorterURLs) { | 286 TEST_F(HistoryURLProviderTest, PromoteShorterURLs) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 RunAdjustOffsetTest(ASCIIToUTF16("http://spaces.com/path%20with%20spa"), 31); | 511 RunAdjustOffsetTest(ASCIIToUTF16("http://spaces.com/path%20with%20spa"), 31); |
512 RunAdjustOffsetTest(ASCIIToUTF16("http://ms/c++ s"), 15); | 512 RunAdjustOffsetTest(ASCIIToUTF16("http://ms/c++ s"), 15); |
513 } | 513 } |
514 | 514 |
515 // Make sure the results for the input 'p' don't change between the first and | 515 // Make sure the results for the input 'p' don't change between the first and |
516 // second passes. | 516 // second passes. |
517 TEST_F(HistoryURLProviderTest, EmptyVisits) { | 517 TEST_F(HistoryURLProviderTest, EmptyVisits) { |
518 // Wait for history to create the in memory DB. | 518 // Wait for history to create the in memory DB. |
519 profile_->BlockUntilHistoryProcessesPendingRequests(); | 519 profile_->BlockUntilHistoryProcessesPendingRequests(); |
520 | 520 |
521 AutocompleteInput input(ASCIIToUTF16("p"), string16::npos, string16(), false, | 521 AutocompleteInput input(ASCIIToUTF16("p"), string16::npos, string16(), GURL(), |
522 false, true, AutocompleteInput::ALL_MATCHES); | 522 false, false, true, AutocompleteInput::ALL_MATCHES); |
523 autocomplete_->Start(input, false); | 523 autocomplete_->Start(input, false); |
524 // HistoryURLProvider shouldn't be done (waiting on async results). | 524 // HistoryURLProvider shouldn't be done (waiting on async results). |
525 EXPECT_FALSE(autocomplete_->done()); | 525 EXPECT_FALSE(autocomplete_->done()); |
526 | 526 |
527 // We should get back an entry for pandora. | 527 // We should get back an entry for pandora. |
528 matches_ = autocomplete_->matches(); | 528 matches_ = autocomplete_->matches(); |
529 ASSERT_GT(matches_.size(), 0u); | 529 ASSERT_GT(matches_.size(), 0u); |
530 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); | 530 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); |
531 int pandora_relevance = matches_[0].relevance; | 531 int pandora_relevance = matches_[0].relevance; |
532 | 532 |
(...skipping 15 matching lines...) Expand all Loading... |
548 | 548 |
549 std::string navigation_2[] = {"http://slash/"}; | 549 std::string navigation_2[] = {"http://slash/"}; |
550 RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2, | 550 RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2, |
551 arraysize(navigation_2)); | 551 arraysize(navigation_2)); |
552 | 552 |
553 RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0); | 553 RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0); |
554 } | 554 } |
555 | 555 |
556 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { | 556 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { |
557 AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(), | 557 AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(), |
558 false, false, true, AutocompleteInput::ALL_MATCHES); | 558 GURL(), false, false, true, |
| 559 AutocompleteInput::ALL_MATCHES); |
559 autocomplete_->Start(input, false); | 560 autocomplete_->Start(input, false); |
560 if (!autocomplete_->done()) | 561 if (!autocomplete_->done()) |
561 MessageLoop::current()->Run(); | 562 MessageLoop::current()->Run(); |
562 | 563 |
563 // None of the matches should attempt to autocomplete. | 564 // None of the matches should attempt to autocomplete. |
564 matches_ = autocomplete_->matches(); | 565 matches_ = autocomplete_->matches(); |
565 for (size_t i = 0; i < matches_.size(); ++i) | 566 for (size_t i = 0; i < matches_.size(); ++i) |
566 EXPECT_EQ(string16::npos, matches_[i].inline_autocomplete_offset); | 567 EXPECT_EQ(string16::npos, matches_[i].inline_autocomplete_offset); |
567 } | 568 } |
568 | 569 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 } | 678 } |
678 | 679 |
679 TEST_F(HistoryURLProviderTest, CrashDueToFixup) { | 680 TEST_F(HistoryURLProviderTest, CrashDueToFixup) { |
680 // This test passes if we don't crash. The results don't matter. | 681 // This test passes if we don't crash. The results don't matter. |
681 const char* const test_cases[] = { | 682 const char* const test_cases[] = { |
682 "//c", | 683 "//c", |
683 "\\@st" | 684 "\\@st" |
684 }; | 685 }; |
685 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 686 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
686 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos, | 687 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos, |
687 string16(), false, false, true, | 688 string16(), GURL(), false, false, true, |
688 AutocompleteInput::ALL_MATCHES); | 689 AutocompleteInput::ALL_MATCHES); |
689 autocomplete_->Start(input, false); | 690 autocomplete_->Start(input, false); |
690 if (!autocomplete_->done()) | 691 if (!autocomplete_->done()) |
691 MessageLoop::current()->Run(); | 692 MessageLoop::current()->Run(); |
692 } | 693 } |
693 } | 694 } |
694 | 695 |
695 TEST_F(HistoryURLProviderTest, CullSearchResults) { | 696 TEST_F(HistoryURLProviderTest, CullSearchResults) { |
696 // Set up a default search engine. | 697 // Set up a default search engine. |
697 TemplateURLData data; | 698 TemplateURLData data; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 RunTest(ASCIIToUTF16("foobar"), string16(), true, | 730 RunTest(ASCIIToUTF16("foobar"), string16(), true, |
730 expected_when_searching_query, arraysize(expected_when_searching_query)); | 731 expected_when_searching_query, arraysize(expected_when_searching_query)); |
731 | 732 |
732 // We should not see search URLs when typing the search engine name. | 733 // We should not see search URLs when typing the search engine name. |
733 const std::string expected_when_searching_site[] = { | 734 const std::string expected_when_searching_site[] = { |
734 test_cases[0].url | 735 test_cases[0].url |
735 }; | 736 }; |
736 RunTest(ASCIIToUTF16("testsearch"), string16(), true, | 737 RunTest(ASCIIToUTF16("testsearch"), string16(), true, |
737 expected_when_searching_site, arraysize(expected_when_searching_site)); | 738 expected_when_searching_site, arraysize(expected_when_searching_site)); |
738 } | 739 } |
OLD | NEW |