DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_service_impl_unittest.cc
blob2ffac177abd61f9ba3a162af5effc9073668e847
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"
7 #include "base/bind.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"
22 namespace content {
24 namespace {
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);
38 *called = true;
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) {
57 *called = true;
58 *out_error = error;
59 *out_registration = registration.Clone();
62 void ErrorCallback(bool* called,
63 BackgroundSyncError* out_error,
64 BackgroundSyncError error) {
65 *called = true;
66 *out_error = error;
69 void ErrorAndRegistrationListCallback(
70 bool* called,
71 BackgroundSyncError* out_error,
72 unsigned long* out_array_size,
73 BackgroundSyncError error,
74 mojo::Array<content::SyncRegistrationPtr> registrations) {
75 *called = true;
76 *out_error = error;
77 if (error == BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE)
78 *out_array_size = registrations.size();
81 class MockPowerMonitorSource : public base::PowerMonitorSource {
82 private:
83 // PowerMonitorSource overrides.
84 bool IsOnBatteryPowerImpl() final { return false; }
87 } // namespace
89 class BackgroundSyncServiceImplTest : public testing::Test {
90 public:
91 BackgroundSyncServiceImplTest()
92 : thread_bundle_(
93 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)),
94 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {
95 default_sync_registration_ = SyncRegistration::New();
98 void SetUp() override {
99 CreateTestHelper();
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() {
136 bool called = false;
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();
142 EXPECT_TRUE(called);
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
152 // instantiated
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_;
207 // Tests
209 TEST_F(BackgroundSyncServiceImplTest, Register) {
210 bool called = false;
211 BackgroundSyncError error;
212 SyncRegistrationPtr reg;
213 RegisterOneShot(
214 default_sync_registration_.Clone(),
215 base::Bind(&ErrorAndRegistrationCallback, &called, &error, &reg));
216 EXPECT_TRUE(called);
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;
225 UnregisterOneShot(
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,
230 unregister_error);
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, &register_called,
241 &register_error, &reg));
242 EXPECT_TRUE(register_called);
243 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, register_error);
244 UnregisterOneShot(reg.Pass(), base::Bind(&ErrorCallback, &unregister_called,
245 &unregister_error));
246 EXPECT_TRUE(unregister_called);
247 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, unregister_error);
250 TEST_F(BackgroundSyncServiceImplTest, GetRegistration) {
251 bool called = false;
252 BackgroundSyncError error;
253 SyncRegistrationPtr reg;
254 GetRegistrationOneShot(
255 "", base::Bind(&ErrorAndRegistrationCallback, &called, &error, &reg));
256 EXPECT_TRUE(called);
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, &register_called,
269 &register_error, &register_reg));
270 EXPECT_TRUE(register_called);
271 EXPECT_EQ(BackgroundSyncError::BACKGROUND_SYNC_ERROR_NONE, register_error);
272 GetRegistrationOneShot(
273 register_reg->tag,
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) {
282 bool called = false;
283 BackgroundSyncError error;
284 unsigned long array_size = 0UL;
285 GetRegistrationsOneShot(base::Bind(&ErrorAndRegistrationListCallback, &called,
286 &error, &array_size));
287 EXPECT_TRUE(called);
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, &register_called,
301 &register_error, &register_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