pdf_conversion_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021 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. #include "chromeos/utils/pdf_conversion.h"
  5. #include <fstream>
  6. #include <string>
  7. #include <vector>
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "third_party/skia/include/core/SkBitmap.h"
  13. #include "ui/gfx/image/image.h"
  14. #include "ui/gfx/image/image_unittest_util.h"
  15. #include "ui/gfx/image/image_util.h"
  16. namespace chromeos {
  17. namespace {
  18. // Returns a manually generated JPG image with specified width and height in
  19. // pixels.
  20. std::vector<uint8_t> CreateJpg(int width, int height) {
  21. gfx::Image original = gfx::test::CreateImage(width, height);
  22. std::vector<uint8_t> jpg_buffer;
  23. if (!gfx::JPEG1xEncodedDataFromImage(original, 80, &jpg_buffer)) {
  24. return {};
  25. }
  26. return jpg_buffer;
  27. }
  28. } // namespace
  29. using ConvertToPdfTest = testing::Test;
  30. // Test that JPG image can be converted to pdf file successfully.
  31. TEST_F(ConvertToPdfTest, ToFileNoDpi) {
  32. std::vector<std::string> images;
  33. std::vector<uint8_t> bytes = CreateJpg(100, 100);
  34. images.push_back(std::string(bytes.begin(), bytes.end()));
  35. base::ScopedTempDir temp_dir;
  36. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  37. auto output_path = temp_dir.GetPath().Append("temp.pdf");
  38. EXPECT_TRUE(ConvertJpgImagesToPdf(images, output_path,
  39. /*rotate_alternate_pages=*/false,
  40. /*dpi=*/absl::nullopt));
  41. EXPECT_TRUE(base::PathExists(output_path));
  42. int64_t file_size;
  43. EXPECT_TRUE(base::GetFileSize(output_path, &file_size));
  44. // Smallest PDF should be at least 20 bytes.
  45. EXPECT_GT(file_size, 20u);
  46. }
  47. // Test that JPG image can be converted to pdf file successfully when scanner
  48. // DPI is specified. Higher DPI results in larger pixel counts, but should
  49. // result in the same PDF page size.
  50. TEST_F(ConvertToPdfTest, ToFileWithDpi) {
  51. base::ScopedTempDir temp_dir;
  52. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  53. // Generate and process 100 DPI image.
  54. std::vector<std::string> images_100;
  55. std::vector<uint8_t> bytes_100 = CreateJpg(100, 100);
  56. images_100.push_back(std::string(bytes_100.begin(), bytes_100.end()));
  57. auto output_path_100 = temp_dir.GetPath().Append("temp_100.pdf");
  58. EXPECT_TRUE(ConvertJpgImagesToPdf(images_100, output_path_100,
  59. /*rotate_alternate_pages=*/false,
  60. /*dpi=*/100));
  61. EXPECT_TRUE(base::PathExists(output_path_100));
  62. // Generate and process 200 DPI image.
  63. std::vector<std::string> images_200;
  64. std::vector<uint8_t> bytes_200 = CreateJpg(200, 200);
  65. images_200.push_back(std::string(bytes_200.begin(), bytes_200.end()));
  66. auto output_path_200 = temp_dir.GetPath().Append("temp_200.pdf");
  67. EXPECT_TRUE(ConvertJpgImagesToPdf(images_200, output_path_200,
  68. /*rotate_alternate_pages=*/false,
  69. /*dpi=*/200));
  70. EXPECT_TRUE(base::PathExists(output_path_200));
  71. // Generate and process 300 DPI image.
  72. std::vector<std::string> images_300;
  73. std::vector<uint8_t> bytes_300 = CreateJpg(300, 300);
  74. images_300.push_back(std::string(bytes_300.begin(), bytes_300.end()));
  75. auto output_path_300 = temp_dir.GetPath().Append("temp_300.pdf");
  76. EXPECT_TRUE(ConvertJpgImagesToPdf(images_300, output_path_300,
  77. /*rotate_alternate_pages=*/false,
  78. /*dpi=*/300));
  79. EXPECT_TRUE(base::PathExists(output_path_300));
  80. // Each file should increase in size as DPI increases.
  81. int64_t file_size_100;
  82. int64_t file_size_200;
  83. int64_t file_size_300;
  84. EXPECT_TRUE(base::GetFileSize(output_path_100, &file_size_100));
  85. EXPECT_TRUE(base::GetFileSize(output_path_200, &file_size_200));
  86. EXPECT_TRUE(base::GetFileSize(output_path_300, &file_size_300));
  87. EXPECT_GT(file_size_200, file_size_100);
  88. EXPECT_GT(file_size_300, file_size_200);
  89. // Verify that the media box is the same size across PDFs.
  90. const char kMediaBoxString[] = "[0 0 72 72]";
  91. std::string file_contents;
  92. EXPECT_TRUE(base::ReadFileToString(output_path_100, &file_contents));
  93. EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));
  94. EXPECT_TRUE(base::ReadFileToString(output_path_200, &file_contents));
  95. EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));
  96. EXPECT_TRUE(base::ReadFileToString(output_path_300, &file_contents));
  97. EXPECT_THAT(file_contents, testing::HasSubstr(kMediaBoxString));
  98. }
  99. // Test that JPG image can be converted to pdf and saved to vector successfully.
  100. TEST_F(ConvertToPdfTest, ToVector) {
  101. std::vector<uint8_t> jpg_buffer = CreateJpg(100, 100);
  102. ASSERT_FALSE(jpg_buffer.empty());
  103. std::vector<uint8_t> pdf_buffer;
  104. EXPECT_TRUE(ConvertJpgImageToPdf(jpg_buffer, &pdf_buffer));
  105. // Smallest PDF should be at least 20 bytes.
  106. EXPECT_GT(pdf_buffer.size(), 20u);
  107. }
  108. } // namespace chromeos