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" | |
16 #include "device/usb/usb_device.h" | 15 #include "device/usb/usb_device.h" |
17 #include "device/usb/usb_device_handle.h" | 16 #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; |
28 using device::UsbService; | 29 using device::UsbService; |
29 using device::UsbSynchronizationType; | 30 using device::UsbSynchronizationType; |
30 using device::UsbTransferCallback; | 31 using device::UsbTransferCallback; |
31 using device::UsbTransferType; | 32 using device::UsbTransferType; |
32 using device::UsbUsageType; | 33 using device::UsbUsageType; |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 struct AndroidTraits { | 37 struct AndroidTraits { |
37 static const int kClass = 0xff; | 38 static const int kClass = 0xff; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 return kSampleListProcesses; | 97 return kSampleListProcesses; |
97 } else if (command == kInstalledChromePackagesCommand) { | 98 } else if (command == kInstalledChromePackagesCommand) { |
98 return kSampleListPackages; | 99 return kSampleListPackages; |
99 } | 100 } |
100 | 101 |
101 DCHECK(false) << "Should not be reached"; | 102 DCHECK(false) << "Should not be reached"; |
102 | 103 |
103 return ""; | 104 return ""; |
104 } | 105 } |
105 | 106 |
| 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 |
106 template <class T> | 226 template <class T> |
107 class MockUsbDevice; | 227 class MockUsbDevice; |
108 | 228 |
109 template <class T> | 229 template <class T> |
110 class MockUsbDeviceHandle : public UsbDeviceHandle { | 230 class MockUsbDeviceHandle : public UsbDeviceHandle { |
111 public: | 231 public: |
112 explicit MockUsbDeviceHandle(MockUsbDevice<T>* device) | 232 explicit MockUsbDeviceHandle(MockUsbDevice<T>* device) |
113 : device_(device), | 233 : device_(device), |
114 remaining_body_length_(0), | 234 remaining_body_length_(0), |
115 next_local_socket_(0) {} | 235 next_local_socket_(0) {} |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 uint32 remaining_body_length_; | 460 uint32 remaining_body_length_; |
341 scoped_refptr<AdbMessage> current_message_; | 461 scoped_refptr<AdbMessage> current_message_; |
342 std::vector<char> output_buffer_; | 462 std::vector<char> output_buffer_; |
343 std::queue<Query> queries_; | 463 std::queue<Query> queries_; |
344 int next_local_socket_; | 464 int next_local_socket_; |
345 }; | 465 }; |
346 | 466 |
347 template <class T> | 467 template <class T> |
348 class MockUsbDevice : public UsbDevice { | 468 class MockUsbDevice : public UsbDevice { |
349 public: | 469 public: |
350 MockUsbDevice() : UsbDevice(0, 0, 0) { | 470 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; | |
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 } | |
374 | 471 |
375 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | 472 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { |
376 return new MockUsbDeviceHandle<T>(this); | 473 return new MockUsbDeviceHandle<T>(this); |
377 } | 474 } |
378 | 475 |
379 virtual const UsbConfigDescriptor& GetConfiguration() OVERRIDE { | 476 virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE { |
380 return config_desc_; | 477 return new MockUsbConfigDescriptor<T>(); |
381 } | 478 } |
382 | 479 |
383 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 480 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { |
384 return true; | 481 return true; |
385 } | 482 } |
386 | 483 |
387 #if defined(OS_CHROMEOS) | 484 #if defined(OS_CHROMEOS) |
388 // On ChromeOS, if an interface of a claimed device is not claimed, the | 485 // On ChromeOS, if an interface of a claimed device is not claimed, the |
389 // permission broker can change the owner of the device so that the unclaimed | 486 // permission broker can change the owner of the device so that the unclaimed |
390 // interfaces can be used. If this argument is missing, permission broker will | 487 // interfaces can be used. If this argument is missing, permission broker will |
391 // not be used and this method fails if the device is claimed. | 488 // not be used and this method fails if the device is claimed. |
392 virtual void RequestUsbAccess( | 489 virtual void RequestUsbAccess( |
393 int interface_id, | 490 int interface_id, |
394 const base::Callback<void(bool success)>& callback) OVERRIDE { | 491 const base::Callback<void(bool success)>& callback) OVERRIDE { |
395 callback.Run(true); | 492 callback.Run(true); |
396 } | 493 } |
397 #endif // OS_CHROMEOS | 494 #endif // OS_CHROMEOS |
398 | 495 |
399 std::set<int> claimed_interfaces_; | 496 std::set<int> claimed_interfaces_; |
400 | 497 |
401 protected: | 498 protected: |
402 virtual ~MockUsbDevice() {} | 499 virtual ~MockUsbDevice() {} |
403 | |
404 private: | |
405 UsbConfigDescriptor config_desc_; | |
406 }; | 500 }; |
407 | 501 |
408 class MockUsbService : public UsbService { | 502 class MockUsbService : public UsbService { |
409 public: | 503 public: |
410 MockUsbService() { | 504 MockUsbService() { |
411 devices_.push_back(new MockUsbDevice<AndroidTraits>()); | 505 devices_.push_back(new MockUsbDevice<AndroidTraits>()); |
412 } | 506 } |
413 | 507 |
414 virtual ~MockUsbService() {} | 508 virtual ~MockUsbService() {} |
415 | 509 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 runner_->Run(); | 856 runner_->Run(); |
763 EXPECT_EQ(2, listener.invoked_); | 857 EXPECT_EQ(2, listener.invoked_); |
764 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); | 858 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); |
765 } | 859 } |
766 | 860 |
767 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { | 861 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { |
768 MockCountListenerForCheckingTraits listener(adb_bridge_); | 862 MockCountListenerForCheckingTraits listener(adb_bridge_); |
769 adb_bridge_->AddDeviceCountListener(&listener); | 863 adb_bridge_->AddDeviceCountListener(&listener); |
770 runner_->Run(); | 864 runner_->Run(); |
771 } | 865 } |
OLD | NEW |