blob: 83d80881429fbc1ddfbd8ccce68c93e2da0cbf4b [file] [log] [blame]
<!DOCTYPE html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/testdriver.js"></script>
<script src="../resources/testdriver-vendor.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/hid.mojom.js"></script>
<script src="file:///gen/third_party/blink/public/mojom/hid/hid.mojom.js"></script>
<script src="resources/hid-test-utils.js"></script>
<script>
const kTestVendorId = 0x1234;
const kTestProductId = 0xabcd;
const kReportId = 0x01;
const kReportBytes = new Uint8Array([0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
// Creates a HidDeviceInfo, adds it to the HID service, opens a connection to
// the newly created device, and returns the HidConnection fake.
async function addAndOpenDevice(fake) {
const deviceInfo = fake.makeDevice(kTestVendorId, kTestProductId);
const guid = fake.addDevice(deviceInfo);
fake.setSelectedDevice(guid);
await trustedClick();
const device = await navigator.hid.requestDevice({filters: []});
assert_true(device instanceof HIDDevice);
await device.open();
assert_true(device.opened);
const fakeConnection = fake.getFakeConnection(guid);
return {device, fakeConnection};
}
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Register an oninputreport listener and check that it receives a simulated
// input report.
fakeConnection.simulateInputReport(kReportId, kReportBytes);
const inputReport = await oninputreport(device);
assert_equals(inputReport.reportId, kReportId);
compareDataViews(inputReport.data, new DataView(kReportBytes.buffer));
}, 'oninputreport event listener works');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate write() success.
fakeConnection.queueExpectedWrite(true, kReportId, kReportBytes);
await device.sendReport(kReportId, kReportBytes);
fakeConnection.assertExpectationsMet();
}, 'sendReport works');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate write() failure.
fakeConnection.queueExpectedWrite(false, kReportId, kReportBytes);
await promise_rejects(t, 'NotAllowedError',
device.sendReport(kReportId, kReportBytes));
fakeConnection.assertExpectationsMet();
}, 'Failed sendReport rejects');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate getFeatureReport() success.
fakeConnection.queueExpectedGetFeatureReport(true, kReportId, kReportBytes);
const featureReport = await device.receiveFeatureReport(kReportId);
compareDataViews(featureReport, new DataView(kReportBytes.buffer));
fakeConnection.assertExpectationsMet();
}, 'receiveFeatureReport works');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate getFeatureReport() failure.
fakeConnection.queueExpectedGetFeatureReport(false, kReportId, kReportBytes);
await promise_rejects(t, 'NotAllowedError',
device.receiveFeatureReport(kReportId));
fakeConnection.assertExpectationsMet();
}, 'Failed receiveFeatureReport rejects');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate sendFeatureReport() success.
fakeConnection.queueExpectedSendFeatureReport(true, kReportId, kReportBytes);
await device.sendFeatureReport(kReportId, kReportBytes);
fakeConnection.assertExpectationsMet();
}, 'sendFeatureReport works');
hid_test(async (t, fake) => {
const {device, fakeConnection} = await addAndOpenDevice(fake);
// Simulate sendFeatureReport() failure.
fakeConnection.queueExpectedSendFeatureReport(false, kReportId, kReportBytes);
await promise_rejects(t, 'NotAllowedError',
device.sendFeatureReport(kReportId, kReportBytes));
fakeConnection.assertExpectationsMet();
}, 'Failed sendFeatureReport rejects');
</script>
</body>