| 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/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 { |
| 112 base::RunLoop run_loop; |
| 113 embedded_worker_test_helper_.context()->storage()->FindRegistrationForId( |
| 114 service_worker_registration_id, origin, |
| 115 base::Bind(&BackgroundFetchEventDispatcherTest:: |
| 116 DidFindServiceWorkerRegistration, |
| 117 base::Unretained(this), &service_worker_registration, |
| 118 run_loop.QuitClosure())); |
| 119 |
| 120 run_loop.Run(); |
| 121 } |
| 122 |
| 123 // Wait for the worker to be activated. |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 |
| 126 if (!service_worker_registration) { |
| 127 ADD_FAILURE() << "Could not find the new Service Worker registration."; |
| 128 return nullptr; |
| 129 } |
| 130 |
| 131 return service_worker_registration; |
| 132 } |
| 133 |
| 134 BackgroundFetchEmbeddedWorkerTestHelper* test_helpers() { |
| 135 return &embedded_worker_test_helper_; |
| 136 } |
| 137 |
| 138 BackgroundFetchEventDispatcher* dispatcher() { return &event_dispatcher_; } |
| 139 |
| 140 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
| 141 |
| 142 private: |
| 143 void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id, |
| 144 base::Closure quit_closure, |
| 145 ServiceWorkerStatusCode status, |
| 146 const std::string& status_message, |
| 147 int64_t service_worker_registration_id) { |
| 148 DCHECK(out_service_worker_registration_id); |
| 149 EXPECT_EQ(SERVICE_WORKER_OK, status) << status_message; |
| 150 |
| 151 *out_service_worker_registration_id = service_worker_registration_id; |
| 152 |
| 153 quit_closure.Run(); |
| 154 } |
| 155 |
| 156 void DidFindServiceWorkerRegistration( |
| 157 scoped_refptr<ServiceWorkerRegistration>* out_service_worker_registration, |
| 158 base::Closure quit_closure, |
| 159 ServiceWorkerStatusCode status, |
| 160 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| 161 DCHECK(out_service_worker_registration); |
| 162 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 163 |
| 164 *out_service_worker_registration = service_worker_registration; |
| 165 |
| 166 quit_closure.Run(); |
| 167 } |
| 168 |
| 169 TestBrowserThreadBundle thread_bundle_; |
| 170 |
| 171 BackgroundFetchEmbeddedWorkerTestHelper embedded_worker_test_helper_; |
| 172 BackgroundFetchEventDispatcher event_dispatcher_; |
| 173 |
| 174 base::HistogramTester histogram_tester_; |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcherTest); |
| 177 }; |
| 178 |
| 179 TEST_F(BackgroundFetchEventDispatcherTest, DispatchInvalidRegistration) { |
| 180 GURL origin(kExampleOrigin); |
| 181 |
| 182 { |
| 183 base::RunLoop run_loop; |
| 184 dispatcher()->DispatchBackgroundFetchAbortEvent( |
| 185 9042 /* random invalid Id */, origin, kExampleTag, |
| 186 run_loop.QuitClosure()); |
| 187 |
| 188 run_loop.Run(); |
| 189 } |
| 190 |
| 191 histogram_tester()->ExpectBucketCount( |
| 192 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 193 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_FIND_WORKER, 1); |
| 194 histogram_tester()->ExpectBucketCount( |
| 195 "BackgroundFetch.EventDispatchFailure.FindWorker.AbortEvent", |
| 196 SERVICE_WORKER_ERROR_NOT_FOUND, 1); |
| 197 } |
| 198 |
| 199 TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) { |
| 200 auto service_worker_registration = RegisterServiceWorker(); |
| 201 ASSERT_TRUE(service_worker_registration); |
| 202 ASSERT_TRUE(service_worker_registration->active_version()); |
| 203 |
| 204 GURL origin(kExampleOrigin); |
| 205 |
| 206 { |
| 207 base::RunLoop run_loop; |
| 208 dispatcher()->DispatchBackgroundFetchAbortEvent( |
| 209 service_worker_registration->id(), origin, kExampleTag, |
| 210 run_loop.QuitClosure()); |
| 211 |
| 212 run_loop.Run(); |
| 213 } |
| 214 |
| 215 histogram_tester()->ExpectUniqueSample( |
| 216 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 217 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 218 |
| 219 test_helpers()->set_fail_abort_event(true); |
| 220 |
| 221 { |
| 222 base::RunLoop run_loop; |
| 223 dispatcher()->DispatchBackgroundFetchAbortEvent( |
| 224 service_worker_registration->id(), origin, kExampleTag, |
| 225 run_loop.QuitClosure()); |
| 226 |
| 227 run_loop.Run(); |
| 228 } |
| 229 |
| 230 histogram_tester()->ExpectBucketCount( |
| 231 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 232 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 233 histogram_tester()->ExpectBucketCount( |
| 234 "BackgroundFetch.EventDispatchResult.AbortEvent", |
| 235 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1); |
| 236 histogram_tester()->ExpectUniqueSample( |
| 237 "BackgroundFetch.EventDispatchFailure.Dispatch.AbortEvent", |
| 238 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1); |
| 239 } |
| 240 |
| 241 TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) { |
| 242 auto service_worker_registration = RegisterServiceWorker(); |
| 243 ASSERT_TRUE(service_worker_registration); |
| 244 ASSERT_TRUE(service_worker_registration->active_version()); |
| 245 |
| 246 GURL origin(kExampleOrigin); |
| 247 |
| 248 { |
| 249 base::RunLoop run_loop; |
| 250 dispatcher()->DispatchBackgroundFetchClickEvent( |
| 251 service_worker_registration->id(), origin, kExampleTag, |
| 252 mojom::BackgroundFetchState::PENDING, run_loop.QuitClosure()); |
| 253 |
| 254 run_loop.Run(); |
| 255 } |
| 256 |
| 257 histogram_tester()->ExpectUniqueSample( |
| 258 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 259 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 260 |
| 261 test_helpers()->set_fail_click_event(true); |
| 262 |
| 263 { |
| 264 base::RunLoop run_loop; |
| 265 dispatcher()->DispatchBackgroundFetchClickEvent( |
| 266 service_worker_registration->id(), origin, kExampleTag, |
| 267 mojom::BackgroundFetchState::PENDING, run_loop.QuitClosure()); |
| 268 |
| 269 run_loop.Run(); |
| 270 } |
| 271 |
| 272 histogram_tester()->ExpectBucketCount( |
| 273 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 274 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); |
| 275 histogram_tester()->ExpectBucketCount( |
| 276 "BackgroundFetch.EventDispatchResult.ClickEvent", |
| 277 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1); |
| 278 histogram_tester()->ExpectUniqueSample( |
| 279 "BackgroundFetch.EventDispatchFailure.Dispatch.ClickEvent", |
| 280 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1); |
| 281 } |
| 282 |
| 283 } // namespace |
| 284 } // namespace content |
| OLD | NEW |