#include "pdf/ui/thumbnail.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_f.h"
namespace chrome_pdf {
namespace {
constexpr float kDeviceToPixelLow = 1;
constexpr float kDeviceToPixelHigh = 2;
struct BestFitSizeParams {
float device_pixel_ratio;
gfx::SizeF page_size;
gfx::Size expected_thumbnail_size;
};
void TestBestFitSize(const BestFitSizeParams& params) {
Thumbnail thumbnail(params.page_size, params.device_pixel_ratio);
EXPECT_EQ(thumbnail.image_size(), params.expected_thumbnail_size)
<< "Failed for ctor with page of size of " << params.page_size.ToString()
<< " and device to pixel ratio of " << params.device_pixel_ratio;
EXPECT_EQ(Thumbnail::CalculateImageSize(params.page_size,
params.device_pixel_ratio),
params.expected_thumbnail_size)
<< "Failed for CalculateImageSize() with page of size of "
<< params.page_size.ToString() << " and device to pixel ratio of "
<< params.device_pixel_ratio;
}
TEST(ThumbnailTest, CalculateBestFitSizeNormal) {
static constexpr BestFitSizeParams kBestFitSizeTestParams[] = {
{kDeviceToPixelLow, {612, 792}, {108, 140}},
{kDeviceToPixelLow, {595, 842}, {108, 152}},
{kDeviceToPixelLow, {200, 200}, {140, 140}},
{kDeviceToPixelLow, {1000, 200}, {540, 108}},
{kDeviceToPixelLow, {200, 1000}, {108, 540}},
{kDeviceToPixelLow, {1500, 50}, {1399, 46}},
{kDeviceToPixelLow, {50, 1500}, {46, 1399}},
{kDeviceToPixelHigh, {612, 792}, {216, 280}},
{kDeviceToPixelHigh, {595, 842}, {214, 303}},
{kDeviceToPixelHigh, {200, 200}, {255, 255}},
{kDeviceToPixelHigh, {1000, 200}, {571, 114}},
{kDeviceToPixelHigh, {200, 1000}, {114, 571}},
{kDeviceToPixelHigh, {1500, 50}, {1399, 46}},
{kDeviceToPixelHigh, {50, 1500}, {46, 1399}},
};
for (const auto& params : kBestFitSizeTestParams) {
TestBestFitSize(params);
}
}
TEST(ThumbnailTest, CalculateBestFitSizeFractional) {
static constexpr BestFitSizeParams kBestFitSizeTestParams[] = {
{kDeviceToPixelLow, {611.976f, 791.968f}, {108, 140}},
{kDeviceToPixelLow, {595.35f, 841.995f}, {108, 152}},
{kDeviceToPixelHigh, {611.976f, 791.968f}, {216, 280}},
{kDeviceToPixelHigh, {595.35f, 841.995f}, {214, 303}},
};
for (const auto& params : kBestFitSizeTestParams) {
TestBestFitSize(params);
}
}
TEST(ThumbnailTest, CalculateBestFitSizeLargeAspectRatio) {
static constexpr BestFitSizeParams kBestFitSizeTestParams[] = {
{kDeviceToPixelLow, {14400, 3}, {17701, 3}},
{kDeviceToPixelLow, {3, 14400}, {3, 17701}},
{kDeviceToPixelLow, {0, 0}, {140, 140}},
{kDeviceToPixelLow, {9999999, 1}, {17701, 3}},
{kDeviceToPixelLow, {1, 9999999}, {3, 17701}},
{kDeviceToPixelHigh, {14400, 3}, {17701, 3}},
{kDeviceToPixelHigh, {3, 14400}, {3, 17701}},
{kDeviceToPixelHigh, {0, 0}, {255, 255}},
{kDeviceToPixelHigh, {9999999, 1}, {17701, 3}},
{kDeviceToPixelHigh, {1, 9999999}, {3, 17701}},
};
for (const auto& params : kBestFitSizeTestParams) {
TestBestFitSize(params);
}
}
TEST(ThumbnailTest, CalculateBestFitSizeNoOverflow) {
static constexpr BestFitSizeParams kBestFitSizeTestParams[] = {
{kDeviceToPixelLow, {9999999, 9999999}, {140, 140}},
{kDeviceToPixelHigh, {9999999, 9999999}, {255, 255}},
};
for (const auto& params : kBestFitSizeTestParams) {
TestBestFitSize(params);
}
}
}
}