Chromium Code Reviews| Index: chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc |
| diff --git a/chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc b/chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3cdd801de1e99473868123da841c0c77dcc7622f |
| --- /dev/null |
| +++ b/chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc |
| @@ -0,0 +1,113 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
|
mmenke
2015/01/15 22:35:30
2015
Mathieu
2015/01/22 19:21:58
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/search/search.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "components/search_engines/template_url_service.h" |
| +#include "content/public/browser/navigation_controller.h" |
| +#include "content/public/browser/navigation_entry.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "net/test/url_request/url_request_mock_http_job.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| +void SetUrlRequestMock(const base::FilePath& path) { |
| + net::URLRequestMockHTTPJob::AddUrlHandlers(path, |
| + BrowserThread::GetBlockingPool()); |
| +} |
| +} // namespace |
| + |
| +class NewTabPageInterceptorTest : public InProcessBrowserTest { |
| + public: |
| + NewTabPageInterceptorTest() {} |
| + |
| + protected: |
| + void SetUpOnMainThread() override { |
| + path_ = ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath()); |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SetUrlRequestMock, path_)); |
| + } |
| + |
| + GURL GetMockURL(const std::string& file) { |
| + return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(file)); |
| + } |
| + |
| + void ChangeDefaultSearchProvider(const std::string& newtab_path) { |
|
mmenke
2015/01/15 22:35:30
Optional: Don't think we get anything out of maki
Mathieu
2015/01/22 19:21:58
Done.
|
| + // Update the New Tab URL for this test case. |
| + new_tab_url_ = GetMockURL(newtab_path); |
| + |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| + ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| + UIThreadSearchTermsData::SetGoogleBaseURL("https://mock.http/"); |
| + std::string base_url("{google:baseURL}"); |
| + TemplateURLData data; |
| + data.SetKeyword(base::UTF8ToUTF16(base_url)); |
| + data.SetURL(base_url + "url?bar={searchTerms}"); |
| + data.new_tab_url = base_url + newtab_path; |
| + TemplateURL* template_url = new TemplateURL(data); |
| + // Takes ownership of |template_url|. |
| + template_url_service->Add(template_url); |
| + template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
| + } |
| + |
| + GURL new_tab_url_; |
| + base::FilePath path_; |
|
mmenke
2015/01/15 22:35:30
Should make these private, per Google style guidel
Mathieu
2015/01/22 19:21:58
Done.
|
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) { |
| + ChangeDefaultSearchProvider("instant_extended.html"); |
| + |
| + ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| + content::WebContents* contents = |
| + browser()->tab_strip_model()->GetWebContentsAt(0); |
| + content::NavigationController* controller = &contents->GetController(); |
| + // A correct, 200-OK file works correctly. |
| + EXPECT_EQ(new_tab_url_, controller->GetLastCommittedEntry()->GetURL()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 404Interception) { |
| + ChangeDefaultSearchProvider("page404.html"); |
| + |
| + ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| + content::WebContents* contents = |
| + browser()->tab_strip_model()->GetWebContentsAt(0); |
| + content::NavigationController* controller = &contents->GetController(); |
| + // 404 makes a redirect to the local NTP. |
| + EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| + controller->GetLastCommittedEntry()->GetURL()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 204Interception) { |
| + ChangeDefaultSearchProvider("page204.html"); |
| + |
| + ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| + content::WebContents* contents = |
| + browser()->tab_strip_model()->GetWebContentsAt(0); |
| + content::NavigationController* controller = &contents->GetController(); |
| + // 204 makes a redirect to the local NTP. |
| + EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| + controller->GetLastCommittedEntry()->GetURL()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, FailedRequestInterception) { |
| + ChangeDefaultSearchProvider("notarealfile.html"); |
| + |
| + ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| + content::WebContents* contents = |
| + browser()->tab_strip_model()->GetWebContentsAt(0); |
| + content::NavigationController* controller = &contents->GetController(); |
| + // 204 makes a redirect to the local NTP. |
| + EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| + controller->GetLastCommittedEntry()->GetURL()); |
| +} |