| 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 <stdint.h> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/power_monitor/power_monitor.h" | 12 #include "base/power_monitor/power_monitor.h" |
| 11 #include "base/power_monitor/power_monitor_source.h" | 13 #include "base/power_monitor/power_monitor_source.h" |
| 12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 13 #include "content/browser/background_sync/background_sync_context_impl.h" | 15 #include "content/browser/background_sync/background_sync_context_impl.h" |
| 14 #include "content/browser/background_sync/background_sync_network_observer.h" | 16 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 15 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 17 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/background_sync_test_util.h" | 20 #include "content/public/test/background_sync_test_util.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "mojo/public/cpp/bindings/interface_ptr.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 21 #include "net/base/network_change_notifier.h" | 23 #include "net/base/network_change_notifier.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 24 namespace content { | 26 namespace content { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 const char kServiceWorkerPattern[] = "https://example.com/a"; | 30 const char kServiceWorkerPattern[] = "https://example.com/a"; |
| 29 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; | 31 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; |
| 30 | 32 |
| 31 // Callbacks from SetUp methods | 33 // Callbacks from SetUp methods |
| 32 void RegisterServiceWorkerCallback(bool* called, | 34 void RegisterServiceWorkerCallback(bool* called, |
| 33 int64* store_registration_id, | 35 int64_t* store_registration_id, |
| 34 ServiceWorkerStatusCode status, | 36 ServiceWorkerStatusCode status, |
| 35 const std::string& status_message, | 37 const std::string& status_message, |
| 36 int64 registration_id) { | 38 int64_t registration_id) { |
| 37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 39 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 38 *called = true; | 40 *called = true; |
| 39 *store_registration_id = registration_id; | 41 *store_registration_id = registration_id; |
| 40 } | 42 } |
| 41 | 43 |
| 42 void FindServiceWorkerRegistrationCallback( | 44 void FindServiceWorkerRegistrationCallback( |
| 43 scoped_refptr<ServiceWorkerRegistration>* out_registration, | 45 scoped_refptr<ServiceWorkerRegistration>* out_registration, |
| 44 ServiceWorkerStatusCode status, | 46 ServiceWorkerStatusCode status, |
| 45 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 47 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 46 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 48 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 void RegisterOneShot( | 190 void RegisterOneShot( |
| 189 SyncRegistrationPtr sync, | 191 SyncRegistrationPtr sync, |
| 190 const BackgroundSyncService::RegisterCallback& callback) { | 192 const BackgroundSyncService::RegisterCallback& callback) { |
| 191 service_impl_->Register(sync.Pass(), sw_registration_id_, | 193 service_impl_->Register(sync.Pass(), sw_registration_id_, |
| 192 false /* requested_from_service_worker */, | 194 false /* requested_from_service_worker */, |
| 193 callback); | 195 callback); |
| 194 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 195 } | 197 } |
| 196 | 198 |
| 197 void UnregisterOneShot( | 199 void UnregisterOneShot( |
| 198 int32 handle_id, | 200 int32_t handle_id, |
| 199 const BackgroundSyncService::UnregisterCallback& callback) { | 201 const BackgroundSyncService::UnregisterCallback& callback) { |
| 200 service_impl_->Unregister( | 202 service_impl_->Unregister( |
| 201 handle_id, sw_registration_id_, callback); | 203 handle_id, sw_registration_id_, callback); |
| 202 base::RunLoop().RunUntilIdle(); | 204 base::RunLoop().RunUntilIdle(); |
| 203 } | 205 } |
| 204 | 206 |
| 205 void GetRegistrationOneShot( | 207 void GetRegistrationOneShot( |
| 206 const mojo::String& tag, | 208 const mojo::String& tag, |
| 207 const BackgroundSyncService::RegisterCallback& callback) { | 209 const BackgroundSyncService::RegisterCallback& callback) { |
| 208 service_impl_->GetRegistration( | 210 service_impl_->GetRegistration( |
| 209 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, tag, | 211 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, tag, |
| 210 sw_registration_id_, callback); | 212 sw_registration_id_, callback); |
| 211 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
| 212 } | 214 } |
| 213 | 215 |
| 214 void GetRegistrationsOneShot( | 216 void GetRegistrationsOneShot( |
| 215 const BackgroundSyncService::GetRegistrationsCallback& callback) { | 217 const BackgroundSyncService::GetRegistrationsCallback& callback) { |
| 216 service_impl_->GetRegistrations( | 218 service_impl_->GetRegistrations( |
| 217 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 219 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 218 sw_registration_id_, callback); | 220 sw_registration_id_, callback); |
| 219 base::RunLoop().RunUntilIdle(); | 221 base::RunLoop().RunUntilIdle(); |
| 220 } | 222 } |
| 221 | 223 |
| 222 void NotifyWhenDone( | 224 void NotifyWhenDone( |
| 223 int32 handle_id, | 225 int32_t handle_id, |
| 224 const BackgroundSyncService::NotifyWhenFinishedCallback& callback) { | 226 const BackgroundSyncService::NotifyWhenFinishedCallback& callback) { |
| 225 service_impl_->NotifyWhenFinished(handle_id, callback); | 227 service_impl_->NotifyWhenFinished(handle_id, callback); |
| 226 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
| 227 } | 229 } |
| 228 | 230 |
| 229 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; | 231 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 230 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 232 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 231 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; | 233 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 232 scoped_ptr<base::PowerMonitor> power_monitor_; | 234 scoped_ptr<base::PowerMonitor> power_monitor_; |
| 233 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; | 235 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; |
| 234 int64 sw_registration_id_; | 236 int64_t sw_registration_id_; |
| 235 scoped_refptr<ServiceWorkerRegistration> sw_registration_; | 237 scoped_refptr<ServiceWorkerRegistration> sw_registration_; |
| 236 BackgroundSyncServicePtr service_ptr_; | 238 BackgroundSyncServicePtr service_ptr_; |
| 237 BackgroundSyncServiceImpl* | 239 BackgroundSyncServiceImpl* |
| 238 service_impl_; // Owned by background_sync_context_ | 240 service_impl_; // Owned by background_sync_context_ |
| 239 SyncRegistrationPtr default_sync_registration_; | 241 SyncRegistrationPtr default_sync_registration_; |
| 240 }; | 242 }; |
| 241 | 243 |
| 242 // Tests | 244 // Tests |
| 243 | 245 |
| 244 TEST_F(BackgroundSyncServiceImplTest, Register) { | 246 TEST_F(BackgroundSyncServiceImplTest, Register) { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 375 |
| 374 NotifyWhenDone(reg->handle_id, | 376 NotifyWhenDone(reg->handle_id, |
| 375 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, | 377 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, |
| 376 ¬ify_done_error, ¬ify_done_sync_state)); | 378 ¬ify_done_error, ¬ify_done_sync_state)); |
| 377 EXPECT_TRUE(notify_done_called); | 379 EXPECT_TRUE(notify_done_called); |
| 378 EXPECT_EQ(BACKGROUND_SYNC_ERROR_NONE, notify_done_error); | 380 EXPECT_EQ(BACKGROUND_SYNC_ERROR_NONE, notify_done_error); |
| 379 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, notify_done_sync_state); | 381 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, notify_done_sync_state); |
| 380 } | 382 } |
| 381 | 383 |
| 382 } // namespace content | 384 } // namespace content |
| OLD | NEW |