This source file includes following definitions.
- CreatePrintSettingsDictionary
- PrintWithJavaScript
- VerifyPageCount
- VerifyPreviewPageCount
- VerifyPagesPrinted
- OnPrintPages
- OnPrintPreview
- OnPrintForPrintPreview
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- VerifyPrintPreviewCancelled
- VerifyPrintPreviewFailed
- VerifyPrintPreviewGenerated
- VerifyPrintFailed
- VerifyPrintPreviewInvalidPrinterSettings
- VerifyDidPreviewPage
- VerifyDefaultPageLayout
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TEST_F
#include "base/command_line.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/print_messages.h"
#include "chrome/renderer/mock_printer.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
#include "chrome/test/base/chrome_render_view_test.h"
#include "content/public/renderer/render_view.h"
#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebRange.h"
#include "third_party/WebKit/public/web/WebView.h"
#if defined(OS_WIN) || defined(OS_MACOSX)
#include "base/file_util.h"
#include "printing/image.h"
using blink::WebFrame;
using blink::WebString;
#endif
namespace printing {
namespace {
const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>";
const char kPrintOnUserAction[] =
"<body>"
" <button id=\"print\" onclick=\"window.print();\">Hello World!</button>"
"</body>";
#if !defined(OS_CHROMEOS)
const char kMultipageHTML[] =
"<html><head><style>"
".break { page-break-after: always; }"
"</style></head>"
"<body>"
"<div class='break'>page1</div>"
"<div class='break'>page2</div>"
"<div>page3</div>"
"</body></html>";
const char kHTMLWithPageSizeCss[] =
"<html><head><style>"
"@media print {"
" @page {"
" size: 4in 4in;"
" }"
"}"
"</style></head>"
"<body>Lorem Ipsum:"
"</body></html>";
const char kHTMLWithLandscapePageCss[] =
"<html><head><style>"
"@media print {"
" @page {"
" size: landscape;"
" }"
"}"
"</style></head>"
"<body>Lorem Ipsum:"
"</body></html>";
const char kLongPageHTML[] =
"<body><img src=\"\" width=10 height=10000 /></body>";
const char kPrintPreviewHTML[] =
"<body><p id=\"pdf-viewer\">Hello World!</p></body>";
void CreatePrintSettingsDictionary(base::DictionaryValue* dict) {
dict->SetBoolean(kSettingLandscape, false);
dict->SetBoolean(kSettingCollate, false);
dict->SetInteger(kSettingColor, GRAY);
dict->SetBoolean(kSettingPrintToPDF, true);
dict->SetInteger(kSettingDuplexMode, SIMPLEX);
dict->SetInteger(kSettingCopies, 1);
dict->SetString(kSettingDeviceName, "dummy");
dict->SetInteger(kPreviewUIID, 4);
dict->SetInteger(kPreviewRequestID, 12345);
dict->SetBoolean(kIsFirstRequest, true);
dict->SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
dict->SetBoolean(kSettingPreviewModifiable, false);
dict->SetBoolean(kSettingHeaderFooterEnabled, false);
dict->SetBoolean(kSettingGenerateDraftData, true);
dict->SetBoolean(kSettingShouldPrintBackgrounds, false);
dict->SetBoolean(kSettingShouldPrintSelectionOnly, false);
}
#endif
}
class PrintWebViewHelperTestBase : public ChromeRenderViewTest {
public:
PrintWebViewHelperTestBase() {}
virtual ~PrintWebViewHelperTestBase() {}
protected:
void PrintWithJavaScript() {
ExecuteJavaScript("window.print();");
ProcessPendingMessages();
}
void VerifyPageCount(int count) {
#if defined(OS_CHROMEOS)
#else
const IPC::Message* page_cnt_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_DidGetPrintedPagesCount::ID);
ASSERT_TRUE(page_cnt_msg);
PrintHostMsg_DidGetPrintedPagesCount::Param post_page_count_param;
PrintHostMsg_DidGetPrintedPagesCount::Read(page_cnt_msg,
&post_page_count_param);
EXPECT_EQ(count, post_page_count_param.b);
#endif
}
void VerifyPreviewPageCount(int count) {
const IPC::Message* page_cnt_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_DidGetPreviewPageCount::ID);
ASSERT_TRUE(page_cnt_msg);
PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param;
PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg,
&post_page_count_param);
EXPECT_EQ(count, post_page_count_param.a.page_count);
}
void VerifyPagesPrinted(bool printed) {
#if defined(OS_CHROMEOS)
bool did_print_msg = (render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_TempFileForPrintingWritten::ID) != NULL);
ASSERT_EQ(printed, did_print_msg);
#else
const IPC::Message* print_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_DidPrintPage::ID);
bool did_print_msg = (NULL != print_msg);
ASSERT_EQ(printed, did_print_msg);
if (printed) {
PrintHostMsg_DidPrintPage::Param post_did_print_page_param;
PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param);
EXPECT_EQ(0, post_did_print_page_param.a.page_number);
}
#endif
}
void OnPrintPages() {
PrintWebViewHelper::Get(view_)->OnPrintPages();
ProcessPendingMessages();
}
void OnPrintPreview(const base::DictionaryValue& dict) {
PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_);
print_web_view_helper->OnInitiatePrintPreview(false);
print_web_view_helper->OnPrintPreview(dict);
ProcessPendingMessages();
}
void OnPrintForPrintPreview(const base::DictionaryValue& dict) {
PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict);
ProcessPendingMessages();
}
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase);
};
class PrintWebViewHelperTest : public PrintWebViewHelperTestBase {
public:
PrintWebViewHelperTest() {}
virtual ~PrintWebViewHelperTest() {}
virtual void SetUp() OVERRIDE {
ChromeRenderViewTest::SetUp();
}
protected:
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTest);
};
TEST_F(PrintWebViewHelperTest, OnPrintPages) {
LoadHTML(kHelloWorldHTML);
OnPrintPages();
VerifyPageCount(1);
VerifyPagesPrinted(true);
}
TEST_F(PrintWebViewHelperTest, PrintWithJavascript) {
PrintWithJavaScript();
VerifyPageCount(1);
VerifyPagesPrinted(true);
}
TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrinting) {
chrome_render_thread_->set_print_dialog_user_response(false);
PrintWithJavaScript();
PrintWithJavaScript();
PrintWithJavaScript();
VerifyPagesPrinted(false);
chrome_render_thread_->set_print_dialog_user_response(true);
PrintWithJavaScript();
VerifyPagesPrinted(false);
PrintWebViewHelper::Get(view_)->ResetScriptedPrintCount();
chrome_render_thread_->printer()->ResetPrinter();
PrintWithJavaScript();
VerifyPageCount(1);
VerifyPagesPrinted(true);
}
TEST_F(PrintWebViewHelperTest, AllowUserOriginatedPrinting) {
chrome_render_thread_->set_print_dialog_user_response(false);
PrintWithJavaScript();
PrintWithJavaScript();
PrintWithJavaScript();
VerifyPagesPrinted(false);
chrome_render_thread_->set_print_dialog_user_response(true);
PrintWithJavaScript();
VerifyPagesPrinted(false);
chrome_render_thread_->printer()->ResetPrinter();
LoadHTML(kPrintOnUserAction);
gfx::Size new_size(200, 100);
Resize(new_size, gfx::Rect(), false);
gfx::Rect bounds = GetElementBounds("print");
EXPECT_FALSE(bounds.IsEmpty());
blink::WebMouseEvent mouse_event;
mouse_event.type = blink::WebInputEvent::MouseDown;
mouse_event.button = blink::WebMouseEvent::ButtonLeft;
mouse_event.x = bounds.CenterPoint().x();
mouse_event.y = bounds.CenterPoint().y();
mouse_event.clickCount = 1;
SendWebMouseEvent(mouse_event);
mouse_event.type = blink::WebInputEvent::MouseUp;
SendWebMouseEvent(mouse_event);
ProcessPendingMessages();
VerifyPageCount(1);
VerifyPagesPrinted(true);
}
TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrintingFromPopup) {
PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_);
print_web_view_helper->SetScriptedPrintBlocked(true);
PrintWithJavaScript();
VerifyPagesPrinted(false);
print_web_view_helper->SetScriptedPrintBlocked(false);
PrintWithJavaScript();
VerifyPageCount(1);
VerifyPagesPrinted(true);
}
#if defined(OS_WIN) || defined(OS_MACOSX)
TEST_F(PrintWebViewHelperTest, PrintWithIframe) {
const char html[] =
"<html><body>Lorem Ipsum:"
"<iframe name=\"sub1\" id=\"sub1\"></iframe><script>"
" document.write(frames['sub1'].name);"
" frames['sub1'].document.write("
" '<p>Cras tempus ante eu felis semper luctus!</p>');"
"</script></body></html>";
LoadHTML(html);
WebFrame* sub1_frame =
view_->GetWebView()->findFrameByName(WebString::fromUTF8("sub1"));
ASSERT_TRUE(sub1_frame);
view_->GetWebView()->setFocusedFrame(sub1_frame);
ASSERT_NE(view_->GetWebView()->focusedFrame(),
view_->GetWebView()->mainFrame());
OnPrintPages();
VerifyPagesPrinted(true);
const MockPrinter* printer(chrome_render_thread_->printer());
ASSERT_EQ(1, printer->GetPrintedPages());
const Image& image1(printer->GetPrintedPage(0)->image());
EXPECT_NE(0, image1.size().width());
EXPECT_NE(0, image1.size().height());
}
#endif
namespace {
struct TestPageData {
const char* page;
size_t printed_pages;
int width;
int height;
const char* checksum;
const wchar_t* file;
};
#if defined(OS_WIN) || defined(OS_MACOSX)
const TestPageData kTestPages[] = {
{"<html>"
"<head>"
"<meta"
" http-equiv=\"Content-Type\""
" content=\"text/html; charset=utf-8\"/>"
"<title>Test 1</title>"
"</head>"
"<body style=\"background-color: white;\">"
"<p style=\"font-family: arial;\">Hello World!</p>"
"</body>",
#if defined(OS_MACOSX)
1, 600, 780,
#else
1, 675, 900,
#endif
NULL,
NULL,
},
};
#endif
}
#if defined(OS_WIN) || defined(OS_MACOSX)
TEST_F(PrintWebViewHelperTest, PrintLayoutTest) {
bool baseline = false;
EXPECT_TRUE(chrome_render_thread_->printer() != NULL);
for (size_t i = 0; i < arraysize(kTestPages); ++i) {
LoadHTML(kTestPages[i].page);
OnPrintPages();
VerifyPagesPrinted(true);
size_t pages = chrome_render_thread_->printer()->GetPrintedPages();
EXPECT_EQ(kTestPages[i].printed_pages, pages);
int width = chrome_render_thread_->printer()->GetWidth(0);
int height = chrome_render_thread_->printer()->GetHeight(0);
const int kErrorMargin = 5;
EXPECT_GT(kTestPages[i].width * (100 + kErrorMargin) / 100, width);
EXPECT_LT(kTestPages[i].width * (100 - kErrorMargin) / 100, width);
EXPECT_GT(kTestPages[i].height * (100 + kErrorMargin) / 100, height);
EXPECT_LT(kTestPages[i].height* (100 - kErrorMargin) / 100, height);
std::string bitmap_actual;
EXPECT_TRUE(
chrome_render_thread_->printer()->GetBitmapChecksum(0, &bitmap_actual));
if (kTestPages[i].checksum)
EXPECT_EQ(kTestPages[i].checksum, bitmap_actual);
if (baseline) {
base::FilePath source_path;
base::CreateTemporaryFile(&source_path);
chrome_render_thread_->printer()->SaveSource(0, source_path);
base::FilePath bitmap_path;
base::CreateTemporaryFile(&bitmap_path);
chrome_render_thread_->printer()->SaveBitmap(0, bitmap_path);
}
}
}
#endif
#if !defined(OS_CHROMEOS)
class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase {
public:
PrintWebViewHelperPreviewTest() {}
virtual ~PrintWebViewHelperPreviewTest() {}
virtual void SetUp() OVERRIDE {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kRendererPrintPreview);
ChromeRenderViewTest::SetUp();
}
protected:
void VerifyPrintPreviewCancelled(bool did_cancel) {
bool print_preview_cancelled =
(render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_PrintPreviewCancelled::ID) != NULL);
EXPECT_EQ(did_cancel, print_preview_cancelled);
}
void VerifyPrintPreviewFailed(bool did_fail) {
bool print_preview_failed =
(render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_PrintPreviewFailed::ID) != NULL);
EXPECT_EQ(did_fail, print_preview_failed);
}
void VerifyPrintPreviewGenerated(bool generated_preview) {
const IPC::Message* preview_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_MetafileReadyForPrinting::ID);
bool did_get_preview_msg = (NULL != preview_msg);
ASSERT_EQ(generated_preview, did_get_preview_msg);
if (did_get_preview_msg) {
PrintHostMsg_MetafileReadyForPrinting::Param preview_param;
PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg, &preview_param);
EXPECT_NE(0, preview_param.a.document_cookie);
EXPECT_NE(0, preview_param.a.expected_pages_count);
EXPECT_NE(0U, preview_param.a.data_size);
}
}
void VerifyPrintFailed(bool did_fail) {
bool print_failed = (render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_PrintingFailed::ID) != NULL);
EXPECT_EQ(did_fail, print_failed);
}
void VerifyPrintPreviewInvalidPrinterSettings(bool settings_invalid) {
bool print_preview_invalid_printer_settings =
(render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_PrintPreviewInvalidPrinterSettings::ID) != NULL);
EXPECT_EQ(settings_invalid, print_preview_invalid_printer_settings);
}
void VerifyDidPreviewPage(bool generate_draft_pages, int page_number) {
bool msg_found = false;
size_t msg_count = render_thread_->sink().message_count();
for (size_t i = 0; i < msg_count; ++i) {
const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
if (msg->type() == PrintHostMsg_DidPreviewPage::ID) {
PrintHostMsg_DidPreviewPage::Param page_param;
PrintHostMsg_DidPreviewPage::Read(msg, &page_param);
if (page_param.a.page_number == page_number) {
msg_found = true;
if (generate_draft_pages)
EXPECT_NE(0U, page_param.a.data_size);
else
EXPECT_EQ(0U, page_param.a.data_size);
break;
}
}
}
ASSERT_EQ(generate_draft_pages, msg_found);
}
void VerifyDefaultPageLayout(int content_width, int content_height,
int margin_top, int margin_bottom,
int margin_left, int margin_right,
bool page_has_print_css) {
const IPC::Message* default_page_layout_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_DidGetDefaultPageLayout::ID);
bool did_get_default_page_layout_msg = (NULL != default_page_layout_msg);
if (did_get_default_page_layout_msg) {
PrintHostMsg_DidGetDefaultPageLayout::Param param;
PrintHostMsg_DidGetDefaultPageLayout::Read(default_page_layout_msg,
¶m);
EXPECT_EQ(content_width, param.a.content_width);
EXPECT_EQ(content_height, param.a.content_height);
EXPECT_EQ(margin_top, param.a.margin_top);
EXPECT_EQ(margin_right, param.a.margin_right);
EXPECT_EQ(margin_left, param.a.margin_left);
EXPECT_EQ(margin_bottom, param.a.margin_bottom);
EXPECT_EQ(page_has_print_css, param.c);
}
}
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest);
};
TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) {
LoadHTML(kHelloWorldHTML);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(540, 720, 36, 36, 36, 36, false);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewHTMLWithPageMarginsCss) {
const char kHTMLWithPageMarginsCss[] =
"<html><head><style>"
"@media print {"
" @page {"
" margin: 3in 1in 2in 0.3in;"
" }"
"}"
"</style></head>"
"<body>Lorem Ipsum:"
"</body></html>";
LoadHTML(kHTMLWithPageMarginsCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, false);
dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(519, 432, 216, 144, 21, 72, false);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, NonDefaultMarginsSelectedIgnorePrintCss) {
LoadHTML(kHTMLWithPageSizeCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, false);
dict.SetInteger(kSettingMarginsType, NO_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(612, 792, 0, 0, 0, 0, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorPrintCss) {
LoadHTML(kHTMLWithPageSizeCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, true);
dict.SetInteger(kSettingMarginsType,
PRINTABLE_AREA_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(252, 252, 18, 18, 18, 18, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorPageMarginsCss) {
const char kHTMLWithPageCss[] =
"<html><head><style>"
"@media print {"
" @page {"
" margin: 3in 1in 2in 0.3in;"
" size: 14in 14in;"
" }"
"}"
"</style></head>"
"<body>Lorem Ipsum:"
"</body></html>";
LoadHTML(kHTMLWithPageCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, true);
dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(915, 648, 216, 144, 21, 72, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewCenterToFitPage) {
LoadHTML(kHTMLWithPageSizeCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, false);
dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(216, 216, 288, 288, 198, 198, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewShrinkToFitPage) {
const char kHTMLWithPageCss[] =
"<html><head><style>"
"@media print {"
" @page {"
" size: 15in 17in;"
" }"
"}"
"</style></head>"
"<body>Lorem Ipsum:"
"</body></html>";
LoadHTML(kHTMLWithPageCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, false);
dict.SetInteger(kSettingMarginsType, DEFAULT_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(571, 652, 69, 71, 20, 21, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintPreviewHonorsOrientationCss) {
LoadHTML(kHTMLWithLandscapePageCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, false);
dict.SetInteger(kSettingMarginsType, NO_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(792, 612, 0, 0, 0, 0, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
}
TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorOrientationCss) {
LoadHTML(kHTMLWithLandscapePageCss);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingPrintToPDF, true);
dict.SetInteger(kSettingMarginsType, CUSTOM_MARGINS);
OnPrintPreview(dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyDefaultPageLayout(748, 568, 21, 23, 21, 23, true);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) {
LoadHTML(kMultipageHTML);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
base::DictionaryValue* page_range = new base::DictionaryValue();
page_range->SetInteger(kSettingPageRangeFrom, 2);
page_range->SetInteger(kSettingPageRangeTo, 3);
base::ListValue* page_range_array = new base::ListValue();
page_range_array->Append(page_range);
dict.Set(kSettingPageRange, page_range_array);
dict.SetBoolean(kSettingGenerateDraftData, false);
OnPrintPreview(dict);
VerifyDidPreviewPage(false, 0);
VerifyDidPreviewPage(false, 1);
VerifyDidPreviewPage(false, 2);
VerifyPreviewPageCount(3);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedText) {
LoadHTML(kMultipageHTML);
GetMainFrame()->selectRange(
blink::WebRange::fromDocumentRange(GetMainFrame(), 1, 3));
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
dict.SetBoolean(kSettingShouldPrintSelectionOnly, true);
OnPrintPreview(dict);
VerifyPreviewPageCount(1);
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(true);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) {
LoadHTML(kHelloWorldHTML);
base::DictionaryValue empty_dict;
OnPrintPreview(empty_dict);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyPrintPreviewCancelled(false);
VerifyPrintPreviewFailed(true);
VerifyPrintPreviewGenerated(false);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewCancel) {
LoadHTML(kLongPageHTML);
const int kCancelPage = 3;
chrome_render_thread_->set_print_preview_cancel_page_number(kCancelPage);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintPreview(dict);
EXPECT_EQ(kCancelPage,
chrome_render_thread_->print_preview_pages_remaining());
VerifyPrintPreviewCancelled(true);
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(false);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreview) {
LoadHTML(kPrintPreviewHTML);
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintForPrintPreview(dict);
VerifyPrintFailed(false);
VerifyPagesPrinted(true);
}
TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) {
LoadHTML(kPrintPreviewHTML);
base::DictionaryValue empty_dict;
OnPrintForPrintPreview(empty_dict);
VerifyPagesPrinted(false);
}
TEST_F(PrintWebViewHelperPreviewTest,
OnPrintPreviewUsingInvalidPrinterSettings) {
LoadHTML(kPrintPreviewHTML);
chrome_render_thread_->printer()->UseInvalidSettings();
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintPreview(dict);
VerifyPrintPreviewInvalidPrinterSettings(true);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(false);
}
TEST_F(PrintWebViewHelperPreviewTest,
OnPrintPreviewUsingInvalidPageSize) {
LoadHTML(kPrintPreviewHTML);
chrome_render_thread_->printer()->UseInvalidPageSize();
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintPreview(dict);
VerifyPrintPreviewInvalidPrinterSettings(true);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(false);
}
TEST_F(PrintWebViewHelperPreviewTest,
OnPrintPreviewUsingInvalidContentSize) {
LoadHTML(kPrintPreviewHTML);
chrome_render_thread_->printer()->UseInvalidContentSize();
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintPreview(dict);
VerifyPrintPreviewInvalidPrinterSettings(true);
EXPECT_EQ(0, chrome_render_thread_->print_preview_pages_remaining());
VerifyPrintPreviewFailed(false);
VerifyPrintPreviewGenerated(false);
}
TEST_F(PrintWebViewHelperPreviewTest,
OnPrintForPrintPreviewUsingInvalidPrinterSettings) {
LoadHTML(kPrintPreviewHTML);
chrome_render_thread_->printer()->UseInvalidSettings();
base::DictionaryValue dict;
CreatePrintSettingsDictionary(&dict);
OnPrintForPrintPreview(dict);
VerifyPrintFailed(true);
VerifyPagesPrinted(false);
}
#endif
class PrintWebViewHelperKioskTest : public PrintWebViewHelperTestBase {
public:
PrintWebViewHelperKioskTest() {}
virtual ~PrintWebViewHelperKioskTest() {}
virtual void SetUp() OVERRIDE {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisableScriptedPrintThrottling);
ChromeRenderViewTest::SetUp();
}
protected:
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperKioskTest);
};
TEST_F(PrintWebViewHelperKioskTest, DontBlockScriptInitiatedPrinting) {
chrome_render_thread_->set_print_dialog_user_response(false);
PrintWithJavaScript();
chrome_render_thread_->printer()->ResetPrinter();
PrintWithJavaScript();
chrome_render_thread_->printer()->ResetPrinter();
PrintWithJavaScript();
chrome_render_thread_->printer()->ResetPrinter();
VerifyPagesPrinted(false);
chrome_render_thread_->set_print_dialog_user_response(true);
PrintWithJavaScript();
VerifyPagesPrinted(true);
}
}