2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as path from 'path';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow, ipcMain } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { closeAllWindows } from './window-helpers';
|
2020-07-23 21:32:20 +00:00
|
|
|
import { emittedOnce } from './events-helpers';
|
2019-10-11 17:20:08 +00:00
|
|
|
|
|
|
|
describe('webFrame module', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
|
2019-10-11 17:20:08 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-10-11 17:20:08 +00:00
|
|
|
|
2020-07-23 21:32:20 +00:00
|
|
|
for (const worldSafe of [true, false]) {
|
|
|
|
it(`can use executeJavaScript with world safe mode ${worldSafe ? 'enabled' : 'disabled'}`, async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
worldSafeExecuteJavaScript: worldSafe,
|
|
|
|
preload: path.join(fixtures, 'pages', 'world-safe-preload.js')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const isSafe = emittedOnce(ipcMain, 'executejs-safe');
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const [, wasSafe] = await isSafe;
|
|
|
|
expect(wasSafe).to.equal(worldSafe);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('can use executeJavaScript with world safe mode enabled and catch conversion errors', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
worldSafeExecuteJavaScript: true,
|
|
|
|
preload: path.join(fixtures, 'pages', 'world-safe-preload-error.js')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const execError = emittedOnce(ipcMain, 'executejs-safe');
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const [, error] = await execError;
|
|
|
|
expect(error).to.not.equal(null, 'Error should not be null');
|
|
|
|
expect(error).to.have.property('message', 'Uncaught Error: An object could not be cloned.');
|
|
|
|
});
|
|
|
|
|
2019-10-11 17:20:08 +00:00
|
|
|
it('calls a spellcheck provider', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-10-11 17:20:08 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'));
|
|
|
|
w.focus();
|
|
|
|
await w.webContents.executeJavaScript('document.querySelector("input").focus()', true);
|
2019-10-11 17:20:08 +00:00
|
|
|
|
|
|
|
const spellCheckerFeedback =
|
|
|
|
new Promise<[string[], boolean]>(resolve => {
|
|
|
|
ipcMain.on('spec-spell-check', (e, words, callbackDefined) => {
|
|
|
|
if (words.length === 5) {
|
|
|
|
// The API calls the provider after every completed word.
|
|
|
|
// The promise is resolved only after this event is received with all words.
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve([words, callbackDefined]);
|
2019-10-11 17:20:08 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const inputText = 'spleling test you\'re ';
|
2019-10-11 17:20:08 +00:00
|
|
|
for (const keyCode of inputText) {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode });
|
2019-10-11 17:20:08 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
const [words, callbackDefined] = await spellCheckerFeedback;
|
|
|
|
expect(words.sort()).to.deep.equal(['spleling', 'test', 'you\'re', 'you', 're'].sort());
|
|
|
|
expect(callbackDefined).to.be.true();
|
|
|
|
});
|
|
|
|
});
|