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