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

Unified Diff: extensions/browser/api/usb/usb_apitest.cc

Issue 800963005: Add browser tests for USB device add/remove events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: extensions/browser/api/usb/usb_apitest.cc
diff --git a/extensions/browser/api/usb/usb_apitest.cc b/extensions/browser/api/usb/usb_apitest.cc
index 57e073fe64a6146b92e8b46326e02ff74ba85bcd..3960731cbcf986dc8e2e4e262cc0b28134d763c4 100644
--- a/extensions/browser/api/usb/usb_apitest.cc
+++ b/extensions/browser/api/usb/usb_apitest.cc
@@ -8,6 +8,7 @@
#include "device/usb/usb_service.h"
#include "extensions/browser/api/usb/usb_api.h"
#include "extensions/shell/test/shell_apitest.h"
+#include "extensions/test/extension_test_message_listener.h"
#include "net/base/io_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -126,6 +127,16 @@ class MockUsbService : public UsbService {
public:
explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
+ // Public wrapper around the protected base class method.
Yoyo Zhou 2015/01/21 06:57:43 Why not make the base class friend class UsbApiTes
Reilly Grant (use Gerrit) 2015/01/22 00:45:01 I tried this and no one in my office could figure
+ void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
+ UsbService::NotifyDeviceAdded(device);
+ }
+
+ // Public wrapper around the protected base class method.
+ void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
+ UsbService::NotifyDeviceRemoved(device);
+ }
+
protected:
scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override {
EXPECT_EQ(unique_id, 0U);
@@ -144,8 +155,12 @@ class UsbApiTest : public ShellApiTest {
public:
void SetUpOnMainThread() override {
ShellApiTest::SetUpOnMainThread();
- mock_device_handle_ = new MockUsbDeviceHandle();
+
mock_device_ = new MockUsbDevice(0, 0, 0);
+ EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_))
+ .WillRepeatedly(Return(false));
+
+ mock_device_handle_ = new MockUsbDeviceHandle();
mock_device_handle_->set_device(mock_device_.get());
EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _))
.WillRepeatedly(Invoke(RequestUsbAccess));
@@ -160,22 +175,26 @@ class UsbApiTest : public ShellApiTest {
}
void SetUpService() {
- UsbService::SetInstanceForTest(new MockUsbService(mock_device_));
+ mock_service_ = new MockUsbService(mock_device_);
+ UsbService::SetInstanceForTest(mock_service_);
}
- void TearDownOnMainThread() override {
- UsbService* service = NULL;
- base::RunLoop run_loop;
- BrowserThread::PostTaskAndReply(
- BrowserThread::FILE, FROM_HERE,
- base::Bind(&UsbService::SetInstanceForTest, service),
- run_loop.QuitClosure());
- run_loop.Run();
+ void AddTestDevices() {
+ scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1));
+ EXPECT_CALL(*device.get(), GetSerialNumber(_))
+ .WillRepeatedly(Return(false));
+ mock_service_->NotifyDeviceAdded(device);
+
+ device = new MockUsbDevice(0x18D1, 0x58F1, 2);
+ EXPECT_CALL(*device.get(), GetSerialNumber(_))
+ .WillRepeatedly(Return(false));
+ mock_service_->NotifyDeviceAdded(device);
}
protected:
scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
scoped_refptr<MockUsbDevice> mock_device_;
+ MockUsbService* mock_service_;
};
} // namespace
@@ -263,4 +282,43 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer"));
}
+IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
+ ExtensionTestMessageListener load_listener("loaded", false);
+ ExtensionTestMessageListener result_listener("success", false);
+ result_listener.set_failure_message("failure");
+
+ ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
+ ASSERT_TRUE(load_listener.WaitUntilSatisfied());
+
+ base::RunLoop run_loop;
+ BrowserThread::PostTaskAndReply(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)),
+ run_loop.QuitClosure());
+ run_loop.Run();
+
+ ASSERT_TRUE(result_listener.WaitUntilSatisfied());
+ ASSERT_EQ("success", result_listener.message());
+}
+
+IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
+ ExtensionTestMessageListener load_listener("loaded", false);
+ ExtensionTestMessageListener result_listener("success", false);
+ result_listener.set_failure_message("failure");
+
+ ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
+ ASSERT_TRUE(load_listener.WaitUntilSatisfied());
+
+ base::RunLoop run_loop;
+ BrowserThread::PostTaskAndReply(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&MockUsbService::NotifyDeviceRemoved,
+ base::Unretained(mock_service_), mock_device_),
+ run_loop.QuitClosure());
+ run_loop.Run();
+
+ ASSERT_TRUE(result_listener.WaitUntilSatisfied());
+ ASSERT_EQ("success", result_listener.message());
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698