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_enumerator.h"
7 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/extensions/component_loader.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/browser_plugin_guest_manager.h"
18 #include "content/public/browser/plugin_service.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/common/manifest_handlers/mime_types_handler.h"
22 #include "extensions/test/result_catcher.h"
23 #include "grit/component_extension_resources.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #include "ui/base/resource/resource_bundle.h"
27 const int kNumberLoadTestParts
= 10;
29 class PDFExtensionTest
: public ExtensionApiTest
,
30 public testing::WithParamInterface
<int> {
32 ~PDFExtensionTest() override
{}
34 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
35 command_line
->AppendSwitch(switches::kDisablePdfMaterialUI
);
38 void SetUpOnMainThread() override
{
39 ExtensionApiTest::SetUpOnMainThread();
40 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
43 void TearDownOnMainThread() override
{
44 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
45 ExtensionApiTest::TearDownOnMainThread();
48 bool PdfIsExpectedToFailLoad(const std::string
& pdf_file
) {
49 const char* kFailingPdfs
[] = {
50 "pdf_private/cfuzz5.pdf",
51 "pdf_private/cfuzz6.pdf",
52 "pdf_private/crash-11-14-44.pdf",
54 "pdf_private/segv-ecx.pdf",
55 "pdf_private/tests.pdf",
57 for (size_t i
= 0; i
< arraysize(kFailingPdfs
); ++i
) {
58 if (kFailingPdfs
[i
] == pdf_file
)
64 // Runs the extensions test at chrome/test/data/pdf/<filename> on the PDF file
65 // at chrome/test/data/pdf/<pdf_filename>.
66 void RunTestsInFile(std::string filename
, std::string pdf_filename
) {
67 extensions::ResultCatcher catcher
;
69 GURL
url(embedded_test_server()->GetURL("/pdf/" + pdf_filename
));
71 // It should be good enough to just navigate to the URL. But loading up the
72 // BrowserPluginGuest seems to happen asynchronously as there was flakiness
73 // being seen due to the BrowserPluginGuest not being available yet (see
74 // crbug.com/498077). So instead use |LoadPdf| which ensures that the PDF is
75 // loaded before continuing.
76 ASSERT_TRUE(LoadPdf(url
));
78 content::WebContents
* contents
=
79 browser()->tab_strip_model()->GetActiveWebContents();
80 content::BrowserPluginGuestManager
* guest_manager
=
81 contents
->GetBrowserContext()->GetGuestManager();
82 content::WebContents
* guest_contents
=
83 guest_manager
->GetFullPageGuest(contents
);
84 ASSERT_TRUE(guest_contents
);
86 base::FilePath test_data_dir
;
87 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
88 test_data_dir
= test_data_dir
.Append(FILE_PATH_LITERAL("pdf"));
89 base::FilePath test_util_path
= test_data_dir
.AppendASCII("test_util.js");
90 std::string test_util_js
;
91 ASSERT_TRUE(base::ReadFileToString(test_util_path
, &test_util_js
));
93 base::FilePath test_file_path
= test_data_dir
.AppendASCII(filename
);
95 ASSERT_TRUE(base::ReadFileToString(test_file_path
, &test_js
));
97 test_util_js
.append(test_js
);
98 ASSERT_TRUE(content::ExecuteScript(guest_contents
, test_util_js
));
100 if (!catcher
.GetNextResult())
101 FAIL() << catcher
.message();
104 // Load the PDF at the given URL and use the PDFScriptingAPI to ensure it has
105 // finished loading. Return true if it loads successfully or false if it
106 // fails. If it doesn't finish loading the test will hang. This is done from
107 // outside of the BrowserPlugin guest to ensure the PDFScriptingAPI works
108 // correctly from there.
109 bool LoadPdf(const GURL
& url
) {
110 ui_test_utils::NavigateToURL(browser(), url
);
111 content::WebContents
* web_contents
=
112 browser()->tab_strip_model()->GetActiveWebContents();
113 std::string scripting_api_js
=
114 ResourceBundle::GetSharedInstance()
115 .GetRawDataResource(IDR_PDF_PDF_SCRIPTING_API_JS
)
117 CHECK(content::ExecuteScript(web_contents
, scripting_api_js
));
119 bool load_success
= false;
120 CHECK(content::ExecuteScriptAndExtractBool(
122 "var scriptingAPI = new PDFScriptingAPI(window, "
123 " document.getElementsByTagName('embed')[0]);"
124 "scriptingAPI.setLoadCallback(function(success) {"
125 " window.domAutomationController.send(success);"
131 // Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs
132 // the test if base::Hash(filename) mod kNumberLoadTestParts == k in order
133 // to shard the files evenly across values of k in [0, kNumberLoadTestParts).
134 void LoadAllPdfsTest(const std::string
& dir_name
, int k
) {
135 base::FilePath test_data_dir
;
136 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
137 base::FileEnumerator
file_enumerator(test_data_dir
.AppendASCII(dir_name
),
138 false, base::FileEnumerator::FILES
,
139 FILE_PATH_LITERAL("*.pdf"));
142 for (base::FilePath file_path
= file_enumerator
.Next(); !file_path
.empty();
143 file_path
= file_enumerator
.Next()) {
144 std::string filename
= file_path
.BaseName().MaybeAsASCII();
145 ASSERT_FALSE(filename
.empty());
147 std::string pdf_file
= dir_name
+ "/" + filename
;
148 if (static_cast<int>(base::Hash(filename
) % kNumberLoadTestParts
) == k
) {
149 LOG(INFO
) << "Loading: " << pdf_file
;
150 bool success
= LoadPdf(embedded_test_server()->GetURL("/" + pdf_file
));
151 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file
), success
);
155 // Assume that there is at least 1 pdf in the directory to guard against
156 // someone deleting the directory and silently making the test pass.
157 ASSERT_GE(count
, 1u);
161 IN_PROC_BROWSER_TEST_P(PDFExtensionTest
, Load
) {
162 #if defined(GOOGLE_CHROME_BUILD)
163 // Load private PDFs.
164 LoadAllPdfsTest("pdf_private", GetParam());
167 LoadAllPdfsTest("pdf", GetParam());
170 // We break PDFTest.Load up into kNumberLoadTestParts.
171 INSTANTIATE_TEST_CASE_P(PDFTestFiles
,
173 testing::Range(0, kNumberLoadTestParts
));
175 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Basic
) {
176 RunTestsInFile("basic_test.js", "test.pdf");
179 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, BasicPlugin
) {
180 RunTestsInFile("basic_plugin_test.js", "test.pdf");
183 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Viewport
) {
184 RunTestsInFile("viewport_test.js", "test.pdf");
187 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Bookmark
) {
188 RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf");
191 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Navigator
) {
192 RunTestsInFile("navigator_test.js", "test.pdf");
195 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, ParamsParser
) {
196 RunTestsInFile("params_parser_test.js", "test.pdf");
199 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, ZoomManager
) {
200 RunTestsInFile("zoom_manager_test.js", "test.pdf");
203 class MaterialPDFExtensionTest
: public PDFExtensionTest
{
204 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
205 command_line
->AppendSwitch(switches::kEnablePdfMaterialUI
);
209 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Basic
) {
210 RunTestsInFile("basic_test_material.js", "test.pdf");
213 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, BasicPlugin
) {
214 RunTestsInFile("basic_plugin_test.js", "test.pdf");
217 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Viewport
) {
218 RunTestsInFile("viewport_test.js", "test.pdf");
221 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Bookmark
) {
222 RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf");
225 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Navigator
) {
226 RunTestsInFile("navigator_test.js", "test.pdf");
229 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, ParamsParser
) {
230 RunTestsInFile("params_parser_test.js", "test.pdf");
233 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, ZoomManager
) {
234 RunTestsInFile("zoom_manager_test.js", "test.pdf");