OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/notifications/NotificationResourcesLoader.h" |
| 6 |
| 7 #include "core/fetch/MemoryCache.h" |
| 8 #include "core/testing/DummyPageHolder.h" |
| 9 #include "platform/heap/Heap.h" |
| 10 #include "platform/testing/URLTestHelpers.h" |
| 11 #include "platform/weborigin/KURL.h" |
| 12 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebURL.h" |
| 14 #include "public/platform/WebURLLoaderMockFactory.h" |
| 15 #include "public/platform/WebURLResponse.h" |
| 16 #include "public/platform/modules/notifications/WebNotificationData.h" |
| 17 #include "public/platform/modules/notifications/WebNotificationResources.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "wtf/Functional.h" |
| 20 #include "wtf/text/WTFString.h" |
| 21 #include <memory> |
| 22 |
| 23 namespace blink { |
| 24 namespace { |
| 25 |
| 26 const char kBaseUrl[] = "http://test.com/"; |
| 27 const char kIcon48x48[] = "48x48.png"; |
| 28 const char kIcon100x100[] = "100x100.png"; |
| 29 const char kIcon110x110[] = "110x110.png"; |
| 30 const char kIcon120x120[] = "120x120.png"; |
| 31 const char kIcon500x500[] = "500x500.png"; |
| 32 |
| 33 const int kMaxIconSizePx = 320; |
| 34 const int kMaxBadgeSizePx = 96; |
| 35 const int kMaxActionIconSizePx = 128; |
| 36 |
| 37 class NotificationResourcesLoaderTest : public ::testing::Test { |
| 38 public: |
| 39 NotificationResourcesLoaderTest() |
| 40 : m_page(DummyPageHolder::create()), m_loader(new NotificationResourcesL
oader(bind<NotificationResourcesLoader*>(&NotificationResourcesLoaderTest::didFe
tchResources, this))) |
| 41 { |
| 42 } |
| 43 |
| 44 ~NotificationResourcesLoaderTest() override |
| 45 { |
| 46 m_loader->stop(); |
| 47 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
| 48 memoryCache()->evictResources(); |
| 49 } |
| 50 |
| 51 protected: |
| 52 ExecutionContext* executionContext() const { return &m_page->document(); } |
| 53 |
| 54 NotificationResourcesLoader* loader() const { return m_loader.get(); } |
| 55 |
| 56 WebNotificationResources* resources() const { return m_resources.get(); } |
| 57 |
| 58 void didFetchResources(NotificationResourcesLoader* loader) |
| 59 { |
| 60 m_resources = loader->getResources(); |
| 61 } |
| 62 |
| 63 // Registers a mocked url. When fetched, |fileName| will be loaded from the |
| 64 // test data directory. |
| 65 WebURL registerMockedURL(const String& fileName) |
| 66 { |
| 67 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); |
| 68 |
| 69 WebURLResponse response(url); |
| 70 response.setMIMEType("image/png"); |
| 71 response.setHTTPStatusCode(200); |
| 72 |
| 73 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, fileName, "
notifications/", response); |
| 74 |
| 75 return url; |
| 76 } |
| 77 |
| 78 // Registers a mocked url that will fail to be fetched, with a 404 error. |
| 79 WebURL registerMockedErrorURL(const String& fileName) |
| 80 { |
| 81 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); |
| 82 URLTestHelpers::registerMockedErrorURLLoad(url); |
| 83 return url; |
| 84 } |
| 85 |
| 86 private: |
| 87 OwnPtr<DummyPageHolder> m_page; |
| 88 Persistent<NotificationResourcesLoader> m_loader; |
| 89 std::unique_ptr<WebNotificationResources> m_resources; |
| 90 }; |
| 91 |
| 92 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) |
| 93 { |
| 94 WebNotificationData notificationData; |
| 95 notificationData.icon = registerMockedURL(kIcon100x100); |
| 96 notificationData.badge = registerMockedURL(kIcon48x48); |
| 97 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); |
| 98 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
| 99 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
| 100 |
| 101 ASSERT_FALSE(resources()); |
| 102 |
| 103 loader()->start(executionContext(), notificationData); |
| 104 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 105 |
| 106 ASSERT_TRUE(resources()); |
| 107 |
| 108 ASSERT_FALSE(resources()->icon.drawsNothing()); |
| 109 ASSERT_EQ(100, resources()->icon.width()); |
| 110 |
| 111 ASSERT_FALSE(resources()->badge.drawsNothing()); |
| 112 ASSERT_EQ(48, resources()->badge.width()); |
| 113 |
| 114 ASSERT_EQ(2u, resources()->actionIcons.size()); |
| 115 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
| 116 ASSERT_EQ(110, resources()->actionIcons[0].width()); |
| 117 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing()); |
| 118 ASSERT_EQ(120, resources()->actionIcons[1].width()); |
| 119 } |
| 120 |
| 121 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) |
| 122 { |
| 123 WebNotificationData notificationData; |
| 124 notificationData.icon = registerMockedURL(kIcon500x500); |
| 125 notificationData.badge = notificationData.icon; |
| 126 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); |
| 127 notificationData.actions[0].icon = notificationData.icon; |
| 128 |
| 129 ASSERT_FALSE(resources()); |
| 130 |
| 131 loader()->start(executionContext(), notificationData); |
| 132 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 133 |
| 134 ASSERT_TRUE(resources()); |
| 135 |
| 136 ASSERT_FALSE(resources()->icon.drawsNothing()); |
| 137 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width()); |
| 138 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height()); |
| 139 |
| 140 ASSERT_FALSE(resources()->badge.drawsNothing()); |
| 141 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width()); |
| 142 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height()); |
| 143 |
| 144 ASSERT_EQ(1u, resources()->actionIcons.size()); |
| 145 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
| 146 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width()); |
| 147 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height()); |
| 148 } |
| 149 |
| 150 |
| 151 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) |
| 152 { |
| 153 WebNotificationData notificationData; |
| 154 |
| 155 ASSERT_FALSE(resources()); |
| 156 |
| 157 loader()->start(executionContext(), notificationData); |
| 158 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 159 |
| 160 ASSERT_TRUE(resources()); |
| 161 |
| 162 ASSERT_TRUE(resources()->icon.drawsNothing()); |
| 163 ASSERT_TRUE(resources()->badge.drawsNothing()); |
| 164 ASSERT_EQ(0u, resources()->actionIcons.size()); |
| 165 } |
| 166 |
| 167 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) |
| 168 { |
| 169 WebNotificationData notificationData; |
| 170 notificationData.icon = registerMockedErrorURL(kIcon100x100); |
| 171 notificationData.badge = notificationData.icon; |
| 172 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); |
| 173 notificationData.actions[0].icon = notificationData.icon; |
| 174 |
| 175 ASSERT_FALSE(resources()); |
| 176 |
| 177 loader()->start(executionContext(), notificationData); |
| 178 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 179 |
| 180 ASSERT_TRUE(resources()); |
| 181 |
| 182 // The test received resources but they are all empty. This ensures that a |
| 183 // notification can still be shown even if the images fail to load. |
| 184 ASSERT_TRUE(resources()->icon.drawsNothing()); |
| 185 ASSERT_TRUE(resources()->badge.drawsNothing()); |
| 186 ASSERT_EQ(1u, resources()->actionIcons.size()); |
| 187 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing()); |
| 188 } |
| 189 |
| 190 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) |
| 191 { |
| 192 WebNotificationData notificationData; |
| 193 notificationData.icon = registerMockedURL(kIcon100x100); |
| 194 notificationData.badge = registerMockedErrorURL(kIcon48x48); |
| 195 |
| 196 ASSERT_FALSE(resources()); |
| 197 |
| 198 loader()->start(executionContext(), notificationData); |
| 199 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 200 |
| 201 ASSERT_TRUE(resources()); |
| 202 |
| 203 // The test received resources even though one image failed to load. This |
| 204 // ensures that a notification can still be shown, though slightly degraded. |
| 205 ASSERT_FALSE(resources()->icon.drawsNothing()); |
| 206 ASSERT_EQ(100, resources()->icon.width()); |
| 207 ASSERT_TRUE(resources()->badge.drawsNothing()); |
| 208 ASSERT_EQ(0u, resources()->actionIcons.size()); |
| 209 } |
| 210 |
| 211 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) |
| 212 { |
| 213 WebNotificationData notificationData; |
| 214 notificationData.icon = registerMockedURL(kIcon100x100); |
| 215 notificationData.badge = registerMockedURL(kIcon48x48); |
| 216 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); |
| 217 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
| 218 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
| 219 |
| 220 ASSERT_FALSE(resources()); |
| 221 |
| 222 loader()->start(executionContext(), notificationData); |
| 223 |
| 224 // Check that starting the loader did not synchronously fail, providing |
| 225 // empty resources. The requests should be pending now. |
| 226 ASSERT_FALSE(resources()); |
| 227 |
| 228 // The loader would stop e.g. when the execution context is destroyed or |
| 229 // when the loader is about to be destroyed, as a pre-finalizer. |
| 230 loader()->stop(); |
| 231 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 232 |
| 233 // Loading should have been cancelled when |stop| was called so no resources |
| 234 // should have been received by the test even though |
| 235 // |serveAsynchronousRequests| was called. |
| 236 ASSERT_FALSE(resources()); |
| 237 } |
| 238 |
| 239 } // namespace |
| 240 } // namespace blink |
OLD | NEW |