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

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

Issue 127573002: Add basic browser test for EmbeddedWorker/ServiceWorker (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "base/run_loop.h"
10 #include "content/browser/service_worker/embedded_worker_instance.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/shell/browser/shell.h"
20 #include "content/test/content_browser_test.h"
21 #include "content/test/content_browser_test_utils.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
23
24 namespace content {
25
26 class ServiceWorkerBrowserTest : public ContentBrowserTest,
27 public EmbeddedWorkerInstance::Observer {
28 public:
29 typedef ServiceWorkerBrowserTest self;
30
31 ServiceWorkerBrowserTest() {}
32 virtual ~ServiceWorkerBrowserTest() {}
33
34 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
35 command_line->AppendSwitch(switches::kEnableServiceWorker);
36 }
37
38 virtual void SetUpOnMainThread() OVERRIDE {
39 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
40 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition(
41 shell()->web_contents()->GetBrowserContext());
42 wrapper_ = partition->GetServiceWorkerContext();
43 }
44
45 virtual void TearDownOnMainThread() OVERRIDE {
46 base::RunLoop run_loop;
47 BrowserThread::PostTask(
48 BrowserThread::IO, FROM_HERE,
49 base::Bind(&self::TearDownOnIOThread, this, run_loop.QuitClosure()));
50 run_loop.Run();
51 wrapper_ = NULL;
52 }
53
54 void TearDownOnIOThread(const base::Closure& done_closure) {
55 if (worker_) {
56 worker_->RemoveObserver(this);
57 worker_.reset();
58 }
59 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure);
60 }
61
62 void StartEmbeddedWorkerOnIOThread() {
63 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
64 worker_ = wrapper_->context()->embedded_worker_registry()->CreateWorker();
65 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status());
66 worker_->AddObserver(this);
67
68 // TODO(kinuko): this manual wiring should go away when this gets wired
69 // in the actual code path.
70 ServiceWorkerProviderHost* provider_host = GetRegisteredProviderHost();
71 worker_->AddProcessReference(provider_host->process_id());
72
73 const int64 service_worker_version_id = 33L;
74 const GURL script_url = embedded_test_server()->GetURL(
75 "/service_worker/worker.js");
76 EXPECT_TRUE(worker_->Start(service_worker_version_id, script_url));
77 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker_->status());
78 }
79
80 void StopEmbeddedWorkerOnIOThread() {
81 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
82 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status());
83
84 EXPECT_TRUE(worker_->Stop());
85 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker_->status());
86 }
87
88 protected:
89 virtual void OnStarted() OVERRIDE {
90 ASSERT_TRUE(worker_ != NULL);
91 ASSERT_FALSE(done_closure_.is_null());
92 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status());
93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
94 }
95
96 virtual void OnStopped() OVERRIDE {
97 ASSERT_TRUE(worker_ != NULL);
98 ASSERT_FALSE(done_closure_.is_null());
99 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status());
100 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
101 }
102
103 ServiceWorkerProviderHost* GetRegisteredProviderHost() {
104 // Assumes only one provider host is registered at this point.
105 std::vector<ServiceWorkerProviderHost*> providers;
106 wrapper_->context()->GetAllProviderHosts(&providers);
107 DCHECK_EQ(1U, providers.size());
108 return providers[0];
109 }
110
111 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
112 scoped_ptr<EmbeddedWorkerInstance> worker_;
113 base::Closure done_closure_;
114 };
115
116 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, EmbeddedWorkerBasic) {
117 // Navigate to the page to set up a provider.
118 NavigateToURLBlockUntilNavigationsComplete(
119 shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1);
120
121 // Start a worker and wait until OnStarted() is called.
122 base::RunLoop start_run_loop;
123 done_closure_ = start_run_loop.QuitClosure();
124 BrowserThread::PostTask(
125 BrowserThread::IO, FROM_HERE,
126 base::Bind(&self::StartEmbeddedWorkerOnIOThread, this));
127 start_run_loop.Run();
alecflett 2014/01/08 23:02:15 is it worth putting a check here that the worker i
kinuko 2014/01/09 09:08:54 Makes sense, I didn't include many cautious guards
128
129 // Stop a worker and wait until OnStopped() is called.
130 base::RunLoop stop_run_loop;
131 done_closure_ = stop_run_loop.QuitClosure();
132 BrowserThread::PostTask(
133 BrowserThread::IO, FROM_HERE,
134 base::Bind(&self::StopEmbeddedWorkerOnIOThread, this));
135 stop_run_loop.Run();
136 }
137
138 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698