Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/renderer/print_web_view_helper_browsertest.cc

Issue 6874038: Remove the last Chrome dependencies from renderer, and enforce no more includes through DEPS. I ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "base/file_util.h"
6 #include "chrome/renderer/print_web_view_helper.h"
7 #include "chrome/test/render_view_test.h"
8 #include "printing/image.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
13
14 using WebKit::WebFrame;
15 using WebKit::WebString;
16 using WebKit::WebView;
17
18 namespace {
19
20 const char kPrintWithJSHTML[] =
21 "<body>Hello<script>window.print()</script>World</body>";
22
23 } // namespace
24
25 // Tests that printing pages work and sending and receiving messages through
26 // that channel all works.
27 TEST_F(RenderViewTest, OnPrintPages) {
28 // Lets simulate a print pages with Hello world.
29 LoadHTML("<body><p>Hello World!</p></body>");
30 PrintWebViewHelper::Get(view_)->OnPrintPages();
31
32 VerifyPageCount(1);
33 VerifyPagesPrinted(true);
34 }
35
36 // Duplicate of OnPrintPagesTest only using javascript to print.
37 TEST_F(RenderViewTest, PrintWithJavascript) {
38 // HTML contains a call to window.print()
39 LoadHTML(kPrintWithJSHTML);
40
41 VerifyPageCount(1);
42 VerifyPagesPrinted(true);
43 }
44
45 // Tests that the renderer blocks window.print() calls if they occur too
46 // frequently.
47 TEST_F(RenderViewTest, BlockScriptInitiatedPrinting) {
48 // Pretend user will cancel printing.
49 render_thread_.set_print_dialog_user_response(false);
50 // Try to print with window.print() a few times.
51 LoadHTML(kPrintWithJSHTML);
52 LoadHTML(kPrintWithJSHTML);
53 LoadHTML(kPrintWithJSHTML);
54 VerifyPagesPrinted(false);
55
56 // Pretend user will print. (but printing is blocked.)
57 render_thread_.set_print_dialog_user_response(true);
58 LoadHTML(kPrintWithJSHTML);
59 VerifyPagesPrinted(false);
60
61 // Unblock script initiated printing and verify printing works.
62 PrintWebViewHelper::Get(view_)->ResetScriptedPrintCount();
63 render_thread_.printer()->ResetPrinter();
64 LoadHTML(kPrintWithJSHTML);
65 VerifyPageCount(1);
66 VerifyPagesPrinted(true);
67 }
68
69 #if defined(OS_WIN) || defined(OS_MACOSX)
70 // TODO(estade): I don't think this test is worth porting to Linux. We will have
71 // to rip out and replace most of the IPC code if we ever plan to improve
72 // printing, and the comment below by sverrir suggests that it doesn't do much
73 // for us anyway.
74 TEST_F(RenderViewTest, PrintWithIframe) {
75 // Document that populates an iframe.
76 const char html[] =
77 "<html><body>Lorem Ipsum:"
78 "<iframe name=\"sub1\" id=\"sub1\"></iframe><script>"
79 " document.write(frames['sub1'].name);"
80 " frames['sub1'].document.write("
81 " '<p>Cras tempus ante eu felis semper luctus!</p>');"
82 "</script></body></html>";
83
84 LoadHTML(html);
85
86 // Find the frame and set it as the focused one. This should mean that that
87 // the printout should only contain the contents of that frame.
88 WebFrame* sub1_frame =
89 view_->webview()->findFrameByName(WebString::fromUTF8("sub1"));
90 ASSERT_TRUE(sub1_frame);
91 view_->webview()->setFocusedFrame(sub1_frame);
92 ASSERT_NE(view_->webview()->focusedFrame(),
93 view_->webview()->mainFrame());
94
95 // Initiate printing.
96 PrintWebViewHelper::Get(view_)->OnPrintPages();
97
98 // Verify output through MockPrinter.
99 const MockPrinter* printer(render_thread_.printer());
100 ASSERT_EQ(1, printer->GetPrintedPages());
101 const printing::Image& image1(printer->GetPrintedPage(0)->image());
102
103 // TODO(sverrir): Figure out a way to improve this test to actually print
104 // only the content of the iframe. Currently image1 will contain the full
105 // page.
106 EXPECT_NE(0, image1.size().width());
107 EXPECT_NE(0, image1.size().height());
108 }
109 #endif
110
111 // Tests if we can print a page and verify its results.
112 // This test prints HTML pages into a pseudo printer and check their outputs,
113 // i.e. a simplified version of the PrintingLayoutTextTest UI test.
114 namespace {
115 // Test cases used in this test.
116 struct TestPageData {
117 const char* page;
118 size_t printed_pages;
119 int width;
120 int height;
121 const char* checksum;
122 const wchar_t* file;
123 };
124
125 const TestPageData kTestPages[] = {
126 {"<html>"
127 "<head>"
128 "<meta"
129 " http-equiv=\"Content-Type\""
130 " content=\"text/html; charset=utf-8\"/>"
131 "<title>Test 1</title>"
132 "</head>"
133 "<body style=\"background-color: white;\">"
134 "<p style=\"font-family: arial;\">Hello World!</p>"
135 "</body>",
136 #if defined(OS_MACOSX)
137 // Mac printing code compensates for the WebKit scale factor while generating
138 // the metafile, so we expect smaller pages.
139 1, 540, 720,
140 #else
141 1, 675, 900,
142 #endif
143 NULL,
144 NULL,
145 },
146 };
147 } // namespace
148
149 // TODO(estade): need to port MockPrinter to get this on Linux. This involves
150 // hooking up Cairo to read a pdf stream, or accessing the cairo surface in the
151 // metafile directly.
152 #if defined(OS_WIN) || defined(OS_MACOSX)
153 TEST_F(RenderViewTest, PrintLayoutTest) {
154 bool baseline = false;
155
156 EXPECT_TRUE(render_thread_.printer() != NULL);
157 for (size_t i = 0; i < arraysize(kTestPages); ++i) {
158 // Load an HTML page and print it.
159 LoadHTML(kTestPages[i].page);
160 PrintWebViewHelper::Get(view_)->OnPrintPages();
161
162 // MockRenderThread::Send() just calls MockRenderThread::OnMsgReceived().
163 // So, all IPC messages sent in the above RenderView::OnPrintPages() call
164 // has been handled by the MockPrinter object, i.e. this printing job
165 // has been already finished.
166 // So, we can start checking the output pages of this printing job.
167 // Retrieve the number of pages actually printed.
168 size_t pages = render_thread_.printer()->GetPrintedPages();
169 EXPECT_EQ(kTestPages[i].printed_pages, pages);
170
171 // Retrieve the width and height of the output page.
172 int width = render_thread_.printer()->GetWidth(0);
173 int height = render_thread_.printer()->GetHeight(0);
174
175 // Check with margin for error. This has been failing with a one pixel
176 // offset on our buildbot.
177 const int kErrorMargin = 5; // 5%
178 EXPECT_GT(kTestPages[i].width * (100 + kErrorMargin) / 100, width);
179 EXPECT_LT(kTestPages[i].width * (100 - kErrorMargin) / 100, width);
180 EXPECT_GT(kTestPages[i].height * (100 + kErrorMargin) / 100, height);
181 EXPECT_LT(kTestPages[i].height* (100 - kErrorMargin) / 100, height);
182
183 // Retrieve the checksum of the bitmap data from the pseudo printer and
184 // compare it with the expected result.
185 std::string bitmap_actual;
186 EXPECT_TRUE(render_thread_.printer()->GetBitmapChecksum(0, &bitmap_actual));
187 if (kTestPages[i].checksum)
188 EXPECT_EQ(kTestPages[i].checksum, bitmap_actual);
189
190 if (baseline) {
191 // Save the source data and the bitmap data into temporary files to
192 // create base-line results.
193 FilePath source_path;
194 file_util::CreateTemporaryFile(&source_path);
195 render_thread_.printer()->SaveSource(0, source_path);
196
197 FilePath bitmap_path;
198 file_util::CreateTemporaryFile(&bitmap_path);
199 render_thread_.printer()->SaveBitmap(0, bitmap_path);
200 }
201 }
202 }
203 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698