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

Side by Side Diff: extensions/browser/api/usb/usb_apitest.cc

Issue 826283002: Add support for sending a USB SET_CONFIGURATION request. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. 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 unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/extension_apitest.h" 5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "content/public/browser/browser_thread.h" 6 #include "content/public/browser/browser_thread.h"
7 #include "content/public/test/test_utils.h" 7 #include "content/public/test/test_utils.h"
8 #include "device/usb/usb_service.h" 8 #include "device/usb/usb_service.h"
9 #include "extensions/browser/api/usb/usb_api.h" 9 #include "extensions/browser/api/usb/usb_api.h"
10 #include "extensions/shell/test/shell_apitest.h" 10 #include "extensions/shell/test/shell_apitest.h"
11 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
12 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
13 13
14 using testing::_;
14 using testing::AnyNumber; 15 using testing::AnyNumber;
15 using testing::_; 16 using testing::Invoke;
16 using testing::Return; 17 using testing::Return;
17 using testing::ReturnRef;
18 using content::BrowserThread; 18 using content::BrowserThread;
19 using device::UsbConfigDescriptor; 19 using device::UsbConfigDescriptor;
20 using device::UsbDevice; 20 using device::UsbDevice;
21 using device::UsbDeviceHandle; 21 using device::UsbDeviceHandle;
22 using device::UsbEndpointDirection; 22 using device::UsbEndpointDirection;
23 using device::UsbInterfaceDescriptor; 23 using device::UsbInterfaceDescriptor;
24 using device::UsbService; 24 using device::UsbService;
25 using device::UsbTransferCallback; 25 using device::UsbTransferCallback;
26 26
27 namespace extensions { 27 namespace extensions {
28 28
29 namespace { 29 namespace {
30 30
31 ACTION_TEMPLATE(InvokeUsbTransferCallback, 31 ACTION_TEMPLATE(InvokeUsbTransferCallback,
32 HAS_1_TEMPLATE_PARAMS(int, k), 32 HAS_1_TEMPLATE_PARAMS(int, k),
33 AND_1_VALUE_PARAMS(p1)) { 33 AND_1_VALUE_PARAMS(p1)) {
34 net::IOBuffer* io_buffer = new net::IOBuffer(1); 34 net::IOBuffer* io_buffer = new net::IOBuffer(1);
35 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. 35 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
36 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); 36 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
37 } 37 }
38 38
39 void RequestUsbAccess(int interface_id,
40 const base::Callback<void(bool success)>& callback) {
41 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true));
42 }
43
39 class MockUsbDeviceHandle : public UsbDeviceHandle { 44 class MockUsbDeviceHandle : public UsbDeviceHandle {
40 public: 45 public:
41 MockUsbDeviceHandle() : UsbDeviceHandle() {} 46 MockUsbDeviceHandle() : UsbDeviceHandle() {}
42 47
43 MOCK_METHOD0(Close, void()); 48 MOCK_METHOD0(Close, void());
44 49
45 MOCK_METHOD10(ControlTransfer, 50 MOCK_METHOD10(ControlTransfer,
46 void(UsbEndpointDirection direction, 51 void(UsbEndpointDirection direction,
47 TransferRequestType request_type, 52 TransferRequestType request_type,
48 TransferRecipient recipient, 53 TransferRecipient recipient,
(...skipping 26 matching lines...) Expand all
75 uint8 endpoint, 80 uint8 endpoint,
76 net::IOBuffer* buffer, 81 net::IOBuffer* buffer,
77 size_t length, 82 size_t length,
78 unsigned int packets, 83 unsigned int packets,
79 unsigned int packet_length, 84 unsigned int packet_length,
80 unsigned int timeout, 85 unsigned int timeout,
81 const UsbTransferCallback& callback)); 86 const UsbTransferCallback& callback));
82 87
83 MOCK_METHOD0(ResetDevice, bool()); 88 MOCK_METHOD0(ResetDevice, bool());
84 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); 89 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*));
90 MOCK_METHOD1(SetConfiguration, bool(int));
85 MOCK_METHOD1(ClaimInterface, bool(int interface_number)); 91 MOCK_METHOD1(ClaimInterface, bool(int interface_number));
86 MOCK_METHOD1(ReleaseInterface, bool(int interface_number)); 92 MOCK_METHOD1(ReleaseInterface, bool(int interface_number));
87 MOCK_METHOD2(SetInterfaceAlternateSetting, 93 MOCK_METHOD2(SetInterfaceAlternateSetting,
88 bool(int interface_number, int alternate_setting)); 94 bool(int interface_number, int alternate_setting));
89 95
90 virtual scoped_refptr<UsbDevice> GetDevice() const override { 96 virtual scoped_refptr<UsbDevice> GetDevice() const override {
91 return device_; 97 return device_;
92 } 98 }
93 99
94 void set_device(UsbDevice* device) { device_ = device; } 100 void set_device(UsbDevice* device) { device_ = device; }
95 101
96 protected: 102 protected:
97 UsbDevice* device_; 103 UsbDevice* device_;
98 104
99 virtual ~MockUsbDeviceHandle() {} 105 virtual ~MockUsbDeviceHandle() {}
100 }; 106 };
101 107
102 class MockUsbDevice : public UsbDevice { 108 class MockUsbDevice : public UsbDevice {
103 public: 109 public:
104 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) 110 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id)
105 : UsbDevice(0, 0, 0), mock_handle_(mock_handle) { 111 : UsbDevice(vendor_id, product_id, unique_id) {}
106 mock_handle->set_device(this);
107 }
108 112
109 virtual scoped_refptr<UsbDeviceHandle> Open() override { 113 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&));
110 return mock_handle_; 114 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>());
111 } 115 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>));
112 116 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*());
113 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) override { 117 MOCK_METHOD1(GetManufacturer, bool(base::string16*));
114 EXPECT_TRUE(false) << "Should not be reached"; 118 MOCK_METHOD1(GetProduct, bool(base::string16*));
115 return false; 119 MOCK_METHOD1(GetSerialNumber, bool(base::string16*));
116 }
117
118 #if defined(OS_CHROMEOS)
119 virtual void RequestUsbAccess(
120 int interface_id,
121 const base::Callback<void(bool success)>& callback) override {
122 BrowserThread::PostTask(
123 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true));
124 }
125 #endif // OS_CHROMEOS
126
127 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&());
128 MOCK_METHOD1(GetManufacturer, bool(base::string16* manufacturer));
129 MOCK_METHOD1(GetProduct, bool(base::string16* product));
130 MOCK_METHOD1(GetSerialNumber, bool(base::string16* serial_number));
131 120
132 private: 121 private:
133 MockUsbDeviceHandle* mock_handle_;
134 virtual ~MockUsbDevice() {} 122 virtual ~MockUsbDevice() {}
135 }; 123 };
136 124
137 class MockUsbService : public UsbService { 125 class MockUsbService : public UsbService {
138 public: 126 public:
139 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} 127 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
140 128
141 protected: 129 protected:
142 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { 130 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override {
143 EXPECT_EQ(unique_id, 0U); 131 EXPECT_EQ(unique_id, 0U);
144 return device_; 132 return device_;
145 } 133 }
146 134
147 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { 135 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override {
148 STLClearObject(devices); 136 STLClearObject(devices);
149 devices->push_back(device_); 137 devices->push_back(device_);
150 } 138 }
151 139
152 scoped_refptr<UsbDevice> device_; 140 scoped_refptr<UsbDevice> device_;
153 }; 141 };
154 142
155 class UsbApiTest : public ShellApiTest { 143 class UsbApiTest : public ShellApiTest {
156 public: 144 public:
157 void SetUpOnMainThread() override { 145 void SetUpOnMainThread() override {
158 ShellApiTest::SetUpOnMainThread(); 146 ShellApiTest::SetUpOnMainThread();
159 mock_device_handle_ = new MockUsbDeviceHandle(); 147 mock_device_handle_ = new MockUsbDeviceHandle();
160 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); 148 mock_device_ = new MockUsbDevice(0, 0, 0);
161 scoped_refptr<content::MessageLoopRunner> runner = 149 mock_device_handle_->set_device(mock_device_.get());
162 new content::MessageLoopRunner; 150 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _))
163 BrowserThread::PostTaskAndReply(BrowserThread::FILE, 151 .WillRepeatedly(Invoke(RequestUsbAccess));
164 FROM_HERE, 152 EXPECT_CALL(*mock_device_.get(), Open())
153 .WillRepeatedly(Return(mock_device_handle_));
154
155 base::RunLoop run_loop;
156 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
165 base::Bind(&UsbApiTest::SetUpService, this), 157 base::Bind(&UsbApiTest::SetUpService, this),
166 runner->QuitClosure()); 158 run_loop.QuitClosure());
167 runner->Run(); 159 run_loop.Run();
168 } 160 }
169 161
170 void SetUpService() { 162 void SetUpService() {
171 UsbService::SetInstanceForTest(new MockUsbService(mock_device_)); 163 UsbService::SetInstanceForTest(new MockUsbService(mock_device_));
172 } 164 }
173 165
174 void TearDownOnMainThread() override { 166 void TearDownOnMainThread() override {
175 scoped_refptr<content::MessageLoopRunner> runner =
176 new content::MessageLoopRunner;
177 UsbService* service = NULL; 167 UsbService* service = NULL;
168 base::RunLoop run_loop;
178 BrowserThread::PostTaskAndReply( 169 BrowserThread::PostTaskAndReply(
179 BrowserThread::FILE, 170 BrowserThread::FILE, FROM_HERE,
180 FROM_HERE,
181 base::Bind(&UsbService::SetInstanceForTest, service), 171 base::Bind(&UsbService::SetInstanceForTest, service),
182 runner->QuitClosure()); 172 run_loop.QuitClosure());
183 runner->Run(); 173 run_loop.Run();
184 } 174 }
185 175
186 protected: 176 protected:
187 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 177 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
188 scoped_refptr<MockUsbDevice> mock_device_; 178 scoped_refptr<MockUsbDevice> mock_device_;
189 }; 179 };
190 180
191 } // namespace 181 } // namespace
192 182
193 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 183 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
194 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); 184 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4);
195 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); 185 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
196 } 186 }
197 187
198 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 188 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
199 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 189 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
200 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 190 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice())
201 .WillOnce(Return(true)) 191 .WillOnce(Return(true))
202 .WillOnce(Return(false)); 192 .WillOnce(Return(false));
203 EXPECT_CALL(*mock_device_handle_.get(), 193 EXPECT_CALL(*mock_device_handle_.get(),
204 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 194 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
205 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 195 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
206 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device")); 196 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
207 } 197 }
208 198
199 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {
200 UsbConfigDescriptor config_descriptor;
201 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1))
202 .WillOnce(Return(true));
203 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
204 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
205 .WillOnce(Return(nullptr))
206 .WillOnce(Return(&config_descriptor));
207 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration"));
208 }
209
209 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 210 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
210 UsbConfigDescriptor config_descriptor; 211 UsbConfigDescriptor config_descriptor;
211 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 212 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
212 EXPECT_CALL(*mock_device_.get(), GetConfiguration()) 213 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
213 .WillOnce(ReturnRef(config_descriptor)); 214 .WillOnce(Return(&config_descriptor));
214 ASSERT_TRUE(RunAppTest("api_test/usb/list_interfaces")); 215 ASSERT_TRUE(RunAppTest("api_test/usb/list_interfaces"));
215 } 216 }
216 217
217 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { 218 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
218 EXPECT_CALL(*mock_device_handle_.get(), 219 EXPECT_CALL(*mock_device_handle_.get(),
219 ControlTransfer(device::USB_DIRECTION_OUTBOUND, 220 ControlTransfer(device::USB_DIRECTION_OUTBOUND,
220 UsbDeviceHandle::STANDARD, 221 UsbDeviceHandle::STANDARD,
221 UsbDeviceHandle::DEVICE, 222 UsbDeviceHandle::DEVICE,
222 1, 223 1,
223 2, 224 2,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 257 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
257 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure")); 258 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure"));
258 } 259 }
259 260
260 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { 261 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
261 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 262 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
262 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer")); 263 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer"));
263 } 264 }
264 265
265 } // namespace extensions 266 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.cc ('k') | extensions/browser/extension_function_histogram_value.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698