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 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 using base::ThreadTaskRunnerHandle; | 17 using base::ThreadTaskRunnerHandle; |
22 using device::HidCollectionInfo; | 18 using device::HidCollectionInfo; |
23 using device::HidConnection; | 19 using device::HidConnection; |
24 using device::HidDeviceId; | 20 using device::HidDeviceId; |
25 using device::HidDeviceInfo; | 21 using device::HidDeviceInfo; |
26 using device::HidService; | 22 using device::HidService; |
27 using device::HidUsageAndPage; | 23 using device::HidUsageAndPage; |
28 using net::IOBuffer; | 24 using net::IOBuffer; |
29 | 25 |
| 26 namespace device { |
| 27 |
30 class MockHidConnection : public HidConnection { | 28 class MockHidConnection : public HidConnection { |
31 public: | 29 public: |
32 MockHidConnection(const HidDeviceInfo& device_info) | 30 MockHidConnection(scoped_refptr<HidDeviceInfo> device_info) |
33 : HidConnection(device_info) {} | 31 : HidConnection(device_info) {} |
34 | 32 |
35 void PlatformClose() override {} | 33 void PlatformClose() override {} |
36 | 34 |
37 void PlatformRead(const ReadCallback& callback) override { | 35 void PlatformRead(const ReadCallback& callback) override { |
38 const char kResult[] = "This is a HID input report."; | 36 const char kResult[] = "This is a HID input report."; |
39 uint8_t report_id = device_info().has_report_id ? 1 : 0; | 37 uint8_t report_id = device_info()->has_report_id() ? 1 : 0; |
40 scoped_refptr<IOBuffer> buffer(new IOBuffer(sizeof(kResult))); | 38 scoped_refptr<IOBuffer> buffer(new IOBuffer(sizeof(kResult))); |
41 buffer->data()[0] = report_id; | 39 buffer->data()[0] = report_id; |
42 memcpy(buffer->data() + 1, kResult, sizeof(kResult) - 1); | 40 memcpy(buffer->data() + 1, kResult, sizeof(kResult) - 1); |
43 ThreadTaskRunnerHandle::Get()->PostTask( | 41 ThreadTaskRunnerHandle::Get()->PostTask( |
44 FROM_HERE, base::Bind(callback, true, buffer, sizeof(kResult))); | 42 FROM_HERE, base::Bind(callback, true, buffer, sizeof(kResult))); |
45 } | 43 } |
46 | 44 |
47 void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, | 45 void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, |
48 size_t size, | 46 size_t size, |
49 const WriteCallback& callback) override { | 47 const WriteCallback& callback) override { |
50 const char kExpected[] = "This is a HID output report."; | 48 const char kExpected[] = "This is a HID output report."; |
51 bool result = false; | 49 bool result = false; |
52 if (size == sizeof(kExpected)) { | 50 if (size == sizeof(kExpected)) { |
53 uint8_t report_id = buffer->data()[0]; | 51 uint8_t report_id = buffer->data()[0]; |
54 uint8_t expected_report_id = device_info().has_report_id ? 1 : 0; | 52 uint8_t expected_report_id = device_info()->has_report_id() ? 1 : 0; |
55 if (report_id == expected_report_id) { | 53 if (report_id == expected_report_id) { |
56 if (memcmp(buffer->data() + 1, kExpected, sizeof(kExpected) - 1) == 0) { | 54 if (memcmp(buffer->data() + 1, kExpected, sizeof(kExpected) - 1) == 0) { |
57 result = true; | 55 result = true; |
58 } | 56 } |
59 } | 57 } |
60 } | 58 } |
61 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 59 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
62 base::Bind(callback, result)); | 60 base::Bind(callback, result)); |
63 } | 61 } |
64 | 62 |
65 void PlatformGetFeatureReport(uint8_t report_id, | 63 void PlatformGetFeatureReport(uint8_t report_id, |
66 const ReadCallback& callback) override { | 64 const ReadCallback& callback) override { |
67 const char kResult[] = "This is a HID feature report."; | 65 const char kResult[] = "This is a HID feature report."; |
68 scoped_refptr<IOBuffer> buffer(new IOBuffer(sizeof(kResult))); | 66 scoped_refptr<IOBuffer> buffer(new IOBuffer(sizeof(kResult))); |
69 size_t offset = 0; | 67 size_t offset = 0; |
70 if (device_info().has_report_id) { | 68 if (device_info()->has_report_id()) { |
71 buffer->data()[offset++] = report_id; | 69 buffer->data()[offset++] = report_id; |
72 } | 70 } |
73 memcpy(buffer->data() + offset, kResult, sizeof(kResult) - 1); | 71 memcpy(buffer->data() + offset, kResult, sizeof(kResult) - 1); |
74 ThreadTaskRunnerHandle::Get()->PostTask( | 72 ThreadTaskRunnerHandle::Get()->PostTask( |
75 FROM_HERE, | 73 FROM_HERE, |
76 base::Bind(callback, true, buffer, sizeof(kResult) - 1 + offset)); | 74 base::Bind(callback, true, buffer, sizeof(kResult) - 1 + offset)); |
77 } | 75 } |
78 | 76 |
79 void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 77 void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
80 size_t size, | 78 size_t size, |
81 const WriteCallback& callback) override { | 79 const WriteCallback& callback) override { |
82 const char kExpected[] = "The app is setting this HID feature report."; | 80 const char kExpected[] = "The app is setting this HID feature report."; |
83 bool result = false; | 81 bool result = false; |
84 if (size == sizeof(kExpected)) { | 82 if (size == sizeof(kExpected)) { |
85 uint8_t report_id = buffer->data()[0]; | 83 uint8_t report_id = buffer->data()[0]; |
86 uint8_t expected_report_id = device_info().has_report_id ? 1 : 0; | 84 uint8_t expected_report_id = device_info()->has_report_id() ? 1 : 0; |
87 if (report_id == expected_report_id && | 85 if (report_id == expected_report_id && |
88 memcmp(buffer->data() + 1, kExpected, sizeof(kExpected) - 1) == 0) { | 86 memcmp(buffer->data() + 1, kExpected, sizeof(kExpected) - 1) == 0) { |
89 result = true; | 87 result = true; |
90 } | 88 } |
91 } | 89 } |
92 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 90 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
93 base::Bind(callback, result)); | 91 base::Bind(callback, result)); |
94 } | 92 } |
95 | 93 |
96 private: | 94 private: |
(...skipping 26 matching lines...) Expand all Loading... |
123 AddDevice("A", 0x18D1, 0x58F0, false); | 121 AddDevice("A", 0x18D1, 0x58F0, false); |
124 AddDevice("B", 0x18D1, 0x58F0, true); | 122 AddDevice("B", 0x18D1, 0x58F0, true); |
125 AddDevice("C", 0x18D1, 0x58F1, false); | 123 AddDevice("C", 0x18D1, 0x58F1, false); |
126 FirstEnumerationComplete(); | 124 FirstEnumerationComplete(); |
127 } | 125 } |
128 | 126 |
129 void AddDevice(const std::string& device_id, | 127 void AddDevice(const std::string& device_id, |
130 int vendor_id, | 128 int vendor_id, |
131 int product_id, | 129 int product_id, |
132 bool report_id) { | 130 bool report_id) { |
133 HidDeviceInfo device_info; | 131 scoped_refptr<HidDeviceInfo> device_info(new HidDeviceInfo()); |
134 device_info.device_id = device_id; | 132 device_info->device_id_ = device_id; |
135 device_info.vendor_id = vendor_id; | 133 device_info->vendor_id_ = vendor_id; |
136 device_info.product_id = product_id; | 134 device_info->product_id_ = product_id; |
137 device_info.max_input_report_size = 128; | 135 device_info->max_input_report_size_ = 128; |
138 device_info.max_output_report_size = 128; | 136 device_info->max_output_report_size_ = 128; |
139 device_info.max_feature_report_size = 128; | 137 device_info->max_feature_report_size_ = 128; |
140 { | 138 { |
141 HidCollectionInfo collection_info; | 139 HidCollectionInfo collection_info; |
142 if (report_id) { | 140 if (report_id) { |
143 collection_info.usage = | 141 collection_info.usage = |
144 HidUsageAndPage(0, HidUsageAndPage::kPageVendor); | 142 HidUsageAndPage(0, HidUsageAndPage::kPageVendor); |
145 collection_info.report_ids.insert(1); | 143 collection_info.report_ids.insert(1); |
146 device_info.has_report_id = true; | 144 device_info->has_report_id_ = true; |
147 } | 145 } |
148 device_info.collections.push_back(collection_info); | 146 device_info->collections_.push_back(collection_info); |
149 } | 147 } |
150 HidService::AddDevice(device_info); | 148 HidService::AddDevice(device_info); |
151 } | 149 } |
152 | 150 |
153 void RemoveDevice(const std::string& device_id) { | 151 void RemoveDevice(const std::string& device_id) { |
154 HidService::RemoveDevice(device_id); | 152 HidService::RemoveDevice(device_id); |
155 } | 153 } |
156 }; | 154 }; |
157 | 155 |
158 } // namespace | 156 } // namespace device |
| 157 |
| 158 namespace extensions { |
159 | 159 |
160 class HidApiTest : public ShellApiTest { | 160 class HidApiTest : public ShellApiTest { |
161 public: | 161 public: |
162 void SetUpOnMainThread() override { | 162 void SetUpOnMainThread() override { |
163 ShellApiTest::SetUpOnMainThread(); | 163 ShellApiTest::SetUpOnMainThread(); |
164 hid_service_ = new MockHidService(); | 164 hid_service_ = new device::MockHidService(); |
165 HidService::SetInstanceForTest(hid_service_); | 165 HidService::SetInstanceForTest(hid_service_); |
166 } | 166 } |
167 | 167 |
168 protected: | 168 protected: |
169 MockHidService* hid_service_; | 169 device::MockHidService* hid_service_; |
170 }; | 170 }; |
171 | 171 |
172 IN_PROC_BROWSER_TEST_F(HidApiTest, HidApp) { | 172 IN_PROC_BROWSER_TEST_F(HidApiTest, HidApp) { |
173 ASSERT_TRUE(RunAppTest("api_test/hid/api")) << message_; | 173 ASSERT_TRUE(RunAppTest("api_test/hid/api")) << message_; |
174 } | 174 } |
175 | 175 |
176 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { | 176 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { |
177 ExtensionTestMessageListener load_listener("loaded", false); | 177 ExtensionTestMessageListener load_listener("loaded", false); |
178 ExtensionTestMessageListener result_listener("success", false); | 178 ExtensionTestMessageListener result_listener("success", false); |
179 result_listener.set_failure_message("failure"); | 179 result_listener.set_failure_message("failure"); |
(...skipping 20 matching lines...) Expand all Loading... |
200 // Device C was not returned by chrome.usb.getDevices, the app will not get | 200 // Device C was not returned by chrome.usb.getDevices, the app will not get |
201 // a notification. | 201 // a notification. |
202 hid_service_->RemoveDevice("C"); | 202 hid_service_->RemoveDevice("C"); |
203 // Device A was returned, the app will get a notification. | 203 // Device A was returned, the app will get a notification. |
204 hid_service_->RemoveDevice("A"); | 204 hid_service_->RemoveDevice("A"); |
205 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 205 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
206 EXPECT_EQ("success", result_listener.message()); | 206 EXPECT_EQ("success", result_listener.message()); |
207 } | 207 } |
208 | 208 |
209 } // namespace extensions | 209 } // namespace extensions |
OLD | NEW |