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.
7 #include "base/base_paths.h"
8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_util.h"
10 #include "base/hash.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/path_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/component_loader.h"
17 #include "chrome/browser/extensions/extension_apitest.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/pdf/pdf_extension_test_util.h"
20 #include "chrome/browser/pdf/pdf_extension_util.h"
21 #include "chrome/browser/plugins/plugin_prefs.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/chrome_content_client.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "content/public/browser/browser_plugin_guest_manager.h"
30 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager.h"
32 #include "content/public/browser/notification_observer.h"
33 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/plugin_service.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/test/browser_test_utils.h"
37 #include "extensions/browser/extension_registry.h"
38 #include "extensions/common/manifest_handlers/mime_types_handler.h"
39 #include "extensions/test/result_catcher.h"
40 #include "net/test/embedded_test_server/embedded_test_server.h"
41 #include "ui/base/resource/resource_bundle.h"
44 const int kNumberLoadTestParts
= 10;
46 class PDFExtensionTest
: public ExtensionApiTest
,
47 public testing::WithParamInterface
<int> {
49 ~PDFExtensionTest() override
{}
51 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
52 command_line
->AppendSwitch(switches::kDisablePdfMaterialUI
);
55 void SetUpOnMainThread() override
{
56 ExtensionApiTest::SetUpOnMainThread();
57 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
60 void TearDownOnMainThread() override
{
61 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
62 ExtensionApiTest::TearDownOnMainThread();
65 bool PdfIsExpectedToFailLoad(const std::string
& pdf_file
) {
66 const char* kFailingPdfs
[] = {
67 "pdf_private/cfuzz5.pdf",
68 "pdf_private/cfuzz6.pdf",
69 "pdf_private/crash-11-14-44.pdf",
71 "pdf_private/segv-ecx.pdf",
72 "pdf_private/tests.pdf",
74 for (size_t i
= 0; i
< arraysize(kFailingPdfs
); ++i
) {
75 if (kFailingPdfs
[i
] == pdf_file
)
81 // Runs the extensions test at chrome/test/data/pdf/<filename> on the PDF file
82 // at chrome/test/data/pdf/<pdf_filename>.
83 void RunTestsInFile(std::string filename
, std::string pdf_filename
) {
84 extensions::ResultCatcher catcher
;
86 GURL
url(embedded_test_server()->GetURL("/pdf/" + pdf_filename
));
88 // It should be good enough to just navigate to the URL. But loading up the
89 // BrowserPluginGuest seems to happen asynchronously as there was flakiness
90 // being seen due to the BrowserPluginGuest not being available yet (see
91 // crbug.com/498077). So instead use |LoadPdf| which ensures that the PDF is
92 // loaded before continuing.
93 ASSERT_TRUE(LoadPdf(url
));
95 content::WebContents
* contents
=
96 browser()->tab_strip_model()->GetActiveWebContents();
97 content::BrowserPluginGuestManager
* guest_manager
=
98 contents
->GetBrowserContext()->GetGuestManager();
99 content::WebContents
* guest_contents
=
100 guest_manager
->GetFullPageGuest(contents
);
101 ASSERT_TRUE(guest_contents
);
103 base::FilePath test_data_dir
;
104 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
105 test_data_dir
= test_data_dir
.Append(FILE_PATH_LITERAL("pdf"));
106 base::FilePath test_util_path
= test_data_dir
.AppendASCII("test_util.js");
107 std::string test_util_js
;
108 ASSERT_TRUE(base::ReadFileToString(test_util_path
, &test_util_js
));
110 base::FilePath test_file_path
= test_data_dir
.AppendASCII(filename
);
112 ASSERT_TRUE(base::ReadFileToString(test_file_path
, &test_js
));
114 test_util_js
.append(test_js
);
115 ASSERT_TRUE(content::ExecuteScript(guest_contents
, test_util_js
));
117 if (!catcher
.GetNextResult())
118 FAIL() << catcher
.message();
121 // Load the PDF at the given URL and use the PDFScriptingAPI to ensure it has
122 // finished loading. Return true if it loads successfully or false if it
123 // fails. If it doesn't finish loading the test will hang. This is done from
124 // outside of the BrowserPlugin guest to ensure the PDFScriptingAPI works
125 // correctly from there.
126 bool LoadPdf(const GURL
& url
) {
127 ui_test_utils::NavigateToURL(browser(), url
);
128 content::WebContents
* web_contents
=
129 browser()->tab_strip_model()->GetActiveWebContents();
130 return pdf_extension_test_util::EnsurePDFHasLoaded(web_contents
);
133 // Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs
134 // the test if base::Hash(filename) mod kNumberLoadTestParts == k in order
135 // to shard the files evenly across values of k in [0, kNumberLoadTestParts).
136 void LoadAllPdfsTest(const std::string
& dir_name
, int k
) {
137 base::FilePath test_data_dir
;
138 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
139 base::FileEnumerator
file_enumerator(test_data_dir
.AppendASCII(dir_name
),
140 false, base::FileEnumerator::FILES
,
141 FILE_PATH_LITERAL("*.pdf"));
144 for (base::FilePath file_path
= file_enumerator
.Next(); !file_path
.empty();
145 file_path
= file_enumerator
.Next()) {
146 std::string filename
= file_path
.BaseName().MaybeAsASCII();
147 ASSERT_FALSE(filename
.empty());
149 std::string pdf_file
= dir_name
+ "/" + filename
;
150 if (static_cast<int>(base::Hash(filename
) % kNumberLoadTestParts
) == k
) {
151 LOG(INFO
) << "Loading: " << pdf_file
;
152 bool success
= LoadPdf(embedded_test_server()->GetURL("/" + pdf_file
));
153 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file
), success
);
157 // Assume that there is at least 1 pdf in the directory to guard against
158 // someone deleting the directory and silently making the test pass.
159 ASSERT_GE(count
, 1u);
163 IN_PROC_BROWSER_TEST_P(PDFExtensionTest
, Load
) {
164 #if defined(GOOGLE_CHROME_BUILD)
165 // Load private PDFs.
166 LoadAllPdfsTest("pdf_private", GetParam());
169 LoadAllPdfsTest("pdf", GetParam());
172 class DisablePluginHelper
: public content::DownloadManager::Observer
,
173 public content::NotificationObserver
{
175 DisablePluginHelper() {}
177 virtual ~DisablePluginHelper() {}
179 void DisablePlugin(Profile
* profile
) {
180 registrar_
.Add(this, chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED
,
181 content::Source
<Profile
>(profile
));
182 scoped_refptr
<PluginPrefs
> prefs(PluginPrefs::GetForProfile(profile
));
184 prefs
->EnablePluginGroup(
185 false, base::UTF8ToUTF16(ChromeContentClient::kPDFPluginName
));
186 // Wait until the plugin has been disabled.
187 disable_run_loop_
.Run();
190 const GURL
& GetLastUrl() {
191 // Wait until the download has been created.
192 download_run_loop_
.Run();
196 // content::DownloadManager::Observer implementation.
197 void OnDownloadCreated(content::DownloadManager
* manager
,
198 content::DownloadItem
* item
) override
{
199 last_url_
= item
->GetURL();
200 download_run_loop_
.Quit();
203 // content::NotificationObserver implementation.
204 void Observe(int type
,
205 const content::NotificationSource
& source
,
206 const content::NotificationDetails
& details
) override
{
207 DCHECK_EQ(chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED
, type
);
208 disable_run_loop_
.Quit();
212 content::NotificationRegistrar registrar_
;
213 base::RunLoop disable_run_loop_
;
214 base::RunLoop download_run_loop_
;
218 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, DisablePlugin
) {
219 // Disable the PDF plugin.
220 content::WebContents
* web_contents
=
221 browser()->tab_strip_model()->GetActiveWebContents();
222 content::BrowserContext
* browser_context
= web_contents
->GetBrowserContext();
223 Profile
* profile
= Profile::FromBrowserContext(browser_context
);
224 DisablePluginHelper helper
;
225 helper
.DisablePlugin(profile
);
227 // Register a download observer.
228 content::DownloadManager
* download_manager
=
229 content::BrowserContext::GetDownloadManager(browser_context
);
230 download_manager
->AddObserver(&helper
);
232 // Navigate to a PDF and test that it is downloaded.
233 GURL
url(embedded_test_server()->GetURL("/pdf/test.pdf"));
234 ui_test_utils::NavigateToURL(browser(), url
);
235 ASSERT_EQ(url
, helper
.GetLastUrl());
237 // Cancel the download to shutdown cleanly.
238 download_manager
->RemoveObserver(&helper
);
239 std::vector
<content::DownloadItem
*> downloads
;
240 download_manager
->GetAllDownloads(&downloads
);
241 ASSERT_EQ(1u, downloads
.size());
242 downloads
[0]->Cancel(false);
245 // We break PDFTest.Load up into kNumberLoadTestParts.
246 INSTANTIATE_TEST_CASE_P(PDFTestFiles
,
248 testing::Range(0, kNumberLoadTestParts
));
250 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Basic
) {
251 RunTestsInFile("basic_test.js", "test.pdf");
254 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, BasicPlugin
) {
255 RunTestsInFile("basic_plugin_test.js", "test.pdf");
258 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Viewport
) {
259 RunTestsInFile("viewport_test.js", "test.pdf");
262 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Bookmark
) {
263 RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf");
266 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, Navigator
) {
267 RunTestsInFile("navigator_test.js", "test.pdf");
270 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, ParamsParser
) {
271 RunTestsInFile("params_parser_test.js", "test.pdf");
274 IN_PROC_BROWSER_TEST_F(PDFExtensionTest
, ZoomManager
) {
275 RunTestsInFile("zoom_manager_test.js", "test.pdf");
278 class MaterialPDFExtensionTest
: public PDFExtensionTest
{
279 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
280 command_line
->AppendSwitch(switches::kEnablePdfMaterialUI
);
284 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Basic
) {
285 RunTestsInFile("basic_test_material.js", "test.pdf");
288 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, BasicPlugin
) {
289 RunTestsInFile("basic_plugin_test.js", "test.pdf");
292 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Viewport
) {
293 RunTestsInFile("viewport_test.js", "test.pdf");
296 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Bookmark
) {
297 RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf");
300 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Navigator
) {
301 RunTestsInFile("navigator_test.js", "test.pdf");
304 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, ParamsParser
) {
305 RunTestsInFile("params_parser_test.js", "test.pdf");
308 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, ZoomManager
) {
309 RunTestsInFile("zoom_manager_test.js", "test.pdf");
312 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, Elements
) {
313 // Although this test file does not require a PDF to be loaded, loading the
314 // elements without loading a PDF is difficult.
315 RunTestsInFile("material_elements_test.js", "test.pdf");
318 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest
, ToolbarManager
) {
319 RunTestsInFile("toolbar_manager_test.js", "test.pdf");