Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: content/browser/devtools/devtools_manager_unittest.cc

Issue 577923002: [DevTools] Implement DevToolsManager::Observer which notifies about target updates. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-enumerate-to-dtm-delegate
Patch Set: fixed test flakiness Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
9 #include "content/browser/devtools/render_view_devtools_agent_host.h" 10 #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"
10 #include "content/common/view_messages.h" 13 #include "content/common/view_messages.h"
14 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/browser/devtools_agent_host.h" 16 #include "content/public/browser/devtools_agent_host.h"
13 #include "content/public/browser/devtools_external_agent_proxy.h" 17 #include "content/public/browser/devtools_external_agent_proxy.h"
14 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" 18 #include "content/public/browser/devtools_external_agent_proxy_delegate.h"
19 #include "content/public/browser/devtools_target.h"
15 #include "content/public/browser/web_contents_delegate.h" 20 #include "content/public/browser/web_contents_delegate.h"
21 #include "content/public/test/test_utils.h"
16 #include "content/test/test_content_browser_client.h" 22 #include "content/test/test_content_browser_client.h"
17 #include "content/test/test_render_view_host.h" 23 #include "content/test/test_render_view_host.h"
18 #include "content/test/test_web_contents.h" 24 #include "content/test/test_web_contents.h"
19 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
20 26
21 using base::TimeDelta; 27 using base::TimeDelta;
22 28
23 namespace content { 29 namespace content {
24 namespace { 30 namespace {
25 31
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 } 92 }
87 93
88 bool renderer_unresponsive_received() const { 94 bool renderer_unresponsive_received() const {
89 return renderer_unresponsive_received_; 95 return renderer_unresponsive_received_;
90 } 96 }
91 97
92 private: 98 private:
93 bool renderer_unresponsive_received_; 99 bool renderer_unresponsive_received_;
94 }; 100 };
95 101
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 virtual ~ContentBrowserClientWithDevTools() {}
170 virtual content::DevToolsManagerDelegate*
171 GetDevToolsManagerDelegate() OVERRIDE {
172 return new TestDevToolsManagerDelegate();
173 }
174 };
175
176 class TestDevToolsManagerObserver : public DevToolsManager::Observer {
177 public:
178 TestDevToolsManagerObserver()
179 : updates_count_(0) {}
180 virtual ~TestDevToolsManagerObserver() {}
181
182 int updates_count() { return updates_count_; }
183 const std::vector<scoped_refptr<DevToolsAgentHost>> hosts() {
184 return hosts_;
185 }
186
187 virtual void TargetListChanged(const TargetList& targets) OVERRIDE {
188 updates_count_++;
189 hosts_.clear();
190 for (TargetList::const_iterator it = targets.begin();
191 it != targets.end(); ++it) {
192 hosts_.push_back((*it)->GetAgentHost());
193 }
194 base::MessageLoop::current()->PostTask(
195 FROM_HERE,
196 base::MessageLoop::QuitClosure());
197 }
198
199 private:
200 int updates_count_;
201 std::vector<scoped_refptr<DevToolsAgentHost>> hosts_;
202 };
203
96 } // namespace 204 } // namespace
97 205
98 class DevToolsManagerTest : public RenderViewHostImplTestHarness { 206 class DevToolsManagerTest : public RenderViewHostImplTestHarness {
207 public:
208 DevToolsManagerTest()
209 : old_browser_client_(NULL) {}
210
99 protected: 211 protected:
100 virtual void SetUp() OVERRIDE { 212 virtual void SetUp() OVERRIDE {
101 RenderViewHostImplTestHarness::SetUp(); 213 RenderViewHostImplTestHarness::SetUp();
102 TestDevToolsClientHost::ResetCounters(); 214 TestDevToolsClientHost::ResetCounters();
215 old_browser_client_ = SetBrowserClientForTesting(&browser_client_);
103 } 216 }
217
218 virtual void TearDown() OVERRIDE {
219 SetBrowserClientForTesting(old_browser_client_);
220 RenderViewHostImplTestHarness::TearDown();
221 }
222
223 ContentBrowserClientWithDevTools browser_client_;
224 ContentBrowserClient* old_browser_client_;
104 }; 225 };
105 226
106 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { 227 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) {
107 scoped_refptr<DevToolsAgentHost> agent( 228 scoped_refptr<DevToolsAgentHost> agent(
108 DevToolsAgentHost::GetOrCreateFor(web_contents())); 229 DevToolsAgentHost::GetOrCreateFor(web_contents()));
109 EXPECT_FALSE(agent->IsAttached()); 230 EXPECT_FALSE(agent->IsAttached());
110 231
111 TestDevToolsClientHost client_host; 232 TestDevToolsClientHost client_host;
112 client_host.InspectAgentHost(agent.get()); 233 client_host.InspectAgentHost(agent.get());
113 // Test that the connection is established. 234 // Test that the connection is established.
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 353
233 TestDevToolsClientHost client_host; 354 TestDevToolsClientHost client_host;
234 client_host.InspectAgentHost(agent_host.get()); 355 client_host.InspectAgentHost(agent_host.get());
235 agent_host->DispatchProtocolMessage("message1"); 356 agent_host->DispatchProtocolMessage("message1");
236 agent_host->DispatchProtocolMessage("message2"); 357 agent_host->DispatchProtocolMessage("message2");
237 agent_host->DispatchProtocolMessage("message2"); 358 agent_host->DispatchProtocolMessage("message2");
238 359
239 client_host.Close(); 360 client_host.Close();
240 } 361 }
241 362
363 TEST_F(DevToolsManagerTest, TestObserver) {
364 GURL url1("data:text/html,<body>Body1</body>");
365 GURL url2("data:text/html,<body>Body2</body>");
366 GURL url3("data:text/html,<body>Body3</body>");
367
368 DevToolsManager* manager = DevToolsManager::GetInstance();
369 DevToolsManager::SetObserverThrottleIntervalForTest(
370 base::TimeDelta::FromMilliseconds(200));
371
372 contents()->NavigateAndCommit(url1);
373 RunAllPendingInMessageLoop();
374
375 scoped_ptr<TestDevToolsManagerObserver> observer(
376 new TestDevToolsManagerObserver());
377 manager->AddObserver(observer.get());
378 RunMessageLoop();
379 // Added observer should get an update.
380 EXPECT_EQ(1, observer->updates_count());
381 EXPECT_EQ(1u, observer->hosts().size());
382 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
383 EXPECT_EQ(url1.spec(), observer->hosts()[0]->GetURL().spec());
384
385 contents()->NavigateAndCommit(url2);
386 RunAllPendingInMessageLoop();
387 contents()->NavigateAndCommit(url3);
388 RunMessageLoop();
389 // Updates should be coalesced.
390 EXPECT_EQ(2, observer->updates_count());
391 EXPECT_EQ(1u, observer->hosts().size());
392 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
393 EXPECT_EQ(url3.spec(), observer->hosts()[0]->GetURL().spec());
394
395 base::MessageLoop::current()->PostDelayedTask(
396 FROM_HERE,
397 base::MessageLoop::QuitClosure(),
398 base::TimeDelta::FromMilliseconds(250));
399 base::MessageLoop::current()->Run();
400 // Check there were no extra updates.
401 EXPECT_EQ(2, observer->updates_count());
402
403 scoped_ptr<WorkerStoragePartition> partition(new WorkerStoragePartition(
404 browser_context()->GetRequestContext(),
405 NULL, NULL, NULL, NULL, NULL, NULL, NULL));
406 WorkerStoragePartitionId partition_id(*partition.get());
407
408 GURL shared_worker_url("http://example.com/shared_worker.js");
409 SharedWorkerInstance shared_worker(
410 shared_worker_url,
411 base::string16(),
412 base::string16(),
413 blink::WebContentSecurityPolicyTypeReport,
414 browser_context()->GetResourceContext(),
415 partition_id);
416 EmbeddedWorkerDevToolsManager::GetInstance()->SharedWorkerCreated(
417 1, 1, shared_worker);
418 contents()->NavigateAndCommit(url2);
419
420 RunMessageLoop();
421 EXPECT_EQ(3, observer->updates_count());
422 EXPECT_EQ(2u, observer->hosts().size());
423 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
424 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec());
425 EXPECT_EQ(DevToolsAgentHost::TYPE_SHARED_WORKER,
426 observer->hosts()[1]->GetType());
427 EXPECT_EQ(shared_worker_url.spec(), observer->hosts()[1]->GetURL().spec());
428
429 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerDestroyed(1, 1);
430 RunMessageLoop();
431 EXPECT_EQ(4, observer->updates_count());
432 EXPECT_EQ(1u, observer->hosts().size());
433 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
434 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec());
435
436 base::MessageLoop::current()->PostDelayedTask(
437 FROM_HERE,
438 base::MessageLoop::QuitClosure(),
439 base::TimeDelta::FromMilliseconds(250));
440 base::MessageLoop::current()->Run();
441 // Check there were no extra updates.
442 EXPECT_EQ(4, observer->updates_count());
443
444 manager->RemoveObserver(observer.get());
445 }
446
242 } // namespace content 447 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/devtools_manager.cc ('k') | content/browser/devtools/embedded_worker_devtools_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698