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_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
11 #include "base/power_monitor/power_monitor_source.h" | 11 #include "base/power_monitor/power_monitor_source.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "content/browser/browser_thread_impl.h" | 15 #include "content/browser/browser_thread_impl.h" |
16 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
17 #include "content/browser/service_worker/service_worker_context_core.h" | 17 #include "content/browser/service_worker/service_worker_context_core.h" |
18 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
19 #include "content/browser/service_worker/service_worker_storage.h" | 19 #include "content/browser/service_worker/service_worker_storage.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "content/public/test/test_utils.h" |
21 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 const char kPattern1[] = "https://example.com/a"; | 29 const char kPattern1[] = "https://example.com/a"; |
29 const char kPattern2[] = "https://example.com/b"; | 30 const char kPattern2[] = "https://example.com/b"; |
30 const char kScript1[] = "https://example.com/a/script.js"; | 31 const char kScript1[] = "https://example.com/a/script.js"; |
31 const char kScript2[] = "https://example.com/b/script.js"; | 32 const char kScript2[] = "https://example.com/b/script.js"; |
32 const int kRenderProcessId = 99; | |
33 | 33 |
34 void RegisterServiceWorkerCallback(bool* called, | 34 void RegisterServiceWorkerCallback( |
35 int64* store_registration_id, | 35 const scoped_refptr<MessageLoopRunner>& runner, |
36 ServiceWorkerStatusCode status, | 36 int64* store_registration_id, |
37 const std::string& status_message, | 37 ServiceWorkerStatusCode status, |
38 int64 registration_id) { | 38 const std::string& status_message, |
| 39 int64 registration_id) { |
39 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 40 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
40 *called = true; | |
41 *store_registration_id = registration_id; | 41 *store_registration_id = registration_id; |
| 42 runner->Quit(); |
42 } | 43 } |
43 | 44 |
44 void FindServiceWorkerRegistrationCallback( | 45 void FindServiceWorkerRegistrationCallback( |
45 scoped_refptr<ServiceWorkerRegistration>* out_registration, | 46 scoped_refptr<ServiceWorkerRegistration>* out_registration, |
46 ServiceWorkerStatusCode status, | 47 ServiceWorkerStatusCode status, |
47 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 48 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
48 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 49 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
49 *out_registration = registration; | 50 *out_registration = registration; |
50 } | 51 } |
51 | 52 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 sync_options_1_.network_state = NETWORK_STATE_ONLINE; | 214 sync_options_1_.network_state = NETWORK_STATE_ONLINE; |
214 sync_options_1_.power_state = POWER_STATE_AUTO; | 215 sync_options_1_.power_state = POWER_STATE_AUTO; |
215 | 216 |
216 sync_options_2_.tag = "bar"; | 217 sync_options_2_.tag = "bar"; |
217 sync_options_2_.periodicity = SYNC_ONE_SHOT; | 218 sync_options_2_.periodicity = SYNC_ONE_SHOT; |
218 sync_options_2_.network_state = NETWORK_STATE_ONLINE; | 219 sync_options_2_.network_state = NETWORK_STATE_ONLINE; |
219 sync_options_2_.power_state = POWER_STATE_AUTO; | 220 sync_options_2_.power_state = POWER_STATE_AUTO; |
220 } | 221 } |
221 | 222 |
222 void SetUp() override { | 223 void SetUp() override { |
223 helper_.reset( | 224 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
224 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | |
225 | 225 |
226 power_monitor_source_ = new TestPowerSource(); | 226 power_monitor_source_ = new TestPowerSource(); |
227 // power_monitor_ takes ownership of power_monitor_source. | 227 // power_monitor_ takes ownership of power_monitor_source. |
228 power_monitor_.reset(new base::PowerMonitor( | 228 power_monitor_.reset(new base::PowerMonitor( |
229 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); | 229 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); |
230 | 230 |
231 SetOnBatteryPower(false); | 231 SetOnBatteryPower(false); |
232 | 232 |
233 SetupBackgroundSyncManager(); | 233 SetupBackgroundSyncManager(); |
234 | 234 |
235 // Wait for storage to finish initializing before registering service | 235 // Wait for storage to finish initializing before registering service |
236 // workers. | 236 // workers. |
237 base::RunLoop().RunUntilIdle(); | 237 base::RunLoop().RunUntilIdle(); |
238 RegisterServiceWorkers(); | 238 RegisterServiceWorkers(); |
239 } | 239 } |
240 | 240 |
241 void RegisterServiceWorkers() { | 241 void RegisterServiceWorkers() { |
242 bool called_1 = false; | 242 scoped_refptr<MessageLoopRunner> runner_1(new MessageLoopRunner); |
243 bool called_2 = false; | 243 scoped_refptr<MessageLoopRunner> runner_2(new MessageLoopRunner); |
244 helper_->context()->RegisterServiceWorker( | 244 helper_->context()->RegisterServiceWorker( |
245 GURL(kPattern1), GURL(kScript1), NULL, | 245 GURL(kPattern1), GURL(kScript1), NULL, |
246 base::Bind(&RegisterServiceWorkerCallback, &called_1, | 246 base::Bind(&RegisterServiceWorkerCallback, runner_1, |
247 &sw_registration_id_1_)); | 247 &sw_registration_id_1_)); |
248 | 248 |
249 helper_->context()->RegisterServiceWorker( | 249 helper_->context()->RegisterServiceWorker( |
250 GURL(kPattern2), GURL(kScript2), NULL, | 250 GURL(kPattern2), GURL(kScript2), NULL, |
251 base::Bind(&RegisterServiceWorkerCallback, &called_2, | 251 base::Bind(&RegisterServiceWorkerCallback, runner_2, |
252 &sw_registration_id_2_)); | 252 &sw_registration_id_2_)); |
253 base::RunLoop().RunUntilIdle(); | 253 |
254 EXPECT_TRUE(called_1); | 254 runner_1->Run(); |
255 EXPECT_TRUE(called_2); | 255 runner_2->Run(); |
256 | 256 |
257 // Hang onto the registrations as they need to be "live" when | 257 // Hang onto the registrations as they need to be "live" when |
258 // calling BackgroundSyncManager::Register. | 258 // calling BackgroundSyncManager::Register. |
259 helper_->context_wrapper()->FindRegistrationForId( | 259 helper_->context_wrapper()->FindRegistrationForId( |
260 sw_registration_id_1_, GURL(kPattern1).GetOrigin(), | 260 sw_registration_id_1_, GURL(kPattern1).GetOrigin(), |
261 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_)); | 261 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_)); |
262 | 262 |
263 helper_->context_wrapper()->FindRegistrationForId( | 263 helper_->context_wrapper()->FindRegistrationForId( |
264 sw_registration_id_2_, GURL(kPattern1).GetOrigin(), | 264 sw_registration_id_2_, GURL(kPattern1).GetOrigin(), |
265 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_)); | 265 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_)); |
(...skipping 945 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1211 RegisterAndVerifySyncEventDelayed(sync_options_1_); | 1211 RegisterAndVerifySyncEventDelayed(sync_options_1_); |
1212 | 1212 |
1213 // Create a new manager which should fire the sync again on init. | 1213 // Create a new manager which should fire the sync again on init. |
1214 SetupBackgroundSyncManager(); | 1214 SetupBackgroundSyncManager(); |
1215 InitSyncEventTest(); | 1215 InitSyncEventTest(); |
1216 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 1216 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
1217 EXPECT_EQ(2, sync_events_called_); | 1217 EXPECT_EQ(2, sync_events_called_); |
1218 } | 1218 } |
1219 | 1219 |
1220 } // namespace content | 1220 } // namespace content |
OLD | NEW |