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

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

Issue 178343011: Support dispatching ServiceWorker FetchEvent and receiving response (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use const for result on failure Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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"
27 29
28 namespace content { 30 namespace content {
29 31
30 namespace { 32 namespace {
31 33
34 struct FetchResult {
35 ServiceWorkerStatusCode status;
36 ServiceWorkerFetchEventResult result;
37 ServiceWorkerResponse response;
38 };
39
32 void RunAndQuit(const base::Closure& closure, 40 void RunAndQuit(const base::Closure& closure,
33 const base::Closure& quit, 41 const base::Closure& quit,
34 base::MessageLoopProxy* original_message_loop) { 42 base::MessageLoopProxy* original_message_loop) {
35 closure.Run(); 43 closure.Run();
36 original_message_loop->PostTask(FROM_HERE, quit); 44 original_message_loop->PostTask(FROM_HERE, quit);
37 } 45 }
38 46
39 void RunOnIOThread(const base::Closure& closure) { 47 void RunOnIOThread(const base::Closure& closure) {
40 base::RunLoop run_loop; 48 base::RunLoop run_loop;
41 BrowserThread::PostTask( 49 BrowserThread::PostTask(
42 BrowserThread::IO, FROM_HERE, 50 BrowserThread::IO, FROM_HERE,
43 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), 51 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(),
44 base::MessageLoopProxy::current())); 52 base::MessageLoopProxy::current()));
45 run_loop.Run(); 53 run_loop.Run();
46 } 54 }
47 55
56 // Contrary to the style guide, the output parameter of this function comes
57 // before input parameters so Bind can be used on it to create a FetchCallback
58 // to pass to DispatchFetchEvent.
48 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, 59 void ReceiveFetchResult(BrowserThread::ID run_quit_thread,
49 const base::Closure& quit, 60 const base::Closure& quit,
50 ServiceWorkerStatusCode* out_status, 61 FetchResult* out_result,
51 ServiceWorkerFetchResponse* out_response,
52 ServiceWorkerStatusCode actual_status, 62 ServiceWorkerStatusCode actual_status,
53 const ServiceWorkerFetchResponse& actual_response) { 63 ServiceWorkerFetchEventResult actual_result,
54 *out_status = actual_status; 64 const ServiceWorkerResponse& actual_response) {
55 *out_response = actual_response; 65 out_result->status = actual_status;
66 out_result->result = actual_result;
67 out_result->response = actual_response;
56 if (!quit.is_null()) 68 if (!quit.is_null())
57 BrowserThread::PostTask(run_quit_thread, FROM_HERE, quit); 69 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit);
58 } 70 }
59 71
60 ServiceWorkerVersion::FetchCallback CreateFetchResponseReceiver( 72 ServiceWorkerVersion::FetchCallback CreateResponseReceiver(
61 BrowserThread::ID run_quit_thread, 73 BrowserThread::ID run_quit_thread,
62 const base::Closure& quit, 74 const base::Closure& quit,
63 ServiceWorkerStatusCode* out_status, 75 FetchResult* result) {
64 ServiceWorkerFetchResponse* out_response) { 76 return base::Bind(&ReceiveFetchResult, run_quit_thread, quit, result);
65 return base::Bind(
66 &ReceiveFetchResult, run_quit_thread, quit, out_status, out_response);
67 } 77 }
68 78
69 } // namespace 79 } // namespace
70 80
71 class ServiceWorkerBrowserTest : public ContentBrowserTest { 81 class ServiceWorkerBrowserTest : public ContentBrowserTest {
72 protected: 82 protected:
73 typedef ServiceWorkerBrowserTest self; 83 typedef ServiceWorkerBrowserTest self;
74 84
75 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 85 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
76 command_line->AppendSwitch(switches::kEnableServiceWorker); 86 command_line->AppendSwitch(switches::kEnableServiceWorker);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 status = SERVICE_WORKER_ERROR_FAILED; 235 status = SERVICE_WORKER_ERROR_FAILED;
226 base::RunLoop stop_run_loop; 236 base::RunLoop stop_run_loop;
227 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 237 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
228 base::Bind(&self::StopOnIOThread, this, 238 base::Bind(&self::StopOnIOThread, this,
229 stop_run_loop.QuitClosure(), 239 stop_run_loop.QuitClosure(),
230 &status)); 240 &status));
231 stop_run_loop.Run(); 241 stop_run_loop.Run();
232 ASSERT_EQ(SERVICE_WORKER_OK, status); 242 ASSERT_EQ(SERVICE_WORKER_OK, status);
233 } 243 }
234 244
245 void FetchTestHelper(const std::string& worker_url,
246 ServiceWorkerFetchEventResult* result,
247 ServiceWorkerResponse* response) {
248 RunOnIOThread(
249 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url));
250
251 FetchResult fetch_result;
252 fetch_result.status = SERVICE_WORKER_ERROR_FAILED;
253 base::RunLoop fetch_run_loop;
254 BrowserThread::PostTask(BrowserThread::IO,
255 FROM_HERE,
256 base::Bind(&self::FetchOnIOThread,
257 this,
258 fetch_run_loop.QuitClosure(),
259 &fetch_result));
260 fetch_run_loop.Run();
261 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status);
262 *result = fetch_result.result;
263 *response = fetch_result.response;
264 }
265
235 void SetUpRegistrationOnIOThread(const std::string& worker_url) { 266 void SetUpRegistrationOnIOThread(const std::string& worker_url) {
236 const int64 version_id = 1L; 267 const int64 version_id = 1L;
237 registration_ = new ServiceWorkerRegistration( 268 registration_ = new ServiceWorkerRegistration(
238 embedded_test_server()->GetURL("/*"), 269 embedded_test_server()->GetURL("/*"),
239 embedded_test_server()->GetURL(worker_url), 270 embedded_test_server()->GetURL(worker_url),
240 next_registration_id_++); 271 next_registration_id_++);
241 version_ = new ServiceWorkerVersion( 272 version_ = new ServiceWorkerVersion(
242 registration_, 273 registration_,
243 wrapper()->context()->embedded_worker_registry(), 274 wrapper()->context()->embedded_worker_registry(),
244 version_id); 275 version_id);
245 AssociateRendererProcessToWorker(version_->embedded_worker()); 276 AssociateRendererProcessToWorker(version_->embedded_worker());
246 } 277 }
247 278
248 void StartOnIOThread(const base::Closure& done, 279 void StartOnIOThread(const base::Closure& done,
249 ServiceWorkerStatusCode* result) { 280 ServiceWorkerStatusCode* result) {
250 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 281 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
251 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); 282 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result));
252 } 283 }
253 284
254 void InstallOnIOThread(const base::Closure& done, 285 void InstallOnIOThread(const base::Closure& done,
255 ServiceWorkerStatusCode* result) { 286 ServiceWorkerStatusCode* result) {
256 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 287 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
257 version_->DispatchInstallEvent( 288 version_->DispatchInstallEvent(
258 -1, CreateReceiver(BrowserThread::UI, done, result)); 289 -1, CreateReceiver(BrowserThread::UI, done, result));
259 } 290 }
260 291
261 void FetchOnIOThread(const base::Closure& done, 292 void FetchOnIOThread(const base::Closure& done, FetchResult* result) {
262 ServiceWorkerStatusCode* result,
263 ServiceWorkerFetchResponse* message) {
264 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 293 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
265 ServiceWorkerFetchRequest request( 294 ServiceWorkerFetchRequest request(
266 embedded_test_server()->GetURL("/service_worker/empty.html"), 295 embedded_test_server()->GetURL("/service_worker/empty.html"),
267 "GET", 296 "GET",
268 std::map<std::string, std::string>()); 297 std::map<std::string, std::string>());
269 version_->DispatchFetchEvent( 298 version_->DispatchFetchEvent(
270 request, 299 request, CreateResponseReceiver(BrowserThread::UI, done, result));
271 CreateFetchResponseReceiver(BrowserThread::UI, done, result, message));
272 } 300 }
273 301
274
275 void StopOnIOThread(const base::Closure& done, 302 void StopOnIOThread(const base::Closure& done,
276 ServiceWorkerStatusCode* result) { 303 ServiceWorkerStatusCode* result) {
277 ASSERT_TRUE(version_); 304 ASSERT_TRUE(version_);
278 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); 305 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result));
279 } 306 }
280 307
281 protected: 308 protected:
282 int64 next_registration_id_; 309 int64 next_registration_id_;
283 scoped_refptr<ServiceWorkerRegistration> registration_; 310 scoped_refptr<ServiceWorkerRegistration> registration_;
284 scoped_refptr<ServiceWorkerVersion> version_; 311 scoped_refptr<ServiceWorkerVersion> version_;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 InstallTestHelper("/service_worker/worker_install_fulfilled.js"); 380 InstallTestHelper("/service_worker/worker_install_fulfilled.js");
354 } 381 }
355 382
356 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 383 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
357 InstallWithWaitUntil_Rejected) { 384 InstallWithWaitUntil_Rejected) {
358 // TODO(kinuko): This should also report back an error, but we 385 // TODO(kinuko): This should also report back an error, but we
359 // don't have plumbing for it yet. 386 // don't have plumbing for it yet.
360 InstallTestHelper("/service_worker/worker_install_rejected.js"); 387 InstallTestHelper("/service_worker/worker_install_rejected.js");
361 } 388 }
362 389
363 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Fetch) { 390 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Response) {
364 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 391 ServiceWorkerFetchEventResult result;
365 "/service_worker/worker.js")); 392 ServiceWorkerResponse response;
366 393 FetchTestHelper("/service_worker/fetch_event.js", &result, &response);
367 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 394 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
368 ServiceWorkerFetchResponse response;
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); 395 ASSERT_EQ(200, response.status_code);
377 ASSERT_EQ("OK", response.status_text); 396 ASSERT_EQ("OK", response.status_text);
378 ASSERT_EQ("GET", response.method); 397 ASSERT_EQ("GET", response.method);
379 std::map<std::string, std::string> expected_headers; 398 std::map<std::string, std::string> expected_headers;
380 ASSERT_EQ(expected_headers, response.headers); 399 ASSERT_EQ(expected_headers, response.headers);
381 } 400 }
382 401
402 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
403 FetchEvent_FallbackToNative) {
404 ServiceWorkerFetchEventResult result;
405 ServiceWorkerResponse response;
406 FetchTestHelper(
407 "/service_worker/fetch_event_fallback.js", &result, &response);
408 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
409 }
410
411 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) {
412 ServiceWorkerFetchEventResult result;
413 ServiceWorkerResponse response;
414 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response);
415 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
416 }
417
383 } // namespace content 418 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698