OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
| 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/test/base/in_process_browser_test.h" |
| 10 #include "components/usb_service/usb_device.h" |
| 11 #include "components/usb_service/usb_device_handle.h" |
| 12 #include "components/usb_service/usb_interface.h" |
| 13 #include "components/usb_service/usb_service.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_utils.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 using usb_service::UsbConfigDescriptor; |
| 20 using usb_service::UsbDevice; |
| 21 using usb_service::UsbDeviceHandle; |
| 22 using usb_service::UsbEndpointDescriptor; |
| 23 using usb_service::UsbInterfaceAltSettingDescriptor; |
| 24 using usb_service::UsbInterfaceDescriptor; |
| 25 using usb_service::UsbService; |
| 26 |
| 27 namespace { |
| 28 |
| 29 struct AndroidTraits { |
| 30 static const int kClass = 0xff; |
| 31 static const int kSubclass = 0x42; |
| 32 static const int kProtocol = 0x1; |
| 33 }; |
| 34 |
| 35 struct NonAndroidTraits { |
| 36 static const int kClass = 0xf0; |
| 37 static const int kSubclass = 0x42; |
| 38 static const int kProtocol = 0x2; |
| 39 }; |
| 40 |
| 41 template <class T> |
| 42 class MockUsbInterfaceAltSettingDescriptor |
| 43 : public UsbInterfaceAltSettingDescriptor { |
| 44 public: |
| 45 MockUsbInterfaceAltSettingDescriptor(int interface_number, |
| 46 int alternate_setting) |
| 47 : interface_number_(interface_number), |
| 48 alternate_setting_(alternate_setting) {} |
| 49 |
| 50 virtual size_t GetNumEndpoints() const OVERRIDE { |
| 51 // See IsAndroidInterface function in android_usb_device.cc |
| 52 return 2; |
| 53 } |
| 54 |
| 55 virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint( |
| 56 size_t index) const OVERRIDE { |
| 57 EXPECT_GT(static_cast<size_t>(2), index); |
| 58 return NULL; |
| 59 } |
| 60 |
| 61 virtual int GetInterfaceNumber() const OVERRIDE { return interface_number_; } |
| 62 |
| 63 virtual int GetAlternateSetting() const OVERRIDE { |
| 64 return alternate_setting_; |
| 65 } |
| 66 |
| 67 virtual int GetInterfaceClass() const OVERRIDE { return T::kClass; } |
| 68 |
| 69 virtual int GetInterfaceSubclass() const OVERRIDE { return T::kSubclass; } |
| 70 |
| 71 virtual int GetInterfaceProtocol() const OVERRIDE { return T::kProtocol; } |
| 72 |
| 73 protected: |
| 74 virtual ~MockUsbInterfaceAltSettingDescriptor() {}; |
| 75 |
| 76 private: |
| 77 const int interface_number_; |
| 78 const int alternate_setting_; |
| 79 }; |
| 80 |
| 81 template <class T> |
| 82 class MockUsbInterfaceDescriptor : public UsbInterfaceDescriptor { |
| 83 public: |
| 84 explicit MockUsbInterfaceDescriptor(int interface_number) |
| 85 : interface_number_(interface_number) {} |
| 86 |
| 87 virtual size_t GetNumAltSettings() const OVERRIDE { |
| 88 // See IsAndroidInterface function in android_usb_device.cc |
| 89 return 1; |
| 90 } |
| 91 virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting( |
| 92 size_t index) const OVERRIDE { |
| 93 EXPECT_EQ(static_cast<size_t>(0), index); |
| 94 return new MockUsbInterfaceAltSettingDescriptor<T>(interface_number_, 0); |
| 95 } |
| 96 |
| 97 protected: |
| 98 const int interface_number_; |
| 99 virtual ~MockUsbInterfaceDescriptor() {} |
| 100 }; |
| 101 |
| 102 template <class T> |
| 103 class MockUsbConfigDescriptor : public UsbConfigDescriptor { |
| 104 public: |
| 105 MockUsbConfigDescriptor() {} |
| 106 |
| 107 virtual size_t GetNumInterfaces() const OVERRIDE { return 1; } |
| 108 |
| 109 virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface( |
| 110 size_t index) const OVERRIDE { |
| 111 EXPECT_EQ(static_cast<size_t>(0), index); |
| 112 return new MockUsbInterfaceDescriptor<T>(index); |
| 113 } |
| 114 |
| 115 protected: |
| 116 virtual ~MockUsbConfigDescriptor() {}; |
| 117 }; |
| 118 |
| 119 template <class T> |
| 120 class MockUsbDevice : public UsbDevice { |
| 121 public: |
| 122 MockUsbDevice() : UsbDevice(0, 0, 0) {} |
| 123 |
| 124 virtual ~MockUsbDevice() {} |
| 125 |
| 126 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { return NULL; } |
| 127 |
| 128 virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE { |
| 129 return new MockUsbConfigDescriptor<T>(); |
| 130 } |
| 131 |
| 132 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) { return true; } |
| 133 |
| 134 #if defined(OS_CHROMEOS) |
| 135 // On ChromeOS, if an interface of a claimed device is not claimed, the |
| 136 // permission broker can change the owner of the device so that the unclaimed |
| 137 // interfaces can be used. If this argument is missing, permission broker will |
| 138 // not be used and this method fails if the device is claimed. |
| 139 virtual void RequestUsbAcess( |
| 140 int interface_id, |
| 141 const base::Callback<void(bool success)>& callback) OVERRIDE { |
| 142 callback.Run(true); |
| 143 } |
| 144 #endif // OS_CHROMEOS |
| 145 }; |
| 146 |
| 147 class MockUsbService : public UsbService { |
| 148 public: |
| 149 virtual ~MockUsbService() {} |
| 150 |
| 151 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE { |
| 152 NOTIMPLEMENTED(); |
| 153 return NULL; |
| 154 } |
| 155 |
| 156 virtual void GetDevices( |
| 157 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE { |
| 158 STLClearObject(devices); |
| 159 // This switch should be kept in sync with |
| 160 // AndroidUsbBrowserTest::DeviceCountChanged. |
| 161 switch (step_) { |
| 162 case 0: |
| 163 // No devices. |
| 164 break; |
| 165 case 1: |
| 166 // Android device. |
| 167 devices->push_back(new MockUsbDevice<AndroidTraits>()); |
| 168 break; |
| 169 case 2: |
| 170 // Android and non-android device. |
| 171 devices->push_back(new MockUsbDevice<AndroidTraits>()); |
| 172 devices->push_back(new MockUsbDevice<NonAndroidTraits>()); |
| 173 break; |
| 174 case 3: |
| 175 // Non-android device. |
| 176 devices->push_back(new MockUsbDevice<NonAndroidTraits>()); |
| 177 break; |
| 178 } |
| 179 step_++; |
| 180 } |
| 181 |
| 182 private: |
| 183 int step_ = 0; |
| 184 }; |
| 185 |
| 186 class AndroidUsbBrowserTest |
| 187 : public InProcessBrowserTest, |
| 188 public DevToolsAndroidBridge::DeviceCountListener { |
| 189 protected: |
| 190 AndroidUsbBrowserTest() : step_(0) {} |
| 191 |
| 192 virtual void SetUpOnMainThread() OVERRIDE { |
| 193 scoped_refptr<content::MessageLoopRunner> runner = |
| 194 new content::MessageLoopRunner; |
| 195 |
| 196 BrowserThread::PostTaskAndReply( |
| 197 BrowserThread::FILE, |
| 198 FROM_HERE, |
| 199 base::Bind(&AndroidUsbBrowserTest::SetUpService, this), |
| 200 runner->QuitClosure()); |
| 201 runner->Run(); |
| 202 runner_ = new content::MessageLoopRunner; |
| 203 adb_bridge_ = |
| 204 DevToolsAndroidBridge::Factory::GetForProfile(browser()->profile()); |
| 205 |
| 206 if (!adb_bridge_) |
| 207 FAIL() << "Failed to get DevToolsAndroidBridge."; |
| 208 |
| 209 adb_bridge_->AddDeviceCountListener(this); |
| 210 } |
| 211 |
| 212 void SetUpService() { |
| 213 service_ = new MockUsbService(); |
| 214 UsbService::SetInstanceForTest(service_); |
| 215 } |
| 216 |
| 217 virtual void CleanUpOnMainThread() OVERRIDE { |
| 218 scoped_refptr<content::MessageLoopRunner> runner = |
| 219 new content::MessageLoopRunner; |
| 220 UsbService* service = NULL; |
| 221 BrowserThread::PostTaskAndReply( |
| 222 BrowserThread::FILE, |
| 223 FROM_HERE, |
| 224 base::Bind(&UsbService::SetInstanceForTest, service), |
| 225 runner->QuitClosure()); |
| 226 runner->Run(); |
| 227 } |
| 228 |
| 229 virtual void DeviceCountChanged(int count) OVERRIDE { |
| 230 adb_bridge_->RemoveDeviceCountListener(this); |
| 231 adb_bridge_->AddDeviceCountListener(this); |
| 232 // This switch should be kept in sync with MockUsbService::GetDevice. |
| 233 switch (step_) { |
| 234 case 0: |
| 235 // Check for 0 devices when no devices present. |
| 236 EXPECT_EQ(0, count); |
| 237 break; |
| 238 case 1: |
| 239 // Check for 1 device when only android device present. |
| 240 EXPECT_EQ(1, count); |
| 241 break; |
| 242 case 2: |
| 243 // Check for 1 device when android and non-android devices present. |
| 244 EXPECT_EQ(1, count); |
| 245 break; |
| 246 case 3: |
| 247 // Check for 0 devices when only non-android devices present. |
| 248 EXPECT_EQ(0, count); |
| 249 adb_bridge_->RemoveDeviceCountListener(this); |
| 250 runner_->Quit(); |
| 251 break; |
| 252 default: |
| 253 EXPECT_TRUE(false) << "Unknown step " << step_; |
| 254 } |
| 255 step_++; |
| 256 } |
| 257 |
| 258 int step_; |
| 259 scoped_refptr<content::MessageLoopRunner> runner_; |
| 260 MockUsbService* service_; |
| 261 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; |
| 262 }; |
| 263 |
| 264 } // namespace |
| 265 |
| 266 IN_PROC_BROWSER_TEST_F(AndroidUsbBrowserTest, TestDeviceCounting) { |
| 267 runner_->Run(); |
| 268 } |
OLD | NEW |