Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: content/browser/service_worker/service_worker_registration_unittest.cc

Issue 2431313003: Mojofy unittests related to service workers (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/service_worker/service_worker_registration.h" 5 #include "content/browser/service_worker/service_worker_registration.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 scoped_refptr<ServiceWorkerRegistration> observed_registration_; 92 scoped_refptr<ServiceWorkerRegistration> observed_registration_;
93 ChangedVersionAttributesMask observed_changed_mask_; 93 ChangedVersionAttributesMask observed_changed_mask_;
94 ServiceWorkerRegistrationInfo observed_info_; 94 ServiceWorkerRegistrationInfo observed_info_;
95 }; 95 };
96 96
97 private: 97 private:
98 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 98 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
99 TestBrowserThreadBundle thread_bundle_; 99 TestBrowserThreadBundle thread_bundle_;
100 }; 100 };
101 101
102 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) { 102 class ServiceWorkerRegistrationTestP
103 : public MojoServiceWorkerTestP<ServiceWorkerRegistrationTest> {};
104
105 TEST_P(ServiceWorkerRegistrationTestP, SetAndUnsetVersions) {
103 const GURL kScope("http://www.example.not/"); 106 const GURL kScope("http://www.example.not/");
104 const GURL kScript("http://www.example.not/service_worker.js"); 107 const GURL kScript("http://www.example.not/service_worker.js");
105 int64_t kRegistrationId = 1L; 108 int64_t kRegistrationId = 1L;
106 scoped_refptr<ServiceWorkerRegistration> registration = 109 scoped_refptr<ServiceWorkerRegistration> registration =
107 new ServiceWorkerRegistration(kScope, kRegistrationId, 110 new ServiceWorkerRegistration(kScope, kRegistrationId,
108 context()->AsWeakPtr()); 111 context()->AsWeakPtr());
109 112
110 const int64_t version_1_id = 1L; 113 const int64_t version_1_id = 1L;
111 const int64_t version_2_id = 2L; 114 const int64_t version_2_id = 2L;
112 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion( 115 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 EXPECT_FALSE(registration->waiting_version()); 163 EXPECT_FALSE(registration->waiting_version());
161 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION, 164 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION,
162 listener.observed_changed_mask_.changed()); 165 listener.observed_changed_mask_.changed());
163 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); 166 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
164 EXPECT_EQ(listener.observed_info_.waiting_version.version_id, 167 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
165 kInvalidServiceWorkerVersionId); 168 kInvalidServiceWorkerVersionId);
166 EXPECT_EQ(listener.observed_info_.installing_version.version_id, 169 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
167 kInvalidServiceWorkerVersionId); 170 kInvalidServiceWorkerVersionId);
168 } 171 }
169 172
170 TEST_F(ServiceWorkerRegistrationTest, FailedRegistrationNoCrash) { 173 TEST_P(ServiceWorkerRegistrationTestP, FailedRegistrationNoCrash) {
171 const GURL kScope("http://www.example.not/"); 174 const GURL kScope("http://www.example.not/");
172 int64_t kRegistrationId = 1L; 175 int64_t kRegistrationId = 1L;
173 scoped_refptr<ServiceWorkerRegistration> registration = 176 scoped_refptr<ServiceWorkerRegistration> registration =
174 new ServiceWorkerRegistration(kScope, kRegistrationId, 177 new ServiceWorkerRegistration(kScope, kRegistrationId,
175 context()->AsWeakPtr()); 178 context()->AsWeakPtr());
176 std::unique_ptr<ServiceWorkerRegistrationHandle> handle( 179 std::unique_ptr<ServiceWorkerRegistrationHandle> handle(
177 new ServiceWorkerRegistrationHandle( 180 new ServiceWorkerRegistrationHandle(
178 context()->AsWeakPtr(), base::WeakPtr<ServiceWorkerProviderHost>(), 181 context()->AsWeakPtr(), base::WeakPtr<ServiceWorkerProviderHost>(),
179 registration.get())); 182 registration.get()));
180 registration->NotifyRegistrationFailed(); 183 registration->NotifyRegistrationFailed();
181 // Don't crash when handle gets destructed. 184 // Don't crash when handle gets destructed.
182 } 185 }
183 186
184 // Sets up a registration with a waiting worker, and an active worker 187 // Sets up a registration with a waiting worker, and an active worker
185 // with a controllee and an inflight request. 188 // with a controllee and an inflight request.
186 class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTest { 189 class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTestP {
187 public: 190 public:
188 ServiceWorkerActivationTest() : ServiceWorkerRegistrationTest() {} 191 ServiceWorkerActivationTest() : ServiceWorkerRegistrationTestP() {}
189 192
190 void SetUp() override { 193 void SetUp() override {
191 ServiceWorkerRegistrationTest::SetUp(); 194 ServiceWorkerRegistrationTest::SetUp();
192 195
193 const GURL kScope("https://www.example.not/"); 196 const GURL kScope("https://www.example.not/");
194 const GURL kScript("https://www.example.not/service_worker.js"); 197 const GURL kScript("https://www.example.not/service_worker.js");
195 198
196 registration_ = new ServiceWorkerRegistration( 199 registration_ = new ServiceWorkerRegistration(
197 kScope, storage()->NewRegistrationId(), context()->AsWeakPtr()); 200 kScope, storage()->NewRegistrationId(), context()->AsWeakPtr());
198 201
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 ServiceWorkerProviderHost* controllee() { return host_.get(); } 257 ServiceWorkerProviderHost* controllee() { return host_.get(); }
255 int inflight_request_id() const { return inflight_request_id_; } 258 int inflight_request_id() const { return inflight_request_id_; }
256 259
257 private: 260 private:
258 scoped_refptr<ServiceWorkerRegistration> registration_; 261 scoped_refptr<ServiceWorkerRegistration> registration_;
259 std::unique_ptr<ServiceWorkerProviderHost> host_; 262 std::unique_ptr<ServiceWorkerProviderHost> host_;
260 int inflight_request_id_ = -1; 263 int inflight_request_id_ = -1;
261 }; 264 };
262 265
263 // Test activation triggered by finishing all requests. 266 // Test activation triggered by finishing all requests.
264 TEST_F(ServiceWorkerActivationTest, NoInflightRequest) { 267 TEST_P(ServiceWorkerActivationTest, NoInflightRequest) {
265 scoped_refptr<ServiceWorkerRegistration> reg = registration(); 268 scoped_refptr<ServiceWorkerRegistration> reg = registration();
266 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); 269 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version();
267 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); 270 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version();
268 271
269 // Remove the controllee. Since there is an in-flight request, 272 // Remove the controllee. Since there is an in-flight request,
270 // activation should not yet happen. 273 // activation should not yet happen.
271 version_1->RemoveControllee(controllee()); 274 version_1->RemoveControllee(controllee());
272 base::RunLoop().RunUntilIdle(); 275 base::RunLoop().RunUntilIdle();
273 EXPECT_EQ(version_1.get(), reg->active_version()); 276 EXPECT_EQ(version_1.get(), reg->active_version());
274 277
275 // Finish the request. Activation should happen. 278 // Finish the request. Activation should happen.
276 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, 279 version_1->FinishRequest(inflight_request_id(), true /* was_handled */,
277 base::Time::Now()); 280 base::Time::Now());
278 base::RunLoop().RunUntilIdle(); 281 base::RunLoop().RunUntilIdle();
279 EXPECT_EQ(version_2.get(), reg->active_version()); 282 EXPECT_EQ(version_2.get(), reg->active_version());
280 } 283 }
281 284
282 // Test activation triggered by loss of controllee. 285 // Test activation triggered by loss of controllee.
283 TEST_F(ServiceWorkerActivationTest, NoControllee) { 286 TEST_P(ServiceWorkerActivationTest, NoControllee) {
284 scoped_refptr<ServiceWorkerRegistration> reg = registration(); 287 scoped_refptr<ServiceWorkerRegistration> reg = registration();
285 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); 288 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version();
286 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); 289 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version();
287 290
288 // Finish the request. Since there is a controllee, activation should not yet 291 // Finish the request. Since there is a controllee, activation should not yet
289 // happen. 292 // happen.
290 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, 293 version_1->FinishRequest(inflight_request_id(), true /* was_handled */,
291 base::Time::Now()); 294 base::Time::Now());
292 base::RunLoop().RunUntilIdle(); 295 base::RunLoop().RunUntilIdle();
293 EXPECT_EQ(version_1.get(), reg->active_version()); 296 EXPECT_EQ(version_1.get(), reg->active_version());
294 297
295 // Remove the controllee. Activation should happen. 298 // Remove the controllee. Activation should happen.
296 version_1->RemoveControllee(controllee()); 299 version_1->RemoveControllee(controllee());
297 base::RunLoop().RunUntilIdle(); 300 base::RunLoop().RunUntilIdle();
298 EXPECT_EQ(version_2.get(), reg->active_version()); 301 EXPECT_EQ(version_2.get(), reg->active_version());
299 } 302 }
300 303
301 // Test activation triggered by skipWaiting. 304 // Test activation triggered by skipWaiting.
302 TEST_F(ServiceWorkerActivationTest, SkipWaiting) { 305 TEST_P(ServiceWorkerActivationTest, SkipWaiting) {
303 scoped_refptr<ServiceWorkerRegistration> reg = registration(); 306 scoped_refptr<ServiceWorkerRegistration> reg = registration();
304 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); 307 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version();
305 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); 308 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version();
306 309
307 // Finish the in-flight request. Since there is a controllee, 310 // Finish the in-flight request. Since there is a controllee,
308 // activation should not happen. 311 // activation should not happen.
309 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, 312 version_1->FinishRequest(inflight_request_id(), true /* was_handled */,
310 base::Time::Now()); 313 base::Time::Now());
311 base::RunLoop().RunUntilIdle(); 314 base::RunLoop().RunUntilIdle();
312 EXPECT_EQ(version_1.get(), reg->active_version()); 315 EXPECT_EQ(version_1.get(), reg->active_version());
313 316
314 // Call skipWaiting. Activation should happen. 317 // Call skipWaiting. Activation should happen.
315 version_2->OnSkipWaiting(77 /* dummy request_id */); 318 version_2->OnSkipWaiting(77 /* dummy request_id */);
316 base::RunLoop().RunUntilIdle(); 319 base::RunLoop().RunUntilIdle();
317 EXPECT_EQ(version_2.get(), reg->active_version()); 320 EXPECT_EQ(version_2.get(), reg->active_version());
318 } 321 }
319 322
320 // Test activation triggered by skipWaiting and finishing requests. 323 // Test activation triggered by skipWaiting and finishing requests.
321 TEST_F(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) { 324 TEST_P(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) {
322 scoped_refptr<ServiceWorkerRegistration> reg = registration(); 325 scoped_refptr<ServiceWorkerRegistration> reg = registration();
323 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); 326 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version();
324 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); 327 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version();
325 328
326 // Set skip waiting flag. Since there is still an in-flight request, 329 // Set skip waiting flag. Since there is still an in-flight request,
327 // activation should not happen. 330 // activation should not happen.
328 version_2->OnSkipWaiting(77 /* dummy request_id */); 331 version_2->OnSkipWaiting(77 /* dummy request_id */);
329 base::RunLoop().RunUntilIdle(); 332 base::RunLoop().RunUntilIdle();
330 EXPECT_EQ(version_1.get(), reg->active_version()); 333 EXPECT_EQ(version_1.get(), reg->active_version());
331 334
332 // Finish the request. Activation should happen. 335 // Finish the request. Activation should happen.
333 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, 336 version_1->FinishRequest(inflight_request_id(), true /* was_handled */,
334 base::Time::Now()); 337 base::Time::Now());
335 base::RunLoop().RunUntilIdle(); 338 base::RunLoop().RunUntilIdle();
336 EXPECT_EQ(version_2.get(), reg->active_version()); 339 EXPECT_EQ(version_2.get(), reg->active_version());
337 } 340 }
338 341
342 INSTANTIATE_TEST_CASE_P(ServiceWorkerRegistrationTest,
343 ServiceWorkerRegistrationTestP,
344 testing::Bool());
345
346 INSTANTIATE_TEST_CASE_P(ServiceWorkerActivationTest,
347 ServiceWorkerActivationTest,
348 testing::Bool());
349
339 } // namespace content 350 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698