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/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 } | 35 } |
36 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 36 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
37 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | 37 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 ~TestURLRequestContextGetter() {} | 41 ~TestURLRequestContextGetter() {} |
42 scoped_refptr<URLRequestContext> context_; | 42 scoped_refptr<URLRequestContext> context_; |
43 }; | 43 }; |
44 | 44 |
45 class ChromePluginTest : public testing::Test, public URLRequest::Delegate { | 45 class ChromePluginTest : public testing::Test, |
| 46 public net::URLRequest::Delegate { |
46 public: | 47 public: |
47 ChromePluginTest() | 48 ChromePluginTest() |
48 : io_thread_(BrowserThread::IO, &message_loop_), | 49 : io_thread_(BrowserThread::IO, &message_loop_), |
49 request_(NULL), | 50 request_(NULL), |
50 response_buffer_(new net::IOBuffer(kResponseBufferSize)), | 51 response_buffer_(new net::IOBuffer(kResponseBufferSize)), |
51 plugin_(NULL), | 52 plugin_(NULL), |
52 expected_payload_(NULL), | 53 expected_payload_(NULL), |
53 request_context_getter_(new TestURLRequestContextGetter()) { | 54 request_context_getter_(new TestURLRequestContextGetter()) { |
54 test_funcs_.test_make_request = NULL; | 55 test_funcs_.test_make_request = NULL; |
55 } | 56 } |
56 | 57 |
57 // Loads/unloads the chrome test plugin. | 58 // Loads/unloads the chrome test plugin. |
58 void LoadPlugin(); | 59 void LoadPlugin(); |
59 void UnloadPlugin(); | 60 void UnloadPlugin(); |
60 | 61 |
61 // Runs the test and expects the given payload as a response. If expectation | 62 // Runs the test and expects the given payload as a response. If expectation |
62 // is NULL, the request is expected to fail. | 63 // is NULL, the request is expected to fail. |
63 void RunTest(const GURL& url, const TestResponsePayload* expected_payload); | 64 void RunTest(const GURL& url, const TestResponsePayload* expected_payload); |
64 | 65 |
65 // URLRequest::Delegate implementations | 66 // net::URLRequest::Delegate implementations |
66 virtual void OnResponseStarted(URLRequest* request); | 67 virtual void OnResponseStarted(net::URLRequest* request); |
67 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | 68 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
68 | 69 |
69 // Helper called when the URLRequest is done. | 70 // Helper called when the net::URLRequest is done. |
70 void OnURLRequestComplete(); | 71 void OnURLRequestComplete(); |
71 | 72 |
72 // testing::Test | 73 // testing::Test |
73 virtual void SetUp() { | 74 virtual void SetUp() { |
74 LoadPlugin(); | 75 LoadPlugin(); |
75 URLRequest::RegisterProtocolFactory("test", &URLRequestTestJob::Factory); | 76 net::URLRequest::RegisterProtocolFactory("test", |
| 77 &URLRequestTestJob::Factory); |
76 | 78 |
77 // We need to setup a default request context in order to issue HTTP | 79 // We need to setup a default request context in order to issue HTTP |
78 // requests. | 80 // requests. |
79 DCHECK(!Profile::GetDefaultRequestContext()); | 81 DCHECK(!Profile::GetDefaultRequestContext()); |
80 Profile::set_default_request_context(request_context_getter_.get()); | 82 Profile::set_default_request_context(request_context_getter_.get()); |
81 } | 83 } |
82 virtual void TearDown() { | 84 virtual void TearDown() { |
83 UnloadPlugin(); | 85 UnloadPlugin(); |
84 URLRequest::RegisterProtocolFactory("test", NULL); | 86 net::URLRequest::RegisterProtocolFactory("test", NULL); |
85 | 87 |
86 Profile::set_default_request_context(NULL); | 88 Profile::set_default_request_context(NULL); |
87 | 89 |
88 // Clear the request before flushing the message loop since killing the | 90 // Clear the request before flushing the message loop since killing the |
89 // request can result in the generation of more tasks. | 91 // request can result in the generation of more tasks. |
90 request_.reset(); | 92 request_.reset(); |
91 | 93 |
92 // Flush the message loop to make Purify happy. | 94 // Flush the message loop to make Purify happy. |
93 message_loop_.RunAllPending(); | 95 message_loop_.RunAllPending(); |
94 } | 96 } |
95 protected: | 97 protected: |
96 MessageLoopForIO message_loop_; | 98 MessageLoopForIO message_loop_; |
97 BrowserThread io_thread_; | 99 BrowserThread io_thread_; |
98 | 100 |
99 // Note: we use URLRequest (instead of URLFetcher) because this allows the | 101 // Note: we use net::URLRequest (instead of URLFetcher) because this allows |
100 // request to be intercepted. | 102 // the request to be intercepted. |
101 scoped_ptr<URLRequest> request_; | 103 scoped_ptr<net::URLRequest> request_; |
102 scoped_refptr<net::IOBuffer> response_buffer_; | 104 scoped_refptr<net::IOBuffer> response_buffer_; |
103 std::string response_data_; | 105 std::string response_data_; |
104 | 106 |
105 ChromePluginLib* plugin_; | 107 ChromePluginLib* plugin_; |
106 TestFuncParams::PluginFuncs test_funcs_; | 108 TestFuncParams::PluginFuncs test_funcs_; |
107 const TestResponsePayload* expected_payload_; | 109 const TestResponsePayload* expected_payload_; |
108 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 110 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
109 }; | 111 }; |
110 | 112 |
111 static void STDCALL CPT_Complete(CPRequest* request, bool success, | 113 static void STDCALL CPT_Complete(CPRequest* request, bool success, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 void ChromePluginTest::UnloadPlugin() { | 161 void ChromePluginTest::UnloadPlugin() { |
160 ChromePluginLib::UnloadAllPlugins(); | 162 ChromePluginLib::UnloadAllPlugins(); |
161 plugin_ = NULL; | 163 plugin_ = NULL; |
162 } | 164 } |
163 | 165 |
164 void ChromePluginTest::RunTest(const GURL& url, | 166 void ChromePluginTest::RunTest(const GURL& url, |
165 const TestResponsePayload* expected_payload) { | 167 const TestResponsePayload* expected_payload) { |
166 expected_payload_ = expected_payload; | 168 expected_payload_ = expected_payload; |
167 | 169 |
168 response_data_.clear(); | 170 response_data_.clear(); |
169 request_.reset(new URLRequest(url, this)); | 171 request_.reset(new net::URLRequest(url, this)); |
170 request_->set_context(new TestURLRequestContext()); | 172 request_->set_context(new TestURLRequestContext()); |
171 request_->Start(); | 173 request_->Start(); |
172 | 174 |
173 MessageLoop::current()->Run(); | 175 MessageLoop::current()->Run(); |
174 } | 176 } |
175 | 177 |
176 void ChromePluginTest::OnResponseStarted(URLRequest* request) { | 178 void ChromePluginTest::OnResponseStarted(net::URLRequest* request) { |
177 DCHECK(request == request_); | 179 DCHECK(request == request_); |
178 | 180 |
179 int bytes_read = 0; | 181 int bytes_read = 0; |
180 if (request_->status().is_success()) | 182 if (request_->status().is_success()) |
181 request_->Read(response_buffer_, kResponseBufferSize, &bytes_read); | 183 request_->Read(response_buffer_, kResponseBufferSize, &bytes_read); |
182 OnReadCompleted(request_.get(), bytes_read); | 184 OnReadCompleted(request_.get(), bytes_read); |
183 } | 185 } |
184 | 186 |
185 void ChromePluginTest::OnReadCompleted(URLRequest* request, int bytes_read) { | 187 void ChromePluginTest::OnReadCompleted(net::URLRequest* request, |
| 188 int bytes_read) { |
186 DCHECK(request == request_); | 189 DCHECK(request == request_); |
187 | 190 |
188 do { | 191 do { |
189 if (!request_->status().is_success() || bytes_read <= 0) | 192 if (!request_->status().is_success() || bytes_read <= 0) |
190 break; | 193 break; |
191 response_data_.append(response_buffer_->data(), bytes_read); | 194 response_data_.append(response_buffer_->data(), bytes_read); |
192 } while (request_->Read(response_buffer_, kResponseBufferSize, &bytes_read)); | 195 } while (request_->Read(response_buffer_, kResponseBufferSize, &bytes_read)); |
193 | 196 |
194 if (!request_->status().is_io_pending()) { | 197 if (!request_->status().is_io_pending()) { |
195 OnURLRequestComplete(); | 198 OnURLRequestComplete(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 TEST_F(ChromePluginTest, DoesNotInterceptOwnRequest) { | 300 TEST_F(ChromePluginTest, DoesNotInterceptOwnRequest) { |
298 const TestResponsePayload& payload = kChromeTestPluginPayloads[0]; | 301 const TestResponsePayload& payload = kChromeTestPluginPayloads[0]; |
299 | 302 |
300 EXPECT_EQ(CPERR_SUCCESS, test_funcs_.test_make_request( | 303 EXPECT_EQ(CPERR_SUCCESS, test_funcs_.test_make_request( |
301 "GET", GURL(payload.url))); | 304 "GET", GURL(payload.url))); |
302 | 305 |
303 MessageLoop::current()->Run(); | 306 MessageLoop::current()->Run(); |
304 } | 307 } |
305 | 308 |
306 } // namespace | 309 } // namespace |
OLD | NEW |