summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
Diffstat (limited to 'chrome')
-rw-r--r--chrome/renderer/mock_printer.cc32
-rw-r--r--chrome/renderer/mock_printer.h5
-rw-r--r--chrome/renderer/mock_render_thread.cc25
-rw-r--r--chrome/renderer/mock_render_thread.h9
-rw-r--r--chrome/renderer/print_web_view_helper.h11
-rw-r--r--chrome/renderer/print_web_view_helper_browsertest.cc154
-rw-r--r--chrome/test/render_view_test.cc36
-rw-r--r--chrome/test/render_view_test.h7
8 files changed, 209 insertions, 70 deletions
diff --git a/chrome/renderer/mock_printer.cc b/chrome/renderer/mock_printer.cc
index 1a9236c..3117a87 100644
--- a/chrome/renderer/mock_printer.cc
+++ b/chrome/renderer/mock_printer.cc
@@ -59,16 +59,7 @@ void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params* params) {
// Assign a unit document cookie and set the print settings.
document_cookie_ = CreateDocumentCookie();
memset(params, 0, sizeof(PrintMsg_Print_Params));
- params->dpi = dpi_;
- params->max_shrink = max_shrink_;
- params->min_shrink = min_shrink_;
- params->desired_dpi = desired_dpi_;
- params->selection_only = selection_only_;
- params->document_cookie = document_cookie_;
- params->page_size = page_size_;
- params->printable_size = printable_size_;
- params->margin_left = margin_left_;
- params->margin_top = margin_top_;
+ SetPrintParams(params);
}
void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) {
@@ -102,6 +93,14 @@ void MockPrinter::ScriptedPrint(int cookie,
printer_status_ = PRINTER_PRINTING;
}
+void MockPrinter::UpdateSettings(int cookie,
+ PrintMsg_PrintPages_Params* params) {
+ EXPECT_EQ(document_cookie_, cookie);
+
+ memset(params, 0, sizeof(PrintMsg_PrintPages_Params));
+ SetPrintParams(&(params->params));
+}
+
void MockPrinter::SetPrintedPagesCount(int cookie, int number_pages) {
// Verify the input parameter and update the printer status so that the
// RenderViewTest class can verify the this function finishes without errors.
@@ -214,3 +213,16 @@ bool MockPrinter::SaveBitmap(
int MockPrinter::CreateDocumentCookie() {
return ++current_document_cookie_;
}
+
+void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) {
+ params->dpi = dpi_;
+ params->max_shrink = max_shrink_;
+ params->min_shrink = min_shrink_;
+ params->desired_dpi = desired_dpi_;
+ params->selection_only = selection_only_;
+ params->document_cookie = document_cookie_;
+ params->page_size = page_size_;
+ params->printable_size = printable_size_;
+ params->margin_left = margin_left_;
+ params->margin_top = margin_top_;
+}
diff --git a/chrome/renderer/mock_printer.h b/chrome/renderer/mock_printer.h
index 271b8de..1c2994c 100644
--- a/chrome/renderer/mock_printer.h
+++ b/chrome/renderer/mock_printer.h
@@ -75,6 +75,7 @@ class MockPrinter {
int expected_pages_count,
bool has_selection,
PrintMsg_PrintPages_Params* settings);
+ void UpdateSettings(int cookie, PrintMsg_PrintPages_Params* params);
void SetPrintedPagesCount(int cookie, int number_pages);
void PrintPage(const PrintHostMsg_DidPrintPage_Params& params);
@@ -96,9 +97,11 @@ class MockPrinter {
protected:
int CreateDocumentCookie();
- bool GetChecksum(const void* data, uint32 size, std::string* checksum) const;
private:
+ // Helper function to fill the fields in |params|.
+ void SetPrintParams(PrintMsg_Print_Params* params);
+
// In pixels according to dpi_x and dpi_y.
gfx::Size page_size_;
gfx::Size printable_size_;
diff --git a/chrome/renderer/mock_render_thread.cc b/chrome/renderer/mock_render_thread.cc
index c832177..578409a 100644
--- a/chrome/renderer/mock_render_thread.cc
+++ b/chrome/renderer/mock_render_thread.cc
@@ -15,6 +15,7 @@
#include "content/common/view_messages.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_sync_message.h"
+#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
MockRenderThread::MockRenderThread()
@@ -103,6 +104,8 @@ bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint,
OnScriptedPrint)
+ IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings,
+ OnUpdatePrintSettings)
#if defined(OS_WIN) || defined(OS_MACOSX)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
OnDidGetPrintedPagesCount)
@@ -208,6 +211,28 @@ void MockRenderThread::OnDidPrintPage(
printer_->PrintPage(params);
}
+void MockRenderThread::OnUpdatePrintSettings(
+ int document_cookie,
+ const DictionaryValue& job_settings,
+ PrintMsg_PrintPages_Params* params) {
+ // Check and make sure the required settings are all there.
+ // We don't actually care about the values.
+ std::string dummy_string;
+ if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
+ !job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
+ !job_settings.GetBoolean(printing::kSettingColor, NULL) ||
+ !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
+ !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
+ !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
+ !job_settings.GetInteger(printing::kSettingCopies, NULL)) {
+ return;
+ }
+
+ // Just return the default settings.
+ if (printer_.get())
+ printer_->UpdateSettings(document_cookie, params);
+}
+
void MockRenderThread::set_print_dialog_user_response(bool response) {
print_dialog_user_response_ = response;
}
diff --git a/chrome/renderer/mock_render_thread.h b/chrome/renderer/mock_render_thread.h
index c1a77dd..3be3134 100644
--- a/chrome/renderer/mock_render_thread.h
+++ b/chrome/renderer/mock_render_thread.h
@@ -112,16 +112,21 @@ class MockRenderThread : public RenderThreadBase {
void OnTempFileForPrintingWritten(int browser_fd);
#endif
- // The RenderView expects default print settings.
+ // PrintWebViewHelper expects default print settings.
void OnGetDefaultPrintSettings(PrintMsg_Print_Params* setting);
- // The RenderView expects final print settings from the user.
+ // PrintWebViewHelper expects final print settings from the user.
void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params,
PrintMsg_PrintPages_Params* settings);
void OnDidGetPrintedPagesCount(int cookie, int number_pages);
void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
+ // For print preview, PrintWebViewHelper will update settings.
+ void OnUpdatePrintSettings(int document_cookie,
+ const DictionaryValue& job_settings,
+ PrintMsg_PrintPages_Params* params);
+
IPC::TestSink sink_;
// Routing id what will be assigned to the Widget.
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index a5bab44..ee64527 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -82,12 +82,15 @@ class PrintWebViewHelper : public RenderViewObserver ,
virtual void didStopLoading();
private:
- FRIEND_TEST_ALL_PREFIXES(RenderViewTest, BlockScriptInitiatedPrinting);
- FRIEND_TEST_ALL_PREFIXES(RenderViewTest, OnPrintPages);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest,
+ BlockScriptInitiatedPrinting);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, OnPrintPages);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest, OnPrintPreview);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest, OnPrintPreviewFail);
#if defined(OS_WIN) || defined(OS_MACOSX)
- FRIEND_TEST_ALL_PREFIXES(RenderViewTest, PrintLayoutTest);
- FRIEND_TEST_ALL_PREFIXES(RenderViewTest, PrintWithIframe);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintLayoutTest);
+ FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintWithIframe);
#endif // defined(OS_WIN) || defined(OS_MACOSX)
// RenderViewObserver implementation.
diff --git a/chrome/renderer/print_web_view_helper_browsertest.cc b/chrome/renderer/print_web_view_helper_browsertest.cc
index 7df3294..21cb426 100644
--- a/chrome/renderer/print_web_view_helper_browsertest.cc
+++ b/chrome/renderer/print_web_view_helper_browsertest.cc
@@ -2,31 +2,88 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/file_util.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/common/print_messages.h"
#include "chrome/renderer/print_web_view_helper.h"
#include "chrome/test/render_view_test.h"
-#include "printing/image.h"
+#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
+#if defined(OS_WIN) || defined(OS_MACOSX)
+#include "base/file_util.h"
+#include "printing/image.h"
+
using WebKit::WebFrame;
using WebKit::WebString;
-using WebKit::WebView;
+#endif
namespace {
+// A simple web page.
+const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>";
+
+// A simple webpage that prints itself.
const char kPrintWithJSHTML[] =
"<body>Hello<script>window.print()</script>World</body>";
} // namespace
+class PrintWebViewHelperTest : public RenderViewTest {
+ public:
+ PrintWebViewHelperTest() {}
+ ~PrintWebViewHelperTest() {}
+
+ protected:
+ // The renderer should be done calculating the number of rendered pages
+ // according to the specified settings defined in the mock render thread.
+ // Verify the page count is correct.
+ void VerifyPageCount(int count) {
+#if defined(OS_CHROMEOS)
+ // The DidGetPrintedPagesCount message isn't sent on ChromeOS. Right now we
+ // always print all pages, and there are checks to that effect built into
+ // the print code.
+#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 // defined(OS_CHROMEOS)
+ }
+
+ // Verifies whether the pages printed or not.
+ 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 // defined(OS_CHROMEOS)
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTest);
+};
+
// Tests that printing pages work and sending and receiving messages through
// that channel all works.
-TEST_F(RenderViewTest, OnPrintPages) {
- // Lets simulate a print pages with Hello world.
- LoadHTML("<body><p>Hello World!</p></body>");
+TEST_F(PrintWebViewHelperTest, OnPrintPages) {
+ LoadHTML(kHelloWorldHTML);
PrintWebViewHelper::Get(view_)->OnPrintPages();
VerifyPageCount(1);
@@ -34,7 +91,7 @@ TEST_F(RenderViewTest, OnPrintPages) {
}
// Duplicate of OnPrintPagesTest only using javascript to print.
-TEST_F(RenderViewTest, PrintWithJavascript) {
+TEST_F(PrintWebViewHelperTest, PrintWithJavascript) {
// HTML contains a call to window.print()
LoadHTML(kPrintWithJSHTML);
@@ -44,7 +101,7 @@ TEST_F(RenderViewTest, PrintWithJavascript) {
// Tests that the renderer blocks window.print() calls if they occur too
// frequently.
-TEST_F(RenderViewTest, BlockScriptInitiatedPrinting) {
+TEST_F(PrintWebViewHelperTest, BlockScriptInitiatedPrinting) {
// Pretend user will cancel printing.
render_thread_.set_print_dialog_user_response(false);
// Try to print with window.print() a few times.
@@ -71,7 +128,7 @@ TEST_F(RenderViewTest, BlockScriptInitiatedPrinting) {
// to rip out and replace most of the IPC code if we ever plan to improve
// printing, and the comment below by sverrir suggests that it doesn't do much
// for us anyway.
-TEST_F(RenderViewTest, PrintWithIframe) {
+TEST_F(PrintWebViewHelperTest, PrintWithIframe) {
// Document that populates an iframe.
const char html[] =
"<html><body>Lorem Ipsum:"
@@ -150,7 +207,7 @@ const TestPageData kTestPages[] = {
// hooking up Cairo to read a pdf stream, or accessing the cairo surface in the
// metafile directly.
#if defined(OS_WIN) || defined(OS_MACOSX)
-TEST_F(RenderViewTest, PrintLayoutTest) {
+TEST_F(PrintWebViewHelperTest, PrintLayoutTest) {
bool baseline = false;
EXPECT_TRUE(render_thread_.printer() != NULL);
@@ -201,3 +258,80 @@ TEST_F(RenderViewTest, PrintLayoutTest) {
}
}
#endif
+
+// These print preview tests do not work on Chrome OS yet.
+#if !defined(OS_CHROMEOS)
+class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTest {
+ public:
+ PrintWebViewHelperPreviewTest() {}
+ virtual ~PrintWebViewHelperPreviewTest() {}
+
+ virtual void SetUp() {
+ // Append the print preview switch before creating the PrintWebViewHelper.
+ // TODO(thestig): Remove when print preview is enabled by default.
+ CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnablePrintPreview);
+
+ RenderViewTest::SetUp();
+ }
+
+ protected:
+ 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_PagesReadyForPreview::ID);
+ bool did_get_preview_msg = (NULL != preview_msg);
+ ASSERT_EQ(generated_preview, did_get_preview_msg);
+ if (did_get_preview_msg) {
+ PrintHostMsg_PagesReadyForPreview::Param preview_param;
+ PrintHostMsg_PagesReadyForPreview::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);
+ }
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest);
+};
+
+// Tests that print preview work and sending and receiving messages through
+// that channel all works.
+TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) {
+ LoadHTML(kHelloWorldHTML);
+
+ // Fill in some dummy values.
+ DictionaryValue dict;
+ dict.SetBoolean(printing::kSettingLandscape, false);
+ dict.SetBoolean(printing::kSettingCollate, false);
+ dict.SetBoolean(printing::kSettingColor, false);
+ dict.SetBoolean(printing::kSettingPrintToPDF, true);
+ dict.SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX);
+ dict.SetInteger(printing::kSettingCopies, 1);
+ dict.SetString(printing::kSettingDeviceName, "dummy");
+ PrintWebViewHelper::Get(view_)->OnPrintPreview(dict);
+
+ VerifyPrintPreviewFailed(false);
+ VerifyPrintPreviewGenerated(true);
+ VerifyPagesPrinted(false);
+}
+
+// Tests that print preview fails and receiving error messages through
+// that channel all works.
+TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) {
+ LoadHTML(kHelloWorldHTML);
+
+ // An empty dictionary should fail.
+ DictionaryValue empty_dict;
+ PrintWebViewHelper::Get(view_)->OnPrintPreview(empty_dict);
+
+ VerifyPrintPreviewFailed(true);
+ VerifyPrintPreviewGenerated(false);
+ VerifyPagesPrinted(false);
+}
+#endif // !defined(OS_CHROMEOS)
diff --git a/chrome/test/render_view_test.cc b/chrome/test/render_view_test.cc
index 1c8e0c6..55a6dc0 100644
--- a/chrome/test/render_view_test.cc
+++ b/chrome/test/render_view_test.cc
@@ -259,42 +259,6 @@ void RenderViewTest::SendNativeKeyEvent(
view_->OnMessageReceived(*input_message);
}
-void RenderViewTest::VerifyPageCount(int count) {
-#if defined(OS_CHROMEOS)
- // The DidGetPrintedPagesCount message isn't sent on ChromeOS. Right now we
- // always print all pages, and there are checks to that effect built into
- // the print code.
-#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 // defined(OS_CHROMEOS)
-}
-
-void RenderViewTest::VerifyPagesPrinted(bool printed) {
-#if defined(OS_CHROMEOS)
- bool did_print_msg = (NULL != render_thread_.sink().GetUniqueMessageMatching(
- PrintHostMsg_TempFileForPrintingWritten::ID));
- 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 // defined(OS_CHROMEOS)
-}
-
const char* const kGetCoordinatesScript =
"(function() {"
" function GetCoordinates(elem) {"
diff --git a/chrome/test/render_view_test.h b/chrome/test/render_view_test.h
index 5d3ec67..4816f9b 100644
--- a/chrome/test/render_view_test.h
+++ b/chrome/test/render_view_test.h
@@ -75,13 +75,6 @@ class RenderViewTest : public testing::Test {
// Sends one native key event over IPC.
void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event);
- // The renderer should be done calculating the number of rendered pages
- // according to the specified settings defined in the mock render thread.
- // Verify the page count is correct.
- void VerifyPageCount(int count);
- // Verifies whether the pages printed or not.
- void VerifyPagesPrinted(bool printed);
-
// Returns the bounds (coordinates and size) of the element with id
// |element_id|. Returns an empty rect if such an element was not found.
gfx::Rect GetElementBounds(const std::string& element_id);