Commit 6e6f909c authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Allow relative src paths with ImageResouce

Change-Id: I81a0ce6ebb664ec993028f561729051884924a26
Reviewed-on: https://chromium-review.googlesource.com/1152948
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarMugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579780}
parent 54d4cbfc
......@@ -93,9 +93,14 @@ KURL BackgroundFetchIconLoader::PickBestIconForDisplay(
ExecutionContext* execution_context,
const WebSize& icon_display_size_pixels) {
std::vector<Manifest::ImageResource> icons;
for (const auto& icon : icons_)
for (auto& icon : icons_) {
// Update the src of |icon| to include the base URL in case relative paths
// were used.
icon.setSrc(execution_context->CompleteURL(icon.src()));
icons.emplace_back(blink::ConvertManifestImageResource(icon));
}
// TODO(crbug.com/868875): Handle cases where `sizes` or `purpose` is empty.
return KURL(ManifestIconSelector::FindBestMatchingIcon(
std::move(icons), icon_display_size_pixels.height, kMinimumIconSizeInPx,
Manifest::ImageResource::Purpose::ANY));
......
......@@ -28,10 +28,11 @@ enum class BackgroundFetchLoadState {
constexpr char kBackgroundFetchImageLoaderBaseUrl[] = "http://test.com/";
constexpr char kBackgroundFetchImageLoaderBaseDir[] = "notifications/";
constexpr char kBackgroundFetchImageLoaderIcon500x500FullPath[] =
"http://test.com/500x500.png";
constexpr char kBackgroundFetchImageLoaderIcon500x500[] = "500x500.png";
constexpr char kBackgroundFetchImageLoaderIcon48x48[] = "48x48.png";
constexpr char kBackgroundFetchImageLoaderIcon3000x2000[] = "3000x2000.png";
constexpr char kBackgroundFetchImageLoaderIcon[] = "3000x2000.png";
} // namespace
......@@ -44,7 +45,13 @@ class BackgroundFetchIconLoaderTest : public PageTestBase {
->UnregisterAllURLsAndClearMemoryCache();
}
void SetUp() override { PageTestBase::SetUp(IntSize()); }
void SetUp() override {
PageTestBase::SetUp(IntSize());
GetDocument().SetBaseURLOverride(KURL(kBackgroundFetchImageLoaderBaseUrl));
RegisterMockedURL(kBackgroundFetchImageLoaderIcon500x500);
RegisterMockedURL(kBackgroundFetchImageLoaderIcon48x48);
RegisterMockedURL(kBackgroundFetchImageLoaderIcon3000x2000);
}
// Registers a mocked URL.
WebURL RegisterMockedURL(const String& file_name) {
......@@ -66,11 +73,11 @@ class BackgroundFetchIconLoaderTest : public PageTestBase {
ManifestImageResource CreateTestIcon(const String& url_str,
const String& size) {
KURL url = RegisterMockedURL(url_str);
ManifestImageResource icon;
icon.setSrc(url.GetString());
icon.setSrc(url_str);
icon.setType("image/png");
icon.setSizes(size);
icon.setPurpose("any");
return icon;
}
......@@ -105,61 +112,49 @@ class BackgroundFetchIconLoaderTest : public PageTestBase {
};
TEST_F(BackgroundFetchIconLoaderTest, SuccessTest) {
KURL url = RegisterMockedURL(kBackgroundFetchImageLoaderIcon500x500);
LoadIcon(url);
LoadIcon(KURL(kBackgroundFetchImageLoaderIcon500x500FullPath));
platform_->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
EXPECT_EQ(BackgroundFetchLoadState::kLoadSuccessful, loaded_);
}
TEST_F(BackgroundFetchIconLoaderTest, PickRightIconTest) {
ManifestImageResource icon0 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon500x500, "500x500");
ManifestImageResource icon1 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon48x48, "48x48");
ManifestImageResource icon2 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon3000x2000, "3000x2000");
TEST_F(BackgroundFetchIconLoaderTest, PickIconRelativePath) {
HeapVector<ManifestImageResource> icons;
icons.push_back(icon0);
icons.push_back(icon1);
icons.push_back(icon2);
icons.push_back(
CreateTestIcon(kBackgroundFetchImageLoaderIcon500x500, "500x500"));
KURL best_icon = PickRightIcon(std::move(icons), WebSize(50, 50));
EXPECT_EQ(best_icon, KURL(kBackgroundFetchImageLoaderIcon48x48));
KURL best_icon = PickRightIcon(std::move(icons), WebSize(500, 500));
ASSERT_TRUE(best_icon.IsValid());
EXPECT_EQ(best_icon, KURL(kBackgroundFetchImageLoaderIcon500x500FullPath));
}
TEST_F(BackgroundFetchIconLoaderTest, PickRightIconGivenAnyTest) {
ManifestImageResource icon0 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon500x500, "500x500");
ManifestImageResource icon1 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon48x48, "48x48");
ManifestImageResource icon2 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon, "any");
TEST_F(BackgroundFetchIconLoaderTest, PickIconFullPath) {
HeapVector<ManifestImageResource> icons;
icons.push_back(icon0);
icons.push_back(icon1);
icons.push_back(icon2);
icons.push_back(CreateTestIcon(kBackgroundFetchImageLoaderIcon500x500FullPath,
"500x500"));
KURL best_icon = PickRightIcon(std::move(icons), WebSize(50, 50));
EXPECT_EQ(best_icon, KURL(kBackgroundFetchImageLoaderIcon));
KURL best_icon = PickRightIcon(std::move(icons), WebSize(500, 500));
ASSERT_TRUE(best_icon.IsValid());
EXPECT_EQ(best_icon, KURL(kBackgroundFetchImageLoaderIcon500x500FullPath));
}
TEST_F(BackgroundFetchIconLoaderTest, PickRightIconWithTieBreakTest) {
// Test that if two icons get the same score, the one declared last gets
// picked.
TEST_F(BackgroundFetchIconLoaderTest, PickRightIcon) {
ManifestImageResource icon0 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon500x500, "500x500");
ManifestImageResource icon1 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon48x48, "48x48");
ManifestImageResource icon2 =
CreateTestIcon(kBackgroundFetchImageLoaderIcon3000x2000, "48x48");
CreateTestIcon(kBackgroundFetchImageLoaderIcon3000x2000, "3000x2000");
HeapVector<ManifestImageResource> icons;
icons.push_back(icon0);
icons.push_back(icon1);
icons.push_back(icon2);
KURL best_icon = PickRightIcon(std::move(icons), WebSize(50, 50));
EXPECT_EQ(best_icon, KURL(kBackgroundFetchImageLoaderIcon3000x2000));
KURL best_icon = PickRightIcon(std::move(icons), WebSize(42, 42));
ASSERT_TRUE(best_icon.IsValid());
// We expect the smallest Icon larger than the ideal display size.
EXPECT_EQ(best_icon, KURL(KURL(kBackgroundFetchImageLoaderBaseUrl),
kBackgroundFetchImageLoaderIcon48x48));
}
} // namespace blink
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