1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/notifications/notification_resources_loader.h"
#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/notifications/notification_constants.h"
#include "third_party/blink/public/mojom/notifications/notification.mojom-blink.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_loader_mock_factory.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
namespace {
constexpr char kResourcesLoaderBaseUrl[] = "http://test.com/";
constexpr char kResourcesLoaderBaseDir[] = "notifications/";
constexpr char kResourcesLoaderIcon48x48[] = "48x48.png";
constexpr char kResourcesLoaderIcon100x100[] = "100x100.png";
constexpr char kResourcesLoaderIcon110x110[] = "110x110.png";
constexpr char kResourcesLoaderIcon120x120[] = "120x120.png";
constexpr char kResourcesLoaderIcon500x500[] = "500x500.png";
constexpr char kResourcesLoaderIcon3000x1000[] = "3000x1000.png";
constexpr char kResourcesLoaderIcon3000x2000[] = "3000x2000.png";
class NotificationResourcesLoaderTest : public PageTestBase {
public:
NotificationResourcesLoaderTest()
: loader_(MakeGarbageCollected<NotificationResourcesLoader>(
WTF::BindOnce(&NotificationResourcesLoaderTest::DidFetchResources,
WTF::Unretained(this)))) {}
~NotificationResourcesLoaderTest() override {
loader_->Stop();
URLLoaderMockFactory::GetSingletonInstance()
->UnregisterAllURLsAndClearMemoryCache();
}
void SetUp() override { PageTestBase::SetUp(gfx::Size()); }
protected:
ExecutionContext* GetExecutionContext() const {
return GetFrame().DomWindow();
}
NotificationResourcesLoader* Loader() const { return loader_.Get(); }
const mojom::blink::NotificationResources* Resources() const {
return resources_.get();
}
void DidFetchResources(NotificationResourcesLoader* loader) {
resources_ = loader->GetResources();
std::move(resources_loaded_closure_).Run();
}
void StartAndWaitForResources(
const mojom::blink::NotificationData& notification_data) {
base::RunLoop run_loop;
resources_loaded_closure_ = run_loop.QuitClosure();
Loader()->Start(GetExecutionContext(), notification_data);
URLLoaderMockFactory::GetSingletonInstance()->ServeAsynchronousRequests();
run_loop.Run();
}
// Registers a mocked url. When fetched, |fileName| will be loaded from the
// test data directory.
KURL RegisterMockedURL(const String& file_name) {
KURL registered_url = url_test_helpers::RegisterMockedURLLoadFromBase(
kResourcesLoaderBaseUrl,
test::CoreTestDataPath(kResourcesLoaderBaseDir), file_name,
"image/png");
return registered_url;
}
// Registers a mocked url that will fail to be fetched, with a 404 error.
KURL RegisterMockedErrorURL(const String& file_name) {
KURL url(kResourcesLoaderBaseUrl + file_name);
url_test_helpers::RegisterMockedErrorURLLoad(url);
return url;
}
ScopedTestingPlatformSupport<TestingPlatformSupport> platform_;
private:
base::OnceClosure resources_loaded_closure_;
Persistent<NotificationResourcesLoader> loader_;
mojom::blink::NotificationResourcesPtr resources_;
};
TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->image = RegisterMockedURL(kResourcesLoaderIcon500x500);
notification_data->icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
notification_data->badge = RegisterMockedURL(kResourcesLoaderIcon48x48);
notification_data->actions = Vector<mojom::blink::NotificationActionPtr>();
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[0]->icon =
RegisterMockedURL(kResourcesLoaderIcon110x110);
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[1]->icon =
RegisterMockedURL(kResourcesLoaderIcon120x120);
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
ASSERT_FALSE(Resources()->image.drawsNothing());
ASSERT_EQ(500, Resources()->image.width());
ASSERT_EQ(500, Resources()->image.height());
ASSERT_FALSE(Resources()->icon.drawsNothing());
ASSERT_EQ(100, Resources()->icon.width());
ASSERT_FALSE(Resources()->badge.drawsNothing());
ASSERT_EQ(48, Resources()->badge.width());
ASSERT_TRUE(Resources()->action_icons.has_value());
auto& action_icons = Resources()->action_icons.value();
ASSERT_EQ(2u, action_icons.size());
ASSERT_FALSE(action_icons[0].drawsNothing());
ASSERT_EQ(110, action_icons[0].width());
ASSERT_FALSE(action_icons[1].drawsNothing());
ASSERT_EQ(120, action_icons[1].width());
}
TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->icon = RegisterMockedURL(kResourcesLoaderIcon500x500);
notification_data->badge = notification_data->icon;
notification_data->actions = Vector<mojom::blink::NotificationActionPtr>();
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[0]->icon = notification_data->icon;
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
ASSERT_FALSE(Resources()->icon.drawsNothing());
ASSERT_EQ(kNotificationMaxIconSizePx, Resources()->icon.width());
ASSERT_EQ(kNotificationMaxIconSizePx, Resources()->icon.height());
ASSERT_FALSE(Resources()->badge.drawsNothing());
ASSERT_EQ(kNotificationMaxBadgeSizePx, Resources()->badge.width());
ASSERT_EQ(kNotificationMaxBadgeSizePx, Resources()->badge.height());
ASSERT_TRUE(Resources()->action_icons.has_value());
auto& action_icons = Resources()->action_icons.value();
ASSERT_EQ(1u, action_icons.size());
ASSERT_FALSE(action_icons[0].drawsNothing());
ASSERT_EQ(kNotificationMaxActionIconSizePx, action_icons[0].width());
ASSERT_EQ(kNotificationMaxActionIconSizePx, action_icons[0].height());
}
TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_1AspectRatio) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->image = RegisterMockedURL(kResourcesLoaderIcon3000x1000);
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
ASSERT_FALSE(Resources()->image.drawsNothing());
ASSERT_EQ(kNotificationMaxImageWidthPx, Resources()->image.width());
ASSERT_EQ(kNotificationMaxImageWidthPx / 3, Resources()->image.height());
}
TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_2AspectRatio) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->image = RegisterMockedURL(kResourcesLoaderIcon3000x2000);
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
ASSERT_FALSE(Resources()->image.drawsNothing());
ASSERT_EQ(kNotificationMaxImageHeightPx * 3 / 2, Resources()->image.width());
ASSERT_EQ(kNotificationMaxImageHeightPx, Resources()->image.height());
}
TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) {
auto notification_data = mojom::blink::NotificationData::New();
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
ASSERT_TRUE(Resources()->image.drawsNothing());
ASSERT_TRUE(Resources()->icon.drawsNothing());
ASSERT_TRUE(Resources()->badge.drawsNothing());
ASSERT_EQ(0u, Resources()->action_icons.value().size());
}
TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->icon = RegisterMockedErrorURL(kResourcesLoaderIcon100x100);
notification_data->image = notification_data->icon;
notification_data->badge = notification_data->icon;
notification_data->actions = Vector<mojom::blink::NotificationActionPtr>();
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[0]->icon = notification_data->icon;
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
// The test received resources but they are all empty. This ensures that a
// notification can still be shown even if the images fail to load.
ASSERT_TRUE(Resources()->image.drawsNothing());
ASSERT_TRUE(Resources()->icon.drawsNothing());
ASSERT_TRUE(Resources()->badge.drawsNothing());
ASSERT_EQ(1u, Resources()->action_icons.value().size());
ASSERT_TRUE(Resources()->action_icons.value()[0].drawsNothing());
}
TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
notification_data->badge = RegisterMockedErrorURL(kResourcesLoaderIcon48x48);
ASSERT_FALSE(Resources());
StartAndWaitForResources(*notification_data);
ASSERT_TRUE(Resources());
// The test received resources even though one image failed to load. This
// ensures that a notification can still be shown, though slightly degraded.
ASSERT_TRUE(Resources()->image.drawsNothing());
ASSERT_FALSE(Resources()->icon.drawsNothing());
ASSERT_EQ(100, Resources()->icon.width());
ASSERT_TRUE(Resources()->badge.drawsNothing());
ASSERT_EQ(0u, Resources()->action_icons.value().size());
}
TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) {
auto notification_data = mojom::blink::NotificationData::New();
notification_data->image = RegisterMockedURL(kResourcesLoaderIcon500x500);
notification_data->icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
notification_data->badge = RegisterMockedURL(kResourcesLoaderIcon48x48);
notification_data->actions = Vector<mojom::blink::NotificationActionPtr>();
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[0]->icon =
RegisterMockedURL(kResourcesLoaderIcon110x110);
notification_data->actions->push_back(
mojom::blink::NotificationAction::New());
notification_data->actions.value()[1]->icon =
RegisterMockedURL(kResourcesLoaderIcon120x120);
ASSERT_FALSE(Resources());
Loader()->Start(GetExecutionContext(), *notification_data);
// Check that starting the loader did not synchronously fail, providing
// empty resources. The requests should be pending now.
ASSERT_FALSE(Resources());
// The loader would stop e.g. when the execution context is destroyed or
// when the loader is about to be destroyed, as a pre-finalizer.
Loader()->Stop();
URLLoaderMockFactory::GetSingletonInstance()->ServeAsynchronousRequests();
// Loading should have been cancelled when |stop| was called so no resources
// should have been received by the test even though
// |serveAsynchronousRequests| was called.
ASSERT_FALSE(Resources());
}
} // namespace
} // namespace blink
|