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