OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/background_sync/background_sync_service_impl.h" | 5 #include "content/browser/background_sync/background_sync_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
11 #include "base/power_monitor/power_monitor_source.h" | 11 #include "base/power_monitor/power_monitor_source.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "content/browser/background_sync/background_sync_context_impl.h" | 13 #include "content/browser/background_sync/background_sync_context_impl.h" |
14 #include "content/browser/background_sync/background_sync_network_observer.h" | 14 #include "content/browser/background_sync/background_sync_network_observer.h" |
15 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 15 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/test/background_sync_test_util.h" | 18 #include "content/public/test/background_sync_test_util.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "mojo/public/cpp/bindings/interface_ptr.h" | 20 #include "mojo/public/cpp/bindings/interface_ptr.h" |
21 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kServiceWorkerPattern[] = "https://example.com/a"; | 28 const char kServiceWorkerPattern[] = "https://example.com/a"; |
29 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; | 29 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; |
30 const int kProviderId = 1; | |
31 | 30 |
32 // Callbacks from SetUp methods | 31 // Callbacks from SetUp methods |
33 | |
34 void RegisterServiceWorkerCallback(bool* called, | 32 void RegisterServiceWorkerCallback(bool* called, |
35 int64* store_registration_id, | 33 int64* store_registration_id, |
36 ServiceWorkerStatusCode status, | 34 ServiceWorkerStatusCode status, |
37 const std::string& status_message, | 35 const std::string& status_message, |
38 int64 registration_id) { | 36 int64 registration_id) { |
39 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
40 *called = true; | 38 *called = true; |
41 *store_registration_id = registration_id; | 39 *store_registration_id = registration_id; |
42 } | 40 } |
43 | 41 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 base::Bind(&RegisterServiceWorkerCallback, &called, | 162 base::Bind(&RegisterServiceWorkerCallback, &called, |
165 &sw_registration_id_)); | 163 &sw_registration_id_)); |
166 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
167 EXPECT_TRUE(called); | 165 EXPECT_TRUE(called); |
168 | 166 |
169 embedded_worker_helper_->context_wrapper()->FindReadyRegistrationForId( | 167 embedded_worker_helper_->context_wrapper()->FindReadyRegistrationForId( |
170 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), | 168 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), |
171 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); | 169 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); |
172 base::RunLoop().RunUntilIdle(); | 170 base::RunLoop().RunUntilIdle(); |
173 EXPECT_TRUE(sw_registration_); | 171 EXPECT_TRUE(sw_registration_); |
174 | |
175 // Register window client for the service worker | |
176 ServiceWorkerProviderHost* provider_host = new ServiceWorkerProviderHost( | |
177 embedded_worker_helper_->mock_render_process_id(), | |
178 MSG_ROUTING_NONE /* render_frame_id */, kProviderId, | |
179 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | |
180 embedded_worker_helper_->context()->AsWeakPtr(), nullptr); | |
181 provider_host->SetDocumentUrl(GURL(kServiceWorkerPattern)); | |
182 embedded_worker_helper_->context()->AddProviderHost( | |
183 make_scoped_ptr(provider_host)); | |
184 } | |
185 | |
186 void RemoveWindowClient() { | |
187 embedded_worker_helper_->context()->RemoveAllProviderHostsForProcess( | |
188 embedded_worker_helper_->mock_render_process_id()); | |
189 } | 172 } |
190 | 173 |
191 void CreateBackgroundSyncServiceImpl() { | 174 void CreateBackgroundSyncServiceImpl() { |
192 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be | 175 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be |
193 // instantiated | 176 // instantiated |
194 mojo::InterfaceRequest<BackgroundSyncService> service_request = | 177 mojo::InterfaceRequest<BackgroundSyncService> service_request = |
195 mojo::GetProxy(&service_ptr_); | 178 mojo::GetProxy(&service_ptr_); |
196 // Create a new BackgroundSyncServiceImpl bound to the dummy channel | 179 // Create a new BackgroundSyncServiceImpl bound to the dummy channel |
197 background_sync_context_->CreateService(service_request.Pass()); | 180 background_sync_context_->CreateService(service_request.Pass()); |
198 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
199 | 182 |
200 service_impl_ = *background_sync_context_->services_.begin(); | 183 service_impl_ = *background_sync_context_->services_.begin(); |
201 ASSERT_TRUE(service_impl_); | 184 ASSERT_TRUE(service_impl_); |
202 } | 185 } |
203 | 186 |
204 // Helpers for testing BackgroundSyncServiceImpl methods | 187 // Helpers for testing BackgroundSyncServiceImpl methods |
205 void RegisterOneShot( | 188 void RegisterOneShot( |
206 SyncRegistrationPtr sync, | 189 SyncRegistrationPtr sync, |
207 const BackgroundSyncService::RegisterCallback& callback) { | 190 const BackgroundSyncService::RegisterCallback& callback) { |
208 service_impl_->Register(sync.Pass(), sw_registration_id_, | 191 service_impl_->Register(sync.Pass(), sw_registration_id_, |
209 true /* requested_from_service_worker */, callback); | |
210 base::RunLoop().RunUntilIdle(); | |
211 } | |
212 | |
213 void RegisterOneShotFromDocument( | |
214 SyncRegistrationPtr sync, | |
215 const BackgroundSyncService::RegisterCallback& callback) { | |
216 service_impl_->Register(sync.Pass(), sw_registration_id_, | |
217 false /* requested_from_service_worker */, | 192 false /* requested_from_service_worker */, |
218 callback); | 193 callback); |
219 base::RunLoop().RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
220 } | 195 } |
221 | 196 |
222 void UnregisterOneShot( | 197 void UnregisterOneShot( |
223 int32 handle_id, | 198 int32 handle_id, |
224 const BackgroundSyncService::UnregisterCallback& callback) { | 199 const BackgroundSyncService::UnregisterCallback& callback) { |
225 service_impl_->Unregister( | 200 service_impl_->Unregister( |
226 handle_id, sw_registration_id_, callback); | 201 handle_id, sw_registration_id_, callback); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 BackgroundSyncError error; | 246 BackgroundSyncError error; |
272 SyncRegistrationPtr reg; | 247 SyncRegistrationPtr reg; |
273 RegisterOneShot( | 248 RegisterOneShot( |
274 default_sync_registration_.Clone(), | 249 default_sync_registration_.Clone(), |
275 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | 250 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); |
276 EXPECT_TRUE(called); | 251 EXPECT_TRUE(called); |
277 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | 252 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); |
278 EXPECT_EQ("", reg->tag); | 253 EXPECT_EQ("", reg->tag); |
279 } | 254 } |
280 | 255 |
281 TEST_F(BackgroundSyncServiceImplTest, RegisterFromServiceWorkerWithWindow) { | |
282 bool called = false; | |
283 BackgroundSyncError error; | |
284 SyncRegistrationPtr reg; | |
285 RegisterOneShot( | |
286 default_sync_registration_.Clone(), | |
287 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
288 EXPECT_TRUE(called); | |
289 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | |
290 } | |
291 | |
292 TEST_F(BackgroundSyncServiceImplTest, RegisterFromServiceWorkerWithoutWindow) { | |
293 bool called = false; | |
294 BackgroundSyncError error; | |
295 SyncRegistrationPtr reg; | |
296 RemoveWindowClient(); | |
297 RegisterOneShot( | |
298 default_sync_registration_.Clone(), | |
299 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
300 EXPECT_TRUE(called); | |
301 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, error); | |
302 } | |
303 | |
304 TEST_F(BackgroundSyncServiceImplTest, RegisterFromDocumentWithWindow) { | |
305 bool called = false; | |
306 BackgroundSyncError error; | |
307 SyncRegistrationPtr reg; | |
308 RegisterOneShotFromDocument( | |
309 default_sync_registration_.Clone(), | |
310 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
311 EXPECT_TRUE(called); | |
312 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | |
313 EXPECT_EQ("", reg->tag); | |
314 } | |
315 | |
316 TEST_F(BackgroundSyncServiceImplTest, RegisterFromDocumentWithoutWindow) { | |
317 bool called = false; | |
318 BackgroundSyncError error; | |
319 SyncRegistrationPtr reg; | |
320 RemoveWindowClient(); | |
321 RegisterOneShotFromDocument( | |
322 default_sync_registration_.Clone(), | |
323 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
324 EXPECT_TRUE(called); | |
325 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | |
326 EXPECT_EQ("", reg->tag); | |
327 } | |
328 | |
329 TEST_F(BackgroundSyncServiceImplTest, Unregister) { | 256 TEST_F(BackgroundSyncServiceImplTest, Unregister) { |
330 bool unregister_called = false; | 257 bool unregister_called = false; |
331 BackgroundSyncError unregister_error; | 258 BackgroundSyncError unregister_error; |
332 SyncRegistrationPtr reg; | 259 SyncRegistrationPtr reg; |
333 UnregisterOneShot( | 260 UnregisterOneShot( |
334 default_sync_registration_->handle_id, | 261 default_sync_registration_->handle_id, |
335 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); | 262 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); |
336 EXPECT_TRUE(unregister_called); | 263 EXPECT_TRUE(unregister_called); |
337 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, | 264 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, |
338 unregister_error); | 265 unregister_error); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 | 373 |
447 NotifyWhenDone(reg->handle_id, | 374 NotifyWhenDone(reg->handle_id, |
448 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, | 375 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, |
449 ¬ify_done_error, ¬ify_done_sync_state)); | 376 ¬ify_done_error, ¬ify_done_sync_state)); |
450 EXPECT_TRUE(notify_done_called); | 377 EXPECT_TRUE(notify_done_called); |
451 EXPECT_EQ(BACKGROUND_SYNC_ERROR_NONE, notify_done_error); | 378 EXPECT_EQ(BACKGROUND_SYNC_ERROR_NONE, notify_done_error); |
452 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, notify_done_sync_state); | 379 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, notify_done_sync_state); |
453 } | 380 } |
454 | 381 |
455 } // namespace content | 382 } // namespace content |
OLD | NEW |