Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / devtools / shared_worker_devtools_manager_unittest.cc
blobb56c07bab3e8f2d8ac02cae663d5ae54659b0d39
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/shared_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/shared_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"
18 namespace content {
19 namespace {
21 class TestDevToolsClientHost : public DevToolsAgentHostClient {
22 public:
23 TestDevToolsClientHost() {}
24 ~TestDevToolsClientHost() override {}
25 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
26 const std::string& message) override {}
27 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {}
29 void InspectAgentHost(DevToolsAgentHost* agent_host) {
30 if (agent_host_.get())
31 agent_host_->DetachClient();
32 agent_host_ = agent_host;
33 if (agent_host_.get())
34 agent_host_->AttachClient(this);
36 private:
37 scoped_refptr<DevToolsAgentHost> agent_host_;
38 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost);
40 } // namespace
42 class SharedWorkerDevToolsManagerTest : public testing::Test {
43 public:
44 typedef SharedWorkerDevToolsAgentHost::WorkerState WorkerState;
46 SharedWorkerDevToolsManagerTest()
47 : ui_thread_(BrowserThread::UI, &message_loop_),
48 browser_context_(new TestBrowserContext()),
49 partition_(
50 new WorkerStoragePartition(browser_context_->GetRequestContext(),
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL,
56 NULL,
57 NULL)),
58 partition_id_(*partition_.get()) {}
60 protected:
61 void SetUp() override {
62 manager_ = SharedWorkerDevToolsManager::GetInstance();
64 void TearDown() override {
65 SharedWorkerDevToolsManager::GetInstance()->ResetForTesting();
68 void CheckWorkerState(int worker_process_id,
69 int worker_route_id,
70 WorkerState state) {
71 const SharedWorkerDevToolsManager::WorkerId id(worker_process_id,
72 worker_route_id);
73 SharedWorkerDevToolsManager::AgentHostMap::iterator it =
74 manager_->workers_.find(id);
75 EXPECT_TRUE(manager_->workers_.end() != it);
76 EXPECT_EQ(state, it->second->state_);
79 void CheckWorkerNotExist(int worker_process_id, int worker_route_id) {
80 const SharedWorkerDevToolsManager::WorkerId id(worker_process_id,
81 worker_route_id);
82 EXPECT_TRUE(manager_->workers_.end() == manager_->workers_.find(id));
85 void CheckWorkerCount(size_t size) {
86 EXPECT_EQ(size, manager_->workers_.size());
89 base::MessageLoopForIO message_loop_;
90 BrowserThreadImpl ui_thread_;
91 scoped_ptr<TestBrowserContext> browser_context_;
92 scoped_ptr<WorkerStoragePartition> partition_;
93 const WorkerStoragePartitionId partition_id_;
94 SharedWorkerDevToolsManager* manager_;
97 TEST_F(SharedWorkerDevToolsManagerTest, BasicTest) {
98 scoped_refptr<DevToolsAgentHostImpl> agent_host;
100 SharedWorkerInstance instance1(GURL("http://example.com/w.js"),
101 base::string16(),
102 base::string16(),
103 blink::WebContentSecurityPolicyTypeReport,
104 browser_context_->GetResourceContext(),
105 partition_id_);
107 agent_host = manager_->GetDevToolsAgentHostForWorker(1, 1);
108 EXPECT_FALSE(agent_host.get());
110 // Created -> Started -> Destroyed
111 CheckWorkerNotExist(1, 1);
112 manager_->WorkerCreated(1, 1, instance1);
113 CheckWorkerState(1, 1, WorkerState::WORKER_UNINSPECTED);
114 manager_->WorkerReadyForInspection(1, 1);
115 CheckWorkerState(1, 1, WorkerState::WORKER_UNINSPECTED);
116 manager_->WorkerDestroyed(1, 1);
117 CheckWorkerNotExist(1, 1);
119 // Created -> GetDevToolsAgentHost -> Started -> Destroyed
120 CheckWorkerNotExist(1, 2);
121 manager_->WorkerCreated(1, 2, instance1);
122 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED);
123 agent_host = manager_->GetDevToolsAgentHostForWorker(1, 2);
124 EXPECT_TRUE(agent_host.get());
125 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED);
126 EXPECT_EQ(agent_host.get(), manager_->GetDevToolsAgentHostForWorker(1, 2));
127 manager_->WorkerReadyForInspection(1, 2);
128 CheckWorkerState(1, 2, WorkerState::WORKER_UNINSPECTED);
129 manager_->WorkerDestroyed(1, 2);
130 CheckWorkerState(1, 2, WorkerState::WORKER_TERMINATED);
131 agent_host = NULL;
132 CheckWorkerNotExist(1, 2);
134 // Created -> Started -> GetDevToolsAgentHost -> Destroyed
135 CheckWorkerNotExist(1, 3);
136 manager_->WorkerCreated(1, 3, instance1);
137 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED);
138 manager_->WorkerReadyForInspection(1, 3);
139 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED);
140 agent_host = manager_->GetDevToolsAgentHostForWorker(1, 3);
141 EXPECT_TRUE(agent_host.get());
142 CheckWorkerState(1, 3, WorkerState::WORKER_UNINSPECTED);
143 manager_->WorkerDestroyed(1, 3);
144 CheckWorkerState(1, 3, WorkerState::WORKER_TERMINATED);
145 agent_host = NULL;
146 CheckWorkerNotExist(1, 3);
148 // Created -> Destroyed
149 CheckWorkerNotExist(1, 4);
150 manager_->WorkerCreated(1, 4, instance1);
151 CheckWorkerState(1, 4, WorkerState::WORKER_UNINSPECTED);
152 manager_->WorkerDestroyed(1, 4);
153 CheckWorkerNotExist(1, 4);
155 // Created -> GetDevToolsAgentHost -> Destroyed
156 CheckWorkerNotExist(1, 5);
157 manager_->WorkerCreated(1, 5, instance1);
158 CheckWorkerState(1, 5, WorkerState::WORKER_UNINSPECTED);
159 agent_host = manager_->GetDevToolsAgentHostForWorker(1, 5);
160 EXPECT_TRUE(agent_host.get());
161 CheckWorkerState(1, 5, WorkerState::WORKER_UNINSPECTED);
162 manager_->WorkerDestroyed(1, 5);
163 CheckWorkerState(1, 5, WorkerState::WORKER_TERMINATED);
164 agent_host = NULL;
165 CheckWorkerNotExist(1, 5);
167 // Created -> GetDevToolsAgentHost -> Free agent_host -> Destroyed
168 CheckWorkerNotExist(1, 6);
169 manager_->WorkerCreated(1, 6, instance1);
170 CheckWorkerState(1, 6, WorkerState::WORKER_UNINSPECTED);
171 agent_host = manager_->GetDevToolsAgentHostForWorker(1, 6);
172 EXPECT_TRUE(agent_host.get());
173 CheckWorkerState(1, 6, WorkerState::WORKER_UNINSPECTED);
174 agent_host = NULL;
175 manager_->WorkerDestroyed(1, 6);
176 CheckWorkerNotExist(1, 6);
179 TEST_F(SharedWorkerDevToolsManagerTest, AttachTest) {
180 scoped_refptr<DevToolsAgentHostImpl> agent_host1;
181 scoped_refptr<DevToolsAgentHostImpl> agent_host2;
183 SharedWorkerInstance instance1(GURL("http://example.com/w1.js"),
184 base::string16(),
185 base::string16(),
186 blink::WebContentSecurityPolicyTypeReport,
187 browser_context_->GetResourceContext(),
188 partition_id_);
189 SharedWorkerInstance instance2(GURL("http://example.com/w2.js"),
190 base::string16(),
191 base::string16(),
192 blink::WebContentSecurityPolicyTypeReport,
193 browser_context_->GetResourceContext(),
194 partition_id_);
196 // Created -> GetDevToolsAgentHost -> Register -> Started -> Destroyed
197 scoped_ptr<TestDevToolsClientHost> client_host1(new TestDevToolsClientHost());
198 CheckWorkerNotExist(2, 1);
199 manager_->WorkerCreated(2, 1, instance1);
200 CheckWorkerState(2, 1, WorkerState::WORKER_UNINSPECTED);
201 agent_host1 = manager_->GetDevToolsAgentHostForWorker(2, 1);
202 EXPECT_TRUE(agent_host1.get());
203 CheckWorkerState(2, 1, WorkerState::WORKER_UNINSPECTED);
204 EXPECT_EQ(agent_host1.get(), manager_->GetDevToolsAgentHostForWorker(2, 1));
205 client_host1->InspectAgentHost(agent_host1.get());
206 CheckWorkerState(2, 1, WorkerState::WORKER_INSPECTED);
207 manager_->WorkerReadyForInspection(2, 1);
208 CheckWorkerState(2, 1, WorkerState::WORKER_INSPECTED);
209 manager_->WorkerDestroyed(2, 1);
210 CheckWorkerState(2, 1, WorkerState::WORKER_TERMINATED);
211 EXPECT_EQ(agent_host1.get(), manager_->GetDevToolsAgentHostForWorker(2, 1));
213 // Created -> Started -> GetDevToolsAgentHost -> Register -> Destroyed
214 scoped_ptr<TestDevToolsClientHost> client_host2(new TestDevToolsClientHost());
215 manager_->WorkerCreated(2, 2, instance2);
216 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED);
217 manager_->WorkerReadyForInspection(2, 2);
218 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED);
219 agent_host2 = manager_->GetDevToolsAgentHostForWorker(2, 2);
220 EXPECT_TRUE(agent_host2.get());
221 EXPECT_NE(agent_host1.get(), agent_host2.get());
222 EXPECT_EQ(agent_host2.get(), manager_->GetDevToolsAgentHostForWorker(2, 2));
223 CheckWorkerState(2, 2, WorkerState::WORKER_UNINSPECTED);
224 client_host2->InspectAgentHost(agent_host2.get());
225 CheckWorkerState(2, 2, WorkerState::WORKER_INSPECTED);
226 manager_->WorkerDestroyed(2, 2);
227 CheckWorkerState(2, 2, WorkerState::WORKER_TERMINATED);
228 EXPECT_EQ(agent_host2.get(), manager_->GetDevToolsAgentHostForWorker(2, 2));
230 // Re-created -> Started -> ClientHostClosing -> Destroyed
231 CheckWorkerState(2, 1, WorkerState::WORKER_TERMINATED);
232 manager_->WorkerCreated(2, 3, instance1);
233 CheckWorkerNotExist(2, 1);
234 CheckWorkerState(2, 3, WorkerState::WORKER_PAUSED_FOR_REATTACH);
235 EXPECT_EQ(agent_host1.get(), manager_->GetDevToolsAgentHostForWorker(2, 3));
236 manager_->WorkerReadyForInspection(2, 3);
237 CheckWorkerState(2, 3, WorkerState::WORKER_INSPECTED);
238 client_host1->InspectAgentHost(NULL);
239 manager_->WorkerDestroyed(2, 3);
240 CheckWorkerState(2, 3, WorkerState::WORKER_TERMINATED);
241 agent_host1 = NULL;
242 CheckWorkerNotExist(2, 3);
244 // Re-created -> Destroyed
245 CheckWorkerState(2, 2, WorkerState::WORKER_TERMINATED);
246 manager_->WorkerCreated(2, 4, instance2);
247 CheckWorkerNotExist(2, 2);
248 CheckWorkerState(2, 4, WorkerState::WORKER_PAUSED_FOR_REATTACH);
249 EXPECT_EQ(agent_host2.get(), manager_->GetDevToolsAgentHostForWorker(2, 4));
250 manager_->WorkerDestroyed(2, 4);
251 CheckWorkerNotExist(2, 2);
252 CheckWorkerState(2, 4, WorkerState::WORKER_TERMINATED);
254 // Re-created -> ClientHostClosing -> Destroyed
255 manager_->WorkerCreated(2, 5, instance2);
256 CheckWorkerNotExist(2, 2);
257 CheckWorkerState(2, 5, WorkerState::WORKER_PAUSED_FOR_REATTACH);
258 EXPECT_EQ(agent_host2.get(), manager_->GetDevToolsAgentHostForWorker(2, 5));
259 client_host2->InspectAgentHost(NULL);
260 CheckWorkerCount(1);
261 agent_host2 = NULL;
262 CheckWorkerCount(1);
263 manager_->WorkerDestroyed(2, 5);
264 CheckWorkerCount(0);
267 TEST_F(SharedWorkerDevToolsManagerTest, ReattachTest) {
268 SharedWorkerInstance instance(GURL("http://example.com/w3.js"),
269 base::string16(),
270 base::string16(),
271 blink::WebContentSecurityPolicyTypeReport,
272 browser_context_->GetResourceContext(),
273 partition_id_);
274 scoped_ptr<TestDevToolsClientHost> client_host(new TestDevToolsClientHost());
275 // Created -> GetDevToolsAgentHost -> Register -> Destroyed
276 manager_->WorkerCreated(3, 1, instance);
277 CheckWorkerState(3, 1, WorkerState::WORKER_UNINSPECTED);
278 scoped_refptr<DevToolsAgentHost> agent_host(
279 manager_->GetDevToolsAgentHostForWorker(3, 1));
280 EXPECT_TRUE(agent_host.get());
281 CheckWorkerState(3, 1, WorkerState::WORKER_UNINSPECTED);
282 client_host->InspectAgentHost(agent_host.get());
283 CheckWorkerState(3, 1, WorkerState::WORKER_INSPECTED);
284 manager_->WorkerDestroyed(3, 1);
285 CheckWorkerState(3, 1, WorkerState::WORKER_TERMINATED);
286 // ClientHostClosing -> Re-created -> release agent_host -> Destroyed
287 client_host->InspectAgentHost(NULL);
288 CheckWorkerState(3, 1, WorkerState::WORKER_TERMINATED);
289 manager_->WorkerCreated(3, 2, instance);
290 CheckWorkerState(3, 2, WorkerState::WORKER_UNINSPECTED);
291 agent_host = NULL;
292 CheckWorkerState(3, 2, WorkerState::WORKER_UNINSPECTED);
293 manager_->WorkerDestroyed(3, 2);
294 CheckWorkerNotExist(3, 2);
295 CheckWorkerCount(0);
298 } // namespace content