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.
5 #include "base/base_paths.h"
6 #include "base/files/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 "extensions/browser/extension_registry.h"
17 #include "extensions/test/result_catcher.h"
18 #include "grit/browser_resources.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
21 class PDFExtensionTest
: public ExtensionApiTest
{
23 virtual ~PDFExtensionTest() {}
25 virtual void SetUpCommandLine(CommandLine
* command_line
) override
{
26 ExtensionApiTest::SetUpCommandLine(command_line
);
27 command_line
->AppendSwitch(switches::kOutOfProcessPdf
);
30 virtual void SetUpOnMainThread() override
{
31 ExtensionApiTest::SetUpOnMainThread();
32 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
36 virtual void TearDownOnMainThread() override
{
37 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
38 ExtensionApiTest::TearDownOnMainThread();
41 void RunTestsInFile(std::string filename
, bool requiresPlugin
) {
42 base::FilePath pdf_plugin_src
;
43 PathService::Get(base::DIR_SOURCE_ROOT
, &pdf_plugin_src
);
44 pdf_plugin_src
= pdf_plugin_src
.AppendASCII("pdf");
45 if (requiresPlugin
&& !base::DirectoryExists(pdf_plugin_src
)) {
46 LOG(WARNING
) << "Not running " << filename
<<
47 " because it requires the PDF plugin which is not available.";
50 ExtensionService
* service
= extensions::ExtensionSystem::Get(
51 profile())->extension_service();
52 service
->component_loader()->Add(IDR_PDF_MANIFEST
,
53 base::FilePath(FILE_PATH_LITERAL("pdf")));
54 const extensions::Extension
* extension
=
55 extensions::ExtensionRegistry::Get(profile())
56 ->enabled_extensions()
57 .GetByID("mhjfbmdgcfjbbpaeojofohoefgiehjai");
58 ASSERT_TRUE(extension
);
59 ASSERT_TRUE(MimeTypesHandler::GetHandler(
60 extension
)->CanHandleMIMEType("application/pdf"));
62 extensions::ResultCatcher catcher
;
64 GURL
url(embedded_test_server()->GetURL("/pdf/test.pdf"));
66 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?" +
68 ui_test_utils::NavigateToURL(browser(), extension_url
);
69 content::WebContents
* contents
=
70 browser()->tab_strip_model()->GetActiveWebContents();
71 content::WaitForLoadStop(contents
);
73 base::FilePath test_data_dir
;
74 PathService::Get(base::DIR_SOURCE_ROOT
, &test_data_dir
);
75 test_data_dir
= test_data_dir
.Append(
76 FILE_PATH_LITERAL("chrome/test/data/pdf"));
77 test_data_dir
= test_data_dir
.AppendASCII(filename
);
80 ASSERT_TRUE(base::ReadFileToString(test_data_dir
, &test_js
));
81 ASSERT_TRUE(content::ExecuteScript(contents
, test_js
));
83 if (!catcher
.GetNextResult())
84 FAIL() << catcher
.message();
88 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Basic
) {
89 RunTestsInFile("basic_test.js", false);
92 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, BasicPlugin
) {
93 RunTestsInFile("basic_plugin_test.js", true);
96 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Viewport
) {
97 RunTestsInFile("viewport_test.js", false);