This source file includes following definitions.
- RunTests
- DoInstallStateTests
- VerifyInstallState
- SetUp
- TearDown
- SimulateDefaultSearchIsManaged
- AddNewTemplateURL
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/search_engines/search_provider_install_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/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace {
class TestGetInstallState {
public:
explicit TestGetInstallState(SearchProviderInstallData* install_data);
void RunTests(const std::string& search_provider_host,
const std::string& default_search_provider_host);
private:
void DoInstallStateTests(const std::string& search_provider_host,
const std::string& default_search_provider_host);
void VerifyInstallState(SearchProviderInstallData::State expected_state,
const std::string& url);
SearchProviderInstallData* install_data_;
DISALLOW_COPY_AND_ASSIGN(TestGetInstallState);
};
TestGetInstallState::TestGetInstallState(
SearchProviderInstallData* install_data)
: install_data_(install_data) {
}
void TestGetInstallState::RunTests(
const std::string& search_provider_host,
const std::string& default_search_provider_host) {
install_data_->CallWhenLoaded(
base::Bind(&TestGetInstallState::DoInstallStateTests,
base::Unretained(this),
search_provider_host, default_search_provider_host));
base::RunLoop().RunUntilIdle();
}
void TestGetInstallState::DoInstallStateTests(
const std::string& search_provider_host,
const std::string& default_search_provider_host) {
SCOPED_TRACE("search provider: " + search_provider_host +
", default search provider: " + default_search_provider_host);
VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT,
"http://" + search_provider_host + "/");
VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT,
"http://" + search_provider_host + ":80/");
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"http://" + search_provider_host + ":96/");
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"https://" + search_provider_host + "/");
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"http://a" + search_provider_host + "/");
if (!default_search_provider_host.empty()) {
VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT,
"http://" + default_search_provider_host + "/");
}
}
void TestGetInstallState::VerifyInstallState(
SearchProviderInstallData::State expected_state,
const std::string& url) {
SearchProviderInstallData::State actual_state =
install_data_->GetInstallState(GURL(url));
EXPECT_EQ(expected_state, actual_state)
<< "GetInstallState for " << url << " failed. Expected "
<< expected_state << ". Actual " << actual_state << ".";
}
}
class SearchProviderInstallDataTest : public testing::Test {
public:
SearchProviderInstallDataTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
void SimulateDefaultSearchIsManaged(const std::string& url);
protected:
TemplateURL* AddNewTemplateURL(const std::string& url,
const base::string16& keyword);
TemplateURLServiceTestUtil util_;
SearchProviderInstallData* install_data_;
scoped_ptr<content::MockRenderProcessHost> process_;
DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest);
};
SearchProviderInstallDataTest::SearchProviderInstallDataTest()
: install_data_(NULL) {
}
void SearchProviderInstallDataTest::SetUp() {
testing::Test::SetUp();
#if defined(OS_ANDROID)
TemplateURLPrepopulateData::InitCountryCode(
std::string() );
#endif
util_.SetUp();
process_.reset(new content::MockRenderProcessHost(util_.profile()));
install_data_ =
new SearchProviderInstallData(util_.profile(), process_.get());
}
void SearchProviderInstallDataTest::TearDown() {
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_);
install_data_ = NULL;
process_.reset();
util_.TearDown();
testing::Test::TearDown();
}
void SearchProviderInstallDataTest::SimulateDefaultSearchIsManaged(
const std::string& url) {
ASSERT_FALSE(url.empty());
TestingPrefServiceSyncable* service =
util_.profile()->GetTestingPrefService();
service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
base::Value::CreateBooleanValue(true));
service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
base::Value::CreateStringValue(url));
service->SetManagedPref(prefs::kDefaultSearchProviderName,
base::Value::CreateStringValue("managed"));
service->SetManagedPref(prefs::kDefaultSearchProviderID,
new base::StringValue(std::string()));
service->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID,
new base::StringValue(std::string()));
util_.model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
}
TemplateURL* SearchProviderInstallDataTest::AddNewTemplateURL(
const std::string& url,
const base::string16& keyword) {
TemplateURLData data;
data.short_name = keyword;
data.SetKeyword(keyword);
data.SetURL(url);
TemplateURL* t_url = new TemplateURL(util_.profile(), data);
util_.model()->Add(t_url);
return t_url;
}
TEST_F(SearchProviderInstallDataTest, GetInstallState) {
util_.ChangeModelToLoadState();
std::string host = "www.unittest.com";
AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest"));
base::RunLoop().RunUntilIdle();
TestGetInstallState test_get_install_state(install_data_);
test_get_install_state.RunTests(host, std::string());
std::string default_host = "www.mmm.com";
TemplateURL* default_url =
AddNewTemplateURL("http://" + default_host + "/",
base::ASCIIToUTF16("mmm"));
util_.model()->SetDefaultSearchProvider(default_url);
test_get_install_state.RunTests(host, default_host);
}
TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) {
util_.ChangeModelToLoadState();
std::string host = "www.unittest.com";
AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest"));
std::string host2 = "www.managedtest.com";
std::string url2 = "http://" + host2 + "/p{searchTerms}";
SimulateDefaultSearchIsManaged(url2);
EXPECT_TRUE(util_.model()->is_default_search_managed());
base::RunLoop().RunUntilIdle();
TestGetInstallState test_get_install_state(install_data_);
test_get_install_state.RunTests(host, host2);
}
TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) {
TestGetInstallState test_get_install_state(install_data_);
util_.ChangeModelToLoadState();
std::string google_host = "w.com";
util_.SetGoogleBaseURL(GURL("http://" + google_host + "/"));
base::RunLoop().RunUntilIdle();
AddNewTemplateURL("{google:baseURL}?q={searchTerms}",
base::ASCIIToUTF16("t"));
TemplateURL* default_url =
AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d"));
util_.model()->SetDefaultSearchProvider(default_url);
base::RunLoop().RunUntilIdle();
test_get_install_state.RunTests(google_host, std::string());
google_host = "foo.com";
util_.SetGoogleBaseURL(GURL("http://" + google_host + "/"));
base::RunLoop().RunUntilIdle();
test_get_install_state.RunTests(google_host, std::string());
}