|
@@ -1,11 +1,12 @@
|
|
|
'use strict'
|
|
|
|
|
|
const assert = require('assert')
|
|
|
+const http = require('http')
|
|
|
const path = require('path')
|
|
|
const {closeWindow} = require('./window-helpers')
|
|
|
|
|
|
const {ipcRenderer, remote} = require('electron')
|
|
|
-const {BrowserWindow, webContents, ipcMain} = remote
|
|
|
+const {BrowserWindow, webContents, ipcMain, session} = remote
|
|
|
|
|
|
const isCi = remote.getGlobal('isCi')
|
|
|
|
|
@@ -322,4 +323,124 @@ describe('webContents module', function () {
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ describe('zoom api', () => {
|
|
|
+ const zoomScheme = remote.getGlobal('zoomScheme')
|
|
|
+ const hostZoomMap = {
|
|
|
+ 'host1': 0.3,
|
|
|
+ 'host2': 0.7,
|
|
|
+ 'host3': 0.2
|
|
|
+ }
|
|
|
+
|
|
|
+ before((done) => {
|
|
|
+ let protocol = session.defaultSession.protocol
|
|
|
+ protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
|
|
+ let response = `<script>
|
|
|
+ const {ipcRenderer, remote} = require('electron')
|
|
|
+ ipcRenderer.send('set-zoom', window.location.hostname)
|
|
|
+ ipcRenderer.on(window.location.hostname + '-zoom-set', () => {
|
|
|
+ remote.getCurrentWebContents().getZoomLevel((zoomLevel) => {
|
|
|
+ ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ </script>`
|
|
|
+ callback({data: response, mimeType: 'text/html'})
|
|
|
+ }, (error) => done(error))
|
|
|
+ })
|
|
|
+
|
|
|
+ after((done) => {
|
|
|
+ let protocol = session.defaultSession.protocol
|
|
|
+ protocol.unregisterProtocol(zoomScheme, (error) => done(error))
|
|
|
+ })
|
|
|
+
|
|
|
+ it('can set the correct zoom level', (done) => {
|
|
|
+ w.loadURL('about:blank')
|
|
|
+ w.webContents.on('did-finish-load', () => {
|
|
|
+ w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
+ assert.equal(zoomLevel, 0.0)
|
|
|
+ w.webContents.setZoomLevel(0.5)
|
|
|
+ w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
+ assert.equal(zoomLevel, 0.5)
|
|
|
+ w.webContents.setZoomLevel(0)
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('can persist zoom level across navigation', (done) => {
|
|
|
+ let finalNavigation = false
|
|
|
+ ipcMain.on('set-zoom', (e, host) => {
|
|
|
+ let zoomLevel = hostZoomMap[host]
|
|
|
+ if (!finalNavigation) {
|
|
|
+ w.webContents.setZoomLevel(zoomLevel)
|
|
|
+ }
|
|
|
+ e.sender.send(`${host}-zoom-set`)
|
|
|
+ })
|
|
|
+ ipcMain.on('host1-zoom-level', (e, zoomLevel) => {
|
|
|
+ let expectedZoomLevel = hostZoomMap['host1']
|
|
|
+ assert.equal(zoomLevel, expectedZoomLevel)
|
|
|
+ if (finalNavigation) {
|
|
|
+ done()
|
|
|
+ } else {
|
|
|
+ w.loadURL(`${zoomScheme}://host2`)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ipcMain.once('host2-zoom-level', (e, zoomLevel) => {
|
|
|
+ let expectedZoomLevel = hostZoomMap['host2']
|
|
|
+ assert.equal(zoomLevel, expectedZoomLevel)
|
|
|
+ finalNavigation = true
|
|
|
+ w.webContents.goBack()
|
|
|
+ })
|
|
|
+ w.loadURL(`${zoomScheme}://host1`)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('can propagate zoom level across same session', (done) => {
|
|
|
+ const w2 = new BrowserWindow({
|
|
|
+ show: false
|
|
|
+ })
|
|
|
+ w2.webContents.on('did-finish-load', () => {
|
|
|
+ w.webContents.getZoomLevel((zoomLevel1) => {
|
|
|
+ assert.equal(zoomLevel1, hostZoomMap['host3'])
|
|
|
+ w2.webContents.getZoomLevel((zoomLevel2) => {
|
|
|
+ assert.equal(zoomLevel1, zoomLevel2)
|
|
|
+ w2.setClosable(true)
|
|
|
+ w2.close()
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ w.webContents.on('did-finish-load', () => {
|
|
|
+ w.webContents.setZoomLevel(hostZoomMap['host3'])
|
|
|
+ w2.loadURL(`${zoomScheme}://host3`)
|
|
|
+ })
|
|
|
+ w.loadURL(`${zoomScheme}://host3`)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('can persist when it contains iframe', (done) => {
|
|
|
+ const server = http.createServer(function (req, res) {
|
|
|
+ setTimeout(() => {
|
|
|
+ res.end()
|
|
|
+ }, 2000)
|
|
|
+ })
|
|
|
+ server.listen(0, '127.0.0.1', function () {
|
|
|
+ const url = 'http://127.0.0.1:' + server.address().port
|
|
|
+ const content = `<iframe src=${url}></iframe>`
|
|
|
+ w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
|
|
|
+ if (!isMainFrame) {
|
|
|
+ w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
+ assert.equal(zoomLevel, 2.0)
|
|
|
+ w.webContents.setZoomLevel(0)
|
|
|
+ server.close()
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ w.webContents.on('dom-ready', () => {
|
|
|
+ w.webContents.setZoomLevel(2.0)
|
|
|
+ })
|
|
|
+ w.loadURL(`data:text/html,${content}`)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
})
|