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