| 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "chrome/browser/download/download_prefs.h" | 7 #include "chrome/browser/download/download_prefs.h" |
| 8 #include "chrome/browser/extensions/event_router.h" | 8 #include "chrome/browser/extensions/event_router.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_info_map.h" | 10 #include "chrome/browser/extensions/extension_info_map.h" |
| 11 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/extensions/mime_types_handler.h" | 15 #include "chrome/common/extensions/mime_types_handler.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/download_item.h" | 18 #include "content/public/browser/download_item.h" |
| 19 #include "content/public/browser/download_manager.h" | 19 #include "content/public/browser/download_manager.h" |
| 20 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/resource_controller.h" | 21 #include "content/public/browser/resource_controller.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/test/download_test_observer.h" | 23 #include "content/public/test/download_test_observer.h" |
| 24 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 24 #include "net/test/embedded_test_server/http_request.h" | 25 #include "net/test/embedded_test_server/http_request.h" |
| 25 #include "net/test/embedded_test_server/http_response.h" | 26 #include "net/test/embedded_test_server/http_response.h" |
| 26 #include "net/test/embedded_test_server/http_server.h" | |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 | 28 |
| 29 using content::BrowserContext; | 29 using content::BrowserContext; |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::DownloadItem; | 31 using content::DownloadItem; |
| 32 using content::DownloadManager; | 32 using content::DownloadManager; |
| 33 using content::DownloadUrlParameters; | 33 using content::DownloadUrlParameters; |
| 34 using content::ResourceController; | 34 using content::ResourceController; |
| 35 using content::WebContents; | 35 using content::WebContents; |
| 36 using extensions::Event; | 36 using extensions::Event; |
| 37 using extensions::ExtensionSystem; | 37 using extensions::ExtensionSystem; |
| 38 using google_apis::test_server::HttpRequest; | 38 using net::test_server::HttpRequest; |
| 39 using google_apis::test_server::HttpResponse; | 39 using net::test_server::HttpResponse; |
| 40 using google_apis::test_server::HttpServer; | 40 using net::test_server::EmbeddedTestServer; |
| 41 using testing::_; | 41 using testing::_; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 // Test server's request handler. | 45 // Test server's request handler. |
| 46 // Returns response that should be sent by the test server. | 46 // Returns response that should be sent by the test server. |
| 47 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { | 47 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
| 48 scoped_ptr<HttpResponse> response(new HttpResponse()); | 48 scoped_ptr<HttpResponse> response(new HttpResponse()); |
| 49 | 49 |
| 50 // For relative path "/doc_path.doc", return success response with MIME type | 50 // For relative path "/doc_path.doc", return success response with MIME type |
| 51 // "application/msword". | 51 // "application/msword". |
| 52 if (request.relative_url == "/doc_path.doc") { | 52 if (request.relative_url == "/doc_path.doc") { |
| 53 response->set_code(google_apis::test_server::SUCCESS); | 53 response->set_code(net::test_server::SUCCESS); |
| 54 response->set_content_type("application/msword"); | 54 response->set_content_type("application/msword"); |
| 55 return response.Pass(); | 55 return response.Pass(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // For relative path "/test_path_attch.txt", return success response with | 58 // For relative path "/test_path_attch.txt", return success response with |
| 59 // MIME type "plain/text" and content "txt content". Also, set content | 59 // MIME type "plain/text" and content "txt content". Also, set content |
| 60 // disposition to be attachment. | 60 // disposition to be attachment. |
| 61 if (request.relative_url == "/text_path_attch.txt") { | 61 if (request.relative_url == "/text_path_attch.txt") { |
| 62 response->set_code(google_apis::test_server::SUCCESS); | 62 response->set_code(net::test_server::SUCCESS); |
| 63 response->set_content("txt content"); | 63 response->set_content("txt content"); |
| 64 response->set_content_type("plain/text"); | 64 response->set_content_type("plain/text"); |
| 65 response->AddCustomHeader("Content-Disposition", | 65 response->AddCustomHeader("Content-Disposition", |
| 66 "attachment; filename=test_path.txt"); | 66 "attachment; filename=test_path.txt"); |
| 67 return response.Pass(); | 67 return response.Pass(); |
| 68 } | 68 } |
| 69 // For relative path "/test_path_attch.txt", return success response with | 69 // For relative path "/test_path_attch.txt", return success response with |
| 70 // MIME type "plain/text" and content "txt content". | 70 // MIME type "plain/text" and content "txt content". |
| 71 if (request.relative_url == "/text_path.txt") { | 71 if (request.relative_url == "/text_path.txt") { |
| 72 response->set_code(google_apis::test_server::SUCCESS); | 72 response->set_code(net::test_server::SUCCESS); |
| 73 response->set_content("txt content"); | 73 response->set_content("txt content"); |
| 74 response->set_content_type("plain/text"); | 74 response->set_content_type("plain/text"); |
| 75 return response.Pass(); | 75 return response.Pass(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // No other requests should be handled in the tests. | 78 // No other requests should be handled in the tests. |
| 79 EXPECT_TRUE(false) << "NOTREACHED!"; | 79 EXPECT_TRUE(false) << "NOTREACHED!"; |
| 80 response->set_code(google_apis::test_server::NOT_FOUND); | 80 response->set_code(net::test_server::NOT_FOUND); |
| 81 return response.Pass(); | 81 return response.Pass(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Tests to verify that resources are correctly intercepted by | 84 // Tests to verify that resources are correctly intercepted by |
| 85 // StreamsResourceThrottle. | 85 // StreamsResourceThrottle. |
| 86 // The test extension expects the resources that should be handed to the | 86 // The test extension expects the resources that should be handed to the |
| 87 // extension to have MIME type 'application/msword' and the resources that | 87 // extension to have MIME type 'application/msword' and the resources that |
| 88 // should be downloaded by the browser to have MIME type 'plain/text'. | 88 // should be downloaded by the browser to have MIME type 'plain/text'. |
| 89 class StreamsPrivateApiTest : public ExtensionApiTest { | 89 class StreamsPrivateApiTest : public ExtensionApiTest { |
| 90 public: | 90 public: |
| 91 StreamsPrivateApiTest() {} | 91 StreamsPrivateApiTest() {} |
| 92 | 92 |
| 93 virtual ~StreamsPrivateApiTest() {} | 93 virtual ~StreamsPrivateApiTest() {} |
| 94 | 94 |
| 95 virtual void SetUpOnMainThread() OVERRIDE { | 95 virtual void SetUpOnMainThread() OVERRIDE { |
| 96 // Init test server. | 96 // Init test server. |
| 97 test_server_.reset(new HttpServer( | 97 test_server_.reset(new EmbeddedTestServer( |
| 98 content::BrowserThread::GetMessageLoopProxyForThread( | 98 content::BrowserThread::GetMessageLoopProxyForThread( |
| 99 content::BrowserThread::IO))); | 99 content::BrowserThread::IO))); |
| 100 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady()); | 100 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady()); |
| 101 test_server_->RegisterRequestHandler(base::Bind(&HandleRequest)); | 101 test_server_->RegisterRequestHandler(base::Bind(&HandleRequest)); |
| 102 | 102 |
| 103 ExtensionApiTest::SetUpOnMainThread(); | 103 ExtensionApiTest::SetUpOnMainThread(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 virtual void CleanUpOnMainThread() OVERRIDE { | 106 virtual void CleanUpOnMainThread() OVERRIDE { |
| 107 // Tear down the test server. | 107 // Tear down the test server. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 DownloadManager* manager) { | 185 DownloadManager* manager) { |
| 186 scoped_refptr<content::DownloadTestFlushObserver> flush_observer( | 186 scoped_refptr<content::DownloadTestFlushObserver> flush_observer( |
| 187 new content::DownloadTestFlushObserver(manager)); | 187 new content::DownloadTestFlushObserver(manager)); |
| 188 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 188 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 189 flush_observer->WaitForFlush(); | 189 flush_observer->WaitForFlush(); |
| 190 } | 190 } |
| 191 | 191 |
| 192 protected: | 192 protected: |
| 193 std::string test_extension_id_; | 193 std::string test_extension_id_; |
| 194 // The HTTP server used in the tests. | 194 // The HTTP server used in the tests. |
| 195 scoped_ptr<HttpServer> test_server_; | 195 scoped_ptr<EmbeddedTestServer> test_server_; |
| 196 base::ScopedTempDir downloads_dir_; | 196 base::ScopedTempDir downloads_dir_; |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 // Tests that navigating to a resource with a MIME type handleable by an | 199 // Tests that navigating to a resource with a MIME type handleable by an |
| 200 // installed, white-listed extension invokes the extension's | 200 // installed, white-listed extension invokes the extension's |
| 201 // onExecuteContentHandler event (and does not start a download). | 201 // onExecuteContentHandler event (and does not start a download). |
| 202 IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, Navigate) { | 202 IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, Navigate) { |
| 203 ASSERT_TRUE(LoadTestExtension()) << message_; | 203 ASSERT_TRUE(LoadTestExtension()) << message_; |
| 204 | 204 |
| 205 ResultCatcher catcher; | 205 ResultCatcher catcher; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // The test extension should not receive any events by now. Send it an event | 305 // The test extension should not receive any events by now. Send it an event |
| 306 // with MIME type "test/done", so it stops waiting for the events. (If there | 306 // with MIME type "test/done", so it stops waiting for the events. (If there |
| 307 // was an event with MIME type 'plain/text', |catcher.GetNextResult()| will | 307 // was an event with MIME type 'plain/text', |catcher.GetNextResult()| will |
| 308 // fail regardless of the sent event; chrome.test.notifySuccess will not be | 308 // fail regardless of the sent event; chrome.test.notifySuccess will not be |
| 309 // called by the extension). | 309 // called by the extension). |
| 310 SendDoneEvent(); | 310 SendDoneEvent(); |
| 311 EXPECT_TRUE(catcher.GetNextResult()); | 311 EXPECT_TRUE(catcher.GetNextResult()); |
| 312 } | 312 } |
| 313 | 313 |
| 314 } // namespace | 314 } // namespace |
| OLD | NEW |