| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/time.h" | |
| 7 #include "content/browser/debugger/devtools_manager_impl.h" | |
| 8 #include "content/browser/debugger/render_view_devtools_agent_host.h" | |
| 9 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 10 #include "content/browser/web_contents/test_web_contents.h" | |
| 11 #include "content/common/view_messages.h" | |
| 12 #include "content/public/browser/content_browser_client.h" | |
| 13 #include "content/public/browser/devtools_agent_host_registry.h" | |
| 14 #include "content/public/browser/devtools_client_host.h" | |
| 15 #include "content/public/browser/web_contents_delegate.h" | |
| 16 #include "content/test/test_content_browser_client.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using base::TimeDelta; | |
| 20 | |
| 21 namespace content { | |
| 22 namespace { | |
| 23 | |
| 24 class TestDevToolsClientHost : public DevToolsClientHost { | |
| 25 public: | |
| 26 TestDevToolsClientHost() | |
| 27 : last_sent_message(NULL), | |
| 28 closed_(false) { | |
| 29 } | |
| 30 | |
| 31 virtual ~TestDevToolsClientHost() { | |
| 32 EXPECT_TRUE(closed_); | |
| 33 } | |
| 34 | |
| 35 virtual void Close(DevToolsManager* manager) { | |
| 36 EXPECT_FALSE(closed_); | |
| 37 close_counter++; | |
| 38 manager->ClientHostClosing(this); | |
| 39 closed_ = true; | |
| 40 } | |
| 41 virtual void InspectedContentsClosing() { | |
| 42 FAIL(); | |
| 43 } | |
| 44 | |
| 45 virtual void DispatchOnInspectorFrontend(const std::string& message) { | |
| 46 last_sent_message = &message; | |
| 47 } | |
| 48 | |
| 49 virtual void ContentsReplaced(WebContents* new_contents) { | |
| 50 } | |
| 51 | |
| 52 virtual void ReplacedWithAnotherClient() { | |
| 53 } | |
| 54 | |
| 55 static void ResetCounters() { | |
| 56 close_counter = 0; | |
| 57 } | |
| 58 | |
| 59 static int close_counter; | |
| 60 | |
| 61 const std::string* last_sent_message; | |
| 62 | |
| 63 private: | |
| 64 bool closed_; | |
| 65 | |
| 66 virtual void FrameNavigating(const std::string& url) {} | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost); | |
| 69 }; | |
| 70 | |
| 71 int TestDevToolsClientHost::close_counter = 0; | |
| 72 | |
| 73 | |
| 74 class TestWebContentsDelegate : public WebContentsDelegate { | |
| 75 public: | |
| 76 TestWebContentsDelegate() : renderer_unresponsive_received_(false) {} | |
| 77 | |
| 78 // Notification that the contents is hung. | |
| 79 virtual void RendererUnresponsive(WebContents* source) { | |
| 80 renderer_unresponsive_received_ = true; | |
| 81 } | |
| 82 | |
| 83 bool renderer_unresponsive_received() const { | |
| 84 return renderer_unresponsive_received_; | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 bool renderer_unresponsive_received_; | |
| 89 }; | |
| 90 | |
| 91 class DevToolsManagerTestBrowserClient : public TestContentBrowserClient { | |
| 92 public: | |
| 93 DevToolsManagerTestBrowserClient() { | |
| 94 } | |
| 95 | |
| 96 virtual bool ShouldSwapProcessesForNavigation( | |
| 97 const GURL& current_url, | |
| 98 const GURL& new_url) OVERRIDE { | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(DevToolsManagerTestBrowserClient); | |
| 104 }; | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 class DevToolsManagerTest : public RenderViewHostImplTestHarness { | |
| 109 public: | |
| 110 DevToolsManagerTest() { | |
| 111 } | |
| 112 | |
| 113 protected: | |
| 114 virtual void SetUp() OVERRIDE { | |
| 115 original_browser_client_ = GetContentClient()->browser(); | |
| 116 GetContentClient()->set_browser_for_testing(&browser_client_); | |
| 117 | |
| 118 RenderViewHostImplTestHarness::SetUp(); | |
| 119 TestDevToolsClientHost::ResetCounters(); | |
| 120 } | |
| 121 | |
| 122 virtual void TearDown() OVERRIDE { | |
| 123 RenderViewHostImplTestHarness::TearDown(); | |
| 124 GetContentClient()->set_browser_for_testing(original_browser_client_); | |
| 125 } | |
| 126 | |
| 127 private: | |
| 128 ContentBrowserClient* original_browser_client_; | |
| 129 DevToolsManagerTestBrowserClient browser_client_; | |
| 130 }; | |
| 131 | |
| 132 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { | |
| 133 DevToolsManagerImpl manager; | |
| 134 | |
| 135 DevToolsAgentHost* agent = | |
| 136 DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh()); | |
| 137 DevToolsClientHost* host = manager.GetDevToolsClientHostFor(agent); | |
| 138 EXPECT_TRUE(NULL == host); | |
| 139 | |
| 140 TestDevToolsClientHost client_host; | |
| 141 manager.RegisterDevToolsClientHostFor(agent, &client_host); | |
| 142 // Test that just registered devtools host is returned. | |
| 143 host = manager.GetDevToolsClientHostFor(agent); | |
| 144 EXPECT_TRUE(&client_host == host); | |
| 145 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); | |
| 146 | |
| 147 // Test that the same devtools host is returned. | |
| 148 host = manager.GetDevToolsClientHostFor(agent); | |
| 149 EXPECT_TRUE(&client_host == host); | |
| 150 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); | |
| 151 | |
| 152 client_host.Close(&manager); | |
| 153 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); | |
| 154 host = manager.GetDevToolsClientHostFor(agent); | |
| 155 EXPECT_TRUE(NULL == host); | |
| 156 } | |
| 157 | |
| 158 TEST_F(DevToolsManagerTest, ForwardMessageToClient) { | |
| 159 DevToolsManagerImpl manager; | |
| 160 | |
| 161 TestDevToolsClientHost client_host; | |
| 162 DevToolsAgentHost* agent_host = | |
| 163 DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh()); | |
| 164 manager.RegisterDevToolsClientHostFor(agent_host, &client_host); | |
| 165 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); | |
| 166 | |
| 167 std::string m = "test message"; | |
| 168 agent_host = DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh()); | |
| 169 manager.DispatchOnInspectorFrontend(agent_host, m); | |
| 170 EXPECT_TRUE(&m == client_host.last_sent_message); | |
| 171 | |
| 172 client_host.Close(&manager); | |
| 173 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); | |
| 174 } | |
| 175 | |
| 176 TEST_F(DevToolsManagerTest, NoUnresponsiveDialogInInspectedContents) { | |
| 177 TestRenderViewHost* inspected_rvh = test_rvh(); | |
| 178 inspected_rvh->set_render_view_created(true); | |
| 179 EXPECT_FALSE(contents()->GetDelegate()); | |
| 180 TestWebContentsDelegate delegate; | |
| 181 contents()->SetDelegate(&delegate); | |
| 182 | |
| 183 TestDevToolsClientHost client_host; | |
| 184 DevToolsAgentHost* agent_host = | |
| 185 DevToolsAgentHostRegistry::GetDevToolsAgentHost(inspected_rvh); | |
| 186 DevToolsManager::GetInstance()-> | |
| 187 RegisterDevToolsClientHostFor(agent_host, &client_host); | |
| 188 | |
| 189 // Start with a short timeout. | |
| 190 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); | |
| 191 // Wait long enough for first timeout and see if it fired. | |
| 192 MessageLoop::current()->PostDelayedTask( | |
| 193 FROM_HERE, MessageLoop::QuitClosure(), TimeDelta::FromMilliseconds(10)); | |
| 194 MessageLoop::current()->Run(); | |
| 195 EXPECT_FALSE(delegate.renderer_unresponsive_received()); | |
| 196 | |
| 197 // Now close devtools and check that the notification is delivered. | |
| 198 client_host.Close(DevToolsManager::GetInstance()); | |
| 199 // Start with a short timeout. | |
| 200 inspected_rvh->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10)); | |
| 201 // Wait long enough for first timeout and see if it fired. | |
| 202 MessageLoop::current()->PostDelayedTask( | |
| 203 FROM_HERE, MessageLoop::QuitClosure(), TimeDelta::FromMilliseconds(10)); | |
| 204 MessageLoop::current()->Run(); | |
| 205 EXPECT_TRUE(delegate.renderer_unresponsive_received()); | |
| 206 | |
| 207 contents()->SetDelegate(NULL); | |
| 208 } | |
| 209 | |
| 210 TEST_F(DevToolsManagerTest, ReattachOnCancelPendingNavigation) { | |
| 211 contents()->transition_cross_site = true; | |
| 212 // Navigate to URL. First URL should use first RenderViewHost. | |
| 213 const GURL url("http://www.google.com"); | |
| 214 controller().LoadURL( | |
| 215 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | |
| 216 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); | |
| 217 EXPECT_FALSE(contents()->cross_navigation_pending()); | |
| 218 | |
| 219 TestDevToolsClientHost client_host; | |
| 220 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | |
| 221 devtools_manager->RegisterDevToolsClientHostFor( | |
| 222 DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh()), | |
| 223 &client_host); | |
| 224 | |
| 225 // Navigate to new site which should get a new RenderViewHost. | |
| 226 const GURL url2("http://www.yahoo.com"); | |
| 227 controller().LoadURL( | |
| 228 url2, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | |
| 229 EXPECT_TRUE(contents()->cross_navigation_pending()); | |
| 230 EXPECT_EQ(&client_host, devtools_manager->GetDevToolsClientHostFor( | |
| 231 DevToolsAgentHostRegistry::GetDevToolsAgentHost(pending_rvh()))); | |
| 232 | |
| 233 // Interrupt pending navigation and navigate back to the original site. | |
| 234 controller().LoadURL( | |
| 235 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | |
| 236 contents()->TestDidNavigate(rvh(), 1, url, PAGE_TRANSITION_TYPED); | |
| 237 EXPECT_FALSE(contents()->cross_navigation_pending()); | |
| 238 EXPECT_EQ(&client_host, devtools_manager->GetDevToolsClientHostFor( | |
| 239 DevToolsAgentHostRegistry::GetDevToolsAgentHost(rvh()))); | |
| 240 client_host.Close(DevToolsManager::GetInstance()); | |
| 241 } | |
| 242 | |
| 243 } // namespace content | |
| OLD | NEW |