| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "chrome/common/chrome_switches.h" | 7 #include "chrome/common/chrome_switches.h" |
| 8 #include "chrome/common/print_messages.h" | 8 #include "chrome/common/print_messages.h" |
| 9 #include "chrome/renderer/printing/mock_printer.h" | 9 #include "chrome/renderer/printing/mock_printer.h" |
| 10 #include "chrome/renderer/printing/print_web_view_helper.h" | 10 #include "chrome/renderer/printing/print_web_view_helper.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using blink::WebString; | 27 using blink::WebString; |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace printing { | 30 namespace printing { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // A simple web page. | 34 // A simple web page. |
| 35 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; | 35 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; |
| 36 | 36 |
| 37 #if !defined(OS_CHROMEOS) |
| 38 |
| 37 // A simple webpage with a button to print itself with. | 39 // A simple webpage with a button to print itself with. |
| 38 const char kPrintOnUserAction[] = | 40 const char kPrintOnUserAction[] = |
| 39 "<body>" | 41 "<body>" |
| 40 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>" | 42 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>" |
| 41 "</body>"; | 43 "</body>"; |
| 42 | 44 |
| 43 #if !defined(OS_CHROMEOS) | |
| 44 // HTML with 3 pages. | 45 // HTML with 3 pages. |
| 45 const char kMultipageHTML[] = | 46 const char kMultipageHTML[] = |
| 46 "<html><head><style>" | 47 "<html><head><style>" |
| 47 ".break { page-break-after: always; }" | 48 ".break { page-break-after: always; }" |
| 48 "</style></head>" | 49 "</style></head>" |
| 49 "<body>" | 50 "<body>" |
| 50 "<div class='break'>page1</div>" | 51 "<div class='break'>page1</div>" |
| 51 "<div class='break'>page2</div>" | 52 "<div class='break'>page2</div>" |
| 52 "<div>page3</div>" | 53 "<div>page3</div>" |
| 53 "</body></html>"; | 54 "</body></html>"; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param); | 186 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param); |
| 186 EXPECT_EQ(0, post_did_print_page_param.a.page_number); | 187 EXPECT_EQ(0, post_did_print_page_param.a.page_number); |
| 187 } | 188 } |
| 188 #endif // defined(OS_CHROMEOS) | 189 #endif // defined(OS_CHROMEOS) |
| 189 } | 190 } |
| 190 void OnPrintPages() { | 191 void OnPrintPages() { |
| 191 PrintWebViewHelper::Get(view_)->OnPrintPages(); | 192 PrintWebViewHelper::Get(view_)->OnPrintPages(); |
| 192 ProcessPendingMessages(); | 193 ProcessPendingMessages(); |
| 193 } | 194 } |
| 194 | 195 |
| 196 void VerifyPreviewRequest(bool requested) { |
| 197 const IPC::Message* print_msg = |
| 198 render_thread_->sink().GetUniqueMessageMatching( |
| 199 PrintHostMsg_SetupScriptedPrintPreview::ID); |
| 200 bool did_print_msg = (NULL != print_msg); |
| 201 ASSERT_EQ(requested, did_print_msg); |
| 202 } |
| 203 |
| 195 void OnPrintPreview(const base::DictionaryValue& dict) { | 204 void OnPrintPreview(const base::DictionaryValue& dict) { |
| 196 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); | 205 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); |
| 197 print_web_view_helper->OnInitiatePrintPreview(false); | 206 print_web_view_helper->OnInitiatePrintPreview(false); |
| 198 base::RunLoop run_loop; | 207 base::RunLoop run_loop; |
| 199 DidPreviewPageListener filter(&run_loop); | 208 DidPreviewPageListener filter(&run_loop); |
| 200 render_thread_->sink().AddFilter(&filter); | 209 render_thread_->sink().AddFilter(&filter); |
| 201 print_web_view_helper->OnPrintPreview(dict); | 210 print_web_view_helper->OnPrintPreview(dict); |
| 202 run_loop.Run(); | 211 run_loop.Run(); |
| 203 render_thread_->sink().RemoveFilter(&filter); | 212 render_thread_->sink().RemoveFilter(&filter); |
| 204 } | 213 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 227 // Tests that printing pages work and sending and receiving messages through | 236 // Tests that printing pages work and sending and receiving messages through |
| 228 // that channel all works. | 237 // that channel all works. |
| 229 TEST_F(PrintWebViewHelperTest, OnPrintPages) { | 238 TEST_F(PrintWebViewHelperTest, OnPrintPages) { |
| 230 LoadHTML(kHelloWorldHTML); | 239 LoadHTML(kHelloWorldHTML); |
| 231 OnPrintPages(); | 240 OnPrintPages(); |
| 232 | 241 |
| 233 VerifyPageCount(1); | 242 VerifyPageCount(1); |
| 234 VerifyPagesPrinted(true); | 243 VerifyPagesPrinted(true); |
| 235 } | 244 } |
| 236 | 245 |
| 237 // Duplicate of OnPrintPagesTest only using javascript to print. | |
| 238 TEST_F(PrintWebViewHelperTest, PrintWithJavascript) { | |
| 239 PrintWithJavaScript(); | |
| 240 | |
| 241 VerifyPageCount(1); | |
| 242 VerifyPagesPrinted(true); | |
| 243 } | |
| 244 | |
| 245 // Tests that the renderer blocks window.print() calls if they occur too | |
| 246 // frequently. | |
| 247 TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrinting) { | |
| 248 // Pretend user will cancel printing. | |
| 249 chrome_render_thread_->set_print_dialog_user_response(false); | |
| 250 // Try to print with window.print() a few times. | |
| 251 PrintWithJavaScript(); | |
| 252 PrintWithJavaScript(); | |
| 253 PrintWithJavaScript(); | |
| 254 VerifyPagesPrinted(false); | |
| 255 | |
| 256 // Pretend user will print. (but printing is blocked.) | |
| 257 chrome_render_thread_->set_print_dialog_user_response(true); | |
| 258 PrintWithJavaScript(); | |
| 259 VerifyPagesPrinted(false); | |
| 260 | |
| 261 // Unblock script initiated printing and verify printing works. | |
| 262 PrintWebViewHelper::Get(view_)->ResetScriptedPrintCount(); | |
| 263 chrome_render_thread_->printer()->ResetPrinter(); | |
| 264 PrintWithJavaScript(); | |
| 265 VerifyPageCount(1); | |
| 266 VerifyPagesPrinted(true); | |
| 267 } | |
| 268 | |
| 269 // Tests that the renderer always allows window.print() calls if they are user | |
| 270 // initiated. | |
| 271 TEST_F(PrintWebViewHelperTest, AllowUserOriginatedPrinting) { | |
| 272 // Pretend user will cancel printing. | |
| 273 chrome_render_thread_->set_print_dialog_user_response(false); | |
| 274 // Try to print with window.print() a few times. | |
| 275 PrintWithJavaScript(); | |
| 276 PrintWithJavaScript(); | |
| 277 PrintWithJavaScript(); | |
| 278 VerifyPagesPrinted(false); | |
| 279 | |
| 280 // Pretend user will print. (but printing is blocked.) | |
| 281 chrome_render_thread_->set_print_dialog_user_response(true); | |
| 282 PrintWithJavaScript(); | |
| 283 VerifyPagesPrinted(false); | |
| 284 | |
| 285 // Try again as if user initiated, without resetting the print count. | |
| 286 chrome_render_thread_->printer()->ResetPrinter(); | |
| 287 LoadHTML(kPrintOnUserAction); | |
| 288 gfx::Size new_size(200, 100); | |
| 289 Resize(new_size, gfx::Rect(), false); | |
| 290 | |
| 291 gfx::Rect bounds = GetElementBounds("print"); | |
| 292 EXPECT_FALSE(bounds.IsEmpty()); | |
| 293 blink::WebMouseEvent mouse_event; | |
| 294 mouse_event.type = blink::WebInputEvent::MouseDown; | |
| 295 mouse_event.button = blink::WebMouseEvent::ButtonLeft; | |
| 296 mouse_event.x = bounds.CenterPoint().x(); | |
| 297 mouse_event.y = bounds.CenterPoint().y(); | |
| 298 mouse_event.clickCount = 1; | |
| 299 SendWebMouseEvent(mouse_event); | |
| 300 mouse_event.type = blink::WebInputEvent::MouseUp; | |
| 301 SendWebMouseEvent(mouse_event); | |
| 302 ProcessPendingMessages(); | |
| 303 | |
| 304 VerifyPageCount(1); | |
| 305 VerifyPagesPrinted(true); | |
| 306 } | |
| 307 | |
| 308 TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrintingFromPopup) { | |
| 309 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); | |
| 310 print_web_view_helper->SetScriptedPrintBlocked(true); | |
| 311 PrintWithJavaScript(); | |
| 312 VerifyPagesPrinted(false); | |
| 313 | |
| 314 print_web_view_helper->SetScriptedPrintBlocked(false); | |
| 315 PrintWithJavaScript(); | |
| 316 VerifyPageCount(1); | |
| 317 VerifyPagesPrinted(true); | |
| 318 } | |
| 319 | |
| 320 #if (defined(OS_WIN) && !WIN_PDF_METAFILE_FOR_PRINTING) || defined(OS_MACOSX) | 246 #if (defined(OS_WIN) && !WIN_PDF_METAFILE_FOR_PRINTING) || defined(OS_MACOSX) |
| 321 // TODO(estade): I don't think this test is worth porting to Linux. We will have | 247 // TODO(estade): I don't think this test is worth porting to Linux. We will have |
| 322 // to rip out and replace most of the IPC code if we ever plan to improve | 248 // to rip out and replace most of the IPC code if we ever plan to improve |
| 323 // printing, and the comment below by sverrir suggests that it doesn't do much | 249 // printing, and the comment below by sverrir suggests that it doesn't do much |
| 324 // for us anyway. | 250 // for us anyway. |
| 325 TEST_F(PrintWebViewHelperTest, PrintWithIframe) { | 251 TEST_F(PrintWebViewHelperTest, PrintWithIframe) { |
| 326 // Document that populates an iframe. | 252 // Document that populates an iframe. |
| 327 const char html[] = | 253 const char html[] = |
| 328 "<html><body>Lorem Ipsum:" | 254 "<html><body>Lorem Ipsum:" |
| 329 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>" | 255 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 } | 385 } |
| 460 #endif | 386 #endif |
| 461 | 387 |
| 462 // These print preview tests do not work on Chrome OS yet. | 388 // These print preview tests do not work on Chrome OS yet. |
| 463 #if !defined(OS_CHROMEOS) | 389 #if !defined(OS_CHROMEOS) |
| 464 class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase { | 390 class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase { |
| 465 public: | 391 public: |
| 466 PrintWebViewHelperPreviewTest() {} | 392 PrintWebViewHelperPreviewTest() {} |
| 467 virtual ~PrintWebViewHelperPreviewTest() {} | 393 virtual ~PrintWebViewHelperPreviewTest() {} |
| 468 | 394 |
| 469 virtual void SetUp() OVERRIDE { | |
| 470 // Append the print preview switch before creating the PrintWebViewHelper. | |
| 471 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 472 switches::kRendererPrintPreview); | |
| 473 | |
| 474 ChromeRenderViewTest::SetUp(); | |
| 475 } | |
| 476 | |
| 477 protected: | 395 protected: |
| 478 void VerifyPrintPreviewCancelled(bool did_cancel) { | 396 void VerifyPrintPreviewCancelled(bool did_cancel) { |
| 479 bool print_preview_cancelled = | 397 bool print_preview_cancelled = |
| 480 (render_thread_->sink().GetUniqueMessageMatching( | 398 (render_thread_->sink().GetUniqueMessageMatching( |
| 481 PrintHostMsg_PrintPreviewCancelled::ID) != NULL); | 399 PrintHostMsg_PrintPreviewCancelled::ID) != NULL); |
| 482 EXPECT_EQ(did_cancel, print_preview_cancelled); | 400 EXPECT_EQ(did_cancel, print_preview_cancelled); |
| 483 } | 401 } |
| 484 | 402 |
| 485 void VerifyPrintPreviewFailed(bool did_fail) { | 403 void VerifyPrintPreviewFailed(bool did_fail) { |
| 486 bool print_preview_failed = | 404 bool print_preview_failed = |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 EXPECT_EQ(margin_right, param.a.margin_right); | 475 EXPECT_EQ(margin_right, param.a.margin_right); |
| 558 EXPECT_EQ(margin_left, param.a.margin_left); | 476 EXPECT_EQ(margin_left, param.a.margin_left); |
| 559 EXPECT_EQ(margin_bottom, param.a.margin_bottom); | 477 EXPECT_EQ(margin_bottom, param.a.margin_bottom); |
| 560 EXPECT_EQ(page_has_print_css, param.c); | 478 EXPECT_EQ(page_has_print_css, param.c); |
| 561 } | 479 } |
| 562 } | 480 } |
| 563 | 481 |
| 564 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); | 482 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); |
| 565 }; | 483 }; |
| 566 | 484 |
| 485 TEST_F(PrintWebViewHelperPreviewTest, BlockScriptInitiatedPrinting) { |
| 486 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); |
| 487 print_web_view_helper->SetScriptedPrintBlocked(true); |
| 488 PrintWithJavaScript(); |
| 489 VerifyPreviewRequest(false); |
| 490 |
| 491 print_web_view_helper->SetScriptedPrintBlocked(false); |
| 492 PrintWithJavaScript(); |
| 493 VerifyPreviewRequest(true); |
| 494 } |
| 495 |
| 496 TEST_F(PrintWebViewHelperPreviewTest, PrintWithJavaScript) { |
| 497 LoadHTML(kPrintOnUserAction); |
| 498 gfx::Size new_size(200, 100); |
| 499 Resize(new_size, gfx::Rect(), false); |
| 500 |
| 501 gfx::Rect bounds = GetElementBounds("print"); |
| 502 EXPECT_FALSE(bounds.IsEmpty()); |
| 503 blink::WebMouseEvent mouse_event; |
| 504 mouse_event.type = blink::WebInputEvent::MouseDown; |
| 505 mouse_event.button = blink::WebMouseEvent::ButtonLeft; |
| 506 mouse_event.x = bounds.CenterPoint().x(); |
| 507 mouse_event.y = bounds.CenterPoint().y(); |
| 508 mouse_event.clickCount = 1; |
| 509 SendWebMouseEvent(mouse_event); |
| 510 mouse_event.type = blink::WebInputEvent::MouseUp; |
| 511 SendWebMouseEvent(mouse_event); |
| 512 |
| 513 VerifyPreviewRequest(true); |
| 514 } |
| 515 |
| 567 // Tests that print preview work and sending and receiving messages through | 516 // Tests that print preview work and sending and receiving messages through |
| 568 // that channel all works. | 517 // that channel all works. |
| 569 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { | 518 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { |
| 570 LoadHTML(kHelloWorldHTML); | 519 LoadHTML(kHelloWorldHTML); |
| 571 | 520 |
| 572 // Fill in some dummy values. | 521 // Fill in some dummy values. |
| 573 base::DictionaryValue dict; | 522 base::DictionaryValue dict; |
| 574 CreatePrintSettingsDictionary(&dict); | 523 CreatePrintSettingsDictionary(&dict); |
| 575 OnPrintPreview(dict); | 524 OnPrintPreview(dict); |
| 576 | 525 |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 957 base::DictionaryValue dict; | 906 base::DictionaryValue dict; |
| 958 CreatePrintSettingsDictionary(&dict); | 907 CreatePrintSettingsDictionary(&dict); |
| 959 OnPrintForPrintPreview(dict); | 908 OnPrintForPrintPreview(dict); |
| 960 | 909 |
| 961 VerifyPrintFailed(true); | 910 VerifyPrintFailed(true); |
| 962 VerifyPagesPrinted(false); | 911 VerifyPagesPrinted(false); |
| 963 } | 912 } |
| 964 | 913 |
| 965 #endif // !defined(OS_CHROMEOS) | 914 #endif // !defined(OS_CHROMEOS) |
| 966 | 915 |
| 967 class PrintWebViewHelperKioskTest : public PrintWebViewHelperTestBase { | |
| 968 public: | |
| 969 PrintWebViewHelperKioskTest() {} | |
| 970 virtual ~PrintWebViewHelperKioskTest() {} | |
| 971 | |
| 972 virtual void SetUp() OVERRIDE { | |
| 973 // Append the throttling disable switch before creating the | |
| 974 // PrintWebViewHelper. | |
| 975 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 976 switches::kDisableScriptedPrintThrottling); | |
| 977 | |
| 978 ChromeRenderViewTest::SetUp(); | |
| 979 } | |
| 980 | |
| 981 protected: | |
| 982 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperKioskTest); | |
| 983 }; | |
| 984 | |
| 985 // Tests that the switch overrides the throttling that blocks window.print() | |
| 986 // calls if they occur too frequently. Compare with | |
| 987 // PrintWebViewHelperTest.BlockScriptInitiatedPrinting above. | |
| 988 TEST_F(PrintWebViewHelperKioskTest, DontBlockScriptInitiatedPrinting) { | |
| 989 // Pretend user will cancel printing. | |
| 990 chrome_render_thread_->set_print_dialog_user_response(false); | |
| 991 // Try to print with window.print() a few times. | |
| 992 PrintWithJavaScript(); | |
| 993 chrome_render_thread_->printer()->ResetPrinter(); | |
| 994 PrintWithJavaScript(); | |
| 995 chrome_render_thread_->printer()->ResetPrinter(); | |
| 996 PrintWithJavaScript(); | |
| 997 chrome_render_thread_->printer()->ResetPrinter(); | |
| 998 VerifyPagesPrinted(false); | |
| 999 | |
| 1000 // Pretend user will print, should not be throttled. | |
| 1001 chrome_render_thread_->set_print_dialog_user_response(true); | |
| 1002 PrintWithJavaScript(); | |
| 1003 VerifyPagesPrinted(true); | |
| 1004 } | |
| 1005 | |
| 1006 } // namespace printing | 916 } // namespace printing |
| OLD | NEW |