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" |
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 "content/browser/background_sync/background_sync_context_impl.h" | 13 #include "content/browser/background_sync/background_sync_context_impl.h" |
14 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 14 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/test_utils.h" |
18 #include "mojo/public/cpp/bindings/interface_ptr.h" | 19 #include "mojo/public/cpp/bindings/interface_ptr.h" |
19 #include "net/base/network_change_notifier.h" | 20 #include "net/base/network_change_notifier.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const char kServiceWorkerPattern[] = "https://example.com/a"; | 27 const char kServiceWorkerPattern[] = "https://example.com/a"; |
27 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; | 28 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; |
28 const int kRenderProcessId = 99; | |
29 | 29 |
30 // Callbacks from SetUp methods | 30 // Callbacks from SetUp methods |
31 | 31 |
32 void RegisterServiceWorkerCallback(bool* called, | 32 void RegisterServiceWorkerCallback( |
33 int64* store_registration_id, | 33 const scoped_refptr<MessageLoopRunner>& runner, |
34 ServiceWorkerStatusCode status, | 34 int64* store_registration_id, |
35 const std::string& status_message, | 35 ServiceWorkerStatusCode status, |
36 int64 registration_id) { | 36 const std::string& status_message, |
| 37 int64 registration_id) { |
37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 38 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
38 *called = true; | |
39 *store_registration_id = registration_id; | 39 *store_registration_id = registration_id; |
| 40 runner->Quit(); |
40 } | 41 } |
41 | 42 |
42 void FindServiceWorkerRegistrationCallback( | 43 void FindServiceWorkerRegistrationCallback( |
43 scoped_refptr<ServiceWorkerRegistration>* out_registration, | 44 scoped_refptr<ServiceWorkerRegistration>* out_registration, |
44 ServiceWorkerStatusCode status, | 45 ServiceWorkerStatusCode status, |
45 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 46 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
46 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 47 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
47 *out_registration = registration; | 48 *out_registration = registration; |
48 } | 49 } |
49 | 50 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 void TearDown() override { | 106 void TearDown() override { |
106 // This must be explicitly destroyed here to ensure that destruction | 107 // This must be explicitly destroyed here to ensure that destruction |
107 // of both the BackgroundSyncContext and the BackgroundSyncManager occurs on | 108 // of both the BackgroundSyncContext and the BackgroundSyncManager occurs on |
108 // the correct thread. | 109 // the correct thread. |
109 background_sync_context_ = nullptr; | 110 background_sync_context_ = nullptr; |
110 } | 111 } |
111 | 112 |
112 // SetUp helper methods | 113 // SetUp helper methods |
113 void CreateTestHelper() { | 114 void CreateTestHelper() { |
114 embedded_worker_helper_.reset( | 115 embedded_worker_helper_.reset( |
115 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | 116 new EmbeddedWorkerTestHelper(base::FilePath())); |
116 } | 117 } |
117 | 118 |
118 void CreateBackgroundSyncContext() { | 119 void CreateBackgroundSyncContext() { |
119 power_monitor_.reset( | 120 power_monitor_.reset( |
120 new base::PowerMonitor(make_scoped_ptr(new MockPowerMonitorSource()))); | 121 new base::PowerMonitor(make_scoped_ptr(new MockPowerMonitorSource()))); |
121 | 122 |
122 background_sync_context_ = new BackgroundSyncContextImpl(); | 123 background_sync_context_ = new BackgroundSyncContextImpl(); |
123 background_sync_context_->Init(embedded_worker_helper_->context_wrapper()); | 124 background_sync_context_->Init(embedded_worker_helper_->context_wrapper()); |
124 | 125 |
125 // Tests do not expect the sync event to fire immediately after | 126 // Tests do not expect the sync event to fire immediately after |
126 // register (and cleanup up the sync registrations). Prevent the sync | 127 // register (and cleanup up the sync registrations). Prevent the sync |
127 // event from firing by setting the network state to have no connection. | 128 // event from firing by setting the network state to have no connection. |
128 // NOTE: The setup of the network connection must happen after the | 129 // NOTE: The setup of the network connection must happen after the |
129 // BackgroundSyncManager has been setup, including any asynchronous | 130 // BackgroundSyncManager has been setup, including any asynchronous |
130 // initialization. | 131 // initialization. |
131 base::RunLoop().RunUntilIdle(); | 132 base::RunLoop().RunUntilIdle(); |
132 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 133 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
133 net::NetworkChangeNotifier::CONNECTION_NONE); | 134 net::NetworkChangeNotifier::CONNECTION_NONE); |
134 base::RunLoop().RunUntilIdle(); | 135 base::RunLoop().RunUntilIdle(); |
135 } | 136 } |
136 | 137 |
137 void CreateServiceWorkerRegistration() { | 138 void CreateServiceWorkerRegistration() { |
138 bool called = false; | 139 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
139 embedded_worker_helper_->context()->RegisterServiceWorker( | 140 embedded_worker_helper_->context()->RegisterServiceWorker( |
140 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, | 141 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, |
141 base::Bind(&RegisterServiceWorkerCallback, &called, | 142 base::Bind(&RegisterServiceWorkerCallback, runner, |
142 &sw_registration_id_)); | 143 &sw_registration_id_)); |
143 base::RunLoop().RunUntilIdle(); | 144 runner->Run(); |
144 EXPECT_TRUE(called); | |
145 embedded_worker_helper_->context_wrapper()->FindRegistrationForId( | 145 embedded_worker_helper_->context_wrapper()->FindRegistrationForId( |
146 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), | 146 sw_registration_id_, GURL(kServiceWorkerPattern).GetOrigin(), |
147 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); | 147 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_)); |
148 base::RunLoop().RunUntilIdle(); | 148 base::RunLoop().RunUntilIdle(); |
149 EXPECT_TRUE(sw_registration_); | 149 EXPECT_TRUE(sw_registration_); |
150 } | 150 } |
151 | 151 |
152 void CreateBackgroundSyncServiceImpl() { | 152 void CreateBackgroundSyncServiceImpl() { |
153 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be | 153 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be |
154 // instantiated | 154 // instantiated |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, | 306 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, |
307 &getregistrations_called, | 307 &getregistrations_called, |
308 &getregistrations_error, &array_size)); | 308 &getregistrations_error, &array_size)); |
309 EXPECT_TRUE(getregistrations_called); | 309 EXPECT_TRUE(getregistrations_called); |
310 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, | 310 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, |
311 getregistrations_error); | 311 getregistrations_error); |
312 EXPECT_EQ(1UL, array_size); | 312 EXPECT_EQ(1UL, array_size); |
313 } | 313 } |
314 | 314 |
315 } // namespace content | 315 } // namespace content |
OLD | NEW |