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

Side by Side Diff: content/browser/background_fetch/background_fetch_event_dispatcher_unittest.cc

Issue 2748213003: Service Worker event dispatcher for Background Fetch (Closed)
Patch Set: Service Worker event dispatcher for Background Fetch Created 3 years, 9 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 2017 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/browser/background_fetch/background_fetch_event_dispatcher.h"
6
7 #include <stdint.h>
8 #include <memory>
9
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/run_loop.h"
15 #include "base/test/histogram_tester.h"
16 #include "content/browser/service_worker/embedded_worker_test_helper.h"
17 #include "content/browser/service_worker/service_worker_context_wrapper.h"
18 #include "content/common/service_worker/service_worker_status_code.h"
19 #include "content/common/service_worker/service_worker_types.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace content {
24 namespace {
25
26 const char kExampleOrigin[] = "https://example.com/";
27 const char kExampleScriptUrl[] = "https://example.com/sw.js";
28 const char kExampleTag[] = "my-tag";
29
30 // Extension of the EmbeddedWorkerTestHelper that enables instrumentation of the
31 // events related to the Background Fetch API. Storage for these tests will
32 // always be kept in memory, as data persistence is tested elsewhere.
33 class BackgroundFetchEmbeddedWorkerTestHelper
34 : public EmbeddedWorkerTestHelper {
35 public:
36 BackgroundFetchEmbeddedWorkerTestHelper()
37 : EmbeddedWorkerTestHelper(base::FilePath() /* in memory */) {}
38 ~BackgroundFetchEmbeddedWorkerTestHelper() override = default;
39
40 void set_fail_abort_event(bool fail) { fail_abort_event_ = fail; }
41
42 void set_fail_click_event(bool fail) { fail_click_event_ = fail; }
43
44 protected:
45 void OnBackgroundFetchAbortEvent(
46 const std::string& tag,
47 const mojom::ServiceWorkerEventDispatcher::
48 DispatchBackgroundFetchAbortEventCallback& callback) override {
49 if (fail_abort_event_) {
50 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
51 base::Time::Now());
52 } else {
53 callback.Run(SERVICE_WORKER_OK, base::Time::Now());
54 }
55 }
56
57 void OnBackgroundFetchClickEvent(
58 const std::string& tag,
59 mojom::BackgroundFetchState state,
60 const mojom::ServiceWorkerEventDispatcher::
61 DispatchBackgroundFetchClickEventCallback& callback) override {
62 if (fail_click_event_) {
63 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
64 base::Time::Now());
65 } else {
66 callback.Run(SERVICE_WORKER_OK, base::Time::Now());
67 }
68 }
69
70 private:
71 bool fail_abort_event_ = false;
72 bool fail_click_event_ = false;
73
74 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper);
75 };
76
77 class BackgroundFetchEventDispatcherTest : public ::testing::Test {
78 public:
79 BackgroundFetchEventDispatcherTest()
80 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
81 event_dispatcher_(embedded_worker_test_helper_.context_wrapper()) {}
82
83 // Creates a new Service Worker registration for a fake origin and scope and
84 // returns the ServiceWorkerRegistration instance associated with it.
85 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker() {
86 GURL origin(kExampleOrigin);
87 GURL script_url(kExampleScriptUrl);
88
89 int64_t service_worker_registration_id =
90 kInvalidServiceWorkerRegistrationId;
91
92 {
93 base::RunLoop run_loop;
94 embedded_worker_test_helper_.context()->RegisterServiceWorker(
95 origin, script_url, nullptr /* provider_host */,
96 base::Bind(
97 &BackgroundFetchEventDispatcherTest::DidRegisterServiceWorker,
98 base::Unretained(this), &service_worker_registration_id,
99 run_loop.QuitClosure()));
100
101 run_loop.Run();
102 }
103
104 if (service_worker_registration_id == kInvalidServiceWorkerRegistrationId) {
105 ADD_FAILURE() << "Could not obtain a valid Service Worker registration";
106 return nullptr;
107 }
108
109 scoped_refptr<ServiceWorkerRegistration> service_worker_registration;
110
111 {
horo 2017/03/16 07:08:22 nit:You don't need the brackets. Or {
Peter Beverloo 2017/03/16 16:05:58 Amended. You found the comment I was looking for :
112 embedded_worker_test_helper_.context()->storage()->FindRegistrationForId(
113 service_worker_registration_id, origin,
114 base::Bind(&BackgroundFetchEventDispatcherTest::
115 DidFindServiceWorkerRegistration,
116 base::Unretained(this), &service_worker_registration));
117
118 base::RunLoop().RunUntilIdle();
119 }
120
121 if (!service_worker_registration) {
122 ADD_FAILURE() << "Could not find the new Service Worker registration.";
123 return nullptr;
124 }
125
126 return service_worker_registration;
127 }
128
129 BackgroundFetchEmbeddedWorkerTestHelper* test_helpers() {
130 return &embedded_worker_test_helper_;
131 }
132
133 BackgroundFetchEventDispatcher* dispatcher() { return &event_dispatcher_; }
134
135 base::HistogramTester* histogram_tester() { return &histogram_tester_; }
136
137 private:
138 void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id,
139 base::Closure quit_closure,
140 ServiceWorkerStatusCode status,
141 const std::string& status_message,
142 int64_t service_worker_registration_id) {
143 DCHECK(out_service_worker_registration_id);
144 EXPECT_EQ(SERVICE_WORKER_OK, status) << status_message;
145
146 *out_service_worker_registration_id = service_worker_registration_id;
147
148 quit_closure.Run();
149 }
150
151 void DidFindServiceWorkerRegistration(
152 scoped_refptr<ServiceWorkerRegistration>* out_service_worker_registration,
153 ServiceWorkerStatusCode status,
154 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {
155 DCHECK(out_service_worker_registration);
156 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
157
158 *out_service_worker_registration = service_worker_registration;
159 }
160
161 TestBrowserThreadBundle thread_bundle_;
162
163 BackgroundFetchEmbeddedWorkerTestHelper embedded_worker_test_helper_;
164 BackgroundFetchEventDispatcher event_dispatcher_;
165
166 base::HistogramTester histogram_tester_;
167
168 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcherTest);
169 };
170
171 TEST_F(BackgroundFetchEventDispatcherTest, DispatchInvalidRegistration) {
172 {
harkness 2017/03/16 17:42:18 micro-nit: don't need the {}
Peter Beverloo 2017/03/17 13:43:28 Done.
173 base::RunLoop run_loop;
174 dispatcher()->DispatchBackgroundFetchAbortEvent(
175 9042 /* random invalid Id */, kExampleTag, run_loop.QuitClosure());
176
177 run_loop.Run();
178 }
179
180 histogram_tester()->ExpectBucketCount(
181 "BackgroundFetch.EventDispatchResult.AbortEvent",
182 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_FIND_WORKER, 1);
183 histogram_tester()->ExpectBucketCount(
184 "BackgroundFetch.EventDispatchFailure.FindWorker.AbortEvent",
185 SERVICE_WORKER_ERROR_NOT_FOUND, 1);
186 }
187
188 TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) {
189 auto service_worker_registration = RegisterServiceWorker();
190 ASSERT_TRUE(service_worker_registration);
191 ASSERT_TRUE(service_worker_registration->active_version());
192
193 {
194 base::RunLoop run_loop;
195 dispatcher()->DispatchBackgroundFetchAbortEvent(
196 service_worker_registration->id(), kExampleTag, run_loop.QuitClosure());
197
198 run_loop.Run();
199 }
200
201 histogram_tester()->ExpectUniqueSample(
202 "BackgroundFetch.EventDispatchResult.AbortEvent",
203 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
204
205 test_helpers()->set_fail_abort_event(true);
206
207 {
208 base::RunLoop run_loop;
209 dispatcher()->DispatchBackgroundFetchAbortEvent(
210 service_worker_registration->id(), kExampleTag, run_loop.QuitClosure());
211
212 run_loop.Run();
213 }
214
215 histogram_tester()->ExpectBucketCount(
216 "BackgroundFetch.EventDispatchResult.AbortEvent",
217 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
218 histogram_tester()->ExpectBucketCount(
219 "BackgroundFetch.EventDispatchResult.AbortEvent",
220 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1);
221 histogram_tester()->ExpectUniqueSample(
222 "BackgroundFetch.EventDispatchFailure.Dispatch.AbortEvent",
223 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1);
224 }
225
226 TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) {
227 auto service_worker_registration = RegisterServiceWorker();
228 ASSERT_TRUE(service_worker_registration);
229 ASSERT_TRUE(service_worker_registration->active_version());
230
231 {
232 base::RunLoop run_loop;
233 dispatcher()->DispatchBackgroundFetchClickEvent(
234 service_worker_registration->id(), kExampleTag,
235 mojom::BackgroundFetchState::PENDING, run_loop.QuitClosure());
236
237 run_loop.Run();
238 }
239
240 histogram_tester()->ExpectUniqueSample(
241 "BackgroundFetch.EventDispatchResult.ClickEvent",
242 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
harkness 2017/03/16 17:42:18 Can you save the state in BackgroundFetchEmbeddedW
Peter Beverloo 2017/03/17 13:43:28 Done, also for the `tag`.
243
244 test_helpers()->set_fail_click_event(true);
245
246 {
247 base::RunLoop run_loop;
248 dispatcher()->DispatchBackgroundFetchClickEvent(
249 service_worker_registration->id(), kExampleTag,
250 mojom::BackgroundFetchState::PENDING, run_loop.QuitClosure());
251
252 run_loop.Run();
253 }
254
255 histogram_tester()->ExpectBucketCount(
256 "BackgroundFetch.EventDispatchResult.ClickEvent",
257 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
258 histogram_tester()->ExpectBucketCount(
259 "BackgroundFetch.EventDispatchResult.ClickEvent",
260 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1);
261 histogram_tester()->ExpectUniqueSample(
262 "BackgroundFetch.EventDispatchFailure.Dispatch.ClickEvent",
263 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1);
264 }
265
266 } // namespace
267 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698