| 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.h" | 8 #include "content/browser/devtools/devtools_manager.h" |
| 9 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | |
| 10 #include "content/browser/devtools/render_view_devtools_agent_host.h" | 9 #include "content/browser/devtools/render_view_devtools_agent_host.h" |
| 11 #include "content/browser/shared_worker/shared_worker_instance.h" | |
| 12 #include "content/browser/shared_worker/worker_storage_partition.h" | |
| 13 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
| 14 #include "content/public/browser/browser_context.h" | |
| 15 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 16 #include "content/public/browser/devtools_agent_host.h" | 12 #include "content/public/browser/devtools_agent_host.h" |
| 17 #include "content/public/browser/devtools_external_agent_proxy.h" | 13 #include "content/public/browser/devtools_external_agent_proxy.h" |
| 18 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 14 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
| 19 #include "content/public/browser/devtools_target.h" | |
| 20 #include "content/public/browser/web_contents_delegate.h" | 15 #include "content/public/browser/web_contents_delegate.h" |
| 21 #include "content/public/test/test_utils.h" | |
| 22 #include "content/test/test_content_browser_client.h" | 16 #include "content/test/test_content_browser_client.h" |
| 23 #include "content/test/test_render_view_host.h" | 17 #include "content/test/test_render_view_host.h" |
| 24 #include "content/test/test_web_contents.h" | 18 #include "content/test/test_web_contents.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 20 |
| 27 using base::TimeDelta; | 21 using base::TimeDelta; |
| 28 | 22 |
| 29 namespace content { | 23 namespace content { |
| 30 namespace { | 24 namespace { |
| 31 | 25 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 86 } |
| 93 | 87 |
| 94 bool renderer_unresponsive_received() const { | 88 bool renderer_unresponsive_received() const { |
| 95 return renderer_unresponsive_received_; | 89 return renderer_unresponsive_received_; |
| 96 } | 90 } |
| 97 | 91 |
| 98 private: | 92 private: |
| 99 bool renderer_unresponsive_received_; | 93 bool renderer_unresponsive_received_; |
| 100 }; | 94 }; |
| 101 | 95 |
| 102 class TestTarget : public DevToolsTarget { | |
| 103 public: | |
| 104 explicit TestTarget(scoped_refptr<DevToolsAgentHost> agent_host) | |
| 105 : agent_host_(agent_host) {} | |
| 106 virtual ~TestTarget() {} | |
| 107 | |
| 108 virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); } | |
| 109 virtual std::string GetParentId() const OVERRIDE { return std::string(); } | |
| 110 virtual std::string GetType() const OVERRIDE { return std::string(); } | |
| 111 virtual std::string GetTitle() const OVERRIDE { | |
| 112 return agent_host_->GetTitle(); | |
| 113 } | |
| 114 virtual std::string GetDescription() const OVERRIDE { return std::string(); } | |
| 115 virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); } | |
| 116 virtual GURL GetFaviconURL() const OVERRIDE { return GURL(); } | |
| 117 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { | |
| 118 return base::TimeTicks(); | |
| 119 } | |
| 120 virtual bool IsAttached() const OVERRIDE { return agent_host_->IsAttached(); } | |
| 121 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { | |
| 122 return agent_host_; | |
| 123 } | |
| 124 virtual bool Activate() const OVERRIDE { return agent_host_->Activate(); } | |
| 125 virtual bool Close() const OVERRIDE { return agent_host_->Close(); } | |
| 126 | |
| 127 private: | |
| 128 scoped_refptr<DevToolsAgentHost> agent_host_; | |
| 129 }; | |
| 130 | |
| 131 class TestDevToolsManagerDelegate : public DevToolsManagerDelegate { | |
| 132 public: | |
| 133 virtual ~TestDevToolsManagerDelegate() {} | |
| 134 | |
| 135 virtual void Inspect(BrowserContext* browser_context, | |
| 136 DevToolsAgentHost* agent_host) OVERRIDE {} | |
| 137 | |
| 138 virtual void DevToolsAgentStateChanged(DevToolsAgentHost* agent_host, | |
| 139 bool attached) OVERRIDE {} | |
| 140 | |
| 141 virtual base::DictionaryValue* HandleCommand( | |
| 142 DevToolsAgentHost* agent_host, | |
| 143 base::DictionaryValue* command) OVERRIDE { return NULL; } | |
| 144 | |
| 145 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { | |
| 146 return scoped_ptr<DevToolsTarget>(); | |
| 147 } | |
| 148 | |
| 149 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { | |
| 150 TargetList result; | |
| 151 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | |
| 152 for (DevToolsAgentHost::List::iterator it = agents.begin(); | |
| 153 it != agents.end(); ++it) { | |
| 154 if ((*it)->GetType() == DevToolsAgentHost::TYPE_WEB_CONTENTS) | |
| 155 result.insert(result.begin(), new TestTarget(*it)); | |
| 156 else | |
| 157 result.push_back(new TestTarget(*it)); | |
| 158 } | |
| 159 callback.Run(result); | |
| 160 } | |
| 161 | |
| 162 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { | |
| 163 return std::string(); | |
| 164 } | |
| 165 }; | |
| 166 | |
| 167 class ContentBrowserClientWithDevTools : public TestContentBrowserClient { | |
| 168 public: | |
| 169 explicit ContentBrowserClientWithDevTools(DevToolsManagerDelegate* delegate) | |
| 170 : delegate_(delegate) {} | |
| 171 virtual ~ContentBrowserClientWithDevTools() {} | |
| 172 | |
| 173 virtual content::DevToolsManagerDelegate* | |
| 174 GetDevToolsManagerDelegate() OVERRIDE { | |
| 175 return delegate_; | |
| 176 } | |
| 177 private: | |
| 178 DevToolsManagerDelegate* delegate_; | |
| 179 }; | |
| 180 | |
| 181 class TestDevToolsManagerObserver : public DevToolsManager::Observer { | |
| 182 public: | |
| 183 TestDevToolsManagerObserver() | |
| 184 : updates_count_(0) {} | |
| 185 virtual ~TestDevToolsManagerObserver() {} | |
| 186 | |
| 187 int updates_count() { return updates_count_; } | |
| 188 const TargetList& target_list() { return target_list_; } | |
| 189 | |
| 190 virtual void TargetListChanged(const TargetList& targets) OVERRIDE { | |
| 191 updates_count_++; | |
| 192 target_list_ = targets; | |
| 193 base::MessageLoop::current()->PostTask( | |
| 194 FROM_HERE, | |
| 195 base::MessageLoop::QuitClosure()); | |
| 196 } | |
| 197 | |
| 198 private: | |
| 199 int updates_count_; | |
| 200 TargetList target_list_; | |
| 201 }; | |
| 202 | |
| 203 } // namespace | 96 } // namespace |
| 204 | 97 |
| 205 class DevToolsManagerTest : public RenderViewHostImplTestHarness { | 98 class DevToolsManagerTest : public RenderViewHostImplTestHarness { |
| 206 public: | |
| 207 DevToolsManagerTest() | |
| 208 : delegate_(new TestDevToolsManagerDelegate()) {} | |
| 209 | |
| 210 protected: | 99 protected: |
| 211 virtual void SetUp() OVERRIDE { | 100 virtual void SetUp() OVERRIDE { |
| 212 RenderViewHostImplTestHarness::SetUp(); | 101 RenderViewHostImplTestHarness::SetUp(); |
| 213 TestDevToolsClientHost::ResetCounters(); | 102 TestDevToolsClientHost::ResetCounters(); |
| 214 SetBrowserClientForTesting(new ContentBrowserClientWithDevTools(delegate_)); | |
| 215 } | 103 } |
| 216 | |
| 217 TestDevToolsManagerDelegate* delegate_; | |
| 218 }; | 104 }; |
| 219 | 105 |
| 220 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { | 106 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { |
| 221 scoped_refptr<DevToolsAgentHost> agent( | 107 scoped_refptr<DevToolsAgentHost> agent( |
| 222 DevToolsAgentHost::GetOrCreateFor(web_contents())); | 108 DevToolsAgentHost::GetOrCreateFor(web_contents())); |
| 223 EXPECT_FALSE(agent->IsAttached()); | 109 EXPECT_FALSE(agent->IsAttached()); |
| 224 | 110 |
| 225 TestDevToolsClientHost client_host; | 111 TestDevToolsClientHost client_host; |
| 226 client_host.InspectAgentHost(agent.get()); | 112 client_host.InspectAgentHost(agent.get()); |
| 227 // Test that the connection is established. | 113 // Test that the connection is established. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 232 |
| 347 TestDevToolsClientHost client_host; | 233 TestDevToolsClientHost client_host; |
| 348 client_host.InspectAgentHost(agent_host.get()); | 234 client_host.InspectAgentHost(agent_host.get()); |
| 349 agent_host->DispatchProtocolMessage("message1"); | 235 agent_host->DispatchProtocolMessage("message1"); |
| 350 agent_host->DispatchProtocolMessage("message2"); | 236 agent_host->DispatchProtocolMessage("message2"); |
| 351 agent_host->DispatchProtocolMessage("message2"); | 237 agent_host->DispatchProtocolMessage("message2"); |
| 352 | 238 |
| 353 client_host.Close(); | 239 client_host.Close(); |
| 354 } | 240 } |
| 355 | 241 |
| 356 TEST_F(DevToolsManagerTest, TestObserver) { | |
| 357 GURL url1("data:text/html,<body>Body1</body>"); | |
| 358 GURL url2("data:text/html,<body>Body2</body>"); | |
| 359 GURL url3("data:text/html,<body>Body3</body>"); | |
| 360 | |
| 361 DevToolsManager* manager = DevToolsManager::GetInstance(); | |
| 362 DevToolsManager::SetObserverThrottleIntervalForTest( | |
| 363 base::TimeDelta::FromMilliseconds(200)); | |
| 364 | |
| 365 contents()->NavigateAndCommit(url1); | |
| 366 RunAllPendingInMessageLoop(); | |
| 367 | |
| 368 TestDevToolsManagerObserver* observer = new TestDevToolsManagerObserver(); | |
| 369 manager->AddObserver(observer); | |
| 370 RunMessageLoop(); | |
| 371 // Added observer should get an update. | |
| 372 EXPECT_EQ(1, observer->updates_count()); | |
| 373 EXPECT_EQ(1u, observer->target_list().size()); | |
| 374 EXPECT_EQ(contents(), | |
| 375 observer->target_list()[0]->GetAgentHost()->GetWebContents()); | |
| 376 EXPECT_EQ(url1.spec(), | |
| 377 observer->target_list()[0]->GetURL().spec()); | |
| 378 | |
| 379 contents()->NavigateAndCommit(url2); | |
| 380 RunAllPendingInMessageLoop(); | |
| 381 contents()->NavigateAndCommit(url3); | |
| 382 RunMessageLoop(); | |
| 383 // Updates should be coalesced. | |
| 384 EXPECT_EQ(2, observer->updates_count()); | |
| 385 EXPECT_EQ(1u, observer->target_list().size()); | |
| 386 EXPECT_EQ(contents(), | |
| 387 observer->target_list()[0]->GetAgentHost()->GetWebContents()); | |
| 388 EXPECT_EQ(url3.spec(), | |
| 389 observer->target_list()[0]->GetURL().spec()); | |
| 390 | |
| 391 base::MessageLoop::current()->PostDelayedTask( | |
| 392 FROM_HERE, | |
| 393 base::MessageLoop::QuitClosure(), | |
| 394 base::TimeDelta::FromMilliseconds(250)); | |
| 395 base::MessageLoop::current()->Run(); | |
| 396 // Check there were no extra updates. | |
| 397 EXPECT_EQ(2, observer->updates_count()); | |
| 398 | |
| 399 scoped_ptr<WorkerStoragePartition> partition(new WorkerStoragePartition( | |
| 400 browser_context()->GetRequestContext(), | |
| 401 NULL, NULL, NULL, NULL, NULL, NULL, NULL)); | |
| 402 WorkerStoragePartitionId partition_id(*partition.get()); | |
| 403 | |
| 404 GURL shared_worker_url("http://example.com/shared_worker.js"); | |
| 405 SharedWorkerInstance shared_worker( | |
| 406 shared_worker_url, | |
| 407 base::string16(), | |
| 408 base::string16(), | |
| 409 blink::WebContentSecurityPolicyTypeReport, | |
| 410 browser_context()->GetResourceContext(), | |
| 411 partition_id); | |
| 412 EmbeddedWorkerDevToolsManager::GetInstance()->SharedWorkerCreated( | |
| 413 1, 1, shared_worker); | |
| 414 contents()->NavigateAndCommit(url2); | |
| 415 | |
| 416 RunMessageLoop(); | |
| 417 EXPECT_EQ(3, observer->updates_count()); | |
| 418 EXPECT_EQ(2u, observer->target_list().size()); | |
| 419 EXPECT_EQ(contents(), | |
| 420 observer->target_list()[0]->GetAgentHost()->GetWebContents()); | |
| 421 EXPECT_EQ(url2.spec(), | |
| 422 observer->target_list()[0]->GetURL().spec()); | |
| 423 EXPECT_EQ(DevToolsAgentHost::TYPE_SHARED_WORKER, | |
| 424 observer->target_list()[1]->GetAgentHost()->GetType()); | |
| 425 EXPECT_EQ(shared_worker_url.spec(), | |
| 426 observer->target_list()[1]->GetURL().spec()); | |
| 427 | |
| 428 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerDestroyed(1, 1); | |
| 429 RunMessageLoop(); | |
| 430 EXPECT_EQ(4, observer->updates_count()); | |
| 431 EXPECT_EQ(1u, observer->target_list().size()); | |
| 432 EXPECT_EQ(contents(), | |
| 433 observer->target_list()[0]->GetAgentHost()->GetWebContents()); | |
| 434 EXPECT_EQ(url2.spec(), | |
| 435 observer->target_list()[0]->GetURL().spec()); | |
| 436 | |
| 437 base::MessageLoop::current()->PostDelayedTask( | |
| 438 FROM_HERE, | |
| 439 base::MessageLoop::QuitClosure(), | |
| 440 base::TimeDelta::FromMilliseconds(250)); | |
| 441 base::MessageLoop::current()->Run(); | |
| 442 // Check there were no extra updates. | |
| 443 EXPECT_EQ(4, observer->updates_count()); | |
| 444 | |
| 445 manager->RemoveObserver(observer); | |
| 446 } | |
| 447 | |
| 448 } // namespace content | 242 } // namespace content |
| OLD | NEW |