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.
5 #include "content/browser/devtools/embedded_worker_devtools_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/devtools/devtools_agent_host_impl.h"
12 #include "content/browser/devtools/embedded_worker_devtools_agent_host.h"
13 #include "content/browser/shared_worker/shared_worker_instance.h"
14 #include "content/browser/shared_worker/worker_storage_partition.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 class TestDevToolsClientHost
: public DevToolsAgentHostClient
{
23 TestDevToolsClientHost() {}
24 virtual ~TestDevToolsClientHost() {}
25 virtual void DispatchProtocolMessage(
26 DevToolsAgentHost
* agent_host
, const std::string
& message
) OVERRIDE
{}
27 virtual void AgentHostClosed(
28 DevToolsAgentHost
* agent_host
, bool replaced
) OVERRIDE
{}
30 void InspectAgentHost(DevToolsAgentHost
* agent_host
) {
31 if (agent_host_
.get())
32 agent_host_
->DetachClient();
33 agent_host_
= agent_host
;
34 if (agent_host_
.get())
35 agent_host_
->AttachClient(this);
38 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
39 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost
);
43 class EmbeddedWorkerDevToolsManagerTest
: public testing::Test
{
45 typedef EmbeddedWorkerDevToolsAgentHost::WorkerState WorkerState
;
47 EmbeddedWorkerDevToolsManagerTest()
48 : ui_thread_(BrowserThread::UI
, &message_loop_
),
49 browser_context_(new TestBrowserContext()),
51 new WorkerStoragePartition(browser_context_
->GetRequestContext(),
59 partition_id_(*partition_
.get()) {}
62 virtual void SetUp() OVERRIDE
{
63 manager_
= EmbeddedWorkerDevToolsManager::GetInstance();
65 virtual void TearDown() OVERRIDE
{
66 EmbeddedWorkerDevToolsManager::GetInstance()->ResetForTesting();
69 void CheckWorkerState(int worker_process_id
,
72 const EmbeddedWorkerDevToolsManager::WorkerId
id(worker_process_id
,
74 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator it
=
75 manager_
->workers_
.find(id
);
76 EXPECT_TRUE(manager_
->workers_
.end() != it
);
77 EXPECT_EQ(state
, it
->second
->state_
);
80 void CheckWorkerNotExist(int worker_process_id
, int worker_route_id
) {
81 const EmbeddedWorkerDevToolsManager::WorkerId
id(worker_process_id
,
83 EXPECT_TRUE(manager_
->workers_
.end() == manager_
->workers_
.find(id
));
86 void CheckWorkerCount(size_t size
) {
87 EXPECT_EQ(size
, manager_
->workers_
.size());
90 base::MessageLoopForIO message_loop_
;
91 BrowserThreadImpl ui_thread_
;
92 scoped_ptr
<TestBrowserContext
> browser_context_
;
93 scoped_ptr
<WorkerStoragePartition
> partition_
;
94 const WorkerStoragePartitionId partition_id_
;
95 EmbeddedWorkerDevToolsManager
* manager_
;
98 TEST_F(EmbeddedWorkerDevToolsManagerTest
, BasicTest
) {
99 scoped_refptr
<DevToolsAgentHostImpl
> agent_host
;
101 SharedWorkerInstance
instance1(GURL("http://example.com/w.js"),
104 blink::WebContentSecurityPolicyTypeReport
,
105 browser_context_
->GetResourceContext(),
108 agent_host
= manager_
->GetDevToolsAgentHostForWorker(1, 1);
109 EXPECT_FALSE(agent_host
.get());
111 // Created -> Started -> Destroyed
112 CheckWorkerNotExist(1, 1);
113 manager_
->SharedWorkerCreated(1, 1, instance1
);
114 CheckWorkerState(1, 1, WorkerState::WORKER_UNINSPECTED
);
115 manager_
->WorkerReadyForInspection(1, 1);
116 manager_
->WorkerContextStarted(1, 1);
117 CheckWorkerState(1, 1, WorkerState::WORKER_UNINSPECTED
);
118 manager_
->WorkerDestroyed(1, 1);
119 CheckWorkerNotExist(1, 1);
121 // Created -> GetDevToolsAgentHost -> Started -> Destroyed
122 CheckWorkerNotExist(1, 2);
123 manager_
->SharedWorkerCreated(1, 2, instance1
);
124 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED
);
125 agent_host
= manager_
->GetDevToolsAgentHostForWorker(1, 2);
126 EXPECT_TRUE(agent_host
.get());
127 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED
);
128 EXPECT_EQ(agent_host
.get(), manager_
->GetDevToolsAgentHostForWorker(1, 2));
129 manager_
->WorkerReadyForInspection(1, 2);
130 manager_
->WorkerContextStarted(1, 2);
131 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED
);
132 manager_
->WorkerDestroyed(1, 2);
133 CheckWorkerState(1, 2, WorkerState::WORKER_TERMINATED
);
135 CheckWorkerNotExist(1, 2);
137 // Created -> Started -> GetDevToolsAgentHost -> Destroyed
138 CheckWorkerNotExist(1, 3);
139 manager_
->SharedWorkerCreated(1, 3, instance1
);
140 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED
);
141 manager_
->WorkerReadyForInspection(1, 3);
142 manager_
->WorkerContextStarted(1, 3);
143 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED
);
144 agent_host
= manager_
->GetDevToolsAgentHostForWorker(1, 3);
145 EXPECT_TRUE(agent_host
.get());
146 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED
);
147 manager_
->WorkerDestroyed(1, 3);
148 CheckWorkerState(1, 3, WorkerState::WORKER_TERMINATED
);
150 CheckWorkerNotExist(1, 3);
152 // Created -> Destroyed
153 CheckWorkerNotExist(1, 4);
154 manager_
->SharedWorkerCreated(1, 4, instance1
);
155 CheckWorkerState(1, 4, WorkerState::WORKER_UNINSPECTED
);
156 manager_
->WorkerDestroyed(1, 4);
157 CheckWorkerNotExist(1, 4);
159 // Created -> GetDevToolsAgentHost -> Destroyed
160 CheckWorkerNotExist(1, 5);
161 manager_
->SharedWorkerCreated(1, 5, instance1
);
162 CheckWorkerState(1, 5, WorkerState::WORKER_UNINSPECTED
);
163 agent_host
= manager_
->GetDevToolsAgentHostForWorker(1, 5);
164 EXPECT_TRUE(agent_host
.get());
165 CheckWorkerState(1, 5, WorkerState::WORKER_UNINSPECTED
);
166 manager_
->WorkerDestroyed(1, 5);
167 CheckWorkerState(1, 5, WorkerState::WORKER_TERMINATED
);
169 CheckWorkerNotExist(1, 5);
171 // Created -> GetDevToolsAgentHost -> Free agent_host -> Destroyed
172 CheckWorkerNotExist(1, 6);
173 manager_
->SharedWorkerCreated(1, 6, instance1
);
174 CheckWorkerState(1, 6, WorkerState::WORKER_UNINSPECTED
);
175 agent_host
= manager_
->GetDevToolsAgentHostForWorker(1, 6);
176 EXPECT_TRUE(agent_host
.get());
177 CheckWorkerState(1, 6, WorkerState::WORKER_UNINSPECTED
);
179 manager_
->WorkerDestroyed(1, 6);
180 CheckWorkerNotExist(1, 6);
183 TEST_F(EmbeddedWorkerDevToolsManagerTest
, AttachTest
) {
184 scoped_refptr
<DevToolsAgentHostImpl
> agent_host1
;
185 scoped_refptr
<DevToolsAgentHostImpl
> agent_host2
;
187 SharedWorkerInstance
instance1(GURL("http://example.com/w1.js"),
190 blink::WebContentSecurityPolicyTypeReport
,
191 browser_context_
->GetResourceContext(),
193 SharedWorkerInstance
instance2(GURL("http://example.com/w2.js"),
196 blink::WebContentSecurityPolicyTypeReport
,
197 browser_context_
->GetResourceContext(),
200 // Created -> GetDevToolsAgentHost -> Register -> Started -> Destroyed
201 scoped_ptr
<TestDevToolsClientHost
> client_host1(new TestDevToolsClientHost());
202 CheckWorkerNotExist(2, 1);
203 manager_
->SharedWorkerCreated(2, 1, instance1
);
204 CheckWorkerState(2, 1, WorkerState::WORKER_UNINSPECTED
);
205 agent_host1
= manager_
->GetDevToolsAgentHostForWorker(2, 1);
206 EXPECT_TRUE(agent_host1
.get());
207 CheckWorkerState(2, 1, WorkerState::WORKER_UNINSPECTED
);
208 EXPECT_EQ(agent_host1
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 1));
209 client_host1
->InspectAgentHost(agent_host1
.get());
210 CheckWorkerState(2, 1, WorkerState::WORKER_INSPECTED
);
211 manager_
->WorkerReadyForInspection(2, 1);
212 manager_
->WorkerContextStarted(2, 1);
213 CheckWorkerState(2, 1, WorkerState::WORKER_INSPECTED
);
214 manager_
->WorkerDestroyed(2, 1);
215 CheckWorkerState(2, 1, WorkerState::WORKER_TERMINATED
);
216 EXPECT_EQ(agent_host1
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 1));
218 // Created -> Started -> GetDevToolsAgentHost -> Register -> Destroyed
219 scoped_ptr
<TestDevToolsClientHost
> client_host2(new TestDevToolsClientHost());
220 manager_
->SharedWorkerCreated(2, 2, instance2
);
221 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED
);
222 manager_
->WorkerReadyForInspection(2, 2);
223 manager_
->WorkerContextStarted(2, 2);
224 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED
);
225 agent_host2
= manager_
->GetDevToolsAgentHostForWorker(2, 2);
226 EXPECT_TRUE(agent_host2
.get());
227 EXPECT_NE(agent_host1
.get(), agent_host2
.get());
228 EXPECT_EQ(agent_host2
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 2));
229 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED
);
230 client_host2
->InspectAgentHost(agent_host2
.get());
231 CheckWorkerState(2, 2, WorkerState::WORKER_INSPECTED
);
232 manager_
->WorkerDestroyed(2, 2);
233 CheckWorkerState(2, 2, WorkerState::WORKER_TERMINATED
);
234 EXPECT_EQ(agent_host2
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 2));
236 // Re-created -> Started -> ClientHostClosing -> Destroyed
237 CheckWorkerState(2, 1, WorkerState::WORKER_TERMINATED
);
238 manager_
->SharedWorkerCreated(2, 3, instance1
);
239 CheckWorkerNotExist(2, 1);
240 CheckWorkerState(2, 3, WorkerState::WORKER_PAUSED_FOR_REATTACH
);
241 EXPECT_EQ(agent_host1
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 3));
242 manager_
->WorkerReadyForInspection(2, 3);
243 manager_
->WorkerContextStarted(2, 3);
244 CheckWorkerState(2, 3, WorkerState::WORKER_INSPECTED
);
245 client_host1
->InspectAgentHost(NULL
);
246 manager_
->WorkerDestroyed(2, 3);
247 CheckWorkerState(2, 3, WorkerState::WORKER_TERMINATED
);
249 CheckWorkerNotExist(2, 3);
251 // Re-created -> Destroyed
252 CheckWorkerState(2, 2, WorkerState::WORKER_TERMINATED
);
253 manager_
->SharedWorkerCreated(2, 4, instance2
);
254 CheckWorkerNotExist(2, 2);
255 CheckWorkerState(2, 4, WorkerState::WORKER_PAUSED_FOR_REATTACH
);
256 EXPECT_EQ(agent_host2
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 4));
257 manager_
->WorkerDestroyed(2, 4);
258 CheckWorkerNotExist(2, 2);
259 CheckWorkerState(2, 4, WorkerState::WORKER_TERMINATED
);
261 // Re-created -> ClientHostClosing -> Destroyed
262 manager_
->SharedWorkerCreated(2, 5, instance2
);
263 CheckWorkerNotExist(2, 2);
264 CheckWorkerState(2, 5, WorkerState::WORKER_PAUSED_FOR_REATTACH
);
265 EXPECT_EQ(agent_host2
.get(), manager_
->GetDevToolsAgentHostForWorker(2, 5));
266 client_host2
->InspectAgentHost(NULL
);
270 manager_
->WorkerDestroyed(2, 5);
274 TEST_F(EmbeddedWorkerDevToolsManagerTest
, ReattachTest
) {
275 SharedWorkerInstance
instance(GURL("http://example.com/w3.js"),
278 blink::WebContentSecurityPolicyTypeReport
,
279 browser_context_
->GetResourceContext(),
281 scoped_ptr
<TestDevToolsClientHost
> client_host(new TestDevToolsClientHost());
282 // Created -> GetDevToolsAgentHost -> Register -> Destroyed
283 manager_
->SharedWorkerCreated(3, 1, instance
);
284 CheckWorkerState(3, 1, WorkerState::WORKER_UNINSPECTED
);
285 scoped_refptr
<DevToolsAgentHost
> agent_host(
286 manager_
->GetDevToolsAgentHostForWorker(3, 1));
287 EXPECT_TRUE(agent_host
.get());
288 CheckWorkerState(3, 1, WorkerState::WORKER_UNINSPECTED
);
289 client_host
->InspectAgentHost(agent_host
.get());
290 CheckWorkerState(3, 1, WorkerState::WORKER_INSPECTED
);
291 manager_
->WorkerDestroyed(3, 1);
292 CheckWorkerState(3, 1, WorkerState::WORKER_TERMINATED
);
293 // ClientHostClosing -> Re-created -> release agent_host -> Destroyed
294 client_host
->InspectAgentHost(NULL
);
295 CheckWorkerState(3, 1, WorkerState::WORKER_TERMINATED
);
296 manager_
->SharedWorkerCreated(3, 2, instance
);
297 CheckWorkerState(3, 2, WorkerState::WORKER_UNINSPECTED
);
299 CheckWorkerState(3, 2, WorkerState::WORKER_UNINSPECTED
);
300 manager_
->WorkerDestroyed(3, 2);
301 CheckWorkerNotExist(3, 2);
305 } // namespace content