| 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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 service_impl_.reset(new BackgroundSyncServiceImpl( |
| 175 background_sync_context_.get(), service_request.Pass())); | 175 background_sync_context_.get(), service_request.Pass())); |
| 176 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Helpers for testing BackgroundSyncServiceImpl methods | 179 // Helpers for testing BackgroundSyncServiceImpl methods |
| 180 void RegisterOneShot( | 180 void RegisterOneShot( |
| 181 SyncRegistrationPtr sync, | 181 SyncRegistrationPtr sync, |
| 182 const BackgroundSyncService::RegisterCallback& callback) { | 182 const BackgroundSyncService::RegisterCallback& callback) { |
| 183 service_impl_->Register(sync.Pass(), sw_registration_id_, | 183 service_impl_->Register(sync.Pass(), sw_registration_id_, callback); |
| 184 true /* requested_from_service_worker */, callback); | |
| 185 base::RunLoop().RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
| 186 } | 185 } |
| 187 | 186 |
| 188 void RegisterOneShotFromDocument( | |
| 189 SyncRegistrationPtr sync, | |
| 190 const BackgroundSyncService::RegisterCallback& callback) { | |
| 191 service_impl_->Register(sync.Pass(), sw_registration_id_, | |
| 192 false /* requested_from_service_worker */, | |
| 193 callback); | |
| 194 base::RunLoop().RunUntilIdle(); | |
| 195 } | |
| 196 | |
| 197 void UnregisterOneShot( | 187 void UnregisterOneShot( |
| 198 SyncRegistrationPtr sync, | 188 SyncRegistrationPtr sync, |
| 199 const BackgroundSyncService::UnregisterCallback& callback) { | 189 const BackgroundSyncService::UnregisterCallback& callback) { |
| 200 service_impl_->Unregister( | 190 service_impl_->Unregister( |
| 201 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 191 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 202 sync->id, sync->tag, sw_registration_id_, callback); | 192 sync->id, sync->tag, sw_registration_id_, callback); |
| 203 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
| 204 } | 194 } |
| 205 | 195 |
| 206 void GetRegistrationOneShot( | 196 void GetRegistrationOneShot( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 BackgroundSyncError error; | 242 BackgroundSyncError error; |
| 253 SyncRegistrationPtr reg; | 243 SyncRegistrationPtr reg; |
| 254 RemoveWindowClient(); | 244 RemoveWindowClient(); |
| 255 RegisterOneShot( | 245 RegisterOneShot( |
| 256 default_sync_registration_.Clone(), | 246 default_sync_registration_.Clone(), |
| 257 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | 247 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); |
| 258 EXPECT_TRUE(called); | 248 EXPECT_TRUE(called); |
| 259 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, error); | 249 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, error); |
| 260 } | 250 } |
| 261 | 251 |
| 262 TEST_F(BackgroundSyncServiceImplTest, RegisterFromControlledClient) { | |
| 263 bool called = false; | |
| 264 BackgroundSyncError error; | |
| 265 SyncRegistrationPtr reg; | |
| 266 RegisterOneShotFromDocument( | |
| 267 default_sync_registration_.Clone(), | |
| 268 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
| 269 EXPECT_TRUE(called); | |
| 270 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | |
| 271 EXPECT_EQ("", reg->tag); | |
| 272 } | |
| 273 | |
| 274 TEST_F(BackgroundSyncServiceImplTest, RegisterFromUncontrolledClient) { | |
| 275 bool called = false; | |
| 276 BackgroundSyncError error; | |
| 277 SyncRegistrationPtr reg; | |
| 278 RemoveWindowClient(); | |
| 279 RegisterOneShotFromDocument( | |
| 280 default_sync_registration_.Clone(), | |
| 281 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | |
| 282 EXPECT_TRUE(called); | |
| 283 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, error); | |
| 284 EXPECT_EQ("", reg->tag); | |
| 285 } | |
| 286 | |
| 287 TEST_F(BackgroundSyncServiceImplTest, Unregister) { | 252 TEST_F(BackgroundSyncServiceImplTest, Unregister) { |
| 288 bool unregister_called = false; | 253 bool unregister_called = false; |
| 289 BackgroundSyncError unregister_error; | 254 BackgroundSyncError unregister_error; |
| 290 SyncRegistrationPtr reg; | 255 SyncRegistrationPtr reg; |
| 291 UnregisterOneShot( | 256 UnregisterOneShot( |
| 292 default_sync_registration_.Clone(), | 257 default_sync_registration_.Clone(), |
| 293 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); | 258 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); |
| 294 EXPECT_TRUE(unregister_called); | 259 EXPECT_TRUE(unregister_called); |
| 295 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND, | 260 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND, |
| 296 unregister_error); | 261 unregister_error); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, | 335 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, |
| 371 &getregistrations_called, | 336 &getregistrations_called, |
| 372 &getregistrations_error, &array_size)); | 337 &getregistrations_error, &array_size)); |
| 373 EXPECT_TRUE(getregistrations_called); | 338 EXPECT_TRUE(getregistrations_called); |
| 374 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, | 339 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, |
| 375 getregistrations_error); | 340 getregistrations_error); |
| 376 EXPECT_EQ(1UL, array_size); | 341 EXPECT_EQ(1UL, array_size); |
| 377 } | 342 } |
| 378 | 343 |
| 379 } // namespace content | 344 } // namespace content |
| OLD | NEW |