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

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: Switch to a bool 'success' result. Created 6 years, 8 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"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 {
436 public:
437 typedef ServiceWorkerBlackBoxBrowserTest self;
438
439 static void ExpectResultAndRun(bool expected,
440 const base::Closure& continuation,
441 bool 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,
460 continuation));
461 }
462
463 void FindRegistrationOnIO2(
464 ServiceWorkerStatusCode* out_status,
465 GURL* script_url,
466 const base::Closure& continuation,
467 ServiceWorkerStatusCode status,
468 const scoped_refptr<ServiceWorkerRegistration>& registration) {
469 *out_status = status;
470 if (registration) {
471 *script_url = registration->script_url();
472 } else {
473 EXPECT_NE(SERVICE_WORKER_OK, status);
474 }
475 continuation.Run();
476 }
477 };
478
479 IN_PROC_BROWSER_TEST_F(ServiceWorkerBlackBoxBrowserTest, Registration) {
480 const std::string kWorkerUrl = "/service_worker/fetch_event.js";
481 {
482 base::RunLoop run_loop;
483 public_context()->RegisterServiceWorker(
484 embedded_test_server()->GetURL("/*"),
485 embedded_test_server()->GetURL(kWorkerUrl),
486 RenderProcessID(),
487 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun,
488 true,
489 run_loop.QuitClosure()));
490 run_loop.Run();
491 }
492 {
493 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
494 GURL script_url;
495 RunOnIOThread(
496 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO,
497 this,
498 embedded_test_server()->GetURL("/service_worker/empty.html"),
499 &status,
500 &script_url));
501 EXPECT_EQ(SERVICE_WORKER_OK, status);
502 EXPECT_EQ(embedded_test_server()->GetURL(kWorkerUrl), script_url);
503 }
504 {
505 base::RunLoop run_loop;
506 public_context()->UnregisterServiceWorker(
507 embedded_test_server()->GetURL("/*"),
508 RenderProcessID(),
509 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun,
510 true,
511 run_loop.QuitClosure()));
512 run_loop.Run();
513 }
514 {
515 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
516 GURL script_url;
517 RunOnIOThread(
518 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO,
519 this,
520 embedded_test_server()->GetURL("/service_worker/empty.html"),
521 &status,
522 &script_url));
523 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status);
524 }
525 }
526
419 } // namespace content 527 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698