| OLD | NEW |
| 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 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 base::RunLoop run_loop; | 534 base::RunLoop run_loop; |
| 535 BrowserThread::PostTask( | 535 BrowserThread::PostTask( |
| 536 BrowserThread::IO, | 536 BrowserThread::IO, |
| 537 FROM_HERE, | 537 FROM_HERE, |
| 538 base::Bind( | 538 base::Bind( |
| 539 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); | 539 &self::ActivateOnIOThread, this, run_loop.QuitClosure(), &status)); |
| 540 run_loop.Run(); | 540 run_loop.Run(); |
| 541 ASSERT_EQ(expected_status, status); | 541 ASSERT_EQ(expected_status, status); |
| 542 } | 542 } |
| 543 | 543 |
| 544 base::string16 RunSyncTestWithConsoleOutput( |
| 545 const std::string& worker_url, |
| 546 ServiceWorkerStatusCode expected_status) { |
| 547 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 548 command_line->AppendSwitch(switches::kEnableServiceWorkerSync); |
| 549 RunOnIOThread( |
| 550 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); |
| 551 return SyncOnRegisteredWorkerWithConsoleOutput(expected_status); |
| 552 } |
| 553 |
| 554 void SyncOnRegisteredWorker(ServiceWorkerStatusCode expected_status) { |
| 555 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 556 base::RunLoop sync_run_loop; |
| 557 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 558 base::Bind(&self::SyncEventOnIOThread, this, |
| 559 sync_run_loop.QuitClosure(), &status)); |
| 560 sync_run_loop.Run(); |
| 561 ASSERT_EQ(expected_status, status); |
| 562 } |
| 563 |
| 564 base::string16 SyncOnRegisteredWorkerWithConsoleOutput( |
| 565 ServiceWorkerStatusCode expected_status) { |
| 566 ConsoleListener console_listener; |
| 567 version_->embedded_worker()->AddListener(&console_listener); |
| 568 |
| 569 SyncOnRegisteredWorker(expected_status); |
| 570 |
| 571 console_listener.WaitForConsoleMessages(1); |
| 572 base::string16 console_output = console_listener.messages()[0]; |
| 573 version_->embedded_worker()->RemoveListener(&console_listener); |
| 574 return console_output; |
| 575 } |
| 576 |
| 544 void FetchOnRegisteredWorker( | 577 void FetchOnRegisteredWorker( |
| 545 ServiceWorkerFetchEventResult* result, | 578 ServiceWorkerFetchEventResult* result, |
| 546 ServiceWorkerResponse* response, | 579 ServiceWorkerResponse* response, |
| 547 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) { | 580 scoped_ptr<storage::BlobDataHandle>* blob_data_handle) { |
| 548 blob_context_ = ChromeBlobStorageContext::GetFor( | 581 blob_context_ = ChromeBlobStorageContext::GetFor( |
| 549 shell()->web_contents()->GetBrowserContext()); | 582 shell()->web_contents()->GetBrowserContext()); |
| 550 bool prepare_result = false; | 583 bool prepare_result = false; |
| 551 FetchResult fetch_result; | 584 FetchResult fetch_result; |
| 552 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; | 585 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; |
| 553 base::RunLoop fetch_run_loop; | 586 base::RunLoop fetch_run_loop; |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1152 sync_run_loop.QuitClosure(), | 1185 sync_run_loop.QuitClosure(), |
| 1153 &status)); | 1186 &status)); |
| 1154 sync_run_loop.Run(); | 1187 sync_run_loop.Run(); |
| 1155 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1188 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1156 | 1189 |
| 1157 // Should 200 after sync event. | 1190 // Should 200 after sync event. |
| 1158 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); | 1191 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); |
| 1159 EXPECT_EQ(200, response.status_code); | 1192 EXPECT_EQ(200, response.status_code); |
| 1160 } | 1193 } |
| 1161 | 1194 |
| 1195 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventInterface) { |
| 1196 // Verify that the fired sync event has the correct interface. |
| 1197 // The js event handler will console.log the event properties. |
| 1198 base::string16 console_output = RunSyncTestWithConsoleOutput( |
| 1199 "/background_sync/sync_event_interface.js", SERVICE_WORKER_OK); |
| 1200 |
| 1201 EXPECT_FALSE(console_output.empty()); |
| 1202 |
| 1203 // Console output is a pipe-delimited string, as: |
| 1204 // <event prototype>|<typeof waitUntil> |
| 1205 std::vector<base::string16> event_properties; |
| 1206 base::SplitString(console_output, '|', &event_properties); |
| 1207 |
| 1208 const base::string16::size_type num_properties = 2; |
| 1209 const base::string16 event_type = base::ASCIIToUTF16("SyncEvent"); |
| 1210 const base::string16 wait_until_type = base::ASCIIToUTF16("function"); |
| 1211 EXPECT_EQ(num_properties, event_properties.size()); |
| 1212 EXPECT_EQ(event_type, event_properties[0]); |
| 1213 EXPECT_EQ(wait_until_type, event_properties[1]); |
| 1214 } |
| 1215 |
| 1216 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 1217 SyncEventWaitUntil_Fulfilled) { |
| 1218 base::string16 console_output = RunSyncTestWithConsoleOutput( |
| 1219 "/background_sync/sync_event_fulfilled.js", SERVICE_WORKER_OK); |
| 1220 |
| 1221 // Verify that the event.waitUntil function resolved the promise. If so, |
| 1222 // the js event handler will console.log the expected output. |
| 1223 const base::string16 expected = base::ASCIIToUTF16("Fulfilling onsync event"); |
| 1224 EXPECT_EQ(expected, console_output); |
| 1225 } |
| 1226 |
| 1227 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 1228 SyncEventWaitUntil_Rejected) { |
| 1229 base::string16 console_output = RunSyncTestWithConsoleOutput( |
| 1230 "/background_sync/sync_event_rejected.js", |
| 1231 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED); |
| 1232 |
| 1233 // Verify that the event.waitUntil function rejected the promise. If so, |
| 1234 // the js event handler will console.log the expected output. |
| 1235 const base::string16 expected = base::ASCIIToUTF16("Rejecting onsync event"); |
| 1236 EXPECT_EQ(expected, console_output); |
| 1237 } |
| 1238 |
| 1162 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { | 1239 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { |
| 1163 const char kPageUrl[] = "/service_worker/reload.html"; | 1240 const char kPageUrl[] = "/service_worker/reload.html"; |
| 1164 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; | 1241 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; |
| 1165 scoped_refptr<WorkerActivatedObserver> observer = | 1242 scoped_refptr<WorkerActivatedObserver> observer = |
| 1166 new WorkerActivatedObserver(wrapper()); | 1243 new WorkerActivatedObserver(wrapper()); |
| 1167 observer->Init(); | 1244 observer->Init(); |
| 1168 public_context()->RegisterServiceWorker( | 1245 public_context()->RegisterServiceWorker( |
| 1169 embedded_test_server()->GetURL(kPageUrl), | 1246 embedded_test_server()->GetURL(kPageUrl), |
| 1170 embedded_test_server()->GetURL(kWorkerUrl), | 1247 embedded_test_server()->GetURL(kWorkerUrl), |
| 1171 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | 1248 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1492 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1569 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1493 // Stop the worker. | 1570 // Stop the worker. |
| 1494 StopWorker(SERVICE_WORKER_OK); | 1571 StopWorker(SERVICE_WORKER_OK); |
| 1495 // Restart the worker. | 1572 // Restart the worker. |
| 1496 StartWorker(SERVICE_WORKER_OK); | 1573 StartWorker(SERVICE_WORKER_OK); |
| 1497 // Stop the worker. | 1574 // Stop the worker. |
| 1498 StopWorker(SERVICE_WORKER_OK); | 1575 StopWorker(SERVICE_WORKER_OK); |
| 1499 } | 1576 } |
| 1500 | 1577 |
| 1501 } // namespace content | 1578 } // namespace content |
| OLD | NEW |