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

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

Issue 214383005: Browser side of ServiceWorker requestSyncEvents() function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up the testing code Created 6 years, 8 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "content/browser/service_worker/embedded_worker_instance.h" 9 #include "content/browser/service_worker/embedded_worker_instance.h"
10 #include "content/browser/service_worker/embedded_worker_registry.h" 10 #include "content/browser/service_worker/embedded_worker_registry.h"
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 base::RunLoop run_loop; 269 base::RunLoop run_loop;
270 BrowserThread::PostTask( 270 BrowserThread::PostTask(
271 BrowserThread::IO, 271 BrowserThread::IO,
272 FROM_HERE, 272 FROM_HERE,
273 base::Bind( 273 base::Bind(
274 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); 274 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status));
275 run_loop.Run(); 275 run_loop.Run();
276 ASSERT_EQ(expected_status, status); 276 ASSERT_EQ(expected_status, status);
277 } 277 }
278 278
279 void FetchOnRegisteredWorker(ServiceWorkerFetchEventResult* result, 279 bool FetchOnRegisteredWorker(ServiceWorkerFetchEventResult* result,
280 ServiceWorkerResponse* response) { 280 ServiceWorkerResponse* response) {
281 FetchResult fetch_result; 281 FetchResult fetch_result;
282 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; 282 fetch_result.status = SERVICE_WORKER_ERROR_FAILED;
283 base::RunLoop fetch_run_loop; 283 base::RunLoop fetch_run_loop;
284 BrowserThread::PostTask(BrowserThread::IO, 284 BrowserThread::PostTask(BrowserThread::IO,
285 FROM_HERE, 285 FROM_HERE,
286 base::Bind(&self::FetchOnIOThread, 286 base::Bind(&self::FetchOnIOThread,
287 this, 287 this,
288 fetch_run_loop.QuitClosure(), 288 fetch_run_loop.QuitClosure(),
289 &fetch_result)); 289 &fetch_result));
290 fetch_run_loop.Run(); 290 fetch_run_loop.Run();
291 *result = fetch_result.result; 291 *result = fetch_result.result;
292 *response = fetch_result.response; 292 *response = fetch_result.response;
293 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); 293 return fetch_result.status == SERVICE_WORKER_OK;
294 } 294 }
295 295
296 void FetchTestHelper(const std::string& worker_url, 296 void FetchTestHelper(const std::string& worker_url,
297 ServiceWorkerFetchEventResult* result, 297 ServiceWorkerFetchEventResult* result,
298 ServiceWorkerResponse* response) { 298 ServiceWorkerResponse* response) {
299 RunOnIOThread( 299 RunOnIOThread(
300 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); 300 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url));
301 301
302 FetchOnRegisteredWorker(result, response); 302 FetchOnRegisteredWorker(result, response);
303 } 303 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); 353 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result));
354 } 354 }
355 355
356 void SyncEventOnIOThread(const base::Closure& done, 356 void SyncEventOnIOThread(const base::Closure& done,
357 ServiceWorkerStatusCode* result) { 357 ServiceWorkerStatusCode* result) {
358 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 358 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
359 version_->DispatchSyncEvent( 359 version_->DispatchSyncEvent(
360 CreateReceiver(BrowserThread::UI, done, result)); 360 CreateReceiver(BrowserThread::UI, done, result));
361 } 361 }
362 362
363 bool FireSyncEvent() {
364 // Run the sync event.
365 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
366 base::RunLoop sync_run_loop;
367 BrowserThread::PostTask(BrowserThread::IO,
368 FROM_HERE,
369 base::Bind(&self::SyncEventOnIOThread,
370 this,
371 sync_run_loop.QuitClosure(),
372 &status));
373 sync_run_loop.Run();
374 return status == SERVICE_WORKER_OK;
375 }
376
363 protected: 377 protected:
364 int64 next_registration_id_; 378 int64 next_registration_id_;
365 scoped_refptr<ServiceWorkerRegistration> registration_; 379 scoped_refptr<ServiceWorkerRegistration> registration_;
366 scoped_refptr<ServiceWorkerVersion> version_; 380 scoped_refptr<ServiceWorkerVersion> version_;
367 }; 381 };
368 382
369 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { 383 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) {
370 // Start a worker and wait until OnStarted() is called. 384 // Start a worker and wait until OnStarted() is called.
371 base::RunLoop start_run_loop; 385 base::RunLoop start_run_loop;
372 done_closure_ = start_run_loop.QuitClosure(); 386 done_closure_ = start_run_loop.QuitClosure();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); 488 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
475 } 489 }
476 490
477 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) { 491 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) {
478 ServiceWorkerFetchEventResult result; 492 ServiceWorkerFetchEventResult result;
479 ServiceWorkerResponse response; 493 ServiceWorkerResponse response;
480 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response); 494 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response);
481 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); 495 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
482 } 496 }
483 497
484 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventHandled) { 498 // Show that sync works and that the requestSyncEvents function appropriately
485 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 499 // controls sync events.
486 "/service_worker/sync.js")); 500 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
501 SyncEventAndRequestFunction) {
502 RunOnIOThread(base::Bind(
503 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js"));
487 ServiceWorkerFetchEventResult result; 504 ServiceWorkerFetchEventResult result;
488 ServiceWorkerResponse response; 505 ServiceWorkerResponse response;
489 506
490 // Should 404 before sync event. 507 // Fetch will return 200 if the sync event was fired, else 404.
491 FetchOnRegisteredWorker(&result, &response); 508 // Each fetch event will cause requestSyncEvent to be called with
509 // different parameters, as described in the comments below.
510 // The simpler ways to do this (e.g., putting the command in the URL)
511 // don't work because at the time of writing SWs don't actually pass
512 // the URL to the fetch event.
513
514 EXPECT_FALSE(FireSyncEvent()); // Should not fire, next response 404.
515 EXPECT_TRUE(FetchOnRegisteredWorker(&result,
516 &response)); // Calls requestSyncEvents()
492 EXPECT_EQ(404, response.status_code); 517 EXPECT_EQ(404, response.status_code);
493 518
494 // Run the sync event. 519 EXPECT_FALSE(FireSyncEvent()); // Should not fire, next response 404.
495 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 520 EXPECT_TRUE(
496 base::RunLoop sync_run_loop; 521 FetchOnRegisteredWorker(&result,
497 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 522 &response)); // Calls requestSyncEvents(true)
498 base::Bind(&self::SyncEventOnIOThread, this, 523 EXPECT_EQ(404, response.status_code);
499 sync_run_loop.QuitClosure(),
500 &status));
501 sync_run_loop.Run();
502 ASSERT_EQ(SERVICE_WORKER_OK, status);
503 524
504 // Should 200 after sync event. 525 EXPECT_TRUE(FireSyncEvent()); // Should fire, next response 200.
505 FetchOnRegisteredWorker(&result, &response); 526 EXPECT_TRUE(FetchOnRegisteredWorker(
527 &result, &response)); // Calls requestSyncEvents(false)
506 EXPECT_EQ(200, response.status_code); 528 EXPECT_EQ(200, response.status_code);
529
530 EXPECT_FALSE(FireSyncEvent()); // Should fire, next response 404.
531 EXPECT_TRUE(FetchOnRegisteredWorker(&result, &response));
532 EXPECT_EQ(404, response.status_code);
507 } 533 }
508 534
509 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { 535 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest {
510 public: 536 public:
511 typedef ServiceWorkerBlackBoxBrowserTest self; 537 typedef ServiceWorkerBlackBoxBrowserTest self;
512 538
513 static void ExpectResultAndRun(bool expected, 539 static void ExpectResultAndRun(bool expected,
514 const base::Closure& continuation, 540 const base::Closure& continuation,
515 bool actual) { 541 bool actual) {
516 EXPECT_EQ(expected, actual); 542 EXPECT_EQ(expected, actual);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, 618 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO,
593 this, 619 this,
594 embedded_test_server()->GetURL("/service_worker/empty.html"), 620 embedded_test_server()->GetURL("/service_worker/empty.html"),
595 &status, 621 &status,
596 &script_url)); 622 &script_url));
597 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); 623 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status);
598 } 624 }
599 } 625 }
600 626
601 } // namespace content 627 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698