Chromium Code Reviews| Index: chrome/browser/policy/policy_browsertest.cc |
| diff --git a/chrome/browser/policy/policy_browsertest.cc b/chrome/browser/policy/policy_browsertest.cc |
| index f687cf4a6c8a9dc49aeec2c31cd65a49eac9de5d..2e34bb77e64b75f1799d2a4dbcfbf02b3bf79408 100644 |
| --- a/chrome/browser/policy/policy_browsertest.cc |
| +++ b/chrome/browser/policy/policy_browsertest.cc |
| @@ -134,8 +134,8 @@ const FilePath::CharType kGoodCrxManifestName[] = |
| FILE_PATH_LITERAL("good_update_manifest.xml"); |
| const char* kURLs[] = { |
| - "http://aaa.com/empty.html", |
| - "http://bbb.com/empty.html", |
| + "http://aaaa.com/empty.html", |
| + "http://bbbb.com/empty.html", |
|
mmenke
2012/12/13 16:12:04
What's the reason for this? Other tests also usin
pauljensen
2012/12/17 16:26:50
There were other tests using this but now it's unn
|
| "http://ccc.com/empty.html", |
| "http://ddd.com/empty.html", |
| "http://eee.com/empty.html", |
| @@ -149,8 +149,18 @@ void RedirectHostsToTestData(const char* const urls[], size_t size) { |
| for (size_t i = 0; i < size; ++i) { |
| const GURL url(urls[i]); |
| EXPECT_TRUE(url.is_valid()); |
| - filter->AddHostnameHandler(url.scheme(), url.host(), |
| - URLRequestMockHTTPJob::Factory); |
| + filter->AddUrlHandler(url, URLRequestMockHTTPJob::Factory); |
| + } |
| +} |
| + |
| +// Remove filters for requests to the hosts in |urls|. |
| +void UndoRedirectHostsToTestData(const char* const urls[], size_t size) { |
| + // Map the given hosts to the test data dir. |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + for (size_t i = 0; i < size; ++i) { |
| + const GURL url(urls[i]); |
| + EXPECT_TRUE(url.is_valid()); |
| + filter->RemoveUrlHandler(url); |
| } |
| } |
| @@ -163,21 +173,42 @@ net::URLRequestJob* FailedJobFactory( |
| request, network_delegate, net::ERR_CONNECTION_RESET); |
| } |
| -// Filters requests to the |host| such that they fail. Run on IO thread. |
| -void MakeRequestFailOnIO(const std::string& host) { |
| - net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| - filter->AddHostnameHandler("http", host, &FailedJobFactory); |
| - filter->AddHostnameHandler("https", host, &FailedJobFactory); |
| -} |
| +// While |MakeRequestFail| is in scope URLRequests to |host| will fail. |
| +class MakeRequestFail { |
| + public: |
| + // Sets up the filter on IO thread such that requests to |host| fail. |
| + explicit MakeRequestFail(const std::string& host) : host_(host) { |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(MakeRequestFailOnIO, host_), |
| + MessageLoop::QuitClosure()); |
| + content::RunMessageLoop(); |
| + } |
| + ~MakeRequestFail() { |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(UndoMakeRequestFailOnIO, host_), |
| + MessageLoop::QuitClosure()); |
| + content::RunMessageLoop(); |
| + } |
| -// Sets up the filter on IO thread such that requests to |host| fail. |
| -void MakeRequestFail(const std::string& host) { |
| - BrowserThread::PostTaskAndReply( |
| - BrowserThread::IO, FROM_HERE, |
| - base::Bind(MakeRequestFailOnIO, host), |
| - MessageLoop::QuitClosure()); |
| - content::RunMessageLoop(); |
| -} |
| + private: |
| + // Filters requests to the |host| such that they fail. Run on IO thread. |
| + static void MakeRequestFailOnIO(const std::string& host) { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + filter->AddHostnameHandler("http", host, &FailedJobFactory); |
| + filter->AddHostnameHandler("https", host, &FailedJobFactory); |
| + } |
| + |
| + // Remove filters for requests to the |host|. Run on IO thread. |
| + static void UndoMakeRequestFailOnIO(const std::string& host) { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + filter->RemoveHostnameHandler("http", host); |
| + filter->RemoveHostnameHandler("https", host); |
| + } |
| + |
| + const std::string host_; |
| +}; |
| // Verifies that the given url |spec| can be opened. This assumes that |spec| |
| // points at empty.html in the test data dir. |
| @@ -616,7 +647,7 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) { |
| } |
| IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) { |
| - MakeRequestFail("search.example"); |
| + MakeRequestFail make_request_fail("search.example"); |
| // Verifies that a default search is made using the provider configured via |
| // policy. Also checks that default search can be completely disabled. |
| @@ -686,7 +717,7 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) { |
| IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) { |
| // Makes the requests fail since all we want to check is that the redirection |
| // is done properly. |
| - MakeRequestFail("google.com"); |
| + MakeRequestFail make_request_fail("google.com"); |
| // Verifies that requests to Google Search engine with the SafeSearch |
| // enabled set the safe=active&ssui=on parameters at the end of the query. |
| @@ -739,7 +770,7 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) { |
| } |
| IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) { |
| - MakeRequestFail("search.example"); |
| + MakeRequestFail make_request_fail("search.example"); |
| chrome::search::EnableInstantExtendedAPIForTesting(); |
| @@ -1417,6 +1448,12 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, URLBlacklist) { |
| CheckURLIsBlocked(browser(), kURLS[1]); |
| CheckCanOpenURL(browser(), kURLS[2]); |
| CheckCanOpenURL(browser(), kURLS[3]); |
| + |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(UndoRedirectHostsToTestData, kURLS, arraysize(kURLS)), |
| + MessageLoop::QuitClosure()); |
| + content::RunMessageLoop(); |
| } |
| // Flaky on Linux. http://crbug.com/155459 |