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

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

Issue 1658953003: Revert of 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: 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
10 #include <map> 9 #include <map>
11 #include <numeric>
12 #include <queue> 10 #include <queue>
13 #include <set> 11 #include <set>
14 #include <string>
15 #include <utility> 12 #include <utility>
16 #include <vector> 13 #include <vector>
17 14
18 #include "base/bind.h" 15 #include "base/bind.h"
19 #include "base/macros.h" 16 #include "base/macros.h"
20 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h" 18 #include "base/run_loop.h"
22 #include "base/stl_util.h" 19 #include "base/stl_util.h"
23 #include "device/devices_app/usb/fake_permission_provider.h" 20 #include "device/devices_app/usb/fake_permission_provider.h"
24 #include "device/usb/mock_usb_device.h" 21 #include "device/usb/mock_usb_device.h"
25 #include "device/usb/mock_usb_device_handle.h" 22 #include "device/usb/mock_usb_device_handle.h"
26 #include "mojo/public/cpp/bindings/interface_request.h" 23 #include "mojo/public/cpp/bindings/interface_request.h"
27 #include "net/base/io_buffer.h" 24 #include "net/base/io_buffer.h"
28 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
29 26
30 using ::testing::Invoke; 27 using ::testing::Invoke;
31 using ::testing::_; 28 using ::testing::_;
32 29
33 namespace device { 30 namespace device {
34 namespace usb { 31 namespace usb {
35 32
36 namespace { 33 namespace {
37 34
38 class ConfigBuilder { 35 class ConfigBuilder {
39 public: 36 public:
40 explicit ConfigBuilder(uint8_t value) { config_.configuration_value = value; } 37 ConfigBuilder(uint8_t value) { config_.configuration_value = value; }
41 38
42 ConfigBuilder& AddInterface(uint8_t interface_number, 39 ConfigBuilder& AddInterface(uint8_t interface_number,
43 uint8_t alternate_setting, 40 uint8_t alternate_setting,
44 uint8_t class_code, 41 uint8_t class_code,
45 uint8_t subclass_code, 42 uint8_t subclass_code,
46 uint8_t protocol_code) { 43 uint8_t protocol_code) {
47 UsbInterfaceDescriptor interface; 44 UsbInterfaceDescriptor interface;
48 interface.interface_number = interface_number; 45 interface.interface_number = interface_number;
49 interface.alternate_setting = alternate_setting; 46 interface.alternate_setting = alternate_setting;
50 interface.interface_class = class_code; 47 interface.interface_class = class_code;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 mojo::Array<uint8_t> actual_bytes) { 95 mojo::Array<uint8_t> actual_bytes) {
99 EXPECT_EQ(expected_status, actual_status); 96 EXPECT_EQ(expected_status, actual_status);
100 ASSERT_EQ(expected_bytes.size(), actual_bytes.size()); 97 ASSERT_EQ(expected_bytes.size(), actual_bytes.size());
101 for (size_t i = 0; i < actual_bytes.size(); ++i) { 98 for (size_t i = 0; i < actual_bytes.size(); ++i) {
102 EXPECT_EQ(expected_bytes[i], actual_bytes[i]) 99 EXPECT_EQ(expected_bytes[i], actual_bytes[i])
103 << "Contents differ at index: " << i; 100 << "Contents differ at index: " << i;
104 } 101 }
105 continuation.Run(); 102 continuation.Run();
106 } 103 }
107 104
108 void ExpectPacketsOutAndThen(const std::vector<uint32_t>& expected_packets, 105 void ExpectPacketsAndThen(
109 const base::Closure& continuation, 106 TransferStatus expected_status,
110 mojo::Array<IsochronousPacketPtr> actual_packets) { 107 const std::vector<std::vector<uint8_t>>& expected_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);
111 ASSERT_EQ(expected_packets.size(), actual_packets.size()); 112 ASSERT_EQ(expected_packets.size(), actual_packets.size());
112 for (size_t i = 0; i < expected_packets.size(); ++i) { 113 for (size_t i = 0; i < expected_packets.size(); ++i) {
113 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length) 114 EXPECT_EQ(expected_packets[i].size(), actual_packets[i].size())
114 << "Packet lengths differ at index: " << i; 115 << "Packet sizes differ at index: " << i;
115 EXPECT_EQ(TransferStatus::COMPLETED, actual_packets[i]->status) 116 for (size_t j = 0; j < expected_packets[i].size(); ++j) {
116 << "Packet at index " << i << " not completed."; 117 EXPECT_EQ(expected_packets[i][j], actual_packets[i][j])
118 << "Contents of packet " << i << " differ at index " << j;
119 }
117 } 120 }
118 continuation.Run(); 121 continuation.Run();
119 } 122 }
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 }
140 123
141 void ExpectTransferStatusAndThen(TransferStatus expected_status, 124 void ExpectTransferStatusAndThen(TransferStatus expected_status,
142 const base::Closure& continuation, 125 const base::Closure& continuation,
143 TransferStatus actual_status) { 126 TransferStatus actual_status) {
144 EXPECT_EQ(expected_status, actual_status); 127 EXPECT_EQ(expected_status, actual_status);
145 continuation.Run(); 128 continuation.Run();
146 } 129 }
147 130
148 class USBDeviceImplTest : public testing::Test { 131 class USBDeviceImplTest : public testing::Test {
149 public: 132 public:
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface)); 179 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface));
197 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _)) 180 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _))
198 .WillByDefault( 181 .WillByDefault(
199 Invoke(this, &USBDeviceImplTest::SetInterfaceAlternateSetting)); 182 Invoke(this, &USBDeviceImplTest::SetInterfaceAlternateSetting));
200 ON_CALL(mock_handle(), ResetDevice(_)) 183 ON_CALL(mock_handle(), ResetDevice(_))
201 .WillByDefault(Invoke(this, &USBDeviceImplTest::ResetDevice)); 184 .WillByDefault(Invoke(this, &USBDeviceImplTest::ResetDevice));
202 ON_CALL(mock_handle(), ControlTransfer(_, _, _, _, _, _, _, _, _, _)) 185 ON_CALL(mock_handle(), ControlTransfer(_, _, _, _, _, _, _, _, _, _))
203 .WillByDefault(Invoke(this, &USBDeviceImplTest::ControlTransfer)); 186 .WillByDefault(Invoke(this, &USBDeviceImplTest::ControlTransfer));
204 ON_CALL(mock_handle(), GenericTransfer(_, _, _, _, _, _)) 187 ON_CALL(mock_handle(), GenericTransfer(_, _, _, _, _, _))
205 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer)); 188 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer));
206 ON_CALL(mock_handle(), IsochronousTransferIn(_, _, _, _)) 189 ON_CALL(mock_handle(), IsochronousTransfer(_, _, _, _, _, _, _, _))
207 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransferIn)); 190 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransfer));
208 ON_CALL(mock_handle(), IsochronousTransferOut(_, _, _, _, _))
209 .WillByDefault(
210 Invoke(this, &USBDeviceImplTest::IsochronousTransferOut));
211 191
212 return proxy; 192 return proxy;
213 } 193 }
214 194
215 DevicePtr GetMockDeviceProxy() { 195 DevicePtr GetMockDeviceProxy() {
216 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); 196 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF");
217 } 197 }
218 198
219 void AddMockConfig(const ConfigBuilder& builder) { 199 void AddMockConfig(const ConfigBuilder& builder) {
220 const UsbConfigDescriptor& config = builder.config(); 200 const UsbConfigDescriptor& config = builder.config();
221 DCHECK(!ContainsKey(mock_configs_, config.configuration_value)); 201 DCHECK(!ContainsKey(mock_configs_, config.configuration_value));
222 mock_configs_[config.configuration_value] = config; 202 mock_configs_[config.configuration_value] = config;
223 } 203 }
224 204
225 void AddMockInboundData(const std::vector<uint8_t>& data) { 205 void AddMockInboundData(const std::vector<uint8_t>& data) {
226 mock_inbound_data_.push(data); 206 mock_inbound_data_.push(data);
227 } 207 }
228 208
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
236 void AddMockOutboundData(const std::vector<uint8_t>& data) { 209 void AddMockOutboundData(const std::vector<uint8_t>& data) {
237 mock_outbound_data_.push(data); 210 mock_outbound_data_.push(data);
238 } 211 }
239 212
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
247 private: 213 private:
248 void OpenMockHandle(const UsbDevice::OpenCallback& callback) { 214 void OpenMockHandle(const UsbDevice::OpenCallback& callback) {
249 EXPECT_FALSE(is_device_open_); 215 EXPECT_FALSE(is_device_open_);
250 is_device_open_ = true; 216 is_device_open_ = true;
251 callback.Run(mock_handle_); 217 callback.Run(mock_handle_);
252 } 218 }
253 219
254 void CloseMockHandle() { 220 void CloseMockHandle() {
255 EXPECT_TRUE(is_device_open_); 221 EXPECT_TRUE(is_device_open_);
256 is_device_open_ = false; 222 is_device_open_ = false;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 scoped_refptr<net::IOBuffer> buffer, 329 scoped_refptr<net::IOBuffer> buffer,
364 size_t length, 330 size_t length,
365 unsigned int timeout, 331 unsigned int timeout,
366 const UsbDeviceHandle::TransferCallback& callback) { 332 const UsbDeviceHandle::TransferCallback& callback) {
367 if (direction == USB_DIRECTION_INBOUND) 333 if (direction == USB_DIRECTION_INBOUND)
368 InboundTransfer(callback); 334 InboundTransfer(callback);
369 else 335 else
370 OutboundTransfer(buffer, length, callback); 336 OutboundTransfer(buffer, length, callback);
371 } 337 }
372 338
373 void IsochronousTransferIn( 339 void IsochronousTransfer(UsbEndpointDirection direction,
374 uint8_t endpoint_number, 340 uint8_t endpoint,
375 const std::vector<uint32_t>& packet_lengths, 341 scoped_refptr<net::IOBuffer> buffer,
376 unsigned int timeout, 342 size_t length,
377 const UsbDeviceHandle::IsochronousTransferCallback& callback) { 343 unsigned int packets,
378 ASSERT_FALSE(mock_inbound_data_.empty()); 344 unsigned int packet_length,
379 const std::vector<uint8_t>& bytes = mock_inbound_data_.front(); 345 unsigned int timeout,
380 size_t length = bytes.size(); 346 const UsbDeviceHandle::TransferCallback& callback) {
381 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(length); 347 if (direction == USB_DIRECTION_INBOUND)
382 std::copy(bytes.begin(), bytes.end(), buffer->data()); 348 InboundTransfer(callback);
383 mock_inbound_data_.pop(); 349 else
384 350 OutboundTransfer(buffer, length, callback);
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);
426 } 351 }
427 352
428 scoped_ptr<base::MessageLoop> message_loop_; 353 scoped_ptr<base::MessageLoop> message_loop_;
429 scoped_refptr<MockUsbDevice> mock_device_; 354 scoped_refptr<MockUsbDevice> mock_device_;
430 scoped_refptr<MockUsbDeviceHandle> mock_handle_; 355 scoped_refptr<MockUsbDeviceHandle> mock_handle_;
431 bool is_device_open_; 356 bool is_device_open_;
432 bool allow_reset_; 357 bool allow_reset_;
433 358
434 std::map<uint8_t, UsbConfigDescriptor> mock_configs_; 359 std::map<uint8_t, UsbConfigDescriptor> mock_configs_;
435 uint8_t current_config_; 360 uint8_t current_config_;
436 361
437 std::queue<std::vector<uint8_t>> mock_inbound_data_; 362 std::queue<std::vector<uint8_t>> mock_inbound_data_;
438 std::queue<std::vector<uint8_t>> mock_outbound_data_; 363 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_;
443 364
444 std::set<uint8_t> claimed_interfaces_; 365 std::set<uint8_t> claimed_interfaces_;
445 366
446 FakePermissionProvider permission_provider_; 367 FakePermissionProvider permission_provider_;
447 368
448 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); 369 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest);
449 }; 370 };
450 371
451 } // namespace 372 } // namespace
452 373
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 751
831 EXPECT_CALL(mock_device(), Open(_)); 752 EXPECT_CALL(mock_device(), Open(_));
832 753
833 { 754 {
834 base::RunLoop loop; 755 base::RunLoop loop;
835 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, 756 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK,
836 loop.QuitClosure())); 757 loop.QuitClosure()));
837 loop.Run(); 758 loop.Run();
838 } 759 }
839 760
840 std::vector<UsbDeviceHandle::IsochronousPacket> fake_packets(4); 761 std::string outbound_packet_data = "aaaaaaaabbbbbbbbccccccccdddddddd";
841 for (size_t i = 0; i < fake_packets.size(); ++i) { 762 std::vector<uint8_t> fake_outbound_packets(outbound_packet_data.size());
842 fake_packets[i].length = 8; 763 std::copy(outbound_packet_data.begin(), outbound_packet_data.end(),
843 fake_packets[i].transferred_length = 8; 764 fake_outbound_packets.begin());
844 fake_packets[i].status = USB_TRANSFER_COMPLETED;
845 }
846 std::vector<uint32_t> fake_packet_lengths(4, 8);
847 765
848 std::vector<uint32_t> expected_transferred_lengths(4, 8); 766 std::string inbound_packet_data = "ddddddddccccccccbbbbbbbbaaaaaaaa";
849 767 std::vector<uint8_t> fake_inbound_packets(inbound_packet_data.size());
850 std::string outbound_data = "aaaaaaaabbbbbbbbccccccccdddddddd"; 768 std::copy(inbound_packet_data.begin(), inbound_packet_data.end(),
851 std::vector<uint8_t> fake_outbound_data(outbound_data.size()); 769 fake_inbound_packets.begin());
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());
859 770
860 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); 771 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3));
861 AddMockOutboundPackets(fake_outbound_data, fake_packets); 772 AddMockOutboundData(fake_outbound_packets);
862 AddMockInboundPackets(fake_inbound_data, fake_packets); 773 AddMockInboundData(fake_inbound_packets);
863 774
864 EXPECT_CALL(mock_handle(), 775 EXPECT_CALL(mock_handle(),
865 IsochronousTransferOut(0x01, _, fake_packet_lengths, 0, _)); 776 IsochronousTransfer(USB_DIRECTION_OUTBOUND, 0x01, _,
777 fake_outbound_packets.size(), 4, 8, 0, _));
866 778
867 { 779 {
868 base::RunLoop loop; 780 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 }
869 device->IsochronousTransferOut( 789 device->IsochronousTransferOut(
870 1, mojo::Array<uint8_t>::From(fake_outbound_data), 790 1, std::move(packets), 0,
871 mojo::Array<uint32_t>::From(fake_packet_lengths), 0, 791 base::Bind(&ExpectTransferStatusAndThen, TransferStatus::COMPLETED,
872 base::Bind(&ExpectPacketsOutAndThen, expected_transferred_lengths,
873 loop.QuitClosure())); 792 loop.QuitClosure()));
874 loop.Run(); 793 loop.Run();
875 } 794 }
876 795
877 EXPECT_CALL(mock_handle(), 796 EXPECT_CALL(mock_handle(),
878 IsochronousTransferIn(0x81, fake_packet_lengths, 0, _)); 797 IsochronousTransfer(USB_DIRECTION_INBOUND, 0x81, _,
798 fake_inbound_packets.size(), 4, 8, 0, _));
879 799
880 { 800 {
881 base::RunLoop loop; 801 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 }
882 device->IsochronousTransferIn( 808 device->IsochronousTransferIn(
883 1, mojo::Array<uint32_t>::From(fake_packet_lengths), 0, 809 1, 4, 8, 0, base::Bind(&ExpectPacketsAndThen, TransferStatus::COMPLETED,
884 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, 810 packets, loop.QuitClosure()));
885 expected_transferred_lengths, loop.QuitClosure()));
886 loop.Run(); 811 loop.Run();
887 } 812 }
888 813
889 EXPECT_CALL(mock_handle(), Close()); 814 EXPECT_CALL(mock_handle(), Close());
890 } 815 }
891 816
892 } // namespace usb 817 } // namespace usb
893 } // namespace device 818 } // 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