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
3 // found in the LICENSE file.
5 #include "content/browser/background_sync/background_sync_service_impl.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_source.h"
12 #include "base/run_loop.h"
13 #include "content/browser/background_sync/background_sync_context_impl.h"
14 #include "content/browser/service_worker/embedded_worker_test_helper.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "mojo/public/cpp/bindings/interface_ptr.h"
19 #include "net/base/network_change_notifier.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 const char kServiceWorkerPattern
[] = "https://example.com/a";
27 const char kServiceWorkerScript
[] = "https://example.com/a/script.js";
28 const int kRenderProcessId
= 99;
30 // Callbacks from SetUp methods
32 void RegisterServiceWorkerCallback(bool* called
,
33 int64
* store_registration_id
,
34 ServiceWorkerStatusCode status
,
35 const std::string
& status_message
,
36 int64 registration_id
) {
37 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
39 *store_registration_id
= registration_id
;
42 void FindServiceWorkerRegistrationCallback(
43 scoped_refptr
<ServiceWorkerRegistration
>* out_registration
,
44 ServiceWorkerStatusCode status
,
45 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
46 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
47 *out_registration
= registration
;
50 // Callbacks from BackgroundSyncServiceImpl methods
52 void ErrorAndRegistrationCallback(bool* called
,
53 BackgroundSyncError
* out_error
,
54 SyncRegistrationPtr
* out_registration
,
55 BackgroundSyncError error
,
56 const SyncRegistrationPtr
& registration
) {
59 *out_registration
= registration
.Clone();
62 void ErrorCallback(bool* called
,
63 BackgroundSyncError
* out_error
,
64 BackgroundSyncError error
) {
69 void ErrorAndRegistrationListCallback(
71 BackgroundSyncError
* out_error
,
72 unsigned long* out_array_size
,
73 BackgroundSyncError error
,
74 mojo::Array
<content::SyncRegistrationPtr
> registrations
) {
77 if (error
== BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
)
78 *out_array_size
= registrations
.size();
81 class MockPowerMonitorSource
: public base::PowerMonitorSource
{
83 // PowerMonitorSource overrides.
84 bool IsOnBatteryPowerImpl() final
{ return false; }
89 class BackgroundSyncServiceImplTest
: public testing::Test
{
91 BackgroundSyncServiceImplTest()
93 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP
)),
94 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {
95 default_sync_registration_
= SyncRegistration::New();
98 void SetUp() override
{
100 CreateBackgroundSyncContext();
101 CreateServiceWorkerRegistration();
102 CreateBackgroundSyncServiceImpl();
105 void TearDown() override
{
106 // This must be explicitly destroyed here to ensure that destruction
107 // of both the BackgroundSyncContext and the BackgroundSyncManager occurs on
108 // the correct thread.
109 background_sync_context_
= nullptr;
112 // SetUp helper methods
113 void CreateTestHelper() {
114 embedded_worker_helper_
.reset(
115 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
));
118 void CreateBackgroundSyncContext() {
119 power_monitor_
.reset(
120 new base::PowerMonitor(make_scoped_ptr(new MockPowerMonitorSource())));
122 background_sync_context_
= new BackgroundSyncContextImpl();
123 background_sync_context_
->Init(embedded_worker_helper_
->context_wrapper());
125 // Tests do not expect the sync event to fire immediately after
126 // register (and cleanup up the sync registrations). Prevent the sync
127 // 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 // BackgroundSyncManager has been setup.
130 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
131 net::NetworkChangeNotifier::CONNECTION_NONE
);
132 base::RunLoop().RunUntilIdle();
135 void CreateServiceWorkerRegistration() {
137 embedded_worker_helper_
->context()->RegisterServiceWorker(
138 GURL(kServiceWorkerPattern
), GURL(kServiceWorkerScript
), NULL
,
139 base::Bind(&RegisterServiceWorkerCallback
, &called
,
140 &sw_registration_id_
));
141 base::RunLoop().RunUntilIdle();
143 embedded_worker_helper_
->context_wrapper()->FindRegistrationForId(
144 sw_registration_id_
, GURL(kServiceWorkerPattern
).GetOrigin(),
145 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_
));
146 base::RunLoop().RunUntilIdle();
147 EXPECT_TRUE(sw_registration_
);
150 void CreateBackgroundSyncServiceImpl() {
151 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be
153 mojo::InterfaceRequest
<BackgroundSyncService
> service_request
=
154 mojo::GetProxy(&service_ptr_
);
155 // Create a new BackgroundSyncServiceImpl bound to the dummy channel
156 service_impl_
.reset(new BackgroundSyncServiceImpl(background_sync_context_
,
157 service_request
.Pass()));
158 base::RunLoop().RunUntilIdle();
161 // Helpers for testing BackgroundSyncServiceImpl methods
162 void RegisterOneShot(
163 SyncRegistrationPtr sync
,
164 const BackgroundSyncService::RegisterCallback
& callback
) {
165 service_impl_
->Register(sync
.Pass(), sw_registration_id_
, callback
);
166 base::RunLoop().RunUntilIdle();
169 void UnregisterOneShot(
170 SyncRegistrationPtr sync
,
171 const BackgroundSyncService::UnregisterCallback
& callback
) {
172 service_impl_
->Unregister(
173 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
,
174 sync
->id
, sync
->tag
, sw_registration_id_
, callback
);
175 base::RunLoop().RunUntilIdle();
178 void GetRegistrationOneShot(
179 const mojo::String
& tag
,
180 const BackgroundSyncService::RegisterCallback
& callback
) {
181 service_impl_
->GetRegistration(
182 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
, tag
,
183 sw_registration_id_
, callback
);
184 base::RunLoop().RunUntilIdle();
187 void GetRegistrationsOneShot(
188 const BackgroundSyncService::GetRegistrationsCallback
& callback
) {
189 service_impl_
->GetRegistrations(
190 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
,
191 sw_registration_id_
, callback
);
192 base::RunLoop().RunUntilIdle();
195 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
196 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
197 scoped_ptr
<EmbeddedWorkerTestHelper
> embedded_worker_helper_
;
198 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
199 scoped_refptr
<BackgroundSyncContextImpl
> background_sync_context_
;
200 int64 sw_registration_id_
;
201 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_
;
202 BackgroundSyncServicePtr service_ptr_
;
203 scoped_ptr
<BackgroundSyncServiceImpl
> service_impl_
;
204 SyncRegistrationPtr default_sync_registration_
;
209 TEST_F(BackgroundSyncServiceImplTest
, Register
) {
211 BackgroundSyncError error
;
212 SyncRegistrationPtr reg
;
214 default_sync_registration_
.Clone(),
215 base::Bind(&ErrorAndRegistrationCallback
, &called
, &error
, ®
));
217 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, error
);
218 EXPECT_EQ("", reg
->tag
);
221 TEST_F(BackgroundSyncServiceImplTest
, Unregister
) {
222 bool unregister_called
= false;
223 BackgroundSyncError unregister_error
;
224 SyncRegistrationPtr reg
;
226 default_sync_registration_
.Clone(),
227 base::Bind(&ErrorCallback
, &unregister_called
, &unregister_error
));
228 EXPECT_TRUE(unregister_called
);
229 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND
,
233 TEST_F(BackgroundSyncServiceImplTest
, UnregisterWithRegisteredSync
) {
234 bool register_called
= false;
235 bool unregister_called
= false;
236 BackgroundSyncError register_error
;
237 BackgroundSyncError unregister_error
;
238 SyncRegistrationPtr reg
;
239 RegisterOneShot(default_sync_registration_
.Clone(),
240 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
241 ®ister_error
, ®
));
242 EXPECT_TRUE(register_called
);
243 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
244 UnregisterOneShot(reg
.Pass(), base::Bind(&ErrorCallback
, &unregister_called
,
246 EXPECT_TRUE(unregister_called
);
247 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, unregister_error
);
250 TEST_F(BackgroundSyncServiceImplTest
, GetRegistration
) {
252 BackgroundSyncError error
;
253 SyncRegistrationPtr reg
;
254 GetRegistrationOneShot(
255 "", base::Bind(&ErrorAndRegistrationCallback
, &called
, &error
, ®
));
257 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND
, error
);
260 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrationWithRegisteredSync
) {
261 bool register_called
= false;
262 bool getregistration_called
= false;
263 BackgroundSyncError register_error
;
264 BackgroundSyncError getregistration_error
;
265 SyncRegistrationPtr register_reg
;
266 SyncRegistrationPtr getregistration_reg
;
267 RegisterOneShot(default_sync_registration_
.Clone(),
268 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
269 ®ister_error
, ®ister_reg
));
270 EXPECT_TRUE(register_called
);
271 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
272 GetRegistrationOneShot(
274 base::Bind(&ErrorAndRegistrationCallback
, &getregistration_called
,
275 &getregistration_error
, &getregistration_reg
));
276 EXPECT_TRUE(getregistration_called
);
277 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
,
278 getregistration_error
);
281 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrations
) {
283 BackgroundSyncError error
;
284 unsigned long array_size
= 0UL;
285 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback
, &called
,
286 &error
, &array_size
));
288 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, error
);
289 EXPECT_EQ(0UL, array_size
);
292 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrationsWithRegisteredSync
) {
293 bool register_called
= false;
294 bool getregistrations_called
= false;
295 BackgroundSyncError register_error
;
296 BackgroundSyncError getregistrations_error
;
297 SyncRegistrationPtr register_reg
;
298 unsigned long array_size
= 0UL;
299 RegisterOneShot(default_sync_registration_
.Clone(),
300 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
301 ®ister_error
, ®ister_reg
));
302 EXPECT_TRUE(register_called
);
303 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
304 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback
,
305 &getregistrations_called
,
306 &getregistrations_error
, &array_size
));
307 EXPECT_TRUE(getregistrations_called
);
308 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
,
309 getregistrations_error
);
310 EXPECT_EQ(1UL, array_size
);
313 } // namespace content