Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: chrome/browser/extensions/api/usb/usb_apitest.cc

Issue 258783002: Extracted UsbService as interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed chromeos issue. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/api/usb/usb_api.h" 5 #include "chrome/browser/extensions/api/usb/usb_api.h"
6 #include "chrome/browser/extensions/extension_apitest.h" 6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/ui/browser.h" 7 #include "chrome/browser/ui/browser.h"
8 #include "components/usb_service/usb_service.h"
8 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/test/test_utils.h"
9 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
10 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
11 13
12 using testing::AnyNumber; 14 using testing::AnyNumber;
13 using testing::_; 15 using testing::_;
14 using testing::Return; 16 using testing::Return;
15 using content::BrowserThread; 17 using content::BrowserThread;
16 using usb_service::UsbConfigDescriptor; 18 using usb_service::UsbConfigDescriptor;
17 using usb_service::UsbDevice; 19 using usb_service::UsbDevice;
18 using usb_service::UsbDeviceHandle; 20 using usb_service::UsbDeviceHandle;
19 using usb_service::UsbEndpointDirection; 21 using usb_service::UsbEndpointDirection;
22 using usb_service::UsbService;
20 using usb_service::UsbTransferCallback; 23 using usb_service::UsbTransferCallback;
21 24
22 namespace { 25 namespace {
23 26
24 ACTION_TEMPLATE(InvokeUsbTransferCallback, 27 ACTION_TEMPLATE(InvokeUsbTransferCallback,
25 HAS_1_TEMPLATE_PARAMS(int, k), 28 HAS_1_TEMPLATE_PARAMS(int, k),
26 AND_1_VALUE_PARAMS(p1)) { 29 AND_1_VALUE_PARAMS(p1)) {
27 ::std::tr1::get<k>(args).Run(p1, new net::IOBuffer(1), 1); 30 ::std::tr1::get<k>(args).Run(p1, new net::IOBuffer(1), 1);
28 } 31 }
29 32
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 const unsigned int timeout, const UsbTransferCallback& callback)); 64 const unsigned int timeout, const UsbTransferCallback& callback));
62 65
63 MOCK_METHOD0(ResetDevice, bool()); 66 MOCK_METHOD0(ResetDevice, bool());
64 67
65 void set_device(UsbDevice* device) { device_ = device; } 68 void set_device(UsbDevice* device) { device_ = device; }
66 69
67 protected: 70 protected:
68 virtual ~MockUsbDeviceHandle() {} 71 virtual ~MockUsbDeviceHandle() {}
69 }; 72 };
70 73
74 class MockUsbConfigDescriptor : public UsbConfigDescriptor {
75 public:
76 MOCK_CONST_METHOD0(GetNumInterfaces, size_t());
77
78 protected:
79 virtual ~MockUsbConfigDescriptor() {}
80 };
81
71 class MockUsbDevice : public UsbDevice { 82 class MockUsbDevice : public UsbDevice {
72 public: 83 public:
73 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) 84 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle)
74 : UsbDevice(), 85 : UsbDevice(),
75 mock_handle_(mock_handle) { 86 mock_handle_(mock_handle) {
76 mock_handle->set_device(this); 87 mock_handle->set_device(this);
77 } 88 }
78 89
79 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { 90 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE {
80 return mock_handle_; 91 return mock_handle_;
81 } 92 }
82 93
83 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { 94 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE {
84 EXPECT_TRUE(false) << "Should not be reached"; 95 EXPECT_TRUE(false) << "Should not be reached";
85 return false; 96 return false;
86 } 97 }
87 98
99 #if defined(OS_CHROMEOS)
100 virtual void RequestUsbAcess(
101 int interface_id,
102 const base::Callback<void(bool success)>& callback) OVERRIDE {
103 BrowserThread::PostTask(
104 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true));
105 }
106 #endif // OS_CHROMEOS
107
88 MOCK_METHOD0(ListInterfaces, scoped_refptr<UsbConfigDescriptor>()); 108 MOCK_METHOD0(ListInterfaces, scoped_refptr<UsbConfigDescriptor>());
89 109
90 private: 110 private:
91 MockUsbDeviceHandle* mock_handle_; 111 MockUsbDeviceHandle* mock_handle_;
92 virtual ~MockUsbDevice() {} 112 virtual ~MockUsbDevice() {}
93 }; 113 };
94 114
115 class MockUsbService : public UsbService {
116 public:
117 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
118
119 protected:
120 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE {
121 EXPECT_EQ(unique_id, 0U);
122 return device_;
123 }
124
125 virtual void GetDevices(
126 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE {
127 STLClearObject(devices);
128 devices->push_back(device_);
129 }
130
131 scoped_refptr<UsbDevice> device_;
132 };
133
95 #if defined(OS_WIN) 134 #if defined(OS_WIN)
96 #pragma warning(pop) 135 #pragma warning(pop)
97 #endif 136 #endif
98 137
99 class UsbApiTest : public ExtensionApiTest { 138 class UsbApiTest : public ExtensionApiTest {
100 public: 139 public:
101 virtual void SetUpOnMainThread() OVERRIDE { 140 virtual void SetUpOnMainThread() OVERRIDE {
102 mock_device_handle_ = new MockUsbDeviceHandle(); 141 mock_device_handle_ = new MockUsbDeviceHandle();
103 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); 142 mock_device_ = new MockUsbDevice(mock_device_handle_.get());
104 extensions::UsbGetDevicesFunction::SetDeviceForTest(mock_device_.get()); 143 scoped_refptr<content::MessageLoopRunner> runner =
144 new content::MessageLoopRunner;
145 BrowserThread::PostTaskAndReply(BrowserThread::FILE,
146 FROM_HERE,
147 base::Bind(&UsbApiTest::SetUpService, this),
148 runner->QuitClosure());
149 runner->Run();
150 }
151
152 void SetUpService() {
153 UsbService::SetInstanceForTest(new MockUsbService(mock_device_));
154 }
155
156 virtual void CleanUpOnMainThread() OVERRIDE {
157 scoped_refptr<content::MessageLoopRunner> runner =
158 new content::MessageLoopRunner;
159 UsbService* service = NULL;
160 BrowserThread::PostTaskAndReply(
161 BrowserThread::FILE,
162 FROM_HERE,
163 base::Bind(&UsbService::SetInstanceForTest, service),
164 runner->QuitClosure());
165 runner->Run();
105 } 166 }
106 167
107 protected: 168 protected:
108 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 169 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
109 scoped_refptr<MockUsbDevice> mock_device_; 170 scoped_refptr<MockUsbDevice> mock_device_;
110 }; 171 };
111 172
112 } // namespace 173 } // namespace
113 174
114 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 175 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
115 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); 176 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4);
116 ASSERT_TRUE(RunExtensionTest("usb/device_handling")); 177 ASSERT_TRUE(RunExtensionTest("usb/device_handling"));
117 } 178 }
118 179
119 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 180 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
120 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 181 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
121 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 182 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice())
122 .WillOnce(Return(true)) 183 .WillOnce(Return(true))
123 .WillOnce(Return(false)); 184 .WillOnce(Return(false));
124 EXPECT_CALL( 185 EXPECT_CALL(
125 *mock_device_handle_.get(), 186 *mock_device_handle_.get(),
126 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 187 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
127 .WillOnce( 188 .WillOnce(
128 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); 189 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED));
129 ASSERT_TRUE(RunExtensionTest("usb/reset_device")); 190 ASSERT_TRUE(RunExtensionTest("usb/reset_device"));
130 } 191 }
131 192
132 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 193 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
194 scoped_refptr<MockUsbConfigDescriptor> mock_descriptor =
195 new MockUsbConfigDescriptor();
196 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
197 EXPECT_CALL(*mock_descriptor.get(), GetNumInterfaces()).WillOnce(Return(0));
133 EXPECT_CALL(*mock_device_.get(), ListInterfaces()) 198 EXPECT_CALL(*mock_device_.get(), ListInterfaces())
134 .WillOnce(Return(scoped_refptr<UsbConfigDescriptor>())); 199 .WillOnce(Return(mock_descriptor));
135 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
136 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); 200 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces"));
137 } 201 }
138 202
139 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { 203 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
140 EXPECT_CALL(*mock_device_handle_.get(), 204 EXPECT_CALL(*mock_device_handle_.get(),
141 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND, 205 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND,
142 UsbDeviceHandle::STANDARD, 206 UsbDeviceHandle::STANDARD,
143 UsbDeviceHandle::DEVICE, 207 UsbDeviceHandle::DEVICE,
144 1, 2, 3, _, 1, _, _)) 208 1, 2, 3, _, 1, _, _))
145 .WillOnce( 209 .WillOnce(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 .WillOnce( 242 .WillOnce(
179 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT)); 243 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT));
180 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 244 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
181 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); 245 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure"));
182 } 246 }
183 247
184 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { 248 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
185 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 249 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
186 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); 250 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer"));
187 } 251 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/usb/usb_api.cc ('k') | chrome/test/data/extensions/api_test/usb/device_handling/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698