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