OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/notifications/NotificationResourcesLoader.h" | 5 #include "modules/notifications/NotificationResourcesLoader.h" |
6 | 6 |
7 #include "core/fetch/MemoryCache.h" | 7 #include "core/fetch/MemoryCache.h" |
8 #include "core/testing/DummyPageHolder.h" | 8 #include "core/testing/DummyPageHolder.h" |
9 #include "platform/heap/Heap.h" | 9 #include "platform/heap/Heap.h" |
10 #include "platform/testing/URLTestHelpers.h" | 10 #include "platform/testing/URLTestHelpers.h" |
11 #include "platform/weborigin/KURL.h" | 11 #include "platform/weborigin/KURL.h" |
12 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
13 #include "public/platform/WebURL.h" | 13 #include "public/platform/WebURL.h" |
14 #include "public/platform/WebURLLoaderMockFactory.h" | 14 #include "public/platform/WebURLLoaderMockFactory.h" |
15 #include "public/platform/WebURLResponse.h" | 15 #include "public/platform/WebURLResponse.h" |
| 16 #include "public/platform/modules/notifications/WebNotificationConstants.h" |
16 #include "public/platform/modules/notifications/WebNotificationData.h" | 17 #include "public/platform/modules/notifications/WebNotificationData.h" |
17 #include "public/platform/modules/notifications/WebNotificationResources.h" | 18 #include "public/platform/modules/notifications/WebNotificationResources.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "wtf/Functional.h" | 20 #include "wtf/Functional.h" |
20 #include "wtf/text/WTFString.h" | 21 #include "wtf/text/WTFString.h" |
21 #include <memory> | 22 #include <memory> |
22 | 23 |
23 namespace blink { | 24 namespace blink { |
24 namespace { | 25 namespace { |
25 | 26 |
26 const char kBaseUrl[] = "http://test.com/"; | 27 const char kBaseUrl[] = "http://test.com/"; |
27 const char kIcon48x48[] = "48x48.png"; | 28 const char kIcon48x48[] = "48x48.png"; |
28 const char kIcon100x100[] = "100x100.png"; | 29 const char kIcon100x100[] = "100x100.png"; |
29 const char kIcon110x110[] = "110x110.png"; | 30 const char kIcon110x110[] = "110x110.png"; |
30 const char kIcon120x120[] = "120x120.png"; | 31 const char kIcon120x120[] = "120x120.png"; |
31 const char kIcon500x500[] = "500x500.png"; | 32 const char kIcon500x500[] = "500x500.png"; |
32 | 33 const char kIcon3000x1000[] = "3000x1000.png"; |
33 const int kMaxIconSizePx = 320; | 34 const char kIcon3000x2000[] = "3000x2000.png"; |
34 const int kMaxBadgeSizePx = 96; | |
35 const int kMaxActionIconSizePx = 128; | |
36 | 35 |
37 class NotificationResourcesLoaderTest : public ::testing::Test { | 36 class NotificationResourcesLoaderTest : public ::testing::Test { |
38 public: | 37 public: |
39 NotificationResourcesLoaderTest() | 38 NotificationResourcesLoaderTest() |
40 : m_page(DummyPageHolder::create()), m_loader(new NotificationResourcesL
oader(bind(&NotificationResourcesLoaderTest::didFetchResources, WTF::unretained(
this)))) | 39 : m_page(DummyPageHolder::create()), m_loader(new NotificationResourcesL
oader(bind(&NotificationResourcesLoaderTest::didFetchResources, WTF::unretained(
this)))) |
41 { | 40 { |
42 } | 41 } |
43 | 42 |
44 ~NotificationResourcesLoaderTest() override | 43 ~NotificationResourcesLoaderTest() override |
45 { | 44 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 84 |
86 private: | 85 private: |
87 std::unique_ptr<DummyPageHolder> m_page; | 86 std::unique_ptr<DummyPageHolder> m_page; |
88 Persistent<NotificationResourcesLoader> m_loader; | 87 Persistent<NotificationResourcesLoader> m_loader; |
89 std::unique_ptr<WebNotificationResources> m_resources; | 88 std::unique_ptr<WebNotificationResources> m_resources; |
90 }; | 89 }; |
91 | 90 |
92 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) | 91 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) |
93 { | 92 { |
94 WebNotificationData notificationData; | 93 WebNotificationData notificationData; |
| 94 notificationData.image = registerMockedURL(kIcon500x500); |
95 notificationData.icon = registerMockedURL(kIcon100x100); | 95 notificationData.icon = registerMockedURL(kIcon100x100); |
96 notificationData.badge = registerMockedURL(kIcon48x48); | 96 notificationData.badge = registerMockedURL(kIcon48x48); |
97 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); | 97 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); |
98 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); | 98 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
99 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); | 99 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
100 | 100 |
101 ASSERT_FALSE(resources()); | 101 ASSERT_FALSE(resources()); |
102 | 102 |
103 loader()->start(executionContext(), notificationData); | 103 loader()->start(executionContext(), notificationData); |
104 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 104 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
105 | 105 |
106 ASSERT_TRUE(resources()); | 106 ASSERT_TRUE(resources()); |
107 | 107 |
| 108 ASSERT_FALSE(resources()->image.drawsNothing()); |
| 109 ASSERT_EQ(500, resources()->image.width()); |
| 110 ASSERT_EQ(500, resources()->image.height()); |
| 111 |
108 ASSERT_FALSE(resources()->icon.drawsNothing()); | 112 ASSERT_FALSE(resources()->icon.drawsNothing()); |
109 ASSERT_EQ(100, resources()->icon.width()); | 113 ASSERT_EQ(100, resources()->icon.width()); |
110 | 114 |
111 ASSERT_FALSE(resources()->badge.drawsNothing()); | 115 ASSERT_FALSE(resources()->badge.drawsNothing()); |
112 ASSERT_EQ(48, resources()->badge.width()); | 116 ASSERT_EQ(48, resources()->badge.width()); |
113 | 117 |
114 ASSERT_EQ(2u, resources()->actionIcons.size()); | 118 ASSERT_EQ(2u, resources()->actionIcons.size()); |
115 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); | 119 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
116 ASSERT_EQ(110, resources()->actionIcons[0].width()); | 120 ASSERT_EQ(110, resources()->actionIcons[0].width()); |
117 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing()); | 121 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing()); |
118 ASSERT_EQ(120, resources()->actionIcons[1].width()); | 122 ASSERT_EQ(120, resources()->actionIcons[1].width()); |
119 } | 123 } |
120 | 124 |
121 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) | 125 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) |
122 { | 126 { |
123 WebNotificationData notificationData; | 127 WebNotificationData notificationData; |
124 notificationData.icon = registerMockedURL(kIcon500x500); | 128 notificationData.icon = registerMockedURL(kIcon500x500); |
125 notificationData.badge = notificationData.icon; | 129 notificationData.badge = notificationData.icon; |
126 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); | 130 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); |
127 notificationData.actions[0].icon = notificationData.icon; | 131 notificationData.actions[0].icon = notificationData.icon; |
128 | 132 |
129 ASSERT_FALSE(resources()); | 133 ASSERT_FALSE(resources()); |
130 | 134 |
131 loader()->start(executionContext(), notificationData); | 135 loader()->start(executionContext(), notificationData); |
132 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 136 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
133 | 137 |
134 ASSERT_TRUE(resources()); | 138 ASSERT_TRUE(resources()); |
135 | 139 |
136 ASSERT_FALSE(resources()->icon.drawsNothing()); | 140 ASSERT_FALSE(resources()->icon.drawsNothing()); |
137 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width()); | 141 ASSERT_EQ(kWebNotificationMaxIconSizePx, resources()->icon.width()); |
138 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height()); | 142 ASSERT_EQ(kWebNotificationMaxIconSizePx, resources()->icon.height()); |
139 | 143 |
140 ASSERT_FALSE(resources()->badge.drawsNothing()); | 144 ASSERT_FALSE(resources()->badge.drawsNothing()); |
141 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width()); | 145 ASSERT_EQ(kWebNotificationMaxBadgeSizePx, resources()->badge.width()); |
142 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height()); | 146 ASSERT_EQ(kWebNotificationMaxBadgeSizePx, resources()->badge.height()); |
143 | 147 |
144 ASSERT_EQ(1u, resources()->actionIcons.size()); | 148 ASSERT_EQ(1u, resources()->actionIcons.size()); |
145 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); | 149 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); |
146 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width()); | 150 ASSERT_EQ(kWebNotificationMaxActionIconSizePx, resources()->actionIcons[0].w
idth()); |
147 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height()); | 151 ASSERT_EQ(kWebNotificationMaxActionIconSizePx, resources()->actionIcons[0].h
eight()); |
148 } | 152 } |
149 | 153 |
| 154 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_1AspectRatio) |
| 155 { |
| 156 WebNotificationData notificationData; |
| 157 notificationData.image = registerMockedURL(kIcon3000x1000); |
| 158 |
| 159 ASSERT_FALSE(resources()); |
| 160 |
| 161 loader()->start(executionContext(), notificationData); |
| 162 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 163 |
| 164 ASSERT_TRUE(resources()); |
| 165 |
| 166 ASSERT_FALSE(resources()->image.drawsNothing()); |
| 167 ASSERT_EQ(kWebNotificationMaxImageWidthPx, resources()->image.width()); |
| 168 ASSERT_EQ(kWebNotificationMaxImageWidthPx / 3, resources()->image.height()); |
| 169 } |
| 170 |
| 171 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_2AspectRatio) |
| 172 { |
| 173 WebNotificationData notificationData; |
| 174 notificationData.image = registerMockedURL(kIcon3000x2000); |
| 175 |
| 176 ASSERT_FALSE(resources()); |
| 177 |
| 178 loader()->start(executionContext(), notificationData); |
| 179 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 180 |
| 181 ASSERT_TRUE(resources()); |
| 182 |
| 183 ASSERT_FALSE(resources()->image.drawsNothing()); |
| 184 ASSERT_EQ(kWebNotificationMaxImageHeightPx * 3 / 2, resources()->image.width
()); |
| 185 ASSERT_EQ(kWebNotificationMaxImageHeightPx, resources()->image.height()); |
| 186 } |
150 | 187 |
151 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) | 188 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) |
152 { | 189 { |
153 WebNotificationData notificationData; | 190 WebNotificationData notificationData; |
154 | 191 |
155 ASSERT_FALSE(resources()); | 192 ASSERT_FALSE(resources()); |
156 | 193 |
157 loader()->start(executionContext(), notificationData); | 194 loader()->start(executionContext(), notificationData); |
158 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 195 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
159 | 196 |
160 ASSERT_TRUE(resources()); | 197 ASSERT_TRUE(resources()); |
161 | 198 |
| 199 ASSERT_TRUE(resources()->image.drawsNothing()); |
162 ASSERT_TRUE(resources()->icon.drawsNothing()); | 200 ASSERT_TRUE(resources()->icon.drawsNothing()); |
163 ASSERT_TRUE(resources()->badge.drawsNothing()); | 201 ASSERT_TRUE(resources()->badge.drawsNothing()); |
164 ASSERT_EQ(0u, resources()->actionIcons.size()); | 202 ASSERT_EQ(0u, resources()->actionIcons.size()); |
165 } | 203 } |
166 | 204 |
167 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) | 205 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) |
168 { | 206 { |
169 WebNotificationData notificationData; | 207 WebNotificationData notificationData; |
| 208 notificationData.image = notificationData.icon; |
170 notificationData.icon = registerMockedErrorURL(kIcon100x100); | 209 notificationData.icon = registerMockedErrorURL(kIcon100x100); |
171 notificationData.badge = notificationData.icon; | 210 notificationData.badge = notificationData.icon; |
172 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); | 211 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(1)); |
173 notificationData.actions[0].icon = notificationData.icon; | 212 notificationData.actions[0].icon = notificationData.icon; |
174 | 213 |
175 ASSERT_FALSE(resources()); | 214 ASSERT_FALSE(resources()); |
176 | 215 |
177 loader()->start(executionContext(), notificationData); | 216 loader()->start(executionContext(), notificationData); |
178 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 217 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
179 | 218 |
180 ASSERT_TRUE(resources()); | 219 ASSERT_TRUE(resources()); |
181 | 220 |
182 // The test received resources but they are all empty. This ensures that a | 221 // 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. | 222 // notification can still be shown even if the images fail to load. |
| 223 ASSERT_TRUE(resources()->image.drawsNothing()); |
184 ASSERT_TRUE(resources()->icon.drawsNothing()); | 224 ASSERT_TRUE(resources()->icon.drawsNothing()); |
185 ASSERT_TRUE(resources()->badge.drawsNothing()); | 225 ASSERT_TRUE(resources()->badge.drawsNothing()); |
186 ASSERT_EQ(1u, resources()->actionIcons.size()); | 226 ASSERT_EQ(1u, resources()->actionIcons.size()); |
187 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing()); | 227 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing()); |
188 } | 228 } |
189 | 229 |
190 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) | 230 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) |
191 { | 231 { |
192 WebNotificationData notificationData; | 232 WebNotificationData notificationData; |
193 notificationData.icon = registerMockedURL(kIcon100x100); | 233 notificationData.icon = registerMockedURL(kIcon100x100); |
194 notificationData.badge = registerMockedErrorURL(kIcon48x48); | 234 notificationData.badge = registerMockedErrorURL(kIcon48x48); |
195 | 235 |
196 ASSERT_FALSE(resources()); | 236 ASSERT_FALSE(resources()); |
197 | 237 |
198 loader()->start(executionContext(), notificationData); | 238 loader()->start(executionContext(), notificationData); |
199 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 239 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
200 | 240 |
201 ASSERT_TRUE(resources()); | 241 ASSERT_TRUE(resources()); |
202 | 242 |
203 // The test received resources even though one image failed to load. This | 243 // The test received resources even though one image failed to load. This |
204 // ensures that a notification can still be shown, though slightly degraded. | 244 // ensures that a notification can still be shown, though slightly degraded. |
| 245 ASSERT_TRUE(resources()->image.drawsNothing()); |
205 ASSERT_FALSE(resources()->icon.drawsNothing()); | 246 ASSERT_FALSE(resources()->icon.drawsNothing()); |
206 ASSERT_EQ(100, resources()->icon.width()); | 247 ASSERT_EQ(100, resources()->icon.width()); |
207 ASSERT_TRUE(resources()->badge.drawsNothing()); | 248 ASSERT_TRUE(resources()->badge.drawsNothing()); |
208 ASSERT_EQ(0u, resources()->actionIcons.size()); | 249 ASSERT_EQ(0u, resources()->actionIcons.size()); |
209 } | 250 } |
210 | 251 |
211 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) | 252 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) |
212 { | 253 { |
213 WebNotificationData notificationData; | 254 WebNotificationData notificationData; |
| 255 notificationData.image = registerMockedURL(kIcon500x500); |
214 notificationData.icon = registerMockedURL(kIcon100x100); | 256 notificationData.icon = registerMockedURL(kIcon100x100); |
215 notificationData.badge = registerMockedURL(kIcon48x48); | 257 notificationData.badge = registerMockedURL(kIcon48x48); |
216 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); | 258 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size
_t>(2)); |
217 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); | 259 notificationData.actions[0].icon = registerMockedURL(kIcon110x110); |
218 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); | 260 notificationData.actions[1].icon = registerMockedURL(kIcon120x120); |
219 | 261 |
220 ASSERT_FALSE(resources()); | 262 ASSERT_FALSE(resources()); |
221 | 263 |
222 loader()->start(executionContext(), notificationData); | 264 loader()->start(executionContext(), notificationData); |
223 | 265 |
224 // Check that starting the loader did not synchronously fail, providing | 266 // Check that starting the loader did not synchronously fail, providing |
225 // empty resources. The requests should be pending now. | 267 // empty resources. The requests should be pending now. |
226 ASSERT_FALSE(resources()); | 268 ASSERT_FALSE(resources()); |
227 | 269 |
228 // The loader would stop e.g. when the execution context is destroyed or | 270 // 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. | 271 // when the loader is about to be destroyed, as a pre-finalizer. |
230 loader()->stop(); | 272 loader()->stop(); |
231 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | 273 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
232 | 274 |
233 // Loading should have been cancelled when |stop| was called so no resources | 275 // Loading should have been cancelled when |stop| was called so no resources |
234 // should have been received by the test even though | 276 // should have been received by the test even though |
235 // |serveAsynchronousRequests| was called. | 277 // |serveAsynchronousRequests| was called. |
236 ASSERT_FALSE(resources()); | 278 ASSERT_FALSE(resources()); |
237 } | 279 } |
238 | 280 |
239 } // namespace | 281 } // namespace |
240 } // namespace blink | 282 } // namespace blink |
OLD | NEW |