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

Side by Side Diff: device/devices_app/usb/device_impl_unittest.cc

Issue 1618393004: Update device/usb and its Mojo interface for variable size ISO packets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Document use of ErrorWithArguments. Created 4 years, 10 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "device/devices_app/usb/device_impl.h" 5 #include "device/devices_app/usb/device_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9
9 #include <map> 10 #include <map>
11 #include <numeric>
10 #include <queue> 12 #include <queue>
11 #include <set> 13 #include <set>
14 #include <string>
12 #include <utility> 15 #include <utility>
13 #include <vector> 16 #include <vector>
14 17
15 #include "base/bind.h" 18 #include "base/bind.h"
16 #include "base/macros.h" 19 #include "base/macros.h"
17 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
18 #include "base/run_loop.h" 21 #include "base/run_loop.h"
19 #include "base/stl_util.h" 22 #include "base/stl_util.h"
20 #include "device/devices_app/usb/fake_permission_provider.h" 23 #include "device/devices_app/usb/fake_permission_provider.h"
21 #include "device/usb/mock_usb_device.h" 24 #include "device/usb/mock_usb_device.h"
22 #include "device/usb/mock_usb_device_handle.h" 25 #include "device/usb/mock_usb_device_handle.h"
23 #include "mojo/public/cpp/bindings/interface_request.h" 26 #include "mojo/public/cpp/bindings/interface_request.h"
24 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
25 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
26 29
27 using ::testing::Invoke; 30 using ::testing::Invoke;
28 using ::testing::_; 31 using ::testing::_;
29 32
30 namespace device { 33 namespace device {
31 namespace usb { 34 namespace usb {
32 35
33 namespace { 36 namespace {
34 37
35 class ConfigBuilder { 38 class ConfigBuilder {
36 public: 39 public:
37 ConfigBuilder(uint8_t value) { config_.configuration_value = value; } 40 explicit ConfigBuilder(uint8_t value) { config_.configuration_value = value; }
38 41
39 ConfigBuilder& AddInterface(uint8_t interface_number, 42 ConfigBuilder& AddInterface(uint8_t interface_number,
40 uint8_t alternate_setting, 43 uint8_t alternate_setting,
41 uint8_t class_code, 44 uint8_t class_code,
42 uint8_t subclass_code, 45 uint8_t subclass_code,
43 uint8_t protocol_code) { 46 uint8_t protocol_code) {
44 UsbInterfaceDescriptor interface; 47 UsbInterfaceDescriptor interface;
45 interface.interface_number = interface_number; 48 interface.interface_number = interface_number;
46 interface.alternate_setting = alternate_setting; 49 interface.alternate_setting = alternate_setting;
47 interface.interface_class = class_code; 50 interface.interface_class = class_code;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 mojo::Array<uint8_t> actual_bytes) { 98 mojo::Array<uint8_t> actual_bytes) {
96 EXPECT_EQ(expected_status, actual_status); 99 EXPECT_EQ(expected_status, actual_status);
97 ASSERT_EQ(expected_bytes.size(), actual_bytes.size()); 100 ASSERT_EQ(expected_bytes.size(), actual_bytes.size());
98 for (size_t i = 0; i < actual_bytes.size(); ++i) { 101 for (size_t i = 0; i < actual_bytes.size(); ++i) {
99 EXPECT_EQ(expected_bytes[i], actual_bytes[i]) 102 EXPECT_EQ(expected_bytes[i], actual_bytes[i])
100 << "Contents differ at index: " << i; 103 << "Contents differ at index: " << i;
101 } 104 }
102 continuation.Run(); 105 continuation.Run();
103 } 106 }
104 107
105 void ExpectPacketsAndThen( 108 void ExpectPacketsOutAndThen(const std::vector<uint32_t>& expected_packets,
106 TransferStatus expected_status, 109 const base::Closure& continuation,
107 const std::vector<std::vector<uint8_t>>& expected_packets, 110 mojo::Array<IsochronousPacketPtr> actual_packets) {
108 const base::Closure& continuation,
109 TransferStatus actual_status,
110 mojo::Array<mojo::Array<uint8_t>> actual_packets) {
111 EXPECT_EQ(expected_status, actual_status);
112 ASSERT_EQ(expected_packets.size(), actual_packets.size()); 111 ASSERT_EQ(expected_packets.size(), actual_packets.size());
113 for (size_t i = 0; i < expected_packets.size(); ++i) { 112 for (size_t i = 0; i < expected_packets.size(); ++i) {
114 EXPECT_EQ(expected_packets[i].size(), actual_packets[i].size()) 113 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length)
115 << "Packet sizes differ at index: " << i; 114 << "Packet lengths differ at index: " << i;
116 for (size_t j = 0; j < expected_packets[i].size(); ++j) { 115 EXPECT_EQ(TransferStatus::COMPLETED, actual_packets[i]->status)
117 EXPECT_EQ(expected_packets[i][j], actual_packets[i][j]) 116 << "Packet at index " << i << " not completed.";
118 << "Contents of packet " << i << " differ at index " << j;
119 }
120 } 117 }
121 continuation.Run(); 118 continuation.Run();
122 } 119 }
120
121 void ExpectPacketsInAndThen(const std::vector<uint8_t>& expected_bytes,
122 const std::vector<uint32_t>& expected_packets,
123 const base::Closure& continuation,
124 mojo::Array<uint8_t> actual_bytes,
125 mojo::Array<IsochronousPacketPtr> actual_packets) {
126 ASSERT_EQ(expected_packets.size(), actual_packets.size());
127 for (size_t i = 0; i < expected_packets.size(); ++i) {
128 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length)
129 << "Packet lengths differ at index: " << i;
130 EXPECT_EQ(TransferStatus::COMPLETED, actual_packets[i]->status)
131 << "Packet at index " << i << " not completed.";
132 }
133 ASSERT_EQ(expected_bytes.size(), actual_bytes.size());
134 for (size_t i = 0; i < expected_bytes.size(); ++i) {
135 EXPECT_EQ(expected_bytes[i], actual_bytes[i])
136 << "Contents differ at index: " << i;
137 }
138 continuation.Run();
139 }
123 140
124 void ExpectTransferStatusAndThen(TransferStatus expected_status, 141 void ExpectTransferStatusAndThen(TransferStatus expected_status,
125 const base::Closure& continuation, 142 const base::Closure& continuation,
126 TransferStatus actual_status) { 143 TransferStatus actual_status) {
127 EXPECT_EQ(expected_status, actual_status); 144 EXPECT_EQ(expected_status, actual_status);
128 continuation.Run(); 145 continuation.Run();
129 } 146 }
130 147
131 class USBDeviceImplTest : public testing::Test { 148 class USBDeviceImplTest : public testing::Test {
132 public: 149 public:
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface)); 196 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface));
180 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _)) 197 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _))
181 .WillByDefault( 198 .WillByDefault(
182 Invoke(this, &USBDeviceImplTest::SetInterfaceAlternateSetting)); 199 Invoke(this, &USBDeviceImplTest::SetInterfaceAlternateSetting));
183 ON_CALL(mock_handle(), ResetDevice(_)) 200 ON_CALL(mock_handle(), ResetDevice(_))
184 .WillByDefault(Invoke(this, &USBDeviceImplTest::ResetDevice)); 201 .WillByDefault(Invoke(this, &USBDeviceImplTest::ResetDevice));
185 ON_CALL(mock_handle(), ControlTransfer(_, _, _, _, _, _, _, _, _, _)) 202 ON_CALL(mock_handle(), ControlTransfer(_, _, _, _, _, _, _, _, _, _))
186 .WillByDefault(Invoke(this, &USBDeviceImplTest::ControlTransfer)); 203 .WillByDefault(Invoke(this, &USBDeviceImplTest::ControlTransfer));
187 ON_CALL(mock_handle(), GenericTransfer(_, _, _, _, _, _)) 204 ON_CALL(mock_handle(), GenericTransfer(_, _, _, _, _, _))
188 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer)); 205 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer));
189 ON_CALL(mock_handle(), IsochronousTransfer(_, _, _, _, _, _, _, _)) 206 ON_CALL(mock_handle(), IsochronousTransferIn(_, _, _, _))
190 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransfer)); 207 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransferIn));
208 ON_CALL(mock_handle(), IsochronousTransferOut(_, _, _, _, _))
209 .WillByDefault(
210 Invoke(this, &USBDeviceImplTest::IsochronousTransferOut));
191 211
192 return proxy; 212 return proxy;
193 } 213 }
194 214
195 DevicePtr GetMockDeviceProxy() { 215 DevicePtr GetMockDeviceProxy() {
196 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); 216 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF");
197 } 217 }
198 218
199 void AddMockConfig(const ConfigBuilder& builder) { 219 void AddMockConfig(const ConfigBuilder& builder) {
200 const UsbConfigDescriptor& config = builder.config(); 220 const UsbConfigDescriptor& config = builder.config();
201 DCHECK(!ContainsKey(mock_configs_, config.configuration_value)); 221 DCHECK(!ContainsKey(mock_configs_, config.configuration_value));
202 mock_configs_[config.configuration_value] = config; 222 mock_configs_[config.configuration_value] = config;
203 } 223 }
204 224
205 void AddMockInboundData(const std::vector<uint8_t>& data) { 225 void AddMockInboundData(const std::vector<uint8_t>& data) {
206 mock_inbound_data_.push(data); 226 mock_inbound_data_.push(data);
207 } 227 }
208 228
229 void AddMockInboundPackets(
230 const std::vector<uint8_t>& data,
231 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) {
232 mock_inbound_data_.push(data);
233 mock_inbound_packets_.push(packets);
234 }
235
209 void AddMockOutboundData(const std::vector<uint8_t>& data) { 236 void AddMockOutboundData(const std::vector<uint8_t>& data) {
210 mock_outbound_data_.push(data); 237 mock_outbound_data_.push(data);
211 } 238 }
212 239
240 void AddMockOutboundPackets(
241 const std::vector<uint8_t>& data,
242 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) {
243 mock_outbound_data_.push(data);
244 mock_outbound_packets_.push(packets);
245 }
246
213 private: 247 private:
214 void OpenMockHandle(const UsbDevice::OpenCallback& callback) { 248 void OpenMockHandle(const UsbDevice::OpenCallback& callback) {
215 EXPECT_FALSE(is_device_open_); 249 EXPECT_FALSE(is_device_open_);
216 is_device_open_ = true; 250 is_device_open_ = true;
217 callback.Run(mock_handle_); 251 callback.Run(mock_handle_);
218 } 252 }
219 253
220 void CloseMockHandle() { 254 void CloseMockHandle() {
221 EXPECT_TRUE(is_device_open_); 255 EXPECT_TRUE(is_device_open_);
222 is_device_open_ = false; 256 is_device_open_ = false;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 scoped_refptr<net::IOBuffer> buffer, 363 scoped_refptr<net::IOBuffer> buffer,
330 size_t length, 364 size_t length,
331 unsigned int timeout, 365 unsigned int timeout,
332 const UsbDeviceHandle::TransferCallback& callback) { 366 const UsbDeviceHandle::TransferCallback& callback) {
333 if (direction == USB_DIRECTION_INBOUND) 367 if (direction == USB_DIRECTION_INBOUND)
334 InboundTransfer(callback); 368 InboundTransfer(callback);
335 else 369 else
336 OutboundTransfer(buffer, length, callback); 370 OutboundTransfer(buffer, length, callback);
337 } 371 }
338 372
339 void IsochronousTransfer(UsbEndpointDirection direction, 373 void IsochronousTransferIn(
340 uint8_t endpoint, 374 uint8_t endpoint_number,
341 scoped_refptr<net::IOBuffer> buffer, 375 const std::vector<uint32_t>& packet_lengths,
342 size_t length, 376 unsigned int timeout,
343 unsigned int packets, 377 const UsbDeviceHandle::IsochronousTransferCallback& callback) {
344 unsigned int packet_length, 378 ASSERT_FALSE(mock_inbound_data_.empty());
345 unsigned int timeout, 379 const std::vector<uint8_t>& bytes = mock_inbound_data_.front();
346 const UsbDeviceHandle::TransferCallback& callback) { 380 size_t length = bytes.size();
347 if (direction == USB_DIRECTION_INBOUND) 381 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(length);
348 InboundTransfer(callback); 382 std::copy(bytes.begin(), bytes.end(), buffer->data());
349 else 383 mock_inbound_data_.pop();
350 OutboundTransfer(buffer, length, callback); 384
385 ASSERT_FALSE(mock_inbound_packets_.empty());
386 std::vector<UsbDeviceHandle::IsochronousPacket> packets =
387 mock_inbound_packets_.front();
388 ASSERT_EQ(packets.size(), packet_lengths.size());
389 for (size_t i = 0; i < packets.size(); ++i) {
390 EXPECT_EQ(packets[i].length, packet_lengths[i])
391 << "Packet lengths differ at index: " << i;
392 }
393 mock_inbound_packets_.pop();
394
395 callback.Run(buffer, packets);
396 }
397
398 void IsochronousTransferOut(
399 uint8_t endpoint_number,
400 scoped_refptr<net::IOBuffer> buffer,
401 const std::vector<uint32_t>& packet_lengths,
402 unsigned int timeout,
403 const UsbDeviceHandle::IsochronousTransferCallback& callback) {
404 ASSERT_FALSE(mock_outbound_data_.empty());
405 const std::vector<uint8_t>& bytes = mock_outbound_data_.front();
406 size_t length =
407 std::accumulate(packet_lengths.begin(), packet_lengths.end(), 0u);
408 ASSERT_EQ(bytes.size(), length);
409 for (size_t i = 0; i < length; ++i) {
410 EXPECT_EQ(bytes[i], buffer->data()[i]) << "Contents differ at index: "
411 << i;
412 }
413 mock_outbound_data_.pop();
414
415 ASSERT_FALSE(mock_outbound_packets_.empty());
416 std::vector<UsbDeviceHandle::IsochronousPacket> packets =
417 mock_outbound_packets_.front();
418 ASSERT_EQ(packets.size(), packet_lengths.size());
419 for (size_t i = 0; i < packets.size(); ++i) {
420 EXPECT_EQ(packets[i].length, packet_lengths[i])
421 << "Packet lengths differ at index: " << i;
422 }
423 mock_outbound_packets_.pop();
424
425 callback.Run(buffer, packets);
351 } 426 }
352 427
353 scoped_ptr<base::MessageLoop> message_loop_; 428 scoped_ptr<base::MessageLoop> message_loop_;
354 scoped_refptr<MockUsbDevice> mock_device_; 429 scoped_refptr<MockUsbDevice> mock_device_;
355 scoped_refptr<MockUsbDeviceHandle> mock_handle_; 430 scoped_refptr<MockUsbDeviceHandle> mock_handle_;
356 bool is_device_open_; 431 bool is_device_open_;
357 bool allow_reset_; 432 bool allow_reset_;
358 433
359 std::map<uint8_t, UsbConfigDescriptor> mock_configs_; 434 std::map<uint8_t, UsbConfigDescriptor> mock_configs_;
360 uint8_t current_config_; 435 uint8_t current_config_;
361 436
362 std::queue<std::vector<uint8_t>> mock_inbound_data_; 437 std::queue<std::vector<uint8_t>> mock_inbound_data_;
363 std::queue<std::vector<uint8_t>> mock_outbound_data_; 438 std::queue<std::vector<uint8_t>> mock_outbound_data_;
439 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>>
440 mock_inbound_packets_;
441 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>>
442 mock_outbound_packets_;
364 443
365 std::set<uint8_t> claimed_interfaces_; 444 std::set<uint8_t> claimed_interfaces_;
366 445
367 FakePermissionProvider permission_provider_; 446 FakePermissionProvider permission_provider_;
368 447
369 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); 448 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest);
370 }; 449 };
371 450
372 } // namespace 451 } // namespace
373 452
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 830
752 EXPECT_CALL(mock_device(), Open(_)); 831 EXPECT_CALL(mock_device(), Open(_));
753 832
754 { 833 {
755 base::RunLoop loop; 834 base::RunLoop loop;
756 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, 835 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK,
757 loop.QuitClosure())); 836 loop.QuitClosure()));
758 loop.Run(); 837 loop.Run();
759 } 838 }
760 839
761 std::string outbound_packet_data = "aaaaaaaabbbbbbbbccccccccdddddddd"; 840 std::vector<UsbDeviceHandle::IsochronousPacket> fake_packets(4);
762 std::vector<uint8_t> fake_outbound_packets(outbound_packet_data.size()); 841 for (size_t i = 0; i < fake_packets.size(); ++i) {
763 std::copy(outbound_packet_data.begin(), outbound_packet_data.end(), 842 fake_packets[i].length = 8;
764 fake_outbound_packets.begin()); 843 fake_packets[i].transferred_length = 8;
844 fake_packets[i].status = USB_TRANSFER_COMPLETED;
845 }
846 std::vector<uint32_t> fake_packet_lengths(4, 8);
765 847
766 std::string inbound_packet_data = "ddddddddccccccccbbbbbbbbaaaaaaaa"; 848 std::vector<uint32_t> expected_transferred_lengths(4, 8);
767 std::vector<uint8_t> fake_inbound_packets(inbound_packet_data.size()); 849
768 std::copy(inbound_packet_data.begin(), inbound_packet_data.end(), 850 std::string outbound_data = "aaaaaaaabbbbbbbbccccccccdddddddd";
769 fake_inbound_packets.begin()); 851 std::vector<uint8_t> fake_outbound_data(outbound_data.size());
852 std::copy(outbound_data.begin(), outbound_data.end(),
853 fake_outbound_data.begin());
854
855 std::string inbound_data = "ddddddddccccccccbbbbbbbbaaaaaaaa";
856 std::vector<uint8_t> fake_inbound_data(inbound_data.size());
857 std::copy(inbound_data.begin(), inbound_data.end(),
858 fake_inbound_data.begin());
770 859
771 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); 860 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3));
772 AddMockOutboundData(fake_outbound_packets); 861 AddMockOutboundPackets(fake_outbound_data, fake_packets);
773 AddMockInboundData(fake_inbound_packets); 862 AddMockInboundPackets(fake_inbound_data, fake_packets);
774 863
775 EXPECT_CALL(mock_handle(), 864 EXPECT_CALL(mock_handle(),
776 IsochronousTransfer(USB_DIRECTION_OUTBOUND, 0x01, _, 865 IsochronousTransferOut(0x01, _, fake_packet_lengths, 0, _));
777 fake_outbound_packets.size(), 4, 8, 0, _));
778 866
779 { 867 {
780 base::RunLoop loop; 868 base::RunLoop loop;
781 mojo::Array<mojo::Array<uint8_t>> packets =
782 mojo::Array<mojo::Array<uint8_t>>::New(4);
783 for (size_t i = 0; i < 4; ++i) {
784 std::vector<uint8_t> bytes(8);
785 std::copy(outbound_packet_data.begin() + i * 8,
786 outbound_packet_data.begin() + i * 8 + 8, bytes.begin());
787 packets[i].Swap(&bytes);
788 }
789 device->IsochronousTransferOut( 869 device->IsochronousTransferOut(
790 1, std::move(packets), 0, 870 1, mojo::Array<uint8_t>::From(fake_outbound_data),
791 base::Bind(&ExpectTransferStatusAndThen, TransferStatus::COMPLETED, 871 mojo::Array<uint32_t>::From(fake_packet_lengths), 0,
872 base::Bind(&ExpectPacketsOutAndThen, expected_transferred_lengths,
792 loop.QuitClosure())); 873 loop.QuitClosure()));
793 loop.Run(); 874 loop.Run();
794 } 875 }
795 876
796 EXPECT_CALL(mock_handle(), 877 EXPECT_CALL(mock_handle(),
797 IsochronousTransfer(USB_DIRECTION_INBOUND, 0x81, _, 878 IsochronousTransferIn(0x81, fake_packet_lengths, 0, _));
798 fake_inbound_packets.size(), 4, 8, 0, _));
799 879
800 { 880 {
801 base::RunLoop loop; 881 base::RunLoop loop;
802 std::vector<std::vector<uint8_t>> packets(4);
803 for (size_t i = 0; i < 4; ++i) {
804 packets[i].resize(8);
805 std::copy(inbound_packet_data.begin() + i * 8,
806 inbound_packet_data.begin() + i * 8 + 8, packets[i].begin());
807 }
808 device->IsochronousTransferIn( 882 device->IsochronousTransferIn(
809 1, 4, 8, 0, base::Bind(&ExpectPacketsAndThen, TransferStatus::COMPLETED, 883 1, mojo::Array<uint32_t>::From(fake_packet_lengths), 0,
810 packets, loop.QuitClosure())); 884 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data,
885 expected_transferred_lengths, loop.QuitClosure()));
811 loop.Run(); 886 loop.Run();
812 } 887 }
813 888
814 EXPECT_CALL(mock_handle(), Close()); 889 EXPECT_CALL(mock_handle(), Close());
815 } 890 }
816 891
817 } // namespace usb 892 } // namespace usb
818 } // namespace device 893 } // namespace device
OLDNEW
« no previous file with comments | « device/devices_app/usb/device_impl.cc ('k') | device/devices_app/usb/public/interfaces/device.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698