Home | History | Annotate | Download | only in chromeos
      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
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/message_loop.h"
      8 #include "chrome/browser/chromeos/gview_request_interceptor.h"
      9 #include "chrome/common/chrome_paths.h"
     10 #include "content/browser/plugin_service.h"
     11 #include "net/base/load_flags.h"
     12 #include "net/url_request/url_request.h"
     13 #include "net/url_request/url_request_job.h"
     14 #include "net/url_request/url_request_test_job.h"
     15 #include "net/url_request/url_request_test_util.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "webkit/glue/plugins/plugin_list.h"
     18 
     19 namespace chromeos {
     20 
     21 class GViewURLRequestTestJob : public net::URLRequestTestJob {
     22  public:
     23   explicit GViewURLRequestTestJob(net::URLRequest* request)
     24       : net::URLRequestTestJob(request, true) {
     25   }
     26 
     27   virtual bool GetMimeType(std::string* mime_type) const {
     28     // During the course of a single test, two URLRequestJobs are
     29     // created -- the first is for the viewable document URL, and the
     30     // second is for the rediected URL.  In order to test the
     31     // interceptor, the mime type of the first request must be one of
     32     // the supported viewable mime types.  So when the net::URLRequestJob
     33     // is a request for one of the test URLs that point to viewable
     34     // content, return an appropraite mime type.  Otherwise, return
     35     // "text/html".
     36     if (request_->url() == GURL("http://foo.com/file.pdf")) {
     37       *mime_type = "application/pdf";
     38     } else if (request_->url() == GURL("http://foo.com/file.ppt")) {
     39       *mime_type = "application/vnd.ms-powerpoint";
     40     } else {
     41       *mime_type = "text/html";
     42     }
     43     return true;
     44   }
     45 
     46  private:
     47   ~GViewURLRequestTestJob() {}
     48 };
     49 
     50 class GViewRequestInterceptorTest : public testing::Test {
     51  public:
     52   virtual void SetUp() {
     53     net::URLRequest::RegisterProtocolFactory("http",
     54                                         &GViewRequestInterceptorTest::Factory);
     55     interceptor_ = GViewRequestInterceptor::GetInstance();
     56     ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_));
     57   }
     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 
     69   void RegisterPDFPlugin() {
     70     webkit::npapi::WebPluginInfo info;
     71     info.path = pdf_path_;
     72     info.enabled = webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED;
     73     webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
     74     webkit::npapi::PluginList::Singleton()->RefreshPlugins();
     75   }
     76 
     77   void UnregisterPDFPlugin() {
     78     webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(pdf_path_);
     79     webkit::npapi::PluginList::Singleton()->RefreshPlugins();
     80   }
     81 
     82   void SetPDFPluginLoadedState(bool want_loaded, bool* out_is_enabled) {
     83     webkit::npapi::WebPluginInfo info;
     84     bool is_loaded =
     85         webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
     86             pdf_path_, &info);
     87     if (is_loaded && !want_loaded) {
     88       UnregisterPDFPlugin();
     89       is_loaded = webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
     90           pdf_path_, &info);
     91     } else if (!is_loaded && want_loaded) {
     92       // This "loads" the plug-in even if it's not present on the
     93       // system - which is OK since we don't actually use it, just
     94       // need it to be "enabled" for the test.
     95       RegisterPDFPlugin();
     96       is_loaded = webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
     97           pdf_path_, &info);
     98     }
     99     EXPECT_EQ(want_loaded, is_loaded);
    100     *out_is_enabled = webkit::npapi::IsPluginEnabled(info);
    101   }
    102 
    103  protected:
    104   MessageLoopForIO message_loop_;
    105   TestDelegate test_delegate_;
    106   net::URLRequest::Interceptor* interceptor_;
    107   FilePath pdf_path_;
    108 };
    109 
    110 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) {
    111   net::URLRequest request(GURL("http://foo.com/index.html"), &test_delegate_);
    112   request.Start();
    113   MessageLoop::current()->Run();
    114   EXPECT_EQ(0, test_delegate_.received_redirect_count());
    115   EXPECT_EQ(GURL("http://foo.com/index.html"), request.url());
    116 }
    117 
    118 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) {
    119   net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_);
    120   request.set_load_flags(net::LOAD_IS_DOWNLOAD);
    121   request.Start();
    122   MessageLoop::current()->Run();
    123   EXPECT_EQ(0, test_delegate_.received_redirect_count());
    124   EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url());
    125 }
    126 
    127 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) {
    128   bool enabled;
    129   SetPDFPluginLoadedState(true, &enabled);
    130 
    131   if (!enabled) {
    132     bool pdf_plugin_enabled =
    133         webkit::npapi::PluginList::Singleton()->EnablePlugin(pdf_path_);
    134     EXPECT_TRUE(pdf_plugin_enabled);
    135   }
    136 
    137   net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_);
    138   request.Start();
    139   MessageLoop::current()->Run();
    140   EXPECT_EQ(0, test_delegate_.received_redirect_count());
    141   EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url());
    142 }
    143 
    144 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) {
    145   bool enabled;
    146   SetPDFPluginLoadedState(true, &enabled);
    147 
    148   if (enabled) {
    149     bool pdf_plugin_disabled =
    150         webkit::npapi::PluginList::Singleton()->DisablePlugin(pdf_path_);
    151     EXPECT_TRUE(pdf_plugin_disabled);
    152   }
    153 
    154   net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_);
    155   request.Start();
    156   MessageLoop::current()->Run();
    157   EXPECT_EQ(1, test_delegate_.received_redirect_count());
    158   EXPECT_EQ(
    159       GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"),
    160       request.url());
    161 }
    162 
    163 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) {
    164   bool enabled;
    165   SetPDFPluginLoadedState(false, &enabled);
    166 
    167   net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_);
    168   request.Start();
    169   MessageLoop::current()->Run();
    170   EXPECT_EQ(1, test_delegate_.received_redirect_count());
    171   EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"),
    172                  request.url());
    173 }
    174 
    175 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) {
    176   net::URLRequest request(GURL("http://foo.com/file.ppt"), &test_delegate_);
    177   request.Start();
    178   MessageLoop::current()->Run();
    179   EXPECT_EQ(1, test_delegate_.received_redirect_count());
    180   EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"),
    181                  request.url());
    182 }
    183 
    184 }  // namespace chromeos
    185