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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
8 #include "device/hid/hid_collection_info.h" | 8 #include "device/hid/hid_collection_info.h" |
9 #include "device/hid/hid_connection.h" | 9 #include "device/hid/hid_connection.h" |
10 #include "device/hid/hid_device_info.h" | 10 #include "device/hid/hid_device_info.h" |
11 #include "device/hid/hid_service.h" | 11 #include "device/hid/hid_service.h" |
12 #include "device/hid/hid_usage_and_page.h" | 12 #include "device/hid/hid_usage_and_page.h" |
13 #include "extensions/browser/api/device_permissions_prompt.h" | |
14 #include "extensions/shell/browser/shell_extensions_api_client.h" | |
13 #include "extensions/shell/test/shell_apitest.h" | 15 #include "extensions/shell/test/shell_apitest.h" |
14 #include "extensions/test/extension_test_message_listener.h" | 16 #include "extensions/test/extension_test_message_listener.h" |
15 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
16 | 18 |
17 using base::ThreadTaskRunnerHandle; | 19 using base::ThreadTaskRunnerHandle; |
18 using device::HidCollectionInfo; | 20 using device::HidCollectionInfo; |
19 using device::HidConnection; | 21 using device::HidConnection; |
20 using device::HidDeviceId; | 22 using device::HidDeviceId; |
21 using device::HidDeviceInfo; | 23 using device::HidDeviceInfo; |
22 using device::HidService; | 24 using device::HidService; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 std::vector<uint8> report_descriptor; | 152 std::vector<uint8> report_descriptor; |
151 if (report_id) { | 153 if (report_id) { |
152 report_descriptor.insert( | 154 report_descriptor.insert( |
153 report_descriptor.begin(), kReportDescriptorWithIDs, | 155 report_descriptor.begin(), kReportDescriptorWithIDs, |
154 kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); | 156 kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); |
155 } else { | 157 } else { |
156 report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, | 158 report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, |
157 kReportDescriptor + sizeof(kReportDescriptor)); | 159 kReportDescriptor + sizeof(kReportDescriptor)); |
158 } | 160 } |
159 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, | 161 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, |
160 "", "", kHIDBusTypeUSB, | 162 "Test Device", "", kHIDBusTypeUSB, |
161 report_descriptor)); | 163 report_descriptor)); |
162 } | 164 } |
163 | 165 |
164 void RemoveDevice(const HidDeviceId& device_id) { | 166 void RemoveDevice(const HidDeviceId& device_id) { |
165 HidService::RemoveDevice(device_id); | 167 HidService::RemoveDevice(device_id); |
166 } | 168 } |
167 }; | 169 }; |
168 | 170 |
169 } // namespace device | 171 } // namespace device |
170 | 172 |
171 namespace extensions { | 173 namespace extensions { |
172 | 174 |
175 class TestDevicePermissionsPrompt | |
176 : public DevicePermissionsPrompt, | |
177 public DevicePermissionsPrompt::Prompt::Observer { | |
178 public: | |
179 TestDevicePermissionsPrompt(content::WebContents* web_contents) | |
180 : DevicePermissionsPrompt(web_contents) {} | |
181 | |
182 ~TestDevicePermissionsPrompt() override { prompt()->SetObserver(nullptr); } | |
183 | |
184 void ShowDialog() override { prompt()->SetObserver(this); } | |
185 | |
186 void OnDevicesChanged() override { | |
187 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) { | |
188 prompt()->GrantDevicePermission(i); | |
189 if (!prompt()->multiple()) { | |
190 break; | |
191 } | |
192 } | |
193 prompt()->Dismissed(); | |
194 } | |
195 }; | |
196 | |
197 class TestExtensionsAPIClient : public ShellExtensionsAPIClient { | |
198 public: | |
199 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} | |
200 | |
201 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( | |
202 content::WebContents* web_contents) const override { | |
203 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); | |
204 } | |
205 }; | |
206 | |
173 class HidApiTest : public ShellApiTest { | 207 class HidApiTest : public ShellApiTest { |
174 public: | 208 public: |
175 void SetUpOnMainThread() override { | 209 void SetUpOnMainThread() override { |
176 ShellApiTest::SetUpOnMainThread(); | 210 ShellApiTest::SetUpOnMainThread(); |
177 hid_service_ = new device::MockHidService(); | 211 hid_service_ = new device::MockHidService(); |
178 HidService::SetInstanceForTest(hid_service_); | 212 HidService::SetInstanceForTest(hid_service_); |
179 } | 213 } |
180 | 214 |
181 protected: | 215 protected: |
182 device::MockHidService* hid_service_; | 216 device::MockHidService* hid_service_; |
(...skipping 29 matching lines...) Expand all Loading... | |
212 | 246 |
213 // Device C was not returned by chrome.hid.getDevices, the app will not get | 247 // Device C was not returned by chrome.hid.getDevices, the app will not get |
214 // a notification. | 248 // a notification. |
215 hid_service_->RemoveDevice(kTestDeviceIds[2]); | 249 hid_service_->RemoveDevice(kTestDeviceIds[2]); |
216 // Device A was returned, the app will get a notification. | 250 // Device A was returned, the app will get a notification. |
217 hid_service_->RemoveDevice(kTestDeviceIds[0]); | 251 hid_service_->RemoveDevice(kTestDeviceIds[0]); |
218 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 252 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
219 EXPECT_EQ("success", result_listener.message()); | 253 EXPECT_EQ("success", result_listener.message()); |
220 } | 254 } |
221 | 255 |
256 IN_PROC_BROWSER_TEST_F(HidApiTest, GetUserSelectedDevices) { | |
257 ExtensionTestMessageListener open_listener("opened_device", false); | |
258 | |
259 TestExtensionsAPIClient test_api_client; | |
260 ASSERT_TRUE(LoadApp("api_test/hid/get_user_selected_devices")); | |
261 ASSERT_TRUE(open_listener.WaitUntilSatisfied()); | |
262 | |
263 ExtensionTestMessageListener remove_listener("removed", false); | |
264 hid_service_->RemoveDevice(kTestDeviceIds[0]); | |
265 ASSERT_TRUE(remove_listener.WaitUntilSatisfied()); | |
266 | |
267 // TODO(reillyg): Enable this part of the test when crbug.com/483328 is fixed. | |
Devlin
2015/05/05 22:19:50
according to the bug, it is fixed?
Reilly Grant (use Gerrit)
2015/05/06 00:52:53
I sent out this review as I was landing the patch.
| |
268 // ExtensionTestMessageListener add_listener("added", false); | |
269 // hid_service_->AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, true); | |
270 // ASSERT_TRUE(add_listener.WaitUntilSatisfied()); | |
271 } | |
272 | |
222 } // namespace extensions | 273 } // namespace extensions |
OLD | NEW |