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..c67209199376d2b096b2bae3b0cfc3c5dc588eee |
--- /dev/null |
+++ b/chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc |
@@ -0,0 +1,122 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// 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/browser.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" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+ |
+void SetUrlRequestMock(const base::FilePath& path) { |
+ net::URLRequestMockHTTPJob::AddUrlHandlers(path, |
+ BrowserThread::GetBlockingPool()); |
+} |
+ |
+} // namespace |
+ |
+class NewTabPageInterceptorTest : public InProcessBrowserTest { |
+ public: |
+ NewTabPageInterceptorTest() {} |
+ |
+ void SetUpOnMainThread() override { |
+ path_ = ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath()); |
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
+ base::Bind(&SetUrlRequestMock, path_)); |
+ } |
+ |
+ const GURL& new_tab_url() const { return new_tab_url_; } |
+ void set_new_tab_url(const GURL& url) { new_tab_url_ = url; } |
+ |
+ void ChangeDefaultSearchProvider(const char* new_tab_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 + new_tab_path; |
+ TemplateURL* template_url = new TemplateURL(data); |
+ // Takes ownership of |template_url|. |
+ template_url_service->Add(template_url); |
+ template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
+ } |
+ |
+ static GURL GetMockURL(const base::FilePath::StringType& path) { |
+ return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(path)); |
+ } |
+ |
+ private: |
+ GURL new_tab_url_; |
+ base::FilePath path_; |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) { |
+ set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("instant_extended.html"))); |
+ 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) { |
+ set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page404.html"))); |
+ 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) { |
+ set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page204.html"))); |
+ 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) { |
+ set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("notarealfile.html"))); |
+ 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(); |
+ // Failed navigation makes a redirect to the local NTP. |
+ EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
+ controller->GetLastCommittedEntry()->GetURL()); |
+} |