[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / components / printing / test / print_web_view_helper_browsertest.cc
blob21ebc5a399007bfc6b284868db376761db3a36bd
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;
29 #endif
31 namespace printing {
33 namespace {
35 #if !defined(OS_CHROMEOS)
37 // A simple web page.
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[] =
42 "<body>"
43 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>"
44 "</body>";
46 // HTML with 3 pages.
47 const char kMultipageHTML[] =
48 "<html><head><style>"
49 ".break { page-break-after: always; }"
50 "</style></head>"
51 "<body>"
52 "<div class='break'>page1</div>"
53 "<div class='break'>page2</div>"
54 "<div>page3</div>"
55 "</body></html>";
57 // A simple web page with print page size css.
58 const char kHTMLWithPageSizeCss[] =
59 "<html><head><style>"
60 "@media print {"
61 " @page {"
62 " size: 4in 4in;"
63 " }"
64 "}"
65 "</style></head>"
66 "<body>Lorem Ipsum:"
67 "</body></html>";
69 // A simple web page with print page layout css.
70 const char kHTMLWithLandscapePageCss[] =
71 "<html><head><style>"
72 "@media print {"
73 " @page {"
74 " size: landscape;"
75 " }"
76 "}"
77 "</style></head>"
78 "<body>Lorem Ipsum:"
79 "</body></html>";
81 // A longer web page.
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 {
110 public:
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)
118 run_loop_->Quit();
119 return false;
122 private:
123 base::RunLoop* const run_loop_;
124 DISALLOW_COPY_AND_ASSIGN(DidPreviewPageListener);
127 } // namespace
129 class PrintWebViewHelperTestBase : public content::RenderViewTest {
130 public:
131 PrintWebViewHelperTestBase() : print_render_thread_(NULL) {}
132 ~PrintWebViewHelperTestBase() override {}
134 protected:
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();
151 #endif
152 content::RenderViewTest::TearDown();
155 void PrintWithJavaScript() {
156 ExecuteJavaScript("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
166 // the print code.
167 #else
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 #if defined(OS_CHROMEOS)
196 bool did_print_msg =
197 (render_thread_->sink().GetUniqueMessageMatching(
198 PrintHostMsg_TempFileForPrintingWritten::ID) != NULL);
199 ASSERT_EQ(printed, did_print_msg);
200 #else
201 const IPC::Message* print_msg =
202 render_thread_->sink().GetUniqueMessageMatching(
203 PrintHostMsg_DidPrintPage::ID);
204 bool did_print_msg = (NULL != print_msg);
205 ASSERT_EQ(printed, did_print_msg);
206 if (printed) {
207 PrintHostMsg_DidPrintPage::Param post_did_print_page_param;
208 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param);
209 EXPECT_EQ(0, base::get<0>(post_did_print_page_param).page_number);
211 #endif // defined(OS_CHROMEOS)
214 #if defined(ENABLE_BASIC_PRINTING)
215 void OnPrintPages() {
216 PrintWebViewHelper::Get(view_)->OnPrintPages();
217 ProcessPendingMessages();
219 #endif // ENABLE_BASIC_PRINTING
221 void VerifyPreviewRequest(bool requested) {
222 const IPC::Message* print_msg =
223 render_thread_->sink().GetUniqueMessageMatching(
224 PrintHostMsg_SetupScriptedPrintPreview::ID);
225 bool did_print_msg = (NULL != print_msg);
226 ASSERT_EQ(requested, did_print_msg);
229 void OnPrintPreview(const base::DictionaryValue& dict) {
230 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_);
231 print_web_view_helper->OnInitiatePrintPreview(false);
232 base::RunLoop run_loop;
233 DidPreviewPageListener filter(&run_loop);
234 render_thread_->sink().AddFilter(&filter);
235 print_web_view_helper->OnPrintPreview(dict);
236 run_loop.Run();
237 render_thread_->sink().RemoveFilter(&filter);
240 void OnPrintForPrintPreview(const base::DictionaryValue& dict) {
241 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict);
242 ProcessPendingMessages();
245 // Naked pointer as ownership is with content::RenderViewTest::render_thread_.
246 PrintMockRenderThread* print_render_thread_;
248 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase);
251 // RenderViewTest-based tests crash on Android
252 // http://crbug.com/187500
253 #if defined(OS_ANDROID)
254 #define MAYBE_PrintWebViewHelperTest DISABLED_PrintWebViewHelperTest
255 #else
256 #define MAYBE_PrintWebViewHelperTest PrintWebViewHelperTest
257 #endif // defined(OS_ANDROID)
259 class MAYBE_PrintWebViewHelperTest : public PrintWebViewHelperTestBase {
260 public:
261 MAYBE_PrintWebViewHelperTest() {}
262 ~MAYBE_PrintWebViewHelperTest() override {}
264 void SetUp() override { PrintWebViewHelperTestBase::SetUp(); }
266 protected:
267 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperTest);
270 // This tests only for platforms without print preview.
271 #if !defined(ENABLE_PRINT_PREVIEW)
272 // Tests that the renderer blocks window.print() calls if they occur too
273 // frequently.
274 TEST_F(MAYBE_PrintWebViewHelperTest, BlockScriptInitiatedPrinting) {
275 // Pretend user will cancel printing.
276 print_render_thread_->set_print_dialog_user_response(false);
277 // Try to print with window.print() a few times.
278 PrintWithJavaScript();
279 PrintWithJavaScript();
280 PrintWithJavaScript();
281 VerifyPagesPrinted(false);
283 // Pretend user will print. (but printing is blocked.)
284 print_render_thread_->set_print_dialog_user_response(true);
285 PrintWithJavaScript();
286 VerifyPagesPrinted(false);
288 // Unblock script initiated printing and verify printing works.
289 PrintWebViewHelper::Get(view_)->scripting_throttler_.Reset();
290 print_render_thread_->printer()->ResetPrinter();
291 PrintWithJavaScript();
292 VerifyPageCount(1);
293 VerifyPagesPrinted(true);
296 // Tests that the renderer always allows window.print() calls if they are user
297 // initiated.
298 TEST_F(MAYBE_PrintWebViewHelperTest, AllowUserOriginatedPrinting) {
299 // Pretend user will cancel printing.
300 print_render_thread_->set_print_dialog_user_response(false);
301 // Try to print with window.print() a few times.
302 PrintWithJavaScript();
303 PrintWithJavaScript();
304 PrintWithJavaScript();
305 VerifyPagesPrinted(false);
307 // Pretend user will print. (but printing is blocked.)
308 print_render_thread_->set_print_dialog_user_response(true);
309 PrintWithJavaScript();
310 VerifyPagesPrinted(false);
312 // Try again as if user initiated, without resetting the print count.
313 print_render_thread_->printer()->ResetPrinter();
314 LoadHTML(kPrintOnUserAction);
315 gfx::Size new_size(200, 100);
316 Resize(new_size, gfx::Rect(), false);
318 gfx::Rect bounds = GetElementBounds("print");
319 EXPECT_FALSE(bounds.IsEmpty());
320 blink::WebMouseEvent mouse_event;
321 mouse_event.type = blink::WebInputEvent::MouseDown;
322 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
323 mouse_event.x = bounds.CenterPoint().x();
324 mouse_event.y = bounds.CenterPoint().y();
325 mouse_event.clickCount = 1;
326 SendWebMouseEvent(mouse_event);
327 mouse_event.type = blink::WebInputEvent::MouseUp;
328 SendWebMouseEvent(mouse_event);
329 ProcessPendingMessages();
331 VerifyPageCount(1);
332 VerifyPagesPrinted(true);
335 // Duplicate of OnPrintPagesTest only using javascript to print.
336 TEST_F(MAYBE_PrintWebViewHelperTest, PrintWithJavascript) {
337 PrintWithJavaScript();
339 VerifyPageCount(1);
340 VerifyPagesPrinted(true);
342 #endif // !ENABLE_PRINT_PREVIEW
344 #if defined(ENABLE_BASIC_PRINTING)
345 // Tests that printing pages work and sending and receiving messages through
346 // that channel all works.
347 TEST_F(MAYBE_PrintWebViewHelperTest, OnPrintPages) {
348 LoadHTML(kHelloWorldHTML);
349 OnPrintPages();
351 VerifyPageCount(1);
352 VerifyPagesPrinted(true);
354 #endif // ENABLE_BASIC_PRINTING
356 #if defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
357 // TODO(estade): I don't think this test is worth porting to Linux. We will have
358 // to rip out and replace most of the IPC code if we ever plan to improve
359 // printing, and the comment below by sverrir suggests that it doesn't do much
360 // for us anyway.
361 TEST_F(MAYBE_PrintWebViewHelperTest, PrintWithIframe) {
362 // Document that populates an iframe.
363 const char html[] =
364 "<html><body>Lorem Ipsum:"
365 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>"
366 " document.write(frames['sub1'].name);"
367 " frames['sub1'].document.write("
368 " '<p>Cras tempus ante eu felis semper luctus!</p>');"
369 " frames['sub1'].document.close();"
370 "</script></body></html>";
372 LoadHTML(html);
374 // Find the frame and set it as the focused one. This should mean that that
375 // the printout should only contain the contents of that frame.
376 WebFrame* sub1_frame =
377 view_->GetWebView()->findFrameByName(WebString::fromUTF8("sub1"));
378 ASSERT_TRUE(sub1_frame);
379 view_->GetWebView()->setFocusedFrame(sub1_frame);
380 ASSERT_NE(view_->GetWebView()->focusedFrame(),
381 view_->GetWebView()->mainFrame());
383 // Initiate printing.
384 OnPrintPages();
385 VerifyPagesPrinted(true);
387 // Verify output through MockPrinter.
388 const MockPrinter* printer(print_render_thread_->printer());
389 ASSERT_EQ(1, printer->GetPrintedPages());
390 const Image& image1(printer->GetPrintedPage(0)->image());
392 // TODO(sverrir): Figure out a way to improve this test to actually print
393 // only the content of the iframe. Currently image1 will contain the full
394 // page.
395 EXPECT_NE(0, image1.size().width());
396 EXPECT_NE(0, image1.size().height());
398 #endif // OS_MACOSX && ENABLE_BASIC_PRINTING
400 // Tests if we can print a page and verify its results.
401 // This test prints HTML pages into a pseudo printer and check their outputs,
402 // i.e. a simplified version of the PrintingLayoutTextTest UI test.
403 namespace {
404 // Test cases used in this test.
405 struct TestPageData {
406 const char* page;
407 size_t printed_pages;
408 int width;
409 int height;
410 const char* checksum;
411 const wchar_t* file;
414 #if defined(OS_WIN) || defined(OS_MACOSX)
415 const TestPageData kTestPages[] = {
417 "<html>"
418 "<head>"
419 "<meta"
420 " http-equiv=\"Content-Type\""
421 " content=\"text/html; charset=utf-8\"/>"
422 "<title>Test 1</title>"
423 "</head>"
424 "<body style=\"background-color: white;\">"
425 "<p style=\"font-family: arial;\">Hello World!</p>"
426 "</body>",
427 #if defined(OS_MACOSX)
428 // Mac printing code compensates for the WebKit scale factor while
429 // generating
430 // the metafile, so we expect smaller pages.
432 600,
433 780,
434 #else
436 675,
437 900,
438 #endif
439 NULL,
440 NULL,
443 #endif // defined(OS_WIN) || defined(OS_MACOSX)
444 } // namespace
446 // TODO(estade): need to port MockPrinter to get this on Linux. This involves
447 // hooking up Cairo to read a pdf stream, or accessing the cairo surface in the
448 // metafile directly.
449 // Same for printing via PDF on Windows.
450 #if defined(OS_MACOSX) && defined(ENABLE_BASIC_PRINTING)
451 TEST_F(MAYBE_PrintWebViewHelperTest, PrintLayoutTest) {
452 bool baseline = false;
454 EXPECT_TRUE(print_render_thread_->printer() != NULL);
455 for (size_t i = 0; i < arraysize(kTestPages); ++i) {
456 // Load an HTML page and print it.
457 LoadHTML(kTestPages[i].page);
458 OnPrintPages();
459 VerifyPagesPrinted(true);
461 // MockRenderThread::Send() just calls MockRenderThread::OnReceived().
462 // So, all IPC messages sent in the above RenderView::OnPrintPages() call
463 // has been handled by the MockPrinter object, i.e. this printing job
464 // has been already finished.
465 // So, we can start checking the output pages of this printing job.
466 // Retrieve the number of pages actually printed.
467 size_t pages = print_render_thread_->printer()->GetPrintedPages();
468 EXPECT_EQ(kTestPages[i].printed_pages, pages);
470 // Retrieve the width and height of the output page.
471 int width = print_render_thread_->printer()->GetWidth(0);
472 int height = print_render_thread_->printer()->GetHeight(0);
474 // Check with margin for error. This has been failing with a one pixel
475 // offset on our buildbot.
476 const int kErrorMargin = 5; // 5%
477 EXPECT_GT(kTestPages[i].width * (100 + kErrorMargin) / 100, width);
478 EXPECT_LT(kTestPages[i].width * (100 - kErrorMargin) / 100, width);
479 EXPECT_GT(kTestPages[i].height * (100 + kErrorMargin) / 100, height);
480 EXPECT_LT(kTestPages[i].height * (100 - kErrorMargin) / 100, height);
482 // Retrieve the checksum of the bitmap data from the pseudo printer and
483 // compare it with the expected result.
484 std::string bitmap_actual;
485 EXPECT_TRUE(
486 print_render_thread_->printer()->GetBitmapChecksum(0, &bitmap_actual));
487 if (kTestPages[i].checksum)
488 EXPECT_EQ(kTestPages[i].checksum, bitmap_actual);
490 if (baseline) {
491 // Save the source data and the bitmap data into temporary files to
492 // create base-line results.
493 base::FilePath source_path;
494 base::CreateTemporaryFile(&source_path);
495 print_render_thread_->printer()->SaveSource(0, source_path);
497 base::FilePath bitmap_path;
498 base::CreateTemporaryFile(&bitmap_path);
499 print_render_thread_->printer()->SaveBitmap(0, bitmap_path);
503 #endif // OS_MACOSX && ENABLE_BASIC_PRINTING
505 // These print preview tests do not work on Chrome OS yet.
506 #if !defined(OS_CHROMEOS)
508 // RenderViewTest-based tests crash on Android
509 // http://crbug.com/187500
510 #if defined(OS_ANDROID)
511 #define MAYBE_PrintWebViewHelperPreviewTest \
512 DISABLED_PrintWebViewHelperPreviewTest
513 #else
514 #define MAYBE_PrintWebViewHelperPreviewTest PrintWebViewHelperPreviewTest
515 #endif // defined(OS_ANDROID)
517 class MAYBE_PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase {
518 public:
519 MAYBE_PrintWebViewHelperPreviewTest() {}
520 ~MAYBE_PrintWebViewHelperPreviewTest() override {}
522 protected:
523 void VerifyPrintPreviewCancelled(bool did_cancel) {
524 bool print_preview_cancelled =
525 (render_thread_->sink().GetUniqueMessageMatching(
526 PrintHostMsg_PrintPreviewCancelled::ID) != NULL);
527 EXPECT_EQ(did_cancel, print_preview_cancelled);
530 void VerifyPrintPreviewFailed(bool did_fail) {
531 bool print_preview_failed =
532 (render_thread_->sink().GetUniqueMessageMatching(
533 PrintHostMsg_PrintPreviewFailed::ID) != NULL);
534 EXPECT_EQ(did_fail, print_preview_failed);
537 void VerifyPrintPreviewGenerated(bool generated_preview) {
538 const IPC::Message* preview_msg =
539 render_thread_->sink().GetUniqueMessageMatching(
540 PrintHostMsg_MetafileReadyForPrinting::ID);
541 bool did_get_preview_msg = (NULL != preview_msg);
542 ASSERT_EQ(generated_preview, did_get_preview_msg);
543 if (did_get_preview_msg) {
544 PrintHostMsg_MetafileReadyForPrinting::Param preview_param;
545 PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg, &preview_param);
546 EXPECT_NE(0, base::get<0>(preview_param).document_cookie);
547 EXPECT_NE(0, base::get<0>(preview_param).expected_pages_count);
548 EXPECT_NE(0U, base::get<0>(preview_param).data_size);
552 void VerifyPrintFailed(bool did_fail) {
553 bool print_failed = (render_thread_->sink().GetUniqueMessageMatching(
554 PrintHostMsg_PrintingFailed::ID) != NULL);
555 EXPECT_EQ(did_fail, print_failed);
558 void VerifyPrintPreviewInvalidPrinterSettings(bool settings_invalid) {
559 bool print_preview_invalid_printer_settings =
560 (render_thread_->sink().GetUniqueMessageMatching(
561 PrintHostMsg_PrintPreviewInvalidPrinterSettings::ID) != NULL);
562 EXPECT_EQ(settings_invalid, print_preview_invalid_printer_settings);
565 // |page_number| is 0-based.
566 void VerifyDidPreviewPage(bool generate_draft_pages, int page_number) {
567 bool msg_found = false;
568 size_t msg_count = render_thread_->sink().message_count();
569 for (size_t i = 0; i < msg_count; ++i) {
570 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
571 if (msg->type() == PrintHostMsg_DidPreviewPage::ID) {
572 PrintHostMsg_DidPreviewPage::Param page_param;
573 PrintHostMsg_DidPreviewPage::Read(msg, &page_param);
574 if (base::get<0>(page_param).page_number == page_number) {
575 msg_found = true;
576 if (generate_draft_pages)
577 EXPECT_NE(0U, base::get<0>(page_param).data_size);
578 else
579 EXPECT_EQ(0U, base::get<0>(page_param).data_size);
580 break;
584 ASSERT_EQ(generate_draft_pages, msg_found);
587 void VerifyDefaultPageLayout(int content_width,
588 int content_height,
589 int margin_top,
590 int margin_bottom,
591 int margin_left,
592 int margin_right,
593 bool page_has_print_css) {
594 const IPC::Message* default_page_layout_msg =
595 render_thread_->sink().GetUniqueMessageMatching(
596 PrintHostMsg_DidGetDefaultPageLayout::ID);
597 bool did_get_default_page_layout_msg = (NULL != default_page_layout_msg);
598 if (did_get_default_page_layout_msg) {
599 PrintHostMsg_DidGetDefaultPageLayout::Param param;
600 PrintHostMsg_DidGetDefaultPageLayout::Read(default_page_layout_msg,
601 &param);
602 EXPECT_EQ(content_width, base::get<0>(param).content_width);
603 EXPECT_EQ(content_height, base::get<0>(param).content_height);
604 EXPECT_EQ(margin_top, base::get<0>(param).margin_top);
605 EXPECT_EQ(margin_right, base::get<0>(param).margin_right);
606 EXPECT_EQ(margin_left, base::get<0>(param).margin_left);
607 EXPECT_EQ(margin_bottom, base::get<0>(param).margin_bottom);
608 EXPECT_EQ(page_has_print_css, base::get<2>(param));
612 DISALLOW_COPY_AND_ASSIGN(MAYBE_PrintWebViewHelperPreviewTest);
615 #if defined(ENABLE_PRINT_PREVIEW)
616 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, BlockScriptInitiatedPrinting) {
617 LoadHTML(kHelloWorldHTML);
618 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_);
619 print_web_view_helper->SetScriptedPrintBlocked(true);
620 PrintWithJavaScript();
621 VerifyPreviewRequest(false);
623 print_web_view_helper->SetScriptedPrintBlocked(false);
624 PrintWithJavaScript();
625 VerifyPreviewRequest(true);
628 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintWithJavaScript) {
629 LoadHTML(kPrintOnUserAction);
630 gfx::Size new_size(200, 100);
631 Resize(new_size, gfx::Rect(), false);
633 gfx::Rect bounds = GetElementBounds("print");
634 EXPECT_FALSE(bounds.IsEmpty());
635 blink::WebMouseEvent mouse_event;
636 mouse_event.type = blink::WebInputEvent::MouseDown;
637 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
638 mouse_event.x = bounds.CenterPoint().x();
639 mouse_event.y = bounds.CenterPoint().y();
640 mouse_event.clickCount = 1;
641 SendWebMouseEvent(mouse_event);
642 mouse_event.type = blink::WebInputEvent::MouseUp;
643 SendWebMouseEvent(mouse_event);
645 VerifyPreviewRequest(true);
647 #endif // ENABLE_PRINT_PREVIEW
649 // Tests that print preview work and sending and receiving messages through
650 // that channel all works.
651 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintPreview) {
652 LoadHTML(kHelloWorldHTML);
654 // Fill in some dummy values.
655 base::DictionaryValue dict;
656 CreatePrintSettingsDictionary(&dict);
657 OnPrintPreview(dict);
659 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
660 VerifyDefaultPageLayout(540, 720, 36, 36, 36, 36, false);
661 VerifyPrintPreviewCancelled(false);
662 VerifyPrintPreviewFailed(false);
663 VerifyPrintPreviewGenerated(true);
664 VerifyPagesPrinted(false);
667 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
668 PrintPreviewHTMLWithPageMarginsCss) {
669 // A simple web page with print margins css.
670 const char kHTMLWithPageMarginsCss[] =
671 "<html><head><style>"
672 "@media print {"
673 " @page {"
674 " margin: 3in 1in 2in 0.3in;"
675 " }"
677 "</style></head>"
678 "<body>Lorem Ipsum:"
679 "</body></html>";
680 LoadHTML(kHTMLWithPageMarginsCss);
682 // Fill in some dummy values.
683 base::DictionaryValue dict;
684 CreatePrintSettingsDictionary(&dict);
685 dict.SetBoolean(kSettingPrintToPDF, false);
686 dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
687 OnPrintPreview(dict);
689 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
690 VerifyDefaultPageLayout(519, 432, 216, 144, 21, 72, false);
691 VerifyPrintPreviewCancelled(false);
692 VerifyPrintPreviewFailed(false);
693 VerifyPrintPreviewGenerated(true);
694 VerifyPagesPrinted(false);
697 // Test to verify that print preview ignores print media css when non-default
698 // margin is selected.
699 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
700 NonDefaultMarginsSelectedIgnorePrintCss) {
701 LoadHTML(kHTMLWithPageSizeCss);
703 // Fill in some dummy values.
704 base::DictionaryValue dict;
705 CreatePrintSettingsDictionary(&dict);
706 dict.SetBoolean(kSettingPrintToPDF, false);
707 dict.SetInteger(kSettingMarginsType, NO_MARGINS);
708 OnPrintPreview(dict);
710 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
711 VerifyDefaultPageLayout(612, 792, 0, 0, 0, 0, true);
712 VerifyPrintPreviewCancelled(false);
713 VerifyPrintPreviewFailed(false);
714 VerifyPrintPreviewGenerated(true);
715 VerifyPagesPrinted(false);
718 // Test to verify that print preview honor print media size css when
719 // PRINT_TO_PDF is selected and doesn't fit to printer default paper size.
720 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorPrintCss) {
721 LoadHTML(kHTMLWithPageSizeCss);
723 // Fill in some dummy values.
724 base::DictionaryValue dict;
725 CreatePrintSettingsDictionary(&dict);
726 dict.SetBoolean(kSettingPrintToPDF, true);
727 dict.SetInteger(kSettingMarginsType, PRINTABLE_AREA_MARGINS);
728 OnPrintPreview(dict);
730 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
731 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
732 // size.
733 VerifyDefaultPageLayout(252, 252, 18, 18, 18, 18, true);
734 VerifyPrintPreviewCancelled(false);
735 VerifyPrintPreviewFailed(false);
738 // Test to verify that print preview honor print margin css when PRINT_TO_PDF
739 // is selected and doesn't fit to printer default paper size.
740 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
741 PrintToPDFSelectedHonorPageMarginsCss) {
742 // A simple web page with print margins css.
743 const char kHTMLWithPageCss[] =
744 "<html><head><style>"
745 "@media print {"
746 " @page {"
747 " margin: 3in 1in 2in 0.3in;"
748 " size: 14in 14in;"
749 " }"
751 "</style></head>"
752 "<body>Lorem Ipsum:"
753 "</body></html>";
754 LoadHTML(kHTMLWithPageCss);
756 // Fill in some dummy values.
757 base::DictionaryValue dict;
758 CreatePrintSettingsDictionary(&dict);
759 dict.SetBoolean(kSettingPrintToPDF, true);
760 dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
761 OnPrintPreview(dict);
763 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
764 // Since PRINT_TO_PDF is selected, pdf page size is equal to print media page
765 // size.
766 VerifyDefaultPageLayout(915, 648, 216, 144, 21, 72, true);
767 VerifyPrintPreviewCancelled(false);
768 VerifyPrintPreviewFailed(false);
771 // Test to verify that print preview workflow center the html page contents to
772 // fit the page size.
773 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintPreviewCenterToFitPage) {
774 LoadHTML(kHTMLWithPageSizeCss);
776 // Fill in some dummy values.
777 base::DictionaryValue dict;
778 CreatePrintSettingsDictionary(&dict);
779 dict.SetBoolean(kSettingPrintToPDF, false);
780 dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
781 OnPrintPreview(dict);
783 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
784 VerifyDefaultPageLayout(216, 216, 288, 288, 198, 198, true);
785 VerifyPrintPreviewCancelled(false);
786 VerifyPrintPreviewFailed(false);
787 VerifyPrintPreviewGenerated(true);
790 // Test to verify that print preview workflow scale the html page contents to
791 // fit the page size.
792 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintPreviewShrinkToFitPage) {
793 // A simple web page with print margins css.
794 const char kHTMLWithPageCss[] =
795 "<html><head><style>"
796 "@media print {"
797 " @page {"
798 " size: 15in 17in;"
799 " }"
801 "</style></head>"
802 "<body>Lorem Ipsum:"
803 "</body></html>";
804 LoadHTML(kHTMLWithPageCss);
806 // Fill in some dummy values.
807 base::DictionaryValue dict;
808 CreatePrintSettingsDictionary(&dict);
809 dict.SetBoolean(kSettingPrintToPDF, false);
810 dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
811 OnPrintPreview(dict);
813 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
814 VerifyDefaultPageLayout(571, 652, 69, 71, 20, 21, true);
815 VerifyPrintPreviewCancelled(false);
816 VerifyPrintPreviewFailed(false);
819 // Test to verify that print preview workflow honor the orientation settings
820 // specified in css.
821 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, PrintPreviewHonorsOrientationCss) {
822 LoadHTML(kHTMLWithLandscapePageCss);
824 // Fill in some dummy values.
825 base::DictionaryValue dict;
826 CreatePrintSettingsDictionary(&dict);
827 dict.SetBoolean(kSettingPrintToPDF, false);
828 dict.SetInteger(kSettingMarginsType, NO_MARGINS);
829 OnPrintPreview(dict);
831 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
832 VerifyDefaultPageLayout(792, 612, 0, 0, 0, 0, true);
833 VerifyPrintPreviewCancelled(false);
834 VerifyPrintPreviewFailed(false);
837 // Test to verify that print preview workflow honors the orientation settings
838 // specified in css when PRINT_TO_PDF is selected.
839 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
840 PrintToPDFSelectedHonorOrientationCss) {
841 LoadHTML(kHTMLWithLandscapePageCss);
843 // Fill in some dummy values.
844 base::DictionaryValue dict;
845 CreatePrintSettingsDictionary(&dict);
846 dict.SetBoolean(kSettingPrintToPDF, true);
847 dict.SetInteger(kSettingMarginsType, CUSTOM_MARGINS);
848 OnPrintPreview(dict);
850 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
851 VerifyDefaultPageLayout(748, 568, 21, 23, 21, 23, true);
852 VerifyPrintPreviewCancelled(false);
853 VerifyPrintPreviewFailed(false);
856 // Test to verify that complete metafile is generated for a subset of pages
857 // without creating draft pages.
858 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) {
859 LoadHTML(kMultipageHTML);
861 // Fill in some dummy values.
862 base::DictionaryValue dict;
863 CreatePrintSettingsDictionary(&dict);
865 // Set a page range and update the dictionary to generate only the complete
866 // metafile with the selected pages. Page numbers used in the dictionary
867 // are 1-based.
868 base::DictionaryValue* page_range = new base::DictionaryValue();
869 page_range->SetInteger(kSettingPageRangeFrom, 2);
870 page_range->SetInteger(kSettingPageRangeTo, 3);
872 base::ListValue* page_range_array = new base::ListValue();
873 page_range_array->Append(page_range);
875 dict.Set(kSettingPageRange, page_range_array);
876 dict.SetBoolean(kSettingGenerateDraftData, false);
878 OnPrintPreview(dict);
880 VerifyDidPreviewPage(false, 0);
881 VerifyDidPreviewPage(false, 1);
882 VerifyDidPreviewPage(false, 2);
883 VerifyPreviewPageCount(3);
884 VerifyPrintPreviewCancelled(false);
885 VerifyPrintPreviewFailed(false);
886 VerifyPrintPreviewGenerated(true);
887 VerifyPagesPrinted(false);
890 // Test to verify that preview generated only for one page.
891 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedText) {
892 LoadHTML(kMultipageHTML);
893 GetMainFrame()->selectRange(
894 blink::WebRange::fromDocumentRange(GetMainFrame(), 1, 3));
896 // Fill in some dummy values.
897 base::DictionaryValue dict;
898 CreatePrintSettingsDictionary(&dict);
899 dict.SetBoolean(kSettingShouldPrintSelectionOnly, true);
901 OnPrintPreview(dict);
903 VerifyPreviewPageCount(1);
904 VerifyPrintPreviewCancelled(false);
905 VerifyPrintPreviewFailed(false);
906 VerifyPrintPreviewGenerated(true);
907 VerifyPagesPrinted(false);
910 // Tests that print preview fails and receiving error messages through
911 // that channel all works.
912 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintPreviewFail) {
913 LoadHTML(kHelloWorldHTML);
915 // An empty dictionary should fail.
916 base::DictionaryValue empty_dict;
917 OnPrintPreview(empty_dict);
919 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
920 VerifyPrintPreviewCancelled(false);
921 VerifyPrintPreviewFailed(true);
922 VerifyPrintPreviewGenerated(false);
923 VerifyPagesPrinted(false);
926 // Tests that cancelling print preview works.
927 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintPreviewCancel) {
928 LoadHTML(kLongPageHTML);
930 const int kCancelPage = 3;
931 print_render_thread_->set_print_preview_cancel_page_number(kCancelPage);
932 // Fill in some dummy values.
933 base::DictionaryValue dict;
934 CreatePrintSettingsDictionary(&dict);
935 OnPrintPreview(dict);
937 EXPECT_EQ(kCancelPage, print_render_thread_->print_preview_pages_remaining());
938 VerifyPrintPreviewCancelled(true);
939 VerifyPrintPreviewFailed(false);
940 VerifyPrintPreviewGenerated(false);
941 VerifyPagesPrinted(false);
944 // Tests that printing from print preview works and sending and receiving
945 // messages through that channel all works.
946 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintForPrintPreview) {
947 LoadHTML(kPrintPreviewHTML);
949 // Fill in some dummy values.
950 base::DictionaryValue dict;
951 CreatePrintSettingsDictionary(&dict);
952 OnPrintForPrintPreview(dict);
954 VerifyPrintFailed(false);
955 VerifyPagesPrinted(true);
958 // Tests that printing from print preview fails and receiving error messages
959 // through that channel all works.
960 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) {
961 LoadHTML(kPrintPreviewHTML);
963 // An empty dictionary should fail.
964 base::DictionaryValue empty_dict;
965 OnPrintForPrintPreview(empty_dict);
967 VerifyPagesPrinted(false);
970 // Tests that when default printer has invalid printer settings, print preview
971 // receives error message.
972 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
973 OnPrintPreviewUsingInvalidPrinterSettings) {
974 LoadHTML(kPrintPreviewHTML);
976 // Set mock printer to provide invalid settings.
977 print_render_thread_->printer()->UseInvalidSettings();
979 // Fill in some dummy values.
980 base::DictionaryValue dict;
981 CreatePrintSettingsDictionary(&dict);
982 OnPrintPreview(dict);
984 // We should have received invalid printer settings from |printer_|.
985 VerifyPrintPreviewInvalidPrinterSettings(true);
986 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
988 // It should receive the invalid printer settings message only.
989 VerifyPrintPreviewFailed(false);
990 VerifyPrintPreviewGenerated(false);
993 // Tests that when the selected printer has invalid page settings, print preview
994 // receives error message.
995 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
996 OnPrintPreviewUsingInvalidPageSize) {
997 LoadHTML(kPrintPreviewHTML);
999 print_render_thread_->printer()->UseInvalidPageSize();
1001 base::DictionaryValue dict;
1002 CreatePrintSettingsDictionary(&dict);
1003 OnPrintPreview(dict);
1005 VerifyPrintPreviewInvalidPrinterSettings(true);
1006 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
1008 // It should receive the invalid printer settings message only.
1009 VerifyPrintPreviewFailed(false);
1010 VerifyPrintPreviewGenerated(false);
1013 // Tests that when the selected printer has invalid content settings, print
1014 // preview receives error message.
1015 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
1016 OnPrintPreviewUsingInvalidContentSize) {
1017 LoadHTML(kPrintPreviewHTML);
1019 print_render_thread_->printer()->UseInvalidContentSize();
1021 base::DictionaryValue dict;
1022 CreatePrintSettingsDictionary(&dict);
1023 OnPrintPreview(dict);
1025 VerifyPrintPreviewInvalidPrinterSettings(true);
1026 EXPECT_EQ(0, print_render_thread_->print_preview_pages_remaining());
1028 // It should receive the invalid printer settings message only.
1029 VerifyPrintPreviewFailed(false);
1030 VerifyPrintPreviewGenerated(false);
1033 TEST_F(MAYBE_PrintWebViewHelperPreviewTest,
1034 OnPrintForPrintPreviewUsingInvalidPrinterSettings) {
1035 LoadHTML(kPrintPreviewHTML);
1037 // Set mock printer to provide invalid settings.
1038 print_render_thread_->printer()->UseInvalidSettings();
1040 // Fill in some dummy values.
1041 base::DictionaryValue dict;
1042 CreatePrintSettingsDictionary(&dict);
1043 OnPrintForPrintPreview(dict);
1045 VerifyPrintFailed(true);
1046 VerifyPagesPrinted(false);
1049 #endif // !defined(OS_CHROMEOS)
1051 } // namespace printing