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