File: pdf_conversion_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (152 lines) | stat: -rw-r--r-- 5,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/utils/pdf_conversion.h"

#include <fstream>
#include <string>
#include <vector>

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/image/image_util.h"

namespace chromeos {

namespace {

// Returns a manually generated JPG image with specified width and height in
// pixels.
std::vector<uint8_t> CreateJpg(int width, int height) {
  gfx::Image original = gfx::test::CreateImage(width, height);
  std::vector<uint8_t> jpg_buffer;
  if (!gfx::JPEG1xEncodedDataFromImage(original, 80, &jpg_buffer)) {
    return {};
  }
  return jpg_buffer;
}

}  // namespace

using ConvertToPdfTest = testing::Test;

// Test that JPG image can be converted to pdf file successfully.
TEST_F(ConvertToPdfTest, ToFileNoDpi) {
  std::vector<std::string> images;
  std::vector<uint8_t> bytes = CreateJpg(100, 100);
  images.push_back(std::string(bytes.begin(), bytes.end()));

  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  auto output_path = temp_dir.GetPath().Append("temp.pdf");

  EXPECT_TRUE(ConvertJpgImagesToPdf(images, output_path,
                                    /*rotate_alternate_pages=*/false,
                                    /*dpi=*/absl::nullopt));
  EXPECT_TRUE(base::PathExists(output_path));

  int64_t file_size;
  EXPECT_TRUE(base::GetFileSize(output_path, &file_size));

  // Smallest PDF should be at least 20 bytes.
  EXPECT_GT(file_size, 20u);
}

// Test that JPG image can be converted to pdf file successfully when scanner
// DPI is specified. Higher DPI results in larger pixel counts, but should
// result in the same PDF page size.
TEST_F(ConvertToPdfTest, ToFileWithDpi) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  // Generate and process 100 DPI image.
  std::vector<std::string> images_100;
  std::vector<uint8_t> bytes_100 = CreateJpg(100, 100);
  images_100.push_back(std::string(bytes_100.begin(), bytes_100.end()));
  auto output_path_100 = temp_dir.GetPath().Append("temp_100.pdf");
  EXPECT_TRUE(ConvertJpgImagesToPdf(images_100, output_path_100,
                                    /*rotate_alternate_pages=*/false,
                                    /*dpi=*/100));
  EXPECT_TRUE(base::PathExists(output_path_100));

  // Generate and process 200 DPI image.
  std::vector<std::string> images_200;
  std::vector<uint8_t> bytes_200 = CreateJpg(200, 200);
  images_200.push_back(std::string(bytes_200.begin(), bytes_200.end()));
  auto output_path_200 = temp_dir.GetPath().Append("temp_200.pdf");
  EXPECT_TRUE(ConvertJpgImagesToPdf(images_200, output_path_200,
                                    /*rotate_alternate_pages=*/false,
                                    /*dpi=*/200));
  EXPECT_TRUE(base::PathExists(output_path_200));

  // Generate and process 300 DPI image.
  std::vector<std::string> images_300;
  std::vector<uint8_t> bytes_300 = CreateJpg(300, 300);
  images_300.push_back(std::string(bytes_300.begin(), bytes_300.end()));
  auto output_path_300 = temp_dir.GetPath().Append("temp_300.pdf");
  EXPECT_TRUE(ConvertJpgImagesToPdf(images_300, output_path_300,
                                    /*rotate_alternate_pages=*/false,
                                    /*dpi=*/300));
  EXPECT_TRUE(base::PathExists(output_path_300));

  // Each file should increase in size as DPI increases.
  int64_t file_size_100;
  int64_t file_size_200;
  int64_t file_size_300;
  EXPECT_TRUE(base::GetFileSize(output_path_100, &file_size_100));
  EXPECT_TRUE(base::GetFileSize(output_path_200, &file_size_200));
  EXPECT_TRUE(base::GetFileSize(output_path_300, &file_size_300));
  EXPECT_GT(file_size_200, file_size_100);
  EXPECT_GT(file_size_300, file_size_200);

  // Verify that the media box is the same size across PDFs.
  const char kMediaBoxString[] = "[0 0 72 72]";
  std::string file_contents;

  EXPECT_TRUE(base::ReadFileToString(output_path_100, &file_contents));
  EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));

  EXPECT_TRUE(base::ReadFileToString(output_path_200, &file_contents));
  EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));

  EXPECT_TRUE(base::ReadFileToString(output_path_300, &file_contents));
  EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));
}

// Test that JPG images can be converted to pdf and saved to vector
// successfully.
TEST_F(ConvertToPdfTest, ToVector) {
  std::vector<uint8_t> pdf_buffer;
  std::vector<uint8_t> buffer_1 = CreateJpg(1, 1);
  ASSERT_FALSE(buffer_1.empty());

  std::vector<std::vector<uint8_t>> jpg_buffers{buffer_1};
  EXPECT_TRUE(ConvertJpgImagesToPdf(jpg_buffers, &pdf_buffer));

  // Smallest PDF should be at least 20 bytes.
  size_t size_1 = pdf_buffer.size();
  EXPECT_GT(size_1, 20u);

  jpg_buffers.push_back(CreateJpg(200, 200));
  jpg_buffers.push_back(CreateJpg(300, 300));
  EXPECT_TRUE(ConvertJpgImagesToPdf(jpg_buffers, &pdf_buffer));
  EXPECT_GT(pdf_buffer.size(), size_1);
}

// Test that passing empty vectors should fail and print error messages.
TEST_F(ConvertToPdfTest, ToVectorEmpty) {
  std::vector<uint8_t> pdf_buffer;
  std::vector<uint8_t> buffer_empty;
  ASSERT_TRUE(buffer_empty.empty());

  std::vector<std::vector<uint8_t>> jpg_buffers{buffer_empty};
  EXPECT_FALSE(ConvertJpgImagesToPdf(jpg_buffers, &pdf_buffer));
}

}  // namespace chromeos