| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "chrome/browser/chromeos/gview_request_interceptor.h" | 7 #include "chrome/browser/chromeos/gview_request_interceptor.h" |
| 8 #include "chrome/browser/plugin_service.h" | 8 #include "chrome/browser/plugin_service.h" |
| 9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 12 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.h" |
| 13 #include "net/url_request/url_request_test_job.h" | 13 #include "net/url_request/url_request_test_job.h" |
| 14 #include "net/url_request/url_request_unittest.h" | 14 #include "net/url_request/url_request_unittest.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/glue/plugins/plugin_list.h" | 16 #include "webkit/glue/plugins/plugin_list.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 | 19 |
| 20 class GViewURLRequestTestJob : public URLRequestTestJob { | 20 class GViewURLRequestTestJob : public URLRequestTestJob { |
| 21 public: | 21 public: |
| 22 explicit GViewURLRequestTestJob(URLRequest* request) | 22 explicit GViewURLRequestTestJob(net::URLRequest* request) |
| 23 : URLRequestTestJob(request, true) { | 23 : URLRequestTestJob(request, true) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 virtual bool GetMimeType(std::string* mime_type) const { | 26 virtual bool GetMimeType(std::string* mime_type) const { |
| 27 // During the course of a single test, two URLRequestJobs are | 27 // During the course of a single test, two URLRequestJobs are |
| 28 // created -- the first is for the viewable document URL, and the | 28 // created -- the first is for the viewable document URL, and the |
| 29 // second is for the rediected URL. In order to test the | 29 // second is for the rediected URL. In order to test the |
| 30 // interceptor, the mime type of the first request must be one of | 30 // interceptor, the mime type of the first request must be one of |
| 31 // the supported viewable mime types. So when the URLRequestJob | 31 // the supported viewable mime types. So when the URLRequestJob |
| 32 // is a request for one of the test URLs that point to viewable | 32 // is a request for one of the test URLs that point to viewable |
| 33 // content, return an appropraite mime type. Otherwise, return | 33 // content, return an appropraite mime type. Otherwise, return |
| 34 // "text/html". | 34 // "text/html". |
| 35 if (request_->url() == GURL("http://foo.com/file.pdf")) { | 35 if (request_->url() == GURL("http://foo.com/file.pdf")) { |
| 36 *mime_type = "application/pdf"; | 36 *mime_type = "application/pdf"; |
| 37 } else if (request_->url() == GURL("http://foo.com/file.ppt")) { | 37 } else if (request_->url() == GURL("http://foo.com/file.ppt")) { |
| 38 *mime_type = "application/vnd.ms-powerpoint"; | 38 *mime_type = "application/vnd.ms-powerpoint"; |
| 39 } else { | 39 } else { |
| 40 *mime_type = "text/html"; | 40 *mime_type = "text/html"; |
| 41 } | 41 } |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 ~GViewURLRequestTestJob() {} | 46 ~GViewURLRequestTestJob() {} |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 class GViewRequestInterceptorTest : public testing::Test { | 49 class GViewRequestInterceptorTest : public testing::Test { |
| 50 public: | 50 public: |
| 51 virtual void SetUp() { | 51 virtual void SetUp() { |
| 52 URLRequest::RegisterProtocolFactory("http", | 52 net::URLRequest::RegisterProtocolFactory("http", |
| 53 &GViewRequestInterceptorTest::Factory); | 53 &GViewRequestInterceptorTest::Factory); |
| 54 interceptor_ = GViewRequestInterceptor::GetGViewRequestInterceptor(); | 54 interceptor_ = GViewRequestInterceptor::GetGViewRequestInterceptor(); |
| 55 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); | 55 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 virtual void TearDown() { | 58 virtual void TearDown() { |
| 59 URLRequest::RegisterProtocolFactory("http", NULL); | 59 net::URLRequest::RegisterProtocolFactory("http", NULL); |
| 60 message_loop_.RunAllPending(); | 60 message_loop_.RunAllPending(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 static URLRequestJob* Factory(URLRequest* request, | 63 static URLRequestJob* Factory(net::URLRequest* request, |
| 64 const std::string& scheme) { | 64 const std::string& scheme) { |
| 65 return new GViewURLRequestTestJob(request); | 65 return new GViewURLRequestTestJob(request); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void RegisterPDFPlugin() { | 68 void RegisterPDFPlugin() { |
| 69 NPAPI::PluginVersionInfo info; | 69 NPAPI::PluginVersionInfo info; |
| 70 info.path = pdf_path_; | 70 info.path = pdf_path_; |
| 71 memset(&info.entry_points, 0, sizeof(info.entry_points)); | 71 memset(&info.entry_points, 0, sizeof(info.entry_points)); |
| 72 NPAPI::PluginList::Singleton()->RegisterInternalPlugin(info); | 72 NPAPI::PluginList::Singleton()->RegisterInternalPlugin(info); |
| 73 NPAPI::PluginList::Singleton()->RefreshPlugins(); | 73 NPAPI::PluginList::Singleton()->RefreshPlugins(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 94 is_loaded = | 94 is_loaded = |
| 95 NPAPI::PluginList::Singleton()->GetPluginInfoByPath(pdf_path_, &info); | 95 NPAPI::PluginList::Singleton()->GetPluginInfoByPath(pdf_path_, &info); |
| 96 } | 96 } |
| 97 EXPECT_EQ(want_loaded, is_loaded); | 97 EXPECT_EQ(want_loaded, is_loaded); |
| 98 *out_is_enabled = info.enabled; | 98 *out_is_enabled = info.enabled; |
| 99 } | 99 } |
| 100 | 100 |
| 101 protected: | 101 protected: |
| 102 MessageLoopForIO message_loop_; | 102 MessageLoopForIO message_loop_; |
| 103 TestDelegate test_delegate_; | 103 TestDelegate test_delegate_; |
| 104 URLRequest::Interceptor* interceptor_; | 104 net::URLRequest::Interceptor* interceptor_; |
| 105 FilePath pdf_path_; | 105 FilePath pdf_path_; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { | 108 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { |
| 109 URLRequest request(GURL("http://foo.com/index.html"), &test_delegate_); | 109 net::URLRequest request(GURL("http://foo.com/index.html"), &test_delegate_); |
| 110 request.Start(); | 110 request.Start(); |
| 111 MessageLoop::current()->Run(); | 111 MessageLoop::current()->Run(); |
| 112 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 112 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 113 EXPECT_EQ(GURL("http://foo.com/index.html"), request.url()); | 113 EXPECT_EQ(GURL("http://foo.com/index.html"), request.url()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { | 116 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { |
| 117 URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 117 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); |
| 118 request.set_load_flags(net::LOAD_IS_DOWNLOAD); | 118 request.set_load_flags(net::LOAD_IS_DOWNLOAD); |
| 119 request.Start(); | 119 request.Start(); |
| 120 MessageLoop::current()->Run(); | 120 MessageLoop::current()->Run(); |
| 121 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 121 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 122 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 122 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); |
| 123 } | 123 } |
| 124 | 124 |
| 125 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { | 125 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { |
| 126 bool enabled; | 126 bool enabled; |
| 127 SetPDFPluginLoadedState(true, &enabled); | 127 SetPDFPluginLoadedState(true, &enabled); |
| 128 | 128 |
| 129 if (!enabled) { | 129 if (!enabled) { |
| 130 bool pdf_plugin_enabled = | 130 bool pdf_plugin_enabled = |
| 131 NPAPI::PluginList::Singleton()->EnablePlugin(pdf_path_); | 131 NPAPI::PluginList::Singleton()->EnablePlugin(pdf_path_); |
| 132 EXPECT_TRUE(pdf_plugin_enabled); | 132 EXPECT_TRUE(pdf_plugin_enabled); |
| 133 } | 133 } |
| 134 | 134 |
| 135 URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 135 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); |
| 136 request.Start(); | 136 request.Start(); |
| 137 MessageLoop::current()->Run(); | 137 MessageLoop::current()->Run(); |
| 138 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 138 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 139 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 139 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { | 142 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { |
| 143 bool enabled; | 143 bool enabled; |
| 144 SetPDFPluginLoadedState(true, &enabled); | 144 SetPDFPluginLoadedState(true, &enabled); |
| 145 | 145 |
| 146 if (enabled) { | 146 if (enabled) { |
| 147 bool pdf_plugin_disabled = | 147 bool pdf_plugin_disabled = |
| 148 NPAPI::PluginList::Singleton()->DisablePlugin(pdf_path_); | 148 NPAPI::PluginList::Singleton()->DisablePlugin(pdf_path_); |
| 149 EXPECT_TRUE(pdf_plugin_disabled); | 149 EXPECT_TRUE(pdf_plugin_disabled); |
| 150 } | 150 } |
| 151 | 151 |
| 152 URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 152 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); |
| 153 request.Start(); | 153 request.Start(); |
| 154 MessageLoop::current()->Run(); | 154 MessageLoop::current()->Run(); |
| 155 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 155 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
| 156 EXPECT_EQ( | 156 EXPECT_EQ( |
| 157 GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | 157 GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), |
| 158 request.url()); | 158 request.url()); |
| 159 } | 159 } |
| 160 | 160 |
| 161 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { | 161 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { |
| 162 bool enabled; | 162 bool enabled; |
| 163 SetPDFPluginLoadedState(false, &enabled); | 163 SetPDFPluginLoadedState(false, &enabled); |
| 164 | 164 |
| 165 URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 165 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); |
| 166 request.Start(); | 166 request.Start(); |
| 167 MessageLoop::current()->Run(); | 167 MessageLoop::current()->Run(); |
| 168 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 168 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
| 169 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | 169 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), |
| 170 request.url()); | 170 request.url()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { | 173 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { |
| 174 URLRequest request(GURL("http://foo.com/file.ppt"), &test_delegate_); | 174 net::URLRequest request(GURL("http://foo.com/file.ppt"), &test_delegate_); |
| 175 request.Start(); | 175 request.Start(); |
| 176 MessageLoop::current()->Run(); | 176 MessageLoop::current()->Run(); |
| 177 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 177 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
| 178 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"), | 178 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"), |
| 179 request.url()); | 179 request.url()); |
| 180 } | 180 } |
| 181 | 181 |
| 182 } // namespace chromeos | 182 } // namespace chromeos |
| OLD | NEW |