Commit 561360a6 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Improve PdfNupConverterClientBrowserTest.

- For input and generated PDFs, validate all the page sizes.
- Set the page size / printable area to 72 DPI, which is what the
  PdfNupConverterClient normally receives.

Bug: 1004529
Change-Id: I615b014c08332a55c395f06352424fe1c96a4601
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1895071
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711884}
parent ca07859b
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "pdf/pdf.h" #include "pdf/pdf.h"
#include "ui/gfx/geometry/size_f.h"
namespace printing { namespace printing {
...@@ -73,6 +74,48 @@ base::MappedReadOnlyRegion GetBadDataRegion() { ...@@ -73,6 +74,48 @@ base::MappedReadOnlyRegion GetBadDataRegion() {
return pdf_region; return pdf_region;
} }
std::vector<gfx::SizeF> GetPdfPageSizes(base::span<const uint8_t> pdf_data) {
int num_pages;
if (!chrome_pdf::GetPDFDocInfo(pdf_data, &num_pages, nullptr) ||
num_pages <= 0) {
return {};
}
std::vector<gfx::SizeF> sizes;
for (int i = 0; i < num_pages; ++i) {
double width;
double height;
if (!chrome_pdf::GetPDFPageSizeByIndex(pdf_data, i, &width, &height))
return {};
sizes.push_back({width, height});
}
return sizes;
}
void VerifyPdf(base::span<const uint8_t> pdf_data,
const std::vector<gfx::SizeF>& expected_sizes) {
std::vector<gfx::SizeF> page_sizes = GetPdfPageSizes(pdf_data);
ASSERT_EQ(expected_sizes.size(), page_sizes.size());
for (size_t i = 0; i < expected_sizes.size(); ++i)
EXPECT_EQ(expected_sizes[i], page_sizes[i]);
}
std::vector<gfx::SizeF> GetExpectedPdfSizes(base::StringPiece pdf_name) {
if (pdf_name == "pdf_converter_basic.pdf") {
static const std::vector<gfx::SizeF> kSizes = {
{612.0f, 792.0f},
{612.0f, 792.0f},
{612.0f, 792.0f},
};
return kSizes;
}
NOTREACHED();
return {};
}
} // namespace } // namespace
class PdfNupConverterClientBrowserTest : public InProcessBrowserTest { class PdfNupConverterClientBrowserTest : public InProcessBrowserTest {
...@@ -94,8 +137,8 @@ class PdfNupConverterClientBrowserTest : public InProcessBrowserTest { ...@@ -94,8 +137,8 @@ class PdfNupConverterClientBrowserTest : public InProcessBrowserTest {
base::RunLoop run_loop; base::RunLoop run_loop;
converter->DoNupPdfDocumentConvert( converter->DoNupPdfDocumentConvert(
/*document_cookie=*/8, pages_per_sheet, /*document_cookie=*/8, pages_per_sheet,
/*page_size=*/gfx::Size(2550, 3300), /*page_size=*/gfx::Size(612, 792),
/*printable_area=*/gfx::Rect(2550, 3300), std::move(pdf_region), /*printable_area=*/gfx::Rect(612, 792), std::move(pdf_region),
base::BindOnce(&ResultCallbackImpl, &status, &nup_pdf_region, &called, base::BindOnce(&ResultCallbackImpl, &status, &nup_pdf_region, &called,
run_loop.QuitClosure())); run_loop.QuitClosure()));
run_loop.Run(); run_loop.Run();
...@@ -115,14 +158,9 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, ...@@ -115,14 +158,9 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
GetPdfRegion("pdf_converter_basic.pdf"); GetPdfRegion("pdf_converter_basic.pdf");
ASSERT_TRUE(pdf_region.IsValid()); ASSERT_TRUE(pdf_region.IsValid());
// Make sure pdf_converter_basic.pdf has 3 pages. // Make sure pdf_converter_basic.pdf is as expected.
int num_pages; VerifyPdf(pdf_region.mapping.GetMemoryAsSpan<uint8_t>(),
double max_width_in_points; GetExpectedPdfSizes("pdf_converter_basic.pdf"));
ASSERT_TRUE(
chrome_pdf::GetPDFDocInfo(pdf_region.mapping.GetMemoryAsSpan<uint8_t>(),
&num_pages, &max_width_in_points));
EXPECT_EQ(3, num_pages);
EXPECT_DOUBLE_EQ(612.0, max_width_in_points);
base::ReadOnlySharedMemoryRegion nup_pdf_region; base::ReadOnlySharedMemoryRegion nup_pdf_region;
base::Optional<mojom::PdfNupConverter::Status> status = Convert( base::Optional<mojom::PdfNupConverter::Status> status = Convert(
...@@ -134,11 +172,11 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, ...@@ -134,11 +172,11 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
ASSERT_TRUE(nup_pdf_mapping.IsValid()); ASSERT_TRUE(nup_pdf_mapping.IsValid());
// For 2-up, a 3 page portrait document fits on 2 landscape-oriented pages. // For 2-up, a 3 page portrait document fits on 2 landscape-oriented pages.
ASSERT_TRUE( const std::vector<gfx::SizeF> kExpectedSizes = {
chrome_pdf::GetPDFDocInfo(nup_pdf_mapping.GetMemoryAsSpan<uint8_t>(), {792.0f, 612.0f},
&num_pages, &max_width_in_points)); {792.0f, 612.0f},
EXPECT_EQ(2, num_pages); };
EXPECT_DOUBLE_EQ(3300.0, max_width_in_points); VerifyPdf(nup_pdf_mapping.GetMemoryAsSpan<uint8_t>(), kExpectedSizes);
} }
IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
...@@ -147,14 +185,9 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, ...@@ -147,14 +185,9 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
GetPdfRegion("pdf_converter_basic.pdf"); GetPdfRegion("pdf_converter_basic.pdf");
ASSERT_TRUE(pdf_region.IsValid()); ASSERT_TRUE(pdf_region.IsValid());
// Make sure pdf_converter_basic.pdf has 3 pages. // Make sure pdf_converter_basic.pdf is as expected.
int num_pages; VerifyPdf(pdf_region.mapping.GetMemoryAsSpan<uint8_t>(),
double max_width_in_points; GetExpectedPdfSizes("pdf_converter_basic.pdf"));
ASSERT_TRUE(
chrome_pdf::GetPDFDocInfo(pdf_region.mapping.GetMemoryAsSpan<uint8_t>(),
&num_pages, &max_width_in_points));
EXPECT_EQ(3, num_pages);
EXPECT_DOUBLE_EQ(612.0, max_width_in_points);
base::ReadOnlySharedMemoryRegion nup_pdf_region; base::ReadOnlySharedMemoryRegion nup_pdf_region;
base::Optional<mojom::PdfNupConverter::Status> status = Convert( base::Optional<mojom::PdfNupConverter::Status> status = Convert(
...@@ -165,12 +198,11 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, ...@@ -165,12 +198,11 @@ IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
base::ReadOnlySharedMemoryMapping nup_pdf_mapping = nup_pdf_region.Map(); base::ReadOnlySharedMemoryMapping nup_pdf_mapping = nup_pdf_region.Map();
ASSERT_TRUE(nup_pdf_mapping.IsValid()); ASSERT_TRUE(nup_pdf_mapping.IsValid());
// For 4-up, a 3 page portrait document fits on 1 landscape-oriented page. // For 4-up, a 3 page portrait document fits on 1 portrait-oriented page.
ASSERT_TRUE( const std::vector<gfx::SizeF> kExpectedSizes = {
chrome_pdf::GetPDFDocInfo(nup_pdf_mapping.GetMemoryAsSpan<uint8_t>(), {612.0f, 792.0f},
&num_pages, &max_width_in_points)); };
EXPECT_EQ(1, num_pages); VerifyPdf(nup_pdf_mapping.GetMemoryAsSpan<uint8_t>(), kExpectedSizes);
EXPECT_DOUBLE_EQ(2550.0, max_width_in_points);
} }
IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest, IN_PROC_BROWSER_TEST_F(PdfNupConverterClientBrowserTest,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment