| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "chrome/common/chrome_switches.h" | 6 #include "chrome/common/chrome_switches.h" |
| 7 #include "chrome/common/print_messages.h" | 7 #include "chrome/common/print_messages.h" |
| 8 #include "chrome/renderer/mock_printer.h" | 8 #include "chrome/renderer/mock_printer.h" |
| 9 #include "chrome/renderer/print_web_view_helper.h" | 9 #include "chrome/renderer/print_web_view_helper.h" |
| 10 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
| 11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
| 12 #include "printing/print_job_constants.h" | 12 #include "printing/print_job_constants.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 17 | 17 |
| 18 #if defined(OS_WIN) || defined(OS_MACOSX) | 18 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
| 20 #include "printing/image.h" | 20 #include "printing/image.h" |
| 21 | 21 |
| 22 using WebKit::WebFrame; | 22 using WebKit::WebFrame; |
| 23 using WebKit::WebString; | 23 using WebKit::WebString; |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 49 " }" | 49 " }" |
| 50 "}" | 50 "}" |
| 51 "</style></head>" | 51 "</style></head>" |
| 52 "<body>Lorem Ipsum:" | 52 "<body>Lorem Ipsum:" |
| 53 "</body></html>"; | 53 "</body></html>"; |
| 54 | 54 |
| 55 // A simple webpage that prints itself. | 55 // A simple webpage that prints itself. |
| 56 const char kPrintWithJSHTML[] = | 56 const char kPrintWithJSHTML[] = |
| 57 "<body>Hello<script>window.print()</script>World</body>"; | 57 "<body>Hello<script>window.print()</script>World</body>"; |
| 58 | 58 |
| 59 // A simple webpage with a button to print itself with. |
| 60 const char kPrintOnUserAction[] = |
| 61 "<body>" |
| 62 " <button id=\"print\" onclick=\"window.print();\">Hello World!</button>" |
| 63 "</body>"; |
| 64 |
| 59 // A longer web page. | 65 // A longer web page. |
| 60 const char kLongPageHTML[] = | 66 const char kLongPageHTML[] = |
| 61 "<body><img src=\"\" width=10 height=10000 /></body>"; | 67 "<body><img src=\"\" width=10 height=10000 /></body>"; |
| 62 | 68 |
| 63 // A web page to simulate the print preview page. | 69 // A web page to simulate the print preview page. |
| 64 const char kPrintPreviewHTML[] = | 70 const char kPrintPreviewHTML[] = |
| 65 "<body><p id=\"pdf-viewer\">Hello World!</p></body>"; | 71 "<body><p id=\"pdf-viewer\">Hello World!</p></body>"; |
| 66 | 72 |
| 67 void CreatePrintSettingsDictionary(DictionaryValue* dict) { | 73 void CreatePrintSettingsDictionary(DictionaryValue* dict) { |
| 68 dict->SetBoolean(printing::kSettingLandscape, false); | 74 dict->SetBoolean(printing::kSettingLandscape, false); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 VerifyPagesPrinted(false); | 197 VerifyPagesPrinted(false); |
| 192 | 198 |
| 193 // Unblock script initiated printing and verify printing works. | 199 // Unblock script initiated printing and verify printing works. |
| 194 PrintWebViewHelper::Get(view_)->ResetScriptedPrintCount(); | 200 PrintWebViewHelper::Get(view_)->ResetScriptedPrintCount(); |
| 195 chrome_render_thread_->printer()->ResetPrinter(); | 201 chrome_render_thread_->printer()->ResetPrinter(); |
| 196 LoadHTML(kPrintWithJSHTML); | 202 LoadHTML(kPrintWithJSHTML); |
| 197 VerifyPageCount(1); | 203 VerifyPageCount(1); |
| 198 VerifyPagesPrinted(true); | 204 VerifyPagesPrinted(true); |
| 199 } | 205 } |
| 200 | 206 |
| 207 // Tests that the renderer always allows window.print() calls if they are user |
| 208 // initiated. |
| 209 TEST_F(PrintWebViewHelperTest, AllowUserOriginatedPrinting) { |
| 210 // Pretend user will cancel printing. |
| 211 chrome_render_thread_->set_print_dialog_user_response(false); |
| 212 // Try to print with window.print() a few times. |
| 213 LoadHTML(kPrintWithJSHTML); |
| 214 LoadHTML(kPrintWithJSHTML); |
| 215 LoadHTML(kPrintWithJSHTML); |
| 216 VerifyPagesPrinted(false); |
| 217 |
| 218 // Pretend user will print. (but printing is blocked.) |
| 219 chrome_render_thread_->set_print_dialog_user_response(true); |
| 220 LoadHTML(kPrintWithJSHTML); |
| 221 VerifyPagesPrinted(false); |
| 222 |
| 223 // Try again as if user initiated, without resetting the print count. |
| 224 chrome_render_thread_->printer()->ResetPrinter(); |
| 225 LoadHTML(kPrintOnUserAction); |
| 226 gfx::Size newSize(200,100); |
| 227 Resize(newSize, gfx::Rect(), false); |
| 228 |
| 229 gfx::Rect bounds = GetElementBounds("print"); |
| 230 EXPECT_FALSE(bounds.IsEmpty()); |
| 231 WebKit::WebMouseEvent mouse_event; |
| 232 mouse_event.type = WebKit::WebInputEvent::MouseDown; |
| 233 mouse_event.button = WebKit::WebMouseEvent::ButtonLeft; |
| 234 mouse_event.x = bounds.CenterPoint().x(); |
| 235 mouse_event.y = bounds.CenterPoint().y(); |
| 236 mouse_event.clickCount = 1; |
| 237 SendWebMouseEvent(mouse_event); |
| 238 mouse_event.type = WebKit::WebInputEvent::MouseUp; |
| 239 SendWebMouseEvent(mouse_event); |
| 240 |
| 241 VerifyPageCount(1); |
| 242 VerifyPagesPrinted(true); |
| 243 } |
| 244 |
| 201 TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrintingFromPopup) { | 245 TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrintingFromPopup) { |
| 202 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); | 246 PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); |
| 203 print_web_view_helper->SetScriptedPrintBlocked(true); | 247 print_web_view_helper->SetScriptedPrintBlocked(true); |
| 204 LoadHTML(kPrintWithJSHTML); | 248 LoadHTML(kPrintWithJSHTML); |
| 205 VerifyPagesPrinted(false); | 249 VerifyPagesPrinted(false); |
| 206 | 250 |
| 207 print_web_view_helper->SetScriptedPrintBlocked(false); | 251 print_web_view_helper->SetScriptedPrintBlocked(false); |
| 208 LoadHTML(kPrintWithJSHTML); | 252 LoadHTML(kPrintWithJSHTML); |
| 209 VerifyPageCount(1); | 253 VerifyPageCount(1); |
| 210 VerifyPagesPrinted(true); | 254 VerifyPagesPrinted(true); |
| (...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 // Fill in some dummy values. | 867 // Fill in some dummy values. |
| 824 DictionaryValue dict; | 868 DictionaryValue dict; |
| 825 CreatePrintSettingsDictionary(&dict); | 869 CreatePrintSettingsDictionary(&dict); |
| 826 OnPrintForPrintPreview(dict); | 870 OnPrintForPrintPreview(dict); |
| 827 | 871 |
| 828 VerifyPrintFailed(true); | 872 VerifyPrintFailed(true); |
| 829 VerifyPagesPrinted(false); | 873 VerifyPagesPrinted(false); |
| 830 } | 874 } |
| 831 | 875 |
| 832 #endif // !defined(OS_CHROMEOS) | 876 #endif // !defined(OS_CHROMEOS) |
| 877 |
| 878 class PrintWebViewHelperKioskTest : public PrintWebViewHelperTestBase { |
| 879 public: |
| 880 PrintWebViewHelperKioskTest() {} |
| 881 virtual ~PrintWebViewHelperKioskTest() {} |
| 882 |
| 883 virtual void SetUp() OVERRIDE { |
| 884 // Append the throttling disable switch before creating the |
| 885 // PrintWebViewHelper. |
| 886 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 887 switches::kDisableScriptedPrintThrottling); |
| 888 |
| 889 ChromeRenderViewTest::SetUp(); |
| 890 } |
| 891 |
| 892 protected: |
| 893 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperKioskTest); |
| 894 }; |
| 895 |
| 896 // Tests that the switch overrides the throttling that blocks window.print() |
| 897 // calls if they occur too frequently. Compare with |
| 898 // PrintWebViewHelperTest.BlockScriptInitiatedPrinting above. |
| 899 TEST_F(PrintWebViewHelperKioskTest, DontBlockScriptInitiatedPrinting) { |
| 900 // Pretend user will cancel printing. |
| 901 chrome_render_thread_->set_print_dialog_user_response(false); |
| 902 // Try to print with window.print() a few times. |
| 903 LoadHTML(kPrintWithJSHTML); |
| 904 chrome_render_thread_->printer()->ResetPrinter(); |
| 905 LoadHTML(kPrintWithJSHTML); |
| 906 chrome_render_thread_->printer()->ResetPrinter(); |
| 907 LoadHTML(kPrintWithJSHTML); |
| 908 chrome_render_thread_->printer()->ResetPrinter(); |
| 909 VerifyPagesPrinted(false); |
| 910 |
| 911 // Pretend user will print, should not be throttled. |
| 912 chrome_render_thread_->set_print_dialog_user_response(true); |
| 913 LoadHTML(kPrintWithJSHTML); |
| 914 VerifyPagesPrinted(true); |
| 915 } |
| OLD | NEW |