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 "components/printing/common/print_messages.h"
8 #include "components/printing/renderer/print_web_view_helper.h"
9 #include "components/printing/test/mock_printer.h"
10 #include "components/printing/test/print_mock_render_thread.h"
11 #include "components/printing/test/print_test_content_renderer_client.h"
12 #include "content/public/renderer/render_view.h"
13 #include "content/public/test/render_view_test.h"
14 #include "ipc/ipc_listener.h"
15 #include "printing/print_job_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/platform/WebString.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebRange.h"
20 #include "third_party/WebKit/public/web/WebView.h"
22 #if defined(OS_WIN) || defined(OS_MACOSX)
23 #include "base/files/file_util.h"
24 #include "printing/image.h"
26 using blink::WebFrame
;
27 using blink::WebLocalFrame
;
28 using blink::WebString
;
35 #if !defined(OS_CHROMEOS)
38 const char kHelloWorldHTML
[] = "<body><p>Hello World!</p></body>";
40 // A simple webpage with a button to print itself with.
41 const char kPrintOnUserAction
[] =
43 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>"
47 const char kMultipageHTML
[] =
49 ".break { page-break-after: always; }"
52 "<div class='break'>page1</div>"
53 "<div class='break'>page2</div>"
57 // A simple web page with print page size css.
58 const char kHTMLWithPageSizeCss
[] =
69 // A simple web page with print page layout css.
70 const char kHTMLWithLandscapePageCss
[] =
82 const char kLongPageHTML
[] =
83 "<body><img src=\"\" width=10 height=10000 /></body>";
85 // A web page to simulate the print preview page.
86 const char kPrintPreviewHTML
[] =
87 "<body><p id=\"pdf-viewer\">Hello World!</p></body>";
89 void CreatePrintSettingsDictionary(base::DictionaryValue
* dict
) {
90 dict
->SetBoolean(kSettingLandscape
, false);
91 dict
->SetBoolean(kSettingCollate
, false);
92 dict
->SetInteger(kSettingColor
, GRAY
);
93 dict
->SetBoolean(kSettingPrintToPDF
, true);
94 dict
->SetInteger(kSettingDuplexMode
, SIMPLEX
);
95 dict
->SetInteger(kSettingCopies
, 1);
96 dict
->SetString(kSettingDeviceName
, "dummy");
97 dict
->SetInteger(kPreviewUIID
, 4);
98 dict
->SetInteger(kPreviewRequestID
, 12345);
99 dict
->SetBoolean(kIsFirstRequest
, true);
100 dict
->SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
101 dict
->SetBoolean(kSettingPreviewModifiable
, false);
102 dict
->SetBoolean(kSettingHeaderFooterEnabled
, false);
103 dict
->SetBoolean(kSettingGenerateDraftData
, true);
104 dict
->SetBoolean(kSettingShouldPrintBackgrounds
, false);
105 dict
->SetBoolean(kSettingShouldPrintSelectionOnly
, false);
107 #endif // !defined(OS_CHROMEOS)
109 class DidPreviewPageListener
: public IPC::Listener
{
111 explicit DidPreviewPageListener(base::RunLoop
* run_loop
)
112 : run_loop_(run_loop
) {}
114 bool OnMessageReceived(const IPC::Message
& message
) override
{
115 if (message
.type() == PrintHostMsg_MetafileReadyForPrinting::ID
||
116 message
.type() == PrintHostMsg_PrintPreviewFailed::ID
||
117 message
.type() == PrintHostMsg_PrintPreviewCancelled::ID
)
123 base::RunLoop
* const run_loop_
;
124 DISALLOW_COPY_AND_ASSIGN(DidPreviewPageListener
);
129 class PrintWebViewHelperTestBase
: public content::RenderViewTest
{
131 PrintWebViewHelperTestBase() : print_render_thread_(NULL
) {}
132 ~PrintWebViewHelperTestBase() override
{}
135 void SetUp() override
{
136 print_render_thread_
= new PrintMockRenderThread();
137 render_thread_
.reset(print_render_thread_
);
139 content::RenderViewTest::SetUp();
142 content::ContentRendererClient
* CreateContentRendererClient() override
{
143 return new PrintTestContentRendererClient();
146 void TearDown() override
{
147 #if defined(LEAK_SANITIZER)
148 // Do this before shutting down V8 in RenderViewTest::TearDown().
149 // http://crbug.com/328552
150 __lsan_do_leak_check();
152 content::RenderViewTest::TearDown();
155 void PrintWithJavaScript() {
156 ExecuteJavaScriptForTests("window.print();");
157 ProcessPendingMessages();
159 // The renderer should be done calculating the number of rendered pages
160 // according to the specified settings defined in the mock render thread.
161 // Verify the page count is correct.
162 void VerifyPageCount(int count
) {
163 #if defined(OS_CHROMEOS)
164 // The DidGetPrintedPagesCount message isn't sent on ChromeOS. Right now we
165 // always print all pages, and there are checks to that effect built into
168 const IPC::Message
* page_cnt_msg
=
169 render_thread_
->sink().GetUniqueMessageMatching(
170 PrintHostMsg_DidGetPrintedPagesCount::ID
);
171 ASSERT_TRUE(page_cnt_msg
);
172 PrintHostMsg_DidGetPrintedPagesCount::Param post_page_count_param
;
173 PrintHostMsg_DidGetPrintedPagesCount::Read(page_cnt_msg
,
174 &post_page_count_param
);
175 EXPECT_EQ(count
, base::get
<1>(post_page_count_param
));
176 #endif // defined(OS_CHROMEOS)
179 // The renderer should be done calculating the number of rendered pages
180 // according to the specified settings defined in the mock render thread.
181 // Verify the page count is correct.
182 void VerifyPreviewPageCount(int count
) {
183 const IPC::Message
* page_cnt_msg
=
184 render_thread_
->sink().GetUniqueMessageMatching(
185 PrintHostMsg_DidGetPreviewPageCount::ID
);
186 ASSERT_TRUE(page_cnt_msg
);
187 PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param
;
188 PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg
,
189 &post_page_count_param
);
190 EXPECT_EQ(count
, base::get
<0>(post_page_count_param
).page_count
);
193 // Verifies whether the pages printed or not.
194 void VerifyPagesPrinted(bool printed
) {
195 const IPC::Message
* print_msg
=
196 render_thread_
->sink().GetUniqueMessageMatching(
197 PrintHostMsg_DidPrintPage::ID
);
198 bool did_print_msg
= (NULL
!= print_msg
);
199 ASSERT_EQ(printed
, did_print_msg
);
201 PrintHostMsg_DidPrintPage::Param post_did_print_page_param
;
202 PrintHostMsg_DidPrintPage::Read(print_msg
, &post_did_print_page_param
);
203 EXPECT_EQ(0, base::get
<0>(post_did_print_page_param
).page_number
);
207 #if defined(ENABLE_BASIC_PRINTING)
208 void OnPrintPages() {
209 PrintWebViewHelper::Get(view_
)->OnPrintPages();
210 ProcessPendingMessages();
212 #endif // ENABLE_BASIC_PRINTING
214 void VerifyPreviewRequest(bool requested
) {
215 const IPC::Message
* print_msg
=
216 render_thread_
->sink().GetUniqueMessageMatching(
217 PrintHostMsg_SetupScriptedPrintPreview::ID
);
218 bool did_print_msg
= (NULL
!= print_msg
);
219 ASSERT_EQ(requested
, did_print_msg
);
222 void OnPrintPreview(const base::DictionaryValue
& dict
) {
223 PrintWebViewHelper
* print_web_view_helper
= PrintWebViewHelper::Get(view_
);
224 print_web_view_helper
->OnInitiatePrintPreview(false);
225 base::RunLoop run_loop
;
226 DidPreviewPageListener
filter(&run_loop
);
227 render_thread_
->sink().AddFilter(&filter
);
228 print_web_view_helper
->OnPrintPreview(dict
);
230 render_thread_
->sink().RemoveFilter(&filter
);
233 void OnPrintForPrintPreview(const base::DictionaryValue
& dict
) {
234 PrintWebViewHelper::Get(view_
)->OnPrintForPrintPreview(dict
);
235 ProcessPendingMessages();
238 // Naked pointer as ownership is with content::RenderViewTest::render_thread_.
239 PrintMockRenderThread
* print_render_thread_
;
241 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase
);
244 // RenderViewTest-based tests crash on Android
245 // http://crbug.com/187500
246 #if defined(OS_ANDROID)
247 #define MAYBE_PrintWebViewHelperTest DISABLED_PrintWebViewHelperTest
249 #define MAYBE_PrintWebViewHelperTest PrintWebViewHelperTest
250 #endif // defined(OS_ANDROID)
252 class MAYBE_PrintWebViewHelperTest
: public PrintWebViewHelperTestBase
{
254 MAYBE_PrintWebViewHelperTest() {}
255 ~MAYBE_PrintWebViewHelperTest() override
{}
257 void SetUp() override
{ PrintWebViewHelperTestBase::SetUp(); }
260 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperTest
);
263 // This tests only for platforms without print preview.
264 #if !defined(ENABLE_PRINT_PREVIEW)
265 // Tests that the renderer blocks window.print() calls if they occur too
267 TEST_F(MAYBE_PrintWebViewHelperTest
, BlockScriptInitiatedPrinting
) {
268 // Pretend user will cancel printing.
269 print_render_thread_
->set_print_dialog_user_response(false);
270 // Try to print with window.print() a few times.
271 PrintWithJavaScript();
272 PrintWithJavaScript();
273 PrintWithJavaScript();
274 VerifyPagesPrinted(false);
276 // Pretend user will print. (but printing is blocked.)
277 print_render_thread_
->set_print_dialog_user_response(true);
278 PrintWithJavaScript();
279 VerifyPagesPrinted(false);
281 // Unblock script initiated printing and verify printing works.
282 PrintWebViewHelper::Get(view_
)->scripting_throttler_
.Reset();
283 print_render_thread_
->printer()->ResetPrinter();
284 PrintWithJavaScript();
286 VerifyPagesPrinted(true);
289 // Tests that the renderer always allows window.print() calls if they are user
291 TEST_F(MAYBE_PrintWebViewHelperTest
, AllowUserOriginatedPrinting
) {
292 // Pretend user will cancel printing.
293 print_render_thread_
->set_print_dialog_user_response(false);
294 // Try to print with window.print() a few times.
295 PrintWithJavaScript();
296 PrintWithJavaScript();
297 PrintWithJavaScript();
298 VerifyPagesPrinted(false);
300 // Pretend user will print. (but printing is blocked.)
301 print_render_thread_
->set_print_dialog_user_response(true);
302 PrintWithJavaScript();
303 VerifyPagesPrinted(false);
305 // Try again as if user initiated, without resetting the print count.
306 print_render_thread_
->printer()->ResetPrinter();
307 LoadHTML(kPrintOnUserAction
);
308 gfx::Size
new_size(200, 100);
309 Resize(new_size
, gfx::Rect(), false);
311 gfx::Rect bounds
= GetElementBounds("print");
312 EXPECT_FALSE(bounds
.IsEmpty());
313 blink::WebMouseEvent mouse_event
;
314 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
315 mouse_event
.button
= blink::WebMouseEvent::ButtonLeft
;
316 mouse_event
.x
= bounds
.CenterPoint().x();
317 mouse_event
.y
= bounds
.CenterPoint().y();
318 mouse_event
.clickCount
= 1;
319 SendWebMouseEvent(mouse_event
);
320 mouse_event
.type
= blink::WebInputEvent::MouseUp
;
321 SendWebMouseEvent(mouse_event
);
322 ProcessPendingMessages();
325 VerifyPagesPrinted(true);
328 // Duplicate of OnPrintPagesTest only using javascript to print.
329 TEST_F(MAYBE_PrintWebViewHelperTest
, PrintWithJavascript
) {
330 PrintWithJavaScript();
333 VerifyPagesPrinted(true);
335 #endif // !ENABLE_PRINT_PREVIEW
337 #if defined(ENABLE_BASIC_PRINTING)
338 // Tests that printing pages work and sending and receiving messages through
339 // that channel all works.
340 TEST_F(MAYBE_PrintWebViewHelperTest
, OnPrintPages
) {
341 LoadHTML(kHelloWorldHTML
);
345 VerifyPagesPrinted(true);
347 #endif // ENABLE_BASIC_PRINTING
349 #if defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
350 // TODO(estade): I don't think this test is worth porting to Linux. We will have
351 // to rip out and replace most of the IPC code if we ever plan to improve
352 // printing, and the comment below by sverrir suggests that it doesn't do much
354 TEST_F(MAYBE_PrintWebViewHelperTest
, PrintWithIframe
) {
355 // Document that populates an iframe.
357 "<html><body>Lorem Ipsum:"
358 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>"
359 " document.write(frames['sub1'].name);"
360 " frames['sub1'].document.write("
361 " '<p>Cras tempus ante eu felis semper luctus!</p>');"
362 " frames['sub1'].document.close();"
363 "</script></body></html>";
367 // Find the frame and set it as the focused one. This should mean that that
368 // the printout should only contain the contents of that frame.
369 WebFrame
* sub1_frame
=
370 view_
->GetWebView()->findFrameByName(WebString::fromUTF8("sub1"));
371 ASSERT_TRUE(sub1_frame
);
372 view_
->GetWebView()->setFocusedFrame(sub1_frame
);
373 ASSERT_NE(view_
->GetWebView()->focusedFrame(),
374 view_
->GetWebView()->mainFrame());
376 // Initiate printing.
378 VerifyPagesPrinted(true);
380 // Verify output through MockPrinter.
381 const MockPrinter
* printer(print_render_thread_
->printer());
382 ASSERT_EQ(1, printer
->GetPrintedPages());
383 const Image
& image1(printer
->GetPrintedPage(0)->image());
385 // TODO(sverrir): Figure out a way to improve this test to actually print
386 // only the content of the iframe. Currently image1 will contain the full
388 EXPECT_NE(0, image1
.size().width());
389 EXPECT_NE(0, image1
.size().height());
391 #endif // OS_MACOSX && ENABLE_BASIC_PRINTING
393 // Tests if we can print a page and verify its results.
394 // This test prints HTML pages into a pseudo printer and check their outputs,
395 // i.e. a simplified version of the PrintingLayoutTextTest UI test.
397 // Test cases used in this test.
398 struct TestPageData
{
400 size_t printed_pages
;
403 const char* checksum
;
407 #if defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
408 const TestPageData kTestPages
[] = {
413 " http-equiv=\"Content-Type\""
414 " content=\"text/html; charset=utf-8\"/>"
415 "<title>Test 1</title>"
417 "<body style=\"background-color: white;\">"
418 "<p style=\"font-family: arial;\">Hello World!</p>"
421 // Mac printing code compensates for the WebKit scale factor while
422 // generating the metafile, so we expect smaller pages. (On non-Mac
423 // platforms, this would be 675x900).
430 #endif // defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
433 // TODO(estade): need to port MockPrinter to get this on Linux. This involves
434 // hooking up Cairo to read a pdf stream, or accessing the cairo surface in the
435 // metafile directly.
436 // Same for printing via PDF on Windows.
437 #if defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
438 TEST_F(MAYBE_PrintWebViewHelperTest
, PrintLayoutTest
) {
439 bool baseline
= false;
441 EXPECT_TRUE(print_render_thread_
->printer() != NULL
);
442 for (size_t i
= 0; i
< arraysize(kTestPages
); ++i
) {
443 // Load an HTML page and print it.
444 LoadHTML(kTestPages
[i
].page
);
446 VerifyPagesPrinted(true);
448 // MockRenderThread::Send() just calls MockRenderThread::OnReceived().
449 // So, all IPC messages sent in the above RenderView::OnPrintPages() call
450 // has been handled by the MockPrinter object, i.e. this printing job
451 // has been already finished.
452 // So, we can start checking the output pages of this printing job.
453 // Retrieve the number of pages actually printed.
454 size_t pages
= print_render_thread_
->printer()->GetPrintedPages();
455 EXPECT_EQ(kTestPages
[i
].printed_pages
, pages
);
457 // Retrieve the width and height of the output page.
458 int width
= print_render_thread_
->printer()->GetWidth(0);
459 int height
= print_render_thread_
->printer()->GetHeight(0);
461 // Check with margin for error. This has been failing with a one pixel
462 // offset on our buildbot.
463 const int kErrorMargin
= 5; // 5%
464 EXPECT_GT(kTestPages
[i
].width
* (100 + kErrorMargin
) / 100, width
);
465 EXPECT_LT(kTestPages
[i
].width
* (100 - kErrorMargin
) / 100, width
);
466 EXPECT_GT(kTestPages
[i
].height
* (100 + kErrorMargin
) / 100, height
);
467 EXPECT_LT(kTestPages
[i
].height
* (100 - kErrorMargin
) / 100, height
);
469 // Retrieve the checksum of the bitmap data from the pseudo printer and
470 // compare it with the expected result.
471 std::string bitmap_actual
;
473 print_render_thread_
->printer()->GetBitmapChecksum(0, &bitmap_actual
));
474 if (kTestPages
[i
].checksum
)
475 EXPECT_EQ(kTestPages
[i
].checksum
, bitmap_actual
);
478 // Save the source data and the bitmap data into temporary files to
479 // create base-line results.
480 base::FilePath source_path
;
481 base::CreateTemporaryFile(&source_path
);
482 print_render_thread_
->printer()->SaveSource(0, source_path
);
484 base::FilePath bitmap_path
;
485 base::CreateTemporaryFile(&bitmap_path
);
486 print_render_thread_
->printer()->SaveBitmap(0, bitmap_path
);
490 #endif // OS_MACOSX && ENABLE_BASIC_PRINTING
492 // These print preview tests do not work on Chrome OS yet.
493 #if !defined(OS_CHROMEOS)
495 // RenderViewTest-based tests crash on Android
496 // http://crbug.com/187500
497 #if defined(OS_ANDROID)
498 #define MAYBE_PrintWebViewHelperPreviewTest \
499 DISABLED_PrintWebViewHelperPreviewTest
501 #define MAYBE_PrintWebViewHelperPreviewTest PrintWebViewHelperPreviewTest
502 #endif // defined(OS_ANDROID)
504 class MAYBE_PrintWebViewHelperPreviewTest
: public PrintWebViewHelperTestBase
{
506 MAYBE_PrintWebViewHelperPreviewTest() {}
507 ~MAYBE_PrintWebViewHelperPreviewTest() override
{}
510 void VerifyPrintPreviewCancelled(bool did_cancel
) {
511 bool print_preview_cancelled
=
512 (render_thread_
->sink().GetUniqueMessageMatching(
513 PrintHostMsg_PrintPreviewCancelled::ID
) != NULL
);
514 EXPECT_EQ(did_cancel
, print_preview_cancelled
);
517 void VerifyPrintPreviewFailed(bool did_fail
) {
518 bool print_preview_failed
=
519 (render_thread_
->sink().GetUniqueMessageMatching(
520 PrintHostMsg_PrintPreviewFailed::ID
) != NULL
);
521 EXPECT_EQ(did_fail
, print_preview_failed
);
524 void VerifyPrintPreviewGenerated(bool generated_preview
) {
525 const IPC::Message
* preview_msg
=
526 render_thread_
->sink().GetUniqueMessageMatching(
527 PrintHostMsg_MetafileReadyForPrinting::ID
);
528 bool did_get_preview_msg
= (NULL
!= preview_msg
);
529 ASSERT_EQ(generated_preview
, did_get_preview_msg
);
530 if (did_get_preview_msg
) {
531 PrintHostMsg_MetafileReadyForPrinting::Param preview_param
;
532 PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg
, &preview_param
);
533 EXPECT_NE(0, base::get
<0>(preview_param
).document_cookie
);
534 EXPECT_NE(0, base::get
<0>(preview_param
).expected_pages_count
);
535 EXPECT_NE(0U, base::get
<0>(preview_param
).data_size
);
539 void VerifyPrintFailed(bool did_fail
) {
540 bool print_failed
= (render_thread_
->sink().GetUniqueMessageMatching(
541 PrintHostMsg_PrintingFailed::ID
) != NULL
);
542 EXPECT_EQ(did_fail
, print_failed
);
545 void VerifyPrintPreviewInvalidPrinterSettings(bool settings_invalid
) {
546 bool print_preview_invalid_printer_settings
=
547 (render_thread_
->sink().GetUniqueMessageMatching(
548 PrintHostMsg_PrintPreviewInvalidPrinterSettings::ID
) != NULL
);
549 EXPECT_EQ(settings_invalid
, print_preview_invalid_printer_settings
);
552 // |page_number| is 0-based.
553 void VerifyDidPreviewPage(bool generate_draft_pages
, int page_number
) {
554 bool msg_found
= false;
555 size_t msg_count
= render_thread_
->sink().message_count();
556 for (size_t i
= 0; i
< msg_count
; ++i
) {
557 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
558 if (msg
->type() == PrintHostMsg_DidPreviewPage::ID
) {
559 PrintHostMsg_DidPreviewPage::Param page_param
;
560 PrintHostMsg_DidPreviewPage::Read(msg
, &page_param
);
561 if (base::get
<0>(page_param
).page_number
== page_number
) {
563 if (generate_draft_pages
)
564 EXPECT_NE(0U, base::get
<0>(page_param
).data_size
);
566 EXPECT_EQ(0U, base::get
<0>(page_param
).data_size
);
571 ASSERT_EQ(generate_draft_pages
, msg_found
);
574 void VerifyDefaultPageLayout(int content_width
,
580 bool page_has_print_css
) {
581 const IPC::Message
* default_page_layout_msg
=
582 render_thread_
->sink().GetUniqueMessageMatching(
583 PrintHostMsg_DidGetDefaultPageLayout::ID
);
584 bool did_get_default_page_layout_msg
= (NULL
!= default_page_layout_msg
);
585 if (did_get_default_page_layout_msg
) {
586 PrintHostMsg_DidGetDefaultPageLayout::Param param
;
587 PrintHostMsg_DidGetDefaultPageLayout::Read(default_page_layout_msg
,
589 EXPECT_EQ(content_width
, base::get
<0>(param
).content_width
);
590 EXPECT_EQ(content_height
, base::get
<0>(param
).content_height
);
591 EXPECT_EQ(margin_top
, base::get
<0>(param
).margin_top
);
592 EXPECT_EQ(margin_right
, base::get
<0>(param
).margin_right
);
593 EXPECT_EQ(margin_left
, base::get
<0>(param
).margin_left
);
594 EXPECT_EQ(margin_bottom
, base::get
<0>(param
).margin_bottom
);
595 EXPECT_EQ(page_has_print_css
, base::get
<2>(param
));
599 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperPreviewTest
);
602 #if defined(ENABLE_PRINT_PREVIEW)
603 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, BlockScriptInitiatedPrinting
) {
604 LoadHTML(kHelloWorldHTML
);
605 PrintWebViewHelper
* print_web_view_helper
= PrintWebViewHelper::Get(view_
);
606 print_web_view_helper
->SetScriptedPrintBlocked(true);
607 PrintWithJavaScript();
608 VerifyPreviewRequest(false);
610 print_web_view_helper
->SetScriptedPrintBlocked(false);
611 PrintWithJavaScript();
612 VerifyPreviewRequest(true);
615 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, PrintWithJavaScript
) {
616 LoadHTML(kPrintOnUserAction
);
617 gfx::Size
new_size(200, 100);
618 Resize(new_size
, gfx::Rect(), false);
620 gfx::Rect bounds
= GetElementBounds("print");
621 EXPECT_FALSE(bounds
.IsEmpty());
622 blink::WebMouseEvent mouse_event
;
623 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
624 mouse_event
.button
= blink::WebMouseEvent::ButtonLeft
;
625 mouse_event
.x
= bounds
.CenterPoint().x();
626 mouse_event
.y
= bounds
.CenterPoint().y();
627 mouse_event
.clickCount
= 1;
628 SendWebMouseEvent(mouse_event
);
629 mouse_event
.type
= blink::WebInputEvent::MouseUp
;
630 SendWebMouseEvent(mouse_event
);
632 VerifyPreviewRequest(true);
634 #endif // ENABLE_PRINT_PREVIEW
636 // Tests that print preview work and sending and receiving messages through
637 // that channel all works.
638 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintPreview
) {
639 LoadHTML(kHelloWorldHTML
);
641 // Fill in some dummy values.
642 base::DictionaryValue dict
;
643 CreatePrintSettingsDictionary(&dict
);
644 OnPrintPreview(dict
);
646 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
647 VerifyDefaultPageLayout(540, 720, 36, 36, 36, 36, false);
648 VerifyPrintPreviewCancelled(false);
649 VerifyPrintPreviewFailed(false);
650 VerifyPrintPreviewGenerated(true);
651 VerifyPagesPrinted(false);
654 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
655 PrintPreviewHTMLWithPageMarginsCss
) {
656 // A simple web page with print margins css.
657 const char kHTMLWithPageMarginsCss
[] =
658 "<html><head><style>"
661 " margin: 3in 1in 2in 0.3in;"
667 LoadHTML(kHTMLWithPageMarginsCss
);
669 // Fill in some dummy values.
670 base::DictionaryValue dict
;
671 CreatePrintSettingsDictionary(&dict
);
672 dict
.SetBoolean(kSettingPrintToPDF
, false);
673 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
674 OnPrintPreview(dict
);
676 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
677 VerifyDefaultPageLayout(519, 432, 216, 144, 21, 72, false);
678 VerifyPrintPreviewCancelled(false);
679 VerifyPrintPreviewFailed(false);
680 VerifyPrintPreviewGenerated(true);
681 VerifyPagesPrinted(false);
684 // Test to verify that print preview ignores print media css when non-default
685 // margin is selected.
686 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
687 NonDefaultMarginsSelectedIgnorePrintCss
) {
688 LoadHTML(kHTMLWithPageSizeCss
);
690 // Fill in some dummy values.
691 base::DictionaryValue dict
;
692 CreatePrintSettingsDictionary(&dict
);
693 dict
.SetBoolean(kSettingPrintToPDF
, false);
694 dict
.SetInteger(kSettingMarginsType
, NO_MARGINS
);
695 OnPrintPreview(dict
);
697 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
698 VerifyDefaultPageLayout(612, 792, 0, 0, 0, 0, true);
699 VerifyPrintPreviewCancelled(false);
700 VerifyPrintPreviewFailed(false);
701 VerifyPrintPreviewGenerated(true);
702 VerifyPagesPrinted(false);
705 // Test to verify that print preview honor print media size css when
706 // PRINT_TO_PDF is selected and doesn't fit to printer default paper size.
707 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, PrintToPDFSelectedHonorPrintCss
) {
708 LoadHTML(kHTMLWithPageSizeCss
);
710 // Fill in some dummy values.
711 base::DictionaryValue dict
;
712 CreatePrintSettingsDictionary(&dict
);
713 dict
.SetBoolean(kSettingPrintToPDF
, true);
714 dict
.SetInteger(kSettingMarginsType
, PRINTABLE_AREA_MARGINS
);
715 OnPrintPreview(dict
);
717 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
718 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
720 VerifyDefaultPageLayout(252, 252, 18, 18, 18, 18, true);
721 VerifyPrintPreviewCancelled(false);
722 VerifyPrintPreviewFailed(false);
725 // Test to verify that print preview honor print margin css when PRINT_TO_PDF
726 // is selected and doesn't fit to printer default paper size.
727 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
728 PrintToPDFSelectedHonorPageMarginsCss
) {
729 // A simple web page with print margins css.
730 const char kHTMLWithPageCss
[] =
731 "<html><head><style>"
734 " margin: 3in 1in 2in 0.3in;"
741 LoadHTML(kHTMLWithPageCss
);
743 // Fill in some dummy values.
744 base::DictionaryValue dict
;
745 CreatePrintSettingsDictionary(&dict
);
746 dict
.SetBoolean(kSettingPrintToPDF
, true);
747 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
748 OnPrintPreview(dict
);
750 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
751 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
753 VerifyDefaultPageLayout(915, 648, 216, 144, 21, 72, true);
754 VerifyPrintPreviewCancelled(false);
755 VerifyPrintPreviewFailed(false);
758 // Test to verify that print preview workflow center the html page contents to
759 // fit the page size.
760 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, PrintPreviewCenterToFitPage
) {
761 LoadHTML(kHTMLWithPageSizeCss
);
763 // Fill in some dummy values.
764 base::DictionaryValue dict
;
765 CreatePrintSettingsDictionary(&dict
);
766 dict
.SetBoolean(kSettingPrintToPDF
, false);
767 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
768 OnPrintPreview(dict
);
770 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
771 VerifyDefaultPageLayout(216, 216, 288, 288, 198, 198, true);
772 VerifyPrintPreviewCancelled(false);
773 VerifyPrintPreviewFailed(false);
774 VerifyPrintPreviewGenerated(true);
777 // Test to verify that print preview workflow scale the html page contents to
778 // fit the page size.
779 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, PrintPreviewShrinkToFitPage
) {
780 // A simple web page with print margins css.
781 const char kHTMLWithPageCss
[] =
782 "<html><head><style>"
791 LoadHTML(kHTMLWithPageCss
);
793 // Fill in some dummy values.
794 base::DictionaryValue dict
;
795 CreatePrintSettingsDictionary(&dict
);
796 dict
.SetBoolean(kSettingPrintToPDF
, false);
797 dict
.SetInteger(kSettingMarginsType
, DEFAULT_MARGINS
);
798 OnPrintPreview(dict
);
800 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
801 VerifyDefaultPageLayout(571, 652, 69, 71, 20, 21, true);
802 VerifyPrintPreviewCancelled(false);
803 VerifyPrintPreviewFailed(false);
806 // Test to verify that print preview workflow honor the orientation settings
808 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, PrintPreviewHonorsOrientationCss
) {
809 LoadHTML(kHTMLWithLandscapePageCss
);
811 // Fill in some dummy values.
812 base::DictionaryValue dict
;
813 CreatePrintSettingsDictionary(&dict
);
814 dict
.SetBoolean(kSettingPrintToPDF
, false);
815 dict
.SetInteger(kSettingMarginsType
, NO_MARGINS
);
816 OnPrintPreview(dict
);
818 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
819 VerifyDefaultPageLayout(792, 612, 0, 0, 0, 0, true);
820 VerifyPrintPreviewCancelled(false);
821 VerifyPrintPreviewFailed(false);
824 // Test to verify that print preview workflow honors the orientation settings
825 // specified in css when PRINT_TO_PDF is selected.
826 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
827 PrintToPDFSelectedHonorOrientationCss
) {
828 LoadHTML(kHTMLWithLandscapePageCss
);
830 // Fill in some dummy values.
831 base::DictionaryValue dict
;
832 CreatePrintSettingsDictionary(&dict
);
833 dict
.SetBoolean(kSettingPrintToPDF
, true);
834 dict
.SetInteger(kSettingMarginsType
, CUSTOM_MARGINS
);
835 OnPrintPreview(dict
);
837 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
838 VerifyDefaultPageLayout(748, 568, 21, 23, 21, 23, true);
839 VerifyPrintPreviewCancelled(false);
840 VerifyPrintPreviewFailed(false);
843 // Test to verify that complete metafile is generated for a subset of pages
844 // without creating draft pages.
845 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintPreviewForSelectedPages
) {
846 LoadHTML(kMultipageHTML
);
848 // Fill in some dummy values.
849 base::DictionaryValue dict
;
850 CreatePrintSettingsDictionary(&dict
);
852 // Set a page range and update the dictionary to generate only the complete
853 // metafile with the selected pages. Page numbers used in the dictionary
855 base::DictionaryValue
* page_range
= new base::DictionaryValue();
856 page_range
->SetInteger(kSettingPageRangeFrom
, 2);
857 page_range
->SetInteger(kSettingPageRangeTo
, 3);
859 base::ListValue
* page_range_array
= new base::ListValue();
860 page_range_array
->Append(page_range
);
862 dict
.Set(kSettingPageRange
, page_range_array
);
863 dict
.SetBoolean(kSettingGenerateDraftData
, false);
865 OnPrintPreview(dict
);
867 VerifyDidPreviewPage(false, 0);
868 VerifyDidPreviewPage(false, 1);
869 VerifyDidPreviewPage(false, 2);
870 VerifyPreviewPageCount(3);
871 VerifyPrintPreviewCancelled(false);
872 VerifyPrintPreviewFailed(false);
873 VerifyPrintPreviewGenerated(true);
874 VerifyPagesPrinted(false);
877 // Test to verify that preview generated only for one page.
878 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintPreviewForSelectedText
) {
879 LoadHTML(kMultipageHTML
);
880 GetMainFrame()->selectRange(
881 blink::WebRange::fromDocumentRange(GetMainFrame(), 1, 3));
883 // Fill in some dummy values.
884 base::DictionaryValue dict
;
885 CreatePrintSettingsDictionary(&dict
);
886 dict
.SetBoolean(kSettingShouldPrintSelectionOnly
, true);
888 OnPrintPreview(dict
);
890 VerifyPreviewPageCount(1);
891 VerifyPrintPreviewCancelled(false);
892 VerifyPrintPreviewFailed(false);
893 VerifyPrintPreviewGenerated(true);
894 VerifyPagesPrinted(false);
897 // Tests that print preview fails and receiving error messages through
898 // that channel all works.
899 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintPreviewFail
) {
900 LoadHTML(kHelloWorldHTML
);
902 // An empty dictionary should fail.
903 base::DictionaryValue empty_dict
;
904 OnPrintPreview(empty_dict
);
906 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
907 VerifyPrintPreviewCancelled(false);
908 VerifyPrintPreviewFailed(true);
909 VerifyPrintPreviewGenerated(false);
910 VerifyPagesPrinted(false);
913 // Tests that cancelling print preview works.
914 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintPreviewCancel
) {
915 LoadHTML(kLongPageHTML
);
917 const int kCancelPage
= 3;
918 print_render_thread_
->set_print_preview_cancel_page_number(kCancelPage
);
919 // Fill in some dummy values.
920 base::DictionaryValue dict
;
921 CreatePrintSettingsDictionary(&dict
);
922 OnPrintPreview(dict
);
924 EXPECT_EQ(kCancelPage
, print_render_thread_
->print_preview_pages_remaining());
925 VerifyPrintPreviewCancelled(true);
926 VerifyPrintPreviewFailed(false);
927 VerifyPrintPreviewGenerated(false);
928 VerifyPagesPrinted(false);
931 // Tests that printing from print preview works and sending and receiving
932 // messages through that channel all works.
933 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintForPrintPreview
) {
934 LoadHTML(kPrintPreviewHTML
);
936 // Fill in some dummy values.
937 base::DictionaryValue dict
;
938 CreatePrintSettingsDictionary(&dict
);
939 OnPrintForPrintPreview(dict
);
941 VerifyPrintFailed(false);
942 VerifyPagesPrinted(true);
945 // Tests that printing from print preview fails and receiving error messages
946 // through that channel all works.
947 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
, OnPrintForPrintPreviewFail
) {
948 LoadHTML(kPrintPreviewHTML
);
950 // An empty dictionary should fail.
951 base::DictionaryValue empty_dict
;
952 OnPrintForPrintPreview(empty_dict
);
954 VerifyPagesPrinted(false);
957 // Tests that when default printer has invalid printer settings, print preview
958 // receives error message.
959 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
960 OnPrintPreviewUsingInvalidPrinterSettings
) {
961 LoadHTML(kPrintPreviewHTML
);
963 // Set mock printer to provide invalid settings.
964 print_render_thread_
->printer()->UseInvalidSettings();
966 // Fill in some dummy values.
967 base::DictionaryValue dict
;
968 CreatePrintSettingsDictionary(&dict
);
969 OnPrintPreview(dict
);
971 // We should have received invalid printer settings from |printer_|.
972 VerifyPrintPreviewInvalidPrinterSettings(true);
973 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
975 // It should receive the invalid printer settings message only.
976 VerifyPrintPreviewFailed(false);
977 VerifyPrintPreviewGenerated(false);
980 // Tests that when the selected printer has invalid page settings, print preview
981 // receives error message.
982 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
983 OnPrintPreviewUsingInvalidPageSize
) {
984 LoadHTML(kPrintPreviewHTML
);
986 print_render_thread_
->printer()->UseInvalidPageSize();
988 base::DictionaryValue dict
;
989 CreatePrintSettingsDictionary(&dict
);
990 OnPrintPreview(dict
);
992 VerifyPrintPreviewInvalidPrinterSettings(true);
993 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
995 // It should receive the invalid printer settings message only.
996 VerifyPrintPreviewFailed(false);
997 VerifyPrintPreviewGenerated(false);
1000 // Tests that when the selected printer has invalid content settings, print
1001 // preview receives error message.
1002 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
1003 OnPrintPreviewUsingInvalidContentSize
) {
1004 LoadHTML(kPrintPreviewHTML
);
1006 print_render_thread_
->printer()->UseInvalidContentSize();
1008 base::DictionaryValue dict
;
1009 CreatePrintSettingsDictionary(&dict
);
1010 OnPrintPreview(dict
);
1012 VerifyPrintPreviewInvalidPrinterSettings(true);
1013 EXPECT_EQ(0, print_render_thread_
->print_preview_pages_remaining());
1015 // It should receive the invalid printer settings message only.
1016 VerifyPrintPreviewFailed(false);
1017 VerifyPrintPreviewGenerated(false);
1020 TEST_F(MAYBE_PrintWebViewHelperPreviewTest
,
1021 OnPrintForPrintPreviewUsingInvalidPrinterSettings
) {
1022 LoadHTML(kPrintPreviewHTML
);
1024 // Set mock printer to provide invalid settings.
1025 print_render_thread_
->printer()->UseInvalidSettings();
1027 // Fill in some dummy values.
1028 base::DictionaryValue dict
;
1029 CreatePrintSettingsDictionary(&dict
);
1030 OnPrintForPrintPreview(dict
);
1032 VerifyPrintFailed(true);
1033 VerifyPagesPrinted(false);
1036 #endif // !defined(OS_CHROMEOS)
1038 } // namespace printing