| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // Tests exercising the Chrome Plugin API. | 4 // Tests exercising the Chrome Plugin API. |
| 5 | 5 |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/chrome_plugin_host.h" | 9 #include "chrome/browser/chrome_plugin_host.h" |
| 10 #include "chrome/browser/net/url_request_context_getter.h" |
| 10 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 11 #include "chrome/common/chrome_plugin_lib.h" | 12 #include "chrome/common/chrome_plugin_lib.h" |
| 12 #include "chrome/test/chrome_plugin/test_chrome_plugin.h" | 13 #include "chrome/test/chrome_plugin/test_chrome_plugin.h" |
| 13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request_test_job.h" | 16 #include "net/url_request/url_request_test_job.h" |
| 16 #include "net/url_request/url_request_unittest.h" | 17 #include "net/url_request/url_request_unittest.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const wchar_t kDocRoot[] = L"chrome/test/data"; | 22 const wchar_t kDocRoot[] = L"chrome/test/data"; |
| 22 const char kPluginFilename[] = "test_chrome_plugin.dll"; | 23 const char kPluginFilename[] = "test_chrome_plugin.dll"; |
| 23 const int kResponseBufferSize = 4096; | 24 const int kResponseBufferSize = 4096; |
| 24 | 25 |
| 26 class TestURLRequestContextGetter : public URLRequestContextGetter { |
| 27 public: |
| 28 virtual URLRequestContext* GetURLRequestContext() { |
| 29 if (!context_) |
| 30 context_ = new TestURLRequestContext(); |
| 31 return context_; |
| 32 } |
| 33 private: |
| 34 scoped_refptr<URLRequestContext> context_; |
| 35 }; |
| 36 |
| 25 class ChromePluginTest : public testing::Test, public URLRequest::Delegate { | 37 class ChromePluginTest : public testing::Test, public URLRequest::Delegate { |
| 26 public: | 38 public: |
| 27 ChromePluginTest() | 39 ChromePluginTest() |
| 28 : request_(NULL), | 40 : request_(NULL), |
| 29 response_buffer_(new net::IOBuffer(kResponseBufferSize)), | 41 response_buffer_(new net::IOBuffer(kResponseBufferSize)), |
| 30 plugin_(NULL), | 42 plugin_(NULL), |
| 31 expected_payload_(NULL), | 43 expected_payload_(NULL), |
| 32 request_context_(new TestURLRequestContext()) { | 44 request_context_getter_(new TestURLRequestContextGetter()) { |
| 33 test_funcs_.test_make_request = NULL; | 45 test_funcs_.test_make_request = NULL; |
| 34 } | 46 } |
| 35 | 47 |
| 36 // Loads/unloads the chrome test plugin. | 48 // Loads/unloads the chrome test plugin. |
| 37 void LoadPlugin(); | 49 void LoadPlugin(); |
| 38 void UnloadPlugin(); | 50 void UnloadPlugin(); |
| 39 | 51 |
| 40 // Runs the test and expects the given payload as a response. If expectation | 52 // Runs the test and expects the given payload as a response. If expectation |
| 41 // is NULL, the request is expected to fail. | 53 // is NULL, the request is expected to fail. |
| 42 void RunTest(const GURL& url, const TestResponsePayload* expected_payload); | 54 void RunTest(const GURL& url, const TestResponsePayload* expected_payload); |
| 43 | 55 |
| 44 // URLRequest::Delegate implementations | 56 // URLRequest::Delegate implementations |
| 45 virtual void OnResponseStarted(URLRequest* request); | 57 virtual void OnResponseStarted(URLRequest* request); |
| 46 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | 58 virtual void OnReadCompleted(URLRequest* request, int bytes_read); |
| 47 | 59 |
| 48 // Helper called when the URLRequest is done. | 60 // Helper called when the URLRequest is done. |
| 49 void OnURLRequestComplete(); | 61 void OnURLRequestComplete(); |
| 50 | 62 |
| 51 // testing::Test | 63 // testing::Test |
| 52 virtual void SetUp() { | 64 virtual void SetUp() { |
| 53 LoadPlugin(); | 65 LoadPlugin(); |
| 54 URLRequest::RegisterProtocolFactory("test", &URLRequestTestJob::Factory); | 66 URLRequest::RegisterProtocolFactory("test", &URLRequestTestJob::Factory); |
| 55 | 67 |
| 56 // We need to setup a default request context in order to issue HTTP | 68 // We need to setup a default request context in order to issue HTTP |
| 57 // requests. | 69 // requests. |
| 58 DCHECK(!Profile::GetDefaultRequestContext()); | 70 DCHECK(!Profile::GetDefaultRequestContext()); |
| 59 Profile::set_default_request_context(request_context_.get()); | 71 Profile::set_default_request_context(request_context_getter_.get()); |
| 60 } | 72 } |
| 61 virtual void TearDown() { | 73 virtual void TearDown() { |
| 62 UnloadPlugin(); | 74 UnloadPlugin(); |
| 63 URLRequest::RegisterProtocolFactory("test", NULL); | 75 URLRequest::RegisterProtocolFactory("test", NULL); |
| 64 | 76 |
| 65 Profile::set_default_request_context(NULL); | 77 Profile::set_default_request_context(NULL); |
| 66 | 78 |
| 67 // Clear the request before flushing the message loop since killing the | 79 // Clear the request before flushing the message loop since killing the |
| 68 // request can result in the generation of more tasks. | 80 // request can result in the generation of more tasks. |
| 69 request_.reset(); | 81 request_.reset(); |
| 70 | 82 |
| 71 // Flush the message loop to make Purify happy. | 83 // Flush the message loop to make Purify happy. |
| 72 message_loop_.RunAllPending(); | 84 message_loop_.RunAllPending(); |
| 73 } | 85 } |
| 74 protected: | 86 protected: |
| 75 MessageLoopForIO message_loop_; | 87 MessageLoopForIO message_loop_; |
| 76 | 88 |
| 77 // Note: we use URLRequest (instead of URLFetcher) because this allows the | 89 // Note: we use URLRequest (instead of URLFetcher) because this allows the |
| 78 // request to be intercepted. | 90 // request to be intercepted. |
| 79 scoped_ptr<URLRequest> request_; | 91 scoped_ptr<URLRequest> request_; |
| 80 scoped_refptr<net::IOBuffer> response_buffer_; | 92 scoped_refptr<net::IOBuffer> response_buffer_; |
| 81 std::string response_data_; | 93 std::string response_data_; |
| 82 | 94 |
| 83 ChromePluginLib* plugin_; | 95 ChromePluginLib* plugin_; |
| 84 TestFuncParams::PluginFuncs test_funcs_; | 96 TestFuncParams::PluginFuncs test_funcs_; |
| 85 const TestResponsePayload* expected_payload_; | 97 const TestResponsePayload* expected_payload_; |
| 86 scoped_refptr<URLRequestContext> request_context_; | 98 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| 87 }; | 99 }; |
| 88 | 100 |
| 89 static void STDCALL CPT_Complete(CPRequest* request, bool success, | 101 static void STDCALL CPT_Complete(CPRequest* request, bool success, |
| 90 const std::string& raw_headers, | 102 const std::string& raw_headers, |
| 91 const std::string& body) { | 103 const std::string& body) { |
| 92 GURL url(request->url); | 104 GURL url(request->url); |
| 93 if (url == GURL(kChromeTestPluginPayloads[0].url)) { | 105 if (url == GURL(kChromeTestPluginPayloads[0].url)) { |
| 94 // This URL should fail, because the plugin should not have intercepted it. | 106 // This URL should fail, because the plugin should not have intercepted it. |
| 95 EXPECT_FALSE(success); | 107 EXPECT_FALSE(success); |
| 96 MessageLoop::current()->Quit(); | 108 MessageLoop::current()->Quit(); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 289 |
| 278 // Tests that the plugin does not intercept its own requests. | 290 // Tests that the plugin does not intercept its own requests. |
| 279 TEST_F(ChromePluginTest, DoesNotInterceptOwnRequest) { | 291 TEST_F(ChromePluginTest, DoesNotInterceptOwnRequest) { |
| 280 const TestResponsePayload& payload = kChromeTestPluginPayloads[0]; | 292 const TestResponsePayload& payload = kChromeTestPluginPayloads[0]; |
| 281 | 293 |
| 282 EXPECT_EQ(CPERR_SUCCESS, test_funcs_.test_make_request( | 294 EXPECT_EQ(CPERR_SUCCESS, test_funcs_.test_make_request( |
| 283 "GET", GURL(payload.url))); | 295 "GET", GURL(payload.url))); |
| 284 | 296 |
| 285 MessageLoop::current()->Run(); | 297 MessageLoop::current()->Run(); |
| 286 } | 298 } |
| OLD | NEW |