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