Chromium Code Reviews| 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 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1142 sync_run_loop.QuitClosure(), | 1175 sync_run_loop.QuitClosure(), |
| 1143 &status)); | 1176 &status)); |
| 1144 sync_run_loop.Run(); | 1177 sync_run_loop.Run(); |
| 1145 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1178 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1146 | 1179 |
| 1147 // Should 200 after sync event. | 1180 // Should 200 after sync event. |
| 1148 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); | 1181 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); |
| 1149 EXPECT_EQ(200, response.status_code); | 1182 EXPECT_EQ(200, response.status_code); |
| 1150 } | 1183 } |
| 1151 | 1184 |
| 1185 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventInterface) { | |
| 1186 // Verify that the fired sync event has the correct interface | |
| 1187 // The js event handler will console.log the event properties | |
|
jkarlin
2015/06/12 13:11:18
Missing periods after each sentence.
chasej
2015/06/12 16:04:12
Done.
| |
| 1188 base::string16 console_output = RunSyncTestWithConsoleOutput( | |
| 1189 "/background_sync/sync_event_interface.js", SERVICE_WORKER_OK); | |
| 1190 | |
| 1191 EXPECT_FALSE(console_output.empty()) << "Console output should not be empty"; | |
|
jkarlin
2015/06/12 13:11:18
The string seems redundant here.
chasej
2015/06/12 16:04:12
Done.
| |
| 1192 | |
| 1193 // Console output is a pipe-delimited string, as: | |
| 1194 // <event prototype>|<typeof waitUntil> | |
| 1195 std::vector<base::string16> event_properties; | |
| 1196 base::SplitString(console_output, '|', &event_properties); | |
| 1197 | |
| 1198 const base::string16::size_type num_properties = 2; | |
| 1199 const base::string16 event_type = base::ASCIIToUTF16("SyncEvent"); | |
| 1200 const base::string16 wait_until_type = base::ASCIIToUTF16("function"); | |
| 1201 EXPECT_EQ(num_properties, event_properties.size()); | |
| 1202 EXPECT_EQ(event_type, event_properties[0]); | |
| 1203 EXPECT_EQ(wait_until_type, event_properties[1]); | |
| 1204 } | |
| 1205 | |
| 1206 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
| 1207 SyncEventWaitUntil_Fulfilled) { | |
| 1208 base::string16 console_output = RunSyncTestWithConsoleOutput( | |
| 1209 "/background_sync/sync_event_fulfilled.js", SERVICE_WORKER_OK); | |
| 1210 | |
| 1211 // Verify that the event.waitUntil function resolved the promise. If so, | |
| 1212 // the js event handler will console.log the expected output | |
|
jkarlin
2015/06/12 13:11:18
missing period at end of sentence
chasej
2015/06/12 16:04:12
Done.
| |
| 1213 const base::string16 expected = base::ASCIIToUTF16("Fulfilling onsync event"); | |
| 1214 EXPECT_EQ(expected, console_output); | |
| 1215 } | |
| 1216 | |
| 1217 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
| 1218 SyncEventWaitUntil_Rejected) { | |
| 1219 base::string16 console_output = RunSyncTestWithConsoleOutput( | |
| 1220 "/background_sync/sync_event_rejected.js", | |
| 1221 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED); | |
| 1222 | |
| 1223 // Verify that the event.waitUntil function rejected the promise. If so, | |
| 1224 // the js event handler will console.log the expected output | |
|
jkarlin
2015/06/12 13:11:18
ditto
chasej
2015/06/12 16:04:12
Done.
| |
| 1225 const base::string16 expected = base::ASCIIToUTF16("Rejecting onsync event"); | |
| 1226 EXPECT_EQ(expected, console_output); | |
| 1227 } | |
| 1228 | |
| 1152 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { | 1229 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { |
| 1153 const char kPageUrl[] = "/service_worker/reload.html"; | 1230 const char kPageUrl[] = "/service_worker/reload.html"; |
| 1154 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; | 1231 const char kWorkerUrl[] = "/service_worker/fetch_event_reload.js"; |
| 1155 scoped_refptr<WorkerActivatedObserver> observer = | 1232 scoped_refptr<WorkerActivatedObserver> observer = |
| 1156 new WorkerActivatedObserver(wrapper()); | 1233 new WorkerActivatedObserver(wrapper()); |
| 1157 observer->Init(); | 1234 observer->Init(); |
| 1158 public_context()->RegisterServiceWorker( | 1235 public_context()->RegisterServiceWorker( |
| 1159 embedded_test_server()->GetURL(kPageUrl), | 1236 embedded_test_server()->GetURL(kPageUrl), |
| 1160 embedded_test_server()->GetURL(kWorkerUrl), | 1237 embedded_test_server()->GetURL(kWorkerUrl), |
| 1161 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); | 1238 base::Bind(&ExpectResultAndRun, true, base::Bind(&base::DoNothing))); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1482 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1559 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 1483 // Stop the worker. | 1560 // Stop the worker. |
| 1484 StopWorker(SERVICE_WORKER_OK); | 1561 StopWorker(SERVICE_WORKER_OK); |
| 1485 // Restart the worker. | 1562 // Restart the worker. |
| 1486 StartWorker(SERVICE_WORKER_OK); | 1563 StartWorker(SERVICE_WORKER_OK); |
| 1487 // Stop the worker. | 1564 // Stop the worker. |
| 1488 StopWorker(SERVICE_WORKER_OK); | 1565 StopWorker(SERVICE_WORKER_OK); |
| 1489 } | 1566 } |
| 1490 | 1567 |
| 1491 } // namespace content | 1568 } // namespace content |
| OLD | NEW |