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, including any asynchronous
131 base::RunLoop().RunUntilIdle();
132 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
133 net::NetworkChangeNotifier::CONNECTION_NONE
);
134 base::RunLoop().RunUntilIdle();
137 void CreateServiceWorkerRegistration() {
139 embedded_worker_helper_
->context()->RegisterServiceWorker(
140 GURL(kServiceWorkerPattern
), GURL(kServiceWorkerScript
), NULL
,
141 base::Bind(&RegisterServiceWorkerCallback
, &called
,
142 &sw_registration_id_
));
143 base::RunLoop().RunUntilIdle();
146 // Register window client for the service worker
147 provider_host_
.reset(new ServiceWorkerProviderHost(
148 34 /* dummy render proces id */, MSG_ROUTING_NONE
/* render_frame_id */,
149 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
150 embedded_worker_helper_
->context()->AsWeakPtr(), nullptr));
151 provider_host_
->SetDocumentUrl(GURL(kServiceWorkerPattern
));
153 embedded_worker_helper_
->context_wrapper()->FindRegistrationForId(
154 sw_registration_id_
, GURL(kServiceWorkerPattern
).GetOrigin(),
155 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_
));
156 base::RunLoop().RunUntilIdle();
157 EXPECT_TRUE(sw_registration_
);
159 sw_registration_
->active_version()->AddControllee(provider_host_
.get());
162 void RemoveWindowClient() {
163 sw_registration_
->active_version()->RemoveControllee(provider_host_
.get());
166 void CreateBackgroundSyncServiceImpl() {
167 // Create a dummy mojo channel so that the BackgroundSyncServiceImpl can be
169 mojo::InterfaceRequest
<BackgroundSyncService
> service_request
=
170 mojo::GetProxy(&service_ptr_
);
171 // Create a new BackgroundSyncServiceImpl bound to the dummy channel
172 service_impl_
.reset(new BackgroundSyncServiceImpl(background_sync_context_
,
173 service_request
.Pass()));
174 base::RunLoop().RunUntilIdle();
177 // Helpers for testing BackgroundSyncServiceImpl methods
178 void RegisterOneShot(
179 SyncRegistrationPtr sync
,
180 const BackgroundSyncService::RegisterCallback
& callback
) {
181 service_impl_
->Register(sync
.Pass(), sw_registration_id_
, callback
);
182 base::RunLoop().RunUntilIdle();
185 void UnregisterOneShot(
186 SyncRegistrationPtr sync
,
187 const BackgroundSyncService::UnregisterCallback
& callback
) {
188 service_impl_
->Unregister(
189 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
,
190 sync
->id
, sync
->tag
, sw_registration_id_
, callback
);
191 base::RunLoop().RunUntilIdle();
194 void GetRegistrationOneShot(
195 const mojo::String
& tag
,
196 const BackgroundSyncService::RegisterCallback
& callback
) {
197 service_impl_
->GetRegistration(
198 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
, tag
,
199 sw_registration_id_
, callback
);
200 base::RunLoop().RunUntilIdle();
203 void GetRegistrationsOneShot(
204 const BackgroundSyncService::GetRegistrationsCallback
& callback
) {
205 service_impl_
->GetRegistrations(
206 BackgroundSyncPeriodicity::BACKGROUND_SYNC_PERIODICITY_ONE_SHOT
,
207 sw_registration_id_
, callback
);
208 base::RunLoop().RunUntilIdle();
211 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
212 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
213 scoped_ptr
<EmbeddedWorkerTestHelper
> embedded_worker_helper_
;
214 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
215 scoped_refptr
<BackgroundSyncContextImpl
> background_sync_context_
;
216 scoped_ptr
<ServiceWorkerProviderHost
> provider_host_
;
217 int64 sw_registration_id_
;
218 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_
;
219 BackgroundSyncServicePtr service_ptr_
;
220 scoped_ptr
<BackgroundSyncServiceImpl
> service_impl_
;
221 SyncRegistrationPtr default_sync_registration_
;
226 TEST_F(BackgroundSyncServiceImplTest
, Register
) {
228 BackgroundSyncError error
;
229 SyncRegistrationPtr reg
;
231 default_sync_registration_
.Clone(),
232 base::Bind(&ErrorAndRegistrationCallback
, &called
, &error
, ®
));
234 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, error
);
235 EXPECT_EQ("", reg
->tag
);
238 TEST_F(BackgroundSyncServiceImplTest
, RegisterWithoutWindow
) {
240 BackgroundSyncError error
;
241 SyncRegistrationPtr reg
;
242 RemoveWindowClient();
244 default_sync_registration_
.Clone(),
245 base::Bind(&ErrorAndRegistrationCallback
, &called
, &error
, ®
));
247 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_ALLOWED
, error
);
250 TEST_F(BackgroundSyncServiceImplTest
, Unregister
) {
251 bool unregister_called
= false;
252 BackgroundSyncError unregister_error
;
253 SyncRegistrationPtr reg
;
255 default_sync_registration_
.Clone(),
256 base::Bind(&ErrorCallback
, &unregister_called
, &unregister_error
));
257 EXPECT_TRUE(unregister_called
);
258 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND
,
262 TEST_F(BackgroundSyncServiceImplTest
, UnregisterWithRegisteredSync
) {
263 bool register_called
= false;
264 bool unregister_called
= false;
265 BackgroundSyncError register_error
;
266 BackgroundSyncError unregister_error
;
267 SyncRegistrationPtr reg
;
268 RegisterOneShot(default_sync_registration_
.Clone(),
269 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
270 ®ister_error
, ®
));
271 EXPECT_TRUE(register_called
);
272 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
273 UnregisterOneShot(reg
.Pass(), base::Bind(&ErrorCallback
, &unregister_called
,
275 EXPECT_TRUE(unregister_called
);
276 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, unregister_error
);
279 TEST_F(BackgroundSyncServiceImplTest
, GetRegistration
) {
281 BackgroundSyncError error
;
282 SyncRegistrationPtr reg
;
283 GetRegistrationOneShot(
284 "", base::Bind(&ErrorAndRegistrationCallback
, &called
, &error
, ®
));
286 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NOT_FOUND
, error
);
289 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrationWithRegisteredSync
) {
290 bool register_called
= false;
291 bool getregistration_called
= false;
292 BackgroundSyncError register_error
;
293 BackgroundSyncError getregistration_error
;
294 SyncRegistrationPtr register_reg
;
295 SyncRegistrationPtr getregistration_reg
;
296 RegisterOneShot(default_sync_registration_
.Clone(),
297 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
298 ®ister_error
, ®ister_reg
));
299 EXPECT_TRUE(register_called
);
300 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
301 GetRegistrationOneShot(
303 base::Bind(&ErrorAndRegistrationCallback
, &getregistration_called
,
304 &getregistration_error
, &getregistration_reg
));
305 EXPECT_TRUE(getregistration_called
);
306 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
,
307 getregistration_error
);
310 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrations
) {
312 BackgroundSyncError error
;
313 unsigned long array_size
= 0UL;
314 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback
, &called
,
315 &error
, &array_size
));
317 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, error
);
318 EXPECT_EQ(0UL, array_size
);
321 TEST_F(BackgroundSyncServiceImplTest
, GetRegistrationsWithRegisteredSync
) {
322 bool register_called
= false;
323 bool getregistrations_called
= false;
324 BackgroundSyncError register_error
;
325 BackgroundSyncError getregistrations_error
;
326 SyncRegistrationPtr register_reg
;
327 unsigned long array_size
= 0UL;
328 RegisterOneShot(default_sync_registration_
.Clone(),
329 base::Bind(&ErrorAndRegistrationCallback
, ®ister_called
,
330 ®ister_error
, ®ister_reg
));
331 EXPECT_TRUE(register_called
);
332 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
, register_error
);
333 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback
,
334 &getregistrations_called
,
335 &getregistrations_error
, &array_size
));
336 EXPECT_TRUE(getregistrations_called
);
337 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE
,
338 getregistrations_error
);
339 EXPECT_EQ(1UL, array_size
);
342 } // namespace content