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

Side by Side Diff: content/child/notifications/pending_notifications_tracker_unittest.cc

Issue 1847863002: Move notification resource loading from content/child to blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 "content/child/notifications/pending_notifications_tracker.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "base/base_paths.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/location.h"
16 #include "base/macros.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/path_service.h"
19 #include "base/run_loop.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "content/common/notification_constants.h"
22 #include "content/public/common/notification_resources.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/WebKit/public/platform/Platform.h"
25 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/platform/WebURL.h"
27 #include "third_party/WebKit/public/platform/WebURLError.h"
28 #include "third_party/WebKit/public/platform/WebURLLoaderMockFactory.h"
29 #include "third_party/WebKit/public/platform/WebURLResponse.h"
30 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onData.h"
31 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onDelegate.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "url/gurl.h"
34
35 namespace content {
36
37 namespace {
38
39 const char kBaseUrl[] = "http://test.com/";
40 const char kIcon48x48[] = "48x48.png";
41 const char kIcon100x100[] = "100x100.png";
42 const char kIcon110x110[] = "110x110.png";
43 const char kIcon120x120[] = "120x120.png";
44 const char kIcon500x500[] = "500x500.png";
45
46 class FakeNotificationDelegate : public blink::WebNotificationDelegate {
47 public:
48 void dispatchClickEvent() override {}
49 void dispatchShowEvent() override {}
50 void dispatchErrorEvent() override {}
51 void dispatchCloseEvent() override {}
52 };
53
54 } // namespace
55
56 class PendingNotificationsTrackerTest : public testing::Test {
57 public:
58 PendingNotificationsTrackerTest()
59 : tracker_(new PendingNotificationsTracker(
60 base::ThreadTaskRunnerHandle::Get())) {}
61
62 ~PendingNotificationsTrackerTest() override {
63 GetURLLoaderMockFactory()->unregisterAllURLs();
64 }
65
66 void DidFetchResources(size_t index, const NotificationResources& resources) {
67 if (resources_.size() < index + 1)
68 resources_.resize(index + 1);
69 resources_[index] = resources;
70 }
71
72 protected:
73 PendingNotificationsTracker* tracker() { return tracker_.get(); }
74
75 size_t CountResources() { return resources_.size(); }
76
77 NotificationResources* GetResources(size_t index) {
78 DCHECK_LT(index, resources_.size());
79 return &resources_[index];
80 }
81
82 size_t CountPendingNotifications() {
83 return tracker_->pending_notifications_.size();
84 }
85
86 size_t CountDelegates() {
87 return tracker_->delegate_to_pending_id_map_.size();
88 }
89
90 blink::WebURLLoaderMockFactory* GetURLLoaderMockFactory() {
91 return blink::Platform::current()->getURLLoaderMockFactory();
92 }
93
94 // Registers a mocked url. When fetched, |file_name| will be loaded from the
95 // test data directory.
96 blink::WebURL RegisterMockedURL(const std::string& file_name) {
97 blink::WebURL url(GURL(kBaseUrl + file_name));
98
99 blink::WebURLResponse response(url);
100 response.setMIMEType("image/png");
101 response.setHTTPStatusCode(200);
102
103 base::FilePath file_path;
104 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
105 file_path = file_path.Append(FILE_PATH_LITERAL("content"))
106 .Append(FILE_PATH_LITERAL("test"))
107 .Append(FILE_PATH_LITERAL("data"))
108 .Append(FILE_PATH_LITERAL("notifications"))
109 .AppendASCII(file_name);
110 file_path = base::MakeAbsoluteFilePath(file_path);
111 blink::WebString string_file_path =
112 blink::WebString::fromUTF8(file_path.MaybeAsASCII());
113
114 GetURLLoaderMockFactory()->registerURL(url, response, string_file_path);
115
116 return url;
117 }
118
119 // Registers a mocked url that will fail to be fetched, with a 404 error.
120 blink::WebURL RegisterMockedErrorURL(const std::string& file_name) {
121 blink::WebURL url(GURL(kBaseUrl + file_name));
122
123 blink::WebURLResponse response(url);
124 response.setMIMEType("image/png");
125 response.setHTTPStatusCode(404);
126
127 blink::WebURLError error;
128 error.reason = 404;
129
130 GetURLLoaderMockFactory()->registerErrorURL(url, response, error);
131 return url;
132 }
133
134 private:
135 base::MessageLoop message_loop_;
136 std::unique_ptr<PendingNotificationsTracker> tracker_;
137 std::vector<NotificationResources> resources_;
138
139 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTrackerTest);
140 };
141
142 TEST_F(PendingNotificationsTrackerTest, OneNotificationMultipleResources) {
143 blink::WebNotificationData notification_data;
144 notification_data.icon = RegisterMockedURL(kIcon100x100);
145 notification_data.badge = RegisterMockedURL(kIcon48x48);
146 notification_data.actions =
147 blink::WebVector<blink::WebNotificationAction>(static_cast<size_t>(2));
148 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110);
149 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120);
150
151 tracker()->FetchResources(
152 notification_data, nullptr /* delegate */,
153 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
154 base::Unretained(this), 0 /* index */));
155
156 ASSERT_EQ(1u, CountPendingNotifications());
157 ASSERT_EQ(0u, CountResources());
158
159 base::RunLoop().RunUntilIdle();
160 GetURLLoaderMockFactory()->serveAsynchronousRequests();
161
162 ASSERT_EQ(0u, CountPendingNotifications());
163 ASSERT_EQ(1u, CountResources());
164
165 NotificationResources* resources = GetResources(0u);
166
167 ASSERT_FALSE(resources->notification_icon.drawsNothing());
168 ASSERT_EQ(100, resources->notification_icon.width());
169
170 ASSERT_FALSE(resources->badge.drawsNothing());
171 ASSERT_EQ(48, resources->badge.width());
172
173 ASSERT_EQ(2u, resources->action_icons.size());
174 ASSERT_FALSE(resources->action_icons[0].drawsNothing());
175 ASSERT_EQ(110, resources->action_icons[0].width());
176 ASSERT_FALSE(resources->action_icons[1].drawsNothing());
177 ASSERT_EQ(120, resources->action_icons[1].width());
178 }
179
180 TEST_F(PendingNotificationsTrackerTest, LargeIconsAreScaledDown) {
181 blink::WebNotificationData notification_data;
182 notification_data.icon = RegisterMockedURL(kIcon500x500);
183 notification_data.badge = notification_data.icon;
184 notification_data.actions =
185 blink::WebVector<blink::WebNotificationAction>(static_cast<size_t>(1));
186 notification_data.actions[0].icon = notification_data.icon;
187
188 tracker()->FetchResources(
189 notification_data, nullptr /* delegate */,
190 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
191 base::Unretained(this), 0 /* index */));
192
193 ASSERT_EQ(1u, CountPendingNotifications());
194 ASSERT_EQ(0u, CountResources());
195
196 base::RunLoop().RunUntilIdle();
197 GetURLLoaderMockFactory()->serveAsynchronousRequests();
198
199 ASSERT_EQ(0u, CountPendingNotifications());
200 ASSERT_EQ(1u, CountResources());
201
202 NotificationResources* resources = GetResources(0u);
203
204 ASSERT_FALSE(resources->notification_icon.drawsNothing());
205 ASSERT_EQ(kPlatformNotificationMaxIconSizePx,
206 resources->notification_icon.width());
207 ASSERT_EQ(kPlatformNotificationMaxIconSizePx,
208 resources->notification_icon.height());
209
210 ASSERT_FALSE(resources->badge.drawsNothing());
211 ASSERT_EQ(kPlatformNotificationMaxBadgeSizePx, resources->badge.width());
212 ASSERT_EQ(kPlatformNotificationMaxBadgeSizePx, resources->badge.height());
213
214 ASSERT_EQ(1u, resources->action_icons.size());
215 ASSERT_FALSE(resources->action_icons[0].drawsNothing());
216 ASSERT_EQ(kPlatformNotificationMaxActionIconSizePx,
217 resources->action_icons[0].width());
218 ASSERT_EQ(kPlatformNotificationMaxActionIconSizePx,
219 resources->action_icons[0].height());
220 }
221
222 TEST_F(PendingNotificationsTrackerTest, TwoNotifications) {
223 blink::WebNotificationData notification_data;
224 notification_data.icon = RegisterMockedURL(kIcon100x100);
225
226 blink::WebNotificationData notification_data_2;
227 notification_data_2.icon = RegisterMockedURL(kIcon110x110);
228
229 tracker()->FetchResources(
230 notification_data, nullptr /* delegate */,
231 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
232 base::Unretained(this), 0 /* index */));
233
234 tracker()->FetchResources(
235 notification_data_2, nullptr /* delegate */,
236 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
237 base::Unretained(this), 1 /* index */));
238
239 ASSERT_EQ(2u, CountPendingNotifications());
240 ASSERT_EQ(0u, CountResources());
241
242 base::RunLoop().RunUntilIdle();
243 GetURLLoaderMockFactory()->serveAsynchronousRequests();
244
245 ASSERT_EQ(0u, CountPendingNotifications());
246 ASSERT_EQ(2u, CountResources());
247
248 ASSERT_FALSE(GetResources(0u)->notification_icon.drawsNothing());
249 ASSERT_EQ(100, GetResources(0u)->notification_icon.width());
250
251 ASSERT_FALSE(GetResources(1u)->notification_icon.drawsNothing());
252 ASSERT_EQ(110, GetResources(1u)->notification_icon.width());
253 }
254
255 TEST_F(PendingNotificationsTrackerTest, FetchError) {
256 blink::WebNotificationData notification_data;
257 notification_data.icon = RegisterMockedErrorURL(kIcon100x100);
258
259 tracker()->FetchResources(
260 notification_data, nullptr /* delegate */,
261 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
262 base::Unretained(this), 0 /* index */));
263
264 ASSERT_EQ(1u, CountPendingNotifications());
265 ASSERT_EQ(0u, CountResources());
266
267 base::RunLoop().RunUntilIdle();
268 GetURLLoaderMockFactory()->serveAsynchronousRequests();
269
270 ASSERT_EQ(0u, CountPendingNotifications());
271 ASSERT_EQ(1u, CountResources());
272
273 ASSERT_TRUE(GetResources(0u)->notification_icon.drawsNothing());
274 }
275
276 TEST_F(PendingNotificationsTrackerTest, CancelFetches) {
277 blink::WebNotificationData notification_data;
278 notification_data.icon = RegisterMockedURL(kIcon100x100);
279
280 FakeNotificationDelegate delegate;
281
282 tracker()->FetchResources(
283 notification_data, &delegate,
284 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
285 base::Unretained(this), 0 /* index */));
286
287 ASSERT_EQ(1u, CountPendingNotifications());
288 ASSERT_EQ(1u, CountDelegates());
289 ASSERT_EQ(0u, CountResources());
290
291 base::RunLoop().RunUntilIdle();
292 tracker()->CancelResourceFetches(&delegate);
293
294 ASSERT_EQ(0u, CountPendingNotifications());
295 ASSERT_EQ(0u, CountDelegates());
296 ASSERT_EQ(0u, CountResources());
297 }
298
299 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698