1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/run_loop.h"
7 #include "base/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_instance.h"
9 #include "content/browser/service_worker/embedded_worker_registry.h"
10 #include "content/browser/service_worker/embedded_worker_test_helper.h"
11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/common/service_worker/embedded_worker_messages.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 const int kRenderProcessId
= 11;
24 void SaveStatusAndCall(ServiceWorkerStatusCode
* out
,
25 const base::Closure
& callback
,
26 ServiceWorkerStatusCode status
) {
33 class EmbeddedWorkerInstanceTest
: public testing::Test
{
35 EmbeddedWorkerInstanceTest()
36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
38 void SetUp() override
{
40 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
));
43 void TearDown() override
{ helper_
.reset(); }
45 ServiceWorkerStatusCode
StartWorker(EmbeddedWorkerInstance
* worker
,
46 int id
, const GURL
& pattern
,
48 ServiceWorkerStatusCode status
;
49 base::RunLoop run_loop
;
50 worker
->Start(id
, pattern
, url
, false,
51 base::Bind(&SaveStatusAndCall
, &status
,
52 run_loop
.QuitClosure()));
57 ServiceWorkerContextCore
* context() { return helper_
->context(); }
59 EmbeddedWorkerRegistry
* embedded_worker_registry() {
61 return context()->embedded_worker_registry();
64 IPC::TestSink
* ipc_sink() { return helper_
->ipc_sink(); }
66 TestBrowserThreadBundle thread_bundle_
;
67 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
70 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest
);
73 TEST_F(EmbeddedWorkerInstanceTest
, StartAndStop
) {
74 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
75 embedded_worker_registry()->CreateWorker();
76 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
78 const int64 service_worker_version_id
= 55L;
79 const GURL
pattern("http://example.com/");
80 const GURL
url("http://example.com/worker.js");
82 // Simulate adding one process to the pattern.
83 helper_
->SimulateAddProcessToPattern(pattern
, kRenderProcessId
);
85 // Start should succeed.
86 ServiceWorkerStatusCode status
;
87 base::RunLoop run_loop
;
89 service_worker_version_id
,
93 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
94 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
96 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
98 // The 'WorkerStarted' message should have been sent by
99 // EmbeddedWorkerTestHelper.
100 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
101 EXPECT_EQ(kRenderProcessId
, worker
->process_id());
104 EXPECT_EQ(SERVICE_WORKER_OK
, worker
->Stop());
105 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING
, worker
->status());
106 base::RunLoop().RunUntilIdle();
108 // The 'WorkerStopped' message should have been sent by
109 // EmbeddedWorkerTestHelper.
110 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
112 // Verify that we've sent two messages to start and terminate the worker.
113 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
114 EmbeddedWorkerMsg_StartWorker::ID
));
115 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
116 EmbeddedWorkerMsg_StopWorker::ID
));
119 TEST_F(EmbeddedWorkerInstanceTest
, StopWhenDevToolsAttached
) {
120 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
121 embedded_worker_registry()->CreateWorker();
122 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
124 const int64 service_worker_version_id
= 55L;
125 const GURL
pattern("http://example.com/");
126 const GURL
url("http://example.com/worker.js");
128 // Simulate adding one process to the pattern.
129 helper_
->SimulateAddProcessToPattern(pattern
, kRenderProcessId
);
131 // Start the worker and then call StopIfIdle().
132 EXPECT_EQ(SERVICE_WORKER_OK
,
133 StartWorker(worker
.get(), service_worker_version_id
, pattern
, url
));
134 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
135 EXPECT_EQ(kRenderProcessId
, worker
->process_id());
136 worker
->StopIfIdle();
137 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING
, worker
->status());
138 base::RunLoop().RunUntilIdle();
140 // The worker must be stopped now.
141 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
143 // Set devtools_attached to true, and do the same.
144 worker
->set_devtools_attached(true);
146 EXPECT_EQ(SERVICE_WORKER_OK
,
147 StartWorker(worker
.get(), service_worker_version_id
, pattern
, url
));
148 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
149 EXPECT_EQ(kRenderProcessId
, worker
->process_id());
150 worker
->StopIfIdle();
151 base::RunLoop().RunUntilIdle();
153 // The worker must not be stopped this time.
154 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
156 // Calling Stop() actually stops the worker regardless of whether devtools
157 // is attached or not.
158 EXPECT_EQ(SERVICE_WORKER_OK
, worker
->Stop());
159 base::RunLoop().RunUntilIdle();
160 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
163 TEST_F(EmbeddedWorkerInstanceTest
, InstanceDestroyedBeforeStartFinishes
) {
164 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
165 embedded_worker_registry()->CreateWorker();
166 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
168 const int64 service_worker_version_id
= 55L;
169 const GURL
pattern("http://example.com/");
170 const GURL
url("http://example.com/worker.js");
172 ServiceWorkerStatusCode status
;
173 base::RunLoop run_loop
;
174 // Begin starting the worker.
175 context()->process_manager()->AddProcessReferenceToPattern(
176 pattern
, kRenderProcessId
);
178 service_worker_version_id
,
182 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
183 // But destroy it before it gets a chance to complete.
186 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT
, status
);
188 // Verify that we didn't send the message to start the worker.
190 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID
));
193 } // namespace content