| 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_, callback); | 183 service_impl_->Register(sync.Pass(), sw_registration_id_, |
| 184 true /* requested_from_service_worker */, callback); |
| 184 base::RunLoop().RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
| 185 } | 186 } |
| 186 | 187 |
| 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 |
| 187 void UnregisterOneShot( | 197 void UnregisterOneShot( |
| 188 SyncRegistrationPtr sync, | 198 SyncRegistrationPtr sync, |
| 189 const BackgroundSyncService::UnregisterCallback& callback) { | 199 const BackgroundSyncService::UnregisterCallback& callback) { |
| 190 service_impl_->Unregister( | 200 service_impl_->Unregister( |
| 191 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 201 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 192 sync->id, sync->tag, sw_registration_id_, callback); | 202 sync->id, sync->tag, sw_registration_id_, callback); |
| 193 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
| 194 } | 204 } |
| 195 | 205 |
| 196 void GetRegistrationOneShot( | 206 void GetRegistrationOneShot( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 BackgroundSyncError error; | 252 BackgroundSyncError error; |
| 243 SyncRegistrationPtr reg; | 253 SyncRegistrationPtr reg; |
| 244 RemoveWindowClient(); | 254 RemoveWindowClient(); |
| 245 RegisterOneShot( | 255 RegisterOneShot( |
| 246 default_sync_registration_.Clone(), | 256 default_sync_registration_.Clone(), |
| 247 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | 257 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); |
| 248 EXPECT_TRUE(called); | 258 EXPECT_TRUE(called); |
| 249 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, error); | 259 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED, error); |
| 250 } | 260 } |
| 251 | 261 |
| 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 |
| 252 TEST_F(BackgroundSyncServiceImplTest, Unregister) { | 287 TEST_F(BackgroundSyncServiceImplTest, Unregister) { |
| 253 bool unregister_called = false; | 288 bool unregister_called = false; |
| 254 BackgroundSyncError unregister_error; | 289 BackgroundSyncError unregister_error; |
| 255 SyncRegistrationPtr reg; | 290 SyncRegistrationPtr reg; |
| 256 UnregisterOneShot( | 291 UnregisterOneShot( |
| 257 default_sync_registration_.Clone(), | 292 default_sync_registration_.Clone(), |
| 258 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); | 293 base::Bind(&ErrorCallback, &unregister_called, &unregister_error)); |
| 259 EXPECT_TRUE(unregister_called); | 294 EXPECT_TRUE(unregister_called); |
| 260 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND, | 295 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND, |
| 261 unregister_error); | 296 unregister_error); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, | 370 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, |
| 336 &getregistrations_called, | 371 &getregistrations_called, |
| 337 &getregistrations_error, &array_size)); | 372 &getregistrations_error, &array_size)); |
| 338 EXPECT_TRUE(getregistrations_called); | 373 EXPECT_TRUE(getregistrations_called); |
| 339 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, | 374 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, |
| 340 getregistrations_error); | 375 getregistrations_error); |
| 341 EXPECT_EQ(1UL, array_size); | 376 EXPECT_EQ(1UL, array_size); |
| 342 } | 377 } |
| 343 | 378 |
| 344 } // namespace content | 379 } // namespace content |
| OLD | NEW |