Chromium Code Reviews| Index: components/webusb/webusb_detector_unittest.cc |
| diff --git a/components/webusb/webusb_detector_unittest.cc b/components/webusb/webusb_detector_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..153e41d67d2d107fd3868781fe405427f98c78a4 |
| --- /dev/null |
| +++ b/components/webusb/webusb_detector_unittest.cc |
| @@ -0,0 +1,300 @@ |
| +// 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 "components/webusb/webusb_detector.h" |
| + |
| +#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/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" |
| + |
| +namespace webusb { |
| + |
| +namespace { |
| + |
| +class FakeDeviceClient : public device::DeviceClient { |
| + public: |
| + FakeDeviceClient() : usb_service_(nullptr) {} |
| + |
| + ~FakeDeviceClient() override {} |
| + |
| + // device::DeviceClient implementation: |
| + device::UsbService* GetUsbService() override { return usb_service_; } |
| + |
| + void set_usb_service(device::UsbService* service) { usb_service_ = service; } |
| + |
| + private: |
| + device::UsbService* usb_service_; |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
Same suggestion to use MockUsbService directly her
juncai
2015/09/08 21:14:00
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeDeviceClient); |
| +}; |
| + |
| +class MockWebUsbBrowserClient : public webusb::WebUsbBrowserClient { |
| + public: |
| + MockWebUsbBrowserClient() {} |
| + |
| + ~MockWebUsbBrowserClient() override {} |
| + |
| + // webusb::WebUsbBrowserClient implementation |
| + MOCK_METHOD3(OnDeviceAdded, |
| + void(const base::string16& product_name, |
| + const GURL& landing_page, |
| + const std::string& notification_id)); |
| + |
| + // webusb::WebUsbBrowserClient implementation |
| + MOCK_METHOD1(OnDeviceRemoved, void(const std::string& notification_id)); |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MockWebUsbBrowserClient); |
| +}; |
| + |
| +} // namespace |
| + |
| +class WebUsbDetectorTest : public testing::Test { |
| + public: |
| + WebUsbDetectorTest() {} |
| + |
| + ~WebUsbDetectorTest() override = default; |
| + |
| + void Initialize(scoped_refptr<device::MockUsbDevice> device, |
| + webusb::WebUsbBrowserClient* webusb_browser_client, |
| + bool set_usb_service_for_device_client) { |
| + device_ = device; |
| + usb_service_.reset(new device::MockUsbService()); |
| + device_client_.reset(new FakeDeviceClient()); |
| + if (set_usb_service_for_device_client) { |
| + device_client_->set_usb_service(usb_service_.get()); |
| + } |
| + webusb_detector_.reset(new webusb::WebUsbDetector(webusb_browser_client)); |
| + } |
| + |
| + protected: |
| + void InvokeUsbAdded() { usb_service_->AddDevice(device_); } |
| + |
| + void InvokeUsbRemoved() { usb_service_->RemoveDevice(device_); } |
| + |
| + scoped_refptr<device::MockUsbDevice> device_; |
| + scoped_ptr<device::MockUsbService> usb_service_; |
| + scoped_ptr<FakeDeviceClient> device_client_; |
| + scoped_ptr<webusb::WebUsbDetector> webusb_detector_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); |
| +}; |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDevicePluggedIn) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + base::string16 product_name = base::UTF8ToUTF16(product_string); |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(product_name, landing_page, guid)) |
| + .Times(1); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDevicePluggedInAndRemoved) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + base::string16 product_name = base::UTF8ToUTF16(product_string); |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(product_name, landing_page, guid)) |
| + .Times(1); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDeviceWithEmptyProductNamePluggedInAndRemoved) { |
| + std::string product_string = ""; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDeviceNoLandingPagePluggedInAndRemoved) { |
| + std::string product_string = "Google Product B"; |
| + GURL landing_page(""); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "002", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, WebUsbBrowserClientIsNullptr) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); |
| + |
| + Initialize(device, nullptr, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +// GetUsbService is nullptr |
| +TEST_F(WebUsbDetectorTest, GetUsbServiceIsNullptr) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); |
| + |
| + Initialize(device, &mock_webusb_browser_client, false); |
| + |
| + InvokeUsbAdded(); |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
It's weird for this test to have a UsbService but
juncai
2015/09/08 21:14:00
Done.
|
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDevicePluggedInAndRemovedMultipleTimes) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + base::string16 product_name = base::UTF8ToUTF16(product_string); |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(product_name, landing_page, guid)) |
| + .Times(3); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(3); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| + InvokeUsbAdded(); |
| + InvokeUsbRemoved(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDevicePluggedInMultipleTimes) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + base::string16 product_name = base::UTF8ToUTF16(product_string); |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(product_name, landing_page, guid)) |
| + .Times(3); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbAdded(); |
| + InvokeUsbAdded(); |
| + InvokeUsbAdded(); |
| +} |
| + |
| +TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndRemovedMultipleTimes) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page("https://www.google.com"); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + |
| + MockWebUsbBrowserClient mock_webusb_browser_client; |
| + |
| + std::string guid = device->guid(); |
| + EXPECT_CALL(mock_webusb_browser_client, |
| + OnDeviceAdded(testing::_, testing::_, testing::_)) |
| + .Times(0); |
| + EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(3); |
| + |
| + Initialize(device, &mock_webusb_browser_client, true); |
| + |
| + InvokeUsbRemoved(); |
| + InvokeUsbRemoved(); |
| + InvokeUsbRemoved(); |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
This is kind of a bug in the mocks... it shouldn't
juncai
2015/09/08 21:14:00
Done.
|
| +} |
| + |
| +} // namespace webusb |