OLD | NEW |
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/usb/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "device/usb/mojo/type_converters.h" | 27 #include "device/usb/mojo/type_converters.h" |
28 #include "mojo/public/cpp/bindings/interface_request.h" | 28 #include "mojo/public/cpp/bindings/interface_request.h" |
29 #include "mojo/public/cpp/bindings/strong_binding.h" | 29 #include "mojo/public/cpp/bindings/strong_binding.h" |
30 #include "net/base/io_buffer.h" | 30 #include "net/base/io_buffer.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 | 32 |
33 using ::testing::Invoke; | 33 using ::testing::Invoke; |
34 using ::testing::_; | 34 using ::testing::_; |
35 | 35 |
36 namespace device { | 36 namespace device { |
| 37 |
| 38 using mojom::UsbIsochronousPacketPtr; |
| 39 using mojom::UsbControlTransferRecipient; |
| 40 using mojom::UsbControlTransferType; |
| 41 using mojom::UsbDevicePtr; |
| 42 |
37 namespace usb { | 43 namespace usb { |
38 | 44 |
39 namespace { | 45 namespace { |
40 | 46 |
41 class ConfigBuilder { | 47 class ConfigBuilder { |
42 public: | 48 public: |
43 explicit ConfigBuilder(uint8_t value) : config_(value, false, false, 0) {} | 49 explicit ConfigBuilder(uint8_t value) : config_(value, false, false, 0) {} |
44 | 50 |
45 ConfigBuilder& AddInterface(uint8_t interface_number, | 51 ConfigBuilder& AddInterface(uint8_t interface_number, |
46 uint8_t alternate_setting, | 52 uint8_t alternate_setting, |
47 uint8_t class_code, | 53 uint8_t class_code, |
48 uint8_t subclass_code, | 54 uint8_t subclass_code, |
49 uint8_t protocol_code) { | 55 uint8_t protocol_code) { |
50 config_.interfaces.emplace_back(interface_number, alternate_setting, | 56 config_.interfaces.emplace_back(interface_number, alternate_setting, |
51 class_code, subclass_code, protocol_code); | 57 class_code, subclass_code, protocol_code); |
52 return *this; | 58 return *this; |
53 } | 59 } |
54 | 60 |
55 const UsbConfigDescriptor& config() const { return config_; } | 61 const UsbConfigDescriptor& config() const { return config_; } |
56 | 62 |
57 private: | 63 private: |
58 UsbConfigDescriptor config_; | 64 UsbConfigDescriptor config_; |
59 }; | 65 }; |
60 | 66 |
61 void ExpectOpenAndThen(OpenDeviceError expected, | 67 void ExpectOpenAndThen(mojom::UsbOpenDeviceError expected, |
62 const base::Closure& continuation, | 68 const base::Closure& continuation, |
63 OpenDeviceError error) { | 69 mojom::UsbOpenDeviceError error) { |
64 EXPECT_EQ(expected, error); | 70 EXPECT_EQ(expected, error); |
65 continuation.Run(); | 71 continuation.Run(); |
66 } | 72 } |
67 | 73 |
68 void ExpectResultAndThen(bool expected_result, | 74 void ExpectResultAndThen(bool expected_result, |
69 const base::Closure& continuation, | 75 const base::Closure& continuation, |
70 bool actual_result) { | 76 bool actual_result) { |
71 EXPECT_EQ(expected_result, actual_result); | 77 EXPECT_EQ(expected_result, actual_result); |
72 continuation.Run(); | 78 continuation.Run(); |
73 } | 79 } |
74 | 80 |
75 void ExpectTransferInAndThen( | 81 void ExpectTransferInAndThen( |
76 TransferStatus expected_status, | 82 mojom::UsbTransferStatus expected_status, |
77 const std::vector<uint8_t>& expected_bytes, | 83 const std::vector<uint8_t>& expected_bytes, |
78 const base::Closure& continuation, | 84 const base::Closure& continuation, |
79 TransferStatus actual_status, | 85 mojom::UsbTransferStatus actual_status, |
80 const base::Optional<std::vector<uint8_t>>& actual_bytes) { | 86 const base::Optional<std::vector<uint8_t>>& actual_bytes) { |
81 EXPECT_EQ(expected_status, actual_status); | 87 EXPECT_EQ(expected_status, actual_status); |
82 ASSERT_TRUE(actual_bytes); | 88 ASSERT_TRUE(actual_bytes); |
83 ASSERT_EQ(expected_bytes.size(), actual_bytes->size()); | 89 ASSERT_EQ(expected_bytes.size(), actual_bytes->size()); |
84 for (size_t i = 0; i < actual_bytes->size(); ++i) { | 90 for (size_t i = 0; i < actual_bytes->size(); ++i) { |
85 EXPECT_EQ(expected_bytes[i], (*actual_bytes)[i]) | 91 EXPECT_EQ(expected_bytes[i], (*actual_bytes)[i]) |
86 << "Contents differ at index: " << i; | 92 << "Contents differ at index: " << i; |
87 } | 93 } |
88 continuation.Run(); | 94 continuation.Run(); |
89 } | 95 } |
90 | 96 |
91 void ExpectPacketsOutAndThen(const std::vector<uint32_t>& expected_packets, | 97 void ExpectPacketsOutAndThen( |
92 const base::Closure& continuation, | 98 const std::vector<uint32_t>& expected_packets, |
93 std::vector<IsochronousPacketPtr> actual_packets) { | 99 const base::Closure& continuation, |
| 100 std::vector<UsbIsochronousPacketPtr> actual_packets) { |
94 ASSERT_EQ(expected_packets.size(), actual_packets.size()); | 101 ASSERT_EQ(expected_packets.size(), actual_packets.size()); |
95 for (size_t i = 0; i < expected_packets.size(); ++i) { | 102 for (size_t i = 0; i < expected_packets.size(); ++i) { |
96 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length) | 103 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length) |
97 << "Packet lengths differ at index: " << i; | 104 << "Packet lengths differ at index: " << i; |
98 EXPECT_EQ(TransferStatus::COMPLETED, actual_packets[i]->status) | 105 EXPECT_EQ(mojom::UsbTransferStatus::COMPLETED, actual_packets[i]->status) |
99 << "Packet at index " << i << " not completed."; | 106 << "Packet at index " << i << " not completed."; |
100 } | 107 } |
101 continuation.Run(); | 108 continuation.Run(); |
102 } | 109 } |
103 | 110 |
104 void ExpectPacketsInAndThen( | 111 void ExpectPacketsInAndThen( |
105 const std::vector<uint8_t>& expected_bytes, | 112 const std::vector<uint8_t>& expected_bytes, |
106 const std::vector<uint32_t>& expected_packets, | 113 const std::vector<uint32_t>& expected_packets, |
107 const base::Closure& continuation, | 114 const base::Closure& continuation, |
108 const base::Optional<std::vector<uint8_t>>& actual_bytes, | 115 const base::Optional<std::vector<uint8_t>>& actual_bytes, |
109 std::vector<IsochronousPacketPtr> actual_packets) { | 116 std::vector<UsbIsochronousPacketPtr> actual_packets) { |
110 ASSERT_EQ(expected_packets.size(), actual_packets.size()); | 117 ASSERT_EQ(expected_packets.size(), actual_packets.size()); |
111 for (size_t i = 0; i < expected_packets.size(); ++i) { | 118 for (size_t i = 0; i < expected_packets.size(); ++i) { |
112 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length) | 119 EXPECT_EQ(expected_packets[i], actual_packets[i]->transferred_length) |
113 << "Packet lengths differ at index: " << i; | 120 << "Packet lengths differ at index: " << i; |
114 EXPECT_EQ(TransferStatus::COMPLETED, actual_packets[i]->status) | 121 EXPECT_EQ(mojom::UsbTransferStatus::COMPLETED, actual_packets[i]->status) |
115 << "Packet at index " << i << " not completed."; | 122 << "Packet at index " << i << " not completed."; |
116 } | 123 } |
117 ASSERT_TRUE(actual_bytes); | 124 ASSERT_TRUE(actual_bytes); |
118 ASSERT_EQ(expected_bytes.size(), actual_bytes->size()); | 125 ASSERT_EQ(expected_bytes.size(), actual_bytes->size()); |
119 for (size_t i = 0; i < actual_bytes->size(); ++i) { | 126 for (size_t i = 0; i < actual_bytes->size(); ++i) { |
120 EXPECT_EQ(expected_bytes[i], (*actual_bytes)[i]) | 127 EXPECT_EQ(expected_bytes[i], (*actual_bytes)[i]) |
121 << "Contents differ at index: " << i; | 128 << "Contents differ at index: " << i; |
122 } | 129 } |
123 continuation.Run(); | 130 continuation.Run(); |
124 } | 131 } |
125 | 132 |
126 void ExpectTransferStatusAndThen(TransferStatus expected_status, | 133 void ExpectTransferStatusAndThen(mojom::UsbTransferStatus expected_status, |
127 const base::Closure& continuation, | 134 const base::Closure& continuation, |
128 TransferStatus actual_status) { | 135 mojom::UsbTransferStatus actual_status) { |
129 EXPECT_EQ(expected_status, actual_status); | 136 EXPECT_EQ(expected_status, actual_status); |
130 continuation.Run(); | 137 continuation.Run(); |
131 } | 138 } |
132 | 139 |
133 class USBDeviceImplTest : public testing::Test { | 140 class USBDeviceImplTest : public testing::Test { |
134 public: | 141 public: |
135 USBDeviceImplTest() | 142 USBDeviceImplTest() |
136 : message_loop_(new base::MessageLoop), | 143 : message_loop_(new base::MessageLoop), |
137 is_device_open_(false), | 144 is_device_open_(false), |
138 allow_reset_(false) {} | 145 allow_reset_(false) {} |
139 | 146 |
140 ~USBDeviceImplTest() override {} | 147 ~USBDeviceImplTest() override {} |
141 | 148 |
142 void TearDown() override { base::RunLoop().RunUntilIdle(); } | 149 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
143 | 150 |
144 protected: | 151 protected: |
145 MockPermissionProvider& permission_provider() { return permission_provider_; } | 152 MockPermissionProvider& permission_provider() { return permission_provider_; } |
146 MockUsbDevice& mock_device() { return *mock_device_.get(); } | 153 MockUsbDevice& mock_device() { return *mock_device_.get(); } |
147 bool is_device_open() const { return is_device_open_; } | 154 bool is_device_open() const { return is_device_open_; } |
148 MockUsbDeviceHandle& mock_handle() { return *mock_handle_.get(); } | 155 MockUsbDeviceHandle& mock_handle() { return *mock_handle_.get(); } |
149 | 156 |
150 void set_allow_reset(bool allow_reset) { allow_reset_ = allow_reset; } | 157 void set_allow_reset(bool allow_reset) { allow_reset_ = allow_reset; } |
151 | 158 |
152 // Creates a mock device and binds a Device proxy to a Device service impl | 159 // Creates a mock device and binds a Device proxy to a Device service impl |
153 // wrapping the mock device. | 160 // wrapping the mock device. |
154 DevicePtr GetMockDeviceProxy(uint16_t vendor_id, | 161 UsbDevicePtr GetMockDeviceProxy(uint16_t vendor_id, |
155 uint16_t product_id, | 162 uint16_t product_id, |
156 const std::string& manufacturer, | 163 const std::string& manufacturer, |
157 const std::string& product, | 164 const std::string& product, |
158 const std::string& serial) { | 165 const std::string& serial) { |
159 mock_device_ = | 166 mock_device_ = |
160 new MockUsbDevice(vendor_id, product_id, manufacturer, product, serial); | 167 new MockUsbDevice(vendor_id, product_id, manufacturer, product, serial); |
161 mock_handle_ = new MockUsbDeviceHandle(mock_device_.get()); | 168 mock_handle_ = new MockUsbDeviceHandle(mock_device_.get()); |
162 | 169 |
163 DevicePtr proxy; | 170 UsbDevicePtr proxy; |
164 DeviceImpl::Create(mock_device_, permission_provider_.GetWeakPtr(), | 171 DeviceImpl::Create(mock_device_, permission_provider_.GetWeakPtr(), |
165 mojo::MakeRequest(&proxy)); | 172 mojo::MakeRequest(&proxy)); |
166 | 173 |
167 // Set up mock handle calls to respond based on mock device configs | 174 // Set up mock handle calls to respond based on mock device configs |
168 // established by the test. | 175 // established by the test. |
169 ON_CALL(mock_device(), Open(_)) | 176 ON_CALL(mock_device(), Open(_)) |
170 .WillByDefault(Invoke(this, &USBDeviceImplTest::OpenMockHandle)); | 177 .WillByDefault(Invoke(this, &USBDeviceImplTest::OpenMockHandle)); |
171 ON_CALL(mock_handle(), Close()) | 178 ON_CALL(mock_handle(), Close()) |
172 .WillByDefault(Invoke(this, &USBDeviceImplTest::CloseMockHandle)); | 179 .WillByDefault(Invoke(this, &USBDeviceImplTest::CloseMockHandle)); |
173 ON_CALL(mock_handle(), SetConfiguration(_, _)) | 180 ON_CALL(mock_handle(), SetConfiguration(_, _)) |
(...skipping 13 matching lines...) Expand all Loading... |
187 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer)); | 194 .WillByDefault(Invoke(this, &USBDeviceImplTest::GenericTransfer)); |
188 ON_CALL(mock_handle(), IsochronousTransferIn(_, _, _, _)) | 195 ON_CALL(mock_handle(), IsochronousTransferIn(_, _, _, _)) |
189 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransferIn)); | 196 .WillByDefault(Invoke(this, &USBDeviceImplTest::IsochronousTransferIn)); |
190 ON_CALL(mock_handle(), IsochronousTransferOut(_, _, _, _, _)) | 197 ON_CALL(mock_handle(), IsochronousTransferOut(_, _, _, _, _)) |
191 .WillByDefault( | 198 .WillByDefault( |
192 Invoke(this, &USBDeviceImplTest::IsochronousTransferOut)); | 199 Invoke(this, &USBDeviceImplTest::IsochronousTransferOut)); |
193 | 200 |
194 return proxy; | 201 return proxy; |
195 } | 202 } |
196 | 203 |
197 DevicePtr GetMockDeviceProxy() { | 204 UsbDevicePtr GetMockDeviceProxy() { |
198 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 205 return GetMockDeviceProxy(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
199 } | 206 } |
200 | 207 |
201 void AddMockConfig(const ConfigBuilder& builder) { | 208 void AddMockConfig(const ConfigBuilder& builder) { |
202 const UsbConfigDescriptor& config = builder.config(); | 209 const UsbConfigDescriptor& config = builder.config(); |
203 DCHECK(!base::ContainsKey(mock_configs_, config.configuration_value)); | 210 DCHECK(!base::ContainsKey(mock_configs_, config.configuration_value)); |
204 mock_configs_.insert(std::make_pair(config.configuration_value, config)); | 211 mock_configs_.insert(std::make_pair(config.configuration_value, config)); |
205 mock_device_->AddMockConfig(config); | 212 mock_device_->AddMockConfig(config); |
206 } | 213 } |
207 | 214 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 std::set<uint8_t> claimed_interfaces_; | 425 std::set<uint8_t> claimed_interfaces_; |
419 | 426 |
420 MockPermissionProvider permission_provider_; | 427 MockPermissionProvider permission_provider_; |
421 | 428 |
422 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); | 429 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); |
423 }; | 430 }; |
424 | 431 |
425 } // namespace | 432 } // namespace |
426 | 433 |
427 TEST_F(USBDeviceImplTest, Disconnect) { | 434 TEST_F(USBDeviceImplTest, Disconnect) { |
428 DevicePtr device = GetMockDeviceProxy(); | 435 UsbDevicePtr device = GetMockDeviceProxy(); |
429 | 436 |
430 base::RunLoop loop; | 437 base::RunLoop loop; |
431 device.set_connection_error_handler(loop.QuitClosure()); | 438 device.set_connection_error_handler(loop.QuitClosure()); |
432 mock_device().NotifyDeviceRemoved(); | 439 mock_device().NotifyDeviceRemoved(); |
433 loop.Run(); | 440 loop.Run(); |
434 } | 441 } |
435 | 442 |
436 TEST_F(USBDeviceImplTest, Open) { | 443 TEST_F(USBDeviceImplTest, Open) { |
437 DevicePtr device = GetMockDeviceProxy(); | 444 UsbDevicePtr device = GetMockDeviceProxy(); |
438 | 445 |
439 EXPECT_FALSE(is_device_open()); | 446 EXPECT_FALSE(is_device_open()); |
440 | 447 |
441 EXPECT_CALL(mock_device(), Open(_)); | 448 EXPECT_CALL(mock_device(), Open(_)); |
442 | 449 |
443 { | 450 { |
444 base::RunLoop loop; | 451 base::RunLoop loop; |
445 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 452 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
446 loop.QuitClosure())); | 453 loop.QuitClosure())); |
447 loop.Run(); | 454 loop.Run(); |
448 } | 455 } |
449 | 456 |
450 { | 457 { |
451 base::RunLoop loop; | 458 base::RunLoop loop; |
452 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::ALREADY_OPEN, | 459 device->Open(base::Bind(&ExpectOpenAndThen, |
| 460 mojom::UsbOpenDeviceError::ALREADY_OPEN, |
453 loop.QuitClosure())); | 461 loop.QuitClosure())); |
454 loop.Run(); | 462 loop.Run(); |
455 } | 463 } |
456 | 464 |
457 EXPECT_CALL(mock_handle(), Close()); | 465 EXPECT_CALL(mock_handle(), Close()); |
458 } | 466 } |
459 | 467 |
460 TEST_F(USBDeviceImplTest, Close) { | 468 TEST_F(USBDeviceImplTest, Close) { |
461 DevicePtr device = GetMockDeviceProxy(); | 469 UsbDevicePtr device = GetMockDeviceProxy(); |
462 | 470 |
463 EXPECT_FALSE(is_device_open()); | 471 EXPECT_FALSE(is_device_open()); |
464 | 472 |
465 EXPECT_CALL(mock_device(), Open(_)); | 473 EXPECT_CALL(mock_device(), Open(_)); |
466 | 474 |
467 { | 475 { |
468 base::RunLoop loop; | 476 base::RunLoop loop; |
469 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 477 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
470 loop.QuitClosure())); | 478 loop.QuitClosure())); |
471 loop.Run(); | 479 loop.Run(); |
472 } | 480 } |
473 | 481 |
474 EXPECT_CALL(mock_handle(), Close()); | 482 EXPECT_CALL(mock_handle(), Close()); |
475 | 483 |
476 { | 484 { |
477 base::RunLoop loop; | 485 base::RunLoop loop; |
478 device->Close(loop.QuitClosure()); | 486 device->Close(loop.QuitClosure()); |
479 loop.Run(); | 487 loop.Run(); |
480 } | 488 } |
481 | 489 |
482 EXPECT_FALSE(is_device_open()); | 490 EXPECT_FALSE(is_device_open()); |
483 } | 491 } |
484 | 492 |
485 TEST_F(USBDeviceImplTest, SetInvalidConfiguration) { | 493 TEST_F(USBDeviceImplTest, SetInvalidConfiguration) { |
486 DevicePtr device = GetMockDeviceProxy(); | 494 UsbDevicePtr device = GetMockDeviceProxy(); |
487 | 495 |
488 EXPECT_CALL(mock_device(), Open(_)); | 496 EXPECT_CALL(mock_device(), Open(_)); |
489 | 497 |
490 { | 498 { |
491 base::RunLoop loop; | 499 base::RunLoop loop; |
492 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 500 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
493 loop.QuitClosure())); | 501 loop.QuitClosure())); |
494 loop.Run(); | 502 loop.Run(); |
495 } | 503 } |
496 | 504 |
497 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 505 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
498 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); | 506 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); |
499 | 507 |
500 { | 508 { |
501 // SetConfiguration should fail because 42 is not a valid mock | 509 // SetConfiguration should fail because 42 is not a valid mock |
502 // configuration. | 510 // configuration. |
503 base::RunLoop loop; | 511 base::RunLoop loop; |
504 device->SetConfiguration( | 512 device->SetConfiguration( |
505 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 513 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
506 loop.Run(); | 514 loop.Run(); |
507 } | 515 } |
508 | 516 |
509 EXPECT_CALL(mock_handle(), Close()); | 517 EXPECT_CALL(mock_handle(), Close()); |
510 } | 518 } |
511 | 519 |
512 TEST_F(USBDeviceImplTest, SetValidConfiguration) { | 520 TEST_F(USBDeviceImplTest, SetValidConfiguration) { |
513 DevicePtr device = GetMockDeviceProxy(); | 521 UsbDevicePtr device = GetMockDeviceProxy(); |
514 | 522 |
515 EXPECT_CALL(mock_device(), Open(_)); | 523 EXPECT_CALL(mock_device(), Open(_)); |
516 | 524 |
517 { | 525 { |
518 base::RunLoop loop; | 526 base::RunLoop loop; |
519 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 527 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
520 loop.QuitClosure())); | 528 loop.QuitClosure())); |
521 loop.Run(); | 529 loop.Run(); |
522 } | 530 } |
523 | 531 |
524 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 532 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
525 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); | 533 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); |
526 | 534 |
527 AddMockConfig(ConfigBuilder(42)); | 535 AddMockConfig(ConfigBuilder(42)); |
528 | 536 |
529 { | 537 { |
530 // SetConfiguration should succeed because 42 is a valid mock configuration. | 538 // SetConfiguration should succeed because 42 is a valid mock configuration. |
531 base::RunLoop loop; | 539 base::RunLoop loop; |
532 device->SetConfiguration( | 540 device->SetConfiguration( |
533 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 541 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
534 loop.Run(); | 542 loop.Run(); |
535 } | 543 } |
536 | 544 |
537 EXPECT_CALL(mock_handle(), Close()); | 545 EXPECT_CALL(mock_handle(), Close()); |
538 } | 546 } |
539 | 547 |
540 // Verify that the result of Reset() reflects the underlying UsbDeviceHandle's | 548 // Verify that the result of Reset() reflects the underlying UsbDeviceHandle's |
541 // ResetDevice() result. | 549 // ResetDevice() result. |
542 TEST_F(USBDeviceImplTest, Reset) { | 550 TEST_F(USBDeviceImplTest, Reset) { |
543 DevicePtr device = GetMockDeviceProxy(); | 551 UsbDevicePtr device = GetMockDeviceProxy(); |
544 | 552 |
545 EXPECT_CALL(mock_device(), Open(_)); | 553 EXPECT_CALL(mock_device(), Open(_)); |
546 | 554 |
547 { | 555 { |
548 base::RunLoop loop; | 556 base::RunLoop loop; |
549 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 557 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
550 loop.QuitClosure())); | 558 loop.QuitClosure())); |
551 loop.Run(); | 559 loop.Run(); |
552 } | 560 } |
553 | 561 |
554 EXPECT_CALL(mock_handle(), ResetDevice(_)); | 562 EXPECT_CALL(mock_handle(), ResetDevice(_)); |
555 | 563 |
556 set_allow_reset(true); | 564 set_allow_reset(true); |
557 | 565 |
558 { | 566 { |
559 base::RunLoop loop; | 567 base::RunLoop loop; |
560 device->Reset(base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 568 device->Reset(base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
561 loop.Run(); | 569 loop.Run(); |
562 } | 570 } |
563 | 571 |
564 EXPECT_CALL(mock_handle(), ResetDevice(_)); | 572 EXPECT_CALL(mock_handle(), ResetDevice(_)); |
565 | 573 |
566 set_allow_reset(false); | 574 set_allow_reset(false); |
567 | 575 |
568 { | 576 { |
569 base::RunLoop loop; | 577 base::RunLoop loop; |
570 device->Reset(base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 578 device->Reset(base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
571 loop.Run(); | 579 loop.Run(); |
572 } | 580 } |
573 | 581 |
574 EXPECT_CALL(mock_handle(), Close()); | 582 EXPECT_CALL(mock_handle(), Close()); |
575 } | 583 } |
576 | 584 |
577 TEST_F(USBDeviceImplTest, ClaimAndReleaseInterface) { | 585 TEST_F(USBDeviceImplTest, ClaimAndReleaseInterface) { |
578 DevicePtr device = GetMockDeviceProxy(); | 586 UsbDevicePtr device = GetMockDeviceProxy(); |
579 | 587 |
580 EXPECT_CALL(mock_device(), Open(_)); | 588 EXPECT_CALL(mock_device(), Open(_)); |
581 | 589 |
582 { | 590 { |
583 base::RunLoop loop; | 591 base::RunLoop loop; |
584 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 592 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
585 loop.QuitClosure())); | 593 loop.QuitClosure())); |
586 loop.Run(); | 594 loop.Run(); |
587 } | 595 } |
588 | 596 |
589 // Now add a mock interface #1. | 597 // Now add a mock interface #1. |
590 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); | 598 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); |
591 | 599 |
592 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 600 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
593 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | 601 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
594 | 602 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 base::RunLoop loop; | 642 base::RunLoop loop; |
635 device->ReleaseInterface( | 643 device->ReleaseInterface( |
636 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 644 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
637 loop.Run(); | 645 loop.Run(); |
638 } | 646 } |
639 | 647 |
640 EXPECT_CALL(mock_handle(), Close()); | 648 EXPECT_CALL(mock_handle(), Close()); |
641 } | 649 } |
642 | 650 |
643 TEST_F(USBDeviceImplTest, SetInterfaceAlternateSetting) { | 651 TEST_F(USBDeviceImplTest, SetInterfaceAlternateSetting) { |
644 DevicePtr device = GetMockDeviceProxy(); | 652 UsbDevicePtr device = GetMockDeviceProxy(); |
645 | 653 |
646 EXPECT_CALL(mock_device(), Open(_)); | 654 EXPECT_CALL(mock_device(), Open(_)); |
647 | 655 |
648 { | 656 { |
649 base::RunLoop loop; | 657 base::RunLoop loop; |
650 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 658 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
651 loop.QuitClosure())); | 659 loop.QuitClosure())); |
652 loop.Run(); | 660 loop.Run(); |
653 } | 661 } |
654 | 662 |
655 AddMockConfig(ConfigBuilder(1) | 663 AddMockConfig(ConfigBuilder(1) |
656 .AddInterface(1, 0, 1, 2, 3) | 664 .AddInterface(1, 0, 1, 2, 3) |
657 .AddInterface(1, 42, 1, 2, 3) | 665 .AddInterface(1, 42, 1, 2, 3) |
658 .AddInterface(2, 0, 1, 2, 3)); | 666 .AddInterface(2, 0, 1, 2, 3)); |
659 | 667 |
660 EXPECT_CALL(mock_handle(), SetInterfaceAlternateSetting(1, 42, _)); | 668 EXPECT_CALL(mock_handle(), SetInterfaceAlternateSetting(1, 42, _)); |
(...skipping 11 matching lines...) Expand all Loading... |
672 base::RunLoop loop; | 680 base::RunLoop loop; |
673 device->SetInterfaceAlternateSetting( | 681 device->SetInterfaceAlternateSetting( |
674 1, 100, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 682 1, 100, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
675 loop.Run(); | 683 loop.Run(); |
676 } | 684 } |
677 | 685 |
678 EXPECT_CALL(mock_handle(), Close()); | 686 EXPECT_CALL(mock_handle(), Close()); |
679 } | 687 } |
680 | 688 |
681 TEST_F(USBDeviceImplTest, ControlTransfer) { | 689 TEST_F(USBDeviceImplTest, ControlTransfer) { |
682 DevicePtr device = GetMockDeviceProxy(); | 690 UsbDevicePtr device = GetMockDeviceProxy(); |
683 | 691 |
684 EXPECT_CALL(mock_device(), Open(_)); | 692 EXPECT_CALL(mock_device(), Open(_)); |
685 | 693 |
686 { | 694 { |
687 base::RunLoop loop; | 695 base::RunLoop loop; |
688 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 696 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
689 loop.QuitClosure())); | 697 loop.QuitClosure())); |
690 loop.Run(); | 698 loop.Run(); |
691 } | 699 } |
692 | 700 |
693 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); | 701 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); |
694 | 702 |
695 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 703 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
696 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | 704 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
697 | 705 |
698 { | 706 { |
699 base::RunLoop loop; | 707 base::RunLoop loop; |
700 device->SetConfiguration( | 708 device->SetConfiguration( |
701 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 709 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
702 loop.Run(); | 710 loop.Run(); |
703 } | 711 } |
704 | 712 |
705 std::vector<uint8_t> fake_data; | 713 std::vector<uint8_t> fake_data; |
706 fake_data.push_back(41); | 714 fake_data.push_back(41); |
707 fake_data.push_back(42); | 715 fake_data.push_back(42); |
708 fake_data.push_back(43); | 716 fake_data.push_back(43); |
709 | 717 |
710 AddMockInboundData(fake_data); | 718 AddMockInboundData(fake_data); |
711 | 719 |
712 EXPECT_CALL(mock_handle(), | 720 EXPECT_CALL(mock_handle(), |
713 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, | 721 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, |
714 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _)); | 722 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _)); |
715 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | 723 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
716 | 724 |
717 { | 725 { |
718 auto params = ControlTransferParams::New(); | 726 auto params = mojom::UsbControlTransferParams::New(); |
719 params->type = ControlTransferType::STANDARD; | 727 params->type = UsbControlTransferType::STANDARD; |
720 params->recipient = ControlTransferRecipient::DEVICE; | 728 params->recipient = UsbControlTransferRecipient::DEVICE; |
721 params->request = 5; | 729 params->request = 5; |
722 params->value = 6; | 730 params->value = 6; |
723 params->index = 7; | 731 params->index = 7; |
724 base::RunLoop loop; | 732 base::RunLoop loop; |
725 device->ControlTransferIn( | 733 device->ControlTransferIn(std::move(params), |
726 std::move(params), static_cast<uint32_t>(fake_data.size()), 0, | 734 static_cast<uint32_t>(fake_data.size()), 0, |
727 base::Bind(&ExpectTransferInAndThen, TransferStatus::COMPLETED, | 735 base::Bind(&ExpectTransferInAndThen, |
728 fake_data, loop.QuitClosure())); | 736 mojom::UsbTransferStatus::COMPLETED, |
| 737 fake_data, loop.QuitClosure())); |
729 loop.Run(); | 738 loop.Run(); |
730 } | 739 } |
731 | 740 |
732 AddMockOutboundData(fake_data); | 741 AddMockOutboundData(fake_data); |
733 | 742 |
734 EXPECT_CALL(mock_handle(), | 743 EXPECT_CALL(mock_handle(), |
735 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, | 744 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, |
736 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _)); | 745 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _)); |
737 EXPECT_CALL(permission_provider(), HasFunctionPermission(7, 1, _)); | 746 EXPECT_CALL(permission_provider(), HasFunctionPermission(7, 1, _)); |
738 | 747 |
739 { | 748 { |
740 auto params = ControlTransferParams::New(); | 749 auto params = mojom::UsbControlTransferParams::New(); |
741 params->type = ControlTransferType::STANDARD; | 750 params->type = UsbControlTransferType::STANDARD; |
742 params->recipient = ControlTransferRecipient::INTERFACE; | 751 params->recipient = UsbControlTransferRecipient::INTERFACE; |
743 params->request = 5; | 752 params->request = 5; |
744 params->value = 6; | 753 params->value = 6; |
745 params->index = 7; | 754 params->index = 7; |
746 base::RunLoop loop; | 755 base::RunLoop loop; |
747 device->ControlTransferOut( | 756 device->ControlTransferOut( |
748 std::move(params), fake_data, 0, | 757 std::move(params), fake_data, 0, |
749 base::Bind(&ExpectTransferStatusAndThen, TransferStatus::COMPLETED, | 758 base::Bind(&ExpectTransferStatusAndThen, |
750 loop.QuitClosure())); | 759 mojom::UsbTransferStatus::COMPLETED, loop.QuitClosure())); |
751 loop.Run(); | 760 loop.Run(); |
752 } | 761 } |
753 | 762 |
754 EXPECT_CALL(mock_handle(), Close()); | 763 EXPECT_CALL(mock_handle(), Close()); |
755 } | 764 } |
756 | 765 |
757 TEST_F(USBDeviceImplTest, GenericTransfer) { | 766 TEST_F(USBDeviceImplTest, GenericTransfer) { |
758 DevicePtr device = GetMockDeviceProxy(); | 767 UsbDevicePtr device = GetMockDeviceProxy(); |
759 | 768 |
760 EXPECT_CALL(mock_device(), Open(_)); | 769 EXPECT_CALL(mock_device(), Open(_)); |
761 | 770 |
762 { | 771 { |
763 base::RunLoop loop; | 772 base::RunLoop loop; |
764 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 773 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
765 loop.QuitClosure())); | 774 loop.QuitClosure())); |
766 loop.Run(); | 775 loop.Run(); |
767 } | 776 } |
768 | 777 |
769 std::string message1 = "say hello please"; | 778 std::string message1 = "say hello please"; |
770 std::vector<uint8_t> fake_outbound_data(message1.size()); | 779 std::vector<uint8_t> fake_outbound_data(message1.size()); |
771 std::copy(message1.begin(), message1.end(), fake_outbound_data.begin()); | 780 std::copy(message1.begin(), message1.end(), fake_outbound_data.begin()); |
772 | 781 |
773 std::string message2 = "hello world!"; | 782 std::string message2 = "hello world!"; |
774 std::vector<uint8_t> fake_inbound_data(message2.size()); | 783 std::vector<uint8_t> fake_inbound_data(message2.size()); |
775 std::copy(message2.begin(), message2.end(), fake_inbound_data.begin()); | 784 std::copy(message2.begin(), message2.end(), fake_inbound_data.begin()); |
776 | 785 |
777 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); | 786 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); |
778 AddMockOutboundData(fake_outbound_data); | 787 AddMockOutboundData(fake_outbound_data); |
779 AddMockInboundData(fake_inbound_data); | 788 AddMockInboundData(fake_inbound_data); |
780 | 789 |
781 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_OUTBOUND, 0x01, _, | 790 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_OUTBOUND, 0x01, _, |
782 fake_outbound_data.size(), 0, _)); | 791 fake_outbound_data.size(), 0, _)); |
783 | 792 |
784 { | 793 { |
785 base::RunLoop loop; | 794 base::RunLoop loop; |
786 device->GenericTransferOut( | 795 device->GenericTransferOut( |
787 1, fake_outbound_data, 0, | 796 1, fake_outbound_data, 0, |
788 base::Bind(&ExpectTransferStatusAndThen, TransferStatus::COMPLETED, | 797 base::Bind(&ExpectTransferStatusAndThen, |
789 loop.QuitClosure())); | 798 mojom::UsbTransferStatus::COMPLETED, loop.QuitClosure())); |
790 loop.Run(); | 799 loop.Run(); |
791 } | 800 } |
792 | 801 |
793 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_INBOUND, 0x81, _, | 802 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_INBOUND, 0x81, _, |
794 fake_inbound_data.size(), 0, _)); | 803 fake_inbound_data.size(), 0, _)); |
795 | 804 |
796 { | 805 { |
797 base::RunLoop loop; | 806 base::RunLoop loop; |
798 device->GenericTransferIn( | 807 device->GenericTransferIn( |
799 1, static_cast<uint32_t>(fake_inbound_data.size()), 0, | 808 1, static_cast<uint32_t>(fake_inbound_data.size()), 0, |
800 base::Bind(&ExpectTransferInAndThen, TransferStatus::COMPLETED, | 809 base::Bind(&ExpectTransferInAndThen, |
801 fake_inbound_data, loop.QuitClosure())); | 810 mojom::UsbTransferStatus::COMPLETED, fake_inbound_data, |
| 811 loop.QuitClosure())); |
802 loop.Run(); | 812 loop.Run(); |
803 } | 813 } |
804 | 814 |
805 EXPECT_CALL(mock_handle(), Close()); | 815 EXPECT_CALL(mock_handle(), Close()); |
806 } | 816 } |
807 | 817 |
808 TEST_F(USBDeviceImplTest, IsochronousTransfer) { | 818 TEST_F(USBDeviceImplTest, IsochronousTransfer) { |
809 DevicePtr device = GetMockDeviceProxy(); | 819 UsbDevicePtr device = GetMockDeviceProxy(); |
810 | 820 |
811 EXPECT_CALL(mock_device(), Open(_)); | 821 EXPECT_CALL(mock_device(), Open(_)); |
812 | 822 |
813 { | 823 { |
814 base::RunLoop loop; | 824 base::RunLoop loop; |
815 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 825 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
816 loop.QuitClosure())); | 826 loop.QuitClosure())); |
817 loop.Run(); | 827 loop.Run(); |
818 } | 828 } |
819 | 829 |
820 std::vector<UsbDeviceHandle::IsochronousPacket> fake_packets(4); | 830 std::vector<UsbDeviceHandle::IsochronousPacket> fake_packets(4); |
821 for (size_t i = 0; i < fake_packets.size(); ++i) { | 831 for (size_t i = 0; i < fake_packets.size(); ++i) { |
822 fake_packets[i].length = 8; | 832 fake_packets[i].length = 8; |
823 fake_packets[i].transferred_length = 8; | 833 fake_packets[i].transferred_length = 8; |
824 fake_packets[i].status = USB_TRANSFER_COMPLETED; | 834 fake_packets[i].status = USB_TRANSFER_COMPLETED; |
825 } | 835 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, | 873 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, |
864 expected_transferred_lengths, loop.QuitClosure())); | 874 expected_transferred_lengths, loop.QuitClosure())); |
865 loop.Run(); | 875 loop.Run(); |
866 } | 876 } |
867 | 877 |
868 EXPECT_CALL(mock_handle(), Close()); | 878 EXPECT_CALL(mock_handle(), Close()); |
869 } | 879 } |
870 | 880 |
871 } // namespace usb | 881 } // namespace usb |
872 } // namespace device | 882 } // namespace device |
OLD | NEW |