Home | History | Annotate | Download | only in pdf
      1 // Copyright 2014 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 "base/base_paths.h"
      6 #include "base/file_util.h"
      7 #include "base/path_service.h"
      8 #include "chrome/browser/extensions/component_loader.h"
      9 #include "chrome/browser/extensions/extension_apitest.h"
     10 #include "chrome/browser/extensions/extension_service.h"
     11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     12 #include "chrome/common/chrome_switches.h"
     13 #include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
     14 #include "chrome/test/base/ui_test_utils.h"
     15 #include "content/public/test/browser_test_utils.h"
     16 #include "grit/browser_resources.h"
     17 #include "net/test/embedded_test_server/embedded_test_server.h"
     18 
     19 class PDFExtensionTest : public ExtensionApiTest {
     20  public:
     21   virtual ~PDFExtensionTest() {}
     22 
     23   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     24     ExtensionApiTest::SetUpCommandLine(command_line);
     25     command_line->AppendSwitch(switches::kOutOfProcessPdf);
     26   }
     27 
     28   virtual void SetUpOnMainThread() OVERRIDE {
     29     ExtensionApiTest::SetUpOnMainThread();
     30     ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     31   }
     32 
     33 
     34   virtual void CleanUpOnMainThread() OVERRIDE {
     35     ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
     36     ExtensionApiTest::CleanUpOnMainThread();
     37   }
     38 
     39   void RunTestsInFile(std::string filename, bool requiresPlugin) {
     40     base::FilePath pdf_plugin_src;
     41     PathService::Get(base::DIR_SOURCE_ROOT, &pdf_plugin_src);
     42     pdf_plugin_src = pdf_plugin_src.AppendASCII("pdf");
     43     if (requiresPlugin && !base::DirectoryExists(pdf_plugin_src)) {
     44       LOG(WARNING) << "Not running " << filename <<
     45           " because it requires the PDF plugin which is not available.";
     46       return;
     47     }
     48     ExtensionService* service = extensions::ExtensionSystem::Get(
     49         profile())->extension_service();
     50     service->component_loader()->Add(IDR_PDF_MANIFEST,
     51         base::FilePath(FILE_PATH_LITERAL("pdf")));
     52     const extensions::Extension* extension =
     53         service->extensions()->GetByID("mhjfbmdgcfjbbpaeojofohoefgiehjai");
     54     ASSERT_TRUE(extension);
     55     ASSERT_TRUE(MimeTypesHandler::GetHandler(
     56         extension)->CanHandleMIMEType("application/pdf"));
     57 
     58     ResultCatcher catcher;
     59 
     60     GURL url(embedded_test_server()->GetURL("/pdf/test.pdf"));
     61     GURL extension_url(
     62         "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?" +
     63         url.spec());
     64     ui_test_utils::NavigateToURL(browser(), extension_url);
     65     content::WebContents* contents =
     66         browser()->tab_strip_model()->GetActiveWebContents();
     67     content::WaitForLoadStop(contents);
     68 
     69     base::FilePath test_data_dir;
     70     PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir);
     71     test_data_dir = test_data_dir.Append(
     72         FILE_PATH_LITERAL("chrome/test/data/pdf"));
     73     test_data_dir = test_data_dir.AppendASCII(filename);
     74 
     75     std::string test_js;
     76     ASSERT_TRUE(base::ReadFileToString(test_data_dir, &test_js));
     77     ASSERT_TRUE(content::ExecuteScript(contents, test_js));
     78 
     79     if (!catcher.GetNextResult())
     80       FAIL() << catcher.message();
     81   }
     82 };
     83 
     84 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Basic) {
     85   RunTestsInFile("basic_test.js", false);
     86 }
     87 
     88 // TODO(raymes): This fails with component builds on linux because the plugin
     89 // plugin crashes due to something related to how the plugin DLL is
     90 // compiled/linked. crbug.com/386436.
     91 #if defined(LINUX) && defined(COMPONENT_BUILD)
     92 #define MAYBE_BasicPlugin DISABLED_BasicPlugin
     93 #else
     94 #define MAYBE_BasicPlugin BasicPlugin
     95 #endif
     96 
     97 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, MAYBE_BasicPlugin) {
     98   RunTestsInFile("basic_plugin_test.js", true);
     99 }
    100 
    101 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Viewport) {
    102   RunTestsInFile("viewport_test.js", false);
    103 }
    104