OLD | NEW |
---|---|
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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/devtools/device/devtools_android_bridge.h" | 8 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
9 #include "chrome/browser/devtools/device/usb/android_usb_device.h" | 9 #include "chrome/browser/devtools/device/usb/android_usb_device.h" |
10 #include "chrome/browser/devtools/device/usb/usb_device_provider.h" | 10 #include "chrome/browser/devtools/device/usb/usb_device_provider.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
15 #include "device/usb/usb_descriptors.h" | |
15 #include "device/usb/usb_device.h" | 16 #include "device/usb/usb_device.h" |
16 #include "device/usb/usb_device_handle.h" | 17 #include "device/usb/usb_device_handle.h" |
17 #include "device/usb/usb_interface.h" | |
18 #include "device/usb/usb_service.h" | 18 #include "device/usb/usb_service.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 using content::BrowserThread; | 21 using content::BrowserThread; |
22 using device::UsbConfigDescriptor; | 22 using device::UsbConfigDescriptor; |
23 using device::UsbDevice; | 23 using device::UsbDevice; |
24 using device::UsbDeviceHandle; | 24 using device::UsbDeviceHandle; |
25 using device::UsbEndpointDescriptor; | 25 using device::UsbEndpointDescriptor; |
26 using device::UsbEndpointDirection; | 26 using device::UsbEndpointDirection; |
27 using device::UsbInterfaceDescriptor; | 27 using device::UsbInterfaceDescriptor; |
28 using device::UsbInterfaceAltSettingDescriptor; | |
29 using device::UsbService; | 28 using device::UsbService; |
30 using device::UsbSynchronizationType; | 29 using device::UsbSynchronizationType; |
31 using device::UsbTransferCallback; | 30 using device::UsbTransferCallback; |
32 using device::UsbTransferType; | 31 using device::UsbTransferType; |
33 using device::UsbUsageType; | 32 using device::UsbUsageType; |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 struct AndroidTraits { | 36 struct AndroidTraits { |
38 static const int kClass = 0xff; | 37 static const int kClass = 0xff; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 return kSampleListProcesses; | 96 return kSampleListProcesses; |
98 } else if (command == kInstalledChromePackagesCommand) { | 97 } else if (command == kInstalledChromePackagesCommand) { |
99 return kSampleListPackages; | 98 return kSampleListPackages; |
100 } | 99 } |
101 | 100 |
102 DCHECK(false) << "Should not be reached"; | 101 DCHECK(false) << "Should not be reached"; |
103 | 102 |
104 return ""; | 103 return ""; |
105 } | 104 } |
106 | 105 |
107 class MockUsbEndpointDescriptor : public UsbEndpointDescriptor { | |
108 public: | |
109 virtual int GetAddress() const OVERRIDE { return address_; } | |
110 | |
111 virtual UsbEndpointDirection GetDirection() const OVERRIDE { | |
112 return direction_; | |
113 } | |
114 | |
115 virtual int GetMaximumPacketSize() const OVERRIDE { | |
116 return maximum_packet_size_; | |
117 } | |
118 | |
119 virtual UsbSynchronizationType GetSynchronizationType() const OVERRIDE { | |
120 return usb_synchronization_type_; | |
121 } | |
122 | |
123 virtual UsbTransferType GetTransferType() const OVERRIDE { | |
124 return usb_transfer_type_; | |
125 } | |
126 virtual UsbUsageType GetUsageType() const OVERRIDE { return usb_usage_type_; } | |
127 | |
128 virtual int GetPollingInterval() const OVERRIDE { return polling_interval_; } | |
129 | |
130 int address_; | |
131 UsbEndpointDirection direction_; | |
132 int maximum_packet_size_; | |
133 UsbSynchronizationType usb_synchronization_type_; | |
134 UsbTransferType usb_transfer_type_; | |
135 UsbUsageType usb_usage_type_; | |
136 int polling_interval_; | |
137 | |
138 private: | |
139 virtual ~MockUsbEndpointDescriptor() {} | |
140 }; | |
141 | |
142 template <class T> | |
143 class MockUsbInterfaceAltSettingDescriptor | |
144 : public UsbInterfaceAltSettingDescriptor { | |
145 public: | |
146 MockUsbInterfaceAltSettingDescriptor(int interface_number, | |
147 int alternate_setting) | |
148 : interface_number_(interface_number), | |
149 alternate_setting_(alternate_setting) {} | |
150 | |
151 virtual size_t GetNumEndpoints() const OVERRIDE { | |
152 // See IsAndroidInterface function in android_usb_device.cc | |
153 return 2; | |
154 } | |
155 | |
156 virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint( | |
157 size_t index) const OVERRIDE { | |
158 EXPECT_GT(static_cast<size_t>(2), index); | |
159 MockUsbEndpointDescriptor* result = new MockUsbEndpointDescriptor(); | |
160 result->address_ = index + 1; | |
161 result->usb_transfer_type_ = device::USB_TRANSFER_BULK; | |
162 result->direction_ = ((index == 0) ? device::USB_DIRECTION_INBOUND | |
163 : device::USB_DIRECTION_OUTBOUND); | |
164 result->maximum_packet_size_ = 1 << 20; // 1Mb maximum packet size | |
165 return result; | |
166 } | |
167 | |
168 virtual int GetInterfaceNumber() const OVERRIDE { return interface_number_; } | |
169 | |
170 virtual int GetAlternateSetting() const OVERRIDE { | |
171 return alternate_setting_; | |
172 } | |
173 | |
174 virtual int GetInterfaceClass() const OVERRIDE { return T::kClass; } | |
175 | |
176 virtual int GetInterfaceSubclass() const OVERRIDE { return T::kSubclass; } | |
177 | |
178 virtual int GetInterfaceProtocol() const OVERRIDE { return T::kProtocol; } | |
179 | |
180 protected: | |
181 virtual ~MockUsbInterfaceAltSettingDescriptor() {}; | |
182 | |
183 private: | |
184 const int interface_number_; | |
185 const int alternate_setting_; | |
186 }; | |
187 | |
188 template <class T> | |
189 class MockUsbInterfaceDescriptor : public UsbInterfaceDescriptor { | |
190 public: | |
191 explicit MockUsbInterfaceDescriptor(int interface_number) | |
192 : interface_number_(interface_number) {} | |
193 | |
194 virtual size_t GetNumAltSettings() const OVERRIDE { | |
195 // See IsAndroidInterface function in android_usb_device.cc | |
196 return 1; | |
197 } | |
198 virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting( | |
199 size_t index) const OVERRIDE { | |
200 EXPECT_EQ(static_cast<size_t>(0), index); | |
201 return new MockUsbInterfaceAltSettingDescriptor<T>(interface_number_, 0); | |
202 } | |
203 | |
204 protected: | |
205 const int interface_number_; | |
206 virtual ~MockUsbInterfaceDescriptor() {} | |
207 }; | |
208 | |
209 template <class T> | |
210 class MockUsbConfigDescriptor : public UsbConfigDescriptor { | |
211 public: | |
212 MockUsbConfigDescriptor() {} | |
213 | |
214 virtual size_t GetNumInterfaces() const OVERRIDE { return 1; } | |
215 | |
216 virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface( | |
217 size_t index) const OVERRIDE { | |
218 EXPECT_EQ(static_cast<size_t>(0), index); | |
219 return new MockUsbInterfaceDescriptor<T>(index); | |
220 } | |
221 | |
222 protected: | |
223 virtual ~MockUsbConfigDescriptor() {}; | |
224 }; | |
225 | |
226 template <class T> | 106 template <class T> |
227 class MockUsbDevice; | 107 class MockUsbDevice; |
228 | 108 |
229 template <class T> | 109 template <class T> |
230 class MockUsbDeviceHandle : public UsbDeviceHandle { | 110 class MockUsbDeviceHandle : public UsbDeviceHandle { |
231 public: | 111 public: |
232 explicit MockUsbDeviceHandle(MockUsbDevice<T>* device) | 112 explicit MockUsbDeviceHandle(MockUsbDevice<T>* device) |
233 : device_(device), | 113 : device_(device), |
234 remaining_body_length_(0), | 114 remaining_body_length_(0), |
235 next_local_socket_(0) {} | 115 next_local_socket_(0) {} |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 uint32 remaining_body_length_; | 340 uint32 remaining_body_length_; |
461 scoped_refptr<AdbMessage> current_message_; | 341 scoped_refptr<AdbMessage> current_message_; |
462 std::vector<char> output_buffer_; | 342 std::vector<char> output_buffer_; |
463 std::queue<Query> queries_; | 343 std::queue<Query> queries_; |
464 int next_local_socket_; | 344 int next_local_socket_; |
465 }; | 345 }; |
466 | 346 |
467 template <class T> | 347 template <class T> |
468 class MockUsbDevice : public UsbDevice { | 348 class MockUsbDevice : public UsbDevice { |
469 public: | 349 public: |
470 MockUsbDevice() : UsbDevice(0, 0, 0) {} | 350 MockUsbDevice() : UsbDevice(0, 0, 0) { |
351 UsbEndpointDescriptor bulk_in; | |
352 bulk_in.address = 0x81; | |
353 bulk_in.direction = device::USB_DIRECTION_INBOUND; | |
354 bulk_in.maximum_packet_size = 512; | |
dgozman
2014/09/11 11:23:47
This was 1Mb before. Why did you change it? This c
Reilly Grant (use Gerrit)
2014/09/11 17:49:26
1Mb is impossibly large for a USB device (USB 2.0
| |
355 bulk_in.transfer_type = device::USB_TRANSFER_BULK; | |
356 | |
357 UsbEndpointDescriptor bulk_out; | |
358 bulk_out.address = 0x01; | |
359 bulk_out.direction = device::USB_DIRECTION_OUTBOUND; | |
360 bulk_out.maximum_packet_size = 512; | |
361 bulk_out.transfer_type = device::USB_TRANSFER_BULK; | |
362 | |
363 UsbInterfaceDescriptor interface_desc; | |
364 interface_desc.interface_number = 0; | |
365 interface_desc.alternate_setting = 0; | |
366 interface_desc.interface_class = T::kClass; | |
367 interface_desc.interface_subclass = T::kSubclass; | |
368 interface_desc.interface_protocol = T::kProtocol; | |
369 interface_desc.endpoints.push_back(bulk_in); | |
370 interface_desc.endpoints.push_back(bulk_out); | |
371 | |
372 config_desc_.interfaces.push_back(interface_desc); | |
373 } | |
471 | 374 |
472 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | 375 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { |
473 return new MockUsbDeviceHandle<T>(this); | 376 return new MockUsbDeviceHandle<T>(this); |
474 } | 377 } |
475 | 378 |
476 virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE { | 379 virtual const UsbConfigDescriptor& GetConfiguration() OVERRIDE { |
477 return new MockUsbConfigDescriptor<T>(); | 380 return config_desc_; |
478 } | 381 } |
479 | 382 |
480 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 383 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { |
481 return true; | 384 return true; |
482 } | 385 } |
483 | 386 |
484 #if defined(OS_CHROMEOS) | 387 #if defined(OS_CHROMEOS) |
485 // On ChromeOS, if an interface of a claimed device is not claimed, the | 388 // On ChromeOS, if an interface of a claimed device is not claimed, the |
486 // permission broker can change the owner of the device so that the unclaimed | 389 // permission broker can change the owner of the device so that the unclaimed |
487 // interfaces can be used. If this argument is missing, permission broker will | 390 // interfaces can be used. If this argument is missing, permission broker will |
488 // not be used and this method fails if the device is claimed. | 391 // not be used and this method fails if the device is claimed. |
489 virtual void RequestUsbAccess( | 392 virtual void RequestUsbAccess( |
490 int interface_id, | 393 int interface_id, |
491 const base::Callback<void(bool success)>& callback) OVERRIDE { | 394 const base::Callback<void(bool success)>& callback) OVERRIDE { |
492 callback.Run(true); | 395 callback.Run(true); |
493 } | 396 } |
494 #endif // OS_CHROMEOS | 397 #endif // OS_CHROMEOS |
495 | 398 |
496 std::set<int> claimed_interfaces_; | 399 std::set<int> claimed_interfaces_; |
497 | 400 |
498 protected: | 401 protected: |
499 virtual ~MockUsbDevice() {} | 402 virtual ~MockUsbDevice() {} |
403 | |
404 private: | |
405 UsbConfigDescriptor config_desc_; | |
500 }; | 406 }; |
501 | 407 |
502 class MockUsbService : public UsbService { | 408 class MockUsbService : public UsbService { |
503 public: | 409 public: |
504 MockUsbService() { | 410 MockUsbService() { |
505 devices_.push_back(new MockUsbDevice<AndroidTraits>()); | 411 devices_.push_back(new MockUsbDevice<AndroidTraits>()); |
506 } | 412 } |
507 | 413 |
508 virtual ~MockUsbService() {} | 414 virtual ~MockUsbService() {} |
509 | 415 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
856 runner_->Run(); | 762 runner_->Run(); |
857 EXPECT_EQ(2, listener.invoked_); | 763 EXPECT_EQ(2, listener.invoked_); |
858 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); | 764 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); |
859 } | 765 } |
860 | 766 |
861 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { | 767 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { |
862 MockCountListenerForCheckingTraits listener(adb_bridge_); | 768 MockCountListenerForCheckingTraits listener(adb_bridge_); |
863 adb_bridge_->AddDeviceCountListener(&listener); | 769 adb_bridge_->AddDeviceCountListener(&listener); |
864 runner_->Run(); | 770 runner_->Run(); |
865 } | 771 } |
OLD | NEW |