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

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

Issue 208843004: Add the beginning of a public Service Worker API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix #include guard name 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
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/common/service_worker/service_worker_types.h"
19 #include "content/public/browser/browser_context.h" 18 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_process_host.h" 20 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/storage_partition.h" 21 #include "content/public/browser/storage_partition.h"
23 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/service_worker_status_code.h"
25 #include "content/shell/browser/shell.h" 25 #include "content/shell/browser/shell.h"
26 #include "content/test/content_browser_test.h" 26 #include "content/test/content_browser_test.h"
27 #include "content/test/content_browser_test_utils.h" 27 #include "content/test/content_browser_test_utils.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h" 28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 29
30 namespace content { 30 namespace content {
31 31
32 namespace { 32 namespace {
33 33
34 struct FetchResult { 34 struct FetchResult {
(...skipping 11 matching lines...) Expand all
46 46
47 void RunOnIOThread(const base::Closure& closure) { 47 void RunOnIOThread(const base::Closure& closure) {
48 base::RunLoop run_loop; 48 base::RunLoop run_loop;
49 BrowserThread::PostTask( 49 BrowserThread::PostTask(
50 BrowserThread::IO, FROM_HERE, 50 BrowserThread::IO, FROM_HERE,
51 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(), 51 base::Bind(&RunAndQuit, closure, run_loop.QuitClosure(),
52 base::MessageLoopProxy::current())); 52 base::MessageLoopProxy::current()));
53 run_loop.Run(); 53 run_loop.Run();
54 } 54 }
55 55
56 void RunOnIOThread(
57 const base::Callback<void(const base::Closure& continuation)>& closure) {
58 base::RunLoop run_loop;
59 base::Closure quit_on_original_thread =
60 base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask),
61 base::MessageLoopProxy::current().get(),
62 FROM_HERE,
63 run_loop.QuitClosure());
64 BrowserThread::PostTask(BrowserThread::IO,
65 FROM_HERE,
66 base::Bind(closure, quit_on_original_thread));
67 run_loop.Run();
68 }
69
56 // Contrary to the style guide, the output parameter of this function comes 70 // 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 71 // before input parameters so Bind can be used on it to create a FetchCallback
58 // to pass to DispatchFetchEvent. 72 // to pass to DispatchFetchEvent.
59 void ReceiveFetchResult(BrowserThread::ID run_quit_thread, 73 void ReceiveFetchResult(BrowserThread::ID run_quit_thread,
60 const base::Closure& quit, 74 const base::Closure& quit,
61 FetchResult* out_result, 75 FetchResult* out_result,
62 ServiceWorkerStatusCode actual_status, 76 ServiceWorkerStatusCode actual_status,
63 ServiceWorkerFetchEventResult actual_result, 77 ServiceWorkerFetchEventResult actual_result,
64 const ServiceWorkerResponse& actual_response) { 78 const ServiceWorkerResponse& actual_response) {
65 out_result->status = actual_status; 79 out_result->status = actual_status;
(...skipping 17 matching lines...) Expand all
83 typedef ServiceWorkerBrowserTest self; 97 typedef ServiceWorkerBrowserTest self;
84 98
85 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 99 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
86 command_line->AppendSwitch(switches::kEnableServiceWorker); 100 command_line->AppendSwitch(switches::kEnableServiceWorker);
87 } 101 }
88 102
89 virtual void SetUpOnMainThread() OVERRIDE { 103 virtual void SetUpOnMainThread() OVERRIDE {
90 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 104 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
91 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( 105 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition(
92 shell()->web_contents()->GetBrowserContext()); 106 shell()->web_contents()->GetBrowserContext());
93 wrapper_ = partition->GetServiceWorkerContext(); 107 wrapper_ = static_cast<ServiceWorkerContextWrapper*>(
108 partition->GetServiceWorkerContext());
94 109
95 // Navigate to the page to set up a renderer page (where we can embed 110 // Navigate to the page to set up a renderer page (where we can embed
96 // a worker). 111 // a worker).
97 NavigateToURLBlockUntilNavigationsComplete( 112 NavigateToURLBlockUntilNavigationsComplete(
98 shell(), 113 shell(),
99 embedded_test_server()->GetURL("/service_worker/empty.html"), 1); 114 embedded_test_server()->GetURL("/service_worker/empty.html"), 1);
100 115
101 RunOnIOThread(base::Bind(&self::SetUpOnIOThread, this)); 116 RunOnIOThread(base::Bind(&self::SetUpOnIOThread, this));
102 } 117 }
103 118
104 virtual void TearDownOnMainThread() OVERRIDE { 119 virtual void TearDownOnMainThread() OVERRIDE {
105 RunOnIOThread(base::Bind(&self::TearDownOnIOThread, this)); 120 RunOnIOThread(base::Bind(&self::TearDownOnIOThread, this));
106 wrapper_ = NULL; 121 wrapper_ = NULL;
107 } 122 }
108 123
109 virtual void SetUpOnIOThread() {} 124 virtual void SetUpOnIOThread() {}
110 virtual void TearDownOnIOThread() {} 125 virtual void TearDownOnIOThread() {}
111 126
112 ServiceWorkerContextWrapper* wrapper() { return wrapper_.get(); } 127 ServiceWorkerContextWrapper* wrapper() { return wrapper_.get(); }
128 ServiceWorkerContext* public_context() { return wrapper(); }
113 129
114 void AssociateRendererProcessToWorker(EmbeddedWorkerInstance* worker) { 130 void AssociateRendererProcessToWorker(EmbeddedWorkerInstance* worker) {
115 worker->AddProcessReference( 131 worker->AddProcessReference(
116 shell()->web_contents()->GetRenderProcessHost()->GetID()); 132 shell()->web_contents()->GetRenderProcessHost()->GetID());
117 } 133 }
118 134
119 private: 135 private:
120 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; 136 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
121 }; 137 };
122 138
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); 425 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
410 } 426 }
411 427
412 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) { 428 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) {
413 ServiceWorkerFetchEventResult result; 429 ServiceWorkerFetchEventResult result;
414 ServiceWorkerResponse response; 430 ServiceWorkerResponse response;
415 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response); 431 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response);
416 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); 432 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result);
417 } 433 }
418 434
435 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest {
kinuko 2014/03/26 05:01:34 (Ok to have this for now, but this file's going a
Jeffrey Yasskin 2014/03/26 19:58:52 I agree with separate files. This test seems to be
436 public:
437 typedef ServiceWorkerBlackBoxBrowserTest self;
438
439 static void ExpectStatusAndRun(ServiceWorkerStatusCode expected,
440 const base::Closure& continuation,
441 ServiceWorkerStatusCode actual) {
442 EXPECT_EQ(expected, actual);
443 continuation.Run();
444 }
445
446 int RenderProcessID() {
447 return shell()->web_contents()->GetRenderProcessHost()->GetID();
448 }
449
450 void FindRegistrationOnIO(const GURL& document_url,
451 ServiceWorkerStatusCode* status,
452 GURL* script_url,
453 const base::Closure& continuation) {
454 wrapper()->context()->storage()->FindRegistrationForDocument(
455 document_url,
456 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO2,
457 this,
458 status,
459 script_url, continuation));
460 }
461
462 void FindRegistrationOnIO2(
463 ServiceWorkerStatusCode* out_status,
464 GURL* script_url,
465 const base::Closure& continuation,
466 ServiceWorkerStatusCode status,
467 const scoped_refptr<ServiceWorkerRegistration>& registration) {
468 *out_status = status;
469 if (registration) {
470 *script_url = registration->script_url();
471 } else {
472 EXPECT_NE(SERVICE_WORKER_OK, status);
473 }
474 continuation.Run();
475 }
476 };
477
478 IN_PROC_BROWSER_TEST_F(ServiceWorkerBlackBoxBrowserTest, Registration) {
479 const std::string kWorkerUrl = "/service_worker/fetch_event.js";
480 {
481 base::RunLoop run_loop;
482 public_context()->RegisterServiceWorker(
483 embedded_test_server()->GetURL("/*"),
484 embedded_test_server()->GetURL(kWorkerUrl),
485 RenderProcessID(),
486 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectStatusAndRun,
487 SERVICE_WORKER_OK,
488 run_loop.QuitClosure()));
489 run_loop.Run();
490 }
491 {
492 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
493 GURL script_url;
494 RunOnIOThread(
495 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO,
496 this,
497 embedded_test_server()->GetURL("/service_worker/empty.html"),
498 &status,
499 &script_url));
500 EXPECT_EQ(SERVICE_WORKER_OK, status);
501 EXPECT_EQ(embedded_test_server()->GetURL(kWorkerUrl), script_url);
502 }
503 {
504 base::RunLoop run_loop;
505 public_context()->UnregisterServiceWorker(
506 embedded_test_server()->GetURL("/*"),
507 RenderProcessID(),
508 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectStatusAndRun,
509 SERVICE_WORKER_OK,
510 run_loop.QuitClosure()));
511 run_loop.Run();
512 }
513 {
514 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
515 GURL script_url;
516 RunOnIOThread(
517 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO,
518 this,
519 embedded_test_server()->GetURL("/service_worker/empty.html"),
520 &status,
521 &script_url));
522 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status);
523 }
524 }
525
419 } // namespace content 526 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698