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