api-web-frame-spec.js 2.6 KB

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