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

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

Issue 1644083002: Fetch notification action icons and pass them through in resources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ActionIconBlink
Patch Set: Rebase. Created 4 years, 10 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 <vector>
8
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/location.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.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/public/common/notification_resources.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/WebKit/public/platform/Platform.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/platform/WebURL.h"
26 #include "third_party/WebKit/public/platform/WebURLError.h"
27 #include "third_party/WebKit/public/platform/WebURLResponse.h"
28 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h"
29 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onData.h"
30 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onDelegate.h"
31 #include "third_party/skia/include/core/SkBitmap.h"
32 #include "url/gurl.h"
33
34 namespace content {
35
36 namespace {
37
38 const char kBaseUrl[] = "http://test.com/";
39 const char kIcon100x100[] = "100x100.png";
40 const char kIcon110x110[] = "110x110.png";
41 const char kIcon120x120[] = "120x120.png";
42
43 class FakeNotificationDelegate : public blink::WebNotificationDelegate {
44 public:
45 void dispatchClickEvent() override {}
46 void dispatchShowEvent() override {}
47 void dispatchErrorEvent() override {}
48 void dispatchCloseEvent() override {}
49 };
50
51 } // namespace
52
53 class PendingNotificationsTrackerTest : public testing::Test {
54 public:
55 PendingNotificationsTrackerTest()
56 : tracker_(new PendingNotificationsTracker(
57 base::ThreadTaskRunnerHandle::Get())) {}
58
59 ~PendingNotificationsTrackerTest() override {
60 UnitTestSupport()->unregisterAllMockedURLs();
61 }
62
63 void DidFetchResources(size_t index, const NotificationResources& resources) {
64 if (resources_.size() < index + 1)
65 resources_.resize(index + 1);
66 resources_[index] = resources;
67 }
68
69 protected:
70 PendingNotificationsTracker* tracker() { return tracker_.get(); }
71
72 size_t CountResources() { return resources_.size(); }
73
74 NotificationResources* GetResources(size_t index) {
75 DCHECK_LT(index, resources_.size());
76 return &resources_[index];
77 }
78
79 size_t CountPendingNotifications() {
80 return tracker_->pending_notifications_.size();
81 }
82
83 size_t CountDelegates() {
84 return tracker_->delegate_to_pending_id_map_.size();
85 }
86
87 blink::WebUnitTestSupport* UnitTestSupport() {
88 return blink::Platform::current()->unitTestSupport();
89 }
90
91 // Registers a mocked url. When fetched, |file_name| will be loaded from the
92 // test data directory.
93 blink::WebURL RegisterMockedURL(const std::string& file_name) {
94 blink::WebURL url(GURL(kBaseUrl + file_name));
95
96 blink::WebURLResponse response(url);
97 response.setMIMEType("image/png");
98 response.setHTTPStatusCode(200);
99
100 base::FilePath file_path;
101 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
102 file_path = file_path.Append(FILE_PATH_LITERAL("content"))
103 .Append(FILE_PATH_LITERAL("test"))
104 .Append(FILE_PATH_LITERAL("data"))
105 .Append(FILE_PATH_LITERAL("notifications"))
106 .AppendASCII(file_name);
107 file_path = base::MakeAbsoluteFilePath(file_path);
108 blink::WebString string_file_path =
109 blink::WebString::fromUTF8(file_path.MaybeAsASCII());
110
111 UnitTestSupport()->registerMockedURL(url, response, string_file_path);
112
113 return url;
114 }
115
116 // Registers a mocked url that will fail to be fetched, with a 404 error.
117 blink::WebURL RegisterMockedErrorURL(const std::string& file_name) {
118 blink::WebURL url(GURL(kBaseUrl + file_name));
119
120 blink::WebURLResponse response(url);
121 response.setMIMEType("image/png");
122 response.setHTTPStatusCode(404);
123
124 blink::WebURLError error;
125 error.reason = 404;
126
127 UnitTestSupport()->registerMockedErrorURL(url, response, error);
128 return url;
129 }
130
131 private:
132 base::MessageLoop message_loop_;
133 scoped_ptr<PendingNotificationsTracker> tracker_;
134 std::vector<NotificationResources> resources_;
135
136 DISALLOW_COPY_AND_ASSIGN(PendingNotificationsTrackerTest);
137 };
138
139 TEST_F(PendingNotificationsTrackerTest, OneNotificationMultipleResources) {
140 blink::WebNotificationData notification_data;
141 notification_data.icon = RegisterMockedURL(kIcon100x100);
142 notification_data.actions =
143 blink::WebVector<blink::WebNotificationAction>(static_cast<size_t>(2));
144 notification_data.actions[0].icon = RegisterMockedURL(kIcon110x110);
145 notification_data.actions[1].icon = RegisterMockedURL(kIcon120x120);
146
147 tracker()->FetchResources(
148 notification_data, nullptr /* delegate */,
149 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
150 base::Unretained(this), 0 /* index */));
151
152 ASSERT_EQ(1u, CountPendingNotifications());
153 ASSERT_EQ(0u, CountResources());
154
155 base::RunLoop().RunUntilIdle();
156 UnitTestSupport()->serveAsynchronousMockedRequests();
157
158 ASSERT_EQ(0u, CountPendingNotifications());
159 ASSERT_EQ(1u, CountResources());
160
161 ASSERT_FALSE(GetResources(0u)->notification_icon.drawsNothing());
162 ASSERT_EQ(100, GetResources(0u)->notification_icon.width());
163
164 ASSERT_EQ(2u, GetResources(0u)->action_icons.size());
165 ASSERT_FALSE(GetResources(0u)->action_icons[0].drawsNothing());
166 ASSERT_EQ(110, GetResources(0u)->action_icons[0].width());
167 ASSERT_FALSE(GetResources(0u)->action_icons[1].drawsNothing());
168 ASSERT_EQ(120, GetResources(0u)->action_icons[1].width());
169 }
170
171 TEST_F(PendingNotificationsTrackerTest, TwoNotifications) {
172 blink::WebNotificationData notification_data;
173 notification_data.icon = RegisterMockedURL(kIcon100x100);
174
175 blink::WebNotificationData notification_data_2;
176 notification_data_2.icon = RegisterMockedURL(kIcon110x110);
177
178 tracker()->FetchResources(
179 notification_data, nullptr /* delegate */,
180 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
181 base::Unretained(this), 0 /* index */));
182
183 tracker()->FetchResources(
184 notification_data_2, nullptr /* delegate */,
185 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
186 base::Unretained(this), 1 /* index */));
187
188 ASSERT_EQ(2u, CountPendingNotifications());
189 ASSERT_EQ(0u, CountResources());
190
191 base::RunLoop().RunUntilIdle();
192 UnitTestSupport()->serveAsynchronousMockedRequests();
193
194 ASSERT_EQ(0u, CountPendingNotifications());
195 ASSERT_EQ(2u, CountResources());
196
197 ASSERT_FALSE(GetResources(0u)->notification_icon.drawsNothing());
198 ASSERT_EQ(100, GetResources(0u)->notification_icon.width());
199
200 ASSERT_FALSE(GetResources(1u)->notification_icon.drawsNothing());
201 ASSERT_EQ(110, GetResources(1u)->notification_icon.width());
202 }
203
204 TEST_F(PendingNotificationsTrackerTest, FetchError) {
205 blink::WebNotificationData notification_data;
206 notification_data.icon = RegisterMockedErrorURL(kIcon100x100);
207
208 tracker()->FetchResources(
209 notification_data, nullptr /* delegate */,
210 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
211 base::Unretained(this), 0 /* index */));
212
213 ASSERT_EQ(1u, CountPendingNotifications());
214 ASSERT_EQ(0u, CountResources());
215
216 base::RunLoop().RunUntilIdle();
217 UnitTestSupport()->serveAsynchronousMockedRequests();
218
219 ASSERT_EQ(0u, CountPendingNotifications());
220 ASSERT_EQ(1u, CountResources());
221
222 ASSERT_TRUE(GetResources(0u)->notification_icon.drawsNothing());
223 }
224
225 TEST_F(PendingNotificationsTrackerTest, CancelFetches) {
226 blink::WebNotificationData notification_data;
227 notification_data.icon = RegisterMockedURL(kIcon100x100);
228
229 FakeNotificationDelegate delegate;
230
231 tracker()->FetchResources(
232 notification_data, &delegate,
233 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources,
234 base::Unretained(this), 0 /* index */));
235
236 ASSERT_EQ(1u, CountPendingNotifications());
237 ASSERT_EQ(1u, CountDelegates());
238 ASSERT_EQ(0u, CountResources());
239
240 base::RunLoop().RunUntilIdle();
241 tracker()->CancelResourceFetches(&delegate);
242
243 ASSERT_EQ(0u, CountPendingNotifications());
244 ASSERT_EQ(0u, CountDelegates());
245 ASSERT_EQ(0u, CountResources());
246 }
247
248 } // namespace content
OLDNEW
« no previous file with comments | « content/child/notifications/pending_notifications_tracker.cc ('k') | content/common/platform_notification_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698