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