Index: headless/lib/headless_web_contents_browsertest.cc |
diff --git a/headless/lib/headless_web_contents_browsertest.cc b/headless/lib/headless_web_contents_browsertest.cc |
index bf11b95952eb33677ba9d331426fce0a9f7d0e4f..b0afaa92751003a3e61133289f228901e1bd6ca7 100644 |
--- a/headless/lib/headless_web_contents_browsertest.cc |
+++ b/headless/lib/headless_web_contents_browsertest.cc |
@@ -16,6 +16,7 @@ |
#include "headless/public/headless_tab_socket.h" |
#include "headless/public/headless_web_contents.h" |
#include "headless/test/headless_browser_test.h" |
+#include "printing/features/features.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/skia/include/core/SkBitmap.h" |
@@ -24,6 +25,14 @@ |
#include "ui/gfx/geometry/size.h" |
#include "url/gurl.h" |
+#if BUILDFLAG(ENABLE_BASIC_PRINTING) |
+#include "base/strings/string_number_conversions.h" |
+#include "pdf/pdf.h" |
+#include "printing/pdf_render_settings.h" |
+#include "printing/units.h" |
+#include "ui/gfx/geometry/rect.h" |
+#endif |
+ |
using testing::UnorderedElementsAre; |
namespace headless { |
@@ -144,7 +153,7 @@ class HeadlessWebContentsScreenshotTest |
void OnScreenshotCaptured( |
std::unique_ptr<page::CaptureScreenshotResult> result) { |
std::string base64 = result->GetData(); |
- EXPECT_LT(0U, base64.length()); |
+ EXPECT_GT(base64.length(), 0U); |
SkBitmap result_bitmap; |
EXPECT_TRUE(DecodePNG(base64, &result_bitmap)); |
@@ -164,6 +173,88 @@ INSTANTIATE_TEST_CASE_P(HeadlessWebContentsScreenshotTests, |
HeadlessWebContentsScreenshotTest, |
::testing::Bool()); |
+#if BUILDFLAG(ENABLE_BASIC_PRINTING) |
+class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest { |
+ public: |
+ const double kPaperWidth = 10; |
+ const double kPaperHeight = 15; |
+ const double kDocHeight = 50; |
+ // Number of color channels in a BGRA bitmap. |
+ const int kColorChannels = 4; |
+ const int kDpi = 300; |
+ |
+ void RunDevTooledTest() override { |
+ std::string height_expression = "document.body.style.height = '" + |
+ base::DoubleToString(kDocHeight) + "in'"; |
+ std::unique_ptr<runtime::EvaluateParams> params = |
+ runtime::EvaluateParams::Builder() |
+ .SetExpression("document.body.style.background = '#123456';" + |
+ height_expression) |
+ .Build(); |
+ devtools_client_->GetRuntime()->Evaluate( |
+ std::move(params), |
+ base::Bind(&HeadlessWebContentsPDFTest::OnPageSetupCompleted, |
+ base::Unretained(this))); |
+ } |
+ |
+ void OnPageSetupCompleted(std::unique_ptr<runtime::EvaluateResult> result) { |
+ devtools_client_->GetPage()->GetExperimental()->PrintToPDF( |
+ page::PrintToPDFParams::Builder() |
+ .SetPrintBackground(true) |
+ .SetPaperHeight(kPaperHeight) |
+ .SetPaperWidth(kPaperWidth) |
+ .SetMarginTop(0) |
+ .SetMarginBottom(0) |
+ .SetMarginLeft(0) |
+ .SetMarginRight(0) |
+ .Build(), |
+ base::Bind(&HeadlessWebContentsPDFTest::OnPDFCreated, |
+ base::Unretained(this))); |
+ } |
+ |
+ void OnPDFCreated(std::unique_ptr<page::PrintToPDFResult> result) { |
+ std::string base64 = result->GetData(); |
+ EXPECT_GT(base64.length(), 0U); |
+ std::string pdf_data; |
+ EXPECT_TRUE(base::Base64Decode(base64, &pdf_data)); |
+ |
+ int num_pages; |
+ EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(), |
+ &num_pages, nullptr)); |
+ EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages); |
+ |
+ for (int i = 0; i < num_pages; i++) { |
+ double width_in_points; |
+ double height_in_points; |
+ EXPECT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( |
+ pdf_data.data(), pdf_data.size(), i, &width_in_points, |
+ &height_in_points)); |
+ EXPECT_EQ(static_cast<int>(width_in_points), |
+ static_cast<int>(kPaperWidth * printing::kPointsPerInch)); |
+ EXPECT_EQ(static_cast<int>(height_in_points), |
+ static_cast<int>(kPaperHeight * printing::kPointsPerInch)); |
+ |
+ gfx::Rect rect(kPaperWidth * kDpi, kPaperHeight * kDpi); |
+ printing::PdfRenderSettings settings( |
+ rect, gfx::Point(0, 0), kDpi, true, |
+ printing::PdfRenderSettings::Mode::NORMAL); |
+ std::vector<uint8_t> page_bitmap_data(kColorChannels * |
+ settings.area.size().GetArea()); |
+ EXPECT_TRUE(chrome_pdf::RenderPDFPageToBitmap( |
+ pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(), |
+ settings.area.size().width(), settings.area.size().height(), |
+ settings.dpi, settings.autorotate)); |
+ EXPECT_EQ(0x56, page_bitmap_data[0]); // B |
+ EXPECT_EQ(0x34, page_bitmap_data[1]); // G |
+ EXPECT_EQ(0x12, page_bitmap_data[2]); // R |
+ } |
+ FinishAsynchronousTest(); |
+ } |
+}; |
+ |
+HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessWebContentsPDFTest); |
+#endif |
+ |
class HeadlessWebContentsSecurityTest |
: public HeadlessAsyncDevTooledBrowserTest, |
public security::ExperimentalObserver { |