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

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

Issue 615533002: Fix DevToolsManagerTest.TestObserver flakiness. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « content/browser/devtools/devtools_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/embedded_worker_devtools_manager.h"
10 #include "content/browser/devtools/render_view_devtools_agent_host.h" 10 #include "content/browser/devtools/render_view_devtools_agent_host.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 return hosts_; 184 return hosts_;
185 } 185 }
186 186
187 virtual void TargetListChanged(const TargetList& targets) OVERRIDE { 187 virtual void TargetListChanged(const TargetList& targets) OVERRIDE {
188 updates_count_++; 188 updates_count_++;
189 hosts_.clear(); 189 hosts_.clear();
190 for (TargetList::const_iterator it = targets.begin(); 190 for (TargetList::const_iterator it = targets.begin();
191 it != targets.end(); ++it) { 191 it != targets.end(); ++it) {
192 hosts_.push_back((*it)->GetAgentHost()); 192 hosts_.push_back((*it)->GetAgentHost());
193 } 193 }
194 base::MessageLoop::current()->PostTask(
195 FROM_HERE,
196 base::MessageLoop::QuitClosure());
197 } 194 }
198 195
199 private: 196 private:
200 int updates_count_; 197 int updates_count_;
201 std::vector<scoped_refptr<DevToolsAgentHost>> hosts_; 198 std::vector<scoped_refptr<DevToolsAgentHost>> hosts_;
202 }; 199 };
203 200
204 } // namespace 201 } // namespace
205 202
206 class DevToolsManagerTest : public RenderViewHostImplTestHarness { 203 class DevToolsManagerTest : public RenderViewHostImplTestHarness {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 350
354 TestDevToolsClientHost client_host; 351 TestDevToolsClientHost client_host;
355 client_host.InspectAgentHost(agent_host.get()); 352 client_host.InspectAgentHost(agent_host.get());
356 agent_host->DispatchProtocolMessage("message1"); 353 agent_host->DispatchProtocolMessage("message1");
357 agent_host->DispatchProtocolMessage("message2"); 354 agent_host->DispatchProtocolMessage("message2");
358 agent_host->DispatchProtocolMessage("message2"); 355 agent_host->DispatchProtocolMessage("message2");
359 356
360 client_host.Close(); 357 client_host.Close();
361 } 358 }
362 359
360 class TestDevToolsManagerScheduler {
361 public:
362 DevToolsManager::Scheduler callback() {
363 return base::Bind(&TestDevToolsManagerScheduler::Schedule,
364 base::Unretained(this));
365 }
366
367 void Run() {
368 ASSERT_FALSE(closure_.is_null());
369 base::Closure copy = closure_;
370 closure_.Reset();
371 copy.Run();
372 }
373
374 bool IsEmpty() {
375 return closure_.is_null();
376 }
377
378 private:
379 void Schedule(base::Closure closure) {
380 EXPECT_TRUE(closure_.is_null());
381 closure_ = closure;
382 }
383
384 base::Closure closure_;
385 };
386
363 TEST_F(DevToolsManagerTest, TestObserver) { 387 TEST_F(DevToolsManagerTest, TestObserver) {
364 GURL url1("data:text/html,<body>Body1</body>"); 388 GURL url1("data:text/html,<body>Body1</body>");
365 GURL url2("data:text/html,<body>Body2</body>"); 389 GURL url2("data:text/html,<body>Body2</body>");
366 GURL url3("data:text/html,<body>Body3</body>"); 390 GURL url3("data:text/html,<body>Body3</body>");
367 391
392 TestDevToolsManagerScheduler scheduler;
368 DevToolsManager* manager = DevToolsManager::GetInstance(); 393 DevToolsManager* manager = DevToolsManager::GetInstance();
369 DevToolsManager::SetObserverThrottleIntervalForTest( 394 manager->SetSchedulerForTest(scheduler.callback());
370 base::TimeDelta::FromMilliseconds(200));
371 395
372 contents()->NavigateAndCommit(url1); 396 contents()->NavigateAndCommit(url1);
373 RunAllPendingInMessageLoop(); 397 RunAllPendingInMessageLoop();
374 398
375 scoped_ptr<TestDevToolsManagerObserver> observer( 399 scoped_ptr<TestDevToolsManagerObserver> observer(
376 new TestDevToolsManagerObserver()); 400 new TestDevToolsManagerObserver());
377 manager->AddObserver(observer.get()); 401 manager->AddObserver(observer.get());
378 RunMessageLoop();
379 // Added observer should get an update. 402 // Added observer should get an update.
380 EXPECT_EQ(1, observer->updates_count()); 403 EXPECT_EQ(1, observer->updates_count());
381 EXPECT_EQ(1u, observer->hosts().size()); 404 ASSERT_EQ(1u, observer->hosts().size());
382 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents()); 405 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
383 EXPECT_EQ(url1.spec(), observer->hosts()[0]->GetURL().spec()); 406 EXPECT_EQ(url1.spec(), observer->hosts()[0]->GetURL().spec());
384 407
385 contents()->NavigateAndCommit(url2); 408 contents()->NavigateAndCommit(url2);
386 RunAllPendingInMessageLoop(); 409 RunAllPendingInMessageLoop();
387 contents()->NavigateAndCommit(url3); 410 contents()->NavigateAndCommit(url3);
388 RunMessageLoop(); 411 scheduler.Run();
389 // Updates should be coalesced. 412 // Updates should be coalesced.
390 EXPECT_EQ(2, observer->updates_count()); 413 EXPECT_EQ(2, observer->updates_count());
391 EXPECT_EQ(1u, observer->hosts().size()); 414 ASSERT_EQ(1u, observer->hosts().size());
392 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents()); 415 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
393 EXPECT_EQ(url3.spec(), observer->hosts()[0]->GetURL().spec()); 416 EXPECT_EQ(url3.spec(), observer->hosts()[0]->GetURL().spec());
394 417
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. 418 // Check there were no extra updates.
419 scheduler.Run();
420 EXPECT_TRUE(scheduler.IsEmpty());
401 EXPECT_EQ(2, observer->updates_count()); 421 EXPECT_EQ(2, observer->updates_count());
402 422
403 scoped_ptr<WorkerStoragePartition> partition(new WorkerStoragePartition( 423 scoped_ptr<WorkerStoragePartition> partition(new WorkerStoragePartition(
404 browser_context()->GetRequestContext(), 424 browser_context()->GetRequestContext(),
405 NULL, NULL, NULL, NULL, NULL, NULL, NULL)); 425 NULL, NULL, NULL, NULL, NULL, NULL, NULL));
406 WorkerStoragePartitionId partition_id(*partition.get()); 426 WorkerStoragePartitionId partition_id(*partition.get());
407 427
408 GURL shared_worker_url("http://example.com/shared_worker.js"); 428 GURL shared_worker_url("http://example.com/shared_worker.js");
409 SharedWorkerInstance shared_worker( 429 SharedWorkerInstance shared_worker(
410 shared_worker_url, 430 shared_worker_url,
411 base::string16(), 431 base::string16(),
412 base::string16(), 432 base::string16(),
413 blink::WebContentSecurityPolicyTypeReport, 433 blink::WebContentSecurityPolicyTypeReport,
414 browser_context()->GetResourceContext(), 434 browser_context()->GetResourceContext(),
415 partition_id); 435 partition_id);
416 EmbeddedWorkerDevToolsManager::GetInstance()->SharedWorkerCreated( 436 EmbeddedWorkerDevToolsManager::GetInstance()->SharedWorkerCreated(
417 1, 1, shared_worker); 437 1, 1, shared_worker);
418 contents()->NavigateAndCommit(url2); 438 contents()->NavigateAndCommit(url2);
419 439
420 RunMessageLoop();
421 EXPECT_EQ(3, observer->updates_count()); 440 EXPECT_EQ(3, observer->updates_count());
422 EXPECT_EQ(2u, observer->hosts().size()); 441 ASSERT_EQ(2u, observer->hosts().size());
423 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents()); 442 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
424 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec()); 443 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec());
425 EXPECT_EQ(DevToolsAgentHost::TYPE_SHARED_WORKER, 444 EXPECT_EQ(DevToolsAgentHost::TYPE_SHARED_WORKER,
426 observer->hosts()[1]->GetType()); 445 observer->hosts()[1]->GetType());
427 EXPECT_EQ(shared_worker_url.spec(), observer->hosts()[1]->GetURL().spec()); 446 EXPECT_EQ(shared_worker_url.spec(), observer->hosts()[1]->GetURL().spec());
428 447
429 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerDestroyed(1, 1); 448 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerDestroyed(1, 1);
430 RunMessageLoop(); 449 scheduler.Run();
431 EXPECT_EQ(4, observer->updates_count()); 450 EXPECT_EQ(4, observer->updates_count());
432 EXPECT_EQ(1u, observer->hosts().size()); 451 ASSERT_EQ(1u, observer->hosts().size());
433 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents()); 452 EXPECT_EQ(contents(), observer->hosts()[0]->GetWebContents());
434 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec()); 453 EXPECT_EQ(url2.spec(), observer->hosts()[0]->GetURL().spec());
435 454
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. 455 // Check there were no extra updates.
456 scheduler.Run();
457 EXPECT_TRUE(scheduler.IsEmpty());
442 EXPECT_EQ(4, observer->updates_count()); 458 EXPECT_EQ(4, observer->updates_count());
443 459
444 manager->RemoveObserver(observer.get()); 460 manager->RemoveObserver(observer.get());
461
462 EXPECT_TRUE(scheduler.IsEmpty());
463 manager->SetSchedulerForTest(DevToolsManager::Scheduler());
445 } 464 }
446 465
447 } // namespace content 466 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/devtools_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698