| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/power_monitor/power_monitor.h" | |
| 14 #include "base/power_monitor/power_monitor_source.h" | |
| 15 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 16 #include "content/browser/background_sync/background_sync_context_impl.h" | 14 #include "content/browser/background_sync/background_sync_context_impl.h" |
| 17 #include "content/browser/background_sync/background_sync_network_observer.h" | 15 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 18 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 19 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 20 #include "content/browser/storage_partition_impl.h" | 18 #include "content/browser/storage_partition_impl.h" |
| 21 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/test/background_sync_test_util.h" | 20 #include "content/public/test/background_sync_test_util.h" |
| 23 #include "content/public/test/test_browser_context.h" | 21 #include "content/public/test/test_browser_context.h" |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 BackgroundSyncError* out_error, | 84 BackgroundSyncError* out_error, |
| 87 unsigned long* out_array_size, | 85 unsigned long* out_array_size, |
| 88 BackgroundSyncError error, | 86 BackgroundSyncError error, |
| 89 mojo::Array<content::SyncRegistrationPtr> registrations) { | 87 mojo::Array<content::SyncRegistrationPtr> registrations) { |
| 90 *called = true; | 88 *called = true; |
| 91 *out_error = error; | 89 *out_error = error; |
| 92 if (error == BackgroundSyncError::NONE) | 90 if (error == BackgroundSyncError::NONE) |
| 93 *out_array_size = registrations.size(); | 91 *out_array_size = registrations.size(); |
| 94 } | 92 } |
| 95 | 93 |
| 96 class MockPowerMonitorSource : public base::PowerMonitorSource { | |
| 97 private: | |
| 98 // PowerMonitorSource overrides. | |
| 99 bool IsOnBatteryPowerImpl() final { return false; } | |
| 100 }; | |
| 101 | |
| 102 } // namespace | 94 } // namespace |
| 103 | 95 |
| 104 class BackgroundSyncServiceImplTest : public testing::Test { | 96 class BackgroundSyncServiceImplTest : public testing::Test { |
| 105 public: | 97 public: |
| 106 BackgroundSyncServiceImplTest() | 98 BackgroundSyncServiceImplTest() |
| 107 : thread_bundle_( | 99 : thread_bundle_( |
| 108 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 100 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
| 109 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { | 101 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { |
| 110 default_sync_registration_ = SyncRegistration::New(); | 102 default_sync_registration_ = SyncRegistration::New(); |
| 111 } | 103 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // use it to access the BrowserContext. | 136 // use it to access the BrowserContext. |
| 145 storage_partition_impl_.reset(new StoragePartitionImpl( | 137 storage_partition_impl_.reset(new StoragePartitionImpl( |
| 146 embedded_worker_helper_->browser_context(), base::FilePath(), nullptr, | 138 embedded_worker_helper_->browser_context(), base::FilePath(), nullptr, |
| 147 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, | 139 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
| 148 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)); | 140 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 149 embedded_worker_helper_->context_wrapper()->set_storage_partition( | 141 embedded_worker_helper_->context_wrapper()->set_storage_partition( |
| 150 storage_partition_impl_.get()); | 142 storage_partition_impl_.get()); |
| 151 } | 143 } |
| 152 | 144 |
| 153 void CreateBackgroundSyncContext() { | 145 void CreateBackgroundSyncContext() { |
| 154 power_monitor_.reset( | |
| 155 new base::PowerMonitor(make_scoped_ptr(new MockPowerMonitorSource()))); | |
| 156 | |
| 157 background_sync_context_ = new BackgroundSyncContextImpl(); | 146 background_sync_context_ = new BackgroundSyncContextImpl(); |
| 158 background_sync_context_->Init(embedded_worker_helper_->context_wrapper()); | 147 background_sync_context_->Init(embedded_worker_helper_->context_wrapper()); |
| 159 | 148 |
| 160 // Tests do not expect the sync event to fire immediately after | 149 // Tests do not expect the sync event to fire immediately after |
| 161 // register (and cleanup up the sync registrations). Prevent the sync | 150 // register (and cleanup up the sync registrations). Prevent the sync |
| 162 // event from firing by setting the network state to have no connection. | 151 // event from firing by setting the network state to have no connection. |
| 163 // NOTE: The setup of the network connection must happen after the | 152 // NOTE: The setup of the network connection must happen after the |
| 164 // BackgroundSyncManager has been setup, including any asynchronous | 153 // BackgroundSyncManager has been setup, including any asynchronous |
| 165 // initialization. | 154 // initialization. |
| 166 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 int32_t handle_id, | 227 int32_t handle_id, |
| 239 const BackgroundSyncService::NotifyWhenFinishedCallback& callback) { | 228 const BackgroundSyncService::NotifyWhenFinishedCallback& callback) { |
| 240 service_impl_->NotifyWhenFinished(handle_id, callback); | 229 service_impl_->NotifyWhenFinished(handle_id, callback); |
| 241 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
| 242 } | 231 } |
| 243 | 232 |
| 244 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; | 233 scoped_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 245 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 234 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 246 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; | 235 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 247 scoped_ptr<StoragePartitionImpl> storage_partition_impl_; | 236 scoped_ptr<StoragePartitionImpl> storage_partition_impl_; |
| 248 scoped_ptr<base::PowerMonitor> power_monitor_; | |
| 249 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; | 237 scoped_refptr<BackgroundSyncContextImpl> background_sync_context_; |
| 250 int64_t sw_registration_id_; | 238 int64_t sw_registration_id_; |
| 251 scoped_refptr<ServiceWorkerRegistration> sw_registration_; | 239 scoped_refptr<ServiceWorkerRegistration> sw_registration_; |
| 252 BackgroundSyncServicePtr service_ptr_; | 240 BackgroundSyncServicePtr service_ptr_; |
| 253 BackgroundSyncServiceImpl* | 241 BackgroundSyncServiceImpl* |
| 254 service_impl_; // Owned by background_sync_context_ | 242 service_impl_; // Owned by background_sync_context_ |
| 255 SyncRegistrationPtr default_sync_registration_; | 243 SyncRegistrationPtr default_sync_registration_; |
| 256 }; | 244 }; |
| 257 | 245 |
| 258 // Tests | 246 // Tests |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 374 |
| 387 NotifyWhenDone(reg->handle_id, | 375 NotifyWhenDone(reg->handle_id, |
| 388 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, | 376 base::Bind(&ErrorAndStateCallback, ¬ify_done_called, |
| 389 ¬ify_done_error, ¬ify_done_sync_state)); | 377 ¬ify_done_error, ¬ify_done_sync_state)); |
| 390 EXPECT_TRUE(notify_done_called); | 378 EXPECT_TRUE(notify_done_called); |
| 391 EXPECT_EQ(BackgroundSyncError::NONE, notify_done_error); | 379 EXPECT_EQ(BackgroundSyncError::NONE, notify_done_error); |
| 392 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, notify_done_sync_state); | 380 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, notify_done_sync_state); |
| 393 } | 381 } |
| 394 | 382 |
| 395 } // namespace content | 383 } // namespace content |
| OLD | NEW |