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

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

Issue 1904163002: Move Web Notifications to use Mojo Base URL: https://chromium.googlesource.com/chromium/src.git@skbitmap-blink
Patch Set: it works \o/ Created 4 years, 7 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 "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/bitmap_type_converters.h"
9 #include "platform/heap/Heap.h" 10 #include "platform/heap/Heap.h"
10 #include "platform/testing/URLTestHelpers.h" 11 #include "platform/testing/URLTestHelpers.h"
11 #include "platform/weborigin/KURL.h" 12 #include "platform/weborigin/KURL.h"
12 #include "public/platform/Platform.h" 13 #include "public/platform/Platform.h"
13 #include "public/platform/WebURL.h" 14 #include "public/platform/WebURL.h"
14 #include "public/platform/WebURLLoaderMockFactory.h" 15 #include "public/platform/WebURLLoaderMockFactory.h"
15 #include "public/platform/WebURLResponse.h" 16 #include "public/platform/WebURLResponse.h"
16 #include "public/platform/modules/notifications/WebNotificationData.h" 17 #include "public/platform/modules/notifications/notification.mojom-blink.h"
17 #include "public/platform/modules/notifications/WebNotificationResources.h" 18 #include "public/platform/modules/notifications/notification_resources.mojom-bli nk.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
33 const int kMaxIconSizePx = 320; 34 const int kMaxIconSizePx = 320;
34 const int kMaxBadgeSizePx = 96; 35 const int kMaxBadgeSizePx = 96;
35 const int kMaxActionIconSizePx = 128; 36 const int kMaxActionIconSizePx = 128;
36 37
37 class NotificationResourcesLoaderTest : public ::testing::Test { 38 class NotificationResourcesLoaderTest : public ::testing::Test {
38 public: 39 public:
39 NotificationResourcesLoaderTest() 40 NotificationResourcesLoaderTest()
40 : m_page(DummyPageHolder::create()), m_loader(new NotificationResourcesL oader(bind<NotificationResourcesLoader*>(&NotificationResourcesLoaderTest::didFe tchResources, this))) 41 : m_page(DummyPageHolder::create()), m_loader(new NotificationResourcesL oader(bind<NotificationResourcesLoader*, mojom::blink::NotificationPtr>(&Notific ationResourcesLoaderTest::didFetchResources, this)))
41 { 42 {
42 } 43 }
43 44
44 ~NotificationResourcesLoaderTest() override 45 ~NotificationResourcesLoaderTest() override
45 { 46 {
46 m_loader->stop(); 47 m_loader->stop();
47 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); 48 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs();
48 memoryCache()->evictResources(); 49 memoryCache()->evictResources();
49 } 50 }
50 51
51 protected: 52 protected:
53 // Structure for representing the resources loaded by the resource loader as
54 // SkBitmap, as opposed to the Mojo Bitmap type.
55 struct Resources {
56 SkBitmap icon;
57 SkBitmap badge;
58 WTF::Vector<SkBitmap> actionIcons;
59 };
60
52 ExecutionContext* executionContext() const { return &m_page->document(); } 61 ExecutionContext* executionContext() const { return &m_page->document(); }
53 62
54 NotificationResourcesLoader* loader() const { return m_loader.get(); } 63 NotificationResourcesLoader* loader() const { return m_loader.get(); }
55 64
56 WebNotificationResources* resources() const { return m_resources.get(); } 65 bool hasResources() const { return m_hasResources; }
57 66
58 void didFetchResources(NotificationResourcesLoader* loader) 67 const Resources* resources() const { return &m_resources; }
68
69 void didFetchResources(NotificationResourcesLoader* loader, mojom::blink::No tificationPtr /* notification */)
59 { 70 {
60 m_resources = loader->getResources(); 71 mojom::blink::NotificationResourcesPtr resources = loader->getResources( );
72 DCHECK(resources);
73
74 m_resources.icon = resources->icon.To<SkBitmap>();
75 m_resources.badge = resources->badge.To<SkBitmap>();
76
77 m_resources.actionIcons.resize(resources->action_icons.size());
78 for (size_t i = 0; i < resources->action_icons.size(); ++i)
79 m_resources.actionIcons[i] = resources->action_icons[i].To<SkBitmap> ();
80
81 m_hasResources = true;
61 } 82 }
62 83
63 // Registers a mocked url. When fetched, |fileName| will be loaded from the 84 // Registers a mocked url. When fetched, |fileName| will be loaded from the
64 // test data directory. 85 // test data directory.
65 WebURL registerMockedURL(const String& fileName) 86 String registerMockedURL(const String& fileName)
66 { 87 {
67 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); 88 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
68 89
69 WebURLResponse response(url); 90 WebURLResponse response(url);
70 response.setMIMEType("image/png"); 91 response.setMIMEType("image/png");
71 response.setHTTPStatusCode(200); 92 response.setHTTPStatusCode(200);
72 93
73 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, fileName, " notifications/", response); 94 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, fileName, " notifications/", response);
74 95
75 return url; 96 return url.string();
76 } 97 }
77 98
78 // Registers a mocked url that will fail to be fetched, with a 404 error. 99 // Registers a mocked url that will fail to be fetched, with a 404 error.
79 WebURL registerMockedErrorURL(const String& fileName) 100 String registerMockedErrorURL(const String& fileName)
80 { 101 {
81 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName)); 102 WebURL url(KURL(ParsedURLString, kBaseUrl + fileName));
82 URLTestHelpers::registerMockedErrorURLLoad(url); 103 URLTestHelpers::registerMockedErrorURLLoad(url);
83 return url; 104
105 return url.string();
84 } 106 }
85 107
86 private: 108 private:
87 OwnPtr<DummyPageHolder> m_page; 109 OwnPtr<DummyPageHolder> m_page;
88 Persistent<NotificationResourcesLoader> m_loader; 110 Persistent<NotificationResourcesLoader> m_loader;
89 std::unique_ptr<WebNotificationResources> m_resources; 111
112 bool m_hasResources = false;
113 Resources m_resources;
90 }; 114 };
91 115
116 // Creates a Mojo NotificationAction object for an action having the |iconUrl|.
117 mojom::blink::NotificationActionPtr createActionWithIcon(String iconUrl) {
118 mojom::blink::NotificationActionPtr action = mojom::blink::NotificationActio n::New();
119 action->icon = iconUrl;
120
121 return action;
122 }
123
92 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources) 124 TEST_F(NotificationResourcesLoaderTest, LoadMultipleResources)
93 { 125 {
94 WebNotificationData notificationData; 126 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New ();
95 notificationData.icon = registerMockedURL(kIcon100x100); 127 notification->icon = registerMockedURL(kIcon100x100);
96 notificationData.badge = registerMockedURL(kIcon48x48); 128 notification->badge = registerMockedURL(kIcon48x48);
97 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(2));
98 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
99 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
100 129
101 ASSERT_FALSE(resources()); 130 notification->actions.resize(static_cast<size_t>(2));
131 notification->actions[0] = createActionWithIcon(registerMockedURL(kIcon110x1 10));
132 notification->actions[1] = createActionWithIcon(registerMockedURL(kIcon120x1 20));
102 133
103 loader()->start(executionContext(), notificationData); 134 ASSERT_FALSE(hasResources());
135
136 loader()->start(executionContext(), std::move(notification));
104 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 137 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
105 138
106 ASSERT_TRUE(resources()); 139 ASSERT_TRUE(hasResources());
107 140
108 ASSERT_FALSE(resources()->icon.drawsNothing()); 141 ASSERT_FALSE(resources()->icon.drawsNothing());
109 ASSERT_EQ(100, resources()->icon.width()); 142 ASSERT_EQ(100, resources()->icon.width());
110 143
111 ASSERT_FALSE(resources()->badge.drawsNothing()); 144 ASSERT_FALSE(resources()->badge.drawsNothing());
112 ASSERT_EQ(48, resources()->badge.width()); 145 ASSERT_EQ(48, resources()->badge.width());
113 146
114 ASSERT_EQ(2u, resources()->actionIcons.size()); 147 ASSERT_EQ(2u, resources()->actionIcons.size());
115 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); 148 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
116 ASSERT_EQ(110, resources()->actionIcons[0].width()); 149 ASSERT_EQ(110, resources()->actionIcons[0].width());
117 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing()); 150 ASSERT_FALSE(resources()->actionIcons[1].drawsNothing());
118 ASSERT_EQ(120, resources()->actionIcons[1].width()); 151 ASSERT_EQ(120, resources()->actionIcons[1].width());
119 } 152 }
120 153
121 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown) 154 TEST_F(NotificationResourcesLoaderTest, LargeIconsAreScaledDown)
122 { 155 {
123 WebNotificationData notificationData; 156 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New ();
124 notificationData.icon = registerMockedURL(kIcon500x500); 157 notification->icon = registerMockedURL(kIcon500x500);
125 notificationData.badge = notificationData.icon; 158 notification->badge = notification->icon;
126 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(1));
127 notificationData.actions[0].icon = notificationData.icon;
128 159
129 ASSERT_FALSE(resources()); 160 notification->actions.resize(static_cast<size_t>(1));
161 notification->actions[0] = createActionWithIcon(notification->icon);
130 162
131 loader()->start(executionContext(), notificationData); 163 ASSERT_FALSE(hasResources());
164
165 loader()->start(executionContext(), std::move(notification));
132 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 166 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
133 167
134 ASSERT_TRUE(resources()); 168 ASSERT_TRUE(hasResources());
135 169
136 ASSERT_FALSE(resources()->icon.drawsNothing()); 170 ASSERT_FALSE(resources()->icon.drawsNothing());
137 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width()); 171 ASSERT_EQ(kMaxIconSizePx, resources()->icon.width());
138 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height()); 172 ASSERT_EQ(kMaxIconSizePx, resources()->icon.height());
139 173
140 ASSERT_FALSE(resources()->badge.drawsNothing()); 174 ASSERT_FALSE(resources()->badge.drawsNothing());
141 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width()); 175 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.width());
142 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height()); 176 ASSERT_EQ(kMaxBadgeSizePx, resources()->badge.height());
143 177
144 ASSERT_EQ(1u, resources()->actionIcons.size()); 178 ASSERT_EQ(1u, resources()->actionIcons.size());
145 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing()); 179 ASSERT_FALSE(resources()->actionIcons[0].drawsNothing());
146 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width()); 180 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].width());
147 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height()); 181 ASSERT_EQ(kMaxActionIconSizePx, resources()->actionIcons[0].height());
148 } 182 }
149 183
150 184
151 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources) 185 TEST_F(NotificationResourcesLoaderTest, EmptyDataYieldsEmptyResources)
152 { 186 {
153 WebNotificationData notificationData; 187 ASSERT_FALSE(hasResources());
154 188
155 ASSERT_FALSE(resources()); 189 loader()->start(executionContext(), mojom::blink::Notification::New());
156
157 loader()->start(executionContext(), notificationData);
158 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 190 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
159 191
160 ASSERT_TRUE(resources()); 192 ASSERT_TRUE(hasResources());
161 193
162 ASSERT_TRUE(resources()->icon.drawsNothing()); 194 ASSERT_TRUE(resources()->icon.drawsNothing());
163 ASSERT_TRUE(resources()->badge.drawsNothing()); 195 ASSERT_TRUE(resources()->badge.drawsNothing());
164 ASSERT_EQ(0u, resources()->actionIcons.size()); 196 ASSERT_EQ(0u, resources()->actionIcons.size());
165 } 197 }
166 198
167 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad) 199 TEST_F(NotificationResourcesLoaderTest, EmptyResourcesIfAllImagesFailToLoad)
168 { 200 {
169 WebNotificationData notificationData; 201 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New ();
170 notificationData.icon = registerMockedErrorURL(kIcon100x100); 202 notification->icon = registerMockedErrorURL(kIcon100x100);
171 notificationData.badge = notificationData.icon; 203 notification->badge = notification->icon;
172 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(1));
173 notificationData.actions[0].icon = notificationData.icon;
174 204
175 ASSERT_FALSE(resources()); 205 notification->actions.resize(static_cast<size_t>(1));
206 notification->actions[0] = createActionWithIcon(notification->icon);
176 207
177 loader()->start(executionContext(), notificationData); 208 ASSERT_FALSE(hasResources());
209
210 loader()->start(executionContext(), std::move(notification));
178 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 211 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
179 212
180 ASSERT_TRUE(resources()); 213 ASSERT_TRUE(hasResources());
181 214
182 // The test received resources but they are all empty. This ensures that a 215 // 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. 216 // notification can still be shown even if the images fail to load.
184 ASSERT_TRUE(resources()->icon.drawsNothing()); 217 ASSERT_TRUE(resources()->icon.drawsNothing());
185 ASSERT_TRUE(resources()->badge.drawsNothing()); 218 ASSERT_TRUE(resources()->badge.drawsNothing());
186 ASSERT_EQ(1u, resources()->actionIcons.size()); 219 ASSERT_EQ(1u, resources()->actionIcons.size());
187 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing()); 220 ASSERT_TRUE(resources()->actionIcons[0].drawsNothing());
188 } 221 }
189 222
190 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad) 223 TEST_F(NotificationResourcesLoaderTest, OneImageFailsToLoad)
191 { 224 {
192 WebNotificationData notificationData; 225 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New ();
193 notificationData.icon = registerMockedURL(kIcon100x100); 226 notification->icon = registerMockedURL(kIcon100x100);
194 notificationData.badge = registerMockedErrorURL(kIcon48x48); 227 notification->badge = registerMockedErrorURL(kIcon48x48);
195 228
196 ASSERT_FALSE(resources()); 229 ASSERT_FALSE(hasResources());
197 230
198 loader()->start(executionContext(), notificationData); 231 loader()->start(executionContext(), std::move(notification));
199 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 232 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
200 233
201 ASSERT_TRUE(resources()); 234 ASSERT_TRUE(hasResources());
202 235
203 // The test received resources even though one image failed to load. This 236 // The test received resources even though one image failed to load. This
204 // ensures that a notification can still be shown, though slightly degraded. 237 // ensures that a notification can still be shown, though slightly degraded.
205 ASSERT_FALSE(resources()->icon.drawsNothing()); 238 ASSERT_FALSE(resources()->icon.drawsNothing());
206 ASSERT_EQ(100, resources()->icon.width()); 239 ASSERT_EQ(100, resources()->icon.width());
207 ASSERT_TRUE(resources()->badge.drawsNothing()); 240 ASSERT_TRUE(resources()->badge.drawsNothing());
208 ASSERT_EQ(0u, resources()->actionIcons.size()); 241 ASSERT_EQ(0u, resources()->actionIcons.size());
209 } 242 }
210 243
211 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources) 244 TEST_F(NotificationResourcesLoaderTest, StopYieldsNoResources)
212 { 245 {
213 WebNotificationData notificationData; 246 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New ();
214 notificationData.icon = registerMockedURL(kIcon100x100); 247 notification->icon = registerMockedURL(kIcon100x100);
215 notificationData.badge = registerMockedURL(kIcon48x48); 248 notification->badge = registerMockedURL(kIcon48x48);
216 notificationData.actions = WebVector<WebNotificationAction>(static_cast<size _t>(2));
217 notificationData.actions[0].icon = registerMockedURL(kIcon110x110);
218 notificationData.actions[1].icon = registerMockedURL(kIcon120x120);
219 249
220 ASSERT_FALSE(resources()); 250 notification->actions.resize(static_cast<size_t>(2));
251 notification->actions[0] = createActionWithIcon(registerMockedURL(kIcon110x1 10));
252 notification->actions[1] = createActionWithIcon(registerMockedURL(kIcon120x1 20));
221 253
222 loader()->start(executionContext(), notificationData); 254 ASSERT_FALSE(hasResources());
255
256 loader()->start(executionContext(), std::move(notification));
223 257
224 // Check that starting the loader did not synchronously fail, providing 258 // Check that starting the loader did not synchronously fail, providing
225 // empty resources. The requests should be pending now. 259 // empty resources. The requests should be pending now.
226 ASSERT_FALSE(resources()); 260 ASSERT_FALSE(hasResources());
227 261
228 // The loader would stop e.g. when the execution context is destroyed or 262 // 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. 263 // when the loader is about to be destroyed, as a pre-finalizer.
230 loader()->stop(); 264 loader()->stop();
231 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 265 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
232 266
233 // Loading should have been cancelled when |stop| was called so no resources 267 // Loading should have been cancelled when |stop| was called so no resources
234 // should have been received by the test even though 268 // should have been received by the test even though
235 // |serveAsynchronousRequests| was called. 269 // |serveAsynchronousRequests| was called.
236 ASSERT_FALSE(resources()); 270 ASSERT_FALSE(hasResources());
237 } 271 }
238 272
239 } // namespace 273 } // namespace
240 } // namespace blink 274 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698