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

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

Issue 1282013004: BackgroundSyncManager tracks client registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed test Created 5 years, 3 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 "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 *body = std::string(data->items()[0]->bytes(), data->items()[0]->length()); 164 *body = std::string(data->items()[0]->bytes(), data->items()[0]->length());
165 } 165 }
166 166
167 void ExpectResultAndRun(bool expected, 167 void ExpectResultAndRun(bool expected,
168 const base::Closure& continuation, 168 const base::Closure& continuation,
169 bool actual) { 169 bool actual) {
170 EXPECT_EQ(expected, actual); 170 EXPECT_EQ(expected, actual);
171 continuation.Run(); 171 continuation.Run();
172 } 172 }
173 173
174 SyncRegistrationPtr CreateOneShotSyncRegistration(const std::string& tag) {
175 SyncRegistrationPtr registration = SyncRegistration::New();
176 registration->tag = tag;
177 return registration.Pass();
178 }
179
180 class WorkerActivatedObserver 174 class WorkerActivatedObserver
181 : public ServiceWorkerContextObserver, 175 : public ServiceWorkerContextObserver,
182 public base::RefCountedThreadSafe<WorkerActivatedObserver> { 176 public base::RefCountedThreadSafe<WorkerActivatedObserver> {
183 public: 177 public:
184 explicit WorkerActivatedObserver(ServiceWorkerContextWrapper* context) 178 explicit WorkerActivatedObserver(ServiceWorkerContextWrapper* context)
185 : context_(context) {} 179 : context_(context) {}
186 void Init() { 180 void Init() {
187 RunOnIOThread(base::Bind(&WorkerActivatedObserver::InitOnIOThread, this)); 181 RunOnIOThread(base::Bind(&WorkerActivatedObserver::InitOnIOThread, this));
188 } 182 }
189 // ServiceWorkerContextObserver overrides. 183 // ServiceWorkerContextObserver overrides.
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 334
341 void AssociateRendererProcessToPattern(const GURL& pattern) { 335 void AssociateRendererProcessToPattern(const GURL& pattern) {
342 wrapper_->process_manager()->AddProcessReferenceToPattern( 336 wrapper_->process_manager()->AddProcessReferenceToPattern(
343 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID()); 337 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID());
344 } 338 }
345 339
346 private: 340 private:
347 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; 341 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
348 }; 342 };
349 343
350 class EmbeddedWorkerBrowserTest : public ServiceWorkerBrowserTest,
351 public EmbeddedWorkerInstance::Listener {
352 public:
353 using self = EmbeddedWorkerBrowserTest;
354
355 EmbeddedWorkerBrowserTest()
356 : last_worker_status_(EmbeddedWorkerInstance::STOPPED) {}
357 ~EmbeddedWorkerBrowserTest() override {}
358
359 void TearDownOnIOThread() override {
360 if (worker_) {
361 worker_->RemoveListener(this);
362 if (worker_->status() == EmbeddedWorkerInstance::STARTING ||
363 worker_->status() == EmbeddedWorkerInstance::RUNNING) {
364 worker_->Stop();
365 }
366 worker_.reset();
367 }
368 }
369
370 void SetUpCommandLine(base::CommandLine* command_line) override {
371 ServiceWorkerBrowserTest::SetUpCommandLine(command_line);
372
373 // Code caching requires a bit more infrastructure that we don't care
374 // about in this test.
375 command_line->AppendSwitchASCII(switches::kV8CacheOptions, "none");
376 }
377
378 void StartOnIOThread() {
379 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
380 worker_ = wrapper()->context()->embedded_worker_registry()->CreateWorker();
381 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status());
382 worker_->AddListener(this);
383
384 const int64 service_worker_version_id = 33L;
385 const GURL pattern = embedded_test_server()->GetURL("/");
386 const GURL script_url = embedded_test_server()->GetURL(
387 "/service_worker/worker.js");
388 AssociateRendererProcessToPattern(pattern);
389 int process_id = shell()->web_contents()->GetRenderProcessHost()->GetID();
390 wrapper()->process_manager()->AddProcessReferenceToPattern(
391 pattern, process_id);
392 worker_->Start(
393 service_worker_version_id,
394 pattern,
395 script_url,
396 base::Bind(&EmbeddedWorkerBrowserTest::StartOnIOThread2, this));
397 }
398
399 void StartOnIOThread2(ServiceWorkerStatusCode status) {
400 last_worker_status_ = worker_->status();
401 EXPECT_EQ(SERVICE_WORKER_OK, status);
402 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, last_worker_status_);
403
404 if (status != SERVICE_WORKER_OK && !done_closure_.is_null())
405 done_closure_.Run();
406 }
407
408 void StopOnIOThread() {
409 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
410 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status());
411
412 ServiceWorkerStatusCode status = worker_->Stop();
413
414 last_worker_status_ = worker_->status();
415 EXPECT_EQ(SERVICE_WORKER_OK, status);
416 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, last_worker_status_);
417
418 if (status != SERVICE_WORKER_OK && !done_closure_.is_null())
419 done_closure_.Run();
420 }
421
422 protected:
423 // EmbeddedWorkerInstance::Observer overrides:
424 void OnStarted() override {
425 ASSERT_TRUE(worker_ != NULL);
426 ASSERT_FALSE(done_closure_.is_null());
427 last_worker_status_ = worker_->status();
428 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
429 }
430 void OnStopped(EmbeddedWorkerInstance::Status old_status) override {
431 ASSERT_TRUE(worker_ != NULL);
432 ASSERT_FALSE(done_closure_.is_null());
433 last_worker_status_ = worker_->status();
434 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
435 }
436 void OnReportException(const base::string16& error_message,
437 int line_number,
438 int column_number,
439 const GURL& source_url) override {}
440 void OnReportConsoleMessage(int source_identifier,
441 int message_level,
442 const base::string16& message,
443 int line_number,
444 const GURL& source_url) override {}
445 bool OnMessageReceived(const IPC::Message& message) override { return false; }
446
447 scoped_ptr<EmbeddedWorkerInstance> worker_;
448 EmbeddedWorkerInstance::Status last_worker_status_;
449
450 // Called by EmbeddedWorkerInstance::Observer overrides so that
451 // test code can wait for the worker status notifications.
452 base::Closure done_closure_;
453 };
454
455 class ConsoleListener : public EmbeddedWorkerInstance::Listener { 344 class ConsoleListener : public EmbeddedWorkerInstance::Listener {
456 public: 345 public:
457 void OnReportConsoleMessage(int source_identifier, 346 void OnReportConsoleMessage(int source_identifier,
458 int message_level, 347 int message_level,
459 const base::string16& message, 348 const base::string16& message,
460 int line_number, 349 int line_number,
461 const GURL& source_url) override { 350 const GURL& source_url) override {
462 messages_.push_back(message); 351 messages_.push_back(message);
463 if (!quit_.is_null() && messages_.size() == expected_message_count_) { 352 if (!quit_.is_null() && messages_.size() == expected_message_count_) {
464 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_); 353 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 base::RunLoop run_loop; 422 base::RunLoop run_loop;
534 BrowserThread::PostTask( 423 BrowserThread::PostTask(
535 BrowserThread::IO, 424 BrowserThread::IO,
536 FROM_HERE, 425 FROM_HERE,
537 base::Bind( 426 base::Bind(
538 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); 427 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status));
539 run_loop.Run(); 428 run_loop.Run();
540 ASSERT_EQ(expected_status, status); 429 ASSERT_EQ(expected_status, status);
541 } 430 }
542 431
543 base::string16 RunSyncTestWithConsoleOutput(
544 const std::string& worker_url,
545 ServiceWorkerStatusCode expected_status) {
546 RunOnIOThread(
547 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url));
548 return SyncOnRegisteredWorkerWithConsoleOutput(expected_status);
549 }
550
551 void SyncOnRegisteredWorker(ServiceWorkerStatusCode expected_status) {
552 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
553 base::RunLoop sync_run_loop;
554 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
555 base::Bind(&self::SyncEventOnIOThread, this,
556 sync_run_loop.QuitClosure(), &status));
557 sync_run_loop.Run();
558 ASSERT_EQ(expected_status, status);
559 }
560
561 base::string16 SyncOnRegisteredWorkerWithConsoleOutput(
562 ServiceWorkerStatusCode expected_status) {
563 ConsoleListener console_listener;
564 version_->embedded_worker()->AddListener(&console_listener);
565
566 SyncOnRegisteredWorker(expected_status);
567
568 console_listener.WaitForConsoleMessages(1);
569 base::string16 console_output = console_listener.messages()[0];
570 version_->embedded_worker()->RemoveListener(&console_listener);
571 return console_output;
572 }
573
574 void FetchOnRegisteredWorker( 432 void FetchOnRegisteredWorker(
575 ServiceWorkerFetchEventResult* result, 433 ServiceWorkerFetchEventResult* result,
576 ServiceWorkerResponse* response, 434 ServiceWorkerResponse* response,
577 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) { 435 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) {
578 blob_context_ = ChromeBlobStorageContext::GetFor( 436 blob_context_ = ChromeBlobStorageContext::GetFor(
579 shell()->web_contents()->GetBrowserContext()); 437 shell()->web_contents()->GetBrowserContext());
580 bool prepare_result = false; 438 bool prepare_result = false;
581 FetchResult fetch_result; 439 FetchResult fetch_result;
582 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; 440 fetch_result.status = SERVICE_WORKER_ERROR_FAILED;
583 base::RunLoop fetch_run_loop; 441 base::RunLoop fetch_run_loop;
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 CreateResponseReceiver( 641 CreateResponseReceiver(
784 BrowserThread::UI, done, blob_context_.get(), result)); 642 BrowserThread::UI, done, blob_context_.get(), result));
785 } 643 }
786 644
787 void StopOnIOThread(const base::Closure& done, 645 void StopOnIOThread(const base::Closure& done,
788 ServiceWorkerStatusCode* result) { 646 ServiceWorkerStatusCode* result) {
789 ASSERT_TRUE(version_.get()); 647 ASSERT_TRUE(version_.get());
790 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); 648 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result));
791 } 649 }
792 650
793 void SyncEventOnIOThread(const base::Closure& done,
794 ServiceWorkerStatusCode* result) {
795 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
796 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
797 version_->DispatchSyncEvent(
798 CreateOneShotSyncRegistration(""),
799 CreateReceiver(BrowserThread::UI, done, result));
800 }
801
802 protected: 651 protected:
803 scoped_refptr<ServiceWorkerRegistration> registration_; 652 scoped_refptr<ServiceWorkerRegistration> registration_;
804 scoped_refptr<ServiceWorkerVersion> version_; 653 scoped_refptr<ServiceWorkerVersion> version_;
805 scoped_refptr<ChromeBlobStorageContext> blob_context_; 654 scoped_refptr<ChromeBlobStorageContext> blob_context_;
806 }; 655 };
807 656
808 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) {
809 // Start a worker and wait until OnStarted() is called.
810 base::RunLoop start_run_loop;
811 done_closure_ = start_run_loop.QuitClosure();
812 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
813 base::Bind(&self::StartOnIOThread, this));
814 start_run_loop.Run();
815
816 ASSERT_EQ(EmbeddedWorkerInstance::RUNNING, last_worker_status_);
817
818 // Stop a worker and wait until OnStopped() is called.
819 base::RunLoop stop_run_loop;
820 done_closure_ = stop_run_loop.QuitClosure();
821 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
822 base::Bind(&self::StopOnIOThread, this));
823 stop_run_loop.Run();
824
825 ASSERT_EQ(EmbeddedWorkerInstance::STOPPED, last_worker_status_);
826 }
827
828 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartAndStop) { 657 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartAndStop) {
829 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 658 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
830 "/service_worker/worker.js")); 659 "/service_worker/worker.js"));
831 660
832 // Start a worker. 661 // Start a worker.
833 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 662 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
834 base::RunLoop start_run_loop; 663 base::RunLoop start_run_loop;
835 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 664 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
836 base::Bind(&self::StartOnIOThread, this, 665 base::Bind(&self::StartOnIOThread, this,
837 start_run_loop.QuitClosure(), 666 start_run_loop.QuitClosure(),
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 console_listener.messages()[0].find(expected1)); 939 console_listener.messages()[0].find(expected1));
1111 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2)); 940 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2));
1112 version_->embedded_worker()->RemoveListener(&console_listener); 941 version_->embedded_worker()->RemoveListener(&console_listener);
1113 942
1114 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); 943 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
1115 EXPECT_EQ(0, response.status_code); 944 EXPECT_EQ(0, response.status_code);
1116 945
1117 ASSERT_FALSE(blob_data_handle); 946 ASSERT_FALSE(blob_data_handle);
1118 } 947 }
1119 948
1120 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventHandled) {
1121 RunOnIOThread(base::Bind(
1122 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js"));
1123 ServiceWorkerFetchEventResult result;
1124 ServiceWorkerResponse response;
1125 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
1126 // Should 404 before sync event.
1127 FetchOnRegisteredWorker(&result, &response, &blob_data_handle);
1128 EXPECT_EQ(404, response.status_code);
1129
1130 // Run the sync event.
1131 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
1132 base::RunLoop sync_run_loop;
1133 BrowserThread::PostTask(BrowserThread::IO,
1134 FROM_HERE,
1135 base::Bind(&self::SyncEventOnIOThread,
1136 this,
1137 sync_run_loop.QuitClosure(),
1138 &status));
1139 sync_run_loop.Run();
1140 ASSERT_EQ(SERVICE_WORKER_OK, status);
1141
1142 // Should 200 after sync event.
1143 FetchOnRegisteredWorker(&result, &response, &blob_data_handle);
1144 EXPECT_EQ(200, response.status_code);
1145 }
1146
1147 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventInterface) {
1148 // Verify that the fired sync event has the correct interface.
1149 // The js event handler will console.log the event properties.
1150 base::string16 console_output = RunSyncTestWithConsoleOutput(
1151 "/background_sync/sync_event_interface.js", SERVICE_WORKER_OK);
1152
1153 EXPECT_FALSE(console_output.empty());
1154
1155 // Console output is a pipe-delimited string, as:
1156 // <event prototype>|<typeof waitUntil>
1157 std::vector<base::string16> event_properties =
1158 base::SplitString(console_output, base::string16(1, '|'),
1159 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
1160
1161 const base::string16::size_type num_properties = 2;
1162 const base::string16 event_type = base::ASCIIToUTF16("SyncEvent");
1163 const base::string16 wait_until_type = base::ASCIIToUTF16("function");
1164 EXPECT_EQ(num_properties, event_properties.size());
1165 EXPECT_EQ(event_type, event_properties[0]);
1166 EXPECT_EQ(wait_until_type, event_properties[1]);
1167 }
1168
1169 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
1170 SyncEventWaitUntil_Fulfilled) {
1171 base::string16 console_output = RunSyncTestWithConsoleOutput(
1172 "/background_sync/sync_event_fulfilled.js", SERVICE_WORKER_OK);
1173
1174 // Verify that the event.waitUntil function resolved the promise. If so,
1175 // the js event handler will console.log the expected output.
1176 const base::string16 expected = base::ASCIIToUTF16("Fulfilling onsync event");
1177 EXPECT_EQ(expected, console_output);
1178 }
1179
1180 // https://crbug.com/504202
1181 #if defined(THREAD_SANITIZER)
1182 #define MAYBE_SyncEventWaitUntil_Rejected DISABLED_SyncEventWaitUntil_Rejected
1183 #else
1184 #define MAYBE_SyncEventWaitUntil_Rejected SyncEventWaitUntil_Rejected
1185 #endif
1186 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
1187 MAYBE_SyncEventWaitUntil_Rejected) {
1188 base::string16 console_output = RunSyncTestWithConsoleOutput(
1189 "/background_sync/sync_event_rejected.js",
1190 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED);
1191
1192 // Verify that the event.waitUntil function rejected the promise. If so,
1193 // the js event handler will console.log the expected output.
1194 const base::string16 expected = base::ASCIIToUTF16("Rejecting onsync event");
1195 EXPECT_EQ(expected, console_output);
1196 }
1197
1198 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { 949 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) {
1199 const char kPageUrl[] = "/service_worker/reload.html"; 950 const char kPageUrl[] = "/service_worker/reload.html";
1200 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; 951 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js";
1201 scoped_refptr<WorkerActivatedObserver> observer = 952 scoped_refptr<WorkerActivatedObserver> observer =
1202 new WorkerActivatedObserver(wrapper()); 953 new WorkerActivatedObserver(wrapper());
1203 observer->Init(); 954 observer->Init();
1204 public_context()->RegisterServiceWorker( 955 public_context()->RegisterServiceWorker(
1205 embedded_test_server()->GetURL(kPageUrl), 956 embedded_test_server()->GetURL(kPageUrl),
1206 embedded_test_server()->GetURL(kWorkerUrl), 957 embedded_test_server()->GetURL(kWorkerUrl),
1207 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); 958 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing)));
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 ASSERT_EQ(SERVICE_WORKER_OK, status); 1279 ASSERT_EQ(SERVICE_WORKER_OK, status);
1529 // Stop the worker. 1280 // Stop the worker.
1530 StopWorker(SERVICE_WORKER_OK); 1281 StopWorker(SERVICE_WORKER_OK);
1531 // Restart the worker. 1282 // Restart the worker.
1532 StartWorker(SERVICE_WORKER_OK); 1283 StartWorker(SERVICE_WORKER_OK);
1533 // Stop the worker. 1284 // Stop the worker.
1534 StopWorker(SERVICE_WORKER_OK); 1285 StopWorker(SERVICE_WORKER_OK);
1535 } 1286 }
1536 1287
1537 } // namespace content 1288 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698