This source file includes following definitions.
- CreateTemplateURLService
- SetUp
- RunTest
- SetUp
- OnProviderUpdate
- SetUpImpl
- TearDown
- FillData
- RunTest
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/autocomplete/history_url_provider.h"
#include <algorithm>
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/autocomplete_provider.h"
#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
#include "chrome/browser/autocomplete/history_quick_provider.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
using base::Time;
using base::TimeDelta;
using content::TestBrowserThreadBundle;
struct TestURLInfo {
const char* url;
const char* title;
int visit_count;
int typed_count;
int age_in_days;
} test_db[] = {
{"http://www.google.com/", "Google", 3, 3, 80},
{"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 80},
{"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 80},
{"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0, 80},
{"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 80},
{"http://news.google.com/", "Google News", 1, 1, 80},
{"http://synthesisatest.com/foo/", "Test A", 1, 1, 80},
{"http://synthesisbtest.com/foo/", "Test B", 1, 1, 80},
{"http://synthesisbtest.com/foo/bar.html", "Test B Bar", 2, 2, 80},
{"http://foo.com/", "Dir", 5, 5, 80},
{"http://foo.com/dir/", "Dir", 2, 2, 80},
{"http://foo.com/dir/another/", "Dir", 5, 1, 80},
{"http://foo.com/dir/another/again/", "Dir", 10, 0, 80},
{"http://foo.com/dir/another/again/myfile.html", "File", 10, 2, 80},
{"http://startest.com/y/a", "A", 2, 2, 80},
{"http://startest.com/y/b", "B", 5, 2, 80},
{"http://startest.com/x/c", "C", 5, 2, 80},
{"http://startest.com/x/d", "D", 5, 5, 80},
{"http://startest.com/y/e", "E", 4, 2, 80},
{"http://startest.com/y/f", "F", 3, 2, 80},
{"http://startest.com/y/g", "G", 3, 2, 80},
{"http://startest.com/y/h", "H", 3, 2, 80},
{"http://startest.com/y/i", "I", 3, 2, 80},
{"http://startest.com/y/j", "J", 3, 2, 80},
{"http://startest.com/y/k", "K", 3, 2, 80},
{"http://startest.com/y/l", "L", 3, 2, 80},
{"http://startest.com/y/m", "M", 3, 2, 80},
{"file:///C:/foo.txt", "", 2, 2, 80},
{"http://bogussite.com/a", "Bogus A", 10002, 10000, 80},
{"http://bogussite.com/b", "Bogus B", 10001, 10000, 80},
{"http://bogussite.com/c", "Bogus C", 10000, 10000, 80},
{"http://www.17173.com/", "Domain with number", 3, 3, 80},
{"http://go/", "Intranet URL", 1, 1, 80},
{"http://gooey/", "Intranet URL 2", 5, 5, 80},
{"http://www.\xEA\xB5\x90\xEC\x9C\xA1.kr/", "Korean", 2, 2, 80},
{"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 80},
{"http://ms/c++%20style%20guide", "Style guide", 2, 2, 80},
{"http://binky/", "Intranet binky", 2, 2, 80},
{"http://winky/", "Intranet winky", 2, 2, 80},
{"http://www.winky.com/", "Internet winky", 5, 0, 80},
{"http://pandora.com/", "Pandora", 2, 2, 80},
{"http://intra/one", "Intranet", 2, 2, 80},
{"http://intra/two", "Intranet two", 1, 1, 80},
{"http://intra/three", "Intranet three", 2, 2, 80},
{"http://moo/bar", "Intranet moo", 1, 1, 80},
{"http://typedhost/typedpath", "Intranet typed", 1, 1, 80},
{"http://typedhost/untypedpath", "Intranet untyped", 1, 0, 80},
{"http://x.com/one", "Internet", 2, 2, 80},
{"http://x.com/two", "Internet two", 1, 1, 80},
{"http://x.com/three", "Internet three", 2, 2, 80},
{"http://7.com/1a", "One", 8, 4, 4},
{"http://7.com/2a", "Two A", 4, 2, 8},
{"http://7.com/2b", "Two B", 4, 1, 8},
{"http://7.com/3a", "Three", 2, 1, 16},
{"http://7.com/4a", "Four A", 1, 1, 32},
{"http://7.com/4b", "Four B", 1, 1, 64},
{"http://7.com/5a", "Five A", 8, 0, 64},
};
class HistoryURLProviderTest : public testing::Test,
public AutocompleteProviderListener {
public:
struct UrlAndLegalDefault {
std::string url;
bool allowed_to_be_default_match;
};
HistoryURLProviderTest()
: sort_matches_(false) {
HistoryQuickProvider::set_disabled(true);
}
virtual ~HistoryURLProviderTest() {
HistoryQuickProvider::set_disabled(false);
}
virtual void OnProviderUpdate(bool updated_matches) OVERRIDE;
protected:
static KeyedService* CreateTemplateURLService(
content::BrowserContext* profile) {
return new TemplateURLService(static_cast<Profile*>(profile));
}
virtual void SetUp() {
ASSERT_TRUE(SetUpImpl(false));
}
virtual void TearDown();
bool SetUpImpl(bool no_db) WARN_UNUSED_RESULT;
void FillData();
void RunTest(const base::string16 text,
const base::string16& desired_tld,
bool prevent_inline_autocomplete,
const UrlAndLegalDefault* expected_urls,
size_t num_results,
AutocompleteInput::Type* identified_input_type);
void RunTest(const base::string16 text,
const base::string16& desired_tld,
bool prevent_inline_autocomplete,
const UrlAndLegalDefault* expected_urls,
size_t num_results) {
AutocompleteInput::Type type;
return RunTest(text, desired_tld, prevent_inline_autocomplete,
expected_urls, num_results, &type);
}
content::TestBrowserThreadBundle thread_bundle_;
ACMatches matches_;
scoped_ptr<TestingProfile> profile_;
HistoryService* history_service_;
scoped_refptr<HistoryURLProvider> autocomplete_;
bool sort_matches_;
};
class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
protected:
virtual void SetUp() {
ASSERT_TRUE(SetUpImpl(true));
}
};
void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
if (autocomplete_->done())
base::MessageLoop::current()->Quit();
}
bool HistoryURLProviderTest::SetUpImpl(bool no_db) {
profile_.reset(new TestingProfile());
if (!(profile_->CreateHistoryService(true, no_db)))
return false;
if (!no_db) {
profile_->BlockUntilHistoryProcessesPendingRequests();
profile_->BlockUntilHistoryIndexIsRefreshed();
}
profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US,en,ko");
history_service_ = HistoryServiceFactory::GetForProfile(
profile_.get(), Profile::EXPLICIT_ACCESS);
autocomplete_ = new HistoryURLProvider(this, profile_.get());
TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), &HistoryURLProviderTest::CreateTemplateURLService);
FillData();
return true;
}
void HistoryURLProviderTest::TearDown() {
autocomplete_ = NULL;
}
void HistoryURLProviderTest::FillData() {
const Time now = Time::Now();
for (size_t i = 0; i < arraysize(test_db); ++i) {
const TestURLInfo& cur = test_db[i];
const GURL current_url(cur.url);
history_service_->AddPageWithDetails(
current_url, base::UTF8ToUTF16(cur.title), cur.visit_count,
cur.typed_count, now - TimeDelta::FromDays(cur.age_in_days), false,
history::SOURCE_BROWSED);
}
history_service_->AddPageWithDetails(
GURL("http://p/"), base::UTF8ToUTF16("p"), 0, 0,
Time::Now() -
TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1),
false, history::SOURCE_BROWSED);
}
void HistoryURLProviderTest::RunTest(
const base::string16 text,
const base::string16& desired_tld,
bool prevent_inline_autocomplete,
const UrlAndLegalDefault* expected_urls,
size_t num_results,
AutocompleteInput::Type* identified_input_type) {
AutocompleteInput input(text, base::string16::npos, desired_tld, GURL(),
AutocompleteInput::INVALID_SPEC,
prevent_inline_autocomplete, false, true,
AutocompleteInput::ALL_MATCHES);
*identified_input_type = input.type();
autocomplete_->Start(input, false);
if (!autocomplete_->done())
base::MessageLoop::current()->Run();
matches_ = autocomplete_->matches();
if (sort_matches_) {
for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i)
i->ComputeStrippedDestinationURL(profile_.get());
std::sort(matches_.begin(), matches_.end(),
&AutocompleteMatch::DestinationSortFunc);
matches_.erase(std::unique(matches_.begin(), matches_.end(),
&AutocompleteMatch::DestinationsEqual),
matches_.end());
std::sort(matches_.begin(), matches_.end(),
&AutocompleteMatch::MoreRelevant);
}
ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text
<< "\nTLD: \"" << desired_tld << "\"";
for (size_t i = 0; i < num_results; ++i) {
EXPECT_EQ(expected_urls[i].url, matches_[i].destination_url.spec());
EXPECT_EQ(expected_urls[i].allowed_to_be_default_match,
matches_[i].allowed_to_be_default_match);
}
}
TEST_F(HistoryURLProviderTest, PromoteShorterURLs) {
const UrlAndLegalDefault expected_nonsynth[] = {
{ "http://slashdot.org/favorite_page.html", false },
{ "http://slashdot.org/", false }
};
RunTest(ASCIIToUTF16("slash"), base::string16(), true, expected_nonsynth,
arraysize(expected_nonsynth));
const UrlAndLegalDefault expected_synth[] = {
{ "http://kerneltrap.org/", false },
{ "http://kerneltrap.org/not_very_popular.html", false }
};
RunTest(ASCIIToUTF16("kernel"), base::string16(), true, expected_synth,
arraysize(expected_synth));
RunTest(ASCIIToUTF16("fresh"), base::string16(), true, NULL, 0);
const UrlAndLegalDefault expected_synthesisa[] = {
{ "http://synthesisatest.com/", true },
{ "http://synthesisatest.com/foo/", true }
};
RunTest(ASCIIToUTF16("synthesisa"), base::string16(), false,
expected_synthesisa, arraysize(expected_synthesisa));
EXPECT_LT(matches_.front().relevance, 1200);
const UrlAndLegalDefault expected_synthesisb[] = {
{ "http://synthesisbtest.com/foo/", true },
{ "http://synthesisbtest.com/foo/bar.html", true }
};
RunTest(ASCIIToUTF16("synthesisb"), base::string16(), false,
expected_synthesisb, arraysize(expected_synthesisb));
EXPECT_GE(matches_.front().relevance, 1410);
const UrlAndLegalDefault expected_combine[] = {
{ "http://news.google.com/", false },
{ "http://news.google.com/?ned=us&topic=n", false },
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), base::string16(), true,
expected_combine, arraysize(expected_combine)));
EXPECT_EQ(ASCIIToUTF16("Google News"), matches_.front().description);
const UrlAndLegalDefault short_1[] = {
{ "http://foo.com/", false },
{ "http://foo.com/dir/another/again/myfile.html", false },
{ "http://foo.com/dir/", false }
};
RunTest(ASCIIToUTF16("foo"), base::string16(), true,
short_1, arraysize(short_1));
const UrlAndLegalDefault short_2[] = {
{ "http://foo.com/", true },
{ "http://foo.com/dir/another/again/myfile.html", false },
{ "http://foo.com/dir/", false },
{ "http://foo.com/dir/another/", false }
};
RunTest(ASCIIToUTF16("foo.com"), base::string16(), true, short_2,
arraysize(short_2));
RunTest(ASCIIToUTF16("foo.com/"), base::string16(), true, short_2,
arraysize(short_2));
const UrlAndLegalDefault short_3[] = {
{ "http://foo.com/d", true },
{ "http://foo.com/dir/another/", false },
{ "http://foo.com/dir/another/again/myfile.html", false },
{ "http://foo.com/dir/", false }
};
RunTest(ASCIIToUTF16("foo.com/d"), base::string16(), true, short_3,
arraysize(short_3));
const UrlAndLegalDefault short_4[] = {
{ "http://foo.com/dir/another/a", true },
{ "http://foo.com/dir/another/again/myfile.html", false },
{ "http://foo.com/dir/another/again/", false }
};
RunTest(ASCIIToUTF16("foo.com/dir/another/a"), base::string16(), true,
short_4, arraysize(short_4));
const UrlAndLegalDefault short_5a[] = {
{ "http://gooey/", true },
{ "http://www.google.com/", true },
{ "http://go/", true }
};
const UrlAndLegalDefault short_5b[] = {
{ "http://go/", true },
{ "http://gooey/", true },
{ "http://www.google.com/", true }
};
RunTest(ASCIIToUTF16("g"), base::string16(), false,
short_5a, arraysize(short_5a));
RunTest(ASCIIToUTF16("go"), base::string16(), false,
short_5b, arraysize(short_5b));
}
TEST_F(HistoryURLProviderTest, CullRedirects) {
struct TestCase {
const char* url;
int count;
} test_cases[] = {
{"http://redirects/A", 30},
{"http://redirects/B", 20},
{"http://redirects/C", 10}
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
history_service_->AddPageWithDetails(GURL(test_cases[i].url),
ASCIIToUTF16("Title"), test_cases[i].count, test_cases[i].count,
Time::Now(), false, history::SOURCE_BROWSED);
}
history::RedirectList redirects_to_a;
redirects_to_a.push_back(GURL(test_cases[1].url));
redirects_to_a.push_back(GURL(test_cases[2].url));
redirects_to_a.push_back(GURL(test_cases[0].url));
history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(),
NULL, 0, GURL(), redirects_to_a, content::PAGE_TRANSITION_TYPED,
history::SOURCE_BROWSED, true);
const base::string16 typing(ASCIIToUTF16("http://redirects/"));
const UrlAndLegalDefault expected_results[] = {
{ base::UTF16ToUTF8(typing), true },
{ test_cases[0].url, false }
};
RunTest(typing, base::string16(), true, expected_results,
arraysize(expected_results));
}
TEST_F(HistoryURLProviderTest, WhatYouTyped) {
RunTest(ASCIIToUTF16("wytmatch"), base::string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("wytmatch foo bar"), base::string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("wytmatch+foo+bar"), base::string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), base::string16(), false,
NULL, 0);
const UrlAndLegalDefault results_1[] = {
{ "http://www.wytmatch.com/", true }
};
RunTest(ASCIIToUTF16("wytmatch"), ASCIIToUTF16("com"), false, results_1,
arraysize(results_1));
const UrlAndLegalDefault results_2[] = {
{ "http://wytmatch%20foo%20bar/", true }
};
RunTest(ASCIIToUTF16("http://wytmatch foo bar"), base::string16(), false,
results_2, arraysize(results_2));
const UrlAndLegalDefault results_3[] = {
{ "https://wytmatch%20foo%20bar/", true }
};
RunTest(ASCIIToUTF16("https://wytmatch foo bar"), base::string16(), false,
results_3, arraysize(results_3));
}
TEST_F(HistoryURLProviderTest, Fixup) {
RunTest(ASCIIToUTF16("\\"), base::string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("#"), base::string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("%20"), base::string16(), false, NULL, 0);
const UrlAndLegalDefault fixup_crash[] = {
{ "http://%EF%BD%A5@s/", true }
};
RunTest(base::WideToUTF16(L"\uff65@s"), base::string16(), false, fixup_crash,
arraysize(fixup_crash));
RunTest(base::WideToUTF16(L"\u2015\u2015@ \uff7c"), base::string16(), false,
NULL, 0);
const base::string16 input_1(ASCIIToUTF16("file:"));
const UrlAndLegalDefault fixup_1[] = {
{ "file:///C:/foo.txt", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(input_1, base::string16(), false, fixup_1,
arraysize(fixup_1)));
EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
matches_.front().inline_autocompletion);
const base::string16 input_2(ASCIIToUTF16("http:/"));
const UrlAndLegalDefault fixup_2[] = {
{ "http://bogussite.com/a", true },
{ "http://bogussite.com/b", true },
{ "http://bogussite.com/c", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(input_2, base::string16(), false, fixup_2,
arraysize(fixup_2)));
EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
matches_.front().inline_autocompletion);
const UrlAndLegalDefault fixup_3[] = {
{ "http://www.56.com/", true }
};
RunTest(ASCIIToUTF16("56"), ASCIIToUTF16("com"), true, fixup_3,
arraysize(fixup_3));
const UrlAndLegalDefault fixup_4[] = {
{ "http://127.0.0.1/", true }
};
RunTest(ASCIIToUTF16("127.0.0.1"), base::string16(), false, fixup_4,
arraysize(fixup_4));
const UrlAndLegalDefault fixup_5[] = {
{ "http://www.17173.com/", true }
};
RunTest(ASCIIToUTF16("17173"), base::string16(), false, fixup_5,
arraysize(fixup_5));
}
TEST_F(HistoryURLProviderTest, EmptyVisits) {
profile_->BlockUntilHistoryProcessesPendingRequests();
AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos,
base::string16(), GURL(),
AutocompleteInput::INVALID_SPEC, false, false, true,
AutocompleteInput::ALL_MATCHES);
autocomplete_->Start(input, false);
EXPECT_FALSE(autocomplete_->done());
matches_ = autocomplete_->matches();
ASSERT_GT(matches_.size(), 0u);
EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
int pandora_relevance = matches_[0].relevance;
base::MessageLoop::current()->Run();
EXPECT_TRUE(autocomplete_->done());
matches_ = autocomplete_->matches();
ASSERT_GT(matches_.size(), 0u);
EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
EXPECT_EQ(pandora_relevance, matches_[0].relevance);
}
TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) {
UrlAndLegalDefault navigation_1[] = {
{ "http://test.com/", true }
};
RunTest(ASCIIToUTF16("test.com"), base::string16(), false, navigation_1,
arraysize(navigation_1));
UrlAndLegalDefault navigation_2[] = {
{ "http://slash/", true }
};
RunTest(ASCIIToUTF16("slash"), base::string16(), false, navigation_2,
arraysize(navigation_2));
RunTest(ASCIIToUTF16("this is a query"), base::string16(), false, NULL, 0);
}
TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) {
AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos,
base::string16(), GURL(),
AutocompleteInput::INVALID_SPEC, false, false,
true, AutocompleteInput::ALL_MATCHES);
autocomplete_->Start(input, false);
if (!autocomplete_->done())
base::MessageLoop::current()->Run();
matches_ = autocomplete_->matches();
for (size_t i = 0; i < matches_.size(); ++i) {
EXPECT_TRUE(matches_[i].inline_autocompletion.empty());
EXPECT_FALSE(matches_[i].allowed_to_be_default_match);
}
}
TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) {
const UrlAndLegalDefault expected[] = {
{ "http://user@foo.com/", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"),
base::string16(), false, expected,
arraysize(expected)));
EXPECT_LE(1200, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1210);
}
TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) {
struct TestCase {
const char* input;
int relevance;
} test_cases[] = {
{ "fooey", 0 },
{ "fooey/", 1200 },
{ "fooey/a", 1200 },
{ "fooey/a b", 1200 },
{ "gooey", 1410 },
{ "gooey/", 1410 },
{ "gooey/a", 1400 },
{ "gooey/a b", 1400 },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
SCOPED_TRACE(test_cases[i].input);
if (test_cases[i].relevance == 0) {
RunTest(ASCIIToUTF16(test_cases[i].input), base::string16(), false,
NULL, 0);
} else {
const UrlAndLegalDefault output[] = {
{ URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(),
true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
base::string16(), false,
output, arraysize(output)));
EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
}
}
}
TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) {
struct TestCase {
const char* input;
int relevance;
AutocompleteInput::Type type;
} test_cases[] = {
{ "gooey", 1410, AutocompleteInput::UNKNOWN },
{ "gooey/", 1410, AutocompleteInput::URL },
{ "gooey#", 1200, AutocompleteInput::UNKNOWN },
{ "gooey/#", 1200, AutocompleteInput::URL },
{ "gooey#foo", 1200, AutocompleteInput::UNKNOWN },
{ "gooey/#foo", 1200, AutocompleteInput::URL },
{ "gooey# foo", 1200, AutocompleteInput::UNKNOWN },
{ "gooey/# foo", 1200, AutocompleteInput::URL },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
SCOPED_TRACE(test_cases[i].input);
const UrlAndLegalDefault output[] = {
{ URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(),
true }
};
AutocompleteInput::Type type;
ASSERT_NO_FATAL_FAILURE(
RunTest(ASCIIToUTF16(test_cases[i].input),
base::string16(), false, output, arraysize(output), &type));
EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
EXPECT_EQ(test_cases[i].type, type);
}
}
TEST_F(HistoryURLProviderTest, IntranetURLCompletion) {
sort_matches_ = true;
const UrlAndLegalDefault expected1[] = {
{ "http://intra/three", true },
{ "http://intra/two", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), base::string16(),
false, expected1, arraysize(expected1)));
EXPECT_LE(1410, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1420);
EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance);
const UrlAndLegalDefault expected2[] = {
{ "http://moo/b", true },
{ "http://moo/bar", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), base::string16(),
false, expected2, arraysize(expected2)));
EXPECT_LE(1400, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1410);
const UrlAndLegalDefault expected3[] = {
{ "http://intra/one", true },
{ "http://intra/three", true },
{ "http://intra/two", true }
};
RunTest(ASCIIToUTF16("intra"), base::string16(), false, expected3,
arraysize(expected3));
const UrlAndLegalDefault expected4[] = {
{ "http://intra/one", true },
{ "http://intra/three", true },
{ "http://intra/two", true }
};
RunTest(ASCIIToUTF16("intra/"), base::string16(), false, expected4,
arraysize(expected4));
const UrlAndLegalDefault expected5[] = {
{ "http://intra/one", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), base::string16(),
false, expected5, arraysize(expected5)));
EXPECT_LE(1410, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1420);
const UrlAndLegalDefault expected6[] = {
{ "http://intra/x", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), base::string16(),
false, expected6, arraysize(expected6)));
EXPECT_LE(1400, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1410);
const UrlAndLegalDefault expected7[] = {
{ "http://typedhost/untypedpath", true }
};
ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
base::string16(), false, expected7, arraysize(expected7)));
EXPECT_LE(1400, matches_[0].relevance);
EXPECT_LT(matches_[0].relevance, 1410);
}
TEST_F(HistoryURLProviderTest, CrashDueToFixup) {
const char* const test_cases[] = {
"//c",
"\\@st",
"view-source:x",
};
for (size_t i = 0; i < arraysize(test_cases); ++i) {
AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos,
base::string16(), GURL(),
AutocompleteInput::INVALID_SPEC,
false, false, true, AutocompleteInput::ALL_MATCHES);
autocomplete_->Start(input, false);
if (!autocomplete_->done())
base::MessageLoop::current()->Run();
}
}
TEST_F(HistoryURLProviderTest, CullSearchResults) {
TemplateURLData data;
data.SetKeyword(ASCIIToUTF16("TestEngine"));
data.SetURL("http://testsearch.com/?q={searchTerms}");
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_.get());
TemplateURL* template_url = new TemplateURL(profile_.get(), data);
template_url_service->Add(template_url);
template_url_service->SetDefaultSearchProvider(template_url);
template_url_service->Load();
struct TestCase {
const char* url;
int count;
} test_cases[] = {
{"https://testsearch.com/", 30},
{"https://testsearch.com/?q=foobar", 20},
{"http://foobar.com/", 10}
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
history_service_->AddPageWithDetails(GURL(test_cases[i].url),
base::UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
Time::Now(), false, history::SOURCE_BROWSED);
}
const UrlAndLegalDefault expected_when_searching_query[] = {
{ test_cases[2].url, false }
};
RunTest(ASCIIToUTF16("foobar"), base::string16(), true,
expected_when_searching_query, arraysize(expected_when_searching_query));
const UrlAndLegalDefault expected_when_searching_site[] = {
{ test_cases[0].url, false }
};
RunTest(ASCIIToUTF16("testsearch"), base::string16(), true,
expected_when_searching_site, arraysize(expected_when_searching_site));
}
TEST_F(HistoryURLProviderTest, SuggestExactInput) {
const size_t npos = std::string::npos;
struct TestCase {
const char* input;
bool trim_http;
const char* contents;
size_t offsets[3];
size_t match_classification_index;
} test_cases[] = {
{ "http://www.somesite.com", false,
"http://www.somesite.com", {0, npos, npos}, 0 },
{ "www.somesite.com", true,
"www.somesite.com", {0, npos, npos}, 0 },
{ "www.somesite.com", false,
"http://www.somesite.com", {0, 7, npos}, 1 },
{ "somesite.com", true,
"somesite.com", {0, npos, npos}, 0 },
{ "somesite.com", false,
"http://somesite.com", {0, 7, npos}, 1 },
{ "w", true,
"w", {0, npos, npos}, 0 },
{ "w", false,
"http://w", {0, 7, npos}, 1 },
{ "w.com", true,
"w.com", {0, npos, npos}, 0 },
{ "w.com", false,
"http://w.com", {0, 7, npos}, 1 },
{ "www.w.com", true,
"www.w.com", {0, npos, npos}, 0 },
{ "www.w.com", false,
"http://www.w.com", {0, 7, npos}, 1 },
{ "view-source:w", true,
"view-source:w", {0, npos, npos}, 0 },
{ "view-source:www.w.com/", true,
"view-source:www.w.com", {0, npos, npos}, npos },
{ "view-source:www.w.com/", false,
"view-source:http://www.w.com", {0, npos, npos}, npos },
{ "view-source:http://www.w.com/", false,
"view-source:http://www.w.com", {0, npos, npos}, 0 },
{ " view-source:", true,
"view-source:", {0, npos, npos}, 0 },
{ "http:////////w.com", false,
"http://w.com", {0, npos, npos}, npos },
{ " http:////////www.w.com", false,
"http://www.w.com", {0, npos, npos}, npos },
{ "http:a///www.w.com", false,
"http://a///www.w.com", {0, npos, npos}, npos },
{ "mailto://a@b.com", true,
"mailto://a@b.com", {0, npos, npos}, 0 },
{ "mailto://a@b.com", false,
"mailto://a@b.com", {0, npos, npos}, 0 },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
SCOPED_TRACE(testing::Message() << "Index " << i << " input: "
<< test_cases[i].input << ", trim_http: "
<< test_cases[i].trim_http);
AutocompleteInput input(ASCIIToUTF16(test_cases[i].input),
base::string16::npos, base::string16(),
GURL("about:blank"),
AutocompleteInput::INVALID_SPEC, false, false, true,
AutocompleteInput::ALL_MATCHES);
AutocompleteMatch match(autocomplete_->SuggestExactInput(
input.text(), input.canonicalized_url(), test_cases[i].trim_http));
EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents);
for (size_t match_index = 0; match_index < match.contents_class.size();
++match_index) {
EXPECT_EQ(test_cases[i].offsets[match_index],
match.contents_class[match_index].offset);
EXPECT_EQ(ACMatchClassification::URL |
(match_index == test_cases[i].match_classification_index ?
ACMatchClassification::MATCH : 0),
match.contents_class[match_index].style);
}
EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]);
}
}
TEST_F(HistoryURLProviderTest, HUPScoringExperiment) {
HUPScoringParams max_2000_no_time_decay;
max_2000_no_time_decay.typed_count_buckets.buckets().push_back(
std::make_pair(0.0, 2000));
HUPScoringParams max_1250_no_time_decay;
max_1250_no_time_decay.typed_count_buckets.buckets().push_back(
std::make_pair(0.0, 1250));
HUPScoringParams max_1000_no_time_decay;
max_1000_no_time_decay.typed_count_buckets.buckets().push_back(
std::make_pair(0.0, 1000));
HUPScoringParams max_1100_with_time_decay_and_max_cap;
max_1100_with_time_decay_and_max_cap.typed_count_buckets.
set_relevance_cap(1400);
max_1100_with_time_decay_and_max_cap.typed_count_buckets.
set_half_life_days(16);
max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
std::make_pair(0.5, 1100));
max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
std::make_pair(0.24, 200));
max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
std::make_pair(0.0, 100));
HUPScoringParams max_1100_visit_typed_decays;
max_1100_visit_typed_decays.typed_count_buckets.set_half_life_days(16);
max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
std::make_pair(0.5, 1100));
max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
std::make_pair(0.0, 100));
max_1100_visit_typed_decays.visited_count_buckets.set_half_life_days(16);
max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
std::make_pair(0.5, 550));
max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
std::make_pair(0.0, 50));
const int kMaxMatches = 3;
struct TestCase {
const char* input;
HUPScoringParams scoring_params;
struct ExpectedMatch {
const char* url;
int control_relevance;
int experiment_relevance;
};
ExpectedMatch matches[kMaxMatches];
} test_cases[] = {
{ "7.com/1", max_2000_no_time_decay,
{{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
{ "7.com/1", max_1250_no_time_decay,
{{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
{ "7.com/2", max_1250_no_time_decay,
{{"7.com/2a", 1413, 1413}, {"7.com/2b", 1412, 1250}, {NULL, 0, 0}} },
{ "7.com/4", max_1000_no_time_decay,
{{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 1000},
{"7.com/4b", 1201, 999}} },
{ "7.com/1", max_1100_with_time_decay_and_max_cap,
{{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
{ "7.com/4", max_1100_with_time_decay_and_max_cap,
{{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 200},
{"7.com/4b", 1201, 100}} },
{ "7.com/5", max_1100_visit_typed_decays,
{{"7.com/5", 1203, 1203}, {"7.com/5a", 1202, 50}, {NULL, 0, 0}} },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
SCOPED_TRACE(test_cases[i].input);
UrlAndLegalDefault output[kMaxMatches];
int max_matches;
for (max_matches = 0; max_matches < kMaxMatches; ++max_matches) {
if (test_cases[i].matches[max_matches].url == NULL)
break;
output[max_matches].url = URLFixerUpper::FixupURL(
test_cases[i].matches[max_matches].url, std::string()).spec();
output[max_matches].allowed_to_be_default_match = true;
}
autocomplete_->scoring_params_ = test_cases[i].scoring_params;
autocomplete_->scoring_params_.experimental_scoring_enabled = false;
ASSERT_NO_FATAL_FAILURE(
RunTest(ASCIIToUTF16(test_cases[i].input),
base::string16(), false, output, max_matches));
for (int j = 0; j < max_matches; ++j) {
EXPECT_EQ(test_cases[i].matches[j].control_relevance,
matches_[j].relevance);
}
autocomplete_->scoring_params_.experimental_scoring_enabled = true;
ASSERT_NO_FATAL_FAILURE(
RunTest(ASCIIToUTF16(test_cases[i].input),
base::string16(), false, output, max_matches));
for (int j = 0; j < max_matches; ++j) {
EXPECT_EQ(test_cases[i].matches[j].experiment_relevance,
matches_[j].relevance);
}
}
}