Index: chrome/renderer/printing/print_web_view_helper_browsertest.cc |
diff --git a/chrome/renderer/printing/print_web_view_helper_browsertest.cc b/chrome/renderer/printing/print_web_view_helper_browsertest.cc |
index 55a8ae5bdb9ccd75d9ec5544733d31fa7e612f96..3c777bd6058c7ece1ad6e5ed035e7a6c601b8961 100644 |
--- a/chrome/renderer/printing/print_web_view_helper_browsertest.cc |
+++ b/chrome/renderer/printing/print_web_view_helper_browsertest.cc |
@@ -236,6 +236,80 @@ class PrintWebViewHelperTest : public PrintWebViewHelperTestBase { |
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTest); |
}; |
+// This tests only for platforms without print preview. |
+#if !defined(ENABLE_FULL_PRINTING) |
+// Tests that the renderer blocks window.print() calls if they occur too |
+// frequently. |
+TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrinting) { |
+ // Pretend user will cancel printing. |
+ chrome_render_thread_->set_print_dialog_user_response(false); |
+ // Try to print with window.print() a few times. |
+ PrintWithJavaScript(); |
+ PrintWithJavaScript(); |
+ PrintWithJavaScript(); |
+ VerifyPagesPrinted(false); |
+ |
+ // Pretend user will print. (but printing is blocked.) |
+ chrome_render_thread_->set_print_dialog_user_response(true); |
+ PrintWithJavaScript(); |
+ VerifyPagesPrinted(false); |
+ |
+ // Unblock script initiated printing and verify printing works. |
+ PrintWebViewHelper::Get(view_)->scripting_throttler_.Reset(); |
+ chrome_render_thread_->printer()->ResetPrinter(); |
+ PrintWithJavaScript(); |
+ VerifyPageCount(1); |
+ VerifyPagesPrinted(true); |
+} |
+ |
+// Tests that the renderer always allows window.print() calls if they are user |
+// initiated. |
+TEST_F(PrintWebViewHelperTest, AllowUserOriginatedPrinting) { |
+ // Pretend user will cancel printing. |
+ chrome_render_thread_->set_print_dialog_user_response(false); |
+ // Try to print with window.print() a few times. |
+ PrintWithJavaScript(); |
+ PrintWithJavaScript(); |
+ PrintWithJavaScript(); |
+ VerifyPagesPrinted(false); |
+ |
+ // Pretend user will print. (but printing is blocked.) |
+ chrome_render_thread_->set_print_dialog_user_response(true); |
+ PrintWithJavaScript(); |
+ VerifyPagesPrinted(false); |
+ |
+ // Try again as if user initiated, without resetting the print count. |
+ 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); |
+} |
+ |
+// Duplicate of OnPrintPagesTest only using javascript to print. |
+TEST_F(PrintWebViewHelperTest, PrintWithJavascript) { |
+ PrintWithJavaScript(); |
+ |
+ VerifyPageCount(1); |
+ VerifyPagesPrinted(true); |
+} |
+#endif // !ENABLE_FULL_PRINTING |
+ |
#if !defined(DISABLE_BASIC_PRINTING) |
// Tests that printing pages work and sending and receiving messages through |
// that channel all works. |
@@ -487,6 +561,7 @@ class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase { |
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); |
}; |
+#if defined(ENABLE_FULL_PRINTING) |
TEST_F(PrintWebViewHelperPreviewTest, BlockScriptInitiatedPrinting) { |
PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); |
print_web_view_helper->SetScriptedPrintBlocked(true); |
@@ -517,6 +592,7 @@ TEST_F(PrintWebViewHelperPreviewTest, PrintWithJavaScript) { |
VerifyPreviewRequest(true); |
} |
+#endif // ENABLE_FULL_PRINTING |
// Tests that print preview work and sending and receiving messages through |
// that channel all works. |