api-web-frame-spec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const assert = require('assert')
  2. const chai = require('chai')
  3. const dirtyChai = require('dirty-chai')
  4. const path = require('path')
  5. const { closeWindow } = require('./window-helpers')
  6. const { remote, webFrame } = require('electron')
  7. const { BrowserWindow, ipcMain } = remote
  8. const { expect } = chai
  9. chai.use(dirtyChai)
  10. /* Most of the APIs here don't use standard callbacks */
  11. /* eslint-disable standard/no-callback-literal */
  12. describe('webFrame module', function () {
  13. const fixtures = path.resolve(__dirname, 'fixtures')
  14. let w = null
  15. afterEach(function () {
  16. return closeWindow(w).then(function () { w = null })
  17. })
  18. it('supports setting the visual and layout zoom level limits', function () {
  19. assert.doesNotThrow(function () {
  20. webFrame.setVisualZoomLevelLimits(1, 50)
  21. webFrame.setLayoutZoomLevelLimits(0, 25)
  22. })
  23. })
  24. it('calls a spellcheck provider', async () => {
  25. w = new BrowserWindow({
  26. show: false,
  27. webPreferences: {
  28. nodeIntegration: true
  29. }
  30. })
  31. await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'))
  32. w.focus()
  33. await w.webContents.executeJavaScript('document.querySelector("input").focus()', true)
  34. const spellCheckerFeedback =
  35. new Promise(resolve => {
  36. ipcMain.on('spec-spell-check', (e, words, callback) => {
  37. if (words.length === 5) {
  38. // The API calls the provider after every completed word.
  39. // The promise is resolved only after this event is received with all words.
  40. resolve([words, callback])
  41. }
  42. })
  43. })
  44. const inputText = `spleling test you're `
  45. for (const keyCode of inputText) {
  46. w.webContents.sendInputEvent({ type: 'char', keyCode })
  47. }
  48. const [words, callback] = await spellCheckerFeedback
  49. expect(words.sort()).to.deep.equal(['spleling', 'test', `you're`, 'you', 're'].sort())
  50. expect(callback).to.be.true()
  51. })
  52. it('top is self for top frame', () => {
  53. expect(webFrame.top.context).to.equal(webFrame.context)
  54. })
  55. it('opener is null for top frame', () => {
  56. expect(webFrame.opener).to.be.null()
  57. })
  58. it('firstChild is null for top frame', () => {
  59. expect(webFrame.firstChild).to.be.null()
  60. })
  61. it('getFrameForSelector() does not crash when not found', () => {
  62. expect(webFrame.getFrameForSelector('unexist-selector')).to.be.null()
  63. })
  64. it('findFrameByName() does not crash when not found', () => {
  65. expect(webFrame.findFrameByName('unexist-name')).to.be.null()
  66. })
  67. it('findFrameByRoutingId() does not crash when not found', () => {
  68. expect(webFrame.findFrameByRoutingId(-1)).to.be.null()
  69. })
  70. })