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.
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/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
20 #include "net/test/url_request/url_request_mock_http_job.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 using content::BrowserThread
;
28 void SetUrlRequestMock(const base::FilePath
& path
) {
29 net::URLRequestMockHTTPJob::AddUrlHandlers(path
,
30 BrowserThread::GetBlockingPool());
35 class NewTabPageInterceptorTest
: public InProcessBrowserTest
{
37 NewTabPageInterceptorTest() {}
39 void SetUpOnMainThread() override
{
40 path_
= ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath());
41 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
42 base::Bind(&SetUrlRequestMock
, path_
));
45 const GURL
& new_tab_url() const { return new_tab_url_
; }
46 void set_new_tab_url(const GURL
& url
) { new_tab_url_
= url
; }
48 void ChangeDefaultSearchProvider(const char* new_tab_path
) {
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}");
55 data
.SetShortName(base::ASCIIToUTF16("Google"));
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
);
65 static GURL
GetMockURL(const base::FilePath::StringType
& path
) {
66 return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(path
));
74 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, NoInterception
) {
75 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("instant_extended.html")));
76 ChangeDefaultSearchProvider("instant_extended.html");
78 ui_test_utils::NavigateToURL(browser(), new_tab_url());
79 content::WebContents
* contents
=
80 browser()->tab_strip_model()->GetWebContentsAt(0);
81 content::NavigationController
* controller
= &contents
->GetController();
82 // A correct, 200-OK file works correctly.
83 EXPECT_EQ(new_tab_url(), controller
->GetLastCommittedEntry()->GetURL());
86 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, 404Interception
) {
87 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page404.html")));
88 ChangeDefaultSearchProvider("page404.html");
90 ui_test_utils::NavigateToURL(browser(), new_tab_url());
91 content::WebContents
* contents
=
92 browser()->tab_strip_model()->GetWebContentsAt(0);
93 content::NavigationController
* controller
= &contents
->GetController();
94 // 404 makes a redirect to the local NTP.
95 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
96 controller
->GetLastCommittedEntry()->GetURL());
99 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, 204Interception
) {
100 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page204.html")));
101 ChangeDefaultSearchProvider("page204.html");
103 ui_test_utils::NavigateToURL(browser(), new_tab_url());
104 content::WebContents
* contents
=
105 browser()->tab_strip_model()->GetWebContentsAt(0);
106 content::NavigationController
* controller
= &contents
->GetController();
107 // 204 makes a redirect to the local NTP.
108 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
109 controller
->GetLastCommittedEntry()->GetURL());
112 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, FailedRequestInterception
) {
113 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("notarealfile.html")));
114 ChangeDefaultSearchProvider("notarealfile.html");
116 ui_test_utils::NavigateToURL(browser(), new_tab_url());
117 content::WebContents
* contents
=
118 browser()->tab_strip_model()->GetWebContentsAt(0);
119 content::NavigationController
* controller
= &contents
->GetController();
120 // Failed navigation makes a redirect to the local NTP.
121 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
122 controller
->GetLastCommittedEntry()->GetURL());