| 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" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 void RemoveWindowClient() { | 164 void RemoveWindowClient() { |
| 165 sw_registration_->active_version()->RemoveControllee(provider_host_.get()); | 165 sw_registration_->active_version()->RemoveControllee(provider_host_.get()); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void CreateBackgroundSyncServiceImpl() { | 168 void CreateBackgroundSyncServiceImpl() { |
| 169 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be | 169 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be |
| 170 // instantiated | 170 // instantiated |
| 171 mojo::InterfaceRequest<BackgroundSyncService> service_request = | 171 mojo::InterfaceRequest<BackgroundSyncService> service_request = |
| 172 mojo::GetProxy(&service_ptr_); | 172 mojo::GetProxy(&service_ptr_); |
| 173 // Create a new BackgroundSyncServiceImpl bound to the dummy channel | 173 // Create a new BackgroundSyncServiceImpl bound to the dummy channel |
| 174 service_impl_.reset(new BackgroundSyncServiceImpl( | 174 background_sync_context_->CreateService(service_request.Pass()); |
| 175 background_sync_context_.get(), service_request.Pass())); | |
| 176 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 176 |
| 177 service_impl_ = *background_sync_context_->services_.begin(); |
| 178 ASSERT_TRUE(service_impl_); |
| 177 } | 179 } |
| 178 | 180 |
| 179 // Helpers for testing BackgroundSyncServiceImpl methods | 181 // Helpers for testing BackgroundSyncServiceImpl methods |
| 180 void RegisterOneShot( | 182 void RegisterOneShot( |
| 181 SyncRegistrationPtr sync, | 183 SyncRegistrationPtr sync, |
| 182 const BackgroundSyncService::RegisterCallback& callback) { | 184 const BackgroundSyncService::RegisterCallback& callback) { |
| 183 service_impl_->Register(sync.Pass(), sw_registration_id_, callback); | 185 service_impl_->Register(sync.Pass(), sw_registration_id_, callback); |
| 184 base::RunLoop().RunUntilIdle(); | 186 base::RunLoop().RunUntilIdle(); |
| 185 } | 187 } |
| 186 | 188 |
| 187 void UnregisterOneShot( | 189 void UnregisterOneShot( |
| 188 SyncRegistrationPtr sync, | 190 SyncRegistrationPtr sync, |
| 189 const BackgroundSyncService::UnregisterCallback& callback) { | 191 const BackgroundSyncService::UnregisterCallback& callback) { |
| 190 service_impl_->Unregister( | 192 service_impl_->Unregister( |
| 191 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 193 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 192 sync->id, sync->tag, sw_registration_id_, callback); | 194 sync->id, sw_registration_id_, callback); |
| 193 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 194 } | 196 } |
| 195 | 197 |
| 196 void GetRegistrationOneShot( | 198 void GetRegistrationOneShot( |
| 197 const mojo::String& tag, | 199 const mojo::String& tag, |
| 198 const BackgroundSyncService::RegisterCallback& callback) { | 200 const BackgroundSyncService::RegisterCallback& callback) { |
| 199 service_impl_->GetRegistration( | 201 service_impl_->GetRegistration( |
| 200 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, tag, | 202 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, tag, |
| 201 sw_registration_id_, callback); | 203 sw_registration_id_, callback); |
| 202 base::RunLoop().RunUntilIdle(); | 204 base::RunLoop().RunUntilIdle(); |
| 203 } | 205 } |
| 204 | 206 |
| 205 void GetRegistrationsOneShot( | 207 void GetRegistrationsOneShot( |
| 206 const BackgroundSyncService::GetRegistrationsCallback& callback) { | 208 const BackgroundSyncService::GetRegistrationsCallback& callback) { |
| 207 service_impl_->GetRegistrations( | 209 service_impl_->GetRegistrations( |
| 208 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 210 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 209 sw_registration_id_, callback); | 211 sw_registration_id_, callback); |
| 210 base::RunLoop().RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
| 211 } | 213 } |
| 212 | 214 |
| 213 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; | 215 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 214 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 216 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 215 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; | 217 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 216 scoped_ptr<base::PowerMonitor> power_monitor_; | 218 scoped_ptr<base::PowerMonitor> power_monitor_; |
| 217 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; | 219 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; |
| 218 scoped_ptr<ServiceWorkerProviderHost> provider_host_; | 220 scoped_ptr<ServiceWorkerProviderHost> provider_host_; |
| 219 int64 sw_registration_id_; | 221 int64 sw_registration_id_; |
| 220 scoped_refptr<ServiceWorkerRegistration> sw_registration_; | 222 scoped_refptr<ServiceWorkerRegistration> sw_registration_; |
| 221 BackgroundSyncServicePtr service_ptr_; | 223 BackgroundSyncServicePtr service_ptr_; |
| 222 scoped_ptr<BackgroundSyncServiceImpl> service_impl_; | 224 BackgroundSyncServiceImpl* |
| 225 service_impl_; // Owned by background_sync_context_ |
| 223 SyncRegistrationPtr default_sync_registration_; | 226 SyncRegistrationPtr default_sync_registration_; |
| 224 }; | 227 }; |
| 225 | 228 |
| 226 // Tests | 229 // Tests |
| 227 | 230 |
| 228 TEST_F(BackgroundSyncServiceImplTest, Register) { | 231 TEST_F(BackgroundSyncServiceImplTest, Register) { |
| 229 bool called = false; | 232 bool called = false; |
| 230 BackgroundSyncError error; | 233 BackgroundSyncError error; |
| 231 SyncRegistrationPtr reg; | 234 SyncRegistrationPtr reg; |
| 232 RegisterOneShot( | 235 RegisterOneShot( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 250 } | 253 } |
| 251 | 254 |
| 252 TEST_F(BackgroundSyncServiceImplTest, Unregister) { | 255 TEST_F(BackgroundSyncServiceImplTest, Unregister) { |
| 253 bool unregister_called = false; | 256 bool unregister_called = false; |
| 254 BackgroundSyncError unregister_error; | 257 BackgroundSyncError unregister_error; |
| 255 SyncRegistrationPtr reg; | 258 SyncRegistrationPtr reg; |
| 256 UnregisterOneShot( | 259 UnregisterOneShot( |
| 257 default_sync_registration_.Clone(), | 260 default_sync_registration_.Clone(), |
| 258 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); | 261 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); |
| 259 EXPECT_TRUE(unregister_called); | 262 EXPECT_TRUE(unregister_called); |
| 260 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND, | 263 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, |
| 261 unregister_error); | 264 unregister_error); |
| 262 } | 265 } |
| 263 | 266 |
| 264 TEST_F(BackgroundSyncServiceImplTest, UnregisterWithRegisteredSync) { | 267 TEST_F(BackgroundSyncServiceImplTest, UnregisterWithRegisteredSync) { |
| 265 bool register_called = false; | 268 bool register_called = false; |
| 266 bool unregister_called = false; | 269 bool unregister_called = false; |
| 267 BackgroundSyncError register_error; | 270 BackgroundSyncError register_error; |
| 268 BackgroundSyncError unregister_error; | 271 BackgroundSyncError unregister_error; |
| 269 SyncRegistrationPtr reg; | 272 SyncRegistrationPtr reg; |
| 270 RegisterOneShot(default_sync_registration_.Clone(), | 273 RegisterOneShot(default_sync_registration_.Clone(), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, | 338 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, |
| 336 &getregistrations_called, | 339 &getregistrations_called, |
| 337 &getregistrations_error, &array_size)); | 340 &getregistrations_error, &array_size)); |
| 338 EXPECT_TRUE(getregistrations_called); | 341 EXPECT_TRUE(getregistrations_called); |
| 339 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, | 342 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, |
| 340 getregistrations_error); | 343 getregistrations_error); |
| 341 EXPECT_EQ(1UL, array_size); | 344 EXPECT_EQ(1UL, array_size); |
| 342 } | 345 } |
| 343 | 346 |
| 344 } // namespace content | 347 } // namespace content |
| OLD | NEW |