| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/devtools/device/devtools_android_bridge.h" | 8 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
| 9 #include "chrome/browser/devtools/device/usb/android_usb_device.h" | 9 #include "chrome/browser/devtools/device/usb/android_usb_device.h" |
| 10 #include "chrome/browser/devtools/device/usb/usb_device_provider.h" | 10 #include "chrome/browser/devtools/device/usb/usb_device_provider.h" |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 private: | 406 private: |
| 407 UsbConfigDescriptor config_desc_; | 407 UsbConfigDescriptor config_desc_; |
| 408 }; | 408 }; |
| 409 | 409 |
| 410 class MockUsbService : public UsbService { | 410 class MockUsbService : public UsbService { |
| 411 public: | 411 public: |
| 412 MockUsbService() { | 412 MockUsbService() { |
| 413 devices_.push_back(new MockUsbDevice<AndroidTraits>()); | 413 devices_.push_back(new MockUsbDevice<AndroidTraits>()); |
| 414 } | 414 } |
| 415 | 415 |
| 416 virtual ~MockUsbService() {} | 416 ~MockUsbService() override {} |
| 417 | 417 |
| 418 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { | 418 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { |
| 419 NOTIMPLEMENTED(); | 419 NOTIMPLEMENTED(); |
| 420 return NULL; | 420 return NULL; |
| 421 } | 421 } |
| 422 | 422 |
| 423 virtual void GetDevices( | 423 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { |
| 424 std::vector<scoped_refptr<UsbDevice> >* devices) override { | |
| 425 STLClearObject(devices); | 424 STLClearObject(devices); |
| 426 std::copy(devices_.begin(), devices_.end(), back_inserter(*devices)); | 425 std::copy(devices_.begin(), devices_.end(), back_inserter(*devices)); |
| 427 } | 426 } |
| 428 | 427 |
| 429 std::vector<scoped_refptr<UsbDevice> > devices_; | 428 std::vector<scoped_refptr<UsbDevice> > devices_; |
| 430 }; | 429 }; |
| 431 | 430 |
| 432 class MockUsbServiceForCheckingTraits : public UsbService { | 431 class MockUsbServiceForCheckingTraits : public UsbService { |
| 433 public: | 432 public: |
| 434 MockUsbServiceForCheckingTraits() : step_(0) {} | 433 MockUsbServiceForCheckingTraits() : step_(0) {} |
| 435 | 434 |
| 436 virtual ~MockUsbServiceForCheckingTraits() {} | 435 ~MockUsbServiceForCheckingTraits() override {} |
| 437 | 436 |
| 438 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { | 437 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { |
| 439 NOTIMPLEMENTED(); | 438 NOTIMPLEMENTED(); |
| 440 return NULL; | 439 return NULL; |
| 441 } | 440 } |
| 442 | 441 |
| 443 virtual void GetDevices( | 442 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { |
| 444 std::vector<scoped_refptr<UsbDevice> >* devices) override { | |
| 445 STLClearObject(devices); | 443 STLClearObject(devices); |
| 446 // This switch should be kept in sync with | 444 // This switch should be kept in sync with |
| 447 // AndroidUsbBrowserTest::DeviceCountChanged. | 445 // AndroidUsbBrowserTest::DeviceCountChanged. |
| 448 switch (step_) { | 446 switch (step_) { |
| 449 case 0: | 447 case 0: |
| 450 // No devices. | 448 // No devices. |
| 451 break; | 449 break; |
| 452 case 1: | 450 case 1: |
| 453 // Android device. | 451 // Android device. |
| 454 devices->push_back(new MockUsbDevice<AndroidTraits>()); | 452 devices->push_back(new MockUsbDevice<AndroidTraits>()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 470 int step_; | 468 int step_; |
| 471 }; | 469 }; |
| 472 | 470 |
| 473 class DevToolsAndroidBridgeWarmUp | 471 class DevToolsAndroidBridgeWarmUp |
| 474 : public DevToolsAndroidBridge::DeviceCountListener { | 472 : public DevToolsAndroidBridge::DeviceCountListener { |
| 475 public: | 473 public: |
| 476 DevToolsAndroidBridgeWarmUp(base::Closure closure, | 474 DevToolsAndroidBridgeWarmUp(base::Closure closure, |
| 477 scoped_refptr<DevToolsAndroidBridge> adb_bridge) | 475 scoped_refptr<DevToolsAndroidBridge> adb_bridge) |
| 478 : closure_(closure), adb_bridge_(adb_bridge) {} | 476 : closure_(closure), adb_bridge_(adb_bridge) {} |
| 479 | 477 |
| 480 virtual void DeviceCountChanged(int count) override { | 478 void DeviceCountChanged(int count) override { |
| 481 adb_bridge_->RemoveDeviceCountListener(this); | 479 adb_bridge_->RemoveDeviceCountListener(this); |
| 482 closure_.Run(); | 480 closure_.Run(); |
| 483 } | 481 } |
| 484 | 482 |
| 485 base::Closure closure_; | 483 base::Closure closure_; |
| 486 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; | 484 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; |
| 487 }; | 485 }; |
| 488 | 486 |
| 489 class AndroidUsbDiscoveryTest : public InProcessBrowserTest { | 487 class AndroidUsbDiscoveryTest : public InProcessBrowserTest { |
| 490 protected: | 488 protected: |
| 491 AndroidUsbDiscoveryTest() | 489 AndroidUsbDiscoveryTest() |
| 492 : scheduler_invoked_(0) { | 490 : scheduler_invoked_(0) { |
| 493 } | 491 } |
| 494 virtual void SetUpOnMainThread() override { | 492 void SetUpOnMainThread() override { |
| 495 scoped_refptr<content::MessageLoopRunner> runner = | 493 scoped_refptr<content::MessageLoopRunner> runner = |
| 496 new content::MessageLoopRunner; | 494 new content::MessageLoopRunner; |
| 497 | 495 |
| 498 BrowserThread::PostTaskAndReply( | 496 BrowserThread::PostTaskAndReply( |
| 499 BrowserThread::FILE, | 497 BrowserThread::FILE, |
| 500 FROM_HERE, | 498 FROM_HERE, |
| 501 base::Bind(&AndroidUsbDiscoveryTest::SetUpService, this), | 499 base::Bind(&AndroidUsbDiscoveryTest::SetUpService, this), |
| 502 runner->QuitClosure()); | 500 runner->QuitClosure()); |
| 503 runner->Run(); | 501 runner->Run(); |
| 504 | 502 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 520 void ScheduleDeviceCountRequest(const base::Closure& request) { | 518 void ScheduleDeviceCountRequest(const base::Closure& request) { |
| 521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 522 scheduler_invoked_++; | 520 scheduler_invoked_++; |
| 523 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, request); | 521 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, request); |
| 524 } | 522 } |
| 525 | 523 |
| 526 virtual void SetUpService() { | 524 virtual void SetUpService() { |
| 527 UsbService::SetInstanceForTest(new MockUsbService()); | 525 UsbService::SetInstanceForTest(new MockUsbService()); |
| 528 } | 526 } |
| 529 | 527 |
| 530 virtual void TearDownOnMainThread() override { | 528 void TearDownOnMainThread() override { |
| 531 scoped_refptr<content::MessageLoopRunner> runner = | 529 scoped_refptr<content::MessageLoopRunner> runner = |
| 532 new content::MessageLoopRunner; | 530 new content::MessageLoopRunner; |
| 533 UsbService* service = NULL; | 531 UsbService* service = NULL; |
| 534 BrowserThread::PostTaskAndReply( | 532 BrowserThread::PostTaskAndReply( |
| 535 BrowserThread::FILE, | 533 BrowserThread::FILE, |
| 536 FROM_HERE, | 534 FROM_HERE, |
| 537 base::Bind(&UsbService::SetInstanceForTest, service), | 535 base::Bind(&UsbService::SetInstanceForTest, service), |
| 538 runner->QuitClosure()); | 536 runner->QuitClosure()); |
| 539 runner->Run(); | 537 runner->Run(); |
| 540 } | 538 } |
| 541 | 539 |
| 542 scoped_refptr<content::MessageLoopRunner> runner_; | 540 scoped_refptr<content::MessageLoopRunner> runner_; |
| 543 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; | 541 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; |
| 544 int scheduler_invoked_; | 542 int scheduler_invoked_; |
| 545 }; | 543 }; |
| 546 | 544 |
| 547 class AndroidUsbCountTest : public AndroidUsbDiscoveryTest { | 545 class AndroidUsbCountTest : public AndroidUsbDiscoveryTest { |
| 548 protected: | 546 protected: |
| 549 virtual void SetUpOnMainThread() override { | 547 void SetUpOnMainThread() override { |
| 550 AndroidUsbDiscoveryTest::SetUpOnMainThread(); | 548 AndroidUsbDiscoveryTest::SetUpOnMainThread(); |
| 551 DevToolsAndroidBridgeWarmUp warmup(runner_->QuitClosure(), adb_bridge_); | 549 DevToolsAndroidBridgeWarmUp warmup(runner_->QuitClosure(), adb_bridge_); |
| 552 adb_bridge_->AddDeviceCountListener(&warmup); | 550 adb_bridge_->AddDeviceCountListener(&warmup); |
| 553 runner_->Run(); | 551 runner_->Run(); |
| 554 runner_ = new content::MessageLoopRunner; | 552 runner_ = new content::MessageLoopRunner; |
| 555 } | 553 } |
| 556 }; | 554 }; |
| 557 | 555 |
| 558 class AndroidUsbTraitsTest : public AndroidUsbDiscoveryTest { | 556 class AndroidUsbTraitsTest : public AndroidUsbDiscoveryTest { |
| 559 protected: | 557 protected: |
| 560 virtual void SetUpService() override { | 558 void SetUpService() override { |
| 561 UsbService::SetInstanceForTest(new MockUsbServiceForCheckingTraits()); | 559 UsbService::SetInstanceForTest(new MockUsbServiceForCheckingTraits()); |
| 562 } | 560 } |
| 563 }; | 561 }; |
| 564 | 562 |
| 565 class MockListListener : public DevToolsAndroidBridge::DeviceListListener { | 563 class MockListListener : public DevToolsAndroidBridge::DeviceListListener { |
| 566 public: | 564 public: |
| 567 MockListListener(scoped_refptr<DevToolsAndroidBridge> adb_bridge, | 565 MockListListener(scoped_refptr<DevToolsAndroidBridge> adb_bridge, |
| 568 const base::Closure& callback) | 566 const base::Closure& callback) |
| 569 : adb_bridge_(adb_bridge), | 567 : adb_bridge_(adb_bridge), |
| 570 callback_(callback) { | 568 callback_(callback) { |
| 571 } | 569 } |
| 572 | 570 |
| 573 virtual void DeviceListChanged( | 571 void DeviceListChanged( |
| 574 const DevToolsAndroidBridge::RemoteDevices& devices) override { | 572 const DevToolsAndroidBridge::RemoteDevices& devices) override { |
| 575 if (devices.size() > 0) { | 573 if (devices.size() > 0) { |
| 576 if (devices[0]->is_connected()) { | 574 if (devices[0]->is_connected()) { |
| 577 ASSERT_EQ(kDeviceModel, devices[0]->model()); | 575 ASSERT_EQ(kDeviceModel, devices[0]->model()); |
| 578 ASSERT_EQ(kDeviceSerial, devices[0]->serial()); | 576 ASSERT_EQ(kDeviceSerial, devices[0]->serial()); |
| 579 adb_bridge_->RemoveDeviceListListener(this); | 577 adb_bridge_->RemoveDeviceListListener(this); |
| 580 callback_.Run(); | 578 callback_.Run(); |
| 581 } | 579 } |
| 582 } | 580 } |
| 583 } | 581 } |
| 584 | 582 |
| 585 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; | 583 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; |
| 586 base::Closure callback_; | 584 base::Closure callback_; |
| 587 }; | 585 }; |
| 588 | 586 |
| 589 class MockCountListener : public DevToolsAndroidBridge::DeviceCountListener { | 587 class MockCountListener : public DevToolsAndroidBridge::DeviceCountListener { |
| 590 public: | 588 public: |
| 591 explicit MockCountListener(scoped_refptr<DevToolsAndroidBridge> adb_bridge) | 589 explicit MockCountListener(scoped_refptr<DevToolsAndroidBridge> adb_bridge) |
| 592 : adb_bridge_(adb_bridge), | 590 : adb_bridge_(adb_bridge), |
| 593 reposts_left_(10), | 591 reposts_left_(10), |
| 594 invoked_(0) { | 592 invoked_(0) { |
| 595 } | 593 } |
| 596 | 594 |
| 597 virtual void DeviceCountChanged(int count) override { | 595 void DeviceCountChanged(int count) override { |
| 598 ++invoked_; | 596 ++invoked_; |
| 599 adb_bridge_->RemoveDeviceCountListener(this); | 597 adb_bridge_->RemoveDeviceCountListener(this); |
| 600 Shutdown(); | 598 Shutdown(); |
| 601 } | 599 } |
| 602 | 600 |
| 603 void Shutdown() { | 601 void Shutdown() { |
| 604 ShutdownOnUIThread(); | 602 ShutdownOnUIThread(); |
| 605 }; | 603 }; |
| 606 | 604 |
| 607 void ShutdownOnUIThread() { | 605 void ShutdownOnUIThread() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 631 }; | 629 }; |
| 632 | 630 |
| 633 class MockCountListenerWithReAdd : public MockCountListener { | 631 class MockCountListenerWithReAdd : public MockCountListener { |
| 634 public: | 632 public: |
| 635 explicit MockCountListenerWithReAdd( | 633 explicit MockCountListenerWithReAdd( |
| 636 scoped_refptr<DevToolsAndroidBridge> adb_bridge) | 634 scoped_refptr<DevToolsAndroidBridge> adb_bridge) |
| 637 : MockCountListener(adb_bridge), | 635 : MockCountListener(adb_bridge), |
| 638 readd_count_(2) { | 636 readd_count_(2) { |
| 639 } | 637 } |
| 640 | 638 |
| 641 virtual void DeviceCountChanged(int count) override { | 639 void DeviceCountChanged(int count) override { |
| 642 ++invoked_; | 640 ++invoked_; |
| 643 adb_bridge_->RemoveDeviceCountListener(this); | 641 adb_bridge_->RemoveDeviceCountListener(this); |
| 644 if (readd_count_ > 0) { | 642 if (readd_count_ > 0) { |
| 645 readd_count_--; | 643 readd_count_--; |
| 646 adb_bridge_->AddDeviceCountListener(this); | 644 adb_bridge_->AddDeviceCountListener(this); |
| 647 adb_bridge_->RemoveDeviceCountListener(this); | 645 adb_bridge_->RemoveDeviceCountListener(this); |
| 648 adb_bridge_->AddDeviceCountListener(this); | 646 adb_bridge_->AddDeviceCountListener(this); |
| 649 } else { | 647 } else { |
| 650 Shutdown(); | 648 Shutdown(); |
| 651 } | 649 } |
| 652 } | 650 } |
| 653 | 651 |
| 654 int readd_count_; | 652 int readd_count_; |
| 655 }; | 653 }; |
| 656 | 654 |
| 657 class MockCountListenerWithReAddWhileQueued : public MockCountListener { | 655 class MockCountListenerWithReAddWhileQueued : public MockCountListener { |
| 658 public: | 656 public: |
| 659 MockCountListenerWithReAddWhileQueued( | 657 MockCountListenerWithReAddWhileQueued( |
| 660 scoped_refptr<DevToolsAndroidBridge> adb_bridge) | 658 scoped_refptr<DevToolsAndroidBridge> adb_bridge) |
| 661 : MockCountListener(adb_bridge), | 659 : MockCountListener(adb_bridge), |
| 662 readded_(false) { | 660 readded_(false) { |
| 663 } | 661 } |
| 664 | 662 |
| 665 virtual void DeviceCountChanged(int count) override { | 663 void DeviceCountChanged(int count) override { |
| 666 ++invoked_; | 664 ++invoked_; |
| 667 if (!readded_) { | 665 if (!readded_) { |
| 668 readded_ = true; | 666 readded_ = true; |
| 669 base::MessageLoop::current()->PostTask( | 667 base::MessageLoop::current()->PostTask( |
| 670 FROM_HERE, | 668 FROM_HERE, |
| 671 base::Bind(&MockCountListenerWithReAddWhileQueued::ReAdd, | 669 base::Bind(&MockCountListenerWithReAddWhileQueued::ReAdd, |
| 672 base::Unretained(this))); | 670 base::Unretained(this))); |
| 673 } else { | 671 } else { |
| 674 adb_bridge_->RemoveDeviceCountListener(this); | 672 adb_bridge_->RemoveDeviceCountListener(this); |
| 675 Shutdown(); | 673 Shutdown(); |
| 676 } | 674 } |
| 677 } | 675 } |
| 678 | 676 |
| 679 void ReAdd() { | 677 void ReAdd() { |
| 680 adb_bridge_->RemoveDeviceCountListener(this); | 678 adb_bridge_->RemoveDeviceCountListener(this); |
| 681 adb_bridge_->AddDeviceCountListener(this); | 679 adb_bridge_->AddDeviceCountListener(this); |
| 682 } | 680 } |
| 683 | 681 |
| 684 bool readded_; | 682 bool readded_; |
| 685 }; | 683 }; |
| 686 | 684 |
| 687 class MockCountListenerForCheckingTraits : public MockCountListener { | 685 class MockCountListenerForCheckingTraits : public MockCountListener { |
| 688 public: | 686 public: |
| 689 MockCountListenerForCheckingTraits( | 687 MockCountListenerForCheckingTraits( |
| 690 scoped_refptr<DevToolsAndroidBridge> adb_bridge) | 688 scoped_refptr<DevToolsAndroidBridge> adb_bridge) |
| 691 : MockCountListener(adb_bridge), | 689 : MockCountListener(adb_bridge), |
| 692 step_(0) { | 690 step_(0) { |
| 693 } | 691 } |
| 694 virtual void DeviceCountChanged(int count) override { | 692 void DeviceCountChanged(int count) override { |
| 695 switch (step_) { | 693 switch (step_) { |
| 696 case 0: | 694 case 0: |
| 697 // Check for 0 devices when no devices present. | 695 // Check for 0 devices when no devices present. |
| 698 EXPECT_EQ(0, count); | 696 EXPECT_EQ(0, count); |
| 699 break; | 697 break; |
| 700 case 1: | 698 case 1: |
| 701 // Check for 1 device when only android device present. | 699 // Check for 1 device when only android device present. |
| 702 EXPECT_EQ(1, count); | 700 EXPECT_EQ(1, count); |
| 703 break; | 701 break; |
| 704 case 2: | 702 case 2: |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 runner_->Run(); | 762 runner_->Run(); |
| 765 EXPECT_EQ(2, listener.invoked_); | 763 EXPECT_EQ(2, listener.invoked_); |
| 766 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); | 764 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); |
| 767 } | 765 } |
| 768 | 766 |
| 769 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { | 767 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { |
| 770 MockCountListenerForCheckingTraits listener(adb_bridge_); | 768 MockCountListenerForCheckingTraits listener(adb_bridge_); |
| 771 adb_bridge_->AddDeviceCountListener(&listener); | 769 adb_bridge_->AddDeviceCountListener(&listener); |
| 772 runner_->Run(); | 770 runner_->Run(); |
| 773 } | 771 } |
| OLD | NEW |