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

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: Rebase 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 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 base::RunLoop run_loop; 527 base::RunLoop run_loop;
534 BrowserThread::PostTask( 528 BrowserThread::PostTask(
535 BrowserThread::IO, 529 BrowserThread::IO,
536 FROM_HERE, 530 FROM_HERE,
537 base::Bind( 531 base::Bind(
538 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); 532 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status));
539 run_loop.Run(); 533 run_loop.Run();
540 ASSERT_EQ(expected_status, status); 534 ASSERT_EQ(expected_status, status);
541 } 535 }
542 536
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( 537 void FetchOnRegisteredWorker(
575 ServiceWorkerFetchEventResult* result, 538 ServiceWorkerFetchEventResult* result,
576 ServiceWorkerResponse* response, 539 ServiceWorkerResponse* response,
577 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) { 540 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) {
578 blob_context_ = ChromeBlobStorageContext::GetFor( 541 blob_context_ = ChromeBlobStorageContext::GetFor(
579 shell()->web_contents()->GetBrowserContext()); 542 shell()->web_contents()->GetBrowserContext());
580 bool prepare_result = false; 543 bool prepare_result = false;
581 FetchResult fetch_result; 544 FetchResult fetch_result;
582 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; 545 fetch_result.status = SERVICE_WORKER_ERROR_FAILED;
583 base::RunLoop fetch_run_loop; 546 base::RunLoop fetch_run_loop;
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 CreateResponseReceiver( 746 CreateResponseReceiver(
784 BrowserThread::UI, done, blob_context_.get(), result)); 747 BrowserThread::UI, done, blob_context_.get(), result));
785 } 748 }
786 749
787 void StopOnIOThread(const base::Closure& done, 750 void StopOnIOThread(const base::Closure& done,
788 ServiceWorkerStatusCode* result) { 751 ServiceWorkerStatusCode* result) {
789 ASSERT_TRUE(version_.get()); 752 ASSERT_TRUE(version_.get());
790 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); 753 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result));
791 } 754 }
792 755
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: 756 protected:
803 scoped_refptr<ServiceWorkerRegistration> registration_; 757 scoped_refptr<ServiceWorkerRegistration> registration_;
804 scoped_refptr<ServiceWorkerVersion> version_; 758 scoped_refptr<ServiceWorkerVersion> version_;
805 scoped_refptr<ChromeBlobStorageContext> blob_context_; 759 scoped_refptr<ChromeBlobStorageContext> blob_context_;
806 }; 760 };
807 761
808 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { 762 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) {
809 // Start a worker and wait until OnStarted() is called. 763 // Start a worker and wait until OnStarted() is called.
810 base::RunLoop start_run_loop; 764 base::RunLoop start_run_loop;
811 done_closure_ = start_run_loop.QuitClosure(); 765 done_closure_ = start_run_loop.QuitClosure();
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 console_listener.messages()[0].find(expected1)); 1064 console_listener.messages()[0].find(expected1));
1111 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2)); 1065 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2));
1112 version_->embedded_worker()->RemoveListener(&console_listener); 1066 version_->embedded_worker()->RemoveListener(&console_listener);
1113 1067
1114 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); 1068 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
1115 EXPECT_EQ(0, response.status_code); 1069 EXPECT_EQ(0, response.status_code);
1116 1070
1117 ASSERT_FALSE(blob_data_handle); 1071 ASSERT_FALSE(blob_data_handle);
1118 } 1072 }
1119 1073
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) { 1074 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) {
1199 const char kPageUrl[] = "/service_worker/reload.html"; 1075 const char kPageUrl[] = "/service_worker/reload.html";
1200 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; 1076 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js";
1201 scoped_refptr<WorkerActivatedObserver> observer = 1077 scoped_refptr<WorkerActivatedObserver> observer =
1202 new WorkerActivatedObserver(wrapper()); 1078 new WorkerActivatedObserver(wrapper());
1203 observer->Init(); 1079 observer->Init();
1204 public_context()->RegisterServiceWorker( 1080 public_context()->RegisterServiceWorker(
1205 embedded_test_server()->GetURL(kPageUrl), 1081 embedded_test_server()->GetURL(kPageUrl),
1206 embedded_test_server()->GetURL(kWorkerUrl), 1082 embedded_test_server()->GetURL(kWorkerUrl),
1207 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); 1083 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); 1404 ASSERT_EQ(SERVICE_WORKER_OK, status);
1529 // Stop the worker. 1405 // Stop the worker.
1530 StopWorker(SERVICE_WORKER_OK); 1406 StopWorker(SERVICE_WORKER_OK);
1531 // Restart the worker. 1407 // Restart the worker.
1532 StartWorker(SERVICE_WORKER_OK); 1408 StartWorker(SERVICE_WORKER_OK);
1533 // Stop the worker. 1409 // Stop the worker.
1534 StopWorker(SERVICE_WORKER_OK); 1410 StopWorker(SERVICE_WORKER_OK);
1535 } 1411 }
1536 1412
1537 } // namespace content 1413 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698