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 "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" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 14 #include "content/browser/service_worker/service_worker_test_utils.h" | 14 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 15 #include "content/browser/service_worker/service_worker_version.h" | 15 #include "content/browser/service_worker/service_worker_version.h" |
| 16 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 17 #include "content/common/service_worker/service_worker_status_code.h" | |
| 18 #include "content/common/service_worker/service_worker_types.h" | |
| 17 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 20 #include "content/public/browser/storage_partition.h" | 22 #include "content/public/browser/storage_partition.h" |
| 21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 23 #include "content/shell/browser/shell.h" | 25 #include "content/shell/browser/shell.h" |
| 24 #include "content/test/content_browser_test.h" | 26 #include "content/test/content_browser_test.h" |
| 25 #include "content/test/content_browser_test_utils.h" | 27 #include "content/test/content_browser_test_utils.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 38 | 40 |
| 39 void RunOnIOThread(const base::Closure& closure) { | 41 void RunOnIOThread(const base::Closure& closure) { |
| 40 base::RunLoop run_loop; | 42 base::RunLoop run_loop; |
| 41 BrowserThread::PostTask( | 43 BrowserThread::PostTask( |
| 42 BrowserThread::IO, FROM_HERE, | 44 BrowserThread::IO, FROM_HERE, |
| 43 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), | 45 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), |
| 44 base::MessageLoopProxy::current())); | 46 base::MessageLoopProxy::current())); |
| 45 run_loop.Run(); | 47 run_loop.Run(); |
| 46 } | 48 } |
| 47 | 49 |
| 50 struct FetchResult { | |
| 51 ServiceWorkerStatusCode status; | |
| 52 ServiceWorkerFetchEventResult result; | |
| 53 ServiceWorkerResponse response; | |
|
kinuko
2014/03/04 06:39:17
nit: 2 space indent
falken
2014/03/05 03:35:41
Done. I also ran git-cl format and it made several
| |
| 54 }; | |
| 55 | |
| 48 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, | 56 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, |
| 49 const base::Closure& quit, | 57 const base::Closure& quit, |
| 50 ServiceWorkerStatusCode* out_status, | 58 FetchResult* out_result, |
| 51 ServiceWorkerFetchResponse* out_response, | |
| 52 ServiceWorkerStatusCode actual_status, | 59 ServiceWorkerStatusCode actual_status, |
| 53 const ServiceWorkerFetchResponse& actual_response) { | 60 ServiceWorkerFetchEventResult actual_result, |
| 54 *out_status = actual_status; | 61 const ServiceWorkerResponse& actual_response) { |
| 55 *out_response = actual_response; | 62 out_result->status = actual_status; |
| 63 out_result->result = actual_result; | |
| 64 out_result->response = actual_response; | |
| 56 if (!quit.is_null()) | 65 if (!quit.is_null()) |
| 57 BrowserThread::PostTask(run_quit_thread, FROM_HERE, quit); | 66 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit); |
| 58 } | 67 } |
| 59 | 68 |
| 60 ServiceWorkerVersion::FetchCallback CreateFetchResponseReceiver( | 69 ServiceWorkerVersion::FetchCallback CreateResponseReceiver( |
| 61 BrowserThread::ID run_quit_thread, | 70 BrowserThread::ID run_quit_thread, |
| 62 const base::Closure& quit, | 71 const base::Closure& quit, |
| 63 ServiceWorkerStatusCode* out_status, | 72 FetchResult* result) { |
| 64 ServiceWorkerFetchResponse* out_response) { | |
| 65 return base::Bind( | 73 return base::Bind( |
| 66 &ReceiveFetchResult, run_quit_thread, quit, out_status, out_response); | 74 &ReceiveFetchResult, run_quit_thread, quit, result); |
| 67 } | 75 } |
| 68 | 76 |
| 69 } // namespace | 77 } // namespace |
| 70 | 78 |
| 71 class ServiceWorkerBrowserTest : public ContentBrowserTest { | 79 class ServiceWorkerBrowserTest : public ContentBrowserTest { |
| 72 protected: | 80 protected: |
| 73 typedef ServiceWorkerBrowserTest self; | 81 typedef ServiceWorkerBrowserTest self; |
| 74 | 82 |
| 75 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 83 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 76 command_line->AppendSwitch(switches::kEnableServiceWorker); | 84 command_line->AppendSwitch(switches::kEnableServiceWorker); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 status = SERVICE_WORKER_ERROR_FAILED; | 233 status = SERVICE_WORKER_ERROR_FAILED; |
| 226 base::RunLoop stop_run_loop; | 234 base::RunLoop stop_run_loop; |
| 227 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 235 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 228 base::Bind(&self::StopOnIOThread, this, | 236 base::Bind(&self::StopOnIOThread, this, |
| 229 stop_run_loop.QuitClosure(), | 237 stop_run_loop.QuitClosure(), |
| 230 &status)); | 238 &status)); |
| 231 stop_run_loop.Run(); | 239 stop_run_loop.Run(); |
| 232 ASSERT_EQ(SERVICE_WORKER_OK, status); | 240 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 233 } | 241 } |
| 234 | 242 |
| 243 void FetchTestHelper(const std::string& worker_url, | |
| 244 ServiceWorkerFetchEventResult* result, | |
| 245 ServiceWorkerResponse* response) { | |
| 246 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | |
| 247 worker_url)); | |
| 248 | |
| 249 FetchResult fetch_result; | |
| 250 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; | |
| 251 base::RunLoop fetch_run_loop; | |
| 252 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 253 base::Bind(&self::FetchOnIOThread, this, | |
| 254 fetch_run_loop.QuitClosure(), | |
| 255 &fetch_result)); | |
| 256 fetch_run_loop.Run(); | |
| 257 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); | |
| 258 *result = fetch_result.result; | |
| 259 *response = fetch_result.response; | |
| 260 } | |
| 261 | |
| 235 void SetUpRegistrationOnIOThread(const std::string& worker_url) { | 262 void SetUpRegistrationOnIOThread(const std::string& worker_url) { |
| 236 const int64 version_id = 1L; | 263 const int64 version_id = 1L; |
| 237 registration_ = new ServiceWorkerRegistration( | 264 registration_ = new ServiceWorkerRegistration( |
| 238 embedded_test_server()->GetURL("/*"), | 265 embedded_test_server()->GetURL("/*"), |
| 239 embedded_test_server()->GetURL(worker_url), | 266 embedded_test_server()->GetURL(worker_url), |
| 240 next_registration_id_++); | 267 next_registration_id_++); |
| 241 version_ = new ServiceWorkerVersion( | 268 version_ = new ServiceWorkerVersion( |
| 242 registration_, | 269 registration_, |
| 243 wrapper()->context()->embedded_worker_registry(), | 270 wrapper()->context()->embedded_worker_registry(), |
| 244 version_id); | 271 version_id); |
| 245 AssociateRendererProcessToWorker(version_->embedded_worker()); | 272 AssociateRendererProcessToWorker(version_->embedded_worker()); |
| 246 } | 273 } |
| 247 | 274 |
| 248 void StartOnIOThread(const base::Closure& done, | 275 void StartOnIOThread(const base::Closure& done, |
| 249 ServiceWorkerStatusCode* result) { | 276 ServiceWorkerStatusCode* result) { |
| 250 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 277 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 251 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); | 278 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); |
| 252 } | 279 } |
| 253 | 280 |
| 254 void InstallOnIOThread(const base::Closure& done, | 281 void InstallOnIOThread(const base::Closure& done, |
| 255 ServiceWorkerStatusCode* result) { | 282 ServiceWorkerStatusCode* result) { |
| 256 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 283 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 257 version_->DispatchInstallEvent( | 284 version_->DispatchInstallEvent( |
| 258 -1, CreateReceiver(BrowserThread::UI, done, result)); | 285 -1, CreateReceiver(BrowserThread::UI, done, result)); |
| 259 } | 286 } |
| 260 | 287 |
| 261 void FetchOnIOThread(const base::Closure& done, | 288 void FetchOnIOThread(const base::Closure& done, |
| 262 ServiceWorkerStatusCode* result, | 289 FetchResult* result) { |
| 263 ServiceWorkerFetchResponse* message) { | |
| 264 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 290 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 265 ServiceWorkerFetchRequest request( | 291 ServiceWorkerFetchRequest request( |
| 266 embedded_test_server()->GetURL("/service_worker/empty.html"), | 292 embedded_test_server()->GetURL("/service_worker/empty.html"), |
| 267 "GET", | 293 "GET", |
| 268 std::map<std::string, std::string>()); | 294 std::map<std::string, std::string>()); |
| 269 version_->DispatchFetchEvent( | 295 version_->DispatchFetchEvent( |
| 270 request, | 296 request, |
| 271 CreateFetchResponseReceiver(BrowserThread::UI, done, result, message)); | 297 CreateResponseReceiver(BrowserThread::UI, done, result)); |
| 272 } | 298 } |
| 273 | 299 |
| 274 | |
| 275 void StopOnIOThread(const base::Closure& done, | 300 void StopOnIOThread(const base::Closure& done, |
| 276 ServiceWorkerStatusCode* result) { | 301 ServiceWorkerStatusCode* result) { |
| 277 ASSERT_TRUE(version_); | 302 ASSERT_TRUE(version_); |
| 278 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); | 303 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); |
| 279 } | 304 } |
| 280 | 305 |
| 281 protected: | 306 protected: |
| 282 int64 next_registration_id_; | 307 int64 next_registration_id_; |
| 283 scoped_refptr<ServiceWorkerRegistration> registration_; | 308 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 284 scoped_refptr<ServiceWorkerVersion> version_; | 309 scoped_refptr<ServiceWorkerVersion> version_; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 InstallTestHelper("/service_worker/worker_install_fulfilled.js"); | 378 InstallTestHelper("/service_worker/worker_install_fulfilled.js"); |
| 354 } | 379 } |
| 355 | 380 |
| 356 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 381 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 357 InstallWithWaitUntil_Rejected) { | 382 InstallWithWaitUntil_Rejected) { |
| 358 // TODO(kinuko): This should also report back an error, but we | 383 // TODO(kinuko): This should also report back an error, but we |
| 359 // don't have plumbing for it yet. | 384 // don't have plumbing for it yet. |
| 360 InstallTestHelper("/service_worker/worker_install_rejected.js"); | 385 InstallTestHelper("/service_worker/worker_install_rejected.js"); |
| 361 } | 386 } |
| 362 | 387 |
| 363 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Fetch) { | 388 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 364 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | 389 FetchEvent_Response) { |
| 365 "/service_worker/worker.js")); | 390 ServiceWorkerFetchEventResult result; |
| 366 | 391 ServiceWorkerResponse response; |
| 367 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 392 FetchTestHelper("/service_worker/fetch_event.js", &result, &response); |
| 368 ServiceWorkerFetchResponse response; | 393 ASSERT_EQ(RESPONSE, result); |
| 369 base::RunLoop fetch_run_loop; | |
| 370 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 371 base::Bind(&self::FetchOnIOThread, this, | |
| 372 fetch_run_loop.QuitClosure(), | |
| 373 &status, &response)); | |
| 374 fetch_run_loop.Run(); | |
| 375 ASSERT_EQ(SERVICE_WORKER_OK, status); | |
| 376 ASSERT_EQ(200, response.status_code); | 394 ASSERT_EQ(200, response.status_code); |
| 377 ASSERT_EQ("OK", response.status_text); | 395 ASSERT_EQ("OK", response.status_text); |
| 378 ASSERT_EQ("GET", response.method); | 396 ASSERT_EQ("GET", response.method); |
| 379 std::map<std::string, std::string> expected_headers; | 397 std::map<std::string, std::string> expected_headers; |
| 380 ASSERT_EQ(expected_headers, response.headers); | 398 ASSERT_EQ(expected_headers, response.headers); |
| 381 } | 399 } |
| 382 | 400 |
| 401 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
| 402 FetchEvent_FallbackToNative) { | |
| 403 ServiceWorkerFetchEventResult result; | |
| 404 ServiceWorkerResponse response; | |
| 405 FetchTestHelper("/service_worker/fetch_event_fallback.js", | |
| 406 &result, &response); | |
| 407 ASSERT_EQ(FALLBACK, result); | |
| 408 } | |
| 409 | |
| 410 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
| 411 FetchEvent_Rejected) { | |
| 412 ServiceWorkerFetchEventResult result; | |
| 413 ServiceWorkerResponse response; | |
| 414 FetchTestHelper("/service_worker/fetch_event_error.js", | |
| 415 &result, &response); | |
| 416 ASSERT_EQ(FALLBACK, result); | |
| 417 } | |
| 418 | |
| 383 } // namespace content | 419 } // namespace content |
| OLD | NEW |