OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/chrome_switches.h" | 5 #include "chrome/common/chrome_switches.h" |
6 #include "chrome/common/print_messages.h" | 6 #include "chrome/common/print_messages.h" |
7 #include "chrome/renderer/print_web_view_helper.h" | 7 #include "chrome/renderer/print_web_view_helper.h" |
8 #include "chrome/test/base/chrome_render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "printing/print_job_constants.h" | 10 #include "printing/print_job_constants.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
15 | 15 |
16 #if defined(OS_WIN) || defined(OS_MACOSX) | 16 #if defined(OS_WIN) || defined(OS_MACOSX) |
17 #include "base/file_util.h" | 17 #include "base/file_util.h" |
18 #include "printing/image.h" | 18 #include "printing/image.h" |
19 | 19 |
20 using WebKit::WebFrame; | 20 using WebKit::WebFrame; |
21 using WebKit::WebString; | 21 using WebKit::WebString; |
22 #endif | 22 #endif |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // A simple web page. | 26 // A simple web page. |
27 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; | 27 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; |
28 | 28 |
| 29 // A simple web page with print page size css. |
| 30 const char kHTMLWithPageSizeCss[] = |
| 31 "<html><head><style>" |
| 32 "@media print {" |
| 33 " @page {" |
| 34 " size: 4in 4in;" |
| 35 " }" |
| 36 "}" |
| 37 "</style></head>" |
| 38 "<body>Lorem Ipsum:" |
| 39 "</body></html>"; |
| 40 |
29 // A simple webpage that prints itself. | 41 // A simple webpage that prints itself. |
30 const char kPrintWithJSHTML[] = | 42 const char kPrintWithJSHTML[] = |
31 "<body>Hello<script>window.print()</script>World</body>"; | 43 "<body>Hello<script>window.print()</script>World</body>"; |
32 | 44 |
33 // A longer web page. | 45 // A longer web page. |
34 const char kLongPageHTML[] = | 46 const char kLongPageHTML[] = |
35 "<body><img src=\"\" width=10 height=10000 /></body>"; | 47 "<body><img src=\"\" width=10 height=10000 /></body>"; |
36 | 48 |
37 // A web page to simulate the print preview page. | 49 // A web page to simulate the print preview page. |
38 const char kPrintPreviewHTML[] = | 50 const char kPrintPreviewHTML[] = |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 " http-equiv=\"Content-Type\"" | 247 " http-equiv=\"Content-Type\"" |
236 " content=\"text/html; charset=utf-8\"/>" | 248 " content=\"text/html; charset=utf-8\"/>" |
237 "<title>Test 1</title>" | 249 "<title>Test 1</title>" |
238 "</head>" | 250 "</head>" |
239 "<body style=\"background-color: white;\">" | 251 "<body style=\"background-color: white;\">" |
240 "<p style=\"font-family: arial;\">Hello World!</p>" | 252 "<p style=\"font-family: arial;\">Hello World!</p>" |
241 "</body>", | 253 "</body>", |
242 #if defined(OS_MACOSX) | 254 #if defined(OS_MACOSX) |
243 // Mac printing code compensates for the WebKit scale factor while generating | 255 // Mac printing code compensates for the WebKit scale factor while generating |
244 // the metafile, so we expect smaller pages. | 256 // the metafile, so we expect smaller pages. |
245 1, 540, 720, | 257 1, 600, 780, |
246 #else | 258 #else |
247 1, 675, 900, | 259 1, 675, 900, |
248 #endif | 260 #endif |
249 NULL, | 261 NULL, |
250 NULL, | 262 NULL, |
251 }, | 263 }, |
252 }; | 264 }; |
253 } // namespace | 265 } // namespace |
254 | 266 |
255 // TODO(estade): need to port MockPrinter to get this on Linux. This involves | 267 // TODO(estade): need to port MockPrinter to get this on Linux. This involves |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 EXPECT_NE(0U, page_param.a.data_size); | 394 EXPECT_NE(0U, page_param.a.data_size); |
383 else | 395 else |
384 EXPECT_EQ(0U, page_param.a.data_size); | 396 EXPECT_EQ(0U, page_param.a.data_size); |
385 break; | 397 break; |
386 } | 398 } |
387 } | 399 } |
388 } | 400 } |
389 ASSERT_EQ(generate_draft_pages, msg_found); | 401 ASSERT_EQ(generate_draft_pages, msg_found); |
390 } | 402 } |
391 | 403 |
| 404 void VerifyDefaultPageLayout(int content_width, int content_height, |
| 405 int margin_top, int margin_bottom, |
| 406 int margin_left, int margin_right, |
| 407 bool page_has_print_css) { |
| 408 const IPC::Message* default_page_layout_msg = |
| 409 render_thread_->sink().GetUniqueMessageMatching( |
| 410 PrintHostMsg_DidGetDefaultPageLayout::ID); |
| 411 bool did_get_default_page_layout_msg = (NULL != default_page_layout_msg); |
| 412 if (did_get_default_page_layout_msg) { |
| 413 PrintHostMsg_DidGetDefaultPageLayout::Param param; |
| 414 PrintHostMsg_DidGetDefaultPageLayout::Read(default_page_layout_msg, |
| 415 ¶m); |
| 416 EXPECT_EQ(content_width, param.a.content_width); |
| 417 EXPECT_EQ(content_height, param.a.content_height); |
| 418 EXPECT_EQ(margin_top, param.a.margin_top); |
| 419 EXPECT_EQ(margin_right, param.a.margin_right); |
| 420 EXPECT_EQ(margin_left, param.a.margin_left); |
| 421 EXPECT_EQ(margin_bottom, param.a.margin_bottom); |
| 422 EXPECT_EQ(page_has_print_css, param.b); |
| 423 } |
| 424 } |
| 425 |
392 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); | 426 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); |
393 }; | 427 }; |
394 | 428 |
395 // Tests that print preview work and sending and receiving messages through | 429 // Tests that print preview work and sending and receiving messages through |
396 // that channel all works. | 430 // that channel all works. |
397 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { | 431 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { |
398 LoadHTML(kHelloWorldHTML); | 432 LoadHTML(kHelloWorldHTML); |
399 | 433 |
400 // Fill in some dummy values. | 434 // Fill in some dummy values. |
401 DictionaryValue dict; | 435 DictionaryValue dict; |
402 CreatePrintSettingsDictionary(&dict); | 436 CreatePrintSettingsDictionary(&dict); |
403 OnPrintPreview(dict); | 437 OnPrintPreview(dict); |
404 | 438 |
405 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); | 439 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 440 VerifyDefaultPageLayout(540, 720, 36, 36, 36, 36, false); |
406 VerifyPrintPreviewCancelled(false); | 441 VerifyPrintPreviewCancelled(false); |
407 VerifyPrintPreviewFailed(false); | 442 VerifyPrintPreviewFailed(false); |
408 VerifyPrintPreviewGenerated(true); | 443 VerifyPrintPreviewGenerated(true); |
409 VerifyPagesPrinted(false); | 444 VerifyPagesPrinted(false); |
410 } | 445 } |
411 | 446 |
| 447 TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewHTMLWithPageMarginsCss) { |
| 448 // A simple web page with print margins css. |
| 449 const char kHTMLWithPageMarginsCss[] = |
| 450 "<html><head><style>" |
| 451 "@media print {" |
| 452 " @page {" |
| 453 " margin: 3in 1in 2in 0.3in;" |
| 454 " }" |
| 455 "}" |
| 456 "</style></head>" |
| 457 "<body>Lorem Ipsum:" |
| 458 "</body></html>"; |
| 459 LoadHTML(kHTMLWithPageMarginsCss); |
| 460 |
| 461 // Fill in some dummy values. |
| 462 DictionaryValue dict; |
| 463 CreatePrintSettingsDictionary(&dict); |
| 464 dict.SetBoolean(printing::kSettingPrintToPDF, false); |
| 465 dict.SetInteger(printing::kSettingMarginsType, printing::DEFAULT_MARGINS); |
| 466 OnPrintPreview(dict); |
| 467 |
| 468 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 469 VerifyDefaultPageLayout(519, 432, 216, 144, 21, 72, false); |
| 470 VerifyPrintPreviewCancelled(false); |
| 471 VerifyPrintPreviewFailed(false); |
| 472 VerifyPrintPreviewGenerated(true); |
| 473 VerifyPagesPrinted(false); |
| 474 } |
| 475 |
| 476 // Test to verify that print preview ignores print media css when non-default |
| 477 // margin is selected. |
| 478 TEST_F(PrintWebViewHelperPreviewTest, NonDefaultMarginsSelectedIgnorePrintCss) { |
| 479 LoadHTML(kHTMLWithPageSizeCss); |
| 480 |
| 481 // Fill in some dummy values. |
| 482 DictionaryValue dict; |
| 483 CreatePrintSettingsDictionary(&dict); |
| 484 dict.SetBoolean(printing::kSettingPrintToPDF, false); |
| 485 dict.SetInteger(printing::kSettingMarginsType, printing::NO_MARGINS); |
| 486 OnPrintPreview(dict); |
| 487 |
| 488 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 489 VerifyDefaultPageLayout(612, 792, 0, 0, 0, 0, true); |
| 490 VerifyPrintPreviewCancelled(false); |
| 491 VerifyPrintPreviewFailed(false); |
| 492 VerifyPrintPreviewGenerated(true); |
| 493 VerifyPagesPrinted(false); |
| 494 } |
| 495 |
| 496 // Test to verify that print preview honor print media size css when |
| 497 // PRINT_TO_PDF is selected and doesn't fit to printer default paper size. |
| 498 TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorPrintCss) { |
| 499 LoadHTML(kHTMLWithPageSizeCss); |
| 500 |
| 501 // Fill in some dummy values. |
| 502 DictionaryValue dict; |
| 503 CreatePrintSettingsDictionary(&dict); |
| 504 dict.SetBoolean(printing::kSettingPrintToPDF, true); |
| 505 dict.SetInteger(printing::kSettingMarginsType, |
| 506 printing::PRINTABLE_AREA_MARGINS); |
| 507 OnPrintPreview(dict); |
| 508 |
| 509 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 510 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page |
| 511 // size. |
| 512 VerifyDefaultPageLayout(252, 252, 18, 18, 18, 18, true); |
| 513 VerifyPrintPreviewCancelled(false); |
| 514 VerifyPrintPreviewFailed(false); |
| 515 } |
| 516 |
| 517 // Test to verify that print preview honor print margin css when PRINT_TO_PDF |
| 518 // is selected and doesn't fit to printer default paper size. |
| 519 TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorPageMarginsCss) { |
| 520 // A simple web page with print margins css. |
| 521 const char kHTMLWithPageCss[] = |
| 522 "<html><head><style>" |
| 523 "@media print {" |
| 524 " @page {" |
| 525 " margin: 3in 1in 2in 0.3in;" |
| 526 " size: 14in 14in;" |
| 527 " }" |
| 528 "}" |
| 529 "</style></head>" |
| 530 "<body>Lorem Ipsum:" |
| 531 "</body></html>"; |
| 532 LoadHTML(kHTMLWithPageCss); |
| 533 |
| 534 // Fill in some dummy values. |
| 535 DictionaryValue dict; |
| 536 CreatePrintSettingsDictionary(&dict); |
| 537 dict.SetBoolean(printing::kSettingPrintToPDF, true); |
| 538 dict.SetInteger(printing::kSettingMarginsType, printing::DEFAULT_MARGINS); |
| 539 OnPrintPreview(dict); |
| 540 |
| 541 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 542 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page |
| 543 // size. |
| 544 VerifyDefaultPageLayout(915, 648, 216, 144, 21, 72, true); |
| 545 VerifyPrintPreviewCancelled(false); |
| 546 VerifyPrintPreviewFailed(false); |
| 547 } |
| 548 |
| 549 // Test to verify that print preview workflow center the html page contents to |
| 550 // fit the page size. |
| 551 TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewCenterToFitPage) { |
| 552 LoadHTML(kHTMLWithPageSizeCss); |
| 553 |
| 554 // Fill in some dummy values. |
| 555 DictionaryValue dict; |
| 556 CreatePrintSettingsDictionary(&dict); |
| 557 dict.SetBoolean(printing::kSettingPrintToPDF, false); |
| 558 dict.SetInteger(printing::kSettingMarginsType, printing::DEFAULT_MARGINS); |
| 559 OnPrintPreview(dict); |
| 560 |
| 561 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 562 VerifyDefaultPageLayout(288, 288, 252, 252, 162, 162, true); |
| 563 VerifyPrintPreviewCancelled(false); |
| 564 VerifyPrintPreviewFailed(false); |
| 565 VerifyPrintPreviewGenerated(true); |
| 566 } |
| 567 |
| 568 // Test to verify that print preview workflow scale the html page contents to |
| 569 // fit the page size. |
| 570 TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewShrinkToFitPage) { |
| 571 // A simple web page with print margins css. |
| 572 const char kHTMLWithPageCss[] = |
| 573 "<html><head><style>" |
| 574 "@media print {" |
| 575 " @page {" |
| 576 " size: 15in 17in;" |
| 577 " }" |
| 578 "}" |
| 579 "</style></head>" |
| 580 "<body>Lorem Ipsum:" |
| 581 "</body></html>"; |
| 582 LoadHTML(kHTMLWithPageCss); |
| 583 |
| 584 // Fill in some dummy values. |
| 585 DictionaryValue dict; |
| 586 CreatePrintSettingsDictionary(&dict); |
| 587 dict.SetBoolean(printing::kSettingPrintToPDF, false); |
| 588 dict.SetInteger(printing::kSettingMarginsType, printing::DEFAULT_MARGINS); |
| 589 OnPrintPreview(dict); |
| 590 |
| 591 EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining()); |
| 592 VerifyDefaultPageLayout(576, 652, 69, 71, 18, 18, true); |
| 593 VerifyPrintPreviewCancelled(false); |
| 594 VerifyPrintPreviewFailed(false); |
| 595 } |
| 596 |
412 // Test to verify that complete metafile is generated for a subset of pages | 597 // Test to verify that complete metafile is generated for a subset of pages |
413 // without creating draft pages. | 598 // without creating draft pages. |
414 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { | 599 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { |
415 LoadHTML(kHelloWorldHTML); | 600 LoadHTML(kHelloWorldHTML); |
416 | 601 |
417 // Fill in some dummy values. | 602 // Fill in some dummy values. |
418 DictionaryValue dict; | 603 DictionaryValue dict; |
419 CreatePrintSettingsDictionary(&dict); | 604 CreatePrintSettingsDictionary(&dict); |
420 | 605 |
421 // Set a page range and update the dictionary to generate only the complete | 606 // Set a page range and update the dictionary to generate only the complete |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 // Fill in some dummy values. | 721 // Fill in some dummy values. |
537 DictionaryValue dict; | 722 DictionaryValue dict; |
538 CreatePrintSettingsDictionary(&dict); | 723 CreatePrintSettingsDictionary(&dict); |
539 OnPrintForPrintPreview(dict); | 724 OnPrintForPrintPreview(dict); |
540 | 725 |
541 VerifyPrintFailed(true); | 726 VerifyPrintFailed(true); |
542 VerifyPagesPrinted(false); | 727 VerifyPagesPrinted(false); |
543 } | 728 } |
544 | 729 |
545 #endif // !defined(OS_CHROMEOS) | 730 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |