This source file includes following definitions.
- GoogleBaseURLValue
- CreateKeywordWithDate
- AddKeywordWithDate
- ExpectSimilar
- model
- TestGenerateSearchURL
- SetUp
- TearDown
- AddKeywordWithDate
- AssertEquals
- CreatePreloadedTemplateURL
- CreateReplaceablePreloadedTemplateURL
- TestLoadUpdatingPreloadedURL
- VerifyObserverCount
- VerifyObserverFired
- 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
- 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
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/mock_time_provider.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/search_engines/search_host_to_urls_map.h"
#include "chrome/browser/search_engines/search_terms_data.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "chrome/browser/webdata/web_data_service_factory.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/webdata/common/web_database.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
using base::Time;
using base::TimeDelta;
using content::BrowserThread;
using ::testing::Return;
using ::testing::StrictMock;
namespace {
class TestSearchTermsData : public SearchTermsData {
public:
explicit TestSearchTermsData(const char* google_base_url);
virtual std::string GoogleBaseURLValue() const OVERRIDE;
private:
std::string google_base_url_;
DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
};
TestSearchTermsData::TestSearchTermsData(const char* google_base_url)
: google_base_url_(google_base_url) {
}
std::string TestSearchTermsData::GoogleBaseURLValue() const {
return google_base_url_;
}
struct QueryHistoryCallbackImpl {
QueryHistoryCallbackImpl() : success(false) {}
void Callback(HistoryService::Handle handle,
bool success,
const history::URLRow* row,
history::VisitVector* visits) {
this->success = success;
if (row)
this->row = *row;
if (visits)
this->visits = *visits;
}
bool success;
history::URLRow row;
history::VisitVector visits;
};
TemplateURL* CreateKeywordWithDate(
TemplateURLService* model,
const std::string& short_name,
const std::string& keyword,
const std::string& url,
const std::string& suggest_url,
const std::string& alternate_url,
const std::string& favicon_url,
bool safe_for_autoreplace,
const std::string& encodings,
Time date_created,
Time last_modified) {
TemplateURLData data;
data.short_name = base::UTF8ToUTF16(short_name);
data.SetKeyword(base::UTF8ToUTF16(keyword));
data.SetURL(url);
data.suggestions_url = suggest_url;
if (!alternate_url.empty())
data.alternate_urls.push_back(alternate_url);
data.favicon_url = GURL(favicon_url);
data.safe_for_autoreplace = safe_for_autoreplace;
base::SplitString(encodings, ';', &data.input_encodings);
data.date_created = date_created;
data.last_modified = last_modified;
return new TemplateURL(model->profile(), data);
}
TemplateURL* AddKeywordWithDate(
TemplateURLService* model,
const std::string& short_name,
const std::string& keyword,
const std::string& url,
const std::string& suggest_url,
const std::string& alternate_url,
const std::string& favicon_url,
bool safe_for_autoreplace,
const std::string& encodings,
Time date_created,
Time last_modified) {
TemplateURL* t_url = CreateKeywordWithDate(
model, short_name, keyword, url, suggest_url, alternate_url,favicon_url,
safe_for_autoreplace, encodings, date_created, last_modified);
model->Add(t_url);
EXPECT_NE(0, t_url->id());
return t_url;
}
void ExpectSimilar(const TemplateURL* expected, const TemplateURL* actual) {
ASSERT_TRUE(expected != NULL);
ASSERT_TRUE(actual != NULL);
EXPECT_EQ(expected->short_name(), actual->short_name());
EXPECT_EQ(expected->keyword(), actual->keyword());
EXPECT_EQ(expected->url(), actual->url());
EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url());
EXPECT_EQ(expected->favicon_url(), actual->favicon_url());
EXPECT_EQ(expected->alternate_urls(), actual->alternate_urls());
EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list());
EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace());
EXPECT_EQ(expected->input_encodings(), actual->input_encodings());
EXPECT_EQ(expected->search_terms_replacement_key(),
actual->search_terms_replacement_key());
}
}
class TemplateURLServiceTest : public testing::Test {
public:
TemplateURLServiceTest();
virtual void SetUp();
virtual void TearDown();
TemplateURL* AddKeywordWithDate(const std::string& short_name,
const std::string& keyword,
const std::string& url,
const std::string& suggest_url,
const std::string& alternate_url,
const std::string& favicon_url,
bool safe_for_autoreplace,
const std::string& encodings,
Time date_created,
Time last_modified);
void AssertEquals(const TemplateURL& expected, const TemplateURL& actual);
TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace,
int prepopulate_id);
TemplateURL* CreateReplaceablePreloadedTemplateURL(
bool safe_for_autoreplace,
size_t index_offset_from_default,
base::string16* prepopulated_display_url);
void TestLoadUpdatingPreloadedURL(size_t index_offset_from_default);
void VerifyObserverCount(int expected_changed_count);
void VerifyObserverFired();
TemplateURLService* model() { return test_util_.model(); }
protected:
TemplateURLServiceTestUtil test_util_;
void TestGenerateSearchURL(SearchTermsData* search_terms_data) {
struct GenerateSearchURLCase {
const char* test_name;
const char* url;
const char* expected;
} generate_url_cases[] = {
{ "invalid URL", "foo{searchTerms}", "" },
{ "URL with no replacements", "http://foo/", "http://foo/" },
{ "basic functionality", "http://foo/{searchTerms}",
"http://foo/blah.blah.blah.blah.blah" }
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) {
TemplateURLData data;
data.SetURL(generate_url_cases[i].url);
TemplateURL t_url(NULL, data);
std::string result;
if (search_terms_data) {
result = TemplateURLService::GenerateSearchURLUsingTermsData(
&t_url, *search_terms_data).spec();
} else {
result = TemplateURLService::GenerateSearchURL(&t_url).spec();
}
EXPECT_EQ(result, generate_url_cases[i].expected)
<< generate_url_cases[i].test_name << " failed. Expected "
<< generate_url_cases[i].expected << " Actual " << result;
}
}
DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest);
};
TemplateURLServiceTest::TemplateURLServiceTest() {
}
void TemplateURLServiceTest::SetUp() {
test_util_.SetUp();
}
void TemplateURLServiceTest::TearDown() {
test_util_.TearDown();
}
TemplateURL* TemplateURLServiceTest::AddKeywordWithDate(
const std::string& short_name,
const std::string& keyword,
const std::string& url,
const std::string& suggest_url,
const std::string& alternate_url,
const std::string& favicon_url,
bool safe_for_autoreplace,
const std::string& encodings,
Time date_created,
Time last_modified) {
return ::AddKeywordWithDate(model(), short_name, keyword, url, suggest_url,
alternate_url, favicon_url, safe_for_autoreplace,
encodings, date_created, last_modified);
}
void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected,
const TemplateURL& actual) {
ASSERT_EQ(expected.short_name(), actual.short_name());
ASSERT_EQ(expected.keyword(), actual.keyword());
ASSERT_EQ(expected.url(), actual.url());
ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url());
ASSERT_EQ(expected.favicon_url(), actual.favicon_url());
ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls());
ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list());
ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace());
ASSERT_EQ(expected.input_encodings(), actual.input_encodings());
ASSERT_EQ(expected.id(), actual.id());
ASSERT_EQ(expected.date_created(), actual.date_created());
ASSERT_EQ(expected.last_modified(), actual.last_modified());
ASSERT_EQ(expected.sync_guid(), actual.sync_guid());
ASSERT_EQ(expected.search_terms_replacement_key(),
actual.search_terms_replacement_key());
}
TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL(
bool safe_for_autoreplace,
int prepopulate_id) {
TemplateURLData data;
data.short_name = ASCIIToUTF16("unittest");
data.SetKeyword(ASCIIToUTF16("unittest"));
data.SetURL("http://www.unittest.com/{searchTerms}");
data.favicon_url = GURL("http://favicon.url");
data.show_in_default_list = true;
data.safe_for_autoreplace = safe_for_autoreplace;
data.input_encodings.push_back("UTF-8");
data.date_created = Time::FromTimeT(100);
data.last_modified = Time::FromTimeT(100);
data.prepopulate_id = prepopulate_id;
return new TemplateURL(test_util_.profile(), data);
}
TemplateURL* TemplateURLServiceTest::CreateReplaceablePreloadedTemplateURL(
bool safe_for_autoreplace,
size_t index_offset_from_default,
base::string16* prepopulated_display_url) {
size_t default_search_provider_index = 0;
ScopedVector<TemplateURL> prepopulated_urls =
TemplateURLPrepopulateData::GetPrepopulatedEngines(
test_util_.profile(), &default_search_provider_index);
EXPECT_LT(index_offset_from_default, prepopulated_urls.size());
size_t prepopulated_index = (default_search_provider_index +
index_offset_from_default) % prepopulated_urls.size();
TemplateURL* t_url = CreatePreloadedTemplateURL(safe_for_autoreplace,
prepopulated_urls[prepopulated_index]->prepopulate_id());
*prepopulated_display_url =
prepopulated_urls[prepopulated_index]->url_ref().DisplayURL();
return t_url;
}
void TemplateURLServiceTest::TestLoadUpdatingPreloadedURL(
size_t index_offset_from_default) {
base::string16 prepopulated_url;
TemplateURL* t_url = CreateReplaceablePreloadedTemplateURL(false,
index_offset_from_default, &prepopulated_url);
base::string16 original_url = t_url->url_ref().DisplayURL();
std::string original_guid = t_url->sync_guid();
EXPECT_NE(prepopulated_url, original_url);
test_util_.ChangeModelToLoadState();
model()->Add(t_url);
const TemplateURL* keyword_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(keyword_url != NULL);
EXPECT_EQ(t_url, keyword_url);
EXPECT_EQ(original_url, keyword_url->url_ref().DisplayURL());
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(keyword_url != NULL);
EXPECT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL());
EXPECT_EQ(original_guid, keyword_url->sync_guid());
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(keyword_url != NULL);
EXPECT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL());
EXPECT_EQ(original_guid, keyword_url->sync_guid());
}
void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) {
EXPECT_EQ(expected_changed_count, test_util_.GetObserverCount());
test_util_.ResetObserverCount();
}
void TemplateURLServiceTest::VerifyObserverFired() {
EXPECT_LE(1, test_util_.GetObserverCount());
test_util_.ResetObserverCount();
}
TEST_F(TemplateURLServiceTest, Load) {
test_util_.VerifyLoad();
}
TEST_F(TemplateURLServiceTest, AddUpdateRemove) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
TemplateURLData data;
data.short_name = ASCIIToUTF16("google");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://www.google.com/foo/bar");
data.favicon_url = GURL("http://favicon.url");
data.safe_for_autoreplace = true;
data.date_created = Time::FromTimeT(100);
data.last_modified = Time::FromTimeT(100);
data.sync_guid = "00000000-0000-0000-0000-000000000001";
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
NULL));
VerifyObserverCount(1);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword()));
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
test_util_.ResetModel(true);
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
TemplateURL* loaded_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
ASSERT_TRUE(loaded_url != NULL);
AssertEquals(*cloned_url, *loaded_url);
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
NULL));
StrictMock<base::MockTimeProvider> mock_time;
model()->set_time_provider(&base::MockTimeProvider::StaticNow);
EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337)));
model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"),
"c");
ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name());
ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword());
ASSERT_EQ("c", loaded_url->url());
ASSERT_FALSE(loaded_url->safe_for_autoreplace());
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
NULL));
ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL));
cloned_url.reset(new TemplateURL(loaded_url->profile(), loaded_url->data()));
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
ASSERT_TRUE(loaded_url != NULL);
AssertEquals(*cloned_url, *loaded_url);
ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified());
model()->Remove(loaded_url);
VerifyObserverCount(1);
test_util_.ResetModel(true);
ASSERT_EQ(initial_count, model()->GetTemplateURLs().size());
EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL);
}
TEST_F(TemplateURLServiceTest, AddSameKeyword) {
test_util_.VerifyLoad();
AddKeywordWithDate(
"first", "keyword", "http://test1", std::string(), std::string(),
std::string(), true, "UTF-8", Time(), Time());
VerifyObserverCount(1);
TemplateURLData data;
data.short_name = ASCIIToUTF16("second");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://test2");
data.safe_for_autoreplace = false;
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
VerifyObserverCount(1);
EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword());
EXPECT_FALSE(t_url->safe_for_autoreplace());
data.short_name = ASCIIToUTF16("third");
data.SetURL("http://test3");
data.safe_for_autoreplace = true;
model()->Add(new TemplateURL(test_util_.profile(), data));
VerifyObserverCount(0);
EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword());
EXPECT_FALSE(t_url->safe_for_autoreplace());
data.short_name = ASCIIToUTF16("fourth");
data.SetURL("http://test4");
data.safe_for_autoreplace = false;
TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url2);
VerifyObserverCount(2);
EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name());
EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword());
EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword());
}
TEST_F(TemplateURLServiceTest, AddExtensionKeyword) {
test_util_.VerifyLoad();
TemplateURL* original1 = AddKeywordWithDate(
"replaceable", "keyword1", "http://test1", std::string(), std::string(),
std::string(), true, "UTF-8", Time(), Time());
TemplateURL* original2 = AddKeywordWithDate(
"nonreplaceable", "keyword2", "http://test2", std::string(),
std::string(), std::string(), false, "UTF-8", Time(), Time());
TemplateURL* original3 = AddKeywordWithDate(
"extension", "keyword3",
std::string(extensions::kExtensionScheme) + "://test3", std::string(),
std::string(), std::string(), false, "UTF-8", Time(), Time());
TemplateURLData data;
data.short_name = ASCIIToUTF16("test");
data.SetKeyword(ASCIIToUTF16("keyword1"));
data.SetURL(std::string(extensions::kExtensionScheme) + "://test4");
data.safe_for_autoreplace = false;
TemplateURL* extension1 = new TemplateURL(test_util_.profile(), data);
model()->Add(extension1);
ASSERT_EQ(extension1,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")));
EXPECT_EQ(original1,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("test1")));
data.SetKeyword(ASCIIToUTF16("keyword2"));
TemplateURL* extension2 = new TemplateURL(test_util_.profile(), data);
model()->Add(extension2);
ASSERT_EQ(extension2,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")));
EXPECT_EQ(original2,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("test2")));
data.SetKeyword(ASCIIToUTF16("keyword3"));
TemplateURL* extension3 = new TemplateURL(test_util_.profile(), data);
model()->Add(extension3);
ASSERT_EQ(extension3,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")));
EXPECT_EQ(original3,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_")));
}
TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) {
test_util_.VerifyLoad();
TemplateURL* extension = AddKeywordWithDate(
"extension", "keyword",
std::string(extensions::kExtensionScheme) + "://test2", std::string(),
std::string(), std::string(), false, "UTF-8", Time(), Time());
AddKeywordWithDate(
"replaceable", "keyword", "http://test1", std::string(), std::string(),
std::string(), true, "UTF-8", Time(), Time());
TemplateURLData data;
data.short_name = ASCIIToUTF16("name1");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://test3");
data.safe_for_autoreplace = true;
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
EXPECT_EQ(extension,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_")));
EXPECT_TRUE(model()->GetTemplateURLForHost("test1") == NULL);
EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3"));
data.short_name = ASCIIToUTF16("name2");
data.SetURL("http://test4");
data.safe_for_autoreplace = false;
TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url2);
EXPECT_EQ(t_url2,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL);
EXPECT_EQ(extension,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_")));
}
TEST_F(TemplateURLServiceTest, GenerateKeyword) {
ASSERT_EQ(ASCIIToUTF16("foo"),
TemplateURLService::GenerateKeyword(GURL("http://foo")));
ASSERT_EQ(ASCIIToUTF16("foo"),
TemplateURLService::GenerateKeyword(GURL("http://www.foo")));
ASSERT_EQ(ASCIIToUTF16("blah"),
TemplateURLService::GenerateKeyword(GURL("http://blah/")));
ASSERT_EQ(ASCIIToUTF16("www"),
TemplateURLService::GenerateKeyword(GURL("http://www.")));
}
TEST_F(TemplateURLServiceTest, GenerateSearchURL) {
TestGenerateSearchURL(NULL);
}
TEST_F(TemplateURLServiceTest, GenerateSearchURLUsingTermsData) {
TestSearchTermsData search_terms_data("http://google.com/");
TestGenerateSearchURL(&search_terms_data);
}
TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) {
Time now = Time::Now();
TimeDelta one_day = TimeDelta::FromDays(1);
Time month_ago = now - TimeDelta::FromDays(30);
EXPECT_EQ(0U, model()->GetTemplateURLs().size());
AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1",
std::string(), "http://icon1", true, "UTF-8;UTF-16",
Time(), Time());
AddKeywordWithDate("name2", "key2", "http://foo2", "http://suggest2",
std::string(), "http://icon2", true, "UTF-8;UTF-16",
now - one_day, Time());
AddKeywordWithDate("name3", "key3", "http://foo3", std::string(),
std::string(), std::string(), true, std::string(), now,
Time());
AddKeywordWithDate("name4", "key4", "http://foo4", std::string(),
std::string(), std::string(), true, std::string(),
now + one_day, Time());
AddKeywordWithDate("name5", "key5", "http://foo5", "http://suggest5",
std::string(), "http://icon5", false, "UTF-8;UTF-16", now,
Time());
AddKeywordWithDate("name6", "key6", "http://foo6", "http://suggest6",
std::string(), "http://icon6", false, "UTF-8;UTF-16",
month_ago, Time());
EXPECT_EQ(6U, model()->GetTemplateURLs().size());
model()->RemoveAutoGeneratedSince(now);
EXPECT_EQ(4U, model()->GetTemplateURLs().size());
model()->RemoveAutoGeneratedBetween(now - TimeDelta::FromDays(60), now);
EXPECT_EQ(3U, model()->GetTemplateURLs().size());
EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
EXPECT_EQ(0U,
model()->GetTemplateURLs()[0]->date_created().ToInternalValue());
EXPECT_EQ(ASCIIToUTF16("key5"), model()->GetTemplateURLs()[1]->keyword());
EXPECT_FALSE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
EXPECT_EQ(now.ToInternalValue(),
model()->GetTemplateURLs()[1]->date_created().ToInternalValue());
EXPECT_EQ(ASCIIToUTF16("key6"), model()->GetTemplateURLs()[2]->keyword());
EXPECT_FALSE(model()->GetTemplateURLs()[2]->safe_for_autoreplace());
EXPECT_EQ(month_ago.ToInternalValue(),
model()->GetTemplateURLs()[2]->date_created().ToInternalValue());
model()->RemoveAutoGeneratedSince(Time());
EXPECT_EQ(2U, model()->GetTemplateURLs().size());
}
TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) {
Time now = Time::Now();
TimeDelta one_day = TimeDelta::FromDays(1);
Time month_ago = now - TimeDelta::FromDays(30);
EXPECT_EQ(0U, model()->GetTemplateURLs().size());
AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1",
std::string(), "http://icon2", true, "UTF-8;UTF-16",
now - one_day, Time());
AddKeywordWithDate("name2", "key2", "http://foo2", std::string(),
std::string(), std::string(), true, std::string(), now,
Time());
AddKeywordWithDate("name3", "key3", "http://foo3", std::string(),
std::string(), std::string(), true, std::string(),
now + one_day, Time());
EXPECT_EQ(3U, model()->GetTemplateURLs().size());
model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago,
now + one_day);
EXPECT_EQ(2U, model()->GetTemplateURLs().size());
EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
EXPECT_EQ(ASCIIToUTF16("key3"), model()->GetTemplateURLs()[1]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo1"), now,
now + one_day);
EXPECT_EQ(2U, model()->GetTemplateURLs().size());
EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
EXPECT_EQ(ASCIIToUTF16("key3"), model()->GetTemplateURLs()[1]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago,
now + one_day + one_day);
EXPECT_EQ(1U, model()->GetTemplateURLs().size());
EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
}
TEST_F(TemplateURLServiceTest, Reset) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
TemplateURLData data;
data.short_name = ASCIIToUTF16("google");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://www.google.com/foo/bar");
data.favicon_url = GURL("http://favicon.url");
data.date_created = Time::FromTimeT(100);
data.last_modified = Time::FromTimeT(100);
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
VerifyObserverCount(1);
base::RunLoop().RunUntilIdle();
StrictMock<base::MockTimeProvider> mock_time;
model()->set_time_provider(&base::MockTimeProvider::StaticNow);
EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337)));
const base::string16 new_short_name(ASCIIToUTF16("a"));
const base::string16 new_keyword(ASCIIToUTF16("b"));
const std::string new_url("c");
model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url);
ASSERT_EQ(new_short_name, t_url->short_name());
ASSERT_EQ(new_keyword, t_url->keyword());
ASSERT_EQ(new_url, t_url->url());
ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword));
ASSERT_TRUE(
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL);
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
test_util_.ResetModel(true);
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword);
ASSERT_TRUE(read_url);
AssertEquals(*cloned_url, *read_url);
ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified());
}
TEST_F(TemplateURLServiceTest, DefaultSearchProvider) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
TemplateURL* t_url = AddKeywordWithDate(
"name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
test_util_.ResetObserverCount();
model()->SetDefaultSearchProvider(t_url);
ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
ASSERT_TRUE(t_url->safe_for_autoreplace());
ASSERT_TRUE(t_url->show_in_default_list());
VerifyObserverCount(1);
base::RunLoop().RunUntilIdle();
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
test_util_.ResetModel(true);
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
ASSERT_TRUE(model()->GetDefaultSearchProvider());
AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) {
test_util_.ChangeModelToLoadState();
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL));
TemplateURL* t_url = AddKeywordWithDate(
"name1", "foo", "http://foo1", "http://sugg1", std::string(),
"http://icon1", true, "UTF-8;UTF-16", Time(), Time());
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
GURL("http://foo2"), NULL));
model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
t_url->url());
ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
GURL("http://foo2"), NULL));
}
TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) {
test_util_.ChangeModelToLoadState();
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
GURL("http://foo.com"), NULL));
TemplateURL* t_url = AddKeywordWithDate(
"name1", "foo", "http://foo.com", "http://sugg1", std::string(),
"http://icon1", true, "UTF-8;UTF-16", Time(), Time());
ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
GURL("http://foo.com"), NULL));
model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
t_url->url());
ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
GURL("http://foo.com"), NULL));
}
TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) {
ASSERT_TRUE(model()->GetDefaultSearchProvider());
test_util_.VerifyLoad();
ASSERT_TRUE(model()->GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) {
test_util_.VerifyLoad();
TemplateURLData data;
data.short_name = ASCIIToUTF16("a");
data.safe_for_autoreplace = true;
data.SetURL("http://url/{searchTerms}");
data.suggestions_url = "http://url2";
data.instant_url = "http://instant";
data.date_created = Time::FromTimeT(100);
data.last_modified = Time::FromTimeT(100);
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
const TemplateURLID id = t_url->id();
model()->SetDefaultSearchProvider(t_url);
base::RunLoop().RunUntilIdle();
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
test_util_.ResetModel(false);
const TemplateURL* default_turl = model()->GetDefaultSearchProvider();
ASSERT_TRUE(default_turl);
EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name());
EXPECT_EQ("http://url/{searchTerms}", default_turl->url());
EXPECT_EQ("http://url2", default_turl->suggestions_url());
EXPECT_EQ("http://instant", default_turl->instant_url());
EXPECT_EQ(id, default_turl->id());
test_util_.VerifyLoad();
ASSERT_TRUE(model()->GetDefaultSearchProvider());
AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) {
test_util_.VerifyLoad();
TemplateURL* google = model()->GetTemplateURLForKeyword(
ASCIIToUTF16("google.com"));
ASSERT_TRUE(google);
model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"),
"http://www.foo.com/s?q={searchTerms}");
EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name());
EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword());
TemplateURL* user_dse = AddKeywordWithDate(
"malware", "google.com", "http://www.goo.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
model()->SetDefaultSearchProvider(user_dse);
EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
TemplateURL* bing = model()->GetTemplateURLForKeyword(
ASCIIToUTF16("bing.com"));
ASSERT_TRUE(bing);
model()->Remove(bing);
EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com")));
model()->RegisterOmniboxKeyword("abcdefg", "extension_name", "bing.com");
EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com")));
model()->RepairPrepopulatedSearchEngines();
ASSERT_EQ(google, model()->GetDefaultSearchProvider());
EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name());
EXPECT_EQ("www.google.com",
TemplateURLService::GenerateSearchURL(google).host());
bing = model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"));
ASSERT_TRUE(bing);
EXPECT_EQ(TemplateURL::NORMAL, bing->GetType());
EXPECT_EQ(user_dse, model()->GetTemplateURLForHost("www.goo.com"));
EXPECT_EQ(ASCIIToUTF16("google.com"), user_dse->keyword());
}
TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) {
const char kName[] = "test1";
const char kKeyword[] = "test.com";
const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
const char kIconURL[] = "http://test.com/icon.jpg";
const char kEncodings[] = "UTF-16;UTF-32";
const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
const char kSearchTermsReplacementKey[] = "espv";
test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword,
kSearchURL, std::string(),
kIconURL, kEncodings,
kAlternateURL,
kSearchTermsReplacementKey);
test_util_.VerifyLoad();
TemplateURLData data;
data.short_name = ASCIIToUTF16(kName);
data.SetKeyword(ASCIIToUTF16(kKeyword));
data.SetURL(kSearchURL);
data.favicon_url = GURL(kIconURL);
data.show_in_default_list = true;
base::SplitString(kEncodings, ';', &data.input_encodings);
data.alternate_urls.push_back(kAlternateURL);
data.search_terms_replacement_key = kSearchTermsReplacementKey;
scoped_ptr<TemplateURL> expected_managed_default(new TemplateURL(
test_util_.profile(), data));
EXPECT_TRUE(model()->is_default_search_managed());
const TemplateURL* actual_managed_default =
model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default.get(), actual_managed_default);
model()->RepairPrepopulatedSearchEngines();
EXPECT_TRUE(model()->is_default_search_managed());
actual_managed_default = model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default.get(), actual_managed_default);
}
TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) {
struct TestData {
const std::string url;
const base::string16 term;
} data[] = {
{ "http://foo/", base::string16() },
{ "http://foo/foo?q=xx", base::string16() },
{ "http://x/bar?q=xx", base::string16() },
{ "http://x/foo?y=xx", base::string16() },
{ "http://x/foo?q=xx", ASCIIToUTF16("xx") },
{ "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") },
{ "http://x/foo?q=b&q=xx", base::string16() },
{ "http://x/foo#query=xx", ASCIIToUTF16("xx") },
{ "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") },
{ "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") },
{ "http://x/foo?query=b#q=xx", base::string16() },
};
test_util_.ChangeModelToLoadState();
AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}",
"http://sugg1", "http://x/foo#query={searchTerms}",
"http://icon1", false, "UTF-8;UTF-16", Time(), Time());
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
history::URLVisitedDetails details;
details.row = history::URLRow(GURL(data[i].url));
details.transition = content::PageTransitionFromInt(0);
model()->UpdateKeywordSearchTermsForURL(details);
EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm());
}
}
TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) {
struct TestData {
const std::string url;
} data[] = {
{ "http://foo/" },
{ "http://x/bar?q=xx" },
{ "http://x/foo?y=xx" },
};
test_util_.ChangeModelToLoadState();
AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(),
"http://icon1", false, "UTF-8;UTF-16", Time(), Time());
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
history::URLVisitedDetails details;
details.row = history::URLRow(GURL(data[i].url));
details.transition = content::PageTransitionFromInt(0);
model()->UpdateKeywordSearchTermsForURL(details);
ASSERT_EQ(base::string16(), test_util_.GetAndClearSearchTerm());
}
}
TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) {
test_util_.ChangeModelToLoadState();
test_util_.SetGoogleBaseURL(GURL("http://google.com/"));
const TemplateURL* t_url = AddKeywordWithDate(
"name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1",
std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com"));
EXPECT_EQ("google.com", t_url->url_ref().GetHost());
EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword());
test_util_.ResetObserverCount();
test_util_.SetGoogleBaseURL(GURL("http://google.co.uk/"));
VerifyObserverCount(1);
ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk"));
EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL);
EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost());
EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
TemplateURL* manual = AddKeywordWithDate(
"manual", "google.de", "http://google.de/search?q={searchTerms}",
std::string(), std::string(), std::string(), false, "UTF-8", Time(),
Time());
test_util_.SetGoogleBaseURL(GURL("http://google.de"));
ASSERT_EQ(manual,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de")));
EXPECT_EQ("google.de", manual->url_ref().GetHost());
ASSERT_EQ(t_url,
model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk")));
EXPECT_EQ("google.de", t_url->url_ref().GetHost());
EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
test_util_.SetGoogleBaseURL(GURL("http://google.fr/"));
ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de"));
EXPECT_EQ("google.de", manual->url_ref().GetHost());
EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword());
ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr"));
EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL);
EXPECT_EQ("google.fr", t_url->url_ref().GetHost());
EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword());
}
TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) {
test_util_.VerifyLoad();
ASSERT_TRUE(test_util_.profile()->CreateHistoryService(true, false));
TemplateURL* t_url = AddKeywordWithDate(
"keyword", "keyword", "http://foo.com/foo?query={searchTerms}",
"http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16",
base::Time::Now(), base::Time::Now());
HistoryService* history =
HistoryServiceFactory::GetForProfile(test_util_.profile(),
Profile::EXPLICIT_ACCESS);
history->AddPage(
GURL(t_url->url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")))),
base::Time::Now(), NULL, 0, GURL(), history::RedirectList(),
content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false);
test_util_.profile()->BlockUntilHistoryProcessesPendingRequests();
CancelableRequestConsumer consumer;
QueryHistoryCallbackImpl callback;
history->QueryURL(GURL("http://keyword"), true, &consumer,
base::Bind(&QueryHistoryCallbackImpl::Callback,
base::Unretained(&callback)));
test_util_.profile()->BlockUntilHistoryProcessesPendingRequests();
EXPECT_TRUE(callback.success);
EXPECT_NE(0, callback.row.id());
ASSERT_EQ(1U, callback.visits.size());
EXPECT_EQ(content::PAGE_TRANSITION_KEYWORD_GENERATED,
content::PageTransitionStripQualifier(callback.visits[0].transition));
}
TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) {
TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
test_util_.ChangeModelToLoadState();
model()->Add(t_url);
ASSERT_TRUE(
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
ASSERT_TRUE(
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
ASSERT_TRUE(
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
}
TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) {
TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999);
test_util_.ChangeModelToLoadState();
model()->Add(t_url);
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
const TemplateURL* url_for_unittest =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(url_for_unittest != NULL);
AssertEquals(*cloned_url, *url_for_unittest);
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
ASSERT_TRUE(
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
}
TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) {
test_util_.VerifyLoad();
TemplateURL* default_search = model()->GetDefaultSearchProvider();
ASSERT_TRUE(default_search != NULL);
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->profile(),
default_search->data()));
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
default_search = model()->GetDefaultSearchProvider();
ASSERT_TRUE(default_search != NULL);
AssertEquals(*cloned_url, *default_search);
}
TEST_F(TemplateURLServiceTest, FindNewDefaultSearchProvider) {
EXPECT_FALSE(model()->FindNewDefaultSearchProvider());
AddKeywordWithDate("name1", "key1", "http://foo1/{searchTerms}",
"http://sugg1", std::string(), "http://icon1", true,
"UTF-8;UTF-16", Time(), Time());
AddKeywordWithDate("name2", "key2", "http://foo2/{searchTerms}",
"http://sugg2", std::string(), "http://icon1", true,
"UTF-8;UTF-16", Time(), Time());
AddKeywordWithDate("name3", "key3", "http://foo1/{searchTerms}",
"http://sugg3", std::string(), "http://icon3", true,
"UTF-8;UTF-16", Time(), Time());
TemplateURLData data;
data.short_name = ASCIIToUTF16("valid");
data.SetKeyword(ASCIIToUTF16("validkeyword"));
data.SetURL("http://valid/{searchTerms}");
data.favicon_url = GURL("http://validicon");
data.show_in_default_list = true;
TemplateURL* valid_turl(new TemplateURL(test_util_.profile(), data));
model()->Add(valid_turl);
EXPECT_EQ(4U, model()->GetTemplateURLs().size());
TemplateURL* new_default = model()->FindNewDefaultSearchProvider();
ASSERT_TRUE(new_default);
EXPECT_EQ(valid_turl, new_default);
model()->Remove(new_default);
EXPECT_FALSE(model()->FindNewDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) {
TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
test_util_.ChangeModelToLoadState();
model()->Add(t_url);
model()->SetDefaultSearchProvider(t_url);
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(),
t_url->data()));
ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
{
const TemplateURL* keyword_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(keyword_url != NULL);
AssertEquals(*cloned_url, *keyword_url);
ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
}
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
{
const TemplateURL* keyword_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
ASSERT_TRUE(keyword_url != NULL);
AssertEquals(*cloned_url, *keyword_url);
ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
}
}
TEST_F(TemplateURLServiceTest, LoadUpdatesDefaultSearchURL) {
TestLoadUpdatingPreloadedURL(0);
}
TEST_F(TemplateURLServiceTest, LoadUpdatesSearchURL) {
TestLoadUpdatingPreloadedURL(1);
}
TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) {
test_util_.VerifyLoad();
TemplateURL* old_default = model()->GetDefaultSearchProvider();
EXPECT_TRUE(old_default);
model()->SetDefaultSearchProvider(NULL);
model()->Remove(old_default);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(model()->GetDefaultSearchProvider());
test_util_.ResetModel(true);
ASSERT_TRUE(model()->GetDefaultSearchProvider());
EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement());
model()->ResetTemplateURL(model()->GetDefaultSearchProvider(),
ASCIIToUTF16("test"), ASCIIToUTF16("test"),
"http://example.com/");
base::RunLoop().RunUntilIdle();
test_util_.ResetModel(true);
ASSERT_TRUE(model()->GetDefaultSearchProvider());
EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement());
}
TEST_F(TemplateURLServiceTest, FailedInit) {
test_util_.VerifyLoad();
test_util_.ClearModel();
scoped_refptr<WebDataService> web_service =
WebDataService::FromBrowserContext(test_util_.profile());
web_service->ShutdownDatabase();
test_util_.ResetModel(false);
model()->Load();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(model()->GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
test_util_.ResetObserverCount();
TemplateURL* regular_default = AddKeywordWithDate(
"name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
VerifyObserverCount(1);
model()->SetDefaultSearchProvider(regular_default);
VerifyObserverCount(1);
EXPECT_FALSE(model()->is_default_search_managed());
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
const char kName[] = "test1";
const char kKeyword[] = "test.com";
const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
const char kIconURL[] = "http://test.com/icon.jpg";
const char kEncodings[] = "UTF-16;UTF-32";
const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
const char kSearchTermsReplacementKey[] = "espv";
test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword,
kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
kSearchTermsReplacementKey);
VerifyObserverFired();
EXPECT_TRUE(model()->is_default_search_managed());
EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
TemplateURLData data;
data.short_name = ASCIIToUTF16(kName);
data.SetKeyword(ASCIIToUTF16(kKeyword));
data.SetURL(kSearchURL);
data.favicon_url = GURL(kIconURL);
data.show_in_default_list = true;
base::SplitString(kEncodings, ';', &data.input_encodings);
data.alternate_urls.push_back(kAlternateURL);
data.search_terms_replacement_key = kSearchTermsReplacementKey;
Profile* profile = test_util_.profile();
scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(profile,
data));
const TemplateURL* actual_managed_default =
model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
EXPECT_TRUE(actual_managed_default->show_in_default_list());
const char kNewName[] = "test2";
const char kNewKeyword[] = "other.com";
const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}";
const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}";
test_util_.SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword,
kNewSearchURL, kNewSuggestURL, std::string(), std::string(),
std::string(), std::string());
VerifyObserverFired();
EXPECT_TRUE(model()->is_default_search_managed());
EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
TemplateURLData data2;
data2.short_name = ASCIIToUTF16(kNewName);
data2.SetKeyword(ASCIIToUTF16(kNewKeyword));
data2.SetURL(kNewSearchURL);
data2.suggestions_url = kNewSuggestURL;
data2.show_in_default_list = true;
scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(profile,
data2));
actual_managed_default = model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default2.get(), actual_managed_default);
EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
test_util_.RemoveManagedDefaultSearchPreferences();
VerifyObserverFired();
EXPECT_FALSE(model()->is_default_search_managed());
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
const TemplateURL* actual_final_managed_default =
model()->GetDefaultSearchProvider();
ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default);
EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true);
test_util_.SetManagedDefaultSearchPreferences(false, std::string(),
std::string(), std::string(), std::string(), std::string(),
std::string(), std::string(), std::string());
VerifyObserverFired();
EXPECT_TRUE(model()->is_default_search_managed());
EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider());
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword,
kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
kSearchTermsReplacementKey);
VerifyObserverFired();
EXPECT_TRUE(model()->is_default_search_managed());
EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
actual_managed_default = model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
test_util_.RemoveManagedDefaultSearchPreferences();
test_util_.ResetModel(true);
TemplateURL* new_default =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1"));
ASSERT_FALSE(new_default == NULL);
model()->SetDefaultSearchProvider(new_default);
EXPECT_EQ(new_default, model()->GetDefaultSearchProvider());
test_util_.ResetModel(false);
test_util_.SetManagedDefaultSearchPreferences(false, std::string(),
std::string(), std::string(), std::string(), std::string(),
std::string(), std::string(), std::string());
test_util_.VerifyLoad();
EXPECT_TRUE(model()->is_default_search_managed());
EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL);
}
TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
TemplateURLData data;
data.short_name = ASCIIToUTF16("google");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://www.google.com/foo/bar");
data.sync_guid.clear();
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
VerifyObserverCount(1);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
test_util_.ResetModel(true);
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
const TemplateURL* loaded_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
ASSERT_FALSE(loaded_url == NULL);
ASSERT_FALSE(loaded_url->sync_guid().empty());
}
TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) {
test_util_.VerifyLoad();
const size_t initial_count = model()->GetTemplateURLs().size();
TemplateURLData data;
data.short_name = ASCIIToUTF16("google");
data.SetKeyword(ASCIIToUTF16("keyword"));
data.SetURL("http://www.google.com/foo/bar");
std::vector<std::string> encodings;
data.input_encodings.push_back("UTF-8");
data.input_encodings.push_back("UTF-8");
data.input_encodings.push_back("UTF-16");
data.input_encodings.push_back("UTF-8");
data.input_encodings.push_back("Big5");
data.input_encodings.push_back("UTF-16");
data.input_encodings.push_back("Big5");
data.input_encodings.push_back("Windows-1252");
TemplateURL* t_url = new TemplateURL(test_util_.profile(), data);
model()->Add(t_url);
VerifyObserverCount(1);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
const TemplateURL* loaded_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
ASSERT_TRUE(loaded_url != NULL);
EXPECT_EQ(8U, loaded_url->input_encodings().size());
test_util_.ResetModel(true);
ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
ASSERT_FALSE(loaded_url == NULL);
EXPECT_EQ(4U, loaded_url->input_encodings().size());
}
TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) {
test_util_.VerifyLoad();
TemplateURL* user_dse = AddKeywordWithDate(
"user", "user", "http://www.goo.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
model()->SetDefaultSearchProvider(user_dse);
EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
TemplateURL* ext_dse = CreateKeywordWithDate(
model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
scoped_ptr<AssociatedExtensionInfo> extension_info(
new AssociatedExtensionInfo);
extension_info->wants_to_be_default_engine = true;
extension_info->extension_id = "ext";
model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
model()->RemoveExtensionControlledTURL("ext");
ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) {
test_util_.VerifyLoad();
TemplateURL* user_dse = AddKeywordWithDate(
"user", "user", "http://www.goo.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
model()->SetDefaultSearchProvider(user_dse);
EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
TemplateURL* ext_dse = CreateKeywordWithDate(
model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
scoped_ptr<AssociatedExtensionInfo> extension_info(
new AssociatedExtensionInfo);
extension_info->wants_to_be_default_engine = false;
extension_info->extension_id = "ext1";
model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
ext_dse = CreateKeywordWithDate(
model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
extension_info.reset(new AssociatedExtensionInfo);
extension_info->wants_to_be_default_engine = true;
extension_info->extension_id = "ext2";
model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
test_util_.ResetModel(true);
user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user"));
ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2")));
}
TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) {
const char kName[] = "test";
const char kKeyword[] = "test.com";
const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
const char kIconURL[] = "http://test.com/icon.jpg";
const char kEncodings[] = "UTF-16;UTF-32";
const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
const char kSearchTermsReplacementKey[] = "espv";
test_util_.SetManagedDefaultSearchPreferences(
true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings,
kAlternateURL, kSearchTermsReplacementKey);
test_util_.VerifyLoad();
TemplateURLData data;
data.short_name = ASCIIToUTF16(kName);
data.SetKeyword(ASCIIToUTF16(kKeyword));
data.SetURL(kSearchURL);
data.favicon_url = GURL(kIconURL);
data.show_in_default_list = true;
base::SplitString(kEncodings, ';', &data.input_encodings);
data.alternate_urls.push_back(kAlternateURL);
data.search_terms_replacement_key = kSearchTermsReplacementKey;
scoped_ptr<TemplateURL> expected_managed_default(new TemplateURL(
test_util_.profile(), data));
EXPECT_TRUE(model()->is_default_search_managed());
const TemplateURL* actual_managed_default =
model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default.get(), actual_managed_default);
TemplateURL* ext_dse = CreateKeywordWithDate(
model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}",
std::string(), std::string(), std::string(),
true, "UTF-8", Time(), Time());
scoped_ptr<AssociatedExtensionInfo> extension_info(
new AssociatedExtensionInfo);
extension_info->wants_to_be_default_engine = true;
extension_info->extension_id = "ext1";
model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
EXPECT_TRUE(model()->is_default_search_managed());
actual_managed_default = model()->GetDefaultSearchProvider();
ExpectSimilar(expected_managed_default.get(), actual_managed_default);
}