OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "chrome/browser/chrome_plugin_service_filter.h" | 9 #include "chrome/browser/chrome_plugin_service_filter.h" |
10 #include "chrome/browser/chromeos/gview_request_interceptor.h" | 10 #include "chrome/browser/chromeos/gview_request_interceptor.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "net/url_request/url_request_job_factory.h" | 22 #include "net/url_request/url_request_job_factory.h" |
23 #include "net/url_request/url_request_test_job.h" | 23 #include "net/url_request/url_request_test_job.h" |
24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "webkit/plugins/npapi/plugin_list.h" | 26 #include "webkit/plugins/npapi/plugin_list.h" |
27 | 27 |
28 namespace chromeos { | 28 namespace chromeos { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
| 32 const char kPdfUrl[] = "http://foo.com/file.pdf"; |
| 33 const char kPptUrl[] = "http://foo.com/file.ppt"; |
| 34 const char kHtmlUrl[] = "http://foo.com/index.html"; |
| 35 const char kPdfBlob[] = "blob:blobinternal:///d17c4eef-28e7-42bd-bafa-78d5cb8"; |
| 36 |
| 37 const char kPdfUrlIntercepted[] = |
| 38 "http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"; |
| 39 const char kPptUrlIntercepted[] = |
| 40 "http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"; |
| 41 |
32 class GViewURLRequestTestJob : public net::URLRequestTestJob { | 42 class GViewURLRequestTestJob : public net::URLRequestTestJob { |
33 public: | 43 public: |
34 explicit GViewURLRequestTestJob(net::URLRequest* request) | 44 explicit GViewURLRequestTestJob(net::URLRequest* request) |
35 : net::URLRequestTestJob(request, true) { | 45 : net::URLRequestTestJob(request, true) { |
36 } | 46 } |
37 | 47 |
38 virtual bool GetMimeType(std::string* mime_type) const { | 48 virtual bool GetMimeType(std::string* mime_type) const { |
39 // During the course of a single test, two URLRequestJobs are | 49 // During the course of a single test, two URLRequestJobs are |
40 // created -- the first is for the viewable document URL, and the | 50 // created -- the first is for the viewable document URL, and the |
41 // second is for the rediected URL. In order to test the | 51 // second is for the rediected URL. In order to test the |
42 // interceptor, the mime type of the first request must be one of | 52 // interceptor, the mime type of the first request must be one of |
43 // the supported viewable mime types. So when the net::URLRequestJob | 53 // the supported viewable mime types. So when the net::URLRequestJob |
44 // is a request for one of the test URLs that point to viewable | 54 // is a request for one of the test URLs that point to viewable |
45 // content, return an appropraite mime type. Otherwise, return | 55 // content, return an appropraite mime type. Otherwise, return |
46 // "text/html". | 56 // "text/html". |
47 if (request_->url() == GURL("http://foo.com/file.pdf")) { | 57 const GURL& request_url = request_->url(); |
| 58 if (request_url == GURL(kPdfUrl) || request_url == GURL(kPdfBlob)) { |
48 *mime_type = "application/pdf"; | 59 *mime_type = "application/pdf"; |
49 } else if (request_->url() == GURL("http://foo.com/file.ppt")) { | 60 } else if (request_url == GURL(kPptUrl)) { |
50 *mime_type = "application/vnd.ms-powerpoint"; | 61 *mime_type = "application/vnd.ms-powerpoint"; |
51 } else { | 62 } else { |
52 *mime_type = "text/html"; | 63 *mime_type = "text/html"; |
53 } | 64 } |
54 return true; | 65 return true; |
55 } | 66 } |
56 | 67 |
57 private: | 68 private: |
58 ~GViewURLRequestTestJob() {} | 69 ~GViewURLRequestTestJob() {} |
59 }; | 70 }; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 TestingPrefService prefs_; | 197 TestingPrefService prefs_; |
187 scoped_refptr<PluginPrefs> plugin_prefs_; | 198 scoped_refptr<PluginPrefs> plugin_prefs_; |
188 net::URLRequestJobFactory job_factory_; | 199 net::URLRequestJobFactory job_factory_; |
189 const net::URLRequestJobFactory* old_factory_; | 200 const net::URLRequestJobFactory* old_factory_; |
190 scoped_refptr<ResourceHandler> handler_; | 201 scoped_refptr<ResourceHandler> handler_; |
191 TestDelegate test_delegate_; | 202 TestDelegate test_delegate_; |
192 FilePath pdf_path_; | 203 FilePath pdf_path_; |
193 }; | 204 }; |
194 | 205 |
195 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { | 206 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { |
196 net::URLRequest request(GURL("http://foo.com/index.html"), &test_delegate_); | 207 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); |
197 SetupRequest(&request); | 208 SetupRequest(&request); |
198 request.Start(); | 209 request.Start(); |
199 MessageLoop::current()->Run(); | 210 MessageLoop::current()->Run(); |
200 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 211 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
201 EXPECT_EQ(GURL("http://foo.com/index.html"), request.url()); | 212 EXPECT_EQ(GURL(kHtmlUrl), request.url()); |
202 } | 213 } |
203 | 214 |
204 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { | 215 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { |
205 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 216 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
206 SetupRequest(&request); | 217 SetupRequest(&request); |
207 request.set_load_flags(net::LOAD_IS_DOWNLOAD); | 218 request.set_load_flags(net::LOAD_IS_DOWNLOAD); |
208 request.Start(); | 219 request.Start(); |
209 MessageLoop::current()->Run(); | 220 MessageLoop::current()->Run(); |
210 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 221 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
211 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 222 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
212 } | 223 } |
213 | 224 |
214 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { | 225 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { |
215 SetPDFPluginLoadedState(true); | 226 SetPDFPluginLoadedState(true); |
216 plugin_prefs_->EnablePlugin(true, pdf_path_); | 227 plugin_prefs_->EnablePlugin(true, pdf_path_); |
217 | 228 |
218 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 229 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
219 SetupRequest(&request); | 230 SetupRequest(&request); |
220 request.Start(); | 231 request.Start(); |
221 MessageLoop::current()->Run(); | 232 MessageLoop::current()->Run(); |
222 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 233 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
223 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 234 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
224 } | 235 } |
225 | 236 |
226 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { | 237 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { |
227 SetPDFPluginLoadedState(true); | 238 SetPDFPluginLoadedState(true); |
228 plugin_prefs_->EnablePlugin(false, pdf_path_); | 239 plugin_prefs_->EnablePlugin(false, pdf_path_); |
229 | 240 |
230 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 241 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
231 SetupRequest(&request); | 242 SetupRequest(&request); |
232 request.Start(); | 243 request.Start(); |
233 MessageLoop::current()->Run(); | 244 MessageLoop::current()->Run(); |
234 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 245 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
235 EXPECT_EQ( | 246 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
236 GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | |
237 request.url()); | |
238 } | 247 } |
239 | 248 |
240 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { | 249 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { |
241 SetPDFPluginLoadedState(false); | 250 SetPDFPluginLoadedState(false); |
242 | 251 |
243 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 252 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
244 SetupRequest(&request); | 253 SetupRequest(&request); |
245 request.Start(); | 254 request.Start(); |
246 MessageLoop::current()->Run(); | 255 MessageLoop::current()->Run(); |
247 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 256 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
248 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | 257 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
249 request.url()); | |
250 } | 258 } |
251 | 259 |
252 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { | 260 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { |
253 net::URLRequest request(GURL("http://foo.com/file.ppt"), &test_delegate_); | 261 net::URLRequest request(GURL(kPptUrl), &test_delegate_); |
254 SetupRequest(&request); | 262 SetupRequest(&request); |
255 request.Start(); | 263 request.Start(); |
256 MessageLoop::current()->Run(); | 264 MessageLoop::current()->Run(); |
257 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 265 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
258 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"), | 266 EXPECT_EQ(GURL(kPptUrlIntercepted), request.url()); |
259 request.url()); | 267 } |
| 268 |
| 269 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPost) { |
| 270 SetPDFPluginLoadedState(false); |
| 271 |
| 272 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 273 SetupRequest(&request); |
| 274 request.set_method("POST"); |
| 275 request.Start(); |
| 276 MessageLoop::current()->Run(); |
| 277 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 278 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 279 } |
| 280 |
| 281 TEST_F(GViewRequestInterceptorTest, DoNotInterceptBlob) { |
| 282 SetPDFPluginLoadedState(false); |
| 283 |
| 284 net::URLRequest request(GURL(kPdfBlob), &test_delegate_); |
| 285 SetupRequest(&request); |
| 286 request.Start(); |
| 287 MessageLoop::current()->Run(); |
| 288 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 289 EXPECT_EQ(GURL(kPdfBlob), request.url()); |
260 } | 290 } |
261 | 291 |
262 } // namespace | 292 } // namespace |
263 | 293 |
264 } // namespace chromeos | 294 } // namespace chromeos |
OLD | NEW |