| 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> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 ServiceWorkerStatusCode status, | 53 ServiceWorkerStatusCode status, |
| 54 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 54 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 55 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 55 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 56 *out_registration = registration; | 56 *out_registration = registration; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Callbacks from BackgroundSyncServiceImpl methods | 59 // Callbacks from BackgroundSyncServiceImpl methods |
| 60 | 60 |
| 61 void ErrorAndRegistrationCallback( | 61 void ErrorAndRegistrationCallback( |
| 62 bool* called, | 62 bool* called, |
| 63 mojom::BackgroundSyncError* out_error, | 63 blink::mojom::BackgroundSyncError* out_error, |
| 64 mojom::SyncRegistrationPtr* out_registration, | 64 blink::mojom::SyncRegistrationPtr* out_registration, |
| 65 mojom::BackgroundSyncError error, | 65 blink::mojom::BackgroundSyncError error, |
| 66 const mojom::SyncRegistrationPtr& registration) { | 66 const blink::mojom::SyncRegistrationPtr& registration) { |
| 67 *called = true; | 67 *called = true; |
| 68 *out_error = error; | 68 *out_error = error; |
| 69 *out_registration = registration.Clone(); | 69 *out_registration = registration.Clone(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void ErrorAndRegistrationListCallback( | 72 void ErrorAndRegistrationListCallback( |
| 73 bool* called, | 73 bool* called, |
| 74 mojom::BackgroundSyncError* out_error, | 74 blink::mojom::BackgroundSyncError* out_error, |
| 75 unsigned long* out_array_size, | 75 unsigned long* out_array_size, |
| 76 mojom::BackgroundSyncError error, | 76 blink::mojom::BackgroundSyncError error, |
| 77 mojo::Array<content::mojom::SyncRegistrationPtr> registrations) { | 77 mojo::Array<blink::mojom::SyncRegistrationPtr> registrations) { |
| 78 *called = true; | 78 *called = true; |
| 79 *out_error = error; | 79 *out_error = error; |
| 80 if (error == mojom::BackgroundSyncError::NONE) | 80 if (error == blink::mojom::BackgroundSyncError::NONE) |
| 81 *out_array_size = registrations.size(); | 81 *out_array_size = registrations.size(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 } // namespace | 84 } // namespace |
| 85 | 85 |
| 86 class BackgroundSyncServiceImplTest : public testing::Test { | 86 class BackgroundSyncServiceImplTest : public testing::Test { |
| 87 public: | 87 public: |
| 88 BackgroundSyncServiceImplTest() | 88 BackgroundSyncServiceImplTest() |
| 89 : thread_bundle_( | 89 : thread_bundle_( |
| 90 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 90 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
| 91 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { | 91 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { |
| 92 default_sync_registration_ = mojom::SyncRegistration::New(); | 92 default_sync_registration_ = blink::mojom::SyncRegistration::New(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void SetUp() override { | 95 void SetUp() override { |
| 96 // Don't let the tests be confused by the real-world device connectivity | 96 // Don't let the tests be confused by the real-world device connectivity |
| 97 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); | 97 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); |
| 98 | 98 |
| 99 CreateTestHelper(); | 99 CreateTestHelper(); |
| 100 CreateStoragePartition(); | 100 CreateStoragePartition(); |
| 101 CreateBackgroundSyncContext(); | 101 CreateBackgroundSyncContext(); |
| 102 CreateServiceWorkerRegistration(); | 102 CreateServiceWorkerRegistration(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 embedded_worker_helper_->context_wrapper()->FindReadyRegistrationForId( | 173 embedded_worker_helper_->context_wrapper()->FindReadyRegistrationForId( |
| 174 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), | 174 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), |
| 175 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); | 175 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); |
| 176 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 177 EXPECT_TRUE(sw_registration_); | 177 EXPECT_TRUE(sw_registration_); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void CreateBackgroundSyncServiceImpl() { | 180 void CreateBackgroundSyncServiceImpl() { |
| 181 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be | 181 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be |
| 182 // instantiated. | 182 // instantiated. |
| 183 mojo::InterfaceRequest<mojom::BackgroundSyncService> service_request = | 183 mojo::InterfaceRequest<blink::mojom::BackgroundSyncService> |
| 184 mojo::GetProxy(&service_ptr_); | 184 service_request = mojo::GetProxy(&service_ptr_); |
| 185 // Create a new BackgroundSyncServiceImpl bound to the dummy channel. | 185 // Create a new BackgroundSyncServiceImpl bound to the dummy channel. |
| 186 background_sync_context_->CreateService(std::move(service_request)); | 186 background_sync_context_->CreateService(std::move(service_request)); |
| 187 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 188 | 188 |
| 189 service_impl_ = *background_sync_context_->services_.begin(); | 189 service_impl_ = *background_sync_context_->services_.begin(); |
| 190 ASSERT_TRUE(service_impl_); | 190 ASSERT_TRUE(service_impl_); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // Helpers for testing BackgroundSyncServiceImpl methods | 193 // Helpers for testing BackgroundSyncServiceImpl methods |
| 194 void Register( | 194 void Register( |
| 195 mojom::SyncRegistrationPtr sync, | 195 blink::mojom::SyncRegistrationPtr sync, |
| 196 const mojom::BackgroundSyncService::RegisterCallback& callback) { | 196 const blink::mojom::BackgroundSyncService::RegisterCallback& callback) { |
| 197 service_impl_->Register(std::move(sync), sw_registration_id_, callback); | 197 service_impl_->Register(std::move(sync), sw_registration_id_, callback); |
| 198 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void GetRegistrations( | 201 void GetRegistrations( |
| 202 const mojom::BackgroundSyncService::GetRegistrationsCallback& callback) { | 202 const blink::mojom::BackgroundSyncService::GetRegistrationsCallback& |
| 203 callback) { |
| 203 service_impl_->GetRegistrations(sw_registration_id_, callback); | 204 service_impl_->GetRegistrations(sw_registration_id_, callback); |
| 204 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
| 205 } | 206 } |
| 206 | 207 |
| 207 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; | 208 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 208 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 209 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 209 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; | 210 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 210 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; | 211 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; |
| 211 scoped_refptr<BackgroundSyncContext> background_sync_context_; | 212 scoped_refptr<BackgroundSyncContext> background_sync_context_; |
| 212 int64_t sw_registration_id_; | 213 int64_t sw_registration_id_; |
| 213 scoped_refptr<ServiceWorkerRegistration> sw_registration_; | 214 scoped_refptr<ServiceWorkerRegistration> sw_registration_; |
| 214 mojom::BackgroundSyncServicePtr service_ptr_; | 215 blink::mojom::BackgroundSyncServicePtr service_ptr_; |
| 215 BackgroundSyncServiceImpl* | 216 BackgroundSyncServiceImpl* |
| 216 service_impl_; // Owned by background_sync_context_ | 217 service_impl_; // Owned by background_sync_context_ |
| 217 mojom::SyncRegistrationPtr default_sync_registration_; | 218 blink::mojom::SyncRegistrationPtr default_sync_registration_; |
| 218 }; | 219 }; |
| 219 | 220 |
| 220 // Tests | 221 // Tests |
| 221 | 222 |
| 222 TEST_F(BackgroundSyncServiceImplTest, Register) { | 223 TEST_F(BackgroundSyncServiceImplTest, Register) { |
| 223 bool called = false; | 224 bool called = false; |
| 224 mojom::BackgroundSyncError error; | 225 blink::mojom::BackgroundSyncError error; |
| 225 mojom::SyncRegistrationPtr reg; | 226 blink::mojom::SyncRegistrationPtr reg; |
| 226 Register(default_sync_registration_.Clone(), | 227 Register(default_sync_registration_.Clone(), |
| 227 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); | 228 base::Bind(&ErrorAndRegistrationCallback, &called, &error, ®)); |
| 228 EXPECT_TRUE(called); | 229 EXPECT_TRUE(called); |
| 229 EXPECT_EQ(mojom::BackgroundSyncError::NONE, error); | 230 EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error); |
| 230 EXPECT_EQ("", reg->tag); | 231 EXPECT_EQ("", reg->tag); |
| 231 } | 232 } |
| 232 | 233 |
| 233 TEST_F(BackgroundSyncServiceImplTest, GetRegistrations) { | 234 TEST_F(BackgroundSyncServiceImplTest, GetRegistrations) { |
| 234 bool called = false; | 235 bool called = false; |
| 235 mojom::BackgroundSyncError error; | 236 blink::mojom::BackgroundSyncError error; |
| 236 unsigned long array_size = 0UL; | 237 unsigned long array_size = 0UL; |
| 237 GetRegistrations(base::Bind(&ErrorAndRegistrationListCallback, &called, | 238 GetRegistrations(base::Bind(&ErrorAndRegistrationListCallback, &called, |
| 238 &error, &array_size)); | 239 &error, &array_size)); |
| 239 EXPECT_TRUE(called); | 240 EXPECT_TRUE(called); |
| 240 EXPECT_EQ(mojom::BackgroundSyncError::NONE, error); | 241 EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error); |
| 241 EXPECT_EQ(0UL, array_size); | 242 EXPECT_EQ(0UL, array_size); |
| 242 } | 243 } |
| 243 | 244 |
| 244 TEST_F(BackgroundSyncServiceImplTest, GetRegistrationsWithRegisteredSync) { | 245 TEST_F(BackgroundSyncServiceImplTest, GetRegistrationsWithRegisteredSync) { |
| 245 bool register_called = false; | 246 bool register_called = false; |
| 246 bool getregistrations_called = false; | 247 bool getregistrations_called = false; |
| 247 mojom::BackgroundSyncError register_error; | 248 blink::mojom::BackgroundSyncError register_error; |
| 248 mojom::BackgroundSyncError getregistrations_error; | 249 blink::mojom::BackgroundSyncError getregistrations_error; |
| 249 mojom::SyncRegistrationPtr register_reg; | 250 blink::mojom::SyncRegistrationPtr register_reg; |
| 250 unsigned long array_size = 0UL; | 251 unsigned long array_size = 0UL; |
| 251 Register(default_sync_registration_.Clone(), | 252 Register(default_sync_registration_.Clone(), |
| 252 base::Bind(&ErrorAndRegistrationCallback, ®ister_called, | 253 base::Bind(&ErrorAndRegistrationCallback, ®ister_called, |
| 253 ®ister_error, ®ister_reg)); | 254 ®ister_error, ®ister_reg)); |
| 254 EXPECT_TRUE(register_called); | 255 EXPECT_TRUE(register_called); |
| 255 EXPECT_EQ(mojom::BackgroundSyncError::NONE, register_error); | 256 EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, register_error); |
| 256 GetRegistrations(base::Bind(&ErrorAndRegistrationListCallback, | 257 GetRegistrations(base::Bind(&ErrorAndRegistrationListCallback, |
| 257 &getregistrations_called, &getregistrations_error, | 258 &getregistrations_called, &getregistrations_error, |
| 258 &array_size)); | 259 &array_size)); |
| 259 EXPECT_TRUE(getregistrations_called); | 260 EXPECT_TRUE(getregistrations_called); |
| 260 EXPECT_EQ(mojom::BackgroundSyncError::NONE, getregistrations_error); | 261 EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, getregistrations_error); |
| 261 EXPECT_EQ(1UL, array_size); | 262 EXPECT_EQ(1UL, array_size); |
| 262 } | 263 } |
| 263 | 264 |
| 264 } // namespace content | 265 } // namespace content |
| OLD | NEW |