api-web-frame-spec.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  48. });
  49. await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'));
  50. w.focus();
  51. await w.webContents.executeJavaScript('document.querySelector("input").focus()', true);
  52. const spellCheckerFeedback =
  53. new Promise<[string[], boolean]>(resolve => {
  54. ipcMain.on('spec-spell-check', (e, words, callbackDefined) => {
  55. if (words.length === 5) {
  56. // The API calls the provider after every completed word.
  57. // The promise is resolved only after this event is received with all words.
  58. resolve([words, callbackDefined]);
  59. }
  60. });
  61. });
  62. const inputText = 'spleling test you\'re ';
  63. for (const keyCode of inputText) {
  64. w.webContents.sendInputEvent({ type: 'char', keyCode });
  65. }
  66. const [words, callbackDefined] = await spellCheckerFeedback;
  67. expect(words.sort()).to.deep.equal(['spleling', 'test', 'you\'re', 'you', 're'].sort());
  68. expect(callbackDefined).to.be.true();
  69. });
  70. });