Chromium Code Reviews| Index: chrome/browser/chrome_webusb_browser_client_unittest.cc |
| diff --git a/chrome/browser/chrome_webusb_browser_client_unittest.cc b/chrome/browser/chrome_webusb_browser_client_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd16e5f149d2bcf7109bcfd87d1527662434c1f4 |
| --- /dev/null |
| +++ b/chrome/browser/chrome_webusb_browser_client_unittest.cc |
| @@ -0,0 +1,168 @@ |
| +// 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 "chrome/browser/chrome_webusb_browser_client.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "components/webusb/webusb_detector.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/gtest/include/gtest/gtest.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/message_center/message_center.h" |
| +#include "ui/message_center/notification.h" |
| +#include "ui/message_center/notification_delegate.h" |
| +#include "url/gurl.h" |
| + |
| +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; } |
|
Lei Zhang
2015/09/04 21:10:18
Just roll this into the ctor?
juncai
2015/09/08 21:14:00
Done.
|
| + |
| + private: |
| + device::UsbService* usb_service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeDeviceClient); |
| +}; |
| + |
| +} // namespace |
| + |
| +class ChromeWebUsbBrowserClientTest : public testing::Test { |
| + public: |
| + ChromeWebUsbBrowserClientTest() {} |
| + |
| + ~ChromeWebUsbBrowserClientTest() override = default; |
| + |
| + void SetUp() override { message_center::MessageCenter::Initialize(); } |
| + |
| + void TearDown() override { message_center::MessageCenter::Shutdown(); } |
| + |
| + void Initialize(scoped_refptr<device::MockUsbDevice> device) { |
| + device_ = device; |
| + usb_service_.reset(new device::MockUsbService()); |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
The pattern in other tests that need a MockUsbServ
juncai
2015/09/08 21:14:00
Done.
|
| + device_client_.reset(new FakeDeviceClient()); |
| + device_client_->set_usb_service(usb_service_.get()); |
| + webusb_browser_client_.reset(new ChromeWebUsbBrowserClient()); |
| + webusb_detector_.reset( |
| + new webusb::WebUsbDetector(webusb_browser_client_.get())); |
| + } |
| + |
| + 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::WebUsbBrowserClient> webusb_browser_client_; |
| + scoped_ptr<webusb::WebUsbDetector> webusb_detector_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ChromeWebUsbBrowserClientTest); |
| +}; |
| + |
| +TEST_F(ChromeWebUsbBrowserClientTest, 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)); |
| + std::string guid = device->guid(); |
| + |
| + Initialize(device); |
| + |
| + InvokeUsbAdded(); |
| + |
| + message_center::MessageCenter* message_center = |
| + message_center::MessageCenter::Get(); |
| + EXPECT_TRUE(message_center != nullptr); |
|
Lei Zhang
2015/09/04 21:10:18
You can omit the != nullptr, ditto below.
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
ASSERT_TRUE so that later code doesn't crash deref
juncai
2015/09/08 21:14:00
Done.
juncai
2015/09/08 21:14:00
Done.
|
| + |
| + message_center::Notification* notification = |
| + message_center->FindVisibleNotificationById(guid); |
| + EXPECT_TRUE(notification != nullptr); |
| + |
| + base::string16 expected_title = |
| + l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
Use the literal English strings here (with base::A
juncai
2015/09/08 21:14:00
Done.
|
| + base::UTF8ToUTF16(product_string)); |
|
Lei Zhang
2015/09/04 21:10:18
nit: you can use ASCIIToUTF16()
juncai
2015/09/08 21:14:00
Done.
|
| + EXPECT_EQ(expected_title, notification->title()); |
| + |
| + base::string16 expected_message = |
| + l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION); |
| + EXPECT_EQ(expected_message, notification->message()); |
| + |
| + message_center::NotificationDelegate* notification_delegate = |
| + notification->delegate(); |
| + EXPECT_TRUE(notification_delegate != nullptr); |
|
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
EXPECT_TRUE(notification->delegate() != nullptr);
juncai
2015/09/08 21:14:00
Done.
|
| + |
| + InvokeUsbRemoved(); |
| + |
| + notification = message_center->FindVisibleNotificationById(guid); |
| + // device is removed, so notification should be removed from the |
| + // message_center too |
| + EXPECT_EQ(nullptr, notification); |
| +} |
| + |
| +TEST_F(ChromeWebUsbBrowserClientTest, |
| + UsbDeviceNoLandingPagePluggedInAndRemoved) { |
| + std::string product_string = "Google Product A"; |
| + GURL landing_page(""); |
| + scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
| + 0, 1, "Google", product_string, "001", landing_page)); |
| + std::string guid = device->guid(); |
| + |
| + Initialize(device); |
| + |
| + InvokeUsbAdded(); |
| + |
| + message_center::MessageCenter* message_center = |
| + message_center::MessageCenter::Get(); |
| + EXPECT_TRUE(message_center != nullptr); |
| + |
| + message_center::Notification* notification = |
| + message_center->FindVisibleNotificationById(guid); |
| + |
| + // for device with no landing page, no notification is generated |
| + EXPECT_EQ(nullptr, notification); |
| + |
| + InvokeUsbRemoved(); |
| + |
| + notification = message_center->FindVisibleNotificationById(guid); |
| + EXPECT_EQ(nullptr, notification); |
| +} |
| + |
| +TEST_F(ChromeWebUsbBrowserClientTest, 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)); |
| + std::string guid = device->guid(); |
| + |
| + Initialize(device); |
| + |
| + message_center::MessageCenter* message_center = |
| + message_center::MessageCenter::Get(); |
| + EXPECT_TRUE(message_center != nullptr); |
| + |
| + message_center::Notification* notification = |
| + message_center->FindVisibleNotificationById(guid); |
| + EXPECT_EQ(nullptr, notification); |
| + |
| + InvokeUsbRemoved(); |
| + |
| + notification = message_center->FindVisibleNotificationById(guid); |
| + EXPECT_EQ(nullptr, notification); |
| +} |