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

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

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 years, 6 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 "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/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 bool prevent_inline_autocomplete, 180 bool prevent_inline_autocomplete,
181 const std::string* expected_urls, 181 const std::string* expected_urls,
182 size_t num_results) { 182 size_t num_results) {
183 AutocompleteInput::Type type; 183 AutocompleteInput::Type type;
184 return RunTest(text, desired_tld, prevent_inline_autocomplete, 184 return RunTest(text, desired_tld, prevent_inline_autocomplete,
185 expected_urls, num_results, &type); 185 expected_urls, num_results, &type);
186 } 186 }
187 187
188 void RunAdjustOffsetTest(const string16 text, size_t expected_offset); 188 void RunAdjustOffsetTest(const string16 text, size_t expected_offset);
189 189
190 MessageLoopForUI message_loop_; 190 base::MessageLoopForUI message_loop_;
191 content::TestBrowserThread ui_thread_; 191 content::TestBrowserThread ui_thread_;
192 content::TestBrowserThread file_thread_; 192 content::TestBrowserThread file_thread_;
193 ACMatches matches_; 193 ACMatches matches_;
194 scoped_ptr<TestingProfile> profile_; 194 scoped_ptr<TestingProfile> profile_;
195 HistoryService* history_service_; 195 HistoryService* history_service_;
196 scoped_refptr<HistoryURLProvider> autocomplete_; 196 scoped_refptr<HistoryURLProvider> autocomplete_;
197 // Should the matches be sorted and duplicates removed? 197 // Should the matches be sorted and duplicates removed?
198 bool sort_matches_; 198 bool sort_matches_;
199 }; 199 };
200 200
201 class HistoryURLProviderTestNoDB : public HistoryURLProviderTest { 201 class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
202 protected: 202 protected:
203 virtual void SetUp() { 203 virtual void SetUp() {
204 SetUpImpl(true); 204 SetUpImpl(true);
205 } 205 }
206 }; 206 };
207 207
208 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) { 208 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
209 if (autocomplete_->done()) 209 if (autocomplete_->done())
210 MessageLoop::current()->Quit(); 210 base::MessageLoop::current()->Quit();
211 } 211 }
212 212
213 void HistoryURLProviderTest::SetUpImpl(bool no_db) { 213 void HistoryURLProviderTest::SetUpImpl(bool no_db) {
214 profile_.reset(new TestingProfile()); 214 profile_.reset(new TestingProfile());
215 profile_->CreateHistoryService(true, no_db); 215 profile_->CreateHistoryService(true, no_db);
216 history_service_ = 216 history_service_ =
217 HistoryServiceFactory::GetForProfile(profile_.get(), 217 HistoryServiceFactory::GetForProfile(profile_.get(),
218 Profile::EXPLICIT_ACCESS); 218 Profile::EXPLICIT_ACCESS);
219 219
220 autocomplete_ = new HistoryURLProvider(this, profile_.get(), "en-US,en,ko"); 220 autocomplete_ = new HistoryURLProvider(this, profile_.get(), "en-US,en,ko");
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 bool prevent_inline_autocomplete, 259 bool prevent_inline_autocomplete,
260 const std::string* expected_urls, 260 const std::string* expected_urls,
261 size_t num_results, 261 size_t num_results,
262 AutocompleteInput::Type* identified_input_type) { 262 AutocompleteInput::Type* identified_input_type) {
263 AutocompleteInput input(text, string16::npos, desired_tld, GURL(), 263 AutocompleteInput input(text, string16::npos, desired_tld, GURL(),
264 prevent_inline_autocomplete, false, true, 264 prevent_inline_autocomplete, false, true,
265 AutocompleteInput::ALL_MATCHES); 265 AutocompleteInput::ALL_MATCHES);
266 *identified_input_type = input.type(); 266 *identified_input_type = input.type();
267 autocomplete_->Start(input, false); 267 autocomplete_->Start(input, false);
268 if (!autocomplete_->done()) 268 if (!autocomplete_->done())
269 MessageLoop::current()->Run(); 269 base::MessageLoop::current()->Run();
270 270
271 matches_ = autocomplete_->matches(); 271 matches_ = autocomplete_->matches();
272 if (sort_matches_) { 272 if (sort_matches_) {
273 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) 273 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i)
274 i->ComputeStrippedDestinationURL(profile_.get()); 274 i->ComputeStrippedDestinationURL(profile_.get());
275 std::sort(matches_.begin(), matches_.end(), 275 std::sort(matches_.begin(), matches_.end(),
276 &AutocompleteMatch::DestinationSortFunc); 276 &AutocompleteMatch::DestinationSortFunc);
277 matches_.erase(std::unique(matches_.begin(), matches_.end(), 277 matches_.erase(std::unique(matches_.begin(), matches_.end(),
278 &AutocompleteMatch::DestinationsEqual), 278 &AutocompleteMatch::DestinationsEqual),
279 matches_.end()); 279 matches_.end());
280 std::sort(matches_.begin(), matches_.end(), 280 std::sort(matches_.begin(), matches_.end(),
281 &AutocompleteMatch::MoreRelevant); 281 &AutocompleteMatch::MoreRelevant);
282 } 282 }
283 ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text 283 ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text
284 << "\nTLD: \"" << desired_tld << "\""; 284 << "\nTLD: \"" << desired_tld << "\"";
285 for (size_t i = 0; i < num_results; ++i) 285 for (size_t i = 0; i < num_results; ++i)
286 EXPECT_EQ(expected_urls[i], matches_[i].destination_url.spec()); 286 EXPECT_EQ(expected_urls[i], matches_[i].destination_url.spec());
287 } 287 }
288 288
289 void HistoryURLProviderTest::RunAdjustOffsetTest(const string16 text, 289 void HistoryURLProviderTest::RunAdjustOffsetTest(const string16 text,
290 size_t expected_offset) { 290 size_t expected_offset) {
291 AutocompleteInput input(text, string16::npos, string16(), GURL(), false, 291 AutocompleteInput input(text, string16::npos, string16(), GURL(), false,
292 false, true, AutocompleteInput::ALL_MATCHES); 292 false, true, AutocompleteInput::ALL_MATCHES);
293 autocomplete_->Start(input, false); 293 autocomplete_->Start(input, false);
294 if (!autocomplete_->done()) 294 if (!autocomplete_->done())
295 MessageLoop::current()->Run(); 295 base::MessageLoop::current()->Run();
296 296
297 matches_ = autocomplete_->matches(); 297 matches_ = autocomplete_->matches();
298 ASSERT_GE(matches_.size(), 1U) << "Input text: " << text; 298 ASSERT_GE(matches_.size(), 1U) << "Input text: " << text;
299 EXPECT_EQ(expected_offset, matches_[0].inline_autocomplete_offset); 299 EXPECT_EQ(expected_offset, matches_[0].inline_autocomplete_offset);
300 } 300 }
301 301
302 TEST_F(HistoryURLProviderTest, PromoteShorterURLs) { 302 TEST_F(HistoryURLProviderTest, PromoteShorterURLs) {
303 // Test that hosts get synthesized below popular pages. 303 // Test that hosts get synthesized below popular pages.
304 const std::string expected_nonsynth[] = { 304 const std::string expected_nonsynth[] = {
305 "http://slashdot.org/favorite_page.html", 305 "http://slashdot.org/favorite_page.html",
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 // HistoryURLProvider shouldn't be done (waiting on async results). 540 // HistoryURLProvider shouldn't be done (waiting on async results).
541 EXPECT_FALSE(autocomplete_->done()); 541 EXPECT_FALSE(autocomplete_->done());
542 542
543 // We should get back an entry for pandora. 543 // We should get back an entry for pandora.
544 matches_ = autocomplete_->matches(); 544 matches_ = autocomplete_->matches();
545 ASSERT_GT(matches_.size(), 0u); 545 ASSERT_GT(matches_.size(), 0u);
546 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 546 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
547 int pandora_relevance = matches_[0].relevance; 547 int pandora_relevance = matches_[0].relevance;
548 548
549 // Run the message loop. When |autocomplete_| finishes the loop is quit. 549 // Run the message loop. When |autocomplete_| finishes the loop is quit.
550 MessageLoop::current()->Run(); 550 base::MessageLoop::current()->Run();
551 EXPECT_TRUE(autocomplete_->done()); 551 EXPECT_TRUE(autocomplete_->done());
552 matches_ = autocomplete_->matches(); 552 matches_ = autocomplete_->matches();
553 ASSERT_GT(matches_.size(), 0u); 553 ASSERT_GT(matches_.size(), 0u);
554 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 554 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
555 EXPECT_EQ(pandora_relevance, matches_[0].relevance); 555 EXPECT_EQ(pandora_relevance, matches_[0].relevance);
556 } 556 }
557 557
558 TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) { 558 TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) {
559 // Ensure that we will still produce matches for navigation when there is no 559 // Ensure that we will still produce matches for navigation when there is no
560 // database. 560 // database.
561 std::string navigation_1[] = {"http://test.com/"}; 561 std::string navigation_1[] = {"http://test.com/"};
562 RunTest(ASCIIToUTF16("test.com"), string16(), false, navigation_1, 562 RunTest(ASCIIToUTF16("test.com"), string16(), false, navigation_1,
563 arraysize(navigation_1)); 563 arraysize(navigation_1));
564 564
565 std::string navigation_2[] = {"http://slash/"}; 565 std::string navigation_2[] = {"http://slash/"};
566 RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2, 566 RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2,
567 arraysize(navigation_2)); 567 arraysize(navigation_2));
568 568
569 RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0); 569 RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0);
570 } 570 }
571 571
572 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { 572 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) {
573 AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(), 573 AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(),
574 GURL(), false, false, true, 574 GURL(), false, false, true,
575 AutocompleteInput::ALL_MATCHES); 575 AutocompleteInput::ALL_MATCHES);
576 autocomplete_->Start(input, false); 576 autocomplete_->Start(input, false);
577 if (!autocomplete_->done()) 577 if (!autocomplete_->done())
578 MessageLoop::current()->Run(); 578 base::MessageLoop::current()->Run();
579 579
580 // None of the matches should attempt to autocomplete. 580 // None of the matches should attempt to autocomplete.
581 matches_ = autocomplete_->matches(); 581 matches_ = autocomplete_->matches();
582 for (size_t i = 0; i < matches_.size(); ++i) 582 for (size_t i = 0; i < matches_.size(); ++i)
583 EXPECT_EQ(string16::npos, matches_[i].inline_autocomplete_offset); 583 EXPECT_EQ(string16::npos, matches_[i].inline_autocomplete_offset);
584 } 584 }
585 585
586 TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) { 586 TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) {
587 // Visiting foo.com should not make this string be treated as a navigation. 587 // Visiting foo.com should not make this string be treated as a navigation.
588 // That means the result should be scored around 1200 ("what you typed") 588 // That means the result should be scored around 1200 ("what you typed")
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 const char* const test_cases[] = { 736 const char* const test_cases[] = {
737 "//c", 737 "//c",
738 "\\@st" 738 "\\@st"
739 }; 739 };
740 for (size_t i = 0; i < arraysize(test_cases); ++i) { 740 for (size_t i = 0; i < arraysize(test_cases); ++i) {
741 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos, 741 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos,
742 string16(), GURL(), false, false, true, 742 string16(), GURL(), false, false, true,
743 AutocompleteInput::ALL_MATCHES); 743 AutocompleteInput::ALL_MATCHES);
744 autocomplete_->Start(input, false); 744 autocomplete_->Start(input, false);
745 if (!autocomplete_->done()) 745 if (!autocomplete_->done())
746 MessageLoop::current()->Run(); 746 base::MessageLoop::current()->Run();
747 } 747 }
748 } 748 }
749 749
750 TEST_F(HistoryURLProviderTest, CullSearchResults) { 750 TEST_F(HistoryURLProviderTest, CullSearchResults) {
751 // Set up a default search engine. 751 // Set up a default search engine.
752 TemplateURLData data; 752 TemplateURLData data;
753 data.SetKeyword(ASCIIToUTF16("TestEngine")); 753 data.SetKeyword(ASCIIToUTF16("TestEngine"));
754 data.SetURL("http://testsearch.com/?q={searchTerms}"); 754 data.SetURL("http://testsearch.com/?q={searchTerms}");
755 TemplateURLService* template_url_service = 755 TemplateURLService* template_url_service =
756 TemplateURLServiceFactory::GetForProfile(profile_.get()); 756 TemplateURLServiceFactory::GetForProfile(profile_.get());
(...skipping 27 matching lines...) Expand all
784 RunTest(ASCIIToUTF16("foobar"), string16(), true, 784 RunTest(ASCIIToUTF16("foobar"), string16(), true,
785 expected_when_searching_query, arraysize(expected_when_searching_query)); 785 expected_when_searching_query, arraysize(expected_when_searching_query));
786 786
787 // We should not see search URLs when typing the search engine name. 787 // We should not see search URLs when typing the search engine name.
788 const std::string expected_when_searching_site[] = { 788 const std::string expected_when_searching_site[] = {
789 test_cases[0].url 789 test_cases[0].url
790 }; 790 };
791 RunTest(ASCIIToUTF16("testsearch"), string16(), true, 791 RunTest(ASCIIToUTF16("testsearch"), string16(), true,
792 expected_when_searching_site, arraysize(expected_when_searching_site)); 792 expected_when_searching_site, arraysize(expected_when_searching_site));
793 } 793 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/history_url_provider.cc ('k') | chrome/browser/autocomplete/search_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698