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

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 style in manifests. Created 6 years, 8 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
88 MOCK_METHOD0(ListInterfaces, scoped_refptr<UsbConfigDescriptor>()); 99 MOCK_METHOD0(ListInterfaces, scoped_refptr<UsbConfigDescriptor>());
89 100
90 private: 101 private:
91 MockUsbDeviceHandle* mock_handle_; 102 MockUsbDeviceHandle* mock_handle_;
92 virtual ~MockUsbDevice() {} 103 virtual ~MockUsbDevice() {}
93 }; 104 };
94 105
106 class MockUsbService : public UsbService {
107 public:
108 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
109
110 protected:
111 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE {
112 EXPECT_EQ(unique_id, 0U);
113 return device_;
114 }
115
116 virtual void GetDevices(
117 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE {
118 STLClearObject(devices);
119 devices->push_back(device_);
120 }
121
122 scoped_refptr<UsbDevice> device_;
123 };
124
95 #if defined(OS_WIN) 125 #if defined(OS_WIN)
96 #pragma warning(pop) 126 #pragma warning(pop)
97 #endif 127 #endif
98 128
99 class UsbApiTest : public ExtensionApiTest { 129 class UsbApiTest : public ExtensionApiTest {
100 public: 130 public:
101 virtual void SetUpOnMainThread() OVERRIDE { 131 virtual void SetUpOnMainThread() OVERRIDE {
102 mock_device_handle_ = new MockUsbDeviceHandle(); 132 mock_device_handle_ = new MockUsbDeviceHandle();
103 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); 133 mock_device_ = new MockUsbDevice(mock_device_handle_.get());
104 extensions::UsbGetDevicesFunction::SetDeviceForTest(mock_device_.get()); 134 scoped_refptr<content::MessageLoopRunner> runner =
135 new content::MessageLoopRunner;
136 BrowserThread::PostTaskAndReply(BrowserThread::FILE,
137 FROM_HERE,
138 base::Bind(&UsbApiTest::SetUpService, this),
139 runner->QuitClosure());
140 runner->Run();
141 }
142
143 void SetUpService() {
144 UsbService::SetInstanceForTest(new MockUsbService(mock_device_));
145 }
146
147 virtual void CleanUpOnMainThread() OVERRIDE {
148 scoped_refptr<content::MessageLoopRunner> runner =
149 new content::MessageLoopRunner;
150 UsbService* service = NULL;
151 BrowserThread::PostTaskAndReply(
152 BrowserThread::FILE,
153 FROM_HERE,
154 base::Bind(&UsbService::SetInstanceForTest, service),
155 runner->QuitClosure());
156 runner->Run();
105 } 157 }
106 158
107 protected: 159 protected:
108 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 160 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
109 scoped_refptr<MockUsbDevice> mock_device_; 161 scoped_refptr<MockUsbDevice> mock_device_;
110 }; 162 };
111 163
112 } // namespace 164 } // namespace
113 165
114 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 166 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
115 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); 167 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4);
116 ASSERT_TRUE(RunExtensionTest("usb/device_handling")); 168 ASSERT_TRUE(RunExtensionTest("usb/device_handling"));
117 } 169 }
118 170
119 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 171 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
120 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 172 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
121 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 173 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice())
122 .WillOnce(Return(true)) 174 .WillOnce(Return(true))
123 .WillOnce(Return(false)); 175 .WillOnce(Return(false));
124 EXPECT_CALL( 176 EXPECT_CALL(
125 *mock_device_handle_.get(), 177 *mock_device_handle_.get(),
126 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 178 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
127 .WillOnce( 179 .WillOnce(
128 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); 180 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED));
129 ASSERT_TRUE(RunExtensionTest("usb/reset_device")); 181 ASSERT_TRUE(RunExtensionTest("usb/reset_device"));
130 } 182 }
131 183
132 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 184 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
185 scoped_refptr<MockUsbConfigDescriptor> mock_descriptor =
186 new MockUsbConfigDescriptor();
187 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
188 EXPECT_CALL(*mock_descriptor.get(), GetNumInterfaces()).WillOnce(Return(0));
133 EXPECT_CALL(*mock_device_.get(), ListInterfaces()) 189 EXPECT_CALL(*mock_device_.get(), ListInterfaces())
134 .WillOnce(Return(scoped_refptr<UsbConfigDescriptor>())); 190 .WillOnce(Return(mock_descriptor));
135 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
136 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); 191 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces"));
137 } 192 }
138 193
139 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { 194 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
140 EXPECT_CALL(*mock_device_handle_.get(), 195 EXPECT_CALL(*mock_device_handle_.get(),
141 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND, 196 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND,
142 UsbDeviceHandle::STANDARD, 197 UsbDeviceHandle::STANDARD,
143 UsbDeviceHandle::DEVICE, 198 UsbDeviceHandle::DEVICE,
144 1, 2, 3, _, 1, _, _)) 199 1, 2, 3, _, 1, _, _))
145 .WillOnce( 200 .WillOnce(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 .WillOnce( 233 .WillOnce(
179 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT)); 234 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT));
180 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 235 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
181 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); 236 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure"));
182 } 237 }
183 238
184 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { 239 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
185 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 240 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
186 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); 241 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer"));
187 } 242 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698