diff options
author | reillyg <reillyg@chromium.org> | 2015-11-24 11:57:01 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-24 19:58:16 +0000 |
commit | bcc08e21cc608fb91eb7f6549a4f9752521c9bf5 (patch) | |
tree | 8f4fb6d51eb56eb60a469a1f60cf7db0a6109e67 | |
parent | 5ab80a80e0ba46888f173a1a019b1dcb64bb016a (diff) | |
download | chromium_src-bcc08e21cc608fb91eb7f6549a4f9752521c9bf5.zip chromium_src-bcc08e21cc608fb91eb7f6549a4f9752521c9bf5.tar.gz chromium_src-bcc08e21cc608fb91eb7f6549a4f9752521c9bf5.tar.bz2 |
Create shared MockDeviceClient and MockHidService classes.
Both of these classes are redefined in multiple tests (and more tests I
have out for review) so this change unifies the definitions.
BUG=None
Review URL: https://codereview.chromium.org/1462793003
Cr-Commit-Position: refs/heads/master@{#361419}
25 files changed, 400 insertions, 340 deletions
diff --git a/chrome/browser/chrome_webusb_browser_client_unittest.cc b/chrome/browser/chrome_webusb_browser_client_unittest.cc index 27cbe39..c2503d9 100644 --- a/chrome/browser/chrome_webusb_browser_client_unittest.cc +++ b/chrome/browser/chrome_webusb_browser_client_unittest.cc @@ -4,11 +4,13 @@ #include "chrome/browser/chrome_webusb_browser_client.h" +#include <string> + #include "base/macros.h" #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "components/webusb/webusb_detector.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/usb/mock_usb_device.h" #include "device/usb/mock_usb_service.h" #include "device/usb/usb_device.h" @@ -31,19 +33,6 @@ const char* kLandingPage_1 = "https://www.google.com/A"; const char* kLandingPage_2 = "https://www.google.com/B"; const char* kLandingPage_3 = "https://www.google.com/C"; -class TestDeviceClient : public device::DeviceClient { - public: - TestDeviceClient() : device::DeviceClient() {} - ~TestDeviceClient() override {} - - device::MockUsbService& mock_usb_service() { return usb_service_; } - - private: - device::UsbService* GetUsbService() override { return &usb_service_; } - - device::MockUsbService usb_service_; -}; - } // namespace class ChromeWebUsbBrowserClientTest : public testing::Test { @@ -57,9 +46,10 @@ class ChromeWebUsbBrowserClientTest : public testing::Test { void TearDown() override { message_center::MessageCenter::Shutdown(); } protected: - TestDeviceClient device_client_; + device::MockDeviceClient device_client_; ChromeWebUsbBrowserClient chrome_webusb_browser_client_; + private: DISALLOW_COPY_AND_ASSIGN(ChromeWebUsbBrowserClientTest); }; @@ -71,7 +61,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); message_center::MessageCenter* message_center = message_center::MessageCenter::Get(); @@ -91,7 +81,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceAddedAndRemoved) { EXPECT_TRUE(notification->delegate() != nullptr); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); // device is removed, so notification should be removed from the // message_center too @@ -108,7 +98,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); message_center::MessageCenter* message_center = message_center::MessageCenter::Get(); @@ -117,7 +107,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, // for device without product name, no notification is generated EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); } @@ -131,7 +121,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); message_center::MessageCenter* message_center = message_center::MessageCenter::Get(); @@ -140,7 +130,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, // for device without landing page, no notification is generated EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); } @@ -159,7 +149,7 @@ TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceWasThereBeforeAndThenRemoved) { EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); } @@ -185,9 +175,9 @@ TEST_F(ChromeWebUsbBrowserClientTest, ThreeUsbDevicesAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().AddDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->AddDevice(device_3); message_center::MessageCenter* message_center = message_center::MessageCenter::Get(); @@ -227,9 +217,9 @@ TEST_F(ChromeWebUsbBrowserClientTest, ThreeUsbDevicesAddedAndRemoved) { EXPECT_TRUE(notification_2->delegate() != nullptr); EXPECT_TRUE(notification_3->delegate() != nullptr); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().RemoveDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->RemoveDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_3); // devices are removed, so notifications should be removed from the // message_center too diff --git a/chrome/browser/extensions/api/device_permissions_manager_unittest.cc b/chrome/browser/extensions/api/device_permissions_manager_unittest.cc index 9863443..468aa5c 100644 --- a/chrome/browser/extensions/api/device_permissions_manager_unittest.cc +++ b/chrome/browser/extensions/api/device_permissions_manager_unittest.cc @@ -7,8 +7,9 @@ #include "base/test/values_test_util.h" #include "chrome/browser/extensions/test_extension_environment.h" #include "chrome/test/base/testing_profile.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/hid/hid_device_info.h" +#include "device/hid/mock_hid_service.h" #include "device/usb/mock_usb_device.h" #include "device/usb/mock_usb_service.h" #include "extensions/browser/api/device_permissions_manager.h" @@ -21,14 +22,8 @@ namespace extensions { namespace { -using device::HidDeviceId; using device::HidDeviceInfo; -using device::HidService; using device::MockUsbDevice; -using device::MockUsbService; -using device::UsbDevice; -using device::UsbDeviceHandle; -using device::UsbService; using testing::_; using testing::DoAll; using testing::Return; @@ -40,51 +35,6 @@ const uint64_t kTestDeviceIds[] = {1, 2, 3, 4}; const char* kTestDeviceIds[] = {"A", "B", "C", "D"}; #endif -class MockDeviceClient : device::DeviceClient { - public: - MockDeviceClient() {} - - // device::DeviceClient implementation: - UsbService* GetUsbService() override { - DCHECK(usb_service_); - return usb_service_; - } - - HidService* GetHidService() override { - DCHECK(hid_service_); - return hid_service_; - } - - void set_usb_service(UsbService* service) { usb_service_ = service; } - void set_hid_service(HidService* service) { hid_service_ = service; } - - private: - UsbService* usb_service_ = nullptr; - HidService* hid_service_ = nullptr; -}; - -class MockHidService : public HidService { - public: - MockHidService() {} - ~MockHidService() override {} - - // Public wrappers around protected functions needed for tests. - void AddDevice(scoped_refptr<HidDeviceInfo> info) { - HidService::AddDevice(info); - } - - void RemoveDevice(const HidDeviceId& device_id) { - HidService::RemoveDevice(device_id); - } - - void FirstEnumerationComplete() { HidService::FirstEnumerationComplete(); } - - private: - MOCK_METHOD2(Connect, - void(const HidDeviceId& device_id, - const ConnectCallback& callback)); -}; - } // namespace class DevicePermissionsManagerTest : public testing::Test { @@ -111,29 +61,25 @@ class DevicePermissionsManagerTest : public testing::Test { device4_ = new HidDeviceInfo(kTestDeviceIds[0], 0, 0, "Test HID Device", "abcde", device::kHIDBusTypeUSB, std::vector<uint8>()); - hid_service_.AddDevice(device4_); + device_client_.hid_service()->AddDevice(device4_); device5_ = new HidDeviceInfo(kTestDeviceIds[1], 0, 0, "Test HID Device", "", device::kHIDBusTypeUSB, std::vector<uint8>()); - hid_service_.AddDevice(device5_); + device_client_.hid_service()->AddDevice(device5_); device6_ = new HidDeviceInfo(kTestDeviceIds[2], 0, 0, "Test HID Device", "67890", device::kHIDBusTypeUSB, std::vector<uint8>()); - hid_service_.AddDevice(device6_); + device_client_.hid_service()->AddDevice(device6_); device7_ = new HidDeviceInfo(kTestDeviceIds[3], 0, 0, "Test HID Device", "", device::kHIDBusTypeUSB, std::vector<uint8>()); - hid_service_.AddDevice(device7_); - hid_service_.FirstEnumerationComplete(); - mock_device_client_.set_usb_service(&usb_service_); - mock_device_client_.set_hid_service(&hid_service_); + device_client_.hid_service()->AddDevice(device7_); + device_client_.hid_service()->FirstEnumerationComplete(); } void TearDown() override { env_.reset(nullptr); } scoped_ptr<extensions::TestExtensionEnvironment> env_; const extensions::Extension* extension_; - MockDeviceClient mock_device_client_; - MockUsbService usb_service_; - MockHidService hid_service_; + device::MockDeviceClient device_client_; scoped_refptr<MockUsbDevice> device0_; scoped_refptr<MockUsbDevice> device1_; scoped_refptr<MockUsbDevice> device2_; @@ -231,10 +177,10 @@ TEST_F(DevicePermissionsManagerTest, DisconnectDevice) { EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device6_).get()); EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device7_).get()); - usb_service_.RemoveDevice(device0_); - usb_service_.RemoveDevice(device1_); - hid_service_.RemoveDevice(device4_->device_id()); - hid_service_.RemoveDevice(device5_->device_id()); + device_client_.usb_service()->RemoveDevice(device0_); + device_client_.usb_service()->RemoveDevice(device1_); + device_client_.hid_service()->RemoveDevice(device4_->device_id()); + device_client_.hid_service()->RemoveDevice(device5_->device_id()); // Device 0 will be accessible when it is reconnected because it can be // recognized by its serial number. diff --git a/chrome/browser/usb/usb_chooser_context_unittest.cc b/chrome/browser/usb/usb_chooser_context_unittest.cc index cab3408..57f7d6f 100644 --- a/chrome/browser/usb/usb_chooser_context_unittest.cc +++ b/chrome/browser/usb/usb_chooser_context_unittest.cc @@ -6,29 +6,11 @@ #include "chrome/browser/usb/usb_chooser_context_factory.h" #include "chrome/test/base/testing_profile.h" #include "content/public/test/test_browser_thread_bundle.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/usb/mock_usb_device.h" #include "device/usb/mock_usb_service.h" using device::MockUsbDevice; -using device::UsbDevice; - -namespace { - -class TestDeviceClient : public device::DeviceClient { - public: - TestDeviceClient() {} - ~TestDeviceClient() override {} - - device::MockUsbService& usb_service() { return usb_service_; } - - private: - device::UsbService* GetUsbService() override { return &usb_service_; } - - device::MockUsbService usb_service_; -}; - -} // namespace class UsbChooserContextTest : public testing::Test { public: @@ -37,19 +19,19 @@ class UsbChooserContextTest : public testing::Test { protected: Profile* profile() { return &profile_; } - device::MockUsbService& usb_service() { return device_client_.usb_service(); } + + device::MockDeviceClient device_client_; private: content::TestBrowserThreadBundle thread_bundle_; - TestDeviceClient device_client_; TestingProfile profile_; }; TEST_F(UsbChooserContextTest, CheckGrantAndRevokePermission) { GURL origin("https://www.google.com"); - scoped_refptr<UsbDevice> device = + scoped_refptr<MockUsbDevice> device = new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); - usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile()); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); @@ -61,11 +43,11 @@ TEST_F(UsbChooserContextTest, CheckGrantAndRevokePermission) { TEST_F(UsbChooserContextTest, CheckGrantAndRevokeEphemeralPermission) { GURL origin("https://www.google.com"); - scoped_refptr<UsbDevice> device = + scoped_refptr<MockUsbDevice> device = new MockUsbDevice(0, 0, "Google", "Gizmo", ""); - scoped_refptr<UsbDevice> other_device = + scoped_refptr<MockUsbDevice> other_device = new MockUsbDevice(0, 0, "Google", "Gizmo", ""); - usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile()); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); @@ -79,40 +61,40 @@ TEST_F(UsbChooserContextTest, CheckGrantAndRevokeEphemeralPermission) { TEST_F(UsbChooserContextTest, DisconnectDeviceWithPermission) { GURL origin("https://www.google.com"); - scoped_refptr<UsbDevice> device = + scoped_refptr<MockUsbDevice> device = new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); - usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile()); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); store->GrantDevicePermission(origin, origin, device->guid()); EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); - usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); - scoped_refptr<UsbDevice> reconnected_device = + scoped_refptr<MockUsbDevice> reconnected_device = new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); - usb_service().AddDevice(reconnected_device); + device_client_.usb_service()->AddDevice(reconnected_device); EXPECT_TRUE( store->HasDevicePermission(origin, origin, reconnected_device->guid())); } TEST_F(UsbChooserContextTest, DisconnectDeviceWithEphemeralPermission) { GURL origin("https://www.google.com"); - scoped_refptr<UsbDevice> device = + scoped_refptr<MockUsbDevice> device = new MockUsbDevice(0, 0, "Google", "Gizmo", ""); - usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile()); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); store->GrantDevicePermission(origin, origin, device->guid()); EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); - usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); - scoped_refptr<UsbDevice> reconnected_device = + scoped_refptr<MockUsbDevice> reconnected_device = new MockUsbDevice(0, 0, "Google", "Gizmo", ""); - usb_service().AddDevice(reconnected_device); + device_client_.usb_service()->AddDevice(reconnected_device); EXPECT_FALSE( store->HasDevicePermission(origin, origin, reconnected_device->guid())); } diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index fb2bf38..f4fc12c 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -2337,6 +2337,8 @@ 'sources': [ '<@(chrome_unit_tests_extensions_sources)' ], 'dependencies': [ 'common/extensions/api/api.gyp:chrome_api', + '../device/core/core.gyp:device_core_mocks', + '../device/hid/hid.gyp:device_hid_mocks', '../device/usb/usb.gyp:device_usb_mocks', '../components/components.gyp:audio_modem_test_support', '../extensions/extensions_resources.gyp:extensions_resources', diff --git a/chrome/test/BUILD.gn b/chrome/test/BUILD.gn index 38f4bfb..4b40d69 100644 --- a/chrome/test/BUILD.gn +++ b/chrome/test/BUILD.gn @@ -1592,6 +1592,7 @@ test("unit_tests") { "//components/webdata_services:test_support", "//content/app/resources", "//device/bluetooth:mocks", + "//device/core:mocks", "//gpu:test_support", "//media:test_support", diff --git a/components/components_tests.gyp b/components/components_tests.gyp index 2a5c13b..5fcd7b3 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -1345,6 +1345,8 @@ 'dependencies': [ '../device/bluetooth/bluetooth.gyp:device_bluetooth_mocks', '../device/core/core.gyp:device_core', + '../device/core/core.gyp:device_core_mocks', + '../device/hid/hid.gyp:device_hid_mocks', '../device/usb/usb.gyp:device_usb', '../device/usb/usb.gyp:device_usb_mocks', '../google_apis/google_apis.gyp:google_apis_test_support', diff --git a/components/webusb/BUILD.gn b/components/webusb/BUILD.gn index 0a7d1fe..f62a2ea 100644 --- a/components/webusb/BUILD.gn +++ b/components/webusb/BUILD.gn @@ -27,6 +27,7 @@ source_set("unit_tests") { ":webusb", "//base", "//device/core", + "//device/core:mocks", "//device/usb", "//device/usb:mocks", "//testing/gmock", diff --git a/components/webusb/webusb_detector_unittest.cc b/components/webusb/webusb_detector_unittest.cc index 6b6e4d3..eebede8 100644 --- a/components/webusb/webusb_detector_unittest.cc +++ b/components/webusb/webusb_detector_unittest.cc @@ -4,15 +4,15 @@ #include "components/webusb/webusb_detector.h" +#include <string> + #include "base/macros.h" #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "components/webusb/webusb_browser_client.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/usb/mock_usb_device.h" #include "device/usb/mock_usb_service.h" -#include "device/usb/usb_device.h" -#include "device/usb/usb_service.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" @@ -35,19 +35,6 @@ namespace webusb { namespace { -class TestDeviceClient : public device::DeviceClient { - public: - TestDeviceClient() : device::DeviceClient() {} - ~TestDeviceClient() override {} - - device::MockUsbService& mock_usb_service() { return usb_service_; } - - private: - device::UsbService* GetUsbService() override { return &usb_service_; } - - device::MockUsbService usb_service_; -}; - class MockWebUsbBrowserClient : public webusb::WebUsbBrowserClient { public: MockWebUsbBrowserClient() {} @@ -76,9 +63,10 @@ class WebUsbDetectorTest : public testing::Test { ~WebUsbDetectorTest() override = default; protected: - TestDeviceClient device_client_; + device::MockDeviceClient device_client_; MockWebUsbBrowserClient mock_webusb_browser_client_; + private: DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); }; @@ -97,7 +85,7 @@ TEST_F(WebUsbDetectorTest, UsbDeviceAdded) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); } TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { @@ -117,8 +105,8 @@ TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->AddDevice(device); + device_client_.usb_service()->RemoveDevice(device); } TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { @@ -135,8 +123,8 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->AddDevice(device); + device_client_.usb_service()->RemoveDevice(device); } TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { @@ -152,8 +140,8 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->AddDevice(device); + device_client_.usb_service()->RemoveDevice(device); } TEST_F(WebUsbDetectorTest, WebUsbBrowserClientIsNullptr) { @@ -169,8 +157,8 @@ TEST_F(WebUsbDetectorTest, WebUsbBrowserClientIsNullptr) { webusb::WebUsbDetector webusb_detector(nullptr); - device_client_.mock_usb_service().AddDevice(device); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->AddDevice(device); + device_client_.usb_service()->RemoveDevice(device); } TEST_F(WebUsbDetectorTest, NoUsbService) { @@ -199,11 +187,11 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid)).Times(1); // usb device was added before webusb_detector was created - device_client_.mock_usb_service().AddDevice(device); + device_client_.usb_service()->AddDevice(device); webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().RemoveDevice(device); + device_client_.usb_service()->RemoveDevice(device); } TEST_F( @@ -243,13 +231,13 @@ TEST_F( // three usb devices were added and removed before webusb_detector was // created - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().AddDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->AddDevice(device_3); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().RemoveDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->RemoveDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_3); webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); } @@ -294,15 +282,15 @@ TEST_F( } // three usb devices were added before webusb_detector was created - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().AddDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->AddDevice(device_3); webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().RemoveDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->RemoveDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_3); } TEST_F(WebUsbDetectorTest, @@ -344,15 +332,15 @@ TEST_F(WebUsbDetectorTest, } // two usb devices were added before webusb_detector was created - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->AddDevice(device_3); webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_3); - device_client_.mock_usb_service().RemoveDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_2); } TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) { @@ -395,12 +383,12 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_2); - device_client_.mock_usb_service().AddDevice(device_3); - device_client_.mock_usb_service().RemoveDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_2); + device_client_.usb_service()->AddDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_3); } TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) { @@ -446,12 +434,12 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) { webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); - device_client_.mock_usb_service().AddDevice(device_1); - device_client_.mock_usb_service().AddDevice(device_2); - device_client_.mock_usb_service().RemoveDevice(device_2); - device_client_.mock_usb_service().AddDevice(device_3); - device_client_.mock_usb_service().RemoveDevice(device_1); - device_client_.mock_usb_service().RemoveDevice(device_3); + device_client_.usb_service()->AddDevice(device_1); + device_client_.usb_service()->AddDevice(device_2); + device_client_.usb_service()->RemoveDevice(device_2); + device_client_.usb_service()->AddDevice(device_3); + device_client_.usb_service()->RemoveDevice(device_1); + device_client_.usb_service()->RemoveDevice(device_3); } } // namespace webusb diff --git a/device/BUILD.gn b/device/BUILD.gn index 1a72de1..e4f56a5 100644 --- a/device/BUILD.gn +++ b/device/BUILD.gn @@ -123,6 +123,7 @@ test("device_unittests") { ] deps += [ "//device/core", + "//device/core:mocks", "//device/devices_app:lib", "//device/devices_app/usb/public/interfaces", "//device/usb", diff --git a/device/core/BUILD.gn b/device/core/BUILD.gn index b7e6ac2..9aaf280 100644 --- a/device/core/BUILD.gn +++ b/device/core/BUILD.gn @@ -24,3 +24,18 @@ component("core") { libs = [ "setupapi.lib" ] } } + +source_set("mocks") { + testonly = true + + sources = [ + "mock_device_client.cc", + "mock_device_client.h", + ] + + deps = [ + ":core", + "//device/hid:mocks", + "//device/usb:mocks", + ] +} diff --git a/device/core/core.gyp b/device/core/core.gyp index 6002a5f..19754cb 100644 --- a/device/core/core.gyp +++ b/device/core/core.gyp @@ -28,5 +28,20 @@ '../../base/base.gyp:base', ] }, + { + 'target_name': 'device_core_mocks', + 'type': 'static_library', + 'include_dirs': [ + '../..', + ], + 'dependencies': [ + '../../testing/gmock.gyp:gmock', + 'device_core', + ], + 'sources': [ + 'mock_device_client.cc', + 'mock_device_client.h', + ], + }, ], } diff --git a/device/core/mock_device_client.cc b/device/core/mock_device_client.cc new file mode 100644 index 0000000..8e82c37 --- /dev/null +++ b/device/core/mock_device_client.cc @@ -0,0 +1,36 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/core/mock_device_client.h" + +#include "device/hid/mock_hid_service.h" +#include "device/usb/mock_usb_service.h" + +namespace device { + +MockDeviceClient::MockDeviceClient() {} + +MockDeviceClient::~MockDeviceClient() {} + +HidService* MockDeviceClient::GetHidService() { + return hid_service(); +} + +UsbService* MockDeviceClient::GetUsbService() { + return usb_service(); +} + +MockHidService* MockDeviceClient::hid_service() { + if (!hid_service_) + hid_service_.reset(new MockHidService()); + return hid_service_.get(); +} + +MockUsbService* MockDeviceClient::usb_service() { + if (!usb_service_) + usb_service_.reset(new MockUsbService()); + return usb_service_.get(); +} + +} // namespace device diff --git a/device/core/mock_device_client.h b/device/core/mock_device_client.h new file mode 100644 index 0000000..e085c63 --- /dev/null +++ b/device/core/mock_device_client.h @@ -0,0 +1,38 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DEVICE_CORE_MOCK_DEVICE_CLIENT_H_ +#define DEVICE_CORE_MOCK_DEVICE_CLIENT_H_ + +#include "base/memory/scoped_ptr.h" +#include "device/core/device_client.h" + +namespace device { + +class HidService; +class MockHidService; +class MockUsbService; +class UsbService; + +class MockDeviceClient : device::DeviceClient { + public: + MockDeviceClient(); + ~MockDeviceClient() override; + + // device::DeviceClient implementation: + UsbService* GetUsbService() override; + HidService* GetHidService() override; + + // Accessors for the mock instances. + MockHidService* hid_service(); + MockUsbService* usb_service(); + + private: + scoped_ptr<MockHidService> hid_service_; + scoped_ptr<MockUsbService> usb_service_; +}; + +} // namespace device + +#endif // DEVICE_CORE_MOCK_DEVICE_CLIENT_H_ diff --git a/device/device_tests.gyp b/device/device_tests.gyp index c0915ff..a3fc8c6 100644 --- a/device/device_tests.gyp +++ b/device/device_tests.gyp @@ -23,11 +23,13 @@ 'bluetooth/bluetooth.gyp:device_bluetooth', 'bluetooth/bluetooth.gyp:device_bluetooth_mocks', 'core/core.gyp:device_core', + 'core/core.gyp:device_core_mocks', 'devices_app/devices_app.gyp:devices_app_lib', 'nfc/nfc.gyp:device_nfc', 'usb/usb.gyp:device_usb', 'usb/usb.gyp:device_usb_mocks', 'hid/hid.gyp:device_hid', + 'hid/hid.gyp:device_hid_mocks', 'serial/serial.gyp:device_serial', 'serial/serial.gyp:device_serial_test_util', ], diff --git a/device/devices_app/usb/device_manager_impl_unittest.cc b/device/devices_app/usb/device_manager_impl_unittest.cc index ca7e0bd..6ab03f6 100644 --- a/device/devices_app/usb/device_manager_impl_unittest.cc +++ b/device/devices_app/usb/device_manager_impl_unittest.cc @@ -11,7 +11,7 @@ #include "base/message_loop/message_loop.h" #include "base/run_loop.h" #include "base/thread_task_runner_handle.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/devices_app/usb/device_impl.h" #include "device/devices_app/usb/device_manager_impl.h" #include "device/devices_app/usb/fake_permission_provider.h" @@ -28,32 +28,12 @@ namespace usb { namespace { -class TestDeviceClient : public DeviceClient { - public: - TestDeviceClient() {} - ~TestDeviceClient() override {} - - MockUsbService& mock_usb_service() { return mock_usb_service_; } - - private: - // DeviceClient implementation: - UsbService* GetUsbService() override { return &mock_usb_service_; } - - MockUsbService mock_usb_service_; -}; - class USBDeviceManagerImplTest : public testing::Test { public: - USBDeviceManagerImplTest() - : device_client_(new TestDeviceClient), - message_loop_(new base::MessageLoop) {} + USBDeviceManagerImplTest() : message_loop_(new base::MessageLoop) {} ~USBDeviceManagerImplTest() override {} protected: - MockUsbService& mock_usb_service() { - return device_client_->mock_usb_service(); - } - DeviceManagerPtr ConnectToDeviceManager() { PermissionProviderPtr permission_provider; permission_provider_.Bind(mojo::GetProxy(&permission_provider)); @@ -63,9 +43,10 @@ class USBDeviceManagerImplTest : public testing::Test { return device_manager.Pass(); } + MockDeviceClient device_client_; + private: FakePermissionProvider permission_provider_; - scoped_ptr<TestDeviceClient> device_client_; scoped_ptr<base::MessageLoop> message_loop_; }; @@ -118,9 +99,9 @@ TEST_F(USBDeviceManagerImplTest, GetDevices) { scoped_refptr<MockUsbDevice> device2 = new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); - mock_usb_service().AddDevice(device0); - mock_usb_service().AddDevice(device1); - mock_usb_service().AddDevice(device2); + device_client_.usb_service()->AddDevice(device0); + device_client_.usb_service()->AddDevice(device1); + device_client_.usb_service()->AddDevice(device2); DeviceManagerPtr device_manager = ConnectToDeviceManager(); @@ -147,7 +128,7 @@ TEST_F(USBDeviceManagerImplTest, GetDevice) { scoped_refptr<MockUsbDevice> mock_device = new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); - mock_usb_service().AddDevice(mock_device); + device_client_.usb_service()->AddDevice(mock_device); DeviceManagerPtr device_manager = ConnectToDeviceManager(); @@ -181,7 +162,7 @@ TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { scoped_refptr<MockUsbDevice> device3 = new MockUsbDevice(0x1234, 0x567b, "ACME", "Frobinator Xtreme", "STUVWX"); - mock_usb_service().AddDevice(device0); + device_client_.usb_service()->AddDevice(device0); DeviceManagerPtr device_manager = ConnectToDeviceManager(); @@ -197,9 +178,9 @@ TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { loop.Run(); } - mock_usb_service().AddDevice(device1); - mock_usb_service().AddDevice(device2); - mock_usb_service().RemoveDevice(device1); + device_client_.usb_service()->AddDevice(device1); + device_client_.usb_service()->AddDevice(device2); + device_client_.usb_service()->RemoveDevice(device1); { std::set<std::string> added_guids; @@ -212,9 +193,9 @@ TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { loop.Run(); } - mock_usb_service().RemoveDevice(device0); - mock_usb_service().RemoveDevice(device2); - mock_usb_service().AddDevice(device3); + device_client_.usb_service()->RemoveDevice(device0); + device_client_.usb_service()->RemoveDevice(device2); + device_client_.usb_service()->AddDevice(device3); { std::set<std::string> added_guids; diff --git a/device/hid/BUILD.gn b/device/hid/BUILD.gn index 9288f70..4270ce8 100644 --- a/device/hid/BUILD.gn +++ b/device/hid/BUILD.gn @@ -61,3 +61,17 @@ source_set("hid") { deps += [ "//chromeos" ] } } + +source_set("mocks") { + testonly = true + + sources = [ + "mock_hid_service.cc", + "mock_hid_service.h", + ] + + public_deps = [ + ":hid", + "//testing/gmock", + ] +} diff --git a/device/hid/hid.gyp b/device/hid/hid.gyp index f7a0baf..2d0169b 100644 --- a/device/hid/hid.gyp +++ b/device/hid/hid.gyp @@ -66,5 +66,20 @@ }], ], }, + { + 'target_name': 'device_hid_mocks', + 'type': 'static_library', + 'include_dirs': [ + '../..', + ], + 'dependencies': [ + '../../testing/gmock.gyp:gmock', + 'device_hid', + ], + 'sources': [ + 'mock_hid_service.cc', + 'mock_hid_service.h', + ], + }, ], } diff --git a/device/hid/mock_hid_service.cc b/device/hid/mock_hid_service.cc new file mode 100644 index 0000000..7c73a35 --- /dev/null +++ b/device/hid/mock_hid_service.cc @@ -0,0 +1,30 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/hid/mock_hid_service.h" + +namespace device { + +MockHidService::MockHidService() {} + +MockHidService::~MockHidService() {} + +void MockHidService::AddDevice(scoped_refptr<HidDeviceInfo> info) { + HidService::AddDevice(info); +} + +void MockHidService::RemoveDevice(const HidDeviceId& device_id) { + HidService::RemoveDevice(device_id); +} + +void MockHidService::FirstEnumerationComplete() { + HidService::FirstEnumerationComplete(); +} + +const std::map<HidDeviceId, scoped_refptr<HidDeviceInfo>>& +MockHidService::devices() const { + return HidService::devices(); +} + +} // namespace device diff --git a/device/hid/mock_hid_service.h b/device/hid/mock_hid_service.h new file mode 100644 index 0000000..f5f6c82 --- /dev/null +++ b/device/hid/mock_hid_service.h @@ -0,0 +1,33 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DEVICE_HID_MOCK_HID_SERVICE_H_ +#define DEVICE_HID_MOCK_HID_SERVICE_H_ + +#include <map> + +#include "device/hid/hid_service.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace device { + +class MockHidService : public HidService { + public: + MockHidService(); + ~MockHidService() override; + + // Public wrappers around protected functions needed for tests. + void AddDevice(scoped_refptr<HidDeviceInfo> info); + void RemoveDevice(const HidDeviceId& device_id); + void FirstEnumerationComplete(); + const std::map<HidDeviceId, scoped_refptr<HidDeviceInfo>>& devices() const; + + MOCK_METHOD2(Connect, + void(const HidDeviceId& device_id, + const ConnectCallback& callback)); +}; + +} // namespace device + +#endif // DEVICE_HID_MOCK_HID_SERVICE_H_ diff --git a/device/usb/BUILD.gn b/device/usb/BUILD.gn index 6d17f7d..99e12ca 100644 --- a/device/usb/BUILD.gn +++ b/device/usb/BUILD.gn @@ -86,6 +86,9 @@ source_set("mocks") { deps = [ ":usb", "//base", + ] + + public_deps = [ "//testing/gmock", ] } diff --git a/device/usb/mock_usb_service.cc b/device/usb/mock_usb_service.cc index f9bf9d9..6a205fc 100644 --- a/device/usb/mock_usb_service.cc +++ b/device/usb/mock_usb_service.cc @@ -4,6 +4,9 @@ #include "device/usb/mock_usb_service.h" +#include <string> +#include <vector> + #include "device/usb/usb_device.h" namespace device { @@ -33,9 +36,8 @@ scoped_refptr<UsbDevice> MockUsbService::GetDevice(const std::string& guid) { void MockUsbService::GetDevices(const GetDevicesCallback& callback) { std::vector<scoped_refptr<UsbDevice>> devices; - for (const auto& map_entry : devices_) { + for (const auto& map_entry : devices_) devices.push_back(map_entry.second); - } callback.Run(devices); } diff --git a/extensions/BUILD.gn b/extensions/BUILD.gn index 1a86e84..ca739a1 100644 --- a/extensions/BUILD.gn +++ b/extensions/BUILD.gn @@ -260,6 +260,8 @@ test("extensions_browsertests") { "//components/storage_monitor:test_support", "//content/test:test_support", "//device/bluetooth:mocks", + "//device/core:mocks", + "//device/hid:mocks", "//device/usb:mocks", "//mojo/environment:chromium", "//mojo/public/cpp/bindings", diff --git a/extensions/browser/api/hid/hid_apitest.cc b/extensions/browser/api/hid/hid_apitest.cc index 1e35ecd..955c67c 100644 --- a/extensions/browser/api/hid/hid_apitest.cc +++ b/extensions/browser/api/hid/hid_apitest.cc @@ -5,12 +5,12 @@ #include "base/bind.h" #include "base/run_loop.h" #include "base/thread_task_runner_handle.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/hid/hid_collection_info.h" #include "device/hid/hid_connection.h" #include "device/hid/hid_device_info.h" -#include "device/hid/hid_service.h" #include "device/hid/hid_usage_and_page.h" +#include "device/hid/mock_hid_service.h" #include "extensions/browser/api/device_permissions_prompt.h" #include "extensions/shell/browser/shell_extensions_api_client.h" #include "extensions/shell/test/shell_apitest.h" @@ -19,12 +19,12 @@ using base::ThreadTaskRunnerHandle; using device::HidCollectionInfo; -using device::HidConnection; using device::HidDeviceId; using device::HidDeviceInfo; -using device::HidService; using device::HidUsageAndPage; +using device::MockDeviceClient; using net::IOBuffer; +using testing::_; #if defined(OS_MACOSX) const uint64_t kTestDeviceIds[] = {1, 2, 3, 4, 5}; @@ -32,8 +32,6 @@ const uint64_t kTestDeviceIds[] = {1, 2, 3, 4, 5}; const char* kTestDeviceIds[] = {"A", "B", "C", "D", "E"}; #endif -namespace device { - // These report descriptors define two devices with 8-byte input, output and // feature reports. The first implements usage page 0xFF00 and has a single // report without and ID. The second implements usage page 0xFF01 and has a @@ -47,9 +45,11 @@ const uint8 kReportDescriptorWithIDs[] = { 0xFF, 0x00, 0x85, 0x01, 0x75, 0x08, 0x95, 0x08, 0x08, 0x81, 0x02, 0x08, 0x91, 0x02, 0x08, 0xB1, 0x02, 0xC0}; -class MockHidConnection : public HidConnection { +namespace extensions { + +class MockHidConnection : public device::HidConnection { public: - MockHidConnection(scoped_refptr<HidDeviceInfo> device_info) + explicit MockHidConnection(scoped_refptr<HidDeviceInfo> device_info) : HidConnection(device_info) {} void PlatformClose() override {} @@ -117,80 +117,11 @@ class MockHidConnection : public HidConnection { ~MockHidConnection() override {} }; -class MockHidService : public HidService { - public: - MockHidService() : HidService() { - // Verify that devices are enumerated properly even when the first - // enumeration happens asynchronously. - ThreadTaskRunnerHandle::Get()->PostTask( - FROM_HERE, base::Bind(&MockHidService::LazyFirstEnumeration, - base::Unretained(this))); - } - - void Connect(const HidDeviceId& device_id, - const ConnectCallback& callback) override { - const auto& device_entry = devices().find(device_id); - scoped_refptr<HidConnection> connection; - if (device_entry != devices().end()) { - connection = new MockHidConnection(device_entry->second); - } - - ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, - base::Bind(callback, connection)); - } - - void LazyFirstEnumeration() { - AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, false); - AddDevice(kTestDeviceIds[1], 0x18D1, 0x58F0, true); - AddDevice(kTestDeviceIds[2], 0x18D1, 0x58F1, false); - FirstEnumerationComplete(); - } - - void AddDevice(const HidDeviceId& device_id, - int vendor_id, - int product_id, - bool report_id) { - std::vector<uint8> report_descriptor; - if (report_id) { - report_descriptor.insert( - report_descriptor.begin(), kReportDescriptorWithIDs, - kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); - } else { - report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, - kReportDescriptor + sizeof(kReportDescriptor)); - } - HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, - "Test Device", "A", kHIDBusTypeUSB, - report_descriptor)); - } - - void RemoveDevice(const HidDeviceId& device_id) { - HidService::RemoveDevice(device_id); - } -}; - -class TestDeviceClient : public DeviceClient { - public: - TestDeviceClient() : DeviceClient() {} - ~TestDeviceClient() override {} - - MockHidService& mock_service() { return hid_service_; } - - private: - HidService* GetHidService() override { return &hid_service_; } - - MockHidService hid_service_; -}; - -} // namespace device - -namespace extensions { - class TestDevicePermissionsPrompt : public DevicePermissionsPrompt, public DevicePermissionsPrompt::Prompt::Observer { public: - TestDevicePermissionsPrompt(content::WebContents* web_contents) + explicit TestDevicePermissionsPrompt(content::WebContents* web_contents) : DevicePermissionsPrompt(web_contents) {} ~TestDevicePermissionsPrompt() override { prompt()->SetObserver(nullptr); } @@ -222,11 +153,57 @@ class HidApiTest : public ShellApiTest { public: void SetUpOnMainThread() override { ShellApiTest::SetUpOnMainThread(); - device_client_.reset(new device::TestDeviceClient()); + + // MockDeviceClient replaces ShellDeviceClient. + device_client_.reset(new MockDeviceClient()); + + ON_CALL(*device_client_->hid_service(), Connect(_, _)) + .WillByDefault(Invoke(this, &HidApiTest::Connect)); + ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::Bind(&HidApiTest::LazyFirstEnumeration, base::Unretained(this))); + } + + void Connect(const HidDeviceId& device_id, + const device::HidService::ConnectCallback& callback) { + const auto& devices = device_client_->hid_service()->devices(); + const auto& device_entry = devices.find(device_id); + scoped_refptr<MockHidConnection> connection; + if (device_entry != devices.end()) { + connection = new MockHidConnection(device_entry->second); + } + + ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, + base::Bind(callback, connection)); + } + + void LazyFirstEnumeration() { + AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, false); + AddDevice(kTestDeviceIds[1], 0x18D1, 0x58F0, true); + AddDevice(kTestDeviceIds[2], 0x18D1, 0x58F1, false); + device_client_->hid_service()->FirstEnumerationComplete(); + } + + void AddDevice(const HidDeviceId& device_id, + int vendor_id, + int product_id, + bool report_id) { + std::vector<uint8> report_descriptor; + if (report_id) { + report_descriptor.insert( + report_descriptor.begin(), kReportDescriptorWithIDs, + kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); + } else { + report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, + kReportDescriptor + sizeof(kReportDescriptor)); + } + device_client_->hid_service()->AddDevice( + new HidDeviceInfo(device_id, vendor_id, product_id, "Test Device", "A", + device::kHIDBusTypeUSB, report_descriptor)); } protected: - scoped_ptr<device::TestDeviceClient> device_client_; + scoped_ptr<MockDeviceClient> device_client_; }; IN_PROC_BROWSER_TEST_F(HidApiTest, HidApp) { @@ -243,10 +220,8 @@ IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { // Add a blocked device first so that the test will fail if a notification is // received. - device_client_->mock_service().AddDevice(kTestDeviceIds[3], 0x18D1, 0x58F1, - false); - device_client_->mock_service().AddDevice(kTestDeviceIds[4], 0x18D1, 0x58F0, - false); + AddDevice(kTestDeviceIds[3], 0x18D1, 0x58F1, false); + AddDevice(kTestDeviceIds[4], 0x18D1, 0x58F0, false); ASSERT_TRUE(result_listener.WaitUntilSatisfied()); EXPECT_EQ("success", result_listener.message()); } @@ -261,9 +236,9 @@ IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceRemoved) { // Device C was not returned by chrome.hid.getDevices, the app will not get // a notification. - device_client_->mock_service().RemoveDevice(kTestDeviceIds[2]); + device_client_->hid_service()->RemoveDevice(kTestDeviceIds[2]); // Device A was returned, the app will get a notification. - device_client_->mock_service().RemoveDevice(kTestDeviceIds[0]); + device_client_->hid_service()->RemoveDevice(kTestDeviceIds[0]); ASSERT_TRUE(result_listener.WaitUntilSatisfied()); EXPECT_EQ("success", result_listener.message()); } @@ -276,12 +251,11 @@ IN_PROC_BROWSER_TEST_F(HidApiTest, GetUserSelectedDevices) { ASSERT_TRUE(open_listener.WaitUntilSatisfied()); ExtensionTestMessageListener remove_listener("removed", false); - device_client_->mock_service().RemoveDevice(kTestDeviceIds[0]); + device_client_->hid_service()->RemoveDevice(kTestDeviceIds[0]); ASSERT_TRUE(remove_listener.WaitUntilSatisfied()); ExtensionTestMessageListener add_listener("added", false); - device_client_->mock_service().AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, - true); + AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, true); ASSERT_TRUE(add_listener.WaitUntilSatisfied()); } diff --git a/extensions/browser/api/usb/usb_apitest.cc b/extensions/browser/api/usb/usb_apitest.cc index 7b654c7..4257630 100644 --- a/extensions/browser/api/usb/usb_apitest.cc +++ b/extensions/browser/api/usb/usb_apitest.cc @@ -5,7 +5,7 @@ #include "chrome/browser/extensions/extension_apitest.h" #include "content/public/browser/browser_thread.h" #include "content/public/test/test_utils.h" -#include "device/core/device_client.h" +#include "device/core/mock_device_client.h" #include "device/usb/mock_usb_device.h" #include "device/usb/mock_usb_device_handle.h" #include "device/usb/mock_usb_service.h" @@ -20,17 +20,13 @@ using testing::_; using testing::AnyNumber; using testing::Invoke; using testing::Return; -using content::BrowserThread; -using device::DeviceClient; +using device::MockDeviceClient; using device::MockUsbDevice; using device::MockUsbDeviceHandle; -using device::MockUsbService; using device::UsbConfigDescriptor; -using device::UsbDevice; using device::UsbDeviceHandle; using device::UsbEndpointDirection; using device::UsbInterfaceDescriptor; -using device::UsbService; namespace extensions { @@ -54,7 +50,7 @@ class TestDevicePermissionsPrompt : public DevicePermissionsPrompt, public DevicePermissionsPrompt::Prompt::Observer { public: - TestDevicePermissionsPrompt(content::WebContents* web_contents) + explicit TestDevicePermissionsPrompt(content::WebContents* web_contents) : DevicePermissionsPrompt(web_contents) {} void ShowDialog() override { prompt()->SetObserver(this); } @@ -70,19 +66,6 @@ class TestDevicePermissionsPrompt } }; -class TestDeviceClient : public DeviceClient { - public: - TestDeviceClient() : DeviceClient() {} - ~TestDeviceClient() override {} - - MockUsbService& mock_usb_service() { return usb_service_; } - - private: - UsbService* GetUsbService() override { return &usb_service_; } - - MockUsbService usb_service_; -}; - class TestExtensionsAPIClient : public ShellExtensionsAPIClient { public: TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} @@ -98,6 +81,9 @@ class UsbApiTest : public ShellApiTest { void SetUpOnMainThread() override { ShellApiTest::SetUpOnMainThread(); + // MockDeviceClient replaces ShellDeviceClient. + device_client_.reset(new MockDeviceClient()); + std::vector<UsbConfigDescriptor> configs; UsbConfigDescriptor config; config.configuration_value = 1; @@ -110,14 +96,13 @@ class UsbApiTest : public ShellApiTest { mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get()); EXPECT_CALL(*mock_device_.get(), Open(_)) .WillRepeatedly(InvokeCallback<0>(mock_device_handle_)); - device_client_.reset(new TestDeviceClient()); - device_client_->mock_usb_service().AddDevice(mock_device_); + device_client_->usb_service()->AddDevice(mock_device_); } protected: scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; scoped_refptr<MockUsbDevice> mock_device_; - scoped_ptr<TestDeviceClient> device_client_; + scoped_ptr<MockDeviceClient> device_client_; }; } // namespace @@ -221,10 +206,10 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { ASSERT_TRUE(load_listener.WaitUntilSatisfied()); scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0)); - device_client_->mock_usb_service().AddDevice(device); + device_client_->usb_service()->AddDevice(device); device = new MockUsbDevice(0x18D1, 0x58F1); - device_client_->mock_usb_service().AddDevice(device); + device_client_->usb_service()->AddDevice(device); ASSERT_TRUE(result_listener.WaitUntilSatisfied()); } @@ -237,7 +222,7 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); ASSERT_TRUE(load_listener.WaitUntilSatisfied()); - device_client_->mock_usb_service().RemoveDevice(mock_device_); + device_client_->usb_service()->RemoveDevice(mock_device_); ASSERT_TRUE(result_listener.WaitUntilSatisfied()); } @@ -252,7 +237,7 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); - device_client_->mock_usb_service().RemoveDevice(mock_device_); + device_client_->usb_service()->RemoveDevice(mock_device_); ASSERT_TRUE(result_listener.WaitUntilSatisfied()); } diff --git a/extensions/extensions_tests.gyp b/extensions/extensions_tests.gyp index ee619eb..771d231 100644 --- a/extensions/extensions_tests.gyp +++ b/extensions/extensions_tests.gyp @@ -85,6 +85,8 @@ '<(DEPTH)/content/content_shell_and_tests.gyp:content_browser_test_support', '<(DEPTH)/content/content_shell_and_tests.gyp:test_support_content', '<(DEPTH)/device/bluetooth/bluetooth.gyp:device_bluetooth_mocks', + '<(DEPTH)/device/core/core.gyp:device_core_mocks', + '<(DEPTH)/device/hid/hid.gyp:device_hid_mocks', '<(DEPTH)/device/usb/usb.gyp:device_usb_mocks', '<(DEPTH)/testing/gmock.gyp:gmock', '<(DEPTH)/testing/gtest.gyp:gtest', |