From 5151a046136231c53b32c3b143e0d72ed68d0f1a Mon Sep 17 00:00:00 2001 From: Aleksei Kuzmin Date: Tue, 19 Jun 2018 13:25:26 +0200 Subject: [PATCH] spec: slightly rewrite webview tests --- spec/webview-spec.js | 135 +++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 70 deletions(-) diff --git a/spec/webview-spec.js b/spec/webview-spec.js index 124d21d4df9..7f52cad128f 100644 --- a/spec/webview-spec.js +++ b/spec/webview-spec.js @@ -1,11 +1,16 @@ const assert = require('assert') -const {expect} = require('chai') +const chai = require('chai') +const dirtyChai = require('dirty-chai') const path = require('path') const http = require('http') const url = require('url') const {ipcRenderer, remote} = require('electron') const {app, session, getGuestWebContents, ipcMain, BrowserWindow, webContents} = remote const {closeWindow} = require('./window-helpers') +const {emittedOnce, waitForEvent} = require('./events-helpers') + +const {expect} = chai +chai.use(dirtyChai) const isCI = remote.getGlobal('isCi') const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled') @@ -31,29 +36,19 @@ describe(' tag', function () { w = null } - const waitForEvent = (eventTarget, eventName) => { - return new Promise((resolve) => { - eventTarget.addEventListener(eventName, resolve, {once: true}) - }) - } - - const waitForOnce = (eventTarget, eventName) => { - return new Promise((resolve) => { - eventTarget.once(eventName, (...args) => resolve(args)) - }) - } - - const loadWebView = (webview, attributes = {}) => { + const loadWebView = async (webview, attributes = {}) => { for (const [name, value] of Object.entries(attributes)) { webview.setAttribute(name, value) } document.body.appendChild(webview) - return waitForEvent(webview, 'did-finish-load') + await waitForEvent(webview, 'did-finish-load') + return webview } - const startLoadingWebViewAndWaitForMessage = (webview, attributes = {}) => { + const startLoadingWebViewAndWaitForMessage = async (webview, attributes = {}) => { loadWebView(webview, attributes) // Don't wait for load to be finished. - return waitForEvent(webview, 'console-message') + const event = await waitForEvent(webview, 'console-message') + return event.message } beforeEach(() => { @@ -72,7 +67,7 @@ describe(' tag', function () { it('works without script tag in page', async () => { const w = await openTheWindow({show: false}) w.loadURL('file://' + fixtures + '/pages/webview-no-script.html') - await waitForOnce(ipcMain, 'pong') + await emittedOnce(ipcMain, 'pong') }) it('is disabled when nodeIntegration is disabled', async () => { @@ -85,7 +80,7 @@ describe(' tag', function () { }) w.loadURL(`file://${fixtures}/pages/webview-no-script.html`) - const [, type] = await waitForOnce(ipcMain, 'webview') + const [, type] = await emittedOnce(ipcMain, 'webview') expect(type).to.equal('undefined', 'WebView still exists') }) @@ -101,14 +96,14 @@ describe(' tag', function () { }) w.loadURL(`file://${fixtures}/pages/webview-no-script.html`) - const [, type] = await waitForOnce(ipcMain, 'webview') + const [, type] = await emittedOnce(ipcMain, 'webview') expect(type).to.not.equal('undefined', 'WebView is not created') }) describe('src attribute', () => { it('specifies the page to load', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/a.html` }) expect(message).to.equal('a') @@ -126,7 +121,7 @@ describe(' tag', function () { }) it('resolves relative URLs', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: '../fixtures/pages/e.html' }) assert.equal(message, 'Window script is loaded before preload script') @@ -144,7 +139,7 @@ describe(' tag', function () { describe('nodeintegration attribute', () => { it('inserts no node symbols when not set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/c.html` }) @@ -158,7 +153,7 @@ describe(' tag', function () { }) it('inserts node symbols when set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { nodeintegration: 'on', src: `file://${fixtures}/pages/d.html` }) @@ -178,7 +173,7 @@ describe(' tag', function () { return } - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { nodeintegration: 'on', src: `file://${fixtures}/pages/post.html` }) @@ -231,7 +226,7 @@ describe(' tag', function () { describe('preload attribute', () => { it('loads the script before other scripts in window', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { preload: `${fixtures}/module/preload.js`, src: `file://${fixtures}/pages/e.html` }) @@ -241,7 +236,7 @@ describe(' tag', function () { }) it('preload script can still use "process" and "Buffer" when nodeintegration is off', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { preload: `${fixtures}/module/preload-node-off.js`, src: `file://${fixtures}/api/blank.html` }) @@ -254,7 +249,7 @@ describe(' tag', function () { }) it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { preload: `${fixtures}/module/preload-node-off-wrapper.js`, src: `file://${fixtures}/api/blank.html` }) @@ -281,7 +276,7 @@ describe(' tag', function () { }) it('works without script tag in page', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { preload: `${fixtures}/module/preload.js`, src: `file://${fixtures}pages/base-page.html` }) @@ -296,7 +291,7 @@ describe(' tag', function () { }) it('resolves relative URLs', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { preload: '../fixtures/module/preload.js', src: `file://${fixtures}/pages/e.html` }) @@ -341,7 +336,7 @@ describe(' tag', function () { describe('useragent attribute', () => { it('sets the user agent', async () => { const referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko' - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/useragent.html`, useragent: referrer }) @@ -355,7 +350,7 @@ describe(' tag', function () { const src = ` ` const encoded = btoa(unescape(encodeURIComponent(src))) - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `data:text/html;base64,${encoded}` }) expect(message).to.be.a('string') @@ -367,7 +362,7 @@ describe(' tag', function () { const src = ` ` const encoded = btoa(unescape(encodeURIComponent(src))) - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { disablewebsecurity: '', src: `data:text/html;base64,${encoded}` }) @@ -375,7 +370,7 @@ describe(' tag', function () { }) it('does not break node integration', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { disablewebsecurity: '', nodeintegration: 'on', src: `file://${fixtures}/pages/d.html` @@ -390,7 +385,7 @@ describe(' tag', function () { }) it('does not break preload script', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { disablewebsecurity: '', preload: `${fixtures}/module/preload.js`, src: `file://${fixtures}/pages/e.html` @@ -408,7 +403,7 @@ describe(' tag', function () { describe('partition attribute', () => { it('inserts no node symbols when not set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { partition: 'test1', src: `file://${fixtures}/pages/c.html` }) @@ -423,7 +418,7 @@ describe(' tag', function () { }) it('inserts node symbols when set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { nodeintegration: 'on', partition: 'test2', src: `file://${fixtures}/pages/d.html` @@ -440,7 +435,7 @@ describe(' tag', function () { it('isolates storage for different id', async () => { window.localStorage.setItem('test', 'one') - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { partition: 'test3', src: `file://${fixtures}/pages/partition/one.html` }) @@ -456,7 +451,7 @@ describe(' tag', function () { const testValue = 'one' window.localStorage.setItem('test', testValue) - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/partition/one.html` }) @@ -470,14 +465,14 @@ describe(' tag', function () { describe('allowpopups attribute', () => { it('can not open new window when not set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/window-open-hide.html` }) expect(message).to.equal('null') }) it('can open new window when set', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { allowpopups: 'on', src: `file://${fixtures}/pages/window-open-hide.html` }) @@ -487,7 +482,7 @@ describe(' tag', function () { describe('webpreferences attribute', () => { it('can enable nodeintegration', async () => { - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `file://${fixtures}/pages/d.html`, webpreferences: 'nodeIntegration' }) @@ -505,7 +500,7 @@ describe(' tag', function () { const src = ` ` const encoded = btoa(unescape(encodeURIComponent(src))) - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { src: `data:text/html;base64,${encoded}`, webpreferences: 'webSecurity=no, nodeIntegration=yes' }) @@ -521,7 +516,7 @@ describe(' tag', function () { webpreferences: 'contextIsolation=yes' }) - const [, data] = await waitForOnce(ipcMain, 'isolated-world') + const [, data] = await emittedOnce(ipcMain, 'isolated-world') assert.deepEqual(data, { preloadContext: { preloadProperty: 'number', @@ -1137,10 +1132,10 @@ describe(' tag', function () { const w = await openTheWindow({ show: false }) w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`) - await waitForOnce(w, 'ready-to-show') + await emittedOnce(w, 'ready-to-show') w.show() - const [, visibilityState, hidden] = await waitForOnce(ipcMain, 'pong') + const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong') assert(!hidden) assert.equal(visibilityState, 'visible') }) @@ -1149,13 +1144,13 @@ describe(' tag', function () { const w = await openTheWindow({ show: false }) w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`) - let [, visibilityState, hidden] = await waitForOnce(ipcMain, 'pong') + let [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong') assert.equal(visibilityState, 'hidden') assert.equal(hidden, true) // We have to start waiting for the event // before we ask the webContents to resize. - let getResponse = waitForOnce(ipcMain, 'pong') + const getResponse = emittedOnce(ipcMain, 'pong') w.webContents.emit('-window-visibility-change', 'visible') return getResponse.then(([, visibilityState, hidden]) => { @@ -1168,7 +1163,7 @@ describe(' tag', function () { describe('will-attach-webview event', () => { it('supports changing the web preferences', async () => { ipcRenderer.send('disable-node-on-next-will-attach-webview') - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { nodeintegration: 'yes', src: `file://${fixtures}/pages/a.html` }) @@ -1194,7 +1189,7 @@ describe(' tag', function () { it('supports removing the preload script', async () => { ipcRenderer.send('disable-preload-on-next-will-attach-webview') - const {message} = await startLoadingWebViewAndWaitForMessage(webview, { + const message = await startLoadingWebViewAndWaitForMessage(webview, { nodeintegration: 'yes', preload: path.join(fixtures, 'module', 'preload-set-global.js'), src: `file://${fixtures}/pages/a.html` @@ -1209,8 +1204,8 @@ describe(' tag', function () { const w = await openTheWindow({ show: false }) w.loadURL(`file://${fixtures}/pages/webview-did-attach-event.html`) - const [, webContents] = await waitForOnce(w.webContents, 'did-attach-webview') - const [, id] = await waitForOnce(ipcMain, 'webview-dom-ready') + const [, webContents] = await emittedOnce(w.webContents, 'did-attach-webview') + const [, id] = await emittedOnce(ipcMain, 'webview-dom-ready') expect(webContents.id).to.equal(id) }) }) @@ -1224,7 +1219,7 @@ describe(' tag', function () { w.loadURL(`file://${fixtures}/pages/webview-devtools.html`) - const [, {runtimeId, tabId}] = await waitForOnce(ipcMain, 'answer') + const [, {runtimeId, tabId}] = await emittedOnce(ipcMain, 'answer') expect(runtimeId).to.equal('foo') expect(tabId).to.be.not.equal(w.webContents.id) }) @@ -1506,15 +1501,15 @@ describe(' tag', function () { const w = await openTheWindow( {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE}) w.loadURL(`file://${fixtures}/pages/webview-guest-resize.html`) - await waitForOnce(ipcMain, 'webview-loaded') + await emittedOnce(ipcMain, 'webview-loaded') - const elementResize = waitForOnce(ipcMain, 'webview-element-resize') + const elementResize = emittedOnce(ipcMain, 'webview-element-resize') .then(([, width, height]) => { assert.equal(width, CONTENT_SIZE) assert.equal(height, CONTENT_SIZE) }) - const guestResize = waitForOnce(ipcMain, 'webview-guest-resize') + const guestResize = emittedOnce(ipcMain, 'webview-guest-resize') .then(([, width, height]) => { assert.equal(width, CONTENT_SIZE) assert.equal(height, CONTENT_SIZE) @@ -1533,10 +1528,10 @@ describe(' tag', function () { const w = await openTheWindow( {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE}) w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`) - await waitForOnce(ipcMain, 'webview-loaded') + await emittedOnce(ipcMain, 'webview-loaded') const noGuestResizePromise = Promise.race([ - waitForOnce(ipcMain, 'webview-guest-resize'), + emittedOnce(ipcMain, 'webview-guest-resize'), new Promise(resolve => setTimeout(() => resolve(), 500)) ]).then((eventData = null) => { if (eventData !== null) { @@ -1558,9 +1553,9 @@ describe(' tag', function () { const w = await openTheWindow( {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE}) w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`) - await waitForOnce(ipcMain, 'webview-loaded') + await emittedOnce(ipcMain, 'webview-loaded') - const elementResizePromise = waitForOnce(ipcMain, 'webview-element-resize') + const elementResizePromise = emittedOnce(ipcMain, 'webview-element-resize') .then(([, width, height]) => { expect(width).to.equal(CONTENT_SIZE) expect(height).to.equal(CONTENT_SIZE) @@ -1579,12 +1574,12 @@ describe(' tag', function () { const w = await openTheWindow( {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE}) w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`) - await waitForOnce(ipcMain, 'webview-loaded') + await emittedOnce(ipcMain, 'webview-loaded') const GUEST_WIDTH = 10 const GUEST_HEIGHT = 20 - const guestResizePromise = waitForOnce(ipcMain, 'webview-guest-resize') + const guestResizePromise = emittedOnce(ipcMain, 'webview-guest-resize') .then(([, width, height]) => { expect(width).to.be.equal(GUEST_WIDTH) expect(height).to.be.equal(GUEST_HEIGHT) @@ -1631,7 +1626,7 @@ describe(' tag', function () { }) w.loadURL(`file://${fixtures}/pages/webview-zoom-factor.html`) - const [, zoomFactor, zoomLevel] = await waitForOnce(ipcMain, 'webview-parent-zoom-level') + const [, zoomFactor, zoomLevel] = await emittedOnce(ipcMain, 'webview-parent-zoom-level') expect(zoomFactor).to.equal(1.2) expect(zoomLevel).to.equal(1) }) @@ -1698,7 +1693,7 @@ describe(' tag', function () { }) w.loadURL(`file://${fixtures}/pages/webview-origin-zoom-level.html`) - const [, zoomLevel] = await waitForOnce(ipcMain, 'webview-origin-zoom-level') + const [, zoomLevel] = await emittedOnce(ipcMain, 'webview-origin-zoom-level') expect(zoomLevel).to.equal(2.0) }) }) @@ -1716,7 +1711,7 @@ describe(' tag', function () { src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}` }) - const [, content] = await waitForOnce(ipcMain, 'answer') + const [, content] = await emittedOnce(ipcMain, 'answer') expect(content).to.equal('Hello') }) @@ -1726,7 +1721,7 @@ describe(' tag', function () { src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}` }) - const [, content] = await waitForOnce(ipcMain, 'answer') + const [, content] = await emittedOnce(ipcMain, 'answer') expect(content).to.equal('Hello') }) @@ -1738,8 +1733,8 @@ describe(' tag', function () { src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}` }) - const [, {windowOpenReturnedNull}] = await waitForOnce(ipcMain, 'answer') - expect(windowOpenReturnedNull).to.equal(true) + const [, {windowOpenReturnedNull}] = await emittedOnce(ipcMain, 'answer') + expect(windowOpenReturnedNull).to.be.true() }) it('blocks accessing cross-origin frames', async () => { @@ -1748,7 +1743,7 @@ describe(' tag', function () { src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}` }) - const [, content] = await waitForOnce(ipcMain, 'answer') + const [, content] = await emittedOnce(ipcMain, 'answer') const expectedContent = 'Blocked a frame with origin "file://" from accessing a cross-origin frame.' @@ -1772,7 +1767,7 @@ describe(' tag', function () { src: `file://${fixtures}/pages/window-open.html` }) - await waitForOnce(app, 'browser-window-created') + await emittedOnce(app, 'browser-window-created') }) it('emits a web-contents-created event', (done) => {