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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "chrome/common/chrome_switches.h"
8 #include "chrome/common/print_messages.h"
9 #include "chrome/renderer/printing/mock_printer.h"
10 #include "chrome/renderer/printing/print_web_view_helper.h"
11 #include "chrome/test/base/chrome_render_view_test.h"
12 #include "content/public/renderer/render_view.h"
13 #include "ipc/ipc_listener.h"
14 #include "printing/print_job_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "third_party/WebKit/public/web/WebRange.h"
19 #include "third_party/WebKit/public/web/WebView.h"
21 #if defined(OS_WIN) || defined(OS_MACOSX)
22 #include "base/file_util.h"
23 #include "printing/image.h"
25 using blink::WebFrame
;
26 using blink::WebLocalFrame
;
27 using blink::WebString
;
35 const char kHelloWorldHTML
[] = "<body><p>Hello World!</p></body>";
37 // A simple webpage with a button to print itself with.
38 const char kPrintOnUserAction
[] =
40 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>"
43 #if !defined(OS_CHROMEOS)
45 const char kMultipageHTML
[] =
47 ".break { page-break-after: always; }"
50 "<div class='break'>page1</div>"
51 "<div class='break'>page2</div>"
55 // A simple web page with print page size css.
56 const char kHTMLWithPageSizeCss
[] =
67 // A simple web page with print page layout css.
68 const char kHTMLWithLandscapePageCss
[] =
80 const char kLongPageHTML
[] =
81 "<body><img src=\"\" width=10 height=10000 /></body>";
83 // A web page to simulate the print preview page.
84 const char kPrintPreviewHTML
[] =
85 "<body><p id=\"pdf-viewer\">Hello World!</p></body>";
87 void CreatePrintSettingsDictionary(base::DictionaryValue
* dict
) {
88 dict
->SetBoolean(kSettingLandscape
, false);
89 dict
->SetBoolean(kSettingCollate
, false);
90 dict
->SetInteger(kSettingColor
, GRAY
);
91 dict
->SetBoolean(kSettingPrintToPDF
, true);
92 dict
->SetInteger(kSettingDuplexMode
, SIMPLEX
);
93 dict
->SetInteger(kSettingCopies
, 1);
94 dict
->SetString(kSettingDeviceName
, "dummy");
95 dict
->SetInteger(kPreviewUIID
, 4);
96 dict
->SetInteger(kPreviewRequestID
, 12345);
97 dict
->SetBoolean(kIsFirstRequest
, true);
98 dict
->SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
99 dict
->SetBoolean(kSettingPreviewModifiable
, false);
100 dict
->SetBoolean(kSettingHeaderFooterEnabled
, false);
101 dict
->SetBoolean(kSettingGenerateDraftData
, true);
102 dict
->SetBoolean(kSettingShouldPrintBackgrounds
, false);
103 dict
->SetBoolean(kSettingShouldPrintSelectionOnly
, false);
105 #endif // !defined(OS_CHROMEOS)
107 class DidPreviewPageListener
: public IPC::Listener
{
109 explicit DidPreviewPageListener(base::RunLoop
* run_loop
)
110 : run_loop_(run_loop
) {}
112 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
{
113 if (message
.type() == PrintHostMsg_MetafileReadyForPrinting::ID
||
114 message
.type() == PrintHostMsg_PrintPreviewFailed::ID
||
115 message
.type() == PrintHostMsg_PrintPreviewCancelled::ID
)
121 base::RunLoop
* const run_loop_
;
122 DISALLOW_COPY_AND_ASSIGN(DidPreviewPageListener
);
127 class PrintWebViewHelperTestBase
: public ChromeRenderViewTest
{
129 PrintWebViewHelperTestBase() {}
130 virtual ~PrintWebViewHelperTestBase() {}
133 void PrintWithJavaScript() {
134 ExecuteJavaScript("window.print();");
135 ProcessPendingMessages();
137 // The renderer should be done calculating the number of rendered pages
138 // according to the specified settings defined in the mock render thread.
139 // Verify the page count is correct.
140 void VerifyPageCount(int count
) {
141 #if defined(OS_CHROMEOS)
142 // The DidGetPrintedPagesCount message isn't sent on ChromeOS. Right now we
143 // always print all pages, and there are checks to that effect built into
146 const IPC::Message
* page_cnt_msg
=
147 render_thread_
->sink().GetUniqueMessageMatching(
148 PrintHostMsg_DidGetPrintedPagesCount::ID
);
149 ASSERT_TRUE(page_cnt_msg
);
150 PrintHostMsg_DidGetPrintedPagesCount::Param post_page_count_param
;
151 PrintHostMsg_DidGetPrintedPagesCount::Read(page_cnt_msg
,
152 &post_page_count_param
);
153 EXPECT_EQ(count
, post_page_count_param
.b
);
154 #endif // defined(OS_CHROMEOS)
157 // The renderer should be done calculating the number of rendered pages
158 // according to the specified settings defined in the mock render thread.
159 // Verify the page count is correct.
160 void VerifyPreviewPageCount(int count
) {
161 const IPC::Message
* page_cnt_msg
=
162 render_thread_
->sink().GetUniqueMessageMatching(
163 PrintHostMsg_DidGetPreviewPageCount::ID
);
164 ASSERT_TRUE(page_cnt_msg
);
165 PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param
;
166 PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg
,
167 &post_page_count_param
);
168 EXPECT_EQ(count
, post_page_count_param
.a
.page_count
);
171 // Verifies whether the pages printed or not.
172 void VerifyPagesPrinted(bool printed
) {
173 #if defined(OS_CHROMEOS)
174 bool did_print_msg
= (render_thread_
->sink().GetUniqueMessageMatching(
175 PrintHostMsg_TempFileForPrintingWritten::ID
) != NULL
);
176 ASSERT_EQ(printed
, did_print_msg
);
178 const IPC::Message
* print_msg
=
179 render_thread_
->sink().GetUniqueMessageMatching(
180 PrintHostMsg_DidPrintPage::ID
);
181 bool did_print_msg
= (NULL
!= print_msg
);
182 ASSERT_EQ(printed
, did_print_msg
);
184 PrintHostMsg_DidPrintPage::Param post_did_print_page_param
;
185 PrintHostMsg_DidPrintPage::Read(print_msg
, &post_did_print_page_param
);
186 EXPECT_EQ(0, post_did_print_page_param
.a
.page_number
);
188 #endif // defined(OS_CHROMEOS)
190 void OnPrintPages() {
191 PrintWebViewHelper::Get(view_
)->OnPrintPages();
192 ProcessPendingMessages();
195 void OnPrintPreview(const base::DictionaryValue
& dict
) {
196 PrintWebViewHelper
* print_web_view_helper
= PrintWebViewHelper::Get(view_
);
197 print_web_view_helper
->OnInitiatePrintPreview(false);
198 base::RunLoop run_loop
;
199 DidPreviewPageListener
filter(&run_loop
);
200 render_thread_
->sink().AddFilter(&filter
);
201 print_web_view_helper
->OnPrintPreview(dict
);
203 render_thread_
->sink().RemoveFilter(&filter
);
206 void OnPrintForPrintPreview(const base::DictionaryValue
& dict
) {
207 PrintWebViewHelper::Get(view_
)->OnPrintForPrintPreview(dict
);
208 ProcessPendingMessages();
211 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase
);
214 class PrintWebViewHelperTest
: public PrintWebViewHelperTestBase
{
216 PrintWebViewHelperTest() {}
217 virtual ~PrintWebViewHelperTest() {}
219 virtual void SetUp() OVERRIDE
{
220 ChromeRenderViewTest::SetUp();
224 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTest
);
227 // Tests that printing pages work and sending and receiving messages through
228 // that channel all works.
229 TEST_F(PrintWebViewHelperTest
, OnPrintPages
) {
230 LoadHTML(kHelloWorldHTML
);
234 VerifyPagesPrinted(true);
237 // Duplicate of OnPrintPagesTest only using javascript to print.
238 TEST_F(PrintWebViewHelperTest
, PrintWithJavascript
) {
239 PrintWithJavaScript();
242 VerifyPagesPrinted(true);
245 // Tests that the renderer blocks window.print() calls if they occur too
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);
256 // Pretend user will print. (but printing is blocked.)
257 chrome_render_thread_
->set_print_dialog_user_response(true);
258 PrintWithJavaScript();
259 VerifyPagesPrinted(false);
261 // Unblock script initiated printing and verify printing works.
262 PrintWebViewHelper::Get(view_
)->ResetScriptedPrintCount();
263 chrome_render_thread_
->printer()->ResetPrinter();
264 PrintWithJavaScript();
266 VerifyPagesPrinted(true);
269 // Tests that the renderer always allows window.print() calls if they are user
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);
280 // Pretend user will print. (but printing is blocked.)
281 chrome_render_thread_
->set_print_dialog_user_response(true);
282 PrintWithJavaScript();
283 VerifyPagesPrinted(false);
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);
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();
305 VerifyPagesPrinted(true);
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);
314 print_web_view_helper
->SetScriptedPrintBlocked(false);
315 PrintWithJavaScript();
317 VerifyPagesPrinted(true);
320 #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
322 // 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
325 TEST_F(PrintWebViewHelperTest
, PrintWithIframe
) {
326 // Document that populates an iframe.
328 "<html><body>Lorem Ipsum:"
329 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>"
330 " document.write(frames['sub1'].name);"
331 " frames['sub1'].document.write("
332 " '<p>Cras tempus ante eu felis semper luctus!</p>');"
333 "</script></body></html>";
337 // Find the frame and set it as the focused one. This should mean that that
338 // the printout should only contain the contents of that frame.
339 WebFrame
* sub1_frame
=
340 view_
->GetWebView()->findFrameByName(WebString::fromUTF8("sub1"));
341 ASSERT_TRUE(sub1_frame
);
342 view_
->GetWebView()->setFocusedFrame(sub1_frame
);
343 ASSERT_NE(view_
->GetWebView()->focusedFrame(),
344 view_
->GetWebView()->mainFrame());
346 // Initiate printing.
348 VerifyPagesPrinted(true);
350 // Verify output through MockPrinter.
351 const MockPrinter
* printer(chrome_render_thread_
->printer());
352 ASSERT_EQ(1, printer
->GetPrintedPages());
353 const Image
& image1(printer
->GetPrintedPage(0)->image());
355 // TODO(sverrir): Figure out a way to improve this test to actually print
356 // only the content of the iframe. Currently image1 will contain the full
358 EXPECT_NE(0, image1
.size().width());
359 EXPECT_NE(0, image1
.size().height());
363 // Tests if we can print a page and verify its results.
364 // This test prints HTML pages into a pseudo printer and check their outputs,
365 // i.e. a simplified version of the PrintingLayoutTextTest UI test.
367 // Test cases used in this test.
368 struct TestPageData
{
370 size_t printed_pages
;
373 const char* checksum
;
377 #if defined(OS_WIN) || defined(OS_MACOSX)
378 const TestPageData kTestPages
[] = {
382 " http-equiv=\"Content-Type\""
383 " content=\"text/html; charset=utf-8\"/>"
384 "<title>Test 1</title>"
386 "<body style=\"background-color: white;\">"
387 "<p style=\"font-family: arial;\">Hello World!</p>"
389 #if defined(OS_MACOSX)
390 // Mac printing code compensates for the WebKit scale factor while generating
391 // the metafile, so we expect smaller pages.
400 #endif // defined(OS_WIN) || defined(OS_MACOSX)
403 // TODO(estade): need to port MockPrinter to get this on Linux. This involves
404 // hooking up Cairo to read a pdf stream, or accessing the cairo surface in the
405 // metafile directly.
406 // Same for printing via PDF on Windows.
407 #if (defined(OS_WIN) && !WIN_PDF_METAFILE_FOR_PRINTING) || defined(OS_MACOSX)
408 TEST_F(PrintWebViewHelperTest
, PrintLayoutTest
) {
409 bool baseline
= false;
411 EXPECT_TRUE(chrome_render_thread_
->printer() != NULL
);
412 for (size_t i
= 0; i
< arraysize(kTestPages
); ++i
) {
413 // Load an HTML page and print it.
414 LoadHTML(kTestPages
[i
].page
);
416 VerifyPagesPrinted(true);
418 // MockRenderThread::Send() just calls MockRenderThread::OnReceived().
419 // So, all IPC messages sent in the above RenderView::OnPrintPages() call
420 // has been handled by the MockPrinter object, i.e. this printing job
421 // has been already finished.
422 // So, we can start checking the output pages of this printing job.
423 // Retrieve the number of pages actually printed.
424 size_t pages
= chrome_render_thread_
->printer()->GetPrintedPages();
425 EXPECT_EQ(kTestPages
[i
].printed_pages
, pages
);
427 // Retrieve the width and height of the output page.
428 int width
= chrome_render_thread_
->printer()->GetWidth(0);
429 int height
= chrome_render_thread_
->printer()->GetHeight(0);
431 // Check with margin for error. This has been failing with a one pixel
432 // offset on our buildbot.
433 const int kErrorMargin
= 5; // 5%
434 EXPECT_GT(kTestPages
[i
].width
* (100 + kErrorMargin
) / 100, width
);
435 EXPECT_LT(kTestPages
[i
].width
* (100 - kErrorMargin
) / 100, width
);
436 EXPECT_GT(kTestPages
[i
].height
* (100 + kErrorMargin
) / 100, height
);
437 EXPECT_LT(kTestPages
[i
].height
* (100 - kErrorMargin
) / 100, height
);
439 // Retrieve the checksum of the bitmap data from the pseudo printer and
440 // compare it with the expected result.
441 std::string bitmap_actual
;
443 chrome_render_thread_
->printer()->GetBitmapChecksum(0, &bitmap_actual
));
444 if (kTestPages
[i
].checksum
)
445 EXPECT_EQ(kTestPages
[i
].checksum
, bitmap_actual
);
448 // Save the source data and the bitmap data into temporary files to
449 // create base-line results.
450 base::FilePath source_path
;
451 base::CreateTemporaryFile(&source_path
);
452 chrome_render_thread_
->printer()->SaveSource(0, source_path
);
454 base::FilePath bitmap_path
;
455 base::CreateTemporaryFile(&bitmap_path
);
456 chrome_render_thread_
->printer()->SaveBitmap(0, bitmap_path
);
462 // These print preview tests do not work on Chrome OS yet.
463 #if !defined(OS_CHROMEOS)
464 class PrintWebViewHelperPreviewTest
: public PrintWebViewHelperTestBase
{
466 PrintWebViewHelperPreviewTest() {}
467 virtual ~PrintWebViewHelperPreviewTest() {}
469 virtual void SetUp() OVERRIDE
{
470 // Append the print preview switch before creating the PrintWebViewHelper.
471 CommandLine::ForCurrentProcess()->AppendSwitch(
472 switches::kRendererPrintPreview
);
474 ChromeRenderViewTest::SetUp();
478 void VerifyPrintPreviewCancelled(bool did_cancel
) {
479 bool print_preview_cancelled
=
480 (render_thread_
->sink().GetUniqueMessageMatching(
481 PrintHostMsg_PrintPreviewCancelled::ID
) != NULL
);
482 EXPECT_EQ(did_cancel
, print_preview_cancelled
);
485 void VerifyPrintPreviewFailed(bool did_fail
) {
486 bool print_preview_failed
=
487 (render_thread_
->sink().GetUniqueMessageMatching(
488 PrintHostMsg_PrintPreviewFailed::ID
) != NULL
);
489 EXPECT_EQ(did_fail
, print_preview_failed
);
492 void VerifyPrintPreviewGenerated(bool generated_preview
) {
493 const IPC::Message
* preview_msg
=
494 render_thread_
->sink().GetUniqueMessageMatching(
495 PrintHostMsg_MetafileReadyForPrinting::ID
);
496 bool did_get_preview_msg
= (NULL
!= preview_msg
);
497 ASSERT_EQ(generated_preview
, did_get_preview_msg
);
498 if (did_get_preview_msg
) {
499 PrintHostMsg_MetafileReadyForPrinting::Param preview_param
;
500 PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg
, &preview_param
);
501 EXPECT_NE(0, preview_param
.a
.document_cookie
);
502 EXPECT_NE(0, preview_param
.a
.expected_pages_count
);
503 EXPECT_NE(0U, preview_param
.a
.data_size
);
507 void VerifyPrintFailed(bool did_fail
) {
508 bool print_failed
= (render_thread_
->sink().GetUniqueMessageMatching(
509 PrintHostMsg_PrintingFailed::ID
) != NULL
);
510 EXPECT_EQ(did_fail
, print_failed
);
513 void VerifyPrintPreviewInvalidPrinterSettings(bool settings_invalid
) {
514 bool print_preview_invalid_printer_settings
=
515 (render_thread_
->sink().GetUniqueMessageMatching(
516 PrintHostMsg_PrintPreviewInvalidPrinterSettings::ID
) != NULL
);
517 EXPECT_EQ(settings_invalid
, print_preview_invalid_printer_settings
);
520 // |page_number| is 0-based.
521 void VerifyDidPreviewPage(bool generate_draft_pages
, int page_number
) {
522 bool msg_found
= false;
523 size_t msg_count
= render_thread_
->sink().message_count();
524 for (size_t i
= 0; i
< msg_count
; ++i
) {
525 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
526 if (msg
->type() == PrintHostMsg_DidPreviewPage::ID
) {
527 PrintHostMsg_DidPreviewPage::Param page_param
;
528 PrintHostMsg_DidPreviewPage::Read(msg
, &page_param
);
529 if (page_param
.a
.page_number
== page_number
) {
531 if (generate_draft_pages
)
532 EXPECT_NE(0U, page_param
.a
.data_size
);
534 EXPECT_EQ(0U, page_param
.a
.data_size
);
539 ASSERT_EQ(generate_draft_pages
, msg_found
);
542 void VerifyDefaultPageLayout(int content_width
, int content_height
,
543 int margin_top
, int margin_bottom
,
544 int margin_left
, int margin_right
,
545 bool page_has_print_css
) {
546 const IPC::Message
* default_page_layout_msg
=
547 render_thread_
->sink().GetUniqueMessageMatching(
548 PrintHostMsg_DidGetDefaultPageLayout::ID
);
549 bool did_get_default_page_layout_msg
= (NULL
!= default_page_layout_msg
);
550 if (did_get_default_page_layout_msg
) {
551 PrintHostMsg_DidGetDefaultPageLayout::Param param
;
552 PrintHostMsg_DidGetDefaultPageLayout::Read(default_page_layout_msg
,
554 EXPECT_EQ(content_width
, param
.a
.content_width
);
555 EXPECT_EQ(content_height
, param
.a
.content_height
);
556 EXPECT_EQ(margin_top
, param
.a
.margin_top
);
557 EXPECT_EQ(margin_right
, param
.a
.margin_right
);
558 EXPECT_EQ(margin_left
, param
.a
.margin_left
);
559 EXPECT_EQ(margin_bottom
, param
.a
.margin_bottom
);
560 EXPECT_EQ(page_has_print_css
, param
.c
);
564 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest
);
567 // Tests that print preview work and sending and receiving messages through
568 // that channel all works.
569 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintPreview
) {
570 LoadHTML(kHelloWorldHTML
);
572 // Fill in some dummy values.
573 base::DictionaryValue dict
;
574 CreatePrintSettingsDictionary(&dict
);
575 OnPrintPreview(dict
);
577 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
578 VerifyDefaultPageLayout(540, 720, 36, 36, 36, 36, false);
579 VerifyPrintPreviewCancelled(false);
580 VerifyPrintPreviewFailed(false);
581 VerifyPrintPreviewGenerated(true);
582 VerifyPagesPrinted(false);
585 TEST_F(PrintWebViewHelperPreviewTest
, PrintPreviewHTMLWithPageMarginsCss
) {
586 // A simple web page with print margins css.
587 const char kHTMLWithPageMarginsCss
[] =
588 "<html><head><style>"
591 " margin: 3in 1in 2in 0.3in;"
597 LoadHTML(kHTMLWithPageMarginsCss
);
599 // Fill in some dummy values.
600 base::DictionaryValue dict
;
601 CreatePrintSettingsDictionary(&dict
);
602 dict
.SetBoolean(kSettingPrintToPDF
, false);
603 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
604 OnPrintPreview(dict
);
606 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
607 VerifyDefaultPageLayout(519, 432, 216, 144, 21, 72, false);
608 VerifyPrintPreviewCancelled(false);
609 VerifyPrintPreviewFailed(false);
610 VerifyPrintPreviewGenerated(true);
611 VerifyPagesPrinted(false);
614 // Test to verify that print preview ignores print media css when non-default
615 // margin is selected.
616 TEST_F(PrintWebViewHelperPreviewTest
, NonDefaultMarginsSelectedIgnorePrintCss
) {
617 LoadHTML(kHTMLWithPageSizeCss
);
619 // Fill in some dummy values.
620 base::DictionaryValue dict
;
621 CreatePrintSettingsDictionary(&dict
);
622 dict
.SetBoolean(kSettingPrintToPDF
, false);
623 dict
.SetInteger(kSettingMarginsType
, NO_MARGINS
);
624 OnPrintPreview(dict
);
626 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
627 VerifyDefaultPageLayout(612, 792, 0, 0, 0, 0, true);
628 VerifyPrintPreviewCancelled(false);
629 VerifyPrintPreviewFailed(false);
630 VerifyPrintPreviewGenerated(true);
631 VerifyPagesPrinted(false);
634 // Test to verify that print preview honor print media size css when
635 // PRINT_TO_PDF is selected and doesn't fit to printer default paper size.
636 TEST_F(PrintWebViewHelperPreviewTest
, PrintToPDFSelectedHonorPrintCss
) {
637 LoadHTML(kHTMLWithPageSizeCss
);
639 // Fill in some dummy values.
640 base::DictionaryValue dict
;
641 CreatePrintSettingsDictionary(&dict
);
642 dict
.SetBoolean(kSettingPrintToPDF
, true);
643 dict
.SetInteger(kSettingMarginsType
,
644 PRINTABLE_AREA_MARGINS
);
645 OnPrintPreview(dict
);
647 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
648 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
650 VerifyDefaultPageLayout(252, 252, 18, 18, 18, 18, true);
651 VerifyPrintPreviewCancelled(false);
652 VerifyPrintPreviewFailed(false);
655 // Test to verify that print preview honor print margin css when PRINT_TO_PDF
656 // is selected and doesn't fit to printer default paper size.
657 TEST_F(PrintWebViewHelperPreviewTest
, PrintToPDFSelectedHonorPageMarginsCss
) {
658 // A simple web page with print margins css.
659 const char kHTMLWithPageCss
[] =
660 "<html><head><style>"
663 " margin: 3in 1in 2in 0.3in;"
670 LoadHTML(kHTMLWithPageCss
);
672 // Fill in some dummy values.
673 base::DictionaryValue dict
;
674 CreatePrintSettingsDictionary(&dict
);
675 dict
.SetBoolean(kSettingPrintToPDF
, true);
676 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
677 OnPrintPreview(dict
);
679 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
680 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
682 VerifyDefaultPageLayout(915, 648, 216, 144, 21, 72, true);
683 VerifyPrintPreviewCancelled(false);
684 VerifyPrintPreviewFailed(false);
687 // Test to verify that print preview workflow center the html page contents to
688 // fit the page size.
689 TEST_F(PrintWebViewHelperPreviewTest
, PrintPreviewCenterToFitPage
) {
690 LoadHTML(kHTMLWithPageSizeCss
);
692 // Fill in some dummy values.
693 base::DictionaryValue dict
;
694 CreatePrintSettingsDictionary(&dict
);
695 dict
.SetBoolean(kSettingPrintToPDF
, false);
696 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
697 OnPrintPreview(dict
);
699 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
700 VerifyDefaultPageLayout(216, 216, 288, 288, 198, 198, true);
701 VerifyPrintPreviewCancelled(false);
702 VerifyPrintPreviewFailed(false);
703 VerifyPrintPreviewGenerated(true);
706 // Test to verify that print preview workflow scale the html page contents to
707 // fit the page size.
708 TEST_F(PrintWebViewHelperPreviewTest
, PrintPreviewShrinkToFitPage
) {
709 // A simple web page with print margins css.
710 const char kHTMLWithPageCss
[] =
711 "<html><head><style>"
720 LoadHTML(kHTMLWithPageCss
);
722 // Fill in some dummy values.
723 base::DictionaryValue dict
;
724 CreatePrintSettingsDictionary(&dict
);
725 dict
.SetBoolean(kSettingPrintToPDF
, false);
726 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
727 OnPrintPreview(dict
);
729 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
730 VerifyDefaultPageLayout(571, 652, 69, 71, 20, 21, true);
731 VerifyPrintPreviewCancelled(false);
732 VerifyPrintPreviewFailed(false);
735 // Test to verify that print preview workflow honor the orientation settings
737 TEST_F(PrintWebViewHelperPreviewTest
, PrintPreviewHonorsOrientationCss
) {
738 LoadHTML(kHTMLWithLandscapePageCss
);
740 // Fill in some dummy values.
741 base::DictionaryValue dict
;
742 CreatePrintSettingsDictionary(&dict
);
743 dict
.SetBoolean(kSettingPrintToPDF
, false);
744 dict
.SetInteger(kSettingMarginsType
, NO_MARGINS
);
745 OnPrintPreview(dict
);
747 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
748 VerifyDefaultPageLayout(792, 612, 0, 0, 0, 0, true);
749 VerifyPrintPreviewCancelled(false);
750 VerifyPrintPreviewFailed(false);
753 // Test to verify that print preview workflow honors the orientation settings
754 // specified in css when PRINT_TO_PDF is selected.
755 TEST_F(PrintWebViewHelperPreviewTest
, PrintToPDFSelectedHonorOrientationCss
) {
756 LoadHTML(kHTMLWithLandscapePageCss
);
758 // Fill in some dummy values.
759 base::DictionaryValue dict
;
760 CreatePrintSettingsDictionary(&dict
);
761 dict
.SetBoolean(kSettingPrintToPDF
, true);
762 dict
.SetInteger(kSettingMarginsType
, CUSTOM_MARGINS
);
763 OnPrintPreview(dict
);
765 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
766 VerifyDefaultPageLayout(748, 568, 21, 23, 21, 23, true);
767 VerifyPrintPreviewCancelled(false);
768 VerifyPrintPreviewFailed(false);
771 // Test to verify that complete metafile is generated for a subset of pages
772 // without creating draft pages.
773 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintPreviewForSelectedPages
) {
774 LoadHTML(kMultipageHTML
);
776 // Fill in some dummy values.
777 base::DictionaryValue dict
;
778 CreatePrintSettingsDictionary(&dict
);
780 // Set a page range and update the dictionary to generate only the complete
781 // metafile with the selected pages. Page numbers used in the dictionary
783 base::DictionaryValue
* page_range
= new base::DictionaryValue();
784 page_range
->SetInteger(kSettingPageRangeFrom
, 2);
785 page_range
->SetInteger(kSettingPageRangeTo
, 3);
787 base::ListValue
* page_range_array
= new base::ListValue();
788 page_range_array
->Append(page_range
);
790 dict
.Set(kSettingPageRange
, page_range_array
);
791 dict
.SetBoolean(kSettingGenerateDraftData
, false);
793 OnPrintPreview(dict
);
795 VerifyDidPreviewPage(false, 0);
796 VerifyDidPreviewPage(false, 1);
797 VerifyDidPreviewPage(false, 2);
798 VerifyPreviewPageCount(3);
799 VerifyPrintPreviewCancelled(false);
800 VerifyPrintPreviewFailed(false);
801 VerifyPrintPreviewGenerated(true);
802 VerifyPagesPrinted(false);
805 // Test to verify that preview generated only for one page.
806 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintPreviewForSelectedText
) {
807 LoadHTML(kMultipageHTML
);
808 GetMainFrame()->selectRange(
809 blink::WebRange::fromDocumentRange(GetMainFrame(), 1, 3));
811 // Fill in some dummy values.
812 base::DictionaryValue dict
;
813 CreatePrintSettingsDictionary(&dict
);
814 dict
.SetBoolean(kSettingShouldPrintSelectionOnly
, true);
816 OnPrintPreview(dict
);
818 VerifyPreviewPageCount(1);
819 VerifyPrintPreviewCancelled(false);
820 VerifyPrintPreviewFailed(false);
821 VerifyPrintPreviewGenerated(true);
822 VerifyPagesPrinted(false);
825 // Tests that print preview fails and receiving error messages through
826 // that channel all works.
827 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintPreviewFail
) {
828 LoadHTML(kHelloWorldHTML
);
830 // An empty dictionary should fail.
831 base::DictionaryValue empty_dict
;
832 OnPrintPreview(empty_dict
);
834 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
835 VerifyPrintPreviewCancelled(false);
836 VerifyPrintPreviewFailed(true);
837 VerifyPrintPreviewGenerated(false);
838 VerifyPagesPrinted(false);
841 // Tests that cancelling print preview works.
842 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintPreviewCancel
) {
843 LoadHTML(kLongPageHTML
);
845 const int kCancelPage
= 3;
846 chrome_render_thread_
->set_print_preview_cancel_page_number(kCancelPage
);
847 // Fill in some dummy values.
848 base::DictionaryValue dict
;
849 CreatePrintSettingsDictionary(&dict
);
850 OnPrintPreview(dict
);
852 EXPECT_EQ(kCancelPage
,
853 chrome_render_thread_
->print_preview_pages_remaining());
854 VerifyPrintPreviewCancelled(true);
855 VerifyPrintPreviewFailed(false);
856 VerifyPrintPreviewGenerated(false);
857 VerifyPagesPrinted(false);
860 // Tests that printing from print preview works and sending and receiving
861 // messages through that channel all works.
862 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintForPrintPreview
) {
863 LoadHTML(kPrintPreviewHTML
);
865 // Fill in some dummy values.
866 base::DictionaryValue dict
;
867 CreatePrintSettingsDictionary(&dict
);
868 OnPrintForPrintPreview(dict
);
870 VerifyPrintFailed(false);
871 VerifyPagesPrinted(true);
874 // Tests that printing from print preview fails and receiving error messages
875 // through that channel all works.
876 TEST_F(PrintWebViewHelperPreviewTest
, OnPrintForPrintPreviewFail
) {
877 LoadHTML(kPrintPreviewHTML
);
879 // An empty dictionary should fail.
880 base::DictionaryValue empty_dict
;
881 OnPrintForPrintPreview(empty_dict
);
883 VerifyPagesPrinted(false);
886 // Tests that when default printer has invalid printer settings, print preview
887 // receives error message.
888 TEST_F(PrintWebViewHelperPreviewTest
,
889 OnPrintPreviewUsingInvalidPrinterSettings
) {
890 LoadHTML(kPrintPreviewHTML
);
892 // Set mock printer to provide invalid settings.
893 chrome_render_thread_
->printer()->UseInvalidSettings();
895 // Fill in some dummy values.
896 base::DictionaryValue dict
;
897 CreatePrintSettingsDictionary(&dict
);
898 OnPrintPreview(dict
);
900 // We should have received invalid printer settings from |printer_|.
901 VerifyPrintPreviewInvalidPrinterSettings(true);
902 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
904 // It should receive the invalid printer settings message only.
905 VerifyPrintPreviewFailed(false);
906 VerifyPrintPreviewGenerated(false);
909 // Tests that when the selected printer has invalid page settings, print preview
910 // receives error message.
911 TEST_F(PrintWebViewHelperPreviewTest
,
912 OnPrintPreviewUsingInvalidPageSize
) {
913 LoadHTML(kPrintPreviewHTML
);
915 chrome_render_thread_
->printer()->UseInvalidPageSize();
917 base::DictionaryValue dict
;
918 CreatePrintSettingsDictionary(&dict
);
919 OnPrintPreview(dict
);
921 VerifyPrintPreviewInvalidPrinterSettings(true);
922 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
924 // It should receive the invalid printer settings message only.
925 VerifyPrintPreviewFailed(false);
926 VerifyPrintPreviewGenerated(false);
929 // Tests that when the selected printer has invalid content settings, print
930 // preview receives error message.
931 TEST_F(PrintWebViewHelperPreviewTest
,
932 OnPrintPreviewUsingInvalidContentSize
) {
933 LoadHTML(kPrintPreviewHTML
);
935 chrome_render_thread_
->printer()->UseInvalidContentSize();
937 base::DictionaryValue dict
;
938 CreatePrintSettingsDictionary(&dict
);
939 OnPrintPreview(dict
);
941 VerifyPrintPreviewInvalidPrinterSettings(true);
942 EXPECT_EQ(0, chrome_render_thread_
->print_preview_pages_remaining());
944 // It should receive the invalid printer settings message only.
945 VerifyPrintPreviewFailed(false);
946 VerifyPrintPreviewGenerated(false);
949 TEST_F(PrintWebViewHelperPreviewTest
,
950 OnPrintForPrintPreviewUsingInvalidPrinterSettings
) {
951 LoadHTML(kPrintPreviewHTML
);
953 // Set mock printer to provide invalid settings.
954 chrome_render_thread_
->printer()->UseInvalidSettings();
956 // Fill in some dummy values.
957 base::DictionaryValue dict
;
958 CreatePrintSettingsDictionary(&dict
);
959 OnPrintForPrintPreview(dict
);
961 VerifyPrintFailed(true);
962 VerifyPagesPrinted(false);
965 #endif // !defined(OS_CHROMEOS)
967 class PrintWebViewHelperKioskTest
: public PrintWebViewHelperTestBase
{
969 PrintWebViewHelperKioskTest() {}
970 virtual ~PrintWebViewHelperKioskTest() {}
972 virtual void SetUp() OVERRIDE
{
973 // Append the throttling disable switch before creating the
974 // PrintWebViewHelper.
975 CommandLine::ForCurrentProcess()->AppendSwitch(
976 switches::kDisableScriptedPrintThrottling
);
978 ChromeRenderViewTest::SetUp();
982 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperKioskTest
);
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);
1000 // Pretend user will print, should not be throttled.
1001 chrome_render_thread_
->set_print_dialog_user_response(true);
1002 PrintWithJavaScript();
1003 VerifyPagesPrinted(true);
1006 } // namespace printing