Chromium Code Reviews| 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 | |
| 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 | |
|
mmenke
2015/01/22 20:53:51
Suggest a linebreak just after the start and just
Mathieu
2015/01/22 21:44:22
Done.
| |
| 29 | |
| 30 class NewTabPageInterceptorTest : public InProcessBrowserTest { | |
| 31 public: | |
| 32 NewTabPageInterceptorTest() {} | |
| 33 | |
| 34 void SetUpOnMainThread() override { | |
| 35 path_ = ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath()); | |
| 36 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 37 base::Bind(&SetUrlRequestMock, path_)); | |
| 38 } | |
| 39 | |
| 40 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.
| |
| 41 | |
| 42 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.
| |
| 43 return net::URLRequestMockHTTPJob::GetMockHttpsUrl(base::FilePath(file)); | |
| 44 } | |
| 45 | |
| 46 void ChangeDefaultSearchProvider(const std::string& newtab_path) { | |
| 47 // Update the New Tab URL for this test case. | |
| 48 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.
| |
| 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 + newtab_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 GURL new_tab_url_; | |
| 67 base::FilePath path_; | |
| 68 }; | |
| 69 | |
| 70 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) { | |
| 71 ChangeDefaultSearchProvider("instant_extended.html"); | |
| 72 | |
| 73 ui_test_utils::NavigateToURL(browser(), new_tab_url()); | |
| 74 content::WebContents* contents = | |
| 75 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 76 content::NavigationController* controller = &contents->GetController(); | |
| 77 // A correct, 200-OK file works correctly. | |
| 78 EXPECT_EQ(new_tab_url(), controller->GetLastCommittedEntry()->GetURL()); | |
| 79 } | |
| 80 | |
| 81 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 404Interception) { | |
| 82 ChangeDefaultSearchProvider("page404.html"); | |
| 83 | |
| 84 ui_test_utils::NavigateToURL(browser(), new_tab_url()); | |
| 85 content::WebContents* contents = | |
| 86 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 87 content::NavigationController* controller = &contents->GetController(); | |
| 88 // 404 makes a redirect to the local NTP. | |
| 89 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | |
| 90 controller->GetLastCommittedEntry()->GetURL()); | |
| 91 } | |
| 92 | |
| 93 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 204Interception) { | |
| 94 ChangeDefaultSearchProvider("page204.html"); | |
| 95 | |
| 96 ui_test_utils::NavigateToURL(browser(), new_tab_url()); | |
| 97 content::WebContents* contents = | |
| 98 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 99 content::NavigationController* controller = &contents->GetController(); | |
| 100 // 204 makes a redirect to the local NTP. | |
| 101 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | |
| 102 controller->GetLastCommittedEntry()->GetURL()); | |
| 103 } | |
| 104 | |
| 105 IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, FailedRequestInterception) { | |
| 106 ChangeDefaultSearchProvider("notarealfile.html"); | |
| 107 | |
| 108 ui_test_utils::NavigateToURL(browser(), new_tab_url()); | |
| 109 content::WebContents* contents = | |
| 110 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 111 content::NavigationController* controller = &contents->GetController(); | |
| 112 // Failed navigation makes a redirect to the local NTP. | |
| 113 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | |
| 114 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.
| |
| 115 } | |
| OLD | NEW |