OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace { |
| 24 void SetUrlRequestMock(const base::FilePath& path) { |
| 25 net::URLRequestMockHTTPJob::AddUrlHandlers(path, |
| 26 BrowserThread::GetBlockingPool()); |
| 27 } |
| 28 } // namespace |
| 29 |
| 30 class NewTabPageInterceptorTest : public InProcessBrowserTest { |
| 31 public: |
| 32 NewTabPageInterceptorTest() {} |
| 33 |
| 34 protected: |
| 35 void SetUpOnMainThread() override { |
| 36 path_ = ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath()); |
| 37 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 38 base::Bind(&SetUrlRequestMock, path_)); |
| 39 } |
| 40 |
| 41 GURL GetMockURL(const std::string& file) { |
| 42 return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(file)); |
| 43 } |
| 44 |
| 45 void ChangeDefaultSearchProvider(const std::string& newtab_path) { |
| 46 // Update the New Tab URL for this test case. |
| 47 new_tab_url_ = GetMockURL(newtab_path); |
| 48 |
| 49 TemplateURLService* template_url_service = |
| 50 TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 51 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| 52 UIThreadSearchTermsData::SetGoogleBaseURL("https://mock.http/"); |
| 53 std::string base_url("{google:baseURL}"); |
| 54 TemplateURLData data; |
| 55 data.SetKeyword(base::UTF8ToUTF16(base_url)); |
| 56 data.SetURL(base_url + "url?bar={searchTerms}"); |
| 57 data.new_tab_url = base_url + newtab_path; |
| 58 TemplateURL* template_url = new TemplateURL(data); |
| 59 // Takes ownership of |template_url|. |
| 60 template_url_service->Add(template_url); |
| 61 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
| 62 } |
| 63 |
| 64 GURL new_tab_url_; |
| 65 base::FilePath path_; |
| 66 }; |
| 67 |
| 68 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) { |
| 69 ChangeDefaultSearchProvider("instant_extended.html"); |
| 70 |
| 71 ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| 72 content::WebContents* contents = |
| 73 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 74 content::NavigationController* controller = &contents->GetController(); |
| 75 // A correct, 200-OK file works correctly. |
| 76 EXPECT_EQ(new_tab_url_, controller->GetLastCommittedEntry()->GetURL()); |
| 77 } |
| 78 |
| 79 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 404Interception) { |
| 80 ChangeDefaultSearchProvider("page404.html"); |
| 81 |
| 82 ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| 83 content::WebContents* contents = |
| 84 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 85 content::NavigationController* controller = &contents->GetController(); |
| 86 // 404 makes a redirect to the local NTP. |
| 87 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 88 controller->GetLastCommittedEntry()->GetURL()); |
| 89 } |
| 90 |
| 91 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 204Interception) { |
| 92 ChangeDefaultSearchProvider("page204.html"); |
| 93 |
| 94 ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| 95 content::WebContents* contents = |
| 96 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 97 content::NavigationController* controller = &contents->GetController(); |
| 98 // 204 makes a redirect to the local NTP. |
| 99 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 100 controller->GetLastCommittedEntry()->GetURL()); |
| 101 } |
| 102 |
| 103 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, FailedRequestInterception) { |
| 104 ChangeDefaultSearchProvider("notarealfile.html"); |
| 105 |
| 106 ui_test_utils::NavigateToURL(browser(), new_tab_url_); |
| 107 content::WebContents* contents = |
| 108 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 109 content::NavigationController* controller = &contents->GetController(); |
| 110 // 204 makes a redirect to the local NTP. |
| 111 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 112 controller->GetLastCommittedEntry()->GetURL()); |
| 113 } |
OLD | NEW |