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

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

Issue 1847863002: Move notification resource loading from content/child to blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Call stop when all fetches have finished. Created 4 years, 8 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
(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 , m_loader(new NotificationResourcesLoader(executionContext(),
40 bind<NotificationResourcesLoader*>(
41 &NotificationResourcesLoaderTest::didFetchResources, this)))
42 {
43 LOG(WARNING) << __FUNCTION__;
44 }
45
46 ~NotificationResourcesLoaderTest() override
47 {
48 LOG(WARNING) << __FUNCTION__;
49 }
50
51 void TearDown() override
52 {
53 LOG(WARNING) << __FUNCTION__;
54 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs();
55 m_loader->stop();
56 }
57
58 protected:
59 ExecutionContext* executionContext() const { return &m_page->document(); }
60
61 NotificationResourcesLoader* loader() const { return m_loader.get(); }
62
63 WebNotificationResources* resources() const { return m_resources.get(); }
64
65 void didFetchResources(NotificationResourcesLoader* loader)
66 {
67 m_resources = loader->getResources();
68 }
69
70 // Registers a mocked url. When fetched, |fileName| will be loaded from the
71 // test data directory.
72 WebURL registerMockedURL(const String& fileName)
73 {
74 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
75
76 WebURLResponse response(url);
77 response.setMIMEType("image/png");
78 response.setHTTPStatusCode(200);
79
80 URLTestHelpers::registerMockedURLLoadWithCustomResponse(
81 url, fileName, "notifications/", response);
82
83 return url;
84 }
85
86 // Registers a mocked url that will fail to be fetched, with a 404 error.
87 WebURL registerMockedErrorURL(const String& fileName)
88 {
89 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
90 URLTestHelpers::registerMockedErrorURLLoad(url);
91 return url;
92 }
93
94 private:
95 OwnPtr<DummyPageHolder> m_page;
96 Persistent<NotificationResourcesLoader> m_loader;
97 std::unique_ptr<WebNotificationResources> m_resources;
98 };
99
100 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources)
101 {
102 WebNotificationData notificationData;
103 notificationData.icon = registerMockedURL(kIcon100x100);
104 notificationData.badge = registerMockedURL(kIcon48x48);
105 notificationData.actions =
106 WebVector<WebNotificationAction>(static_cast<size_t>(2));
107 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
108 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
109
110 ASSERT_FALSE(resources());
111
112 loader()->start(notificationData);
113 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
114
115 ASSERT_TRUE(resources());
116
117 ASSERT_FALSE(resources()->icon.drawsNothing());
118 ASSERT_EQ(100, resources()->icon.width());
119
120 ASSERT_FALSE(resources()->badge.drawsNothing());
121 ASSERT_EQ(48, resources()->badge.width());
122
123 ASSERT_EQ(2u, resources()->actionIcons.size());
124 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
125 ASSERT_EQ(110, resources()->actionIcons[0].width());
126 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing());
127 ASSERT_EQ(120, resources()->actionIcons[1].width());
128 }
129
130 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown)
131 {
132 WebNotificationData notificationData;
133 notificationData.icon = registerMockedURL(kIcon500x500);
134 notificationData.badge = notificationData.icon;
135 notificationData.actions =
136 WebVector<WebNotificationAction>(static_cast<size_t>(1));
137 notificationData.actions[0].icon = notificationData.icon;
138
139 ASSERT_FALSE(resources());
140
141 loader()->start(notificationData);
142 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
143
144 ASSERT_TRUE(resources());
145
146 ASSERT_FALSE(resources()->icon.drawsNothing());
147 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width());
148 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height());
149
150 ASSERT_FALSE(resources()->badge.drawsNothing());
151 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width());
152 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height());
153
154 ASSERT_EQ(1u, resources()->actionIcons.size());
155 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
156 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width());
157 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height());
158 }
159
160
161 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources)
162 {
163 WebNotificationData notificationData;
164
165 ASSERT_FALSE(resources());
166
167 loader()->start(notificationData);
168 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
169
170 ASSERT_TRUE(resources());
171
172 ASSERT_TRUE(resources()->icon.drawsNothing());
173 ASSERT_TRUE(resources()->badge.drawsNothing());
174 ASSERT_EQ(0u, resources()->actionIcons.size());
175 }
176
177 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad)
178 {
179 WebNotificationData notificationData;
180 notificationData.icon = registerMockedErrorURL(kIcon100x100);
181 notificationData.badge = notificationData.icon;
182 notificationData.actions =
183 WebVector<WebNotificationAction>(static_cast<size_t>(1));
184 notificationData.actions[0].icon = notificationData.icon;
185
186 ASSERT_FALSE(resources());
187
188 loader()->start(notificationData);
189 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
190
191 ASSERT_TRUE(resources());
192
193 // The test received resources but they are all empty. This ensures that a
194 // notification can still be shown even if the images fail to load.
195 ASSERT_TRUE(resources()->icon.drawsNothing());
196 ASSERT_TRUE(resources()->badge.drawsNothing());
197 ASSERT_EQ(1u, resources()->actionIcons.size());
198 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing());
199 }
200
201 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad)
202 {
203 WebNotificationData notificationData;
204 notificationData.icon = registerMockedURL(kIcon100x100);
205 notificationData.badge = registerMockedErrorURL(kIcon48x48);
206
207 ASSERT_FALSE(resources());
208
209 loader()->start(notificationData);
210 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
211
212 ASSERT_TRUE(resources());
213
214 // The test received resources even though one image failed to load. This
215 // ensures that a notification can still be shown, though slightly degraded.
216 ASSERT_FALSE(resources()->icon.drawsNothing());
217 ASSERT_EQ(100, resources()->icon.width());
218 ASSERT_TRUE(resources()->badge.drawsNothing());
219 ASSERT_EQ(0u, resources()->actionIcons.size());
220 }
221
222 TEST_F(NotificationResourcesLoaderTest, ExecutionContextDestroyed)
223 {
224 WebNotificationData notificationData;
225 notificationData.icon = registerMockedURL(kIcon100x100);
226 notificationData.badge = registerMockedURL(kIcon48x48);
227 notificationData.actions =
228 WebVector<WebNotificationAction>(static_cast<size_t>(2));
229 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
230 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
231
232 ASSERT_FALSE(resources());
233
234 loader()->start(notificationData);
235
236 // Check that starting the loader did not synchronously fail, providing
237 // empty resources. The requests should be pending now.
238 ASSERT_FALSE(resources());
239
240 executionContext()->notifyContextDestroyed();
241 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
242
243 // Loading should have been cancelled when |notifyContextDestroyed| was
244 // called so no resources should have been received by the test even though
245 // |serveAsynchronousRequests| was called.
246 ASSERT_FALSE(resources());
247 }
248
249 } // namespace
250 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698