Index: third_party/WebKit/Source/modules/notifications/NotificationResourcesLoaderTest.cpp |
diff --git a/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoaderTest.cpp b/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoaderTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0fe145f4fdcbf1fd8097fa0b727eb6d61c71b60f |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/notifications/NotificationResourcesLoaderTest.cpp |
@@ -0,0 +1,259 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/notifications/NotificationResourcesLoader.h" |
+ |
+#include "core/testing/DummyPageHolder.h" |
+#include "platform/testing/URLTestHelpers.h" |
+#include "platform/weborigin/KURL.h" |
+#include "public/platform/Platform.h" |
+#include "public/platform/WebURL.h" |
+#include "public/platform/WebURLLoaderMockFactory.h" |
+#include "public/platform/WebURLResponse.h" |
+#include "public/platform/modules/notifications/WebNotificationData.h" |
+#include "public/platform/modules/notifications/WebNotificationResources.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "wtf/Functional.h" |
+#include "wtf/text/WTFString.h" |
+#include <memory> |
+ |
+namespace blink { |
+namespace { |
+ |
+const char kBaseUrl[] = "http://test.com/"; |
+const char kIcon48x48[] = "48x48.png"; |
+const char kIcon100x100[] = "100x100.png"; |
+const char kIcon110x110[] = "110x110.png"; |
+const char kIcon120x120[] = "120x120.png"; |
+const char kIcon500x500[] = "500x500.png"; |
+ |
+static const int kMaxIconSizePx = 320; |
+static const int kMaxBadgeSizePx = 96; |
+static const int kMaxActionIconSizePx = 128; |
+ |
+class NotificationResourcesLoaderTest : public ::testing::Test { |
+public: |
+ NotificationResourcesLoaderTest() |
+ : m_page(DummyPageHolder::create()) |
+ { |
+ } |
+ |
+ ~NotificationResourcesLoaderTest() override |
+ { |
+ } |
+ |
+ void TearDown() override |
+ { |
+ Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
+ } |
+ |
+protected: |
+ ExecutionContext* executionContext() { return &m_page->document(); } |
+ |
+ WebNotificationResources* resources() { return m_resources.get(); } |
+ |
+ void didFetchResources(NotificationResourcesLoader* loader) |
+ { |
+ m_resources = loader->getResources(); |
+ } |
+ |
+ NotificationResourcesLoader* createLoader() |
+ { |
+ return new NotificationResourcesLoader(executionContext(), |
+ bind<NotificationResourcesLoader*>( |
+ &NotificationResourcesLoaderTest::didFetchResources, this)); |
+ } |
+ |
+ // Registers a mocked url. When fetched, |fileName| will be loaded from the |
+ // test data directory. |
+ WebURL registerMockedURL(const String& fileName) |
+ { |
+ WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); |
+ |
+ WebURLResponse response(url); |
+ response.setMIMEType("image/png"); |
+ response.setHTTPStatusCode(200); |
+ |
+ URLTestHelpers::registerMockedURLLoadWithCustomResponse( |
+ url, fileName, "notifications/", response); |
+ |
+ return url; |
+ } |
+ |
+ // Registers a mocked url that will fail to be fetched, with a 404 error. |
+ WebURL registerMockedErrorURL(const String& fileName) |
+ { |
+ WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); |
+ URLTestHelpers::registerMockedErrorURLLoad(url); |
+ return url; |
+ } |
+ |
+private: |
+ OwnPtr<DummyPageHolder> m_page; |
+ std::unique_ptr<WebNotificationResources> m_resources; |
+}; |
+ |
+TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ notificationData.icon = registerMockedURL(kIcon100x100); |
+ notificationData.badge = registerMockedURL(kIcon48x48); |
+ notificationData.actions = |
+ WebVector<WebNotificationAction>(static_cast<size_t>(2)); |
+ notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
+ notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ ASSERT_TRUE(resources()); |
+ |
+ ASSERT_FALSE(resources()->icon.drawsNothing()); |
+ ASSERT_EQ(100, resources()->icon.width()); |
+ |
+ ASSERT_FALSE(resources()->badge.drawsNothing()); |
+ ASSERT_EQ(48, resources()->badge.width()); |
+ |
+ ASSERT_EQ(2u, resources()->actionIcons.size()); |
+ ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
+ ASSERT_EQ(110, resources()->actionIcons[0].width()); |
+ ASSERT_FALSE(resources()->actionIcons[1].drawsNothing()); |
+ ASSERT_EQ(120, resources()->actionIcons[1].width()); |
+} |
+ |
+TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ notificationData.icon = registerMockedURL(kIcon500x500); |
+ notificationData.badge = notificationData.icon; |
+ notificationData.actions = |
+ WebVector<WebNotificationAction>(static_cast<size_t>(1)); |
+ notificationData.actions[0].icon = notificationData.icon; |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ ASSERT_TRUE(resources()); |
+ |
+ ASSERT_FALSE(resources()->icon.drawsNothing()); |
+ ASSERT_EQ(kMaxIconSizePx, resources()->icon.width()); |
+ ASSERT_EQ(kMaxIconSizePx, resources()->icon.height()); |
+ |
+ ASSERT_FALSE(resources()->badge.drawsNothing()); |
+ ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width()); |
+ ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height()); |
+ |
+ ASSERT_EQ(1u, resources()->actionIcons.size()); |
+ ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
+ ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width()); |
+ ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height()); |
+} |
+ |
+ |
+TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ ASSERT_TRUE(resources()); |
+ |
+ ASSERT_TRUE(resources()->icon.drawsNothing()); |
+ ASSERT_TRUE(resources()->badge.drawsNothing()); |
+ ASSERT_EQ(0u, resources()->actionIcons.size()); |
+} |
+ |
+TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ notificationData.icon = registerMockedErrorURL(kIcon100x100); |
+ notificationData.badge = notificationData.icon; |
+ notificationData.actions = |
+ WebVector<WebNotificationAction>(static_cast<size_t>(1)); |
+ notificationData.actions[0].icon = notificationData.icon; |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ 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()->icon.drawsNothing()); |
+ ASSERT_TRUE(resources()->badge.drawsNothing()); |
+ ASSERT_EQ(1u, resources()->actionIcons.size()); |
+ ASSERT_TRUE(resources()->actionIcons[0].drawsNothing()); |
+} |
+ |
+TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ notificationData.icon = registerMockedURL(kIcon100x100); |
+ notificationData.badge = registerMockedErrorURL(kIcon48x48); |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ 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_FALSE(resources()->icon.drawsNothing()); |
+ ASSERT_EQ(100, resources()->icon.width()); |
+ ASSERT_TRUE(resources()->badge.drawsNothing()); |
+ ASSERT_EQ(0u, resources()->actionIcons.size()); |
+} |
+ |
+TEST_F(NotificationResourcesLoaderTest, ExecutionContextDestroyed) |
+{ |
+ NotificationResourcesLoader* loader = createLoader(); |
+ |
+ WebNotificationData notificationData; |
+ notificationData.icon = registerMockedURL(kIcon100x100); |
+ notificationData.badge = registerMockedURL(kIcon48x48); |
+ notificationData.actions = |
+ WebVector<WebNotificationAction>(static_cast<size_t>(2)); |
+ notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
+ notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
+ |
+ ASSERT_FALSE(resources()); |
+ |
+ loader->start(notificationData); |
+ |
+ // Check that starting the loader did not synchronously fail, providing |
+ // empty resources. The requests should be pending now. |
+ ASSERT_FALSE(resources()); |
+ |
+ executionContext()->notifyContextDestroyed(); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ |
+ // Loading should have been cancelled when |notifyContextDestroyed| was |
+ // called so no resources should have been received by the test even though |
+ // |serveAsynchronousRequests| was called. |
+ ASSERT_FALSE(resources()); |
+} |
+ |
+} // namespace |
+} // namespace blink |
Peter Beverloo
2016/04/13 18:32:28
Did you solve the redirection crash you found?
Michael van Ouwerkerk
2016/04/14 13:42:12
Yes. It wasn't actually about the redirect, but th
|