| OLD | NEW |
| (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/optional.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/test/histogram_tester.h" |
| 17 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 19 #include "content/common/service_worker/service_worker_status_code.h" |
| 20 #include "content/common/service_worker/service_worker_types.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace content { |
| 25 namespace { |
| 26 |
| 27 const char kExampleOrigin[] = "https://example.com/"; |
| 28 const char kExampleScriptUrl[] = "https://example.com/sw.js"; |
| 29 const char kExampleTag[] = "my-tag"; |
| 30 const char kExampleTag2[] = "my-second-tag"; |
| 31 |
| 32 // Extension of the EmbeddedWorkerTestHelper that enables instrumentation of the |
| 33 // events related to the Background Fetch API. Storage for these tests will |
| 34 // always be kept in memory, as data persistence is tested elsewhere. |
| 35 class BackgroundFetchEmbeddedWorkerTestHelper |
| 36 : public EmbeddedWorkerTestHelper { |
| 37 public: |
| 38 BackgroundFetchEmbeddedWorkerTestHelper() |
| 39 : EmbeddedWorkerTestHelper(base::FilePath() /* in memory */) {} |
| 40 ~BackgroundFetchEmbeddedWorkerTestHelper() override = default; |
| 41 |
| 42 void set_fail_abort_event(bool fail) { fail_abort_event_ = fail; } |
| 43 |
| 44 void set_fail_click_event(bool fail) { fail_click_event_ = fail; } |
| 45 |
| 46 const base::Optional<std::string>& last_tag() const { return last_tag_; } |
| 47 const base::Optional<mojom::BackgroundFetchState>& last_state() const { |
| 48 return last_state_; |
| 49 } |
| 50 |
| 51 protected: |
| 52 void OnBackgroundFetchAbortEvent( |
| 53 const std::string& tag, |
| 54 const mojom::ServiceWorkerEventDispatcher:: |
| 55 DispatchBackgroundFetchAbortEventCallback& callback) override { |
| 56 last_tag_ = tag; |
| 57 |
| 58 if (fail_abort_event_) { |
| 59 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, |
| 60 base::Time::Now()); |
| 61 } else { |
| 62 callback.Run(SERVICE_WORKER_OK, base::Time::Now()); |
| 63 } |
| 64 } |
| 65 |
| 66 void OnBackgroundFetchClickEvent( |
| 67 const std::string& tag, |
| 68 mojom::BackgroundFetchState state, |
| 69 const mojom::ServiceWorkerEventDispatcher:: |
| 70 DispatchBackgroundFetchClickEventCallback& callback) override { |
| 71 last_tag_ = tag; |
| 72 last_state_ = state; |
| 73 |
| 74 if (fail_click_event_) { |
| 75 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, |
| 76 base::Time::Now()); |
| 77 } else { |
| 78 callback.Run(SERVICE_WORKER_OK, base::Time::Now()); |
| 79 } |
| 80 } |
| 81 |
| 82 private: |
| 83 bool fail_abort_event_ = false; |
| 84 bool fail_click_event_ = false; |
| 85 |
| 86 base::Optional<std::string> last_tag_; |
| 87 base::Optional<mojom::BackgroundFetchState> last_state_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper); |
| 90 }; |
| 91 |
| 92 class BackgroundFetchEventDispatcherTest : public ::testing::Test { |
| 93 public: |
| 94 BackgroundFetchEventDispatcherTest() |
| 95 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 96 event_dispatcher_(embedded_worker_test_helper_.context_wrapper()) {} |
| 97 |
| 98 // Creates a new Service Worker registration for a fake origin and scope and |
| 99 // returns the ServiceWorkerRegistration instance associated with it. |
| 100 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker() { |
| 101 GURL origin(kExampleOrigin); |
| 102 GURL script_url(kExampleScriptUrl); |
| 103 |
| 104 int64_t service_worker_registration_id = |
| 105 kInvalidServiceWorkerRegistrationId; |
| 106 |
| 107 { |
| 108 base::RunLoop run_loop; |
| 109 embedded_worker_test_helper_.context()->RegisterServiceWorker( |
| 110 origin, script_url, nullptr /* provider_host */, |
| 111 base::Bind( |
| 112 &BackgroundFetchEventDispatcherTest::DidRegisterServiceWorker, |
| 113 base::Unretained(this), &service_worker_registration_id, |
| 114 run_loop.QuitClosure())); |
| 115 |
| 116 run_loop.Run(); |
| 117 } |
| 118 |
| 119 if (service_worker_registration_id == kInvalidServiceWorkerRegistrationId) { |
| 120 ADD_FAILURE() << "Could not obtain a valid Service Worker registration"; |
| 121 return nullptr; |
| 122 } |
| 123 |
| 124 scoped_refptr<ServiceWorkerRegistration> service_worker_registration; |
| 125 |
| 126 { |
| 127 base::RunLoop run_loop; |
| 128 embedded_worker_test_helper_.context()->storage()->FindRegistrationForId( |
| 129 service_worker_registration_id, origin, |
| 130 base::Bind(&BackgroundFetchEventDispatcherTest:: |
| 131 DidFindServiceWorkerRegistration, |
| 132 base::Unretained(this), &service_worker_registration, |
| 133 run_loop.QuitClosure())); |
| 134 |
| 135 run_loop.Run(); |
| 136 } |
| 137 |
| 138 // Wait for the worker to be activated. |
| 139 base::RunLoop().RunUntilIdle(); |
| 140 |
| 141 if (!service_worker_registration) { |
| 142 ADD_FAILURE() << "Could not find the new Service Worker registration."; |
| 143 return nullptr; |
| 144 } |
| 145 |
| 146 return service_worker_registration; |
| 147 } |
| 148 |
| 149 BackgroundFetchEmbeddedWorkerTestHelper* test_helpers() { |
| 150 return &embedded_worker_test_helper_; |
| 151 } |
| 152 |
| 153 BackgroundFetchEventDispatcher* dispatcher() { return &event_dispatcher_; } |
| 154 |
| 155 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
| 156 |
| 157 private: |
| 158 void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id, |
| 159 base::Closure quit_closure, |
| 160 ServiceWorkerStatusCode status, |
| 161 const std::string& status_message, |
| 162 int64_t service_worker_registration_id) { |
| 163 DCHECK(out_service_worker_registration_id); |
| 164 EXPECT_EQ(SERVICE_WORKER_OK, status) << status_message; |
| 165 |
| 166 *out_service_worker_registration_id = service_worker_registration_id; |
| 167 |
| 168 quit_closure.Run(); |
| 169 } |
| 170 |
| 171 void DidFindServiceWorkerRegistration( |
| 172 scoped_refptr<ServiceWorkerRegistration>* out_service_worker_registration, |
| 173 base::Closure quit_closure, |
| 174 ServiceWorkerStatusCode status, |
| 175 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| 176 DCHECK(out_service_worker_registration); |
| 177 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 178 |
| 179 *out_service_worker_registration = service_worker_registration; |
| 180 |
| 181 quit_closure.Run(); |
| 182 } |
| 183 |
| 184 TestBrowserThreadBundle thread_bundle_; |
| 185 |
| 186 BackgroundFetchEmbeddedWorkerTestHelper embedded_worker_test_helper_; |
| 187 BackgroundFetchEventDispatcher event_dispatcher_; |
| 188 |
| 189 base::HistogramTester histogram_tester_; |
| 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcherTest); |
| 192 }; |
| 193 |
| 194 TEST_F(BackgroundFetchEventDispatcherTest, DispatchInvalidRegistration) { |
| 195 GURL origin(kExampleOrigin); |
| 196 |
| 197 base::RunLoop run_loop; |
| 198 dispatcher()->DispatchBackgroundFetchAbortEvent(9042 /* random invalid Id */, |
| 199 origin, kExampleTag, |
| 200 run_loop.QuitClosure()); |
| 201 |
| 202 run_loop.Run(); |
| 203 |
| 204 histogram_tester()->ExpectBucketCount( |
| 205 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 206 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_FIND_WORKER, 1); |
| 207 histogram_tester()->ExpectBucketCount( |
| 208 "BackgroundFetch.EventDispatchFailure.FindWorker.AbortEvent", |
| 209 SERVICE_WORKER_ERROR_NOT_FOUND, 1); |
| 210 } |
| 211 |
| 212 TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) { |
| 213 auto service_worker_registration = RegisterServiceWorker(); |
| 214 ASSERT_TRUE(service_worker_registration); |
| 215 ASSERT_TRUE(service_worker_registration->active_version()); |
| 216 |
| 217 GURL origin(kExampleOrigin); |
| 218 |
| 219 { |
| 220 base::RunLoop run_loop; |
| 221 dispatcher()->DispatchBackgroundFetchAbortEvent( |
| 222 service_worker_registration->id(), origin, kExampleTag, |
| 223 run_loop.QuitClosure()); |
| 224 |
| 225 run_loop.Run(); |
| 226 } |
| 227 |
| 228 ASSERT_TRUE(test_helpers()->last_tag().has_value()); |
| 229 EXPECT_EQ(kExampleTag, test_helpers()->last_tag().value()); |
| 230 |
| 231 histogram_tester()->ExpectUniqueSample( |
| 232 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 233 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 234 |
| 235 test_helpers()->set_fail_abort_event(true); |
| 236 |
| 237 { |
| 238 base::RunLoop run_loop; |
| 239 dispatcher()->DispatchBackgroundFetchAbortEvent( |
| 240 service_worker_registration->id(), origin, kExampleTag2, |
| 241 run_loop.QuitClosure()); |
| 242 |
| 243 run_loop.Run(); |
| 244 } |
| 245 |
| 246 ASSERT_TRUE(test_helpers()->last_tag().has_value()); |
| 247 EXPECT_EQ(kExampleTag2, test_helpers()->last_tag().value()); |
| 248 |
| 249 histogram_tester()->ExpectBucketCount( |
| 250 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 251 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 252 histogram_tester()->ExpectBucketCount( |
| 253 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 254 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1); |
| 255 histogram_tester()->ExpectUniqueSample( |
| 256 "BackgroundFetch.EventDispatchFailure.Dispatch.AbortEvent", |
| 257 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1); |
| 258 } |
| 259 |
| 260 TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) { |
| 261 auto service_worker_registration = RegisterServiceWorker(); |
| 262 ASSERT_TRUE(service_worker_registration); |
| 263 ASSERT_TRUE(service_worker_registration->active_version()); |
| 264 |
| 265 GURL origin(kExampleOrigin); |
| 266 |
| 267 { |
| 268 base::RunLoop run_loop; |
| 269 dispatcher()->DispatchBackgroundFetchClickEvent( |
| 270 service_worker_registration->id(), origin, kExampleTag, |
| 271 mojom::BackgroundFetchState::PENDING, run_loop.QuitClosure()); |
| 272 |
| 273 run_loop.Run(); |
| 274 } |
| 275 |
| 276 ASSERT_TRUE(test_helpers()->last_tag().has_value()); |
| 277 EXPECT_EQ(kExampleTag, test_helpers()->last_tag().value()); |
| 278 |
| 279 ASSERT_TRUE(test_helpers()->last_state().has_value()); |
| 280 EXPECT_EQ(mojom::BackgroundFetchState::PENDING, test_helpers()->last_state()); |
| 281 |
| 282 histogram_tester()->ExpectUniqueSample( |
| 283 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 284 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 285 |
| 286 test_helpers()->set_fail_click_event(true); |
| 287 |
| 288 { |
| 289 base::RunLoop run_loop; |
| 290 dispatcher()->DispatchBackgroundFetchClickEvent( |
| 291 service_worker_registration->id(), origin, kExampleTag2, |
| 292 mojom::BackgroundFetchState::SUCCEEDED, run_loop.QuitClosure()); |
| 293 |
| 294 run_loop.Run(); |
| 295 } |
| 296 |
| 297 ASSERT_TRUE(test_helpers()->last_tag().has_value()); |
| 298 EXPECT_EQ(kExampleTag2, test_helpers()->last_tag().value()); |
| 299 |
| 300 ASSERT_TRUE(test_helpers()->last_state().has_value()); |
| 301 EXPECT_EQ(mojom::BackgroundFetchState::SUCCEEDED, |
| 302 test_helpers()->last_state()); |
| 303 |
| 304 histogram_tester()->ExpectBucketCount( |
| 305 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 306 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 307 histogram_tester()->ExpectBucketCount( |
| 308 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 309 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1); |
| 310 histogram_tester()->ExpectUniqueSample( |
| 311 "BackgroundFetch.EventDispatchFailure.Dispatch.ClickEvent", |
| 312 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1); |
| 313 } |
| 314 |
| 315 } // namespace |
| 316 } // namespace content |
| OLD | NEW |