| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "content/browser/devtools/devtools_manager_impl.h" | 8 #include "content/browser/devtools/devtools_manager_impl.h" |
| 9 #include "content/browser/devtools/render_view_devtools_agent_host.h" | 9 #include "content/browser/devtools/render_view_devtools_agent_host.h" |
| 10 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
| 11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "content/public/browser/devtools_agent_host.h" | 12 #include "content/public/browser/devtools_agent_host.h" |
| 13 #include "content/public/browser/devtools_client_host.h" |
| 13 #include "content/public/browser/devtools_external_agent_proxy.h" | 14 #include "content/public/browser/devtools_external_agent_proxy.h" |
| 14 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 15 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
| 15 #include "content/public/browser/web_contents_delegate.h" | 16 #include "content/public/browser/web_contents_delegate.h" |
| 16 #include "content/test/test_content_browser_client.h" | 17 #include "content/test/test_content_browser_client.h" |
| 17 #include "content/test/test_render_view_host.h" | 18 #include "content/test/test_render_view_host.h" |
| 18 #include "content/test/test_web_contents.h" | 19 #include "content/test/test_web_contents.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 using base::TimeDelta; | 22 using base::TimeDelta; |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 class TestDevToolsClientHost : public DevToolsAgentHostClient { | 27 class TestDevToolsClientHost : public DevToolsClientHost { |
| 27 public: | 28 public: |
| 28 TestDevToolsClientHost() | 29 TestDevToolsClientHost() |
| 29 : last_sent_message(NULL), | 30 : last_sent_message(NULL), |
| 30 closed_(false) { | 31 closed_(false) { |
| 31 } | 32 } |
| 32 | 33 |
| 33 virtual ~TestDevToolsClientHost() { | 34 virtual ~TestDevToolsClientHost() { |
| 34 EXPECT_TRUE(closed_); | 35 EXPECT_TRUE(closed_); |
| 35 } | 36 } |
| 36 | 37 |
| 37 void Close() { | 38 virtual void Close(DevToolsManager* manager) { |
| 38 EXPECT_FALSE(closed_); | 39 EXPECT_FALSE(closed_); |
| 39 close_counter++; | 40 close_counter++; |
| 40 agent_host_->DetachClient(); | 41 manager->ClientHostClosing(this); |
| 41 closed_ = true; | 42 closed_ = true; |
| 42 } | 43 } |
| 43 | 44 virtual void InspectedContentsClosing() OVERRIDE { |
| 44 virtual void AgentHostClosed( | |
| 45 DevToolsAgentHost* agent_host, bool replaced) OVERRIDE { | |
| 46 FAIL(); | 45 FAIL(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 virtual void DispatchProtocolMessage( | 48 virtual void DispatchOnInspectorFrontend( |
| 50 DevToolsAgentHost* agent_host, const std::string& message) OVERRIDE { | 49 const std::string& message) OVERRIDE { |
| 51 last_sent_message = &message; | 50 last_sent_message = &message; |
| 52 } | 51 } |
| 53 | 52 |
| 54 void InspectAgentHost(DevToolsAgentHost* agent_host) { | 53 virtual void ReplacedWithAnotherClient() OVERRIDE { |
| 55 agent_host_ = agent_host; | |
| 56 agent_host_->AttachClient(this); | |
| 57 } | 54 } |
| 58 | 55 |
| 59 DevToolsAgentHost* agent_host() { return agent_host_.get(); } | |
| 60 | |
| 61 static void ResetCounters() { | 56 static void ResetCounters() { |
| 62 close_counter = 0; | 57 close_counter = 0; |
| 63 } | 58 } |
| 64 | 59 |
| 65 static int close_counter; | 60 static int close_counter; |
| 66 | 61 |
| 67 const std::string* last_sent_message; | 62 const std::string* last_sent_message; |
| 68 | 63 |
| 69 private: | 64 private: |
| 70 bool closed_; | 65 bool closed_; |
| 71 scoped_refptr<DevToolsAgentHost> agent_host_; | |
| 72 | 66 |
| 73 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost); | 67 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost); |
| 74 }; | 68 }; |
| 75 | 69 |
| 76 int TestDevToolsClientHost::close_counter = 0; | 70 int TestDevToolsClientHost::close_counter = 0; |
| 77 | 71 |
| 78 | 72 |
| 79 class TestWebContentsDelegate : public WebContentsDelegate { | 73 class TestWebContentsDelegate : public WebContentsDelegate { |
| 80 public: | 74 public: |
| 81 TestWebContentsDelegate() : renderer_unresponsive_received_(false) {} | 75 TestWebContentsDelegate() : renderer_unresponsive_received_(false) {} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 97 | 91 |
| 98 class DevToolsManagerTest : public RenderViewHostImplTestHarness { | 92 class DevToolsManagerTest : public RenderViewHostImplTestHarness { |
| 99 protected: | 93 protected: |
| 100 virtual void SetUp() OVERRIDE { | 94 virtual void SetUp() OVERRIDE { |
| 101 RenderViewHostImplTestHarness::SetUp(); | 95 RenderViewHostImplTestHarness::SetUp(); |
| 102 TestDevToolsClientHost::ResetCounters(); | 96 TestDevToolsClientHost::ResetCounters(); |
| 103 } | 97 } |
| 104 }; | 98 }; |
| 105 | 99 |
| 106 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { | 100 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { |
| 101 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 102 |
| 107 scoped_refptr<DevToolsAgentHost> agent( | 103 scoped_refptr<DevToolsAgentHost> agent( |
| 108 DevToolsAgentHost::GetOrCreateFor(web_contents())); | 104 DevToolsAgentHost::GetOrCreateFor(web_contents())); |
| 109 EXPECT_FALSE(agent->IsAttached()); | 105 EXPECT_FALSE(agent->IsAttached()); |
| 110 | 106 |
| 111 TestDevToolsClientHost client_host; | 107 TestDevToolsClientHost client_host; |
| 112 client_host.InspectAgentHost(agent.get()); | 108 manager->RegisterDevToolsClientHostFor(agent.get(), &client_host); |
| 113 // Test that the connection is established. | 109 // Test that the connection is established. |
| 114 EXPECT_TRUE(agent->IsAttached()); | 110 EXPECT_TRUE(agent->IsAttached()); |
| 111 EXPECT_EQ(agent, manager->GetDevToolsAgentHostFor(&client_host)); |
| 115 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); | 112 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); |
| 116 | 113 |
| 117 client_host.Close(); | 114 client_host.Close(manager); |
| 118 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); | 115 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); |
| 119 EXPECT_FALSE(agent->IsAttached()); | 116 EXPECT_FALSE(agent->IsAttached()); |
| 120 } | 117 } |
| 121 | 118 |
| 119 TEST_F(DevToolsManagerTest, ForwardMessageToClient) { |
| 120 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance(); |
| 121 |
| 122 TestDevToolsClientHost client_host; |
| 123 scoped_refptr<DevToolsAgentHost> agent_host( |
| 124 DevToolsAgentHost::GetOrCreateFor(web_contents())); |
| 125 manager->RegisterDevToolsClientHostFor(agent_host.get(), &client_host); |
| 126 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); |
| 127 |
| 128 std::string m = "test message"; |
| 129 agent_host = DevToolsAgentHost::GetOrCreateFor(web_contents()); |
| 130 manager->DispatchOnInspectorFrontend(agent_host.get(), m); |
| 131 EXPECT_TRUE(&m == client_host.last_sent_message); |
| 132 |
| 133 client_host.Close(manager); |
| 134 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); |
| 135 } |
| 136 |
| 122 TEST_F(DevToolsManagerTest, NoUnresponsiveDialogInInspectedContents) { | 137 TEST_F(DevToolsManagerTest, NoUnresponsiveDialogInInspectedContents) { |
| 123 TestRenderViewHost* inspected_rvh = test_rvh(); | 138 TestRenderViewHost* inspected_rvh = test_rvh(); |
| 124 inspected_rvh->set_render_view_created(true); | 139 inspected_rvh->set_render_view_created(true); |
| 125 EXPECT_FALSE(contents()->GetDelegate()); | 140 EXPECT_FALSE(contents()->GetDelegate()); |
| 126 TestWebContentsDelegate delegate; | 141 TestWebContentsDelegate delegate; |
| 127 contents()->SetDelegate(&delegate); | 142 contents()->SetDelegate(&delegate); |
| 128 | 143 |
| 129 TestDevToolsClientHost client_host; | 144 TestDevToolsClientHost client_host; |
| 130 scoped_refptr<DevToolsAgentHost> agent_host(DevToolsAgentHost::GetOrCreateFor( | 145 scoped_refptr<DevToolsAgentHost> agent_host(DevToolsAgentHost::GetOrCreateFor( |
| 131 WebContents::FromRenderViewHost(inspected_rvh))); | 146 WebContents::FromRenderViewHost(inspected_rvh))); |
| 132 client_host.InspectAgentHost(agent_host.get()); | 147 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( |
| 148 agent_host.get(), &client_host); |
| 133 | 149 |
| 134 // Start with a short timeout. | 150 // Start with a short timeout. |
| 135 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); | 151 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); |
| 136 // Wait long enough for first timeout and see if it fired. | 152 // Wait long enough for first timeout and see if it fired. |
| 137 base::MessageLoop::current()->PostDelayedTask( | 153 base::MessageLoop::current()->PostDelayedTask( |
| 138 FROM_HERE, | 154 FROM_HERE, |
| 139 base::MessageLoop::QuitClosure(), | 155 base::MessageLoop::QuitClosure(), |
| 140 TimeDelta::FromMilliseconds(10)); | 156 TimeDelta::FromMilliseconds(10)); |
| 141 base::MessageLoop::current()->Run(); | 157 base::MessageLoop::current()->Run(); |
| 142 EXPECT_FALSE(delegate.renderer_unresponsive_received()); | 158 EXPECT_FALSE(delegate.renderer_unresponsive_received()); |
| 143 | 159 |
| 144 // Now close devtools and check that the notification is delivered. | 160 // Now close devtools and check that the notification is delivered. |
| 145 client_host.Close(); | 161 client_host.Close(DevToolsManager::GetInstance()); |
| 146 // Start with a short timeout. | 162 // Start with a short timeout. |
| 147 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); | 163 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); |
| 148 // Wait long enough for first timeout and see if it fired. | 164 // Wait long enough for first timeout and see if it fired. |
| 149 base::MessageLoop::current()->PostDelayedTask( | 165 base::MessageLoop::current()->PostDelayedTask( |
| 150 FROM_HERE, | 166 FROM_HERE, |
| 151 base::MessageLoop::QuitClosure(), | 167 base::MessageLoop::QuitClosure(), |
| 152 TimeDelta::FromMilliseconds(10)); | 168 TimeDelta::FromMilliseconds(10)); |
| 153 base::MessageLoop::current()->Run(); | 169 base::MessageLoop::current()->Run(); |
| 154 EXPECT_TRUE(delegate.renderer_unresponsive_received()); | 170 EXPECT_TRUE(delegate.renderer_unresponsive_received()); |
| 155 | 171 |
| 156 contents()->SetDelegate(NULL); | 172 contents()->SetDelegate(NULL); |
| 157 } | 173 } |
| 158 | 174 |
| 159 TEST_F(DevToolsManagerTest, ReattachOnCancelPendingNavigation) { | 175 TEST_F(DevToolsManagerTest, ReattachOnCancelPendingNavigation) { |
| 160 // Navigate to URL. First URL should use first RenderViewHost. | 176 // Navigate to URL. First URL should use first RenderViewHost. |
| 161 const GURL url("http://www.google.com"); | 177 const GURL url("http://www.google.com"); |
| 162 controller().LoadURL( | 178 controller().LoadURL( |
| 163 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 179 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 164 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); | 180 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); |
| 165 EXPECT_FALSE(contents()->cross_navigation_pending()); | 181 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 166 | 182 |
| 167 TestDevToolsClientHost client_host; | 183 TestDevToolsClientHost client_host; |
| 168 client_host.InspectAgentHost( | 184 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); |
| 169 DevToolsAgentHost::GetOrCreateFor(web_contents()).get()); | 185 devtools_manager->RegisterDevToolsClientHostFor( |
| 186 DevToolsAgentHost::GetOrCreateFor(web_contents()).get(), &client_host); |
| 170 | 187 |
| 171 // Navigate to new site which should get a new RenderViewHost. | 188 // Navigate to new site which should get a new RenderViewHost. |
| 172 const GURL url2("http://www.yahoo.com"); | 189 const GURL url2("http://www.yahoo.com"); |
| 173 controller().LoadURL( | 190 controller().LoadURL( |
| 174 url2, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 191 url2, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 175 EXPECT_TRUE(contents()->cross_navigation_pending()); | 192 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 176 EXPECT_EQ(client_host.agent_host(), | 193 EXPECT_EQ(devtools_manager->GetDevToolsAgentHostFor(&client_host), |
| 177 DevToolsAgentHost::GetOrCreateFor(web_contents())); | 194 DevToolsAgentHost::GetOrCreateFor(web_contents())); |
| 178 | 195 |
| 179 // Interrupt pending navigation and navigate back to the original site. | 196 // Interrupt pending navigation and navigate back to the original site. |
| 180 controller().LoadURL( | 197 controller().LoadURL( |
| 181 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 198 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 182 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); | 199 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); |
| 183 EXPECT_FALSE(contents()->cross_navigation_pending()); | 200 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 184 EXPECT_EQ(client_host.agent_host(), | 201 EXPECT_EQ(devtools_manager->GetDevToolsAgentHostFor(&client_host), |
| 185 DevToolsAgentHost::GetOrCreateFor(web_contents())); | 202 DevToolsAgentHost::GetOrCreateFor(web_contents())); |
| 186 client_host.Close(); | 203 client_host.Close(DevToolsManager::GetInstance()); |
| 187 } | 204 } |
| 188 | 205 |
| 189 class TestExternalAgentDelegate: public DevToolsExternalAgentProxyDelegate { | 206 class TestExternalAgentDelegate: public DevToolsExternalAgentProxyDelegate { |
| 190 std::map<std::string,int> event_counter_; | 207 std::map<std::string,int> event_counter_; |
| 191 | 208 |
| 192 void recordEvent(const std::string& name) { | 209 void recordEvent(const std::string& name) { |
| 193 if (event_counter_.find(name) == event_counter_.end()) | 210 if (event_counter_.find(name) == event_counter_.end()) |
| 194 event_counter_[name] = 0; | 211 event_counter_[name] = 0; |
| 195 event_counter_[name] = event_counter_[name] + 1; | 212 event_counter_[name] = event_counter_[name] + 1; |
| 196 } | 213 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 221 } | 238 } |
| 222 }; | 239 }; |
| 223 | 240 |
| 224 TEST_F(DevToolsManagerTest, TestExternalProxy) { | 241 TEST_F(DevToolsManagerTest, TestExternalProxy) { |
| 225 TestExternalAgentDelegate* delegate = new TestExternalAgentDelegate(); | 242 TestExternalAgentDelegate* delegate = new TestExternalAgentDelegate(); |
| 226 | 243 |
| 227 scoped_refptr<DevToolsAgentHost> agent_host = | 244 scoped_refptr<DevToolsAgentHost> agent_host = |
| 228 DevToolsAgentHost::Create(delegate); | 245 DevToolsAgentHost::Create(delegate); |
| 229 EXPECT_EQ(agent_host, DevToolsAgentHost::GetForId(agent_host->GetId())); | 246 EXPECT_EQ(agent_host, DevToolsAgentHost::GetForId(agent_host->GetId())); |
| 230 | 247 |
| 248 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 249 |
| 231 TestDevToolsClientHost client_host; | 250 TestDevToolsClientHost client_host; |
| 232 client_host.InspectAgentHost(agent_host.get()); | 251 manager->RegisterDevToolsClientHostFor(agent_host.get(), &client_host); |
| 233 agent_host->DispatchProtocolMessage("message1"); | |
| 234 agent_host->DispatchProtocolMessage("message2"); | |
| 235 agent_host->DispatchProtocolMessage("message2"); | |
| 236 | 252 |
| 237 client_host.Close(); | 253 manager->DispatchOnInspectorBackend(&client_host, "message1"); |
| 254 manager->DispatchOnInspectorBackend(&client_host, "message2"); |
| 255 manager->DispatchOnInspectorBackend(&client_host, "message2"); |
| 256 |
| 257 client_host.Close(manager); |
| 238 } | 258 } |
| 239 | 259 |
| 240 } // namespace content | 260 } // namespace content |
| OLD | NEW |