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
.SetKeyword(base::UTF8ToUTF16(base_url
));
56 data
.SetURL(base_url
+ "url?bar={searchTerms}");
57 data
.new_tab_url
= base_url
+ new_tab_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
);
64 static GURL
GetMockURL(const base::FilePath::StringType
& path
) {
65 return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(path
));
73 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, NoInterception
) {
74 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("instant_extended.html")));
75 ChangeDefaultSearchProvider("instant_extended.html");
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());
85 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, 404Interception
) {
86 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page404.html")));
87 ChangeDefaultSearchProvider("page404.html");
89 ui_test_utils::NavigateToURL(browser(), new_tab_url());
90 content::WebContents
* contents
=
91 browser()->tab_strip_model()->GetWebContentsAt(0);
92 content::NavigationController
* controller
= &contents
->GetController();
93 // 404 makes a redirect to the local NTP.
94 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
95 controller
->GetLastCommittedEntry()->GetURL());
98 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, 204Interception
) {
99 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("page204.html")));
100 ChangeDefaultSearchProvider("page204.html");
102 ui_test_utils::NavigateToURL(browser(), new_tab_url());
103 content::WebContents
* contents
=
104 browser()->tab_strip_model()->GetWebContentsAt(0);
105 content::NavigationController
* controller
= &contents
->GetController();
106 // 204 makes a redirect to the local NTP.
107 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
108 controller
->GetLastCommittedEntry()->GetURL());
111 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest
, FailedRequestInterception
) {
112 set_new_tab_url(GetMockURL(FILE_PATH_LITERAL("notarealfile.html")));
113 ChangeDefaultSearchProvider("notarealfile.html");
115 ui_test_utils::NavigateToURL(browser(), new_tab_url());
116 content::WebContents
* contents
=
117 browser()->tab_strip_model()->GetWebContentsAt(0);
118 content::NavigationController
* controller
= &contents
->GetController();
119 // Failed navigation makes a redirect to the local NTP.
120 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
121 controller
->GetLastCommittedEntry()->GetURL());