OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/download/download_prefs.h" | 8 #include "chrome/browser/download/download_prefs.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/common/extensions/mime_types_handler.h" | 13 #include "chrome/common/extensions/mime_types_handler.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/test_switches.h" | 15 #include "chrome/test/base/test_switches.h" |
16 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
17 #include "content/public/browser/download_item.h" | 17 #include "content/public/browser/download_item.h" |
18 #include "content/public/browser/download_manager.h" | 18 #include "content/public/browser/download_manager.h" |
19 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/resource_controller.h" | 20 #include "content/public/browser/resource_controller.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/test/download_test_observer.h" | 22 #include "content/public/test/download_test_observer.h" |
23 #include "extensions/browser/event_router.h" | 23 #include "extensions/browser/event_router.h" |
24 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 25 #include "net/dns/mock_host_resolver.h" |
25 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
26 #include "net/test/embedded_test_server/http_request.h" | 27 #include "net/test/embedded_test_server/http_request.h" |
27 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
29 | 30 |
30 using content::BrowserContext; | 31 using content::BrowserContext; |
31 using content::BrowserThread; | 32 using content::BrowserThread; |
32 using content::DownloadItem; | 33 using content::DownloadItem; |
33 using content::DownloadManager; | 34 using content::DownloadManager; |
34 using content::DownloadUrlParameters; | 35 using content::DownloadUrlParameters; |
(...skipping 15 matching lines...) Expand all Loading... |
50 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse()); | 51 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse()); |
51 | 52 |
52 // For relative path "/doc_path.doc", return success response with MIME type | 53 // For relative path "/doc_path.doc", return success response with MIME type |
53 // "application/msword". | 54 // "application/msword". |
54 if (request.relative_url == "/doc_path.doc") { | 55 if (request.relative_url == "/doc_path.doc") { |
55 response->set_code(net::HTTP_OK); | 56 response->set_code(net::HTTP_OK); |
56 response->set_content_type("application/msword"); | 57 response->set_content_type("application/msword"); |
57 return response.PassAs<HttpResponse>(); | 58 return response.PassAs<HttpResponse>(); |
58 } | 59 } |
59 | 60 |
60 // For relative path "/test_path_attch.txt", return success response with | 61 // For relative path "/text_path_attch.txt", return success response with |
61 // MIME type "plain/text" and content "txt content". Also, set content | 62 // MIME type "plain/text" and content "txt content". Also, set content |
62 // disposition to be attachment. | 63 // disposition to be attachment. |
63 if (request.relative_url == "/text_path_attch.txt") { | 64 if (request.relative_url == "/text_path_attch.txt") { |
64 response->set_code(net::HTTP_OK); | 65 response->set_code(net::HTTP_OK); |
65 response->set_content("txt content"); | 66 response->set_content("txt content"); |
66 response->set_content_type("plain/text"); | 67 response->set_content_type("plain/text"); |
67 response->AddCustomHeader("Content-Disposition", | 68 response->AddCustomHeader("Content-Disposition", |
68 "attachment; filename=test_path.txt"); | 69 "attachment; filename=test_path.txt"); |
69 return response.PassAs<HttpResponse>(); | 70 return response.PassAs<HttpResponse>(); |
70 } | 71 } |
| 72 |
71 // For relative path "/test_path_attch.txt", return success response with | 73 // For relative path "/test_path_attch.txt", return success response with |
72 // MIME type "plain/text" and content "txt content". | 74 // MIME type "plain/text" and content "txt content". |
73 if (request.relative_url == "/text_path.txt") { | 75 if (request.relative_url == "/text_path.txt") { |
74 response->set_code(net::HTTP_OK); | 76 response->set_code(net::HTTP_OK); |
75 response->set_content("txt content"); | 77 response->set_content("txt content"); |
76 response->set_content_type("plain/text"); | 78 response->set_content_type("plain/text"); |
77 return response.PassAs<HttpResponse>(); | 79 return response.PassAs<HttpResponse>(); |
78 } | 80 } |
79 | 81 |
| 82 // A random HTML file to navigate to. |
| 83 if (request.relative_url == "/index.html") { |
| 84 response->set_code(net::HTTP_OK); |
| 85 response->set_content("html content"); |
| 86 response->set_content_type("text/html"); |
| 87 return response.PassAs<HttpResponse>(); |
| 88 } |
| 89 |
| 90 // Respond to /favicon.ico for navigating to the page. |
| 91 if (request.relative_url == "/favicon.ico") { |
| 92 response->set_code(net::HTTP_NOT_FOUND); |
| 93 return response.PassAs<HttpResponse>(); |
| 94 } |
| 95 |
80 // No other requests should be handled in the tests. | 96 // No other requests should be handled in the tests. |
81 EXPECT_TRUE(false) << "NOTREACHED!"; | 97 EXPECT_TRUE(false) << "NOTREACHED!"; |
82 response->set_code(net::HTTP_NOT_FOUND); | 98 response->set_code(net::HTTP_NOT_FOUND); |
83 return response.PassAs<HttpResponse>(); | 99 return response.PassAs<HttpResponse>(); |
84 } | 100 } |
85 | 101 |
86 // Tests to verify that resources are correctly intercepted by | 102 // Tests to verify that resources are correctly intercepted by |
87 // StreamsResourceThrottle. | 103 // StreamsResourceThrottle. |
88 // The test extension expects the resources that should be handed to the | 104 // The test extension expects the resources that should be handed to the |
89 // extension to have MIME type 'application/msword' and the resources that | 105 // extension to have MIME type 'application/msword' and the resources that |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 DownloadManager* download_manager = GetDownloadManager(); | 237 DownloadManager* download_manager = GetDownloadManager(); |
222 std::vector<DownloadItem*> downloads; | 238 std::vector<DownloadItem*> downloads; |
223 download_manager->GetAllDownloads(&downloads); | 239 download_manager->GetAllDownloads(&downloads); |
224 ASSERT_EQ(0u, downloads.size()); | 240 ASSERT_EQ(0u, downloads.size()); |
225 | 241 |
226 // The test extension should receive onExecuteContentHandler event with MIME | 242 // The test extension should receive onExecuteContentHandler event with MIME |
227 // type 'application/msword' (and call chrome.test.notifySuccess). | 243 // type 'application/msword' (and call chrome.test.notifySuccess). |
228 EXPECT_TRUE(catcher.GetNextResult()); | 244 EXPECT_TRUE(catcher.GetNextResult()); |
229 } | 245 } |
230 | 246 |
| 247 // Tests that navigating cross-site to a resource with a MIME type handleable by |
| 248 // an installed, white-listed extension invokes the extension's |
| 249 // onExecuteContentHandler event (and does not start a download). |
| 250 // Regression test for http://crbug.com/342999. |
| 251 IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, NavigateCrossSite) { |
| 252 #if defined(OS_WIN) && defined(USE_ASH) |
| 253 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
| 254 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) |
| 255 return; |
| 256 #endif |
| 257 |
| 258 ASSERT_TRUE(LoadTestExtension()) << message_; |
| 259 |
| 260 ResultCatcher catcher; |
| 261 |
| 262 // Navigate to a URL on a different hostname. |
| 263 std::string initial_host = "www.example.com"; |
| 264 host_resolver()->AddRule(initial_host, "127.0.0.1"); |
| 265 GURL::Replacements replacements; |
| 266 replacements.SetHostStr(initial_host); |
| 267 GURL initial_url = |
| 268 test_server_->GetURL("/index.html").ReplaceComponents(replacements); |
| 269 ui_test_utils::NavigateToURL(browser(), initial_url); |
| 270 |
| 271 // Now navigate to the doc file; the extension should pick it up normally. |
| 272 ui_test_utils::NavigateToURL(browser(), |
| 273 test_server_->GetURL("/doc_path.doc")); |
| 274 |
| 275 // Wait for the response from the test server. |
| 276 base::MessageLoop::current()->RunUntilIdle(); |
| 277 |
| 278 // There should be no downloads started by the navigation. |
| 279 DownloadManager* download_manager = GetDownloadManager(); |
| 280 std::vector<DownloadItem*> downloads; |
| 281 download_manager->GetAllDownloads(&downloads); |
| 282 ASSERT_EQ(0u, downloads.size()); |
| 283 |
| 284 // The test extension should receive onExecuteContentHandler event with MIME |
| 285 // type 'application/msword' (and call chrome.test.notifySuccess). |
| 286 EXPECT_TRUE(catcher.GetNextResult()); |
| 287 } |
| 288 |
231 // Tests that navigation to an attachment starts a download, even if there is an | 289 // Tests that navigation to an attachment starts a download, even if there is an |
232 // extension with a file browser handler that can handle the attachment's MIME | 290 // extension with a file browser handler that can handle the attachment's MIME |
233 // type. | 291 // type. |
234 IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, NavigateToAnAttachment) { | 292 IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, NavigateToAnAttachment) { |
235 InitializeDownloadSettings(); | 293 InitializeDownloadSettings(); |
236 | 294 |
237 ASSERT_TRUE(LoadTestExtension()) << message_; | 295 ASSERT_TRUE(LoadTestExtension()) << message_; |
238 | 296 |
239 ResultCatcher catcher; | 297 ResultCatcher catcher; |
240 | 298 |
241 // The test should start a downloadm. | 299 // The test should start a download. |
242 DownloadManager* download_manager = GetDownloadManager(); | 300 DownloadManager* download_manager = GetDownloadManager(); |
243 scoped_ptr<content::DownloadTestObserver> download_observer( | 301 scoped_ptr<content::DownloadTestObserver> download_observer( |
244 new content::DownloadTestObserverInProgress(download_manager, 1)); | 302 new content::DownloadTestObserverInProgress(download_manager, 1)); |
245 | 303 |
246 ui_test_utils::NavigateToURL(browser(), | 304 ui_test_utils::NavigateToURL(browser(), |
247 test_server_->GetURL("/text_path_attch.txt")); | 305 test_server_->GetURL("/text_path_attch.txt")); |
248 | 306 |
249 // Wait for the download to start. | 307 // Wait for the download to start. |
250 download_observer->WaitForFinished(); | 308 download_observer->WaitForFinished(); |
251 | 309 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 // The test extension should not receive any events by now. Send it an event | 370 // The test extension should not receive any events by now. Send it an event |
313 // with MIME type "test/done", so it stops waiting for the events. (If there | 371 // with MIME type "test/done", so it stops waiting for the events. (If there |
314 // was an event with MIME type 'plain/text', |catcher.GetNextResult()| will | 372 // was an event with MIME type 'plain/text', |catcher.GetNextResult()| will |
315 // fail regardless of the sent event; chrome.test.notifySuccess will not be | 373 // fail regardless of the sent event; chrome.test.notifySuccess will not be |
316 // called by the extension). | 374 // called by the extension). |
317 SendDoneEvent(); | 375 SendDoneEvent(); |
318 EXPECT_TRUE(catcher.GetNextResult()); | 376 EXPECT_TRUE(catcher.GetNextResult()); |
319 } | 377 } |
320 | 378 |
321 } // namespace | 379 } // namespace |
OLD | NEW |