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

Side by Side Diff: headless/lib/headless_web_contents_browsertest.cc

Issue 2829973002: add customized printing setting for headless (Closed)
Patch Set: add skia as public_deps instead Created 3 years, 7 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "content/public/test/browser_test.h" 10 #include "content/public/test/browser_test.h"
11 #include "headless/public/devtools/domains/page.h" 11 #include "headless/public/devtools/domains/page.h"
12 #include "headless/public/devtools/domains/runtime.h" 12 #include "headless/public/devtools/domains/runtime.h"
13 #include "headless/public/devtools/domains/security.h" 13 #include "headless/public/devtools/domains/security.h"
14 #include "headless/public/headless_browser.h" 14 #include "headless/public/headless_browser.h"
15 #include "headless/public/headless_devtools_client.h" 15 #include "headless/public/headless_devtools_client.h"
16 #include "headless/public/headless_tab_socket.h" 16 #include "headless/public/headless_tab_socket.h"
17 #include "headless/public/headless_web_contents.h" 17 #include "headless/public/headless_web_contents.h"
18 #include "headless/test/headless_browser_test.h" 18 #include "headless/test/headless_browser_test.h"
19 #include "printing/features/features.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/skia/include/core/SkBitmap.h" 22 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "third_party/skia/include/core/SkColor.h" 23 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/gfx/codec/png_codec.h" 24 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/geometry/size.h" 25 #include "ui/gfx/geometry/size.h"
25 #include "url/gurl.h" 26 #include "url/gurl.h"
26 27
28 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
29 #include "base/strings/string_number_conversions.h"
30 #include "pdf/pdf.h"
31 #include "printing/pdf_render_settings.h"
32 #include "printing/units.h"
33 #include "ui/gfx/geometry/rect.h"
34 #endif
35
27 using testing::UnorderedElementsAre; 36 using testing::UnorderedElementsAre;
28 37
29 namespace headless { 38 namespace headless {
30 39
31 class HeadlessWebContentsTest : public HeadlessBrowserTest {}; 40 class HeadlessWebContentsTest : public HeadlessBrowserTest {};
32 41
33 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) { 42 IN_PROC_BROWSER_TEST_F(HeadlessWebContentsTest, Navigation) {
34 EXPECT_TRUE(embedded_test_server()->Start()); 43 EXPECT_TRUE(embedded_test_server()->Start());
35 44
36 HeadlessBrowserContext* browser_context = 45 HeadlessBrowserContext* browser_context =
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 void OnPageSetupCompleted(std::unique_ptr<runtime::EvaluateResult> result) { 146 void OnPageSetupCompleted(std::unique_ptr<runtime::EvaluateResult> result) {
138 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot( 147 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot(
139 page::CaptureScreenshotParams::Builder().SetFromSurface(true).Build(), 148 page::CaptureScreenshotParams::Builder().SetFromSurface(true).Build(),
140 base::Bind(&HeadlessWebContentsScreenshotTest::OnScreenshotCaptured, 149 base::Bind(&HeadlessWebContentsScreenshotTest::OnScreenshotCaptured,
141 base::Unretained(this))); 150 base::Unretained(this)));
142 } 151 }
143 152
144 void OnScreenshotCaptured( 153 void OnScreenshotCaptured(
145 std::unique_ptr<page::CaptureScreenshotResult> result) { 154 std::unique_ptr<page::CaptureScreenshotResult> result) {
146 std::string base64 = result->GetData(); 155 std::string base64 = result->GetData();
147 EXPECT_LT(0U, base64.length()); 156 EXPECT_GT(base64.length(), 0U);
148 SkBitmap result_bitmap; 157 SkBitmap result_bitmap;
149 EXPECT_TRUE(DecodePNG(base64, &result_bitmap)); 158 EXPECT_TRUE(DecodePNG(base64, &result_bitmap));
150 159
151 EXPECT_EQ(800, result_bitmap.width()); 160 EXPECT_EQ(800, result_bitmap.width());
152 EXPECT_EQ(600, result_bitmap.height()); 161 EXPECT_EQ(600, result_bitmap.height());
153 SkColor actual_color = result_bitmap.getColor(400, 300); 162 SkColor actual_color = result_bitmap.getColor(400, 300);
154 SkColor expected_color = SkColorSetRGB(0x00, 0x00, 0xff); 163 SkColor expected_color = SkColorSetRGB(0x00, 0x00, 0xff);
155 EXPECT_EQ(expected_color, actual_color); 164 EXPECT_EQ(expected_color, actual_color);
156 FinishAsynchronousTest(); 165 FinishAsynchronousTest();
157 } 166 }
158 }; 167 };
159 168
160 HEADLESS_ASYNC_DEVTOOLED_TEST_P(HeadlessWebContentsScreenshotTest); 169 HEADLESS_ASYNC_DEVTOOLED_TEST_P(HeadlessWebContentsScreenshotTest);
161 170
162 // Instantiate test case for both software and gpu compositing modes. 171 // Instantiate test case for both software and gpu compositing modes.
163 INSTANTIATE_TEST_CASE_P(HeadlessWebContentsScreenshotTests, 172 INSTANTIATE_TEST_CASE_P(HeadlessWebContentsScreenshotTests,
164 HeadlessWebContentsScreenshotTest, 173 HeadlessWebContentsScreenshotTest,
165 ::testing::Bool()); 174 ::testing::Bool());
166 175
176 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
177 class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
178 public:
179 const double kPaperWidth = 10;
180 const double kPaperHeight = 15;
181 const double kDocHeight = 50;
182 // Number of color channels in a BGRA bitmap.
183 const int kColorChannels = 4;
184 const int kDpi = 300;
185
186 void RunDevTooledTest() override {
187 std::string height_expression = "document.body.style.height = '" +
188 base::DoubleToString(kDocHeight) + "in'";
189 std::unique_ptr<runtime::EvaluateParams> params =
190 runtime::EvaluateParams::Builder()
191 .SetExpression("document.body.style.background = '#123456';" +
192 height_expression)
193 .Build();
194 devtools_client_->GetRuntime()->Evaluate(
195 std::move(params),
196 base::Bind(&HeadlessWebContentsPDFTest::OnPageSetupCompleted,
197 base::Unretained(this)));
198 }
199
200 void OnPageSetupCompleted(std::unique_ptr<runtime::EvaluateResult> result) {
201 devtools_client_->GetPage()->GetExperimental()->PrintToPDF(
202 page::PrintToPDFParams::Builder()
203 .SetPrintBackground(true)
204 .SetPaperHeight(kPaperHeight)
205 .SetPaperWidth(kPaperWidth)
206 .SetMarginTop(0)
207 .SetMarginBottom(0)
208 .SetMarginLeft(0)
209 .SetMarginRight(0)
210 .Build(),
211 base::Bind(&HeadlessWebContentsPDFTest::OnPDFCreated,
212 base::Unretained(this)));
213 }
214
215 void OnPDFCreated(std::unique_ptr<page::PrintToPDFResult> result) {
216 std::string base64 = result->GetData();
217 EXPECT_GT(base64.length(), 0U);
218 std::string pdf_data;
219 EXPECT_TRUE(base::Base64Decode(base64, &pdf_data));
220
221 int num_pages;
222 EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(),
223 &num_pages, nullptr));
224 EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages);
225
226 for (int i = 0; i < num_pages; i++) {
227 double width_in_points;
228 double height_in_points;
229 EXPECT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(
230 pdf_data.data(), pdf_data.size(), i, &width_in_points,
231 &height_in_points));
232 EXPECT_EQ(static_cast<int>(width_in_points),
233 static_cast<int>(kPaperWidth * printing::kPointsPerInch));
234 EXPECT_EQ(static_cast<int>(height_in_points),
235 static_cast<int>(kPaperHeight * printing::kPointsPerInch));
236
237 gfx::Rect rect(kPaperWidth * kDpi, kPaperHeight * kDpi);
238 printing::PdfRenderSettings settings(
239 rect, gfx::Point(0, 0), kDpi, true,
240 printing::PdfRenderSettings::Mode::NORMAL);
241 std::vector<uint8_t> page_bitmap_data(kColorChannels *
242 settings.area.size().GetArea());
243 EXPECT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
244 pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(),
245 settings.area.size().width(), settings.area.size().height(),
246 settings.dpi, settings.autorotate));
247 EXPECT_EQ(0x56, page_bitmap_data[0]); // B
248 EXPECT_EQ(0x34, page_bitmap_data[1]); // G
249 EXPECT_EQ(0x12, page_bitmap_data[2]); // R
250 }
251 FinishAsynchronousTest();
252 }
253 };
254
255 HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessWebContentsPDFTest);
256 #endif
257
167 class HeadlessWebContentsSecurityTest 258 class HeadlessWebContentsSecurityTest
168 : public HeadlessAsyncDevTooledBrowserTest, 259 : public HeadlessAsyncDevTooledBrowserTest,
169 public security::ExperimentalObserver { 260 public security::ExperimentalObserver {
170 public: 261 public:
171 void RunDevTooledTest() override { 262 void RunDevTooledTest() override {
172 devtools_client_->GetSecurity()->GetExperimental()->AddObserver(this); 263 devtools_client_->GetSecurity()->GetExperimental()->AddObserver(this);
173 devtools_client_->GetSecurity()->GetExperimental()->Enable( 264 devtools_client_->GetSecurity()->GetExperimental()->Enable(
174 security::EnableParams::Builder().Build()); 265 security::EnableParams::Builder().Build());
175 } 266 }
176 267
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 EXPECT_EQ("Embedder sent us: Hello", message); 307 EXPECT_EQ("Embedder sent us: Hello", message);
217 FinishAsynchronousTest(); 308 FinishAsynchronousTest();
218 } 309 }
219 310
220 bool GetCreateTabSocket() override { return true; } 311 bool GetCreateTabSocket() override { return true; }
221 }; 312 };
222 313
223 HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessTabSocketTest); 314 HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessTabSocketTest);
224 315
225 } // namespace headless 316 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698