1 // Copyright (c) 2012 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/memory/scoped_ptr.h"
7 #include "base/time/time.h"
8 #include "content/browser/devtools/devtools_manager.h"
9 #include "content/browser/devtools/shared_worker_devtools_manager.h"
10 #include "content/browser/shared_worker/shared_worker_instance.h"
11 #include "content/browser/shared_worker/worker_storage_partition.h"
12 #include "content/common/view_messages.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/devtools_external_agent_proxy.h"
17 #include "content/public/browser/devtools_external_agent_proxy_delegate.h"
18 #include "content/public/browser/devtools_target.h"
19 #include "content/public/browser/web_contents_delegate.h"
20 #include "content/public/test/test_utils.h"
21 #include "content/test/test_content_browser_client.h"
22 #include "content/test/test_render_view_host.h"
23 #include "content/test/test_web_contents.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using base::TimeDelta
;
31 class TestDevToolsClientHost
: public DevToolsAgentHostClient
{
33 TestDevToolsClientHost()
34 : last_sent_message(NULL
),
38 ~TestDevToolsClientHost() override
{ EXPECT_TRUE(closed_
); }
41 EXPECT_FALSE(closed_
);
43 agent_host_
->DetachClient();
47 void AgentHostClosed(DevToolsAgentHost
* agent_host
, bool replaced
) override
{
51 void DispatchProtocolMessage(DevToolsAgentHost
* agent_host
,
52 const std::string
& message
) override
{
53 last_sent_message
= &message
;
56 void InspectAgentHost(DevToolsAgentHost
* agent_host
) {
57 agent_host_
= agent_host
;
58 agent_host_
->AttachClient(this);
61 DevToolsAgentHost
* agent_host() { return agent_host_
.get(); }
63 static void ResetCounters() {
67 static int close_counter
;
69 const std::string
* last_sent_message
;
73 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
75 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost
);
78 int TestDevToolsClientHost::close_counter
= 0;
81 class TestWebContentsDelegate
: public WebContentsDelegate
{
83 TestWebContentsDelegate() : renderer_unresponsive_received_(false) {}
85 // Notification that the contents is hung.
86 void RendererUnresponsive(WebContents
* source
) override
{
87 renderer_unresponsive_received_
= true;
90 bool renderer_unresponsive_received() const {
91 return renderer_unresponsive_received_
;
95 bool renderer_unresponsive_received_
;
98 class TestTarget
: public DevToolsTarget
{
100 explicit TestTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
101 : agent_host_(agent_host
) {}
102 ~TestTarget() override
{}
104 std::string
GetId() const override
{ return agent_host_
->GetId(); }
105 std::string
GetParentId() const override
{ return std::string(); }
106 std::string
GetType() const override
{ return std::string(); }
107 std::string
GetTitle() const override
{ return agent_host_
->GetTitle(); }
108 std::string
GetDescription() const override
{ return std::string(); }
109 GURL
GetURL() const override
{ return agent_host_
->GetURL(); }
110 GURL
GetFaviconURL() const override
{ return GURL(); }
111 base::TimeTicks
GetLastActivityTime() const override
{
112 return base::TimeTicks();
114 bool IsAttached() const override
{ return agent_host_
->IsAttached(); }
115 scoped_refptr
<DevToolsAgentHost
> GetAgentHost() const override
{
118 bool Activate() const override
{ return agent_host_
->Activate(); }
119 bool Close() const override
{ return agent_host_
->Close(); }
122 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
125 class TestDevToolsManagerDelegate
: public DevToolsManagerDelegate
{
127 ~TestDevToolsManagerDelegate() override
{}
129 void Inspect(BrowserContext
* browser_context
,
130 DevToolsAgentHost
* agent_host
) override
{}
132 void DevToolsAgentStateChanged(DevToolsAgentHost
* agent_host
,
133 bool attached
) override
{}
135 base::DictionaryValue
* HandleCommand(
136 DevToolsAgentHost
* agent_host
,
137 base::DictionaryValue
* command
) override
{
141 scoped_ptr
<DevToolsTarget
> CreateNewTarget(const GURL
& url
) override
{
142 return scoped_ptr
<DevToolsTarget
>();
145 void EnumerateTargets(TargetCallback callback
) override
{
147 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
148 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
149 it
!= agents
.end(); ++it
) {
150 if ((*it
)->GetType() == DevToolsAgentHost::TYPE_WEB_CONTENTS
)
151 result
.insert(result
.begin(), new TestTarget(*it
));
153 result
.push_back(new TestTarget(*it
));
155 callback
.Run(result
);
158 std::string
GetPageThumbnailData(const GURL
& url
) override
{
159 return std::string();
163 class ContentBrowserClientWithDevTools
: public TestContentBrowserClient
{
165 ~ContentBrowserClientWithDevTools() override
{}
166 content::DevToolsManagerDelegate
* GetDevToolsManagerDelegate() override
{
167 return new TestDevToolsManagerDelegate();
173 class DevToolsManagerTest
: public RenderViewHostImplTestHarness
{
175 DevToolsManagerTest()
176 : old_browser_client_(NULL
) {}
179 void SetUp() override
{
180 RenderViewHostImplTestHarness::SetUp();
181 TestDevToolsClientHost::ResetCounters();
182 old_browser_client_
= SetBrowserClientForTesting(&browser_client_
);
185 void TearDown() override
{
186 SetBrowserClientForTesting(old_browser_client_
);
187 RenderViewHostImplTestHarness::TearDown();
190 ContentBrowserClientWithDevTools browser_client_
;
191 ContentBrowserClient
* old_browser_client_
;
194 TEST_F(DevToolsManagerTest
, OpenAndManuallyCloseDevToolsClientHost
) {
195 scoped_refptr
<DevToolsAgentHost
> agent(
196 DevToolsAgentHost::GetOrCreateFor(web_contents()));
197 EXPECT_FALSE(agent
->IsAttached());
199 TestDevToolsClientHost client_host
;
200 client_host
.InspectAgentHost(agent
.get());
201 // Test that the connection is established.
202 EXPECT_TRUE(agent
->IsAttached());
203 EXPECT_EQ(0, TestDevToolsClientHost::close_counter
);
206 EXPECT_EQ(1, TestDevToolsClientHost::close_counter
);
207 EXPECT_FALSE(agent
->IsAttached());
210 TEST_F(DevToolsManagerTest
, NoUnresponsiveDialogInInspectedContents
) {
211 const GURL
url("http://www.google.com");
212 contents()->NavigateAndCommit(url
);
213 TestRenderViewHost
* inspected_rvh
= test_rvh();
214 EXPECT_TRUE(inspected_rvh
->IsRenderViewLive());
215 EXPECT_FALSE(contents()->GetDelegate());
216 TestWebContentsDelegate delegate
;
217 contents()->SetDelegate(&delegate
);
219 TestDevToolsClientHost client_host
;
220 scoped_refptr
<DevToolsAgentHost
> agent_host(DevToolsAgentHost::GetOrCreateFor(
221 WebContents::FromRenderViewHost(inspected_rvh
)));
222 client_host
.InspectAgentHost(agent_host
.get());
224 // Start with a short timeout.
225 inspected_rvh
->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10));
226 // Wait long enough for first timeout and see if it fired.
227 base::MessageLoop::current()->PostDelayedTask(
229 base::MessageLoop::QuitClosure(),
230 TimeDelta::FromMilliseconds(10));
231 base::MessageLoop::current()->Run();
232 EXPECT_FALSE(delegate
.renderer_unresponsive_received());
234 // Now close devtools and check that the notification is delivered.
236 // Start with a short timeout.
237 inspected_rvh
->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10));
238 // Wait long enough for first timeout and see if it fired.
239 base::MessageLoop::current()->PostDelayedTask(
241 base::MessageLoop::QuitClosure(),
242 TimeDelta::FromMilliseconds(10));
243 base::MessageLoop::current()->Run();
244 EXPECT_TRUE(delegate
.renderer_unresponsive_received());
246 contents()->SetDelegate(NULL
);
249 TEST_F(DevToolsManagerTest
, ReattachOnCancelPendingNavigation
) {
250 // Navigate to URL. First URL should use first RenderViewHost.
251 const GURL
url("http://www.google.com");
252 controller().LoadURL(
253 url
, Referrer(), ui::PAGE_TRANSITION_TYPED
, std::string());
254 contents()->GetMainFrame()->PrepareForCommit();
255 contents()->TestDidNavigate(
256 contents()->GetMainFrame(), 1, url
, ui::PAGE_TRANSITION_TYPED
);
257 EXPECT_FALSE(contents()->CrossProcessNavigationPending());
259 TestDevToolsClientHost client_host
;
260 client_host
.InspectAgentHost(
261 DevToolsAgentHost::GetOrCreateFor(web_contents()).get());
263 // Navigate to new site which should get a new RenderViewHost.
264 const GURL
url2("http://www.yahoo.com");
265 controller().LoadURL(
266 url2
, Referrer(), ui::PAGE_TRANSITION_TYPED
, std::string());
267 contents()->GetMainFrame()->PrepareForCommit();
268 EXPECT_TRUE(contents()->CrossProcessNavigationPending());
269 EXPECT_EQ(client_host
.agent_host(),
270 DevToolsAgentHost::GetOrCreateFor(web_contents()).get());
272 // Interrupt pending navigation and navigate back to the original site.
273 controller().LoadURL(
274 url
, Referrer(), ui::PAGE_TRANSITION_TYPED
, std::string());
275 contents()->GetMainFrame()->PrepareForCommit();
276 contents()->TestDidNavigate(
277 contents()->GetMainFrame(), 1, url
, ui::PAGE_TRANSITION_TYPED
);
278 EXPECT_FALSE(contents()->CrossProcessNavigationPending());
279 EXPECT_EQ(client_host
.agent_host(),
280 DevToolsAgentHost::GetOrCreateFor(web_contents()).get());
284 class TestExternalAgentDelegate
: public DevToolsExternalAgentProxyDelegate
{
285 std::map
<std::string
,int> event_counter_
;
287 void recordEvent(const std::string
& name
) {
288 if (event_counter_
.find(name
) == event_counter_
.end())
289 event_counter_
[name
] = 0;
290 event_counter_
[name
] = event_counter_
[name
] + 1;
293 void expectEvent(int count
, const std::string
& name
) {
294 EXPECT_EQ(count
, event_counter_
[name
]);
297 void Attach(DevToolsExternalAgentProxy
* proxy
) override
{
298 recordEvent("Attach");
301 void Detach() override
{ recordEvent("Detach"); };
303 void SendMessageToBackend(const std::string
& message
) override
{
304 recordEvent(std::string("SendMessageToBackend.") + message
);
308 ~TestExternalAgentDelegate() override
{
309 expectEvent(1, "Attach");
310 expectEvent(1, "Detach");
311 expectEvent(0, "SendMessageToBackend.message0");
312 expectEvent(1, "SendMessageToBackend.message1");
313 expectEvent(2, "SendMessageToBackend.message2");
317 TEST_F(DevToolsManagerTest
, TestExternalProxy
) {
318 TestExternalAgentDelegate
* delegate
= new TestExternalAgentDelegate();
320 scoped_refptr
<DevToolsAgentHost
> agent_host
=
321 DevToolsAgentHost::Create(delegate
);
322 EXPECT_EQ(agent_host
, DevToolsAgentHost::GetForId(agent_host
->GetId()));
324 TestDevToolsClientHost client_host
;
325 client_host
.InspectAgentHost(agent_host
.get());
326 agent_host
->DispatchProtocolMessage("message1");
327 agent_host
->DispatchProtocolMessage("message2");
328 agent_host
->DispatchProtocolMessage("message2");
333 } // namespace content