Commit e59d30f9 authored by Alan Cutter's avatar Alan Cutter Committed by Commit Bot

WebApps: Remove unused BitmapAndSource struct

With the change to split WebApplicationInfo::icons into icon_infos and
icon_bitmaps [1] we don't need to preserve URLs when downloading icons.
This means we can simplfy the BitmapAndSource struct into just SkBitmap.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/1928600/10

Bug: 926083
Change-Id: I9c36d1bf1e726aeb6d1f79f1bdab005b8032265a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1936441
Commit-Queue: Alan Cutter <alancutter@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720830}
parent 59630335
......@@ -36,7 +36,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
public:
explicit GeneratedIconImageSource(base::char16 letter,
SkColor color,
int output_size)
SquareSizePx output_size)
: gfx::CanvasImageSource(gfx::Size(output_size, output_size)),
letter_(letter),
color_(color),
......@@ -46,7 +46,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
private:
// gfx::CanvasImageSource overrides:
void Draw(gfx::Canvas* canvas) override {
const int icon_size = output_size_ * 3 / 4;
const SquareSizePx icon_size = output_size_ * 3 / 4;
const int icon_inset = output_size_ / 8;
const size_t border_radius = output_size_ / 16;
const size_t font_size = output_size_ * 7 / 16;
......@@ -88,21 +88,21 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
// to |bitmaps| by drawing the given |letter| into a rounded background of
// |color|. For each size, if an icon of the requested size already exists in
// |bitmaps|, nothing will happen.
void GenerateIcon(std::map<int, BitmapAndSource>* bitmaps,
int output_size,
void GenerateIcon(std::map<SquareSizePx, SkBitmap>* bitmaps,
SquareSizePx output_size,
SkColor color,
base::char16 letter) {
// Do nothing if there is already an icon of |output_size|.
if (bitmaps->count(output_size))
return;
(*bitmaps)[output_size].bitmap = GenerateBitmap(output_size, color, letter);
(*bitmaps)[output_size] = GenerateBitmap(output_size, color, letter);
}
void GenerateIcons(std::set<int> generate_sizes,
void GenerateIcons(std::set<SquareSizePx> generate_sizes,
const GURL& app_url,
SkColor generated_icon_color,
std::map<int, BitmapAndSource>* bitmap_map) {
std::map<SquareSizePx, SkBitmap>* bitmap_map) {
// If no color has been specified, use a dark gray so it will stand out on the
// black shelf.
if (generated_icon_color == SK_ColorTRANSPARENT)
......@@ -110,14 +110,14 @@ void GenerateIcons(std::set<int> generate_sizes,
const base::char16 icon_letter = GenerateIconLetterFromUrl(app_url);
for (int size : generate_sizes)
for (SquareSizePx size : generate_sizes)
GenerateIcon(bitmap_map, size, generated_icon_color, icon_letter);
}
} // namespace
std::set<int> SizesToGenerate() {
return std::set<int>({
std::set<SquareSizePx> SizesToGenerate() {
return std::set<SquareSizePx>({
icon_size::k32,
icon_size::k64,
icon_size::k48,
......@@ -127,23 +127,14 @@ std::set<int> SizesToGenerate() {
});
}
BitmapAndSource::BitmapAndSource() {}
BitmapAndSource::BitmapAndSource(const GURL& source_url_p,
const SkBitmap& bitmap_p)
: source_url(source_url_p), bitmap(bitmap_p) {}
BitmapAndSource::~BitmapAndSource() {}
std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
const std::vector<BitmapAndSource>& bitmaps,
const std::set<int>& sizes) {
std::map<int, BitmapAndSource> output_bitmaps;
std::map<int, BitmapAndSource> ordered_bitmaps;
for (const BitmapAndSource& bitmap_and_source : bitmaps) {
const SkBitmap& bitmap = bitmap_and_source.bitmap;
std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<SkBitmap>& bitmaps,
const std::set<SquareSizePx>& sizes) {
std::map<SquareSizePx, SkBitmap> output_bitmaps;
std::map<SquareSizePx, SkBitmap> ordered_bitmaps;
for (const SkBitmap& bitmap : bitmaps) {
DCHECK(bitmap.width() == bitmap.height());
ordered_bitmaps[bitmap.width()] = bitmap_and_source;
ordered_bitmaps[bitmap.width()] = bitmap;
}
if (!ordered_bitmaps.empty()) {
......@@ -157,10 +148,10 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
output_bitmaps[size] = ordered_bitmaps.rbegin()->second;
// Resize the bitmap if it does not exactly match the desired size.
if (output_bitmaps[size].bitmap.width() != size) {
output_bitmaps[size].bitmap = skia::ImageOperations::Resize(
output_bitmaps[size].bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
size, size);
if (output_bitmaps[size].width() != size) {
output_bitmaps[size] = skia::ImageOperations::Resize(
output_bitmaps[size], skia::ImageOperations::RESIZE_LANCZOS3, size,
size);
}
}
}
......@@ -168,7 +159,9 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
return output_bitmaps;
}
SkBitmap GenerateBitmap(int output_size, SkColor color, base::char16 letter) {
SkBitmap GenerateBitmap(SquareSizePx output_size,
SkColor color,
base::char16 letter) {
gfx::ImageSkia icon_image(
std::make_unique<GeneratedIconImageSource>(letter, color, output_size),
gfx::Size(output_size, output_size));
......@@ -202,22 +195,22 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url) {
return icon_letter;
}
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons,
const std::set<int>& sizes_to_generate,
std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<SkBitmap>& icons,
const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url,
SkColor* generated_icon_color) {
DCHECK(generated_icon_color);
// Resize provided icons to make sure we have versions for each size in
// |sizes_to_generate|.
std::map<int, BitmapAndSource> resized_bitmaps(
std::map<SquareSizePx, SkBitmap> resized_bitmaps(
ConstrainBitmapsToSizes(icons, sizes_to_generate));
// Also add all provided icon sizes.
for (const BitmapAndSource& icon : icons) {
if (resized_bitmaps.find(icon.bitmap.width()) == resized_bitmaps.end())
resized_bitmaps.insert(std::make_pair(icon.bitmap.width(), icon));
for (const SkBitmap& icon : icons) {
if (resized_bitmaps.find(icon.width()) == resized_bitmaps.end())
resized_bitmaps.insert(std::make_pair(icon.width(), icon));
}
// Determine the color that will be used for the icon's background. For this
......@@ -225,13 +218,13 @@ std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
if (!resized_bitmaps.empty()) {
color_utils::GridSampler sampler;
*generated_icon_color = color_utils::CalculateKMeanColorOfBitmap(
resized_bitmaps.begin()->second.bitmap);
resized_bitmaps.begin()->second);
}
// Work out what icons we need to generate here. Icons are only generated if
// there is no icon in the required size.
std::set<int> generate_sizes;
for (int size : sizes_to_generate) {
std::set<SquareSizePx> generate_sizes;
for (SquareSizePx size : sizes_to_generate) {
if (resized_bitmaps.find(size) == resized_bitmaps.end())
generate_sizes.insert(size);
}
......
......@@ -38,30 +38,21 @@ enum {
} // namespace icon_size
// Returns icon sizes to be generated from downloaded icons.
std::set<int> SizesToGenerate();
// TODO(https://crbug.com/926083): Replace all occurrences of this with
// SkBitmap. We now record the URL in WebApplicationIconInfo instead.
struct BitmapAndSource {
BitmapAndSource();
BitmapAndSource(const GURL& source_url_p, const SkBitmap& bitmap_p);
~BitmapAndSource();
GURL source_url;
SkBitmap bitmap;
};
std::set<SquareSizePx> SizesToGenerate();
// This finds the closest not-smaller bitmap in |bitmaps| for each size in
// |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
// which contains only bitmaps of a size in |sizes| and at most one bitmap of
// each size.
std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
const std::vector<BitmapAndSource>& bitmaps,
const std::set<int>& sizes);
std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<SkBitmap>& bitmaps,
const std::set<SquareSizePx>& sizes);
// Generates a square container icon of |output_size| by drawing the given
// |letter| into a rounded background of |color|.
SkBitmap GenerateBitmap(int output_size, SkColor color, base::char16 letter);
SkBitmap GenerateBitmap(SquareSizePx output_size,
SkColor color,
base::char16 letter);
// Returns the letter that will be painted on the generated icon.
base::char16 GenerateIconLetterFromUrl(const GURL& app_url);
......@@ -70,9 +61,9 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url);
// Note that |app_url| is the launch URL for the app.
// Output: |generated_icon_color| is the color to use if an icon needs to be
// generated for the web app.
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons,
const std::set<int>& sizes_to_generate,
std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<SkBitmap>& icons,
const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url,
SkColor* generated_icon_color);
......
......@@ -27,13 +27,6 @@ const GURL kAppIconURL3("http://foo.com/3.png");
const int kIconSizeSmallBetweenMediumAndLarge = 63;
const int kIconSizeLargeBetweenMediumAndLarge = 96;
BitmapAndSource CreateSquareIcon(const GURL& gurl, int size, SkColor color) {
SkBitmap bitmap;
bitmap.allocN32Pixels(size, size);
bitmap.eraseColor(color);
return BitmapAndSource(gurl, bitmap);
}
std::set<int> TestSizesToGenerate() {
const int kIconSizesToGenerate[] = {
icon_size::k32, icon_size::k48, icon_size::k128,
......@@ -43,84 +36,76 @@ std::set<int> TestSizesToGenerate() {
}
void ValidateAllIconsWithURLsArePresent(
const std::vector<BitmapAndSource>& icons_to_check,
const std::map<int, BitmapAndSource>& size_map) {
EXPECT_EQ(icons_to_check.size(), size_map.size());
// Check that every icon with URL has a mapped icon.
for (const auto& icon : icons_to_check) {
if (!icon.source_url.is_empty()) {
bool found = false;
if (base::Contains(size_map, icon.bitmap.width())) {
const BitmapAndSource& mapped_icon = size_map.at(icon.bitmap.width());
if (mapped_icon.source_url == icon.source_url &&
mapped_icon.bitmap.width() == icon.bitmap.width()) {
found = true;
}
}
EXPECT_TRUE(found);
const std::vector<SkBitmap>& bitmaps_to_check,
const std::map<int, SkBitmap>& size_map) {
EXPECT_EQ(bitmaps_to_check.size(), size_map.size());
// Check that every icon has a mapped icon.
for (const SkBitmap& bitmap : bitmaps_to_check) {
bool found = false;
if (base::Contains(size_map, bitmap.width())) {
const SkBitmap& mapped_icon = size_map.at(bitmap.width());
if (mapped_icon.width() == bitmap.width())
found = true;
}
EXPECT_TRUE(found);
}
}
std::vector<BitmapAndSource>::const_iterator FindLargestBitmapAndSourceVector(
const std::vector<BitmapAndSource>& bitmap_vector) {
std::vector<SkBitmap>::const_iterator FindLargestSkBitmapVector(
const std::vector<SkBitmap>& bitmap_vector) {
auto result = bitmap_vector.end();
int largest = -1;
for (auto it = bitmap_vector.begin(); it != bitmap_vector.end(); ++it) {
if (it->bitmap.width() > largest) {
if (it->width() > largest) {
result = it;
}
}
return result;
}
std::vector<BitmapAndSource>::const_iterator FindMatchingBitmapAndSourceVector(
const std::vector<BitmapAndSource>& bitmap_vector,
std::vector<SkBitmap>::const_iterator FindMatchingSkBitmapVector(
const std::vector<SkBitmap>& bitmap_vector,
int size) {
for (auto it = bitmap_vector.begin(); it != bitmap_vector.end(); ++it) {
if (it->bitmap.width() == size) {
if (it->width() == size) {
return it;
}
}
return bitmap_vector.end();
}
std::vector<BitmapAndSource>::const_iterator
FindEqualOrLargerBitmapAndSourceVector(
const std::vector<BitmapAndSource>& bitmap_vector,
std::vector<SkBitmap>::const_iterator FindEqualOrLargerSkBitmapVector(
const std::vector<SkBitmap>& bitmap_vector,
int size) {
for (auto it = bitmap_vector.begin(); it != bitmap_vector.end(); ++it) {
if (it->bitmap.width() >= size) {
if (it->width() >= size) {
return it;
}
}
return bitmap_vector.end();
}
void ValidateIconsGeneratedAndResizedCorrectly(
std::vector<BitmapAndSource> downloaded,
std::map<int, BitmapAndSource> size_map,
std::set<int> sizes_to_generate,
int expected_generated,
int expected_resized) {
void ValidateIconsGeneratedAndResizedCorrectly(std::vector<SkBitmap> downloaded,
std::map<int, SkBitmap> size_map,
std::set<int> sizes_to_generate,
int expected_generated,
int expected_resized) {
GURL empty_url("");
int number_generated = 0;
int number_resized = 0;
auto icon_largest = FindLargestBitmapAndSourceVector(downloaded);
auto icon_largest = FindLargestSkBitmapVector(downloaded);
for (const auto& size : sizes_to_generate) {
auto icon_downloaded = FindMatchingBitmapAndSourceVector(downloaded, size);
auto icon_larger = FindEqualOrLargerBitmapAndSourceVector(downloaded, size);
auto icon_downloaded = FindMatchingSkBitmapVector(downloaded, size);
auto icon_larger = FindEqualOrLargerSkBitmapVector(downloaded, size);
if (icon_downloaded == downloaded.end()) {
auto icon_resized = size_map.find(size);
if (icon_largest == downloaded.end()) {
// There are no downloaded icons. Expect an icon to be generated.
EXPECT_NE(size_map.end(), icon_resized);
EXPECT_EQ(size, icon_resized->second.bitmap.width());
EXPECT_EQ(size, icon_resized->second.bitmap.height());
EXPECT_EQ(size, icon_resized->second.bitmap.height());
EXPECT_EQ(empty_url, icon_resized->second.source_url);
EXPECT_EQ(size, icon_resized->second.width());
EXPECT_EQ(size, icon_resized->second.height());
++number_generated;
} else {
// If there is a larger downloaded icon, it should be resized. Otherwise
......@@ -129,10 +114,8 @@ void ValidateIconsGeneratedAndResizedCorrectly(
if (icon_larger != downloaded.end())
icon_to_resize = icon_larger;
EXPECT_NE(size_map.end(), icon_resized);
EXPECT_EQ(size, icon_resized->second.bitmap.width());
EXPECT_EQ(size, icon_resized->second.bitmap.height());
EXPECT_EQ(size, icon_resized->second.bitmap.height());
EXPECT_EQ(icon_to_resize->source_url, icon_resized->second.source_url);
EXPECT_EQ(size, icon_resized->second.width());
EXPECT_EQ(size, icon_resized->second.height());
++number_resized;
}
} else {
......@@ -140,11 +123,10 @@ void ValidateIconsGeneratedAndResizedCorrectly(
// generated, and the existing downloaded icon to be used.
auto icon_resized = size_map.find(size);
EXPECT_NE(size_map.end(), icon_resized);
EXPECT_EQ(size, icon_resized->second.bitmap.width());
EXPECT_EQ(size, icon_resized->second.bitmap.height());
EXPECT_EQ(size, icon_downloaded->bitmap.width());
EXPECT_EQ(size, icon_downloaded->bitmap.height());
EXPECT_EQ(icon_downloaded->source_url, icon_resized->second.source_url);
EXPECT_EQ(size, icon_resized->second.width());
EXPECT_EQ(size, icon_resized->second.height());
EXPECT_EQ(size, icon_downloaded->width());
EXPECT_EQ(size, icon_downloaded->height());
}
}
EXPECT_EQ(expected_generated, number_generated);
......@@ -161,10 +143,10 @@ void ValidateBitmapSizeAndColor(SkBitmap bitmap, int size, SkColor color) {
void TestIconGeneration(int icon_size,
int expected_generated,
int expected_resized) {
std::vector<BitmapAndSource> downloaded;
std::vector<SkBitmap> downloaded;
// Add an icon with a URL and bitmap. 'Download' it.
downloaded.push_back(CreateSquareIcon(kAppIconURL1, icon_size, SK_ColorRED));
// Add an icon. 'Download' it.
downloaded.push_back(CreateSquareIcon(icon_size, SK_ColorRED));
// Now run the resizing/generation and validation.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
......@@ -200,83 +182,82 @@ TEST_F(WebAppIconGeneratorTest, ConstrainBitmapsToSizes) {
desired_sizes.insert(256);
{
std::vector<BitmapAndSource> bitmaps;
bitmaps.push_back(CreateSquareIcon(GURL(), 16, SK_ColorRED));
bitmaps.push_back(CreateSquareIcon(GURL(), 32, SK_ColorGREEN));
bitmaps.push_back(CreateSquareIcon(GURL(), 144, SK_ColorYELLOW));
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateSquareIcon(16, SK_ColorRED));
bitmaps.push_back(CreateSquareIcon(32, SK_ColorGREEN));
bitmaps.push_back(CreateSquareIcon(144, SK_ColorYELLOW));
std::map<int, BitmapAndSource> results =
std::map<int, SkBitmap> results =
ConstrainBitmapsToSizes(bitmaps, desired_sizes);
EXPECT_EQ(6u, results.size());
ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorRED);
ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorGREEN);
ValidateBitmapSizeAndColor(results[48].bitmap, 48, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[96].bitmap, 96, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[128].bitmap, 128, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[256].bitmap, 256, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[16], 16, SK_ColorRED);
ValidateBitmapSizeAndColor(results[32], 32, SK_ColorGREEN);
ValidateBitmapSizeAndColor(results[48], 48, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[96], 96, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[128], 128, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[256], 256, SK_ColorYELLOW);
}
{
std::vector<BitmapAndSource> bitmaps;
bitmaps.push_back(CreateSquareIcon(GURL(), 512, SK_ColorRED));
bitmaps.push_back(CreateSquareIcon(GURL(), 18, SK_ColorGREEN));
bitmaps.push_back(CreateSquareIcon(GURL(), 33, SK_ColorBLUE));
bitmaps.push_back(CreateSquareIcon(GURL(), 17, SK_ColorYELLOW));
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateSquareIcon(512, SK_ColorRED));
bitmaps.push_back(CreateSquareIcon(18, SK_ColorGREEN));
bitmaps.push_back(CreateSquareIcon(33, SK_ColorBLUE));
bitmaps.push_back(CreateSquareIcon(17, SK_ColorYELLOW));
std::map<int, BitmapAndSource> results =
std::map<int, SkBitmap> results =
ConstrainBitmapsToSizes(bitmaps, desired_sizes);
EXPECT_EQ(6u, results.size());
ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorBLUE);
ValidateBitmapSizeAndColor(results[48].bitmap, 48, SK_ColorRED);
ValidateBitmapSizeAndColor(results[96].bitmap, 96, SK_ColorRED);
ValidateBitmapSizeAndColor(results[128].bitmap, 128, SK_ColorRED);
ValidateBitmapSizeAndColor(results[256].bitmap, 256, SK_ColorRED);
ValidateBitmapSizeAndColor(results[16], 16, SK_ColorYELLOW);
ValidateBitmapSizeAndColor(results[32], 32, SK_ColorBLUE);
ValidateBitmapSizeAndColor(results[48], 48, SK_ColorRED);
ValidateBitmapSizeAndColor(results[96], 96, SK_ColorRED);
ValidateBitmapSizeAndColor(results[128], 128, SK_ColorRED);
ValidateBitmapSizeAndColor(results[256], 256, SK_ColorRED);
}
}
TEST_F(WebAppIconGeneratorTest, LinkedAppIconsAreNotChanged) {
std::vector<BitmapAndSource> icons;
std::vector<SkBitmap> icons;
const GURL url = kAppIconURL3;
const SkColor color = SK_ColorBLACK;
icons.push_back(CreateSquareIcon(url, icon_size::k48, color));
icons.push_back(CreateSquareIcon(url, icon_size::k32, color));
icons.push_back(CreateSquareIcon(url, icon_size::k128, color));
icons.push_back(CreateSquareIcon(icon_size::k48, color));
icons.push_back(CreateSquareIcon(icon_size::k32, color));
icons.push_back(CreateSquareIcon(icon_size::k128, color));
// 'Download' one of the icons without a size or bitmap.
std::vector<BitmapAndSource> downloaded;
downloaded.push_back(CreateSquareIcon(url, icon_size::k128, color));
std::vector<SkBitmap> downloaded;
downloaded.push_back(CreateSquareIcon(icon_size::k128, color));
const auto& sizes = TestSizesToGenerate();
// Now run the resizing and generation into a new web icons info.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
std::map<int, BitmapAndSource> size_map = ResizeIconsAndGenerateMissing(
std::map<int, SkBitmap> size_map = ResizeIconsAndGenerateMissing(
downloaded, sizes, GURL(), &generated_icon_color);
EXPECT_EQ(sizes.size(), size_map.size());
// Now check that the linked app icons (i.e. those with URLs) are matching.
// Now check that the linked app icons are matching.
ValidateAllIconsWithURLsArePresent(icons, size_map);
}
TEST_F(WebAppIconGeneratorTest, IconsResizedFromOddSizes) {
std::vector<BitmapAndSource> downloaded;
std::vector<SkBitmap> downloaded;
const SkColor color = SK_ColorRED;
// Add three icons with a URL and bitmap. 'Download' each of them.
downloaded.push_back(CreateSquareIcon(kAppIconURL1, icon_size::k32, color));
downloaded.push_back(CreateSquareIcon(
kAppIconURL2, kIconSizeSmallBetweenMediumAndLarge, color));
downloaded.push_back(CreateSquareIcon(
kAppIconURL3, kIconSizeLargeBetweenMediumAndLarge, color));
// Add three icons. 'Download' each of them.
downloaded.push_back(CreateSquareIcon(icon_size::k32, color));
downloaded.push_back(
CreateSquareIcon(kIconSizeSmallBetweenMediumAndLarge, color));
downloaded.push_back(
CreateSquareIcon(kIconSizeLargeBetweenMediumAndLarge, color));
// Now run the resizing and generation.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
std::map<int, BitmapAndSource> size_map = ResizeIconsAndGenerateMissing(
std::map<int, SkBitmap> size_map = ResizeIconsAndGenerateMissing(
downloaded, TestSizesToGenerate(), GURL(), &generated_icon_color);
// No icons should be generated. The LARGE and MEDIUM sizes should be resized.
......@@ -285,18 +266,16 @@ TEST_F(WebAppIconGeneratorTest, IconsResizedFromOddSizes) {
}
TEST_F(WebAppIconGeneratorTest, IconsResizedFromLarger) {
std::vector<BitmapAndSource> downloaded;
std::vector<SkBitmap> downloaded;
// Add three icons with a URL and bitmap. 'Download' two of them and pretend
// Add three icons. 'Download' two of them and pretend
// the third failed to download.
downloaded.push_back(
CreateSquareIcon(kAppIconURL1, icon_size::k32, SK_ColorRED));
downloaded.push_back(
CreateSquareIcon(kAppIconURL3, icon_size::k512, SK_ColorBLACK));
downloaded.push_back(CreateSquareIcon(icon_size::k32, SK_ColorRED));
downloaded.push_back(CreateSquareIcon(icon_size::k512, SK_ColorBLACK));
// Now run the resizing and generation.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
std::map<int, BitmapAndSource> size_map = ResizeIconsAndGenerateMissing(
std::map<int, SkBitmap> size_map = ResizeIconsAndGenerateMissing(
downloaded, TestSizesToGenerate(), GURL(), &generated_icon_color);
// Expect icon for MEDIUM and LARGE to be resized from the gigantor icon
......@@ -306,12 +285,12 @@ TEST_F(WebAppIconGeneratorTest, IconsResizedFromLarger) {
}
TEST_F(WebAppIconGeneratorTest, AllIconsGeneratedWhenNotDownloaded) {
// Add three icons with a URL and bitmap. 'Download' none of them.
std::vector<BitmapAndSource> downloaded;
// Add three icons. 'Download' none of them.
std::vector<SkBitmap> downloaded;
// Now run the resizing and generation.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
std::map<int, BitmapAndSource> size_map = ResizeIconsAndGenerateMissing(
std::map<int, SkBitmap> size_map = ResizeIconsAndGenerateMissing(
downloaded, TestSizesToGenerate(), GURL(), &generated_icon_color);
// Expect all icons to be generated.
......@@ -320,17 +299,15 @@ TEST_F(WebAppIconGeneratorTest, AllIconsGeneratedWhenNotDownloaded) {
}
TEST_F(WebAppIconGeneratorTest, IconResizedFromLargerAndSmaller) {
std::vector<BitmapAndSource> downloaded;
std::vector<SkBitmap> downloaded;
// Pretend the huge icon wasn't downloaded but two smaller ones were.
downloaded.push_back(
CreateSquareIcon(kAppIconURL1, icon_size::k16, SK_ColorRED));
downloaded.push_back(
CreateSquareIcon(kAppIconURL2, icon_size::k48, SK_ColorBLUE));
downloaded.push_back(CreateSquareIcon(icon_size::k16, SK_ColorRED));
downloaded.push_back(CreateSquareIcon(icon_size::k48, SK_ColorBLUE));
// Now run the resizing and generation.
SkColor generated_icon_color = SK_ColorTRANSPARENT;
std::map<int, BitmapAndSource> size_map = ResizeIconsAndGenerateMissing(
std::map<int, SkBitmap> size_map = ResizeIconsAndGenerateMissing(
downloaded, TestSizesToGenerate(), GURL(), &generated_icon_color);
// Expect no icons to be generated, but the LARGE and SMALL icons to be
......@@ -341,7 +318,6 @@ TEST_F(WebAppIconGeneratorTest, IconResizedFromLargerAndSmaller) {
// Verify specifically that the LARGE icons was resized from the medium icon.
const auto it = size_map.find(icon_size::k128);
EXPECT_NE(size_map.end(), it);
EXPECT_EQ(kAppIconURL2, it->second.source_url);
}
TEST_F(WebAppIconGeneratorTest, IconsResizedWhenOnlyATinyOneIsProvided) {
......
......@@ -25,15 +25,15 @@ namespace web_app {
namespace {
constexpr int kMaxIcons = 20;
constexpr int kMaxIconSize = 1024;
constexpr SquareSizePx kMaxIconSize = 1024;
// Get a list of non-empty square icons from |icons_map|.
void FilterSquareIconsFromMap(const IconsMap& icons_map,
std::vector<BitmapAndSource>* square_icons) {
std::vector<SkBitmap>* square_icons) {
for (const std::pair<GURL, std::vector<SkBitmap>>& url_icon : icons_map) {
for (const SkBitmap& icon : url_icon.second) {
if (!icon.empty() && icon.width() == icon.height())
square_icons->push_back(BitmapAndSource(url_icon.first, icon));
square_icons->push_back(icon);
}
}
}
......@@ -41,12 +41,12 @@ void FilterSquareIconsFromMap(const IconsMap& icons_map,
// Get all non-empty square icons from |icons_map|.
void FilterSquareIconsFromBitmaps(
const std::map<SquareSizePx, SkBitmap> bitmaps,
std::vector<BitmapAndSource>* square_icons) {
std::vector<SkBitmap>* square_icons) {
for (const std::pair<SquareSizePx, SkBitmap>& icon : bitmaps) {
DCHECK_EQ(icon.first, icon.second.width());
DCHECK_EQ(icon.first, icon.second.height());
if (!icon.second.empty())
square_icons->push_back(BitmapAndSource(GURL(), icon.second));
square_icons->push_back(icon.second);
}
}
......@@ -142,13 +142,13 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
// icons for any sizes which have failed to download. This ensures that the
// created manifest for the web app does not contain links to icons
// which are not actually created and linked on disk.
std::vector<BitmapAndSource> square_icons;
std::vector<SkBitmap> square_icons;
if (icons_map)
FilterSquareIconsFromMap(*icons_map, &square_icons);
if (!is_for_sync)
FilterSquareIconsFromBitmaps(web_app_info->icon_bitmaps, &square_icons);
std::set<int> sizes_to_generate = SizesToGenerate();
std::set<SquareSizePx> sizes_to_generate = SizesToGenerate();
if (is_for_sync) {
// Ensure that all icon widths in the web app info icon array are present in
// the sizes to generate set. This ensures that we will have all of the
......@@ -161,14 +161,14 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
web_app_info->generated_icon_color = SK_ColorTRANSPARENT;
// TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's
// not necessary and would simplify this code path to remove.
std::map<int, BitmapAndSource> size_to_icon = ResizeIconsAndGenerateMissing(
std::map<SquareSizePx, SkBitmap> size_to_icon = ResizeIconsAndGenerateMissing(
square_icons, sizes_to_generate, web_app_info->app_url,
&web_app_info->generated_icon_color);
for (std::pair<const int, BitmapAndSource>& item : size_to_icon) {
for (std::pair<const SquareSizePx, SkBitmap>& item : size_to_icon) {
// Retain any bitmaps provided as input to the installation.
if (web_app_info->icon_bitmaps.count(item.first) == 0) {
web_app_info->icon_bitmaps[item.first] = std::move(item.second.bitmap);
web_app_info->icon_bitmaps[item.first] = std::move(item.second);
}
}
}
......
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