add zoom specs based on standard protocols

This commit is contained in:
deepak1556 2017-02-17 23:28:29 +05:30
parent 0c022fdc36
commit a57af31b99
5 changed files with 79 additions and 14 deletions

View file

@ -496,7 +496,7 @@ describe('webContents module', function () {
}) })
}) })
ipcMain.once('temporary-zoom-set', (e, zoomLevel) => { ipcMain.once('temporary-zoom-set', (e, zoomLevel) => {
w2.loadURL(`file://${fixtures}/pages/a.html`) w2.loadURL(`file://${fixtures}/pages/c.html`)
finalZoomLevel = zoomLevel finalZoomLevel = zoomLevel
}) })
w.loadURL(`file://${fixtures}/pages/webframe-zoom.html`) w.loadURL(`file://${fixtures}/pages/webframe-zoom.html`)
@ -521,10 +521,10 @@ describe('webContents module', function () {
}) })
ipcMain.once('zoom-level-set', (e, zoomLevel) => { ipcMain.once('zoom-level-set', (e, zoomLevel) => {
assert.equal(zoomLevel, 0.6) assert.equal(zoomLevel, 0.6)
w.loadURL(`file://${fixtures}/pages/b.html`) w.loadURL(`file://${fixtures}/pages/d.html`)
initialNavigation = false initialNavigation = false
}) })
w.loadURL(`file://${fixtures}/pages/a.html`) w.loadURL(`file://${fixtures}/pages/c.html`)
}) })
}) })
}) })

View file

@ -1,20 +1,23 @@
<html> <html>
<body> <body>
<webview nodeintegration src="a.html" id="view"/> <webview nodeintegration src="zoom://host1" id="view" partition="webview-temp"/>
</body> </body>
<script> <script>
const {ipcRenderer, webFrame} = require('electron') const {ipcRenderer, webFrame} = require('electron')
const view = document.getElementById('view') const view = document.getElementById('view')
let finalNavigation = false let finalNavigation = false
view.addEventListener('dom-ready', () => { view.addEventListener('dom-ready', () => {
if (!finalNavigation) if (!finalNavigation && !view.canGoBack()) {
view.setZoomLevel(2.0) view.setZoomLevel(2.0)
}
view.getZoomLevel((zoomLevel) => { view.getZoomLevel((zoomLevel) => {
view.getZoomFactor((zoomFactor) => { view.getZoomFactor((zoomFactor) => {
ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, finalNavigation) ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, view.canGoBack(), finalNavigation)
if (!finalNavigation) { if (!view.canGoBack() && !finalNavigation) {
view.src = 'zoom://host2'
} else if (!finalNavigation) {
finalNavigation = true finalNavigation = true
view.src = 'b.html' view.goBack()
} }
}) })
}) })

View file

@ -0,0 +1,32 @@
<html>
<body>
<webview nodeintegration src="zoom://host1" id="view" partition="webview-temp"/>
</body>
<script>
const {ipcRenderer, webFrame} = require('electron')
const view = document.getElementById('view')
let finalNavigation = false
function SendZoomLevel() {
return new Promise((resolve, reject) => {
view.getZoomLevel((zoomLevel) => {
view.getZoomFactor((zoomFactor) => {
ipcRenderer.send('webview-zoom-in-page', zoomLevel, zoomFactor, finalNavigation)
resolve()
})
})
})
}
view.addEventListener('dom-ready', () => {
view.setZoomLevel(2.0)
SendZoomLevel().then(() => {
if (!finalNavigation) {
finalNavigation = true
view.executeJavaScript('window.location.hash=123', () => {})
}
})
})
view.addEventListener('did-navigate-in-page', () => {
SendZoomLevel()
})
</script>
</html>

View file

@ -2,7 +2,7 @@
<body> <body>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
const {ipcRenderer, webFrame} = require('electron') const {ipcRenderer, webFrame} = require('electron')
ipcRenderer.send('webview-zoom-level', webFrame.getZoomFactor(), webFrame.getZoomLevel()) ipcRenderer.send('webview-parent-zoom-level', webFrame.getZoomFactor(), webFrame.getZoomLevel())
</script> </script>
</body> </body>
</html> </html>

View file

@ -1527,6 +1527,21 @@ describe('<webview> tag', function () {
}) })
describe('zoom behavior', () => { describe('zoom behavior', () => {
const zoomScheme = remote.getGlobal('zoomScheme')
const webviewSession = session.fromPartition('webview-temp')
before((done) => {
const protocol = webviewSession.protocol
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
callback('hello')
}, (error) => done(error))
})
after((done) => {
const protocol = webviewSession.protocol
protocol.unregisterProtocol(zoomScheme, (error) => done(error))
})
it('inherits the zoomFactor of the parent window', (done) => { it('inherits the zoomFactor of the parent window', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
@ -1534,7 +1549,7 @@ describe('<webview> tag', function () {
zoomFactor: 1.2 zoomFactor: 1.2
} }
}) })
ipcMain.once('webview-zoom-level', (event, zoomFactor, zoomLevel) => { ipcMain.once('webview-parent-zoom-level', (event, zoomFactor, zoomLevel) => {
assert.equal(zoomFactor, 1.2) assert.equal(zoomFactor, 1.2)
assert.equal(zoomLevel, 1) assert.equal(zoomLevel, 1)
done() done()
@ -1542,24 +1557,39 @@ describe('<webview> tag', function () {
w.loadURL(`file://${fixtures}/pages/webview-zoom-factor.html`) w.loadURL(`file://${fixtures}/pages/webview-zoom-factor.html`)
}) })
it('inherits the zoomLevel of parent window on navigation', (done) => { it('maintains zoom level on navigation', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
zoomFactor: 1.2 zoomFactor: 1.2
} }
}) })
ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, final) => { ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, newHost, final) => {
if (!final) { if (!newHost) {
assert.equal(zoomFactor, 1.44) assert.equal(zoomFactor, 1.44)
assert.equal(zoomLevel, 2.0) assert.equal(zoomLevel, 2.0)
} else { } else {
assert.equal(zoomFactor, 1.2) assert.equal(zoomFactor, 1.2)
assert.equal(zoomLevel, 1) assert.equal(zoomLevel, 1)
done()
} }
if (final) done()
}) })
w.loadURL(`file://${fixtures}/pages/webview-custom-zoom-level.html`) w.loadURL(`file://${fixtures}/pages/webview-custom-zoom-level.html`)
}) })
it('maintains zoom level when navigating within same page', (done) => {
w = new BrowserWindow({
show: false,
webPreferences: {
zoomFactor: 1.2
}
})
ipcMain.on('webview-zoom-in-page', (event, zoomLevel, zoomFactor, final) => {
assert.equal(zoomFactor, 1.44)
assert.equal(zoomLevel, 2.0)
if (final) done()
})
w.loadURL(`file://${fixtures}/pages/webview-in-page-navigate.html`)
})
}) })
}) })