Skip to content

Commit

Permalink
Add unit test for WebappIcon.GetIdealSizeInPx
Browse files Browse the repository at this point in the history
Bug: 1444216
Change-Id: If491edba150d040553811e9d6841ca455fdc905a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4573881
Commit-Queue: Ella Ge <eirage@chromium.org>
Commit-Queue: Glenn Hartmann <hartmanng@chromium.org>
Auto-Submit: Ella Ge <eirage@chromium.org>
Reviewed-by: Glenn Hartmann <hartmanng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1150326}
  • Loading branch information
EiraGe authored and Chromium LUCI CQ committed May 29, 2023
1 parent 55d12c6 commit 0f38570
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/webapps/browser/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ source_set("unit_tests") {
"shortcut_info_unittest.cc",
"webapk/webapk_icon_hasher_unittest.cc",
"webapk/webapk_proto_builder_unittest.cc",
"webapp_icon_unittest.cc",
"webapps_utils_unittest.cc",
]
deps = [
Expand Down
86 changes: 86 additions & 0 deletions components/webapps/browser/android/webapp_icon_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.

#include "components/webapps/browser/android/webapp_icon.h"

#include "components/webapps/browser/android/webapps_icon_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace webapps {

namespace {

const int kIdealHomescreenIconSize = 96;
const int kMinimumHomescreenIconSize = 48;
const int kIdealSplashImageSize = 256;
const int kMinimumSplashImageSize = 32;
const int kIdealMonochromeIconSize = 48;
const int kIdealAdaptiveLauncherIconSize = 166;
const int kIdealShortcutIconSize = 72;

} // namespace

class WebappIconTest : public testing::Test {
public:
WebappIconTest() = default;

WebappIconTest(const WebappIconTest&) = delete;
WebappIconTest& operator=(const WebappIconTest&) = delete;

protected:
void SetUp() override {
WebappsIconUtils::SetIconSizesForTesting({
kIdealHomescreenIconSize,
kMinimumHomescreenIconSize,
kIdealSplashImageSize,
kMinimumSplashImageSize,
kIdealMonochromeIconSize,
kIdealAdaptiveLauncherIconSize,
kIdealShortcutIconSize,
});
}

const GURL kIconUrl = GURL("http://example.com");
};

TEST_F(WebappIconTest, GetIdealIconSizes) {
WebappIcon primary_icon(kIconUrl, false /* is_maskable*/,
webapk::Image::PRIMARY_ICON);
EXPECT_EQ(primary_icon.GetIdealSizeInPx(), kIdealHomescreenIconSize);

WebappIcon maskable_primary_icon(kIconUrl, true /* is_maskable*/,
webapk::Image::PRIMARY_ICON);
EXPECT_EQ(maskable_primary_icon.GetIdealSizeInPx(),
kIdealAdaptiveLauncherIconSize);

WebappIcon splash_icon(kIconUrl, false /* is_maskable*/,
webapk::Image::SPLASH_ICON);
EXPECT_EQ(splash_icon.GetIdealSizeInPx(), kIdealSplashImageSize);

WebappIcon shortcut_icon(kIconUrl, false /* is_maskable*/,
webapk::Image::SHORTCUT_ICON);
EXPECT_EQ(shortcut_icon.GetIdealSizeInPx(), kIdealShortcutIconSize);
}

// Test that icon with multiple usages have ideal size of the largest usage.
TEST_F(WebappIconTest, GetIdealSizeForMultiplePurpose) {
WebappIcon icon(kIconUrl);
icon.AddUsage(webapk::Image::PRIMARY_ICON);
icon.AddUsage(webapk::Image::SPLASH_ICON);
EXPECT_EQ(icon.GetIdealSizeInPx(), kIdealSplashImageSize);

WebappIcon icon2(kIconUrl);
icon2.AddUsage(webapk::Image::PRIMARY_ICON);
icon2.AddUsage(webapk::Image::SHORTCUT_ICON);
EXPECT_EQ(icon2.GetIdealSizeInPx(), kIdealHomescreenIconSize);

WebappIcon icon3(kIconUrl);
icon3.AddUsage(webapk::Image::PRIMARY_ICON);
icon3.AddUsage(webapk::Image::SPLASH_ICON);
icon3.AddUsage(webapk::Image::SHORTCUT_ICON);
EXPECT_EQ(icon3.GetIdealSizeInPx(), kIdealSplashImageSize);
}

} // namespace webapps
11 changes: 11 additions & 0 deletions components/webapps/browser/android/webapps_icon_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,15 @@ void WebappsIconUtils::SetIdealShortcutSizeForTesting(int size) {
g_ideal_shortcut_icon_size = size;
}

void WebappsIconUtils::SetIconSizesForTesting(std::vector<int> sizes) {
// This ordering must be kept up to date with the |GetIconSizes()| above.
g_ideal_homescreen_icon_size = sizes[0];
g_minimum_homescreen_icon_size = sizes[1];
g_ideal_splash_image_size = sizes[2];
g_minimum_splash_image_size = sizes[3];
g_ideal_monochrome_icon_size = sizes[4];
g_ideal_adaptive_launcher_icon_size = sizes[5];
g_ideal_shortcut_icon_size = sizes[6];
}

} // namespace webapps
1 change: 1 addition & 0 deletions components/webapps/browser/android/webapps_icon_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class WebappsIconUtils {
static int GetIdealIconCornerRadiusPxForPromptUI();

static void SetIdealShortcutSizeForTesting(int size);
static void SetIconSizesForTesting(std::vector<int> sizes);
};

} // namespace webapps
Expand Down

0 comments on commit 0f38570

Please sign in to comment.