OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/files/file_path.h" |
| 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/search/search.h" |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/url_constants.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "components/search_engines/template_url_service.h" |
| 16 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/navigation_entry.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "net/test/url_request/url_request_mock_http_job.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" |
| 22 |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace { |
| 26 |
| 27 void SetUrlRequestMock(const base::FilePath& path) { |
| 28 net::URLRequestMockHTTPJob::AddUrlHandlers(path, |
| 29 BrowserThread::GetBlockingPool()); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 class NewTabPageInterceptorTest : public InProcessBrowserTest { |
| 35 public: |
| 36 NewTabPageInterceptorTest() {} |
| 37 |
| 38 void SetUpOnMainThread() override { |
| 39 path_ = ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath()); |
| 40 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 41 base::Bind(&SetUrlRequestMock, path_)); |
| 42 } |
| 43 |
| 44 const GURL& new_tab_url() const { return new_tab_url_; } |
| 45 |
| 46 void ChangeDefaultSearchProvider(const std::string& new_tab_path) { |
| 47 // Update the New Tab URL for this test case. |
| 48 new_tab_url_ = GetMockURL(new_tab_path); |
| 49 |
| 50 TemplateURLService* template_url_service = |
| 51 TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 52 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| 53 UIThreadSearchTermsData::SetGoogleBaseURL("https://mock.http/"); |
| 54 std::string base_url("{google:baseURL}"); |
| 55 TemplateURLData data; |
| 56 data.SetKeyword(base::UTF8ToUTF16(base_url)); |
| 57 data.SetURL(base_url + "url?bar={searchTerms}"); |
| 58 data.new_tab_url = base_url + new_tab_path; |
| 59 TemplateURL* template_url = new TemplateURL(data); |
| 60 // Takes ownership of |template_url|. |
| 61 template_url_service->Add(template_url); |
| 62 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
| 63 } |
| 64 |
| 65 private: |
| 66 static GURL GetMockURL(const std::string& file) { |
| 67 return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(file)); |
| 68 } |
| 69 |
| 70 GURL new_tab_url_; |
| 71 base::FilePath path_; |
| 72 }; |
| 73 |
| 74 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) { |
| 75 ChangeDefaultSearchProvider("instant_extended.html"); |
| 76 |
| 77 ui_test_utils::NavigateToURL(browser(), new_tab_url()); |
| 78 content::WebContents* contents = |
| 79 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 80 content::NavigationController* controller = &contents->GetController(); |
| 81 // A correct, 200-OK file works correctly. |
| 82 EXPECT_EQ(new_tab_url(), controller->GetLastCommittedEntry()->GetURL()); |
| 83 } |
| 84 |
| 85 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 404Interception) { |
| 86 ChangeDefaultSearchProvider("page404.html"); |
| 87 |
| 88 ui_test_utils::NavigateToURL(browser(), new_tab_url()); |
| 89 content::WebContents* contents = |
| 90 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 91 content::NavigationController* controller = &contents->GetController(); |
| 92 // 404 makes a redirect to the local NTP. |
| 93 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 94 controller->GetLastCommittedEntry()->GetURL()); |
| 95 } |
| 96 |
| 97 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 204Interception) { |
| 98 ChangeDefaultSearchProvider("page204.html"); |
| 99 |
| 100 ui_test_utils::NavigateToURL(browser(), new_tab_url()); |
| 101 content::WebContents* contents = |
| 102 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 103 content::NavigationController* controller = &contents->GetController(); |
| 104 // 204 makes a redirect to the local NTP. |
| 105 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 106 controller->GetLastCommittedEntry()->GetURL()); |
| 107 } |
| 108 |
| 109 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, FailedRequestInterception) { |
| 110 ChangeDefaultSearchProvider("notarealfile.html"); |
| 111 |
| 112 ui_test_utils::NavigateToURL(browser(), new_tab_url()); |
| 113 content::WebContents* contents = |
| 114 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 115 content::NavigationController* controller = &contents->GetController(); |
| 116 // Failed navigation makes a redirect to the local NTP. |
| 117 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 118 controller->GetLastCommittedEntry()->GetURL()); |
| 119 } |
OLD | NEW |