| 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 "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
| 6 #include "chrome/browser/ui/browser.h" | 6 #include "chrome/browser/ui/browser.h" |
| 7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/test/test_utils.h" | 8 #include "content/public/test/test_utils.h" |
| 9 #include "device/usb/usb_service.h" | 9 #include "device/usb/usb_service.h" |
| 10 #include "extensions/browser/api/usb/usb_api.h" | 10 #include "extensions/browser/api/usb/usb_api.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 const unsigned int timeout, | 86 const unsigned int timeout, |
| 87 const UsbTransferCallback& callback)); | 87 const UsbTransferCallback& callback)); |
| 88 | 88 |
| 89 MOCK_METHOD0(ResetDevice, bool()); | 89 MOCK_METHOD0(ResetDevice, bool()); |
| 90 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); | 90 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); |
| 91 MOCK_METHOD1(ClaimInterface, bool(const int interface_number)); | 91 MOCK_METHOD1(ClaimInterface, bool(const int interface_number)); |
| 92 MOCK_METHOD1(ReleaseInterface, bool(const int interface_number)); | 92 MOCK_METHOD1(ReleaseInterface, bool(const int interface_number)); |
| 93 MOCK_METHOD2(SetInterfaceAlternateSetting, | 93 MOCK_METHOD2(SetInterfaceAlternateSetting, |
| 94 bool(const int interface_number, const int alternate_setting)); | 94 bool(const int interface_number, const int alternate_setting)); |
| 95 | 95 |
| 96 virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE { | 96 virtual scoped_refptr<UsbDevice> GetDevice() const override { |
| 97 return device_; | 97 return device_; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void set_device(UsbDevice* device) { device_ = device; } | 100 void set_device(UsbDevice* device) { device_ = device; } |
| 101 | 101 |
| 102 protected: | 102 protected: |
| 103 UsbDevice* device_; | 103 UsbDevice* device_; |
| 104 | 104 |
| 105 virtual ~MockUsbDeviceHandle() {} | 105 virtual ~MockUsbDeviceHandle() {} |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 class MockUsbDevice : public UsbDevice { | 108 class MockUsbDevice : public UsbDevice { |
| 109 public: | 109 public: |
| 110 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) | 110 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) |
| 111 : UsbDevice(0, 0, 0), mock_handle_(mock_handle) { | 111 : UsbDevice(0, 0, 0), mock_handle_(mock_handle) { |
| 112 mock_handle->set_device(this); | 112 mock_handle->set_device(this); |
| 113 } | 113 } |
| 114 | 114 |
| 115 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | 115 virtual scoped_refptr<UsbDeviceHandle> Open() override { |
| 116 return mock_handle_; | 116 return mock_handle_; |
| 117 } | 117 } |
| 118 | 118 |
| 119 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 119 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) override { |
| 120 EXPECT_TRUE(false) << "Should not be reached"; | 120 EXPECT_TRUE(false) << "Should not be reached"; |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 | 123 |
| 124 #if defined(OS_CHROMEOS) | 124 #if defined(OS_CHROMEOS) |
| 125 virtual void RequestUsbAccess( | 125 virtual void RequestUsbAccess( |
| 126 int interface_id, | 126 int interface_id, |
| 127 const base::Callback<void(bool success)>& callback) OVERRIDE { | 127 const base::Callback<void(bool success)>& callback) override { |
| 128 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 129 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true)); | 129 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true)); |
| 130 } | 130 } |
| 131 #endif // OS_CHROMEOS | 131 #endif // OS_CHROMEOS |
| 132 | 132 |
| 133 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); | 133 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); |
| 134 MOCK_METHOD1(GetManufacturer, bool(base::string16* manufacturer)); | 134 MOCK_METHOD1(GetManufacturer, bool(base::string16* manufacturer)); |
| 135 MOCK_METHOD1(GetProduct, bool(base::string16* product)); | 135 MOCK_METHOD1(GetProduct, bool(base::string16* product)); |
| 136 MOCK_METHOD1(GetSerialNumber, bool(base::string16* serial_number)); | 136 MOCK_METHOD1(GetSerialNumber, bool(base::string16* serial_number)); |
| 137 | 137 |
| 138 private: | 138 private: |
| 139 MockUsbDeviceHandle* mock_handle_; | 139 MockUsbDeviceHandle* mock_handle_; |
| 140 virtual ~MockUsbDevice() {} | 140 virtual ~MockUsbDevice() {} |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 class MockUsbService : public UsbService { | 143 class MockUsbService : public UsbService { |
| 144 public: | 144 public: |
| 145 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} | 145 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} |
| 146 | 146 |
| 147 protected: | 147 protected: |
| 148 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE { | 148 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { |
| 149 EXPECT_EQ(unique_id, 0U); | 149 EXPECT_EQ(unique_id, 0U); |
| 150 return device_; | 150 return device_; |
| 151 } | 151 } |
| 152 | 152 |
| 153 virtual void GetDevices( | 153 virtual void GetDevices( |
| 154 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE { | 154 std::vector<scoped_refptr<UsbDevice> >* devices) override { |
| 155 STLClearObject(devices); | 155 STLClearObject(devices); |
| 156 devices->push_back(device_); | 156 devices->push_back(device_); |
| 157 } | 157 } |
| 158 | 158 |
| 159 scoped_refptr<UsbDevice> device_; | 159 scoped_refptr<UsbDevice> device_; |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 #if defined(OS_WIN) | 162 #if defined(OS_WIN) |
| 163 #pragma warning(pop) | 163 #pragma warning(pop) |
| 164 #endif | 164 #endif |
| 165 | 165 |
| 166 class UsbApiTest : public ExtensionApiTest { | 166 class UsbApiTest : public ExtensionApiTest { |
| 167 public: | 167 public: |
| 168 virtual void SetUpOnMainThread() OVERRIDE { | 168 virtual void SetUpOnMainThread() override { |
| 169 mock_device_handle_ = new MockUsbDeviceHandle(); | 169 mock_device_handle_ = new MockUsbDeviceHandle(); |
| 170 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); | 170 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); |
| 171 scoped_refptr<content::MessageLoopRunner> runner = | 171 scoped_refptr<content::MessageLoopRunner> runner = |
| 172 new content::MessageLoopRunner; | 172 new content::MessageLoopRunner; |
| 173 BrowserThread::PostTaskAndReply(BrowserThread::FILE, | 173 BrowserThread::PostTaskAndReply(BrowserThread::FILE, |
| 174 FROM_HERE, | 174 FROM_HERE, |
| 175 base::Bind(&UsbApiTest::SetUpService, this), | 175 base::Bind(&UsbApiTest::SetUpService, this), |
| 176 runner->QuitClosure()); | 176 runner->QuitClosure()); |
| 177 runner->Run(); | 177 runner->Run(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void SetUpService() { | 180 void SetUpService() { |
| 181 UsbService::SetInstanceForTest(new MockUsbService(mock_device_)); | 181 UsbService::SetInstanceForTest(new MockUsbService(mock_device_)); |
| 182 } | 182 } |
| 183 | 183 |
| 184 virtual void TearDownOnMainThread() OVERRIDE { | 184 virtual void TearDownOnMainThread() override { |
| 185 scoped_refptr<content::MessageLoopRunner> runner = | 185 scoped_refptr<content::MessageLoopRunner> runner = |
| 186 new content::MessageLoopRunner; | 186 new content::MessageLoopRunner; |
| 187 UsbService* service = NULL; | 187 UsbService* service = NULL; |
| 188 BrowserThread::PostTaskAndReply( | 188 BrowserThread::PostTaskAndReply( |
| 189 BrowserThread::FILE, | 189 BrowserThread::FILE, |
| 190 FROM_HERE, | 190 FROM_HERE, |
| 191 base::Bind(&UsbService::SetInstanceForTest, service), | 191 base::Bind(&UsbService::SetInstanceForTest, service), |
| 192 runner->QuitClosure()); | 192 runner->QuitClosure()); |
| 193 runner->Run(); | 193 runner->Run(); |
| 194 } | 194 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR)) | 264 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR)) |
| 265 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); | 265 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); |
| 266 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 266 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
| 267 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); | 267 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); |
| 268 } | 268 } |
| 269 | 269 |
| 270 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { | 270 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { |
| 271 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 271 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
| 272 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); | 272 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); |
| 273 } | 273 } |
| OLD | NEW |