Skip to content

Commit

Permalink
Improve Code Coverage for WebUSB
Browse files Browse the repository at this point in the history
This change improves the code coverage for the sources files under
//third_party/blink/renderer/modules/webusb.

The affected files are:
* usb.cc
* usb_alternate_interface.cc
* usb_configuration.cc
* usb_device.cc
* usb_endpoint.cc
* usb_interface.cc

Bug: 854831
Change-Id: Ie67533ece27f1c944b0e9186d01a61219a30f805
Reviewed-on: https://chromium-review.googlesource.com/1183789
Commit-Queue: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586930}
  • Loading branch information
odejesush authored and Chrome-bot committed Aug 28, 2018
1 parent 2c03d31 commit 192937d
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 69 deletions.
17 changes: 13 additions & 4 deletions webusb/usb-disabled-by-feature-policy.https.sub.html
@@ -1,8 +1,8 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/feature-policy/resources/featurepolicy.js></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/feature-policy/resources/featurepolicy.js"></script>
<script>
'use strict';
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
Expand All @@ -19,7 +19,16 @@
}, error => {
assert_equals(error.name, 'SecurityError');
});
}, header + ' disallows the top-level document.');
}, header + ' disallows getDevices in the top-level document.');

promise_test(async () => {
try {
await navigator.usb.requestDevice({ filters: [] });
assert_unreached('expected promise to reject with SecurityError');
} catch(error) {
assert_equals(error.name, 'SecurityError');
}
}, header + ' disallows requestDevice in the top-level document.');

async_test(t => {
test_feature_availability('usb.getDevices()', t, same_origin_src,
Expand Down
6 changes: 4 additions & 2 deletions webusb/usb.https.window.js
Expand Up @@ -60,7 +60,8 @@ usb_test(() => {
usb_test(() => {
const expectedFilters = [
{ vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" },
{ vendorId: 5678, productId: 0xF00F }
{ vendorId: 5678, productId: 0xF00F },
{ vendorId: 9012, classCode: 0xFF, subclassCode: 0xEE, protocolCode: 0xDD },
];

navigator.usb.test.onrequestdevice = event => {
Expand All @@ -77,7 +78,8 @@ usb_test(() => {
return callWithTrustedClick(() => {
return navigator.usb.requestDevice({ filters: expectedFilters })
.then(device => {
assert_unreachable('requestDevice should reject because no device selected');
assert_unreachable(
'requestDevice should reject because no device selected');
})
.catch(error => {
assert_equals(error.code, DOMException.NOT_FOUND_ERR);
Expand Down
33 changes: 33 additions & 0 deletions webusb/usbAlternateInterface.https.any.js
@@ -0,0 +1,33 @@
// META: script=/webusb/resources/fake-devices.js
// META: script=/webusb/resources/usb-helpers.js
'use strict';

usb_test(async () => {
let { device } = await getFakeDevice();
let configuration = new USBConfiguration(
device, device.configurations[1].configurationValue);
let usbInterface = new USBInterface(
configuration, configuration.interfaces[0].interfaceNumber);
let alternateInterface = new USBAlternateInterface(
usbInterface, usbInterface.alternates[1].alternateSetting);
assertDeviceInfoEquals(
alternateInterface,
fakeDeviceInit.configurations[1].interfaces[0].alternates[1]);
}, 'Can construct a USBAlternateInterface.');

usb_test(async () => {
let { device } = await getFakeDevice();
let configuration = new USBConfiguration(
device, device.configurations[1].configurationValue);
let usbInterface = new USBInterface(
configuration, configuration.interfaces[0].interfaceNumber);
try {
let alternateInterface = new USBAlternateInterface(
usbInterface, usbInterface.alternates.length);
assert_unreached(
'USBAlternateInterface should reject an invalid alternate setting');
} catch (error) {
assert_equals(error.name, 'RangeError');
}
}, 'Constructing a USBAlternateInterface with an invalid alternate setting ' +
'throws a range error.');
23 changes: 23 additions & 0 deletions webusb/usbConfiguration.https.any.js
@@ -0,0 +1,23 @@
// META: script=/webusb/resources/fake-devices.js
// META: script=/webusb/resources/usb-helpers.js
'use strict';

usb_test(async () => {
let { device } = await getFakeDevice();
let configuration = new USBConfiguration(
device, device.configurations[1].configurationValue);
assertDeviceInfoEquals(configuration, fakeDeviceInit.configurations[1]);
}, 'Can construct a USBConfiguration.');

usb_test(async () => {
let { device } = await getFakeDevice();
try {
let configuration =
new USBConfiguration(device, device.configurations.length + 1);
assert_unreached(
'USBConfiguration should reject an invalid configuration value');
} catch (error) {
assert_equals(error.name, 'RangeError');
}
}, 'Constructing a USBConfiguration with an invalid configuration value ' +
'throws a range error.');

0 comments on commit 192937d

Please sign in to comment.