Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: third_party/WebKit/Source/modules/notifications/NotificationResourcesLoaderTest.cpp

Issue 2867693004: Snapshot of all changes to get jumbo in blink and content.
Patch Set: Rebased again Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory> 7 #include <memory>
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/loader/fetch/MemoryCache.h" 10 #include "platform/loader/fetch/MemoryCache.h"
11 #include "platform/testing/URLTestHelpers.h" 11 #include "platform/testing/URLTestHelpers.h"
12 #include "platform/testing/UnitTestHelpers.h" 12 #include "platform/testing/UnitTestHelpers.h"
13 #include "platform/weborigin/KURL.h" 13 #include "platform/weborigin/KURL.h"
14 #include "platform/wtf/Functional.h" 14 #include "platform/wtf/Functional.h"
15 #include "platform/wtf/text/WTFString.h" 15 #include "platform/wtf/text/WTFString.h"
16 #include "public/platform/Platform.h" 16 #include "public/platform/Platform.h"
17 #include "public/platform/WebURL.h" 17 #include "public/platform/WebURL.h"
18 #include "public/platform/WebURLLoaderMockFactory.h" 18 #include "public/platform/WebURLLoaderMockFactory.h"
19 #include "public/platform/WebURLResponse.h" 19 #include "public/platform/WebURLResponse.h"
20 #include "public/platform/modules/notifications/WebNotificationConstants.h" 20 #include "public/platform/modules/notifications/WebNotificationConstants.h"
21 #include "public/platform/modules/notifications/WebNotificationData.h" 21 #include "public/platform/modules/notifications/WebNotificationData.h"
22 #include "public/platform/modules/notifications/WebNotificationResources.h" 22 #include "public/platform/modules/notifications/WebNotificationResources.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 namespace blink { 25 namespace blink {
26 namespace { 26 namespace {
27 27
28 constexpr char kBaseUrl[] = "http://test.com/"; 28 constexpr char kResourcesLoaderBaseUrl[] = "http://test.com/";
29 constexpr char kBaseDir[] = "notifications/"; 29 constexpr char kResourcesLoaderBaseDir[] = "notifications/";
30 constexpr char kIcon48x48[] = "48x48.png"; 30 constexpr char kResourcesLoaderIcon48x48[] = "48x48.png";
31 constexpr char kIcon100x100[] = "100x100.png"; 31 constexpr char kResourcesLoaderIcon100x100[] = "100x100.png";
32 constexpr char kIcon110x110[] = "110x110.png"; 32 constexpr char kResourcesLoaderIcon110x110[] = "110x110.png";
33 constexpr char kIcon120x120[] = "120x120.png"; 33 constexpr char kResourcesLoaderIcon120x120[] = "120x120.png";
34 constexpr char kIcon500x500[] = "500x500.png"; 34 constexpr char kResourcesLoaderIcon500x500[] = "500x500.png";
35 constexpr char kIcon3000x1000[] = "3000x1000.png"; 35 constexpr char kResourcesLoaderIcon3000x1000[] = "3000x1000.png";
36 constexpr char kIcon3000x2000[] = "3000x2000.png"; 36 constexpr char kResourcesLoaderIcon3000x2000[] = "3000x2000.png";
37 37
38 class NotificationResourcesLoaderTest : public ::testing::Test { 38 class NotificationResourcesLoaderTest : public ::testing::Test {
39 public: 39 public:
40 NotificationResourcesLoaderTest() 40 NotificationResourcesLoaderTest()
41 : page_(DummyPageHolder::Create()), 41 : page_(DummyPageHolder::Create()),
42 loader_(new NotificationResourcesLoader( 42 loader_(new NotificationResourcesLoader(
43 Bind(&NotificationResourcesLoaderTest::DidFetchResources, 43 Bind(&NotificationResourcesLoaderTest::DidFetchResources,
44 WTF::Unretained(this)))) {} 44 WTF::Unretained(this)))) {}
45 45
46 ~NotificationResourcesLoaderTest() override { 46 ~NotificationResourcesLoaderTest() override {
(...skipping 13 matching lines...) Expand all
60 WebNotificationResources* Resources() const { return resources_.get(); } 60 WebNotificationResources* Resources() const { return resources_.get(); }
61 61
62 void DidFetchResources(NotificationResourcesLoader* loader) { 62 void DidFetchResources(NotificationResourcesLoader* loader) {
63 resources_ = loader->GetResources(); 63 resources_ = loader->GetResources();
64 } 64 }
65 65
66 // Registers a mocked url. When fetched, |fileName| will be loaded from the 66 // Registers a mocked url. When fetched, |fileName| will be loaded from the
67 // test data directory. 67 // test data directory.
68 WebURL RegisterMockedURL(const String& file_name) { 68 WebURL RegisterMockedURL(const String& file_name) {
69 WebURL registered_url = URLTestHelpers::RegisterMockedURLLoadFromBase( 69 WebURL registered_url = URLTestHelpers::RegisterMockedURLLoadFromBase(
70 kBaseUrl, testing::CoreTestDataPath(kBaseDir), file_name, "image/png"); 70 kResourcesLoaderBaseUrl,
71 testing::CoreTestDataPath(kResourcesLoaderBaseDir), file_name,
72 "image/png");
71 return registered_url; 73 return registered_url;
72 } 74 }
73 75
74 // Registers a mocked url that will fail to be fetched, with a 404 error. 76 // Registers a mocked url that will fail to be fetched, with a 404 error.
75 WebURL RegisterMockedErrorURL(const String& file_name) { 77 WebURL RegisterMockedErrorURL(const String& file_name) {
76 WebURL url(KURL(kParsedURLString, kBaseUrl + file_name)); 78 WebURL url(KURL(kParsedURLString, kResourcesLoaderBaseUrl + file_name));
77 URLTestHelpers::RegisterMockedErrorURLLoad(url); 79 URLTestHelpers::RegisterMockedErrorURLLoad(url);
78 return url; 80 return url;
79 } 81 }
80 82
81 private: 83 private:
82 std::unique_ptr<DummyPageHolder> page_; 84 std::unique_ptr<DummyPageHolder> page_;
83 Persistent<NotificationResourcesLoader> loader_; 85 Persistent<NotificationResourcesLoader> loader_;
84 std::unique_ptr<WebNotificationResources> resources_; 86 std::unique_ptr<WebNotificationResources> resources_;
85 }; 87 };
86 88
87 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) { 89 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) {
88 WebNotificationData notification_data; 90 WebNotificationData notification_data;
89 notification_data.image = RegisterMockedURL(kIcon500x500); 91 notification_data.image = RegisterMockedURL(kResourcesLoaderIcon500x500);
90 notification_data.icon = RegisterMockedURL(kIcon100x100); 92 notification_data.icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
91 notification_data.badge = RegisterMockedURL(kIcon48x48); 93 notification_data.badge = RegisterMockedURL(kResourcesLoaderIcon48x48);
92 notification_data.actions = 94 notification_data.actions =
93 WebVector<WebNotificationAction>(static_cast<size_t>(2)); 95 WebVector<WebNotificationAction>(static_cast<size_t>(2));
94 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110); 96 notification_data.actions[0].icon =
95 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120); 97 RegisterMockedURL(kResourcesLoaderIcon110x110);
98 notification_data.actions[1].icon =
99 RegisterMockedURL(kResourcesLoaderIcon120x120);
96 100
97 ASSERT_FALSE(Resources()); 101 ASSERT_FALSE(Resources());
98 102
99 Loader()->Start(GetExecutionContext(), notification_data); 103 Loader()->Start(GetExecutionContext(), notification_data);
100 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 104 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
101 105
102 ASSERT_TRUE(Resources()); 106 ASSERT_TRUE(Resources());
103 107
104 ASSERT_FALSE(Resources()->image.drawsNothing()); 108 ASSERT_FALSE(Resources()->image.drawsNothing());
105 ASSERT_EQ(500, Resources()->image.width()); 109 ASSERT_EQ(500, Resources()->image.width());
106 ASSERT_EQ(500, Resources()->image.height()); 110 ASSERT_EQ(500, Resources()->image.height());
107 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()->action_icons.size()); 118 ASSERT_EQ(2u, Resources()->action_icons.size());
115 ASSERT_FALSE(Resources()->action_icons[0].drawsNothing()); 119 ASSERT_FALSE(Resources()->action_icons[0].drawsNothing());
116 ASSERT_EQ(110, Resources()->action_icons[0].width()); 120 ASSERT_EQ(110, Resources()->action_icons[0].width());
117 ASSERT_FALSE(Resources()->action_icons[1].drawsNothing()); 121 ASSERT_FALSE(Resources()->action_icons[1].drawsNothing());
118 ASSERT_EQ(120, Resources()->action_icons[1].width()); 122 ASSERT_EQ(120, Resources()->action_icons[1].width());
119 } 123 }
120 124
121 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) { 125 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) {
122 WebNotificationData notification_data; 126 WebNotificationData notification_data;
123 notification_data.icon = RegisterMockedURL(kIcon500x500); 127 notification_data.icon = RegisterMockedURL(kResourcesLoaderIcon500x500);
124 notification_data.badge = notification_data.icon; 128 notification_data.badge = notification_data.icon;
125 notification_data.actions = 129 notification_data.actions =
126 WebVector<WebNotificationAction>(static_cast<size_t>(1)); 130 WebVector<WebNotificationAction>(static_cast<size_t>(1));
127 notification_data.actions[0].icon = notification_data.icon; 131 notification_data.actions[0].icon = notification_data.icon;
128 132
129 ASSERT_FALSE(Resources()); 133 ASSERT_FALSE(Resources());
130 134
131 Loader()->Start(GetExecutionContext(), notification_data); 135 Loader()->Start(GetExecutionContext(), notification_data);
132 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 136 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
133 137
(...skipping 10 matching lines...) Expand all
144 ASSERT_EQ(1u, Resources()->action_icons.size()); 148 ASSERT_EQ(1u, Resources()->action_icons.size());
145 ASSERT_FALSE(Resources()->action_icons[0].drawsNothing()); 149 ASSERT_FALSE(Resources()->action_icons[0].drawsNothing());
146 ASSERT_EQ(kWebNotificationMaxActionIconSizePx, 150 ASSERT_EQ(kWebNotificationMaxActionIconSizePx,
147 Resources()->action_icons[0].width()); 151 Resources()->action_icons[0].width());
148 ASSERT_EQ(kWebNotificationMaxActionIconSizePx, 152 ASSERT_EQ(kWebNotificationMaxActionIconSizePx,
149 Resources()->action_icons[0].height()); 153 Resources()->action_icons[0].height());
150 } 154 }
151 155
152 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_1AspectRatio) { 156 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_1AspectRatio) {
153 WebNotificationData notification_data; 157 WebNotificationData notification_data;
154 notification_data.image = RegisterMockedURL(kIcon3000x1000); 158 notification_data.image = RegisterMockedURL(kResourcesLoaderIcon3000x1000);
155 159
156 ASSERT_FALSE(Resources()); 160 ASSERT_FALSE(Resources());
157 161
158 Loader()->Start(GetExecutionContext(), notification_data); 162 Loader()->Start(GetExecutionContext(), notification_data);
159 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 163 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
160 164
161 ASSERT_TRUE(Resources()); 165 ASSERT_TRUE(Resources());
162 166
163 ASSERT_FALSE(Resources()->image.drawsNothing()); 167 ASSERT_FALSE(Resources()->image.drawsNothing());
164 ASSERT_EQ(kWebNotificationMaxImageWidthPx, Resources()->image.width()); 168 ASSERT_EQ(kWebNotificationMaxImageWidthPx, Resources()->image.width());
165 ASSERT_EQ(kWebNotificationMaxImageWidthPx / 3, Resources()->image.height()); 169 ASSERT_EQ(kWebNotificationMaxImageWidthPx / 3, Resources()->image.height());
166 } 170 }
167 171
168 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_2AspectRatio) { 172 TEST_F(NotificationResourcesLoaderTest, DownscalingPreserves3_2AspectRatio) {
169 WebNotificationData notification_data; 173 WebNotificationData notification_data;
170 notification_data.image = RegisterMockedURL(kIcon3000x2000); 174 notification_data.image = RegisterMockedURL(kResourcesLoaderIcon3000x2000);
171 175
172 ASSERT_FALSE(Resources()); 176 ASSERT_FALSE(Resources());
173 177
174 Loader()->Start(GetExecutionContext(), notification_data); 178 Loader()->Start(GetExecutionContext(), notification_data);
175 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 179 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
176 180
177 ASSERT_TRUE(Resources()); 181 ASSERT_TRUE(Resources());
178 182
179 ASSERT_FALSE(Resources()->image.drawsNothing()); 183 ASSERT_FALSE(Resources()->image.drawsNothing());
180 ASSERT_EQ(kWebNotificationMaxImageHeightPx * 3 / 2, 184 ASSERT_EQ(kWebNotificationMaxImageHeightPx * 3 / 2,
(...skipping 13 matching lines...) Expand all
194 198
195 ASSERT_TRUE(Resources()->image.drawsNothing()); 199 ASSERT_TRUE(Resources()->image.drawsNothing());
196 ASSERT_TRUE(Resources()->icon.drawsNothing()); 200 ASSERT_TRUE(Resources()->icon.drawsNothing());
197 ASSERT_TRUE(Resources()->badge.drawsNothing()); 201 ASSERT_TRUE(Resources()->badge.drawsNothing());
198 ASSERT_EQ(0u, Resources()->action_icons.size()); 202 ASSERT_EQ(0u, Resources()->action_icons.size());
199 } 203 }
200 204
201 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) { 205 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) {
202 WebNotificationData notification_data; 206 WebNotificationData notification_data;
203 notification_data.image = notification_data.icon; 207 notification_data.image = notification_data.icon;
204 notification_data.icon = RegisterMockedErrorURL(kIcon100x100); 208 notification_data.icon = RegisterMockedErrorURL(kResourcesLoaderIcon100x100);
205 notification_data.badge = notification_data.icon; 209 notification_data.badge = notification_data.icon;
206 notification_data.actions = 210 notification_data.actions =
207 WebVector<WebNotificationAction>(static_cast<size_t>(1)); 211 WebVector<WebNotificationAction>(static_cast<size_t>(1));
208 notification_data.actions[0].icon = notification_data.icon; 212 notification_data.actions[0].icon = notification_data.icon;
209 213
210 ASSERT_FALSE(Resources()); 214 ASSERT_FALSE(Resources());
211 215
212 Loader()->Start(GetExecutionContext(), notification_data); 216 Loader()->Start(GetExecutionContext(), notification_data);
213 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 217 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
214 218
215 ASSERT_TRUE(Resources()); 219 ASSERT_TRUE(Resources());
216 220
217 // 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
218 // 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.
219 ASSERT_TRUE(Resources()->image.drawsNothing()); 223 ASSERT_TRUE(Resources()->image.drawsNothing());
220 ASSERT_TRUE(Resources()->icon.drawsNothing()); 224 ASSERT_TRUE(Resources()->icon.drawsNothing());
221 ASSERT_TRUE(Resources()->badge.drawsNothing()); 225 ASSERT_TRUE(Resources()->badge.drawsNothing());
222 ASSERT_EQ(1u, Resources()->action_icons.size()); 226 ASSERT_EQ(1u, Resources()->action_icons.size());
223 ASSERT_TRUE(Resources()->action_icons[0].drawsNothing()); 227 ASSERT_TRUE(Resources()->action_icons[0].drawsNothing());
224 } 228 }
225 229
226 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) { 230 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) {
227 WebNotificationData notification_data; 231 WebNotificationData notification_data;
228 notification_data.icon = RegisterMockedURL(kIcon100x100); 232 notification_data.icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
229 notification_data.badge = RegisterMockedErrorURL(kIcon48x48); 233 notification_data.badge = RegisterMockedErrorURL(kResourcesLoaderIcon48x48);
230 234
231 ASSERT_FALSE(Resources()); 235 ASSERT_FALSE(Resources());
232 236
233 Loader()->Start(GetExecutionContext(), notification_data); 237 Loader()->Start(GetExecutionContext(), notification_data);
234 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 238 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
235 239
236 ASSERT_TRUE(Resources()); 240 ASSERT_TRUE(Resources());
237 241
238 // The test received resources even though one image failed to load. This 242 // The test received resources even though one image failed to load. This
239 // ensures that a notification can still be shown, though slightly degraded. 243 // ensures that a notification can still be shown, though slightly degraded.
240 ASSERT_TRUE(Resources()->image.drawsNothing()); 244 ASSERT_TRUE(Resources()->image.drawsNothing());
241 ASSERT_FALSE(Resources()->icon.drawsNothing()); 245 ASSERT_FALSE(Resources()->icon.drawsNothing());
242 ASSERT_EQ(100, Resources()->icon.width()); 246 ASSERT_EQ(100, Resources()->icon.width());
243 ASSERT_TRUE(Resources()->badge.drawsNothing()); 247 ASSERT_TRUE(Resources()->badge.drawsNothing());
244 ASSERT_EQ(0u, Resources()->action_icons.size()); 248 ASSERT_EQ(0u, Resources()->action_icons.size());
245 } 249 }
246 250
247 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) { 251 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) {
248 WebNotificationData notification_data; 252 WebNotificationData notification_data;
249 notification_data.image = RegisterMockedURL(kIcon500x500); 253 notification_data.image = RegisterMockedURL(kResourcesLoaderIcon500x500);
250 notification_data.icon = RegisterMockedURL(kIcon100x100); 254 notification_data.icon = RegisterMockedURL(kResourcesLoaderIcon100x100);
251 notification_data.badge = RegisterMockedURL(kIcon48x48); 255 notification_data.badge = RegisterMockedURL(kResourcesLoaderIcon48x48);
252 notification_data.actions = 256 notification_data.actions =
253 WebVector<WebNotificationAction>(static_cast<size_t>(2)); 257 WebVector<WebNotificationAction>(static_cast<size_t>(2));
254 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110); 258 notification_data.actions[0].icon =
255 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120); 259 RegisterMockedURL(kResourcesLoaderIcon110x110);
260 notification_data.actions[1].icon =
261 RegisterMockedURL(kResourcesLoaderIcon120x120);
256 262
257 ASSERT_FALSE(Resources()); 263 ASSERT_FALSE(Resources());
258 264
259 Loader()->Start(GetExecutionContext(), notification_data); 265 Loader()->Start(GetExecutionContext(), notification_data);
260 266
261 // Check that starting the loader did not synchronously fail, providing 267 // Check that starting the loader did not synchronously fail, providing
262 // empty resources. The requests should be pending now. 268 // empty resources. The requests should be pending now.
263 ASSERT_FALSE(Resources()); 269 ASSERT_FALSE(Resources());
264 270
265 // The loader would stop e.g. when the execution context is destroyed or 271 // The loader would stop e.g. when the execution context is destroyed or
266 // when the loader is about to be destroyed, as a pre-finalizer. 272 // when the loader is about to be destroyed, as a pre-finalizer.
267 Loader()->Stop(); 273 Loader()->Stop();
268 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 274 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
269 275
270 // Loading should have been cancelled when |stop| was called so no resources 276 // Loading should have been cancelled when |stop| was called so no resources
271 // should have been received by the test even though 277 // should have been received by the test even though
272 // |serveAsynchronousRequests| was called. 278 // |serveAsynchronousRequests| was called.
273 ASSERT_FALSE(Resources()); 279 ASSERT_FALSE(Resources());
274 } 280 }
275 281
276 } // namespace 282 } // namespace
277 } // namespace blink 283 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698