Chromium Code Reviews| 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 struct AndroidTraits { | |
| 28 static const int kClass = 0xff; | |
| 29 static const int kSubclass = 0x42; | |
| 30 static const int kProtocol = 0x1; | |
| 31 }; | |
| 32 | |
| 33 struct NonAndroidTraits { | |
| 34 static const int kClass = 0xf0; | |
| 35 static const int kSubclass = 0x42; | |
| 36 static const int kProtocol = 0x2; | |
| 37 }; | |
| 38 | |
| 39 template <class T> | |
| 40 class MockUsbInterfaceAltSettingDescriptor | |
| 41 : public UsbInterfaceAltSettingDescriptor { | |
| 42 public: | |
| 43 MockUsbInterfaceAltSettingDescriptor(int interface_number, | |
| 44 int alternate_setting) | |
| 45 : interface_number_(interface_number), | |
| 46 alternate_setting_(alternate_setting) {} | |
| 47 | |
| 48 virtual size_t GetNumEndpoints() const OVERRIDE { | |
| 49 // See IsAndroidInterface function in android_usb_device.cc | |
| 50 return 2; | |
| 51 } | |
| 52 | |
| 53 virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint( | |
| 54 size_t index) const OVERRIDE { | |
| 55 EXPECT_GT(static_cast<size_t>(2), index); | |
| 56 return NULL; | |
| 57 } | |
| 58 | |
| 59 virtual int GetInterfaceNumber() const OVERRIDE { return interface_number_; } | |
| 60 | |
| 61 virtual int GetAlternateSetting() const OVERRIDE { | |
| 62 return alternate_setting_; | |
| 63 } | |
| 64 | |
| 65 virtual int GetInterfaceClass() const OVERRIDE { return T::kClass; } | |
| 66 | |
| 67 virtual int GetInterfaceSubclass() const OVERRIDE { return T::kSubclass; } | |
| 68 | |
| 69 virtual int GetInterfaceProtocol() const OVERRIDE { return T::kProtocol; } | |
| 70 | |
| 71 protected: | |
| 72 virtual ~MockUsbInterfaceAltSettingDescriptor() {}; | |
| 73 | |
| 74 private: | |
| 75 const int interface_number_; | |
| 76 const int alternate_setting_; | |
| 77 }; | |
| 78 | |
| 79 template <class T> | |
| 80 class MockUsbInterfaceDescriptor : public UsbInterfaceDescriptor { | |
| 81 public: | |
| 82 explicit MockUsbInterfaceDescriptor(int interface_number) | |
| 83 : interface_number_(interface_number) {} | |
| 84 | |
| 85 virtual size_t GetNumAltSettings() const OVERRIDE { | |
| 86 // See IsAndroidInterface function in android_usb_device.cc | |
| 87 return 1; | |
| 88 } | |
| 89 virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting( | |
| 90 size_t index) const OVERRIDE { | |
| 91 EXPECT_EQ(static_cast<size_t>(0), index); | |
| 92 return new MockUsbInterfaceAltSettingDescriptor<T>(interface_number_, 0); | |
| 93 } | |
| 94 | |
| 95 protected: | |
| 96 const int interface_number_; | |
| 97 virtual ~MockUsbInterfaceDescriptor() {} | |
| 98 }; | |
| 99 | |
| 100 template <class T> | |
| 101 class MockUsbConfigDescriptor : public UsbConfigDescriptor { | |
| 102 public: | |
| 103 MockUsbConfigDescriptor() {} | |
| 104 | |
| 105 virtual size_t GetNumInterfaces() const OVERRIDE { return 1; } | |
| 106 | |
| 107 virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface( | |
| 108 size_t index) const OVERRIDE { | |
| 109 EXPECT_EQ(static_cast<size_t>(0), index); | |
| 110 return new MockUsbInterfaceDescriptor<T>(index); | |
| 111 } | |
| 112 | |
| 113 protected: | |
| 114 virtual ~MockUsbConfigDescriptor() {}; | |
| 115 }; | |
| 116 | |
| 117 template <class T> | |
| 118 class MockUsbDevice : public UsbDevice { | |
| 119 public: | |
| 120 MockUsbDevice() : UsbDevice(0, 0, 0) {} | |
| 121 | |
| 122 virtual ~MockUsbDevice() {} | |
| 123 | |
| 124 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { return NULL; } | |
| 125 | |
| 126 virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE { | |
| 127 return new MockUsbConfigDescriptor<T>(); | |
| 128 } | |
| 129 | |
| 130 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) { return true; } | |
| 131 | |
| 132 #if defined(OS_CHROMEOS) | |
| 133 // On ChromeOS, if an interface of a claimed device is not claimed, the | |
| 134 // permission broker can change the owner of the device so that the unclaimed | |
| 135 // interfaces can be used. If this argument is missing, permission broker will | |
| 136 // not be used and this method fails if the device is claimed. | |
| 137 virtual void RequestUsbAcess( | |
| 138 int interface_id, | |
| 139 const base::Callback<void(bool success)>& callback) OVERRIDE { | |
| 140 callback.Run(true); | |
| 141 } | |
| 142 #endif // OS_CHROMEOS | |
| 143 }; | |
| 144 | |
| 145 class MockUsbService : public UsbService { | |
| 146 public: | |
| 147 virtual ~MockUsbService() {} | |
| 148 | |
| 149 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE { | |
| 150 NOTIMPLEMENTED(); | |
| 151 return NULL; | |
| 152 } | |
| 153 | |
| 154 virtual void GetDevices( | |
| 155 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE { | |
| 156 STLClearObject(devices); | |
| 157 | |
| 158 std::copy(devices_.begin(), devices_.end(), back_inserter(*devices)); | |
| 159 } | |
| 160 | |
| 161 std::vector<scoped_refptr<UsbDevice> > devices_; | |
| 162 }; | |
| 163 | |
| 164 class AndroidUsbBrowserTest | |
| 165 : public InProcessBrowserTest, | |
| 166 public DevToolsAndroidBridge::DeviceCountListener { | |
| 167 protected: | |
| 168 AndroidUsbBrowserTest() : step_(0), service_(new MockUsbService()) {} | |
| 169 | |
| 170 void init() { runner_ = new content::MessageLoopRunner; } | |
| 171 | |
| 172 virtual void DeviceCountChanged(int count) OVERRIDE { | |
| 173 switch (step_) { | |
| 174 case 0: | |
| 175 // Check for 0 devices when no devices present | |
| 176 EXPECT_EQ(0, count); | |
| 177 service_->devices_.push_back(new MockUsbDevice<AndroidTraits>()); | |
| 178 break; | |
| 179 case 1: | |
| 180 // Check for 1 device when only android device present | |
| 181 EXPECT_EQ(1, count); | |
| 182 service_->devices_.push_back(new MockUsbDevice<NonAndroidTraits>()); | |
| 183 break; | |
| 184 case 2: | |
| 185 // Check for 1 device when android and non-android devices present | |
| 186 EXPECT_EQ(1, count); | |
| 187 service_->devices_.erase(service_->devices_.begin()); | |
| 188 break; | |
| 189 case 3: | |
| 190 // Check for 0 devices when only non-android devices present | |
| 191 EXPECT_EQ(0, count); | |
| 192 adb_bridge_->RemoveDeviceCountListener(this); | |
| 193 runner_->Quit(); | |
| 194 break; | |
| 195 default: | |
| 196 EXPECT_TRUE(false) << "Unknown step " << step_; | |
| 197 } | |
| 198 step_++; | |
| 199 } | |
| 200 | |
| 201 int step_; | |
| 202 scoped_refptr<content::MessageLoopRunner> runner_; | |
| 203 MockUsbService* service_; | |
| 204 scoped_refptr<DevToolsAndroidBridge> adb_bridge_; | |
| 205 }; | |
| 206 | |
| 207 IN_PROC_BROWSER_TEST_F(AndroidUsbBrowserTest, TestDeviceCounting) { | |
| 208 init(); | |
| 209 | |
| 210 BrowserThread::PostTask( | |
| 211 BrowserThread::FILE, | |
| 212 FROM_HERE, | |
| 213 base::Bind(&UsbService::SetInstanceForTest, service_)); | |
| 214 | |
| 215 adb_bridge_ = | |
| 216 DevToolsAndroidBridge::Factory::GetForProfile(browser()->profile()); | |
| 217 | |
| 218 if (!adb_bridge_) { | |
| 219 FAIL() << "Failed to get DevToolsAndroidBridge."; | |
| 220 } | |
| 221 | |
| 222 adb_bridge_->set_polling_interval_for_test(1); | |
|
Vladislav Kaznacheev
2014/05/12 17:21:08
You can do without this call if you just remove an
Dmitry Zvorygin
2014/05/15 11:35:13
Done.
| |
| 223 adb_bridge_->AddDeviceCountListener(this); | |
| 224 | |
| 225 runner_->Run(); | |
| 226 | |
| 227 adb_bridge_->set_polling_interval_for_test(0); | |
| 228 } | |
| OLD | NEW |