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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/thumbnail/cc/jpeg_thumbnail_helper.h"
#include <cstring>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "chrome/browser/thumbnail/cc/thumbnail.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/gfx/codec/jpeg_codec.h"
namespace thumbnail {
namespace {
constexpr int kDimension = 16;
constexpr int kKiB = 1024;
SkPaint SetupPaint() {
SkColor colors[] = {SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE};
SkScalar pos[] = {0, SK_Scalar1 / 2, SK_Scalar1};
SkPaint paint;
paint.setShader(SkGradientShader::MakeSweep(256, 256, colors, pos, 3));
return paint;
}
} // anonymous namespace
class JpegThumbnailHelperTest : public ::testing::Test {
protected:
JpegThumbnailHelperTest()
: task_environment_(
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED) {}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
interface_ = std::make_unique<thumbnail::JpegThumbnailHelper>(
temp_dir_.GetPath(),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
}
void TearDown() override {}
thumbnail::JpegThumbnailHelper& GetInterface() { return *interface_; }
base::FilePath GetFile(int tab_id) {
return interface_->GetJpegFilePath(tab_id);
}
base::test::TaskEnvironment task_environment_;
private:
std::unique_ptr<thumbnail::JpegThumbnailHelper> interface_;
base::ScopedTempDir temp_dir_;
};
TEST_F(JpegThumbnailHelperTest, CompressThumbnail) {
// Create a bitmap
SkBitmap image;
ASSERT_TRUE(image.tryAllocN32Pixels(kDimension * kKiB, kDimension));
SkCanvas canvas(image);
canvas.drawPaint(SetupPaint());
image.setImmutable();
// Compress the bitmap
base::RunLoop loop1;
base::OnceCallback<void(std::vector<uint8_t>)> once =
base::BindOnce([](std::vector<uint8_t> jpeg_data) {
EXPECT_FALSE(jpeg_data.empty());
SkBitmap bitmap = gfx::JPEGCodec::Decode(jpeg_data);
EXPECT_FALSE(bitmap.isNull());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}).Then(loop1.QuitClosure());
GetInterface().Compress(image, std::move(once));
task_environment_.RunUntilIdle();
loop1.Run();
}
TEST_F(JpegThumbnailHelperTest, WriteThumbnail) {
int tab_id = 0;
// Create a bitmap.
SkBitmap image;
ASSERT_TRUE(image.tryAllocN32Pixels(kDimension * kKiB, kDimension));
SkCanvas canvas(image);
canvas.drawPaint(SetupPaint());
image.setImmutable();
constexpr int kCompressionQuality = 97;
std::optional<std::vector<uint8_t>> data =
gfx::JPEGCodec::Encode(image, kCompressionQuality);
// Write the image.
base::RunLoop loop1;
GetInterface().Write(tab_id, data.value(),
base::BindOnce(
[](base::OnceClosure quit, bool success) {
EXPECT_TRUE(success);
std::move(quit).Run();
},
loop1.QuitClosure()));
task_environment_.RunUntilIdle();
loop1.Run();
base::FilePath file_path = GetFile(tab_id);
EXPECT_TRUE(base::PathExists(file_path));
// Compare original data with written data.
std::optional<std::vector<uint8_t>> read_data =
base::ReadFileToBytes(file_path);
EXPECT_EQ(data, read_data);
}
TEST_F(JpegThumbnailHelperTest, ReadThumbnail) {
int tab_id = 0;
// Create a bitmap
SkBitmap image;
ASSERT_TRUE(image.tryAllocN32Pixels(kDimension * kKiB, kDimension));
SkCanvas canvas(image);
canvas.drawPaint(SetupPaint());
image.setImmutable();
constexpr int kCompressionQuality = 97;
std::optional<std::vector<uint8_t>> data =
gfx::JPEGCodec::Encode(image, kCompressionQuality);
// Write the image
base::FilePath file_path = GetFile(tab_id);
base::File file(file_path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
file.Write(0, reinterpret_cast<const char*>(data.value().data()),
data.value().size());
// Read the image
base::RunLoop loop1;
base::OnceCallback<void(std::optional<std::vector<uint8_t>>)> once =
base::BindOnce([](std::optional<std::vector<uint8_t>> compressed_data) {
ASSERT_TRUE(compressed_data.has_value());
EXPECT_FALSE(compressed_data->empty());
SkBitmap bitmap = gfx::JPEGCodec::Decode(compressed_data.value());
EXPECT_FALSE(bitmap.isNull());
EXPECT_GT(bitmap.width(), 0);
EXPECT_GT(bitmap.height(), 0);
}).Then(loop1.QuitClosure());
GetInterface().Read(tab_id, std::move(once));
task_environment_.RunUntilIdle();
loop1.Run();
}
TEST_F(JpegThumbnailHelperTest, DeleteThumbnail) {
int tab_id = 0;
// Create a bitmap
SkBitmap image;
ASSERT_TRUE(image.tryAllocN32Pixels(kDimension * kKiB, kDimension));
SkCanvas canvas(image);
canvas.drawPaint(SetupPaint());
image.setImmutable();
constexpr int kCompressionQuality = 97;
std::optional<std::vector<uint8_t>> data =
gfx::JPEGCodec::Encode(image, kCompressionQuality);
// Write the image
base::FilePath file_path = GetFile(tab_id);
base::File file(file_path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
file.Write(0, reinterpret_cast<const char*>(data->data()), data->size());
// Delete the image
GetInterface().Delete(tab_id);
task_environment_.RunUntilIdle();
// Check deletion occurred
base::FilePath post_delete_file_path = GetFile(tab_id);
EXPECT_FALSE(base::PathExists(post_delete_file_path));
}
} // namespace thumbnail
|