1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
'use strict';
// These tests rely on the User Agent providing an implementation of the
// WebUSB Testing API (https://wicg.github.io/webusb/test/).
//
// In Chromium-based browsers this implementation is provided by a polyfill
// in order to reduce the amount of test-only code shipped to users. To enable
// these tests the browser must be run with these options:
//
// --enable-blink-features=MojoJS,MojoJSTest
(() => {
// Load scripts needed by the test API on context creation.
if (isChromiumBased) {
loadScript('/resources/chromium/webusb-child-test.js');
}
})();
function usb_test(func, name, properties) {
promise_test(async (t) => {
assert_implements(navigator.usb, 'missing navigator.usb');
if (navigator.usb.test === undefined) {
// Try loading a polyfill for the WebUSB Testing API.
if (isChromiumBased) {
await loadScript('/resources/chromium/webusb-test.js');
}
}
assert_implements(navigator.usb.test, 'missing navigator.usb.test after initialization');
await navigator.usb.test.initialize();
try {
await func(t);
} finally {
await navigator.usb.test.reset();
}
}, name, properties);
}
// Returns a promise that is resolved when the next USBConnectionEvent of the
// given type is received.
function connectionEventPromise(eventType) {
return new Promise(resolve => {
let eventHandler = e => {
assert_true(e instanceof USBConnectionEvent);
navigator.usb.removeEventListener(eventType, eventHandler);
resolve(e.device);
};
navigator.usb.addEventListener(eventType, eventHandler);
});
}
// Creates a fake device and returns a promise that resolves once the
// 'connect' event is fired for the fake device. The promise is resolved with
// an object containing the fake USB device and the corresponding USBDevice.
function getFakeDevice() {
let promise = connectionEventPromise('connect');
let fakeDevice = navigator.usb.test.addFakeDevice(fakeDeviceInit);
return promise.then(device => {
return { device: device, fakeDevice: fakeDevice };
});
}
// Disconnects the given device and returns a promise that is resolved when it
// is done.
function waitForDisconnect(fakeDevice) {
let promise = connectionEventPromise('disconnect');
fakeDevice.disconnect();
return promise;
}
function assertDeviceInfoEquals(usbDevice, deviceInit) {
for (var property in deviceInit) {
if (property == 'activeConfigurationValue') {
if (deviceInit.activeConfigurationValue == 0) {
assert_equals(usbDevice.configuration, null);
} else {
assert_equals(usbDevice.configuration.configurationValue,
deviceInit.activeConfigurationValue);
}
} else if (Array.isArray(deviceInit[property])) {
assert_equals(usbDevice[property].length, deviceInit[property].length);
for (var i = 0; i < usbDevice[property].length; ++i)
assertDeviceInfoEquals(usbDevice[property][i], deviceInit[property][i]);
} else {
assert_equals(usbDevice[property], deviceInit[property], property);
}
}
}
function callWithTrustedClick(callback) {
return new Promise(resolve => {
let button = document.createElement('button');
button.textContent = 'click to continue test';
button.style.display = 'block';
button.style.fontSize = '20px';
button.style.padding = '10px';
button.onclick = () => {
resolve(callback());
document.body.removeChild(button);
};
document.body.appendChild(button);
test_driver.click(button);
});
}
|