api-web-frame-spec.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import { BrowserWindow, ipcMain } from 'electron/main';
  4. import { closeAllWindows } from './window-helpers';
  5. import { emittedOnce } from './events-helpers';
  6. describe('webFrame module', () => {
  7. const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
  8. afterEach(closeAllWindows);
  9. for (const worldSafe of [true, false]) {
  10. it(`can use executeJavaScript with world safe mode ${worldSafe ? 'enabled' : 'disabled'}`, async () => {
  11. const w = new BrowserWindow({
  12. show: true,
  13. webPreferences: {
  14. nodeIntegration: true,
  15. contextIsolation: true,
  16. worldSafeExecuteJavaScript: worldSafe,
  17. preload: path.join(fixtures, 'pages', 'world-safe-preload.js')
  18. }
  19. });
  20. const isSafe = emittedOnce(ipcMain, 'executejs-safe');
  21. w.loadURL('about:blank');
  22. const [, wasSafe] = await isSafe;
  23. expect(wasSafe).to.equal(worldSafe);
  24. });
  25. }
  26. it('can use executeJavaScript with world safe mode enabled and catch conversion errors', async () => {
  27. const w = new BrowserWindow({
  28. show: true,
  29. webPreferences: {
  30. nodeIntegration: true,
  31. contextIsolation: true,
  32. worldSafeExecuteJavaScript: true,
  33. preload: path.join(fixtures, 'pages', 'world-safe-preload-error.js')
  34. }
  35. });
  36. const execError = emittedOnce(ipcMain, 'executejs-safe');
  37. w.loadURL('about:blank');
  38. const [, error] = await execError;
  39. expect(error).to.not.equal(null, 'Error should not be null');
  40. expect(error).to.have.property('message', 'Uncaught Error: An object could not be cloned.');
  41. });
  42. it('calls a spellcheck provider', async () => {
  43. const w = new BrowserWindow({
  44. show: false,
  45. webPreferences: {
  46. nodeIntegration: true,
  47. contextIsolation: false
  48. }
  49. });
  50. await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'));
  51. w.focus();
  52. await w.webContents.executeJavaScript('document.querySelector("input").focus()', true);
  53. const spellCheckerFeedback =
  54. new Promise<[string[], boolean]>(resolve => {
  55. ipcMain.on('spec-spell-check', (e, words, callbackDefined) => {
  56. if (words.length === 5) {
  57. // The API calls the provider after every completed word.
  58. // The promise is resolved only after this event is received with all words.
  59. resolve([words, callbackDefined]);
  60. }
  61. });
  62. });
  63. const inputText = 'spleling test you\'re ';
  64. for (const keyCode of inputText) {
  65. w.webContents.sendInputEvent({ type: 'char', keyCode });
  66. }
  67. const [words, callbackDefined] = await spellCheckerFeedback;
  68. expect(words.sort()).to.deep.equal(['spleling', 'test', 'you\'re', 'you', 're'].sort());
  69. expect(callbackDefined).to.be.true();
  70. });
  71. });