| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/printing/renderer/print_web_view_helper.h" | 5 #include "components/printing/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <tuple> | 10 #include <tuple> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "components/printing/common/print_messages.h" | 17 #include "components/printing/common/print_messages.h" |
| 18 #include "components/printing/test/mock_printer.h" | 18 #include "components/printing/test/mock_printer.h" |
| 19 #include "components/printing/test/print_mock_render_thread.h" | 19 #include "components/printing/test/print_mock_render_thread.h" |
| 20 #include "components/printing/test/print_test_content_renderer_client.h" | 20 #include "components/printing/test/print_test_content_renderer_client.h" |
| 21 #include "content/public/renderer/render_frame.h" |
| 21 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
| 22 #include "content/public/test/render_view_test.h" | 23 #include "content/public/test/render_view_test.h" |
| 23 #include "ipc/ipc_listener.h" | 24 #include "ipc/ipc_listener.h" |
| 24 #include "printing/features/features.h" | 25 #include "printing/features/features.h" |
| 25 #include "printing/print_job_constants.h" | 26 #include "printing/print_job_constants.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "third_party/WebKit/public/platform/WebString.h" | 28 #include "third_party/WebKit/public/platform/WebString.h" |
| 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 29 #include "third_party/WebKit/public/web/WebRange.h" | 30 #include "third_party/WebKit/public/web/WebRange.h" |
| 30 #include "third_party/WebKit/public/web/WebView.h" | 31 #include "third_party/WebKit/public/web/WebView.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 ASSERT_EQ(printed, did_print_msg); | 218 ASSERT_EQ(printed, did_print_msg); |
| 218 if (printed) { | 219 if (printed) { |
| 219 PrintHostMsg_DidPrintPage::Param post_did_print_page_param; | 220 PrintHostMsg_DidPrintPage::Param post_did_print_page_param; |
| 220 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param); | 221 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param); |
| 221 EXPECT_EQ(0, std::get<0>(post_did_print_page_param).page_number); | 222 EXPECT_EQ(0, std::get<0>(post_did_print_page_param).page_number); |
| 222 } | 223 } |
| 223 } | 224 } |
| 224 | 225 |
| 225 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 226 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 226 void OnPrintPages() { | 227 void OnPrintPages() { |
| 227 PrintWebViewHelper::Get(view_)->OnPrintPages(); | 228 GetPrintWebViewHelper()->OnPrintPages(); |
| 228 ProcessPendingMessages(); | 229 ProcessPendingMessages(); |
| 229 } | 230 } |
| 230 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 231 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 231 | 232 |
| 232 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 233 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 233 void VerifyPreviewRequest(bool requested) { | 234 void VerifyPreviewRequest(bool requested) { |
| 234 const IPC::Message* print_msg = | 235 const IPC::Message* print_msg = |
| 235 render_thread_->sink().GetUniqueMessageMatching( | 236 render_thread_->sink().GetUniqueMessageMatching( |
| 236 PrintHostMsg_SetupScriptedPrintPreview::ID); | 237 PrintHostMsg_SetupScriptedPrintPreview::ID); |
| 237 bool did_print_msg = !!print_msg; | 238 bool did_print_msg = !!print_msg; |
| 238 ASSERT_EQ(requested, did_print_msg); | 239 ASSERT_EQ(requested, did_print_msg); |
| 239 } | 240 } |
| 240 | 241 |
| 241 void OnPrintPreview(const base::DictionaryValue& dict) { | 242 void OnPrintPreview(const base::DictionaryValue& dict) { |
| 242 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); | 243 PrintWebViewHelper* print_web_view_helper = GetPrintWebViewHelper(); |
| 243 print_web_view_helper->OnInitiatePrintPreview(false); | 244 print_web_view_helper->OnInitiatePrintPreview(false); |
| 244 base::RunLoop run_loop; | 245 base::RunLoop run_loop; |
| 245 DidPreviewPageListener filter(&run_loop); | 246 DidPreviewPageListener filter(&run_loop); |
| 246 render_thread_->sink().AddFilter(&filter); | 247 render_thread_->sink().AddFilter(&filter); |
| 247 print_web_view_helper->OnPrintPreview(dict); | 248 print_web_view_helper->OnPrintPreview(dict); |
| 248 run_loop.Run(); | 249 run_loop.Run(); |
| 249 render_thread_->sink().RemoveFilter(&filter); | 250 render_thread_->sink().RemoveFilter(&filter); |
| 250 } | 251 } |
| 251 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 252 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 252 | 253 |
| 253 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 254 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 254 void OnPrintForPrintPreview(const base::DictionaryValue& dict) { | 255 void OnPrintForPrintPreview(const base::DictionaryValue& dict) { |
| 255 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict); | 256 GetPrintWebViewHelper()->OnPrintForPrintPreview(dict); |
| 256 ProcessPendingMessages(); | 257 ProcessPendingMessages(); |
| 257 } | 258 } |
| 258 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 259 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 259 | 260 |
| 261 PrintWebViewHelper* GetPrintWebViewHelper() { |
| 262 return PrintWebViewHelper::Get( |
| 263 content::RenderFrame::FromWebFrame(GetMainFrame())); |
| 264 } |
| 265 |
| 260 // Naked pointer as ownership is with content::RenderViewTest::render_thread_. | 266 // Naked pointer as ownership is with content::RenderViewTest::render_thread_. |
| 261 PrintMockRenderThread* print_render_thread_; | 267 PrintMockRenderThread* print_render_thread_; |
| 262 | 268 |
| 263 private: | 269 private: |
| 264 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase); | 270 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase); |
| 265 }; | 271 }; |
| 266 | 272 |
| 267 // RenderViewTest-based tests crash on Android | 273 // RenderViewTest-based tests crash on Android |
| 268 // http://crbug.com/187500 | 274 // http://crbug.com/187500 |
| 269 #if defined(OS_ANDROID) | 275 #if defined(OS_ANDROID) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 295 PrintWithJavaScript(); | 301 PrintWithJavaScript(); |
| 296 PrintWithJavaScript(); | 302 PrintWithJavaScript(); |
| 297 VerifyPagesPrinted(false); | 303 VerifyPagesPrinted(false); |
| 298 | 304 |
| 299 // Pretend user will print. (but printing is blocked.) | 305 // Pretend user will print. (but printing is blocked.) |
| 300 print_render_thread_->set_print_dialog_user_response(true); | 306 print_render_thread_->set_print_dialog_user_response(true); |
| 301 PrintWithJavaScript(); | 307 PrintWithJavaScript(); |
| 302 VerifyPagesPrinted(false); | 308 VerifyPagesPrinted(false); |
| 303 | 309 |
| 304 // Unblock script initiated printing and verify printing works. | 310 // Unblock script initiated printing and verify printing works. |
| 305 PrintWebViewHelper::Get(view_)->scripting_throttler_.Reset(); | 311 GetPrintWebViewHelper()->scripting_throttler_.Reset(); |
| 306 print_render_thread_->printer()->ResetPrinter(); | 312 print_render_thread_->printer()->ResetPrinter(); |
| 307 PrintWithJavaScript(); | 313 PrintWithJavaScript(); |
| 308 VerifyPageCount(1); | 314 VerifyPageCount(1); |
| 309 VerifyPagesPrinted(true); | 315 VerifyPagesPrinted(true); |
| 310 } | 316 } |
| 311 | 317 |
| 312 // Tests that the renderer always allows window.print() calls if they are user | 318 // Tests that the renderer always allows window.print() calls if they are user |
| 313 // initiated. | 319 // initiated. |
| 314 TEST_F(MAYBE_PrintWebViewHelperTest, AllowUserOriginatedPrinting) { | 320 TEST_F(MAYBE_PrintWebViewHelperTest, AllowUserOriginatedPrinting) { |
| 315 // Pretend user will cancel printing. | 321 // Pretend user will cancel printing. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 " document.write(frames['sub1'].name);" | 388 " document.write(frames['sub1'].name);" |
| 383 " frames['sub1'].document.write(" | 389 " frames['sub1'].document.write(" |
| 384 " '<p>Cras tempus ante eu felis semper luctus!</p>');" | 390 " '<p>Cras tempus ante eu felis semper luctus!</p>');" |
| 385 " frames['sub1'].document.close();" | 391 " frames['sub1'].document.close();" |
| 386 "</script></body></html>"; | 392 "</script></body></html>"; |
| 387 | 393 |
| 388 LoadHTML(html); | 394 LoadHTML(html); |
| 389 | 395 |
| 390 // Find the frame and set it as the focused one. This should mean that that | 396 // Find the frame and set it as the focused one. This should mean that that |
| 391 // the printout should only contain the contents of that frame. | 397 // the printout should only contain the contents of that frame. |
| 392 WebFrame* sub1_frame = | 398 auto* web_view = view_->GetWebView(); |
| 393 view_->GetWebView()->findFrameByName(WebString::fromUTF8("sub1")); | 399 WebFrame* sub1_frame = web_view->findFrameByName(WebString::fromUTF8("sub1")); |
| 394 ASSERT_TRUE(sub1_frame); | 400 ASSERT_TRUE(sub1_frame); |
| 395 view_->GetWebView()->setFocusedFrame(sub1_frame); | 401 web_view->setFocusedFrame(sub1_frame); |
| 396 ASSERT_NE(view_->GetWebView()->focusedFrame(), | 402 ASSERT_NE(web_view->focusedFrame(), web_view->mainFrame()); |
| 397 view_->GetWebView()->mainFrame()); | |
| 398 | 403 |
| 399 // Initiate printing. | 404 // Initiate printing. |
| 400 OnPrintPages(); | 405 OnPrintPages(); |
| 401 VerifyPagesPrinted(true); | 406 VerifyPagesPrinted(true); |
| 402 | 407 |
| 403 // Verify output through MockPrinter. | 408 // Verify output through MockPrinter. |
| 404 const MockPrinter* printer(print_render_thread_->printer()); | 409 const MockPrinter* printer(print_render_thread_->printer()); |
| 405 ASSERT_EQ(1, printer->GetPrintedPages()); | 410 ASSERT_EQ(1, printer->GetPrintedPages()); |
| 406 const Image& image1(printer->GetPrintedPage(0)->image()); | 411 const Image& image1(printer->GetPrintedPage(0)->image()); |
| 407 | 412 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 EXPECT_EQ(page_has_print_css, std::get<2>(param)); | 621 EXPECT_EQ(page_has_print_css, std::get<2>(param)); |
| 617 } | 622 } |
| 618 } | 623 } |
| 619 | 624 |
| 620 private: | 625 private: |
| 621 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperPreviewTest); | 626 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperPreviewTest); |
| 622 }; | 627 }; |
| 623 | 628 |
| 624 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, BlockScriptInitiatedPrinting) { | 629 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, BlockScriptInitiatedPrinting) { |
| 625 LoadHTML(kHelloWorldHTML); | 630 LoadHTML(kHelloWorldHTML); |
| 626 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); | 631 PrintWebViewHelper* print_web_view_helper = GetPrintWebViewHelper(); |
| 627 print_web_view_helper->SetScriptedPrintBlocked(true); | 632 print_web_view_helper->OnSetPrintingEnabled(false); |
| 628 PrintWithJavaScript(); | 633 PrintWithJavaScript(); |
| 629 VerifyPreviewRequest(false); | 634 VerifyPreviewRequest(false); |
| 630 | 635 |
| 631 print_web_view_helper->SetScriptedPrintBlocked(false); | 636 print_web_view_helper->OnSetPrintingEnabled(true); |
| 632 PrintWithJavaScript(); | 637 PrintWithJavaScript(); |
| 633 VerifyPreviewRequest(true); | 638 VerifyPreviewRequest(true); |
| 634 } | 639 } |
| 635 | 640 |
| 636 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintWithJavaScript) { | 641 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintWithJavaScript) { |
| 637 LoadHTML(kPrintOnUserAction); | 642 LoadHTML(kPrintOnUserAction); |
| 638 gfx::Size new_size(200, 100); | 643 gfx::Size new_size(200, 100); |
| 639 Resize(new_size, false); | 644 Resize(new_size, false); |
| 640 | 645 |
| 641 gfx::Rect bounds = GetElementBounds("print"); | 646 gfx::Rect bounds = GetElementBounds("print"); |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1054 | 1059 |
| 1055 VerifyPrintFailed(true); | 1060 VerifyPrintFailed(true); |
| 1056 VerifyPagesPrinted(false); | 1061 VerifyPagesPrinted(false); |
| 1057 } | 1062 } |
| 1058 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 1063 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 1059 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 1064 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 1060 | 1065 |
| 1061 #endif // !defined(OS_CHROMEOS) | 1066 #endif // !defined(OS_CHROMEOS) |
| 1062 | 1067 |
| 1063 } // namespace printing | 1068 } // namespace printing |
| OLD | NEW |