| 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/shell/test/shell_apitest.h" | 13 #include "extensions/shell/test/shell_apitest.h" |
| 14 #include "extensions/test/extension_test_message_listener.h" | 14 #include "extensions/test/extension_test_message_listener.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 | 16 |
| 17 using base::ThreadTaskRunnerHandle; | 17 using base::ThreadTaskRunnerHandle; |
| 18 using device::HidCollectionInfo; | 18 using device::HidCollectionInfo; |
| 19 using device::HidConnection; | 19 using device::HidConnection; |
| 20 using device::HidDeviceId; | 20 using device::HidDeviceId; |
| 21 using device::HidDeviceInfo; | 21 using device::HidDeviceInfo; |
| 22 using device::HidService; | 22 using device::HidService; |
| 23 using device::HidUsageAndPage; | 23 using device::HidUsageAndPage; |
| 24 using net::IOBuffer; | 24 using net::IOBuffer; |
| 25 | 25 |
| 26 #if defined(OS_MACOSX) |
| 27 const uint64_t kTestDeviceIds[] = {1, 2, 3, 4, 5}; |
| 28 #else |
| 29 const char* kTestDeviceIds[] = {"A", "B", "C", "D", "E"}; |
| 30 #endif |
| 31 |
| 26 namespace device { | 32 namespace device { |
| 27 | 33 |
| 28 // These report descriptors define two devices with 8-byte input, output and | 34 // These report descriptors define two devices with 8-byte input, output and |
| 29 // feature reports. The first implements usage page 0xFF00 and has a single | 35 // feature reports. The first implements usage page 0xFF00 and has a single |
| 30 // report without and ID. The second implements usage page 0xFF01 and has a | 36 // report without and ID. The second implements usage page 0xFF01 and has a |
| 31 // single report with ID 1. | 37 // single report with ID 1. |
| 32 const uint8 kReportDescriptor[] = {0x06, 0x00, 0xFF, 0x08, 0xA1, 0x01, 0x15, | 38 const uint8 kReportDescriptor[] = {0x06, 0x00, 0xFF, 0x08, 0xA1, 0x01, 0x15, |
| 33 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, | 39 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, |
| 34 0x08, 0x08, 0x81, 0x02, 0x08, 0x91, 0x02, | 40 0x08, 0x08, 0x81, 0x02, 0x08, 0x91, 0x02, |
| 35 0x08, 0xB1, 0x02, 0xC0}; | 41 0x08, 0xB1, 0x02, 0xC0}; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 scoped_refptr<HidConnection> connection; | 130 scoped_refptr<HidConnection> connection; |
| 125 if (device_entry != devices().end()) { | 131 if (device_entry != devices().end()) { |
| 126 connection = new MockHidConnection(device_entry->second); | 132 connection = new MockHidConnection(device_entry->second); |
| 127 } | 133 } |
| 128 | 134 |
| 129 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 135 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 130 base::Bind(callback, connection)); | 136 base::Bind(callback, connection)); |
| 131 } | 137 } |
| 132 | 138 |
| 133 void LazyFirstEnumeration() { | 139 void LazyFirstEnumeration() { |
| 134 AddDevice("A", 0x18D1, 0x58F0, false); | 140 AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, false); |
| 135 AddDevice("B", 0x18D1, 0x58F0, true); | 141 AddDevice(kTestDeviceIds[1], 0x18D1, 0x58F0, true); |
| 136 AddDevice("C", 0x18D1, 0x58F1, false); | 142 AddDevice(kTestDeviceIds[2], 0x18D1, 0x58F1, false); |
| 137 FirstEnumerationComplete(); | 143 FirstEnumerationComplete(); |
| 138 } | 144 } |
| 139 | 145 |
| 140 void AddDevice(const std::string& device_id, | 146 void AddDevice(const HidDeviceId& device_id, |
| 141 int vendor_id, | 147 int vendor_id, |
| 142 int product_id, | 148 int product_id, |
| 143 bool report_id) { | 149 bool report_id) { |
| 144 std::vector<uint8> report_descriptor; | 150 std::vector<uint8> report_descriptor; |
| 145 if (report_id) { | 151 if (report_id) { |
| 146 report_descriptor.insert( | 152 report_descriptor.insert( |
| 147 report_descriptor.begin(), kReportDescriptorWithIDs, | 153 report_descriptor.begin(), kReportDescriptorWithIDs, |
| 148 kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); | 154 kReportDescriptorWithIDs + sizeof(kReportDescriptorWithIDs)); |
| 149 } else { | 155 } else { |
| 150 report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, | 156 report_descriptor.insert(report_descriptor.begin(), kReportDescriptor, |
| 151 kReportDescriptor + sizeof(kReportDescriptor)); | 157 kReportDescriptor + sizeof(kReportDescriptor)); |
| 152 } | 158 } |
| 153 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, | 159 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, |
| 154 "", "", kHIDBusTypeUSB, | 160 "", "", kHIDBusTypeUSB, |
| 155 report_descriptor)); | 161 report_descriptor)); |
| 156 } | 162 } |
| 157 | 163 |
| 158 void RemoveDevice(const std::string& device_id) { | 164 void RemoveDevice(const HidDeviceId& device_id) { |
| 159 HidService::RemoveDevice(device_id); | 165 HidService::RemoveDevice(device_id); |
| 160 } | 166 } |
| 161 }; | 167 }; |
| 162 | 168 |
| 163 } // namespace device | 169 } // namespace device |
| 164 | 170 |
| 165 namespace extensions { | 171 namespace extensions { |
| 166 | 172 |
| 167 class HidApiTest : public ShellApiTest { | 173 class HidApiTest : public ShellApiTest { |
| 168 public: | 174 public: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 183 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { | 189 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { |
| 184 ExtensionTestMessageListener load_listener("loaded", false); | 190 ExtensionTestMessageListener load_listener("loaded", false); |
| 185 ExtensionTestMessageListener result_listener("success", false); | 191 ExtensionTestMessageListener result_listener("success", false); |
| 186 result_listener.set_failure_message("failure"); | 192 result_listener.set_failure_message("failure"); |
| 187 | 193 |
| 188 ASSERT_TRUE(LoadApp("api_test/hid/add_event")); | 194 ASSERT_TRUE(LoadApp("api_test/hid/add_event")); |
| 189 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 195 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 190 | 196 |
| 191 // Add a blocked device first so that the test will fail if a notification is | 197 // Add a blocked device first so that the test will fail if a notification is |
| 192 // received. | 198 // received. |
| 193 hid_service_->AddDevice("D", 0x18D1, 0x58F1, false); | 199 hid_service_->AddDevice(kTestDeviceIds[3], 0x18D1, 0x58F1, false); |
| 194 hid_service_->AddDevice("E", 0x18D1, 0x58F0, false); | 200 hid_service_->AddDevice(kTestDeviceIds[4], 0x18D1, 0x58F0, false); |
| 195 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 201 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 196 EXPECT_EQ("success", result_listener.message()); | 202 EXPECT_EQ("success", result_listener.message()); |
| 197 } | 203 } |
| 198 | 204 |
| 199 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceRemoved) { | 205 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceRemoved) { |
| 200 ExtensionTestMessageListener load_listener("loaded", false); | 206 ExtensionTestMessageListener load_listener("loaded", false); |
| 201 ExtensionTestMessageListener result_listener("success", false); | 207 ExtensionTestMessageListener result_listener("success", false); |
| 202 result_listener.set_failure_message("failure"); | 208 result_listener.set_failure_message("failure"); |
| 203 | 209 |
| 204 ASSERT_TRUE(LoadApp("api_test/hid/remove_event")); | 210 ASSERT_TRUE(LoadApp("api_test/hid/remove_event")); |
| 205 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 211 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 206 | 212 |
| 207 // Device C was not returned by chrome.hid.getDevices, the app will not get | 213 // Device C was not returned by chrome.hid.getDevices, the app will not get |
| 208 // a notification. | 214 // a notification. |
| 209 hid_service_->RemoveDevice("C"); | 215 hid_service_->RemoveDevice(kTestDeviceIds[2]); |
| 210 // Device A was returned, the app will get a notification. | 216 // Device A was returned, the app will get a notification. |
| 211 hid_service_->RemoveDevice("A"); | 217 hid_service_->RemoveDevice(kTestDeviceIds[0]); |
| 212 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 218 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 213 EXPECT_EQ("success", result_listener.message()); | 219 EXPECT_EQ("success", result_listener.message()); |
| 214 } | 220 } |
| 215 | 221 |
| 216 } // namespace extensions | 222 } // namespace extensions |
| OLD | NEW |