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..6ad74f894b04573e8590b1ce1c3bd369e1fe5dae |
--- /dev/null |
+++ b/chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc |
@@ -0,0 +1,115 @@ |
+// 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/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 |
mmenke
2015/01/22 20:53:51
Suggest a linebreak just after the start and just
Mathieu
2015/01/22 21:44:22
Done.
|
+ |
+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() { return new_tab_url_; } |
mmenke
2015/01/22 20:53:50
const GURL& new_tab_url() const { return new_tab_u
Mathieu
2015/01/22 21:44:22
Done.
|
+ |
+ GURL GetMockURL(const std::string& file) { |
mmenke
2015/01/22 20:53:51
This should be static and probably private as well
Mathieu
2015/01/22 21:44:22
Done.
|
+ return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(file)); |
+ } |
+ |
+ void ChangeDefaultSearchProvider(const std::string& newtab_path) { |
+ // Update the New Tab URL for this test case. |
+ new_tab_url_ = GetMockURL(newtab_path); |
mmenke
2015/01/22 20:53:50
new_tab_path seems more consistent (Particularly g
Mathieu
2015/01/22 21:44:22
Done.
|
+ |
+ 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); |
+ } |
+ |
+ private: |
+ GURL new_tab_url_; |
+ base::FilePath path_; |
+}; |
+ |
+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(); |
+ // Failed navigation makes a redirect to the local NTP. |
+ EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
+ controller->GetLastCommittedEntry()->GetURL()); |
mmenke
2015/01/22 20:53:51
Should probably include gurl.h and gtest.h
Mathieu
2015/01/22 21:44:22
Done.
|
+} |