feat: add serial api support (#25237)

* feat: add serial api support

resolves #22478

* Put serial port support behind a flag and mark as experimental

* Update docs/api/session.md

Co-authored-by: Jeremy Rose <jeremya@chromium.org>

* Use enable-blink-features=Serial instead of enable-experimental-web-platform-features

* Set enableBlinkFeatures on webPreferences instead of commandline

Co-authored-by: Jeremy Rose <jeremya@chromium.org>
This commit is contained in:
John Kleinschmidt 2020-09-28 12:22:03 -04:00 committed by GitHub
parent bed50bb73c
commit fd63510ca9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 936 additions and 1 deletions

View file

@ -1455,3 +1455,51 @@ describe('iframe using HTML fullscreen API while window is OS-fullscreened', ()
expect(width).to.equal(0);
});
});
describe('navigator.serial', () => {
let w: BrowserWindow;
before(async () => {
w = new BrowserWindow({
show: false,
webPreferences: {
enableBlinkFeatures: 'Serial'
}
});
await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html'));
});
const getPorts: any = () => {
return w.webContents.executeJavaScript(`
navigator.serial.requestPort().then(port => port.toString()).catch(err => err.toString());
`, true);
};
after(closeAllWindows);
afterEach(() => {
session.defaultSession.setPermissionCheckHandler(null);
session.defaultSession.removeAllListeners('select-serial-port');
});
it('does not return a port if select-serial-port event is not defined', async () => {
w.loadFile(path.join(fixturesPath, 'pages', 'blank.html'));
const port = await getPorts();
expect(port).to.equal('NotFoundError: No port selected by the user.');
});
it('does not return a port when permission denied', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback(portList[0].portId);
});
session.defaultSession.setPermissionCheckHandler(() => false);
const port = await getPorts();
expect(port).to.equal('NotFoundError: No port selected by the user.');
});
it('returns a port when select-serial-port event is defined', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback(portList[0].portId);
});
const port = await getPorts();
expect(port).to.equal('[object SerialPort]');
});
});