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

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: Rebase. Fix ASAN. Cleanups. Address peter's comments. 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/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 static const int kMaxIconSizePx = 320;
Peter Beverloo 2016/04/18 15:22:37 nit: these are constants in an anonymous namespace
Michael van Ouwerkerk 2016/04/18 17:03:15 Done.
34 static const int kMaxBadgeSizePx = 96;
35 static const int kMaxActionIconSizePx = 128;
36
37 class NotificationResourcesLoaderTest : public ::testing::Test {
38 public:
39 NotificationResourcesLoaderTest() : m_page(DummyPageHolder::create()), m_loa der(new NotificationResourcesLoader(bind<NotificationResourcesLoader*>(&Notifica tionResourcesLoaderTest::didFetchResources, this)))
40 {
41 }
42
43 ~NotificationResourcesLoaderTest() override
44 {
45 m_loader->stop();
46 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs();
47
48 // Clean up (and silence the asan bot).
Peter Beverloo 2016/04/18 15:22:37 nit: the asan bot remark is not relevant
Michael van Ouwerkerk 2016/04/18 17:03:15 Done.
49 memoryCache()->evictResources();
50 }
51
52 protected:
53 ExecutionContext* executionContext() const { return &m_page->document(); }
54
55 NotificationResourcesLoader* loader() const { return m_loader.get(); }
56
57 WebNotificationResources* resources() const { return m_resources.get(); }
58
59 void didFetchResources(NotificationResourcesLoader* loader)
60 {
61 m_resources = loader->getResources();
62 }
63
64 // Registers a mocked url. When fetched, |fileName| will be loaded from the
65 // test data directory.
66 WebURL registerMockedURL(const String& fileName)
67 {
68 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
69
70 WebURLResponse response(url);
71 response.setMIMEType("image/png");
72 response.setHTTPStatusCode(200);
73
74 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, fileName, " notifications/", response);
75
76 return url;
77 }
78
79 // Registers a mocked url that will fail to be fetched, with a 404 error.
80 WebURL registerMockedErrorURL(const String& fileName)
81 {
82 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
83 URLTestHelpers::registerMockedErrorURLLoad(url);
84 return url;
85 }
86
87 private:
88 OwnPtr<DummyPageHolder> m_page;
89 Persistent<NotificationResourcesLoader> m_loader;
90 std::unique_ptr<WebNotificationResources> m_resources;
91 };
92
93 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources)
94 {
95 WebNotificationData notificationData;
96 notificationData.icon = registerMockedURL(kIcon100x100);
97 notificationData.badge = registerMockedURL(kIcon48x48);
98 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(2));
99 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
100 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
101
102 ASSERT_FALSE(resources());
103
104 loader()->start(executionContext(), notificationData);
105 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
106
107 ASSERT_TRUE(resources());
108
109 ASSERT_FALSE(resources()->icon.drawsNothing());
110 ASSERT_EQ(100, resources()->icon.width());
111
112 ASSERT_FALSE(resources()->badge.drawsNothing());
113 ASSERT_EQ(48, resources()->badge.width());
114
115 ASSERT_EQ(2u, resources()->actionIcons.size());
116 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
117 ASSERT_EQ(110, resources()->actionIcons[0].width());
118 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing());
119 ASSERT_EQ(120, resources()->actionIcons[1].width());
120 }
121
122 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown)
123 {
124 WebNotificationData notificationData;
125 notificationData.icon = registerMockedURL(kIcon500x500);
126 notificationData.badge = notificationData.icon;
127 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(1));
128 notificationData.actions[0].icon = notificationData.icon;
129
130 ASSERT_FALSE(resources());
131
132 loader()->start(executionContext(), notificationData);
133 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
134
135 ASSERT_TRUE(resources());
136
137 ASSERT_FALSE(resources()->icon.drawsNothing());
138 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width());
139 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height());
140
141 ASSERT_FALSE(resources()->badge.drawsNothing());
142 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width());
143 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height());
144
145 ASSERT_EQ(1u, resources()->actionIcons.size());
146 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
147 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width());
148 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height());
149 }
150
151
152 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources)
153 {
154 WebNotificationData notificationData;
155
156 ASSERT_FALSE(resources());
157
158 loader()->start(executionContext(), notificationData);
159 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
160
161 ASSERT_TRUE(resources());
162
163 ASSERT_TRUE(resources()->icon.drawsNothing());
164 ASSERT_TRUE(resources()->badge.drawsNothing());
165 ASSERT_EQ(0u, resources()->actionIcons.size());
166 }
167
168 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad)
169 {
170 WebNotificationData notificationData;
171 notificationData.icon = registerMockedErrorURL(kIcon100x100);
172 notificationData.badge = notificationData.icon;
173 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(1));
174 notificationData.actions[0].icon = notificationData.icon;
175
176 ASSERT_FALSE(resources());
177
178 loader()->start(executionContext(), notificationData);
179 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
180
181 ASSERT_TRUE(resources());
182
183 // The test received resources but they are all empty. This ensures that a
184 // notification can still be shown even if the images fail to load.
185 ASSERT_TRUE(resources()->icon.drawsNothing());
186 ASSERT_TRUE(resources()->badge.drawsNothing());
187 ASSERT_EQ(1u, resources()->actionIcons.size());
188 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing());
189 }
190
191 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad)
192 {
193 WebNotificationData notificationData;
194 notificationData.icon = registerMockedURL(kIcon100x100);
195 notificationData.badge = registerMockedErrorURL(kIcon48x48);
196
197 ASSERT_FALSE(resources());
198
199 loader()->start(executionContext(), notificationData);
200 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
201
202 ASSERT_TRUE(resources());
203
204 // The test received resources even though one image failed to load. This
205 // ensures that a notification can still be shown, though slightly degraded.
206 ASSERT_FALSE(resources()->icon.drawsNothing());
207 ASSERT_EQ(100, resources()->icon.width());
208 ASSERT_TRUE(resources()->badge.drawsNothing());
209 ASSERT_EQ(0u, resources()->actionIcons.size());
210 }
211
212 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources)
213 {
214 WebNotificationData notificationData;
215 notificationData.icon = registerMockedURL(kIcon100x100);
216 notificationData.badge = registerMockedURL(kIcon48x48);
217 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(2));
218 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
219 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
220
221 ASSERT_FALSE(resources());
222
223 loader()->start(executionContext(), notificationData);
224
225 // Check that starting the loader did not synchronously fail, providing
226 // empty resources. The requests should be pending now.
227 ASSERT_FALSE(resources());
228
229 // The loader would stop e.g. when the execution context is destroyed or
230 // when the loader is about to be destroyed, as a pre-finalizer.
231 loader()->stop();
232 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
233
234 // Loading should have been cancelled when |stop| was called so no resources
235 // should have been received by the test even though
236 // |serveAsynchronousRequests| was called.
237 ASSERT_FALSE(resources());
238 }
239
240 } // namespace
241 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698