OLD | NEW |
---|---|
(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 const char kTestDataPath[] = "content/test/data/notifications/"; | |
Peter Beverloo
2016/02/05 15:43:21
micro nit: blank line above here
Michael van Ouwerkerk
2016/02/08 14:38:53
Done.
| |
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 NotificationResources* GetResources(size_t index) { | |
74 return &resources_[index]; | |
Peter Beverloo
2016/02/05 15:43:21
nit: DCHECK_LT(index, resources_.size())?
Michael van Ouwerkerk
2016/02/08 14:38:53
Done.
| |
75 } | |
76 | |
77 size_t CountPendingNotifications() { | |
78 return tracker_->pending_notifications_.size(); | |
79 } | |
80 | |
81 size_t CountDelegates() { | |
82 return tracker_->delegate_to_pending_id_map_.size(); | |
83 } | |
84 | |
85 void runLoop() { | |
Peter Beverloo
2016/02/05 15:43:21
nit: RunLoop
Why not use base::RunLoop().RunUntil
Michael van Ouwerkerk
2016/02/08 14:38:53
Because of the warning in the documentation for Ru
| |
86 base::RunLoop run_loop; | |
87 base::MessageLoop::current()->PostTask(FROM_HERE, run_loop.QuitClosure()); | |
88 run_loop.Run(); | |
89 } | |
90 | |
91 blink::WebUnitTestSupport* unitTestSupport() { | |
Peter Beverloo
2016/02/05 15:43:21
nit: UnitTestSupport
Michael van Ouwerkerk
2016/02/08 14:38:53
Done.
| |
92 return blink::Platform::current()->unitTestSupport(); | |
93 } | |
94 | |
95 // Registers a mocked url. When fetched, |file_name| will be loaded from the | |
96 // test data directory. | |
97 blink::WebURL RegisterMockedURL(const std::string& file_name) { | |
98 blink::WebURL url(GURL(kBaseUrl + file_name)); | |
99 | |
100 blink::WebURLResponse response(url); | |
101 response.setMIMEType("image/png"); | |
102 response.setHTTPStatusCode(200); | |
103 | |
104 base::FilePath file_path; | |
105 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | |
106 file_path = file_path.Append(FILE_PATH_LITERAL(kTestDataPath + file_name)); | |
Peter Beverloo
2016/02/05 15:43:21
I think you can fix the build issues by using the
Michael van Ouwerkerk
2016/02/08 14:38:53
Done.
| |
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()->FetchNotificationResources( | |
148 notification_data, nullptr /* delegate */, | |
149 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | |
150 base::Unretained(this), 0)); | |
Peter Beverloo
2016/02/05 15:43:21
nit: 0 /* index */
dito elsewhere in this file
Michael van Ouwerkerk
2016/02/08 14:38:53
Done.
| |
151 | |
152 ASSERT_EQ(1u, CountPendingNotifications()); | |
153 ASSERT_EQ(0u, CountResources()); | |
154 | |
155 runLoop(); | |
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()->FetchNotificationResources( | |
179 notification_data, nullptr /* delegate */, | |
180 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | |
181 base::Unretained(this), 0)); | |
182 | |
183 tracker()->FetchNotificationResources( | |
184 notification_data_2, nullptr /* delegate */, | |
185 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | |
186 base::Unretained(this), 1)); | |
187 | |
188 ASSERT_EQ(2u, CountPendingNotifications()); | |
189 ASSERT_EQ(0u, CountResources()); | |
190 | |
191 runLoop(); | |
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()->FetchNotificationResources( | |
209 notification_data, nullptr /* delegate */, | |
210 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | |
211 base::Unretained(this), 0)); | |
212 | |
213 ASSERT_EQ(1u, CountPendingNotifications()); | |
214 ASSERT_EQ(0u, CountResources()); | |
215 | |
216 runLoop(); | |
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()->FetchNotificationResources( | |
232 notification_data, &delegate, | |
233 base::Bind(&PendingNotificationsTrackerTest::DidFetchResources, | |
234 base::Unretained(this), 0)); | |
235 | |
236 ASSERT_EQ(1u, CountPendingNotifications()); | |
237 ASSERT_EQ(1u, CountDelegates()); | |
238 ASSERT_EQ(0u, CountResources()); | |
239 | |
240 runLoop(); | |
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 | |
OLD | NEW |