api-web-frame-spec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const assert = require('assert')
  2. const path = require('path')
  3. const {closeWindow} = require('./window-helpers')
  4. const {remote, webFrame} = require('electron')
  5. const {BrowserWindow, protocol, ipcMain} = remote
  6. describe('webFrame module', function () {
  7. var fixtures = path.resolve(__dirname, 'fixtures')
  8. describe('webFrame.registerURLSchemeAsPrivileged', function () {
  9. it('supports fetch api', function (done) {
  10. webFrame.registerURLSchemeAsPrivileged('file')
  11. var url = 'file://' + fixtures + '/assets/logo.png'
  12. window.fetch(url).then(function (response) {
  13. assert(response.ok)
  14. done()
  15. }).catch(function (err) {
  16. done('unexpected error : ' + err)
  17. })
  18. })
  19. it('allows CORS requests', function (done) {
  20. const standardScheme = remote.getGlobal('standardScheme')
  21. const url = standardScheme + '://fake-host'
  22. const content = `<html>
  23. <script>
  24. const {ipcRenderer, webFrame} = require('electron')
  25. webFrame.registerURLSchemeAsPrivileged('cors')
  26. fetch('cors://myhost').then(function (response) {
  27. ipcRenderer.send('response', response.status)
  28. })
  29. </script>
  30. </html>`
  31. var w = new BrowserWindow({show: false})
  32. after(function (done) {
  33. protocol.unregisterProtocol('cors', function () {
  34. protocol.unregisterProtocol(standardScheme, function () {
  35. closeWindow(w).then(function () {
  36. w = null
  37. done()
  38. })
  39. })
  40. })
  41. })
  42. const handler = function (request, callback) {
  43. callback({data: content, mimeType: 'text/html'})
  44. }
  45. protocol.registerStringProtocol(standardScheme, handler, function (error) {
  46. if (error) return done(error)
  47. })
  48. protocol.registerStringProtocol('cors', function (request, callback) {
  49. callback('')
  50. }, function (error) {
  51. if (error) return done(error)
  52. ipcMain.once('response', function (event, status) {
  53. assert.equal(status, 200)
  54. done()
  55. })
  56. w.loadURL(url)
  57. })
  58. })
  59. })
  60. it('supports setting the visual and layout zoom level limits', function () {
  61. assert.doesNotThrow(function () {
  62. webFrame.setZoomLevelLimits(1, 100)
  63. webFrame.setVisualZoomLevelLimits(1, 50)
  64. webFrame.setLayoutZoomLevelLimits(0, 25)
  65. })
  66. })
  67. })