OLD | NEW |
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 25 matching lines...) Expand all Loading... |
36 #include "content/public/test/browser_test_utils.h" | 36 #include "content/public/test/browser_test_utils.h" |
37 #include "extensions/browser/extension_registry.h" | 37 #include "extensions/browser/extension_registry.h" |
38 #include "extensions/common/manifest_handlers/mime_types_handler.h" | 38 #include "extensions/common/manifest_handlers/mime_types_handler.h" |
39 #include "extensions/test/result_catcher.h" | 39 #include "extensions/test/result_catcher.h" |
40 #include "net/test/embedded_test_server/embedded_test_server.h" | 40 #include "net/test/embedded_test_server/embedded_test_server.h" |
41 #include "ui/base/resource/resource_bundle.h" | 41 #include "ui/base/resource/resource_bundle.h" |
42 #include "url/gurl.h" | 42 #include "url/gurl.h" |
43 | 43 |
44 const int kNumberLoadTestParts = 10; | 44 const int kNumberLoadTestParts = 10; |
45 | 45 |
| 46 bool GetGuestCallback(content::WebContents** guest_out, |
| 47 content::WebContents* guest) { |
| 48 EXPECT_FALSE(*guest_out); |
| 49 *guest_out = guest; |
| 50 // Return false so that we iterate through all the guests and verify there is |
| 51 // only one. |
| 52 return false; |
| 53 } |
| 54 |
46 class PDFExtensionTest : public ExtensionApiTest, | 55 class PDFExtensionTest : public ExtensionApiTest, |
47 public testing::WithParamInterface<int> { | 56 public testing::WithParamInterface<int> { |
48 public: | 57 public: |
49 ~PDFExtensionTest() override {} | 58 ~PDFExtensionTest() override {} |
50 | 59 |
51 void SetUpCommandLine(base::CommandLine* command_line) override { | 60 void SetUpCommandLine(base::CommandLine* command_line) override { |
52 command_line->AppendSwitch(switches::kDisablePdfMaterialUI); | 61 command_line->AppendSwitch(switches::kDisablePdfMaterialUI); |
53 } | 62 } |
54 | 63 |
55 void SetUpOnMainThread() override { | 64 void SetUpOnMainThread() override { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 LOG(INFO) << "Loading: " << pdf_file; | 160 LOG(INFO) << "Loading: " << pdf_file; |
152 bool success = LoadPdf(embedded_test_server()->GetURL("/" + pdf_file)); | 161 bool success = LoadPdf(embedded_test_server()->GetURL("/" + pdf_file)); |
153 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file), success); | 162 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file), success); |
154 } | 163 } |
155 ++count; | 164 ++count; |
156 } | 165 } |
157 // Assume that there is at least 1 pdf in the directory to guard against | 166 // 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. | 167 // someone deleting the directory and silently making the test pass. |
159 ASSERT_GE(count, 1u); | 168 ASSERT_GE(count, 1u); |
160 } | 169 } |
| 170 |
| 171 void TestGetSelectedTextReply(GURL url, bool expect_success) { |
| 172 ui_test_utils::NavigateToURL(browser(), url); |
| 173 content::WebContents* web_contents = |
| 174 browser()->tab_strip_model()->GetActiveWebContents(); |
| 175 ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(web_contents)); |
| 176 |
| 177 // Reach into the guest and hook into it such that it posts back a 'flush' |
| 178 // message after every getSelectedTextReply message sent. |
| 179 content::BrowserPluginGuestManager* guest_manager = |
| 180 web_contents->GetBrowserContext()->GetGuestManager(); |
| 181 content::WebContents* guest_contents = nullptr; |
| 182 ASSERT_NO_FATAL_FAILURE(guest_manager->ForEachGuest( |
| 183 web_contents, base::Bind(&GetGuestCallback, &guest_contents))); |
| 184 ASSERT_TRUE(guest_contents); |
| 185 ASSERT_TRUE(content::ExecuteScript( |
| 186 guest_contents, |
| 187 "var oldSendScriptingMessage = " |
| 188 " PDFViewer.prototype.sendScriptingMessage_;" |
| 189 "PDFViewer.prototype.sendScriptingMessage_ = function(message) {" |
| 190 " oldSendScriptingMessage.bind(this)(message);" |
| 191 " if (message.type == 'getSelectedTextReply')" |
| 192 " this.parentWindow_.postMessage('flush', '*');" |
| 193 "}")); |
| 194 |
| 195 // Add an event listener for flush messages and request the selected text. |
| 196 // If we get a flush message without receiving getSelectedText we know that |
| 197 // the message didn't come through. |
| 198 bool success = false; |
| 199 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 200 web_contents, |
| 201 "window.addEventListener('message', function(event) {" |
| 202 " if (event.data == 'flush')" |
| 203 " window.domAutomationController.send(false);" |
| 204 " if (event.data.type == 'getSelectedTextReply')" |
| 205 " window.domAutomationController.send(true);" |
| 206 "});" |
| 207 "document.getElementsByTagName('embed')[0].postMessage(" |
| 208 " {type: 'getSelectedText'});", |
| 209 &success)); |
| 210 ASSERT_EQ(expect_success, success); |
| 211 } |
161 }; | 212 }; |
162 | 213 |
163 IN_PROC_BROWSER_TEST_P(PDFExtensionTest, Load) { | 214 IN_PROC_BROWSER_TEST_P(PDFExtensionTest, Load) { |
164 #if defined(GOOGLE_CHROME_BUILD) | 215 #if defined(GOOGLE_CHROME_BUILD) |
165 // Load private PDFs. | 216 // Load private PDFs. |
166 LoadAllPdfsTest("pdf_private", GetParam()); | 217 LoadAllPdfsTest("pdf_private", GetParam()); |
167 #endif | 218 #endif |
168 // Load public PDFs. | 219 // Load public PDFs. |
169 LoadAllPdfsTest("pdf", GetParam()); | 220 LoadAllPdfsTest("pdf", GetParam()); |
170 } | 221 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 319 } |
269 | 320 |
270 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ParamsParser) { | 321 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ParamsParser) { |
271 RunTestsInFile("params_parser_test.js", "test.pdf"); | 322 RunTestsInFile("params_parser_test.js", "test.pdf"); |
272 } | 323 } |
273 | 324 |
274 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ZoomManager) { | 325 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ZoomManager) { |
275 RunTestsInFile("zoom_manager_test.js", "test.pdf"); | 326 RunTestsInFile("zoom_manager_test.js", "test.pdf"); |
276 } | 327 } |
277 | 328 |
| 329 // Ensure that the internal PDF plugin application/x-google-chrome-pdf won't be |
| 330 // loaded if it's not loaded in the chrome extension page. |
| 331 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureInternalPluginDisabled) { |
| 332 std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec(); |
| 333 std::string data_url = |
| 334 "data:text/html," |
| 335 "<html><body>" |
| 336 "<embed type=\"application/x-google-chrome-pdf\" src=\"" + |
| 337 url + |
| 338 "\">" |
| 339 "</body></html>"; |
| 340 ui_test_utils::NavigateToURL(browser(), GURL(data_url)); |
| 341 content::WebContents* web_contents = |
| 342 browser()->tab_strip_model()->GetActiveWebContents(); |
| 343 bool plugin_loaded = false; |
| 344 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 345 web_contents, |
| 346 "var plugin_loaded = " |
| 347 " document.getElementsByTagName('embed')[0].postMessage !== undefined;" |
| 348 "window.domAutomationController.send(plugin_loaded);", |
| 349 &plugin_loaded)); |
| 350 ASSERT_FALSE(plugin_loaded); |
| 351 } |
| 352 |
| 353 // Ensure cross-origin replies won't work for getSelectedText. |
| 354 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureCrossOriginRepliesBlocked) { |
| 355 std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec(); |
| 356 std::string data_url = |
| 357 "data:text/html," |
| 358 "<html><body>" |
| 359 "<embed type=\"application/pdf\" src=\"" + |
| 360 url + |
| 361 "\">" |
| 362 "</body></html>"; |
| 363 TestGetSelectedTextReply(GURL(data_url), false); |
| 364 } |
| 365 |
| 366 // Ensure same-origin replies do work for getSelectedText. |
| 367 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureSameOriginRepliesAllowed) { |
| 368 TestGetSelectedTextReply(embedded_test_server()->GetURL("/pdf/test.pdf"), |
| 369 true); |
| 370 } |
| 371 |
278 class MaterialPDFExtensionTest : public PDFExtensionTest { | 372 class MaterialPDFExtensionTest : public PDFExtensionTest { |
279 void SetUpCommandLine(base::CommandLine* command_line) override { | 373 void SetUpCommandLine(base::CommandLine* command_line) override { |
280 command_line->AppendSwitch(switches::kEnablePdfMaterialUI); | 374 command_line->AppendSwitch(switches::kEnablePdfMaterialUI); |
281 } | 375 } |
282 }; | 376 }; |
283 | 377 |
284 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) { | 378 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) { |
285 RunTestsInFile("basic_test_material.js", "test.pdf"); | 379 RunTestsInFile("basic_test_material.js", "test.pdf"); |
286 } | 380 } |
287 | 381 |
(...skipping 23 matching lines...) Expand all Loading... |
311 | 405 |
312 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Elements) { | 406 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Elements) { |
313 // Although this test file does not require a PDF to be loaded, loading the | 407 // Although this test file does not require a PDF to be loaded, loading the |
314 // elements without loading a PDF is difficult. | 408 // elements without loading a PDF is difficult. |
315 RunTestsInFile("material_elements_test.js", "test.pdf"); | 409 RunTestsInFile("material_elements_test.js", "test.pdf"); |
316 } | 410 } |
317 | 411 |
318 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ToolbarManager) { | 412 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ToolbarManager) { |
319 RunTestsInFile("toolbar_manager_test.js", "test.pdf"); | 413 RunTestsInFile("toolbar_manager_test.js", "test.pdf"); |
320 } | 414 } |
OLD | NEW |