Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(947)

Side by Side Diff: chrome/browser/pdf/pdf_extension_test.cc

Issue 1362433002: Fix for "chrome://" links in PDFs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests. Addressed comments. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/files/file_enumerator.h" 8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/hash.h" 10 #include "base/hash.h"
(...skipping 14 matching lines...) Expand all
25 #include "chrome/common/chrome_content_client.h" 25 #include "chrome/common/chrome_content_client.h"
26 #include "chrome/common/chrome_paths.h" 26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/base/ui_test_utils.h" 28 #include "chrome/test/base/ui_test_utils.h"
29 #include "content/public/browser/browser_plugin_guest_manager.h" 29 #include "content/public/browser/browser_plugin_guest_manager.h"
30 #include "content/public/browser/download_item.h" 30 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager.h" 31 #include "content/public/browser/download_manager.h"
32 #include "content/public/browser/notification_observer.h" 32 #include "content/public/browser/notification_observer.h"
33 #include "content/public/browser/notification_registrar.h" 33 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/plugin_service.h" 34 #include "content/public/browser/plugin_service.h"
35 #include "content/public/browser/render_process_host.h"
35 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
36 #include "content/public/test/browser_test_utils.h" 37 #include "content/public/test/browser_test_utils.h"
37 #include "extensions/browser/extension_registry.h" 38 #include "extensions/browser/extension_registry.h"
38 #include "extensions/common/manifest_handlers/mime_types_handler.h" 39 #include "extensions/common/manifest_handlers/mime_types_handler.h"
39 #include "extensions/test/result_catcher.h" 40 #include "extensions/test/result_catcher.h"
40 #include "net/test/embedded_test_server/embedded_test_server.h" 41 #include "net/test/embedded_test_server/embedded_test_server.h"
41 #include "ui/base/resource/resource_bundle.h" 42 #include "ui/base/resource/resource_bundle.h"
42 #include "url/gurl.h" 43 #include "url/gurl.h"
43 44
44 const int kNumberLoadTestParts = 10; 45 const int kNumberLoadTestParts = 10;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 void RunTestsInFile(std::string filename, std::string pdf_filename) { 93 void RunTestsInFile(std::string filename, std::string pdf_filename) {
93 extensions::ResultCatcher catcher; 94 extensions::ResultCatcher catcher;
94 95
95 GURL url(embedded_test_server()->GetURL("/pdf/" + pdf_filename)); 96 GURL url(embedded_test_server()->GetURL("/pdf/" + pdf_filename));
96 97
97 // It should be good enough to just navigate to the URL. But loading up the 98 // It should be good enough to just navigate to the URL. But loading up the
98 // BrowserPluginGuest seems to happen asynchronously as there was flakiness 99 // BrowserPluginGuest seems to happen asynchronously as there was flakiness
99 // being seen due to the BrowserPluginGuest not being available yet (see 100 // being seen due to the BrowserPluginGuest not being available yet (see
100 // crbug.com/498077). So instead use |LoadPdf| which ensures that the PDF is 101 // crbug.com/498077). So instead use |LoadPdf| which ensures that the PDF is
101 // loaded before continuing. 102 // loaded before continuing.
102 ASSERT_TRUE(LoadPdf(url)); 103 content::WebContents* guest_contents = LoadPdfGetGuestContents(url);
103
104 content::WebContents* contents =
105 browser()->tab_strip_model()->GetActiveWebContents();
106 content::BrowserPluginGuestManager* guest_manager =
107 contents->GetBrowserContext()->GetGuestManager();
108 content::WebContents* guest_contents =
109 guest_manager->GetFullPageGuest(contents);
110 ASSERT_TRUE(guest_contents); 104 ASSERT_TRUE(guest_contents);
111 105
112 base::FilePath test_data_dir; 106 base::FilePath test_data_dir;
113 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); 107 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
114 test_data_dir = test_data_dir.Append(FILE_PATH_LITERAL("pdf")); 108 test_data_dir = test_data_dir.Append(FILE_PATH_LITERAL("pdf"));
115 base::FilePath test_util_path = test_data_dir.AppendASCII("test_util.js"); 109 base::FilePath test_util_path = test_data_dir.AppendASCII("test_util.js");
116 std::string test_util_js; 110 std::string test_util_js;
117 ASSERT_TRUE(base::ReadFileToString(test_util_path, &test_util_js)); 111 ASSERT_TRUE(base::ReadFileToString(test_util_path, &test_util_js));
118 112
119 base::FilePath test_file_path = test_data_dir.AppendASCII(filename); 113 base::FilePath test_file_path = test_data_dir.AppendASCII(filename);
(...skipping 12 matching lines...) Expand all
132 // fails. If it doesn't finish loading the test will hang. This is done from 126 // fails. If it doesn't finish loading the test will hang. This is done from
133 // outside of the BrowserPlugin guest to ensure the PDFScriptingAPI works 127 // outside of the BrowserPlugin guest to ensure the PDFScriptingAPI works
134 // correctly from there. 128 // correctly from there.
135 bool LoadPdf(const GURL& url) { 129 bool LoadPdf(const GURL& url) {
136 ui_test_utils::NavigateToURL(browser(), url); 130 ui_test_utils::NavigateToURL(browser(), url);
137 content::WebContents* web_contents = 131 content::WebContents* web_contents =
138 browser()->tab_strip_model()->GetActiveWebContents(); 132 browser()->tab_strip_model()->GetActiveWebContents();
139 return pdf_extension_test_util::EnsurePDFHasLoaded(web_contents); 133 return pdf_extension_test_util::EnsurePDFHasLoaded(web_contents);
140 } 134 }
141 135
136 // Same as |LoadPdf|, but also returns a pointer to the guest WebContents for
137 // the loaded PDF. Returns nullptr if the load fails.
138 content::WebContents* LoadPdfGetGuestContents(const GURL& url) {
139 if (!LoadPdf(url))
140 return nullptr;
141
142 content::WebContents* contents =
143 browser()->tab_strip_model()->GetActiveWebContents();
144 content::BrowserPluginGuestManager* guest_manager =
145 contents->GetBrowserContext()->GetGuestManager();
146 content::WebContents* guest_contents =
147 guest_manager->GetFullPageGuest(contents);
148 return guest_contents;
149 }
150
142 // Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs 151 // Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs
143 // the test if base::Hash(filename) mod kNumberLoadTestParts == k in order 152 // the test if base::Hash(filename) mod kNumberLoadTestParts == k in order
144 // to shard the files evenly across values of k in [0, kNumberLoadTestParts). 153 // to shard the files evenly across values of k in [0, kNumberLoadTestParts).
145 void LoadAllPdfsTest(const std::string& dir_name, int k) { 154 void LoadAllPdfsTest(const std::string& dir_name, int k) {
146 base::FilePath test_data_dir; 155 base::FilePath test_data_dir;
147 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); 156 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
148 base::FileEnumerator file_enumerator(test_data_dir.AppendASCII(dir_name), 157 base::FileEnumerator file_enumerator(test_data_dir.AppendASCII(dir_name),
149 false, base::FileEnumerator::FILES, 158 false, base::FileEnumerator::FILES,
150 FILE_PATH_LITERAL("*.pdf")); 159 FILE_PATH_LITERAL("*.pdf"));
151 160
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 "</body></html>"; 379 "</body></html>";
371 TestGetSelectedTextReply(GURL(data_url), false); 380 TestGetSelectedTextReply(GURL(data_url), false);
372 } 381 }
373 382
374 // Ensure same-origin replies do work for getSelectedText. 383 // Ensure same-origin replies do work for getSelectedText.
375 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureSameOriginRepliesAllowed) { 384 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureSameOriginRepliesAllowed) {
376 TestGetSelectedTextReply(embedded_test_server()->GetURL("/pdf/test.pdf"), 385 TestGetSelectedTextReply(embedded_test_server()->GetURL("/pdf/test.pdf"),
377 true); 386 true);
378 } 387 }
379 388
389 // This test ensures that link permissions are enforced properly in PDFs.
390 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, LinkPermissions) {
391 GURL test_pdf_url(embedded_test_server()->GetURL("/pdf/test.pdf"));
392 content::WebContents* guest_contents = LoadPdfGetGuestContents(test_pdf_url);
393 ASSERT_TRUE(guest_contents);
394
395 // chrome://favicon links should be allowed for PDFs, while chrome://settings
396 // links should not.
397 GURL valid_link_url("chrome://favicon/https://www.google.ca/");
398 GURL invalid_link_url("chrome://settings");
399
400 GURL unfiltered_valid_link_url(valid_link_url);
401 content::RenderProcessHost* rph = guest_contents->GetRenderProcessHost();
402 rph->FilterURL(true, &valid_link_url);
403 rph->FilterURL(true, &invalid_link_url);
Charlie Reis 2015/09/29 22:15:53 Hmm, is there no way to attempt to request the res
paulmeyer 2015/09/30 14:21:31 The issue behind this bug is that context menus al
Charlie Reis 2015/09/30 20:39:17 Acknowledged.
404
405 // Invalid link URLs should be changed to "about:blank" when filtered.
406 ASSERT_EQ(valid_link_url, unfiltered_valid_link_url);
Charlie Reis 2015/09/29 22:15:53 nit: Use EXPECT_EQ for test expectations (so that
paulmeyer 2015/09/30 14:21:31 Done.
407 ASSERT_EQ(invalid_link_url, GURL("about:blank"));
Charlie Reis 2015/09/29 22:15:53 nit: The arguments should be (expected, actual), s
paulmeyer 2015/09/30 14:21:31 Done.
408 }
409
380 class MaterialPDFExtensionTest : public PDFExtensionTest { 410 class MaterialPDFExtensionTest : public PDFExtensionTest {
381 void SetUpCommandLine(base::CommandLine* command_line) override { 411 void SetUpCommandLine(base::CommandLine* command_line) override {
382 command_line->AppendSwitch(switches::kEnablePdfMaterialUI); 412 command_line->AppendSwitch(switches::kEnablePdfMaterialUI);
383 } 413 }
384 }; 414 };
385 415
386 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) { 416 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) {
387 RunTestsInFile("basic_test_material.js", "test.pdf"); 417 RunTestsInFile("basic_test_material.js", "test.pdf");
388 } 418 }
389 419
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 RunTestsInFile("toolbar_manager_test.js", "test.pdf"); 451 RunTestsInFile("toolbar_manager_test.js", "test.pdf");
422 } 452 }
423 453
424 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Title) { 454 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Title) {
425 RunTestsInFile("title_test.js", "test-title.pdf"); 455 RunTestsInFile("title_test.js", "test-title.pdf");
426 } 456 }
427 457
428 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, WhitespaceTitle) { 458 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, WhitespaceTitle) {
429 RunTestsInFile("whitespace_title_test.js", "test-whitespace-title.pdf"); 459 RunTestsInFile("whitespace_title_test.js", "test-whitespace-title.pdf");
430 } 460 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698