From f64ffe36d6224e2f92bd0f4d4b0b40ae4f2a87c7 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 17 Jan 2019 10:24:49 -0800 Subject: [PATCH 1/3] test: test legacy callback functions --- lib/browser/api/app.js | 2 +- lib/browser/api/session.js | 2 +- lib/browser/api/web-contents.js | 2 +- spec/api-app-spec.js | 32 ++++++++++++++++++++++++- spec/api-browser-window-spec.js | 36 +++++++++++++++++++++++++++- spec/api-protocol-spec.js | 42 ++++++++++++++++++++++++++++++--- 6 files changed, 108 insertions(+), 8 deletions(-) diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 06ff5b5972363..694ab2e59bb67 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -33,7 +33,7 @@ Object.assign(app, { } }) -app.getFileIcon = deprecate.promisify(app.getFileIcon, 2) +app.getFileIcon = deprecate.promisify(app.getFileIcon, 3) const nativeAppMetrics = app.getAppMetrics app.getAppMetrics = () => { diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index 89d3802098823..fa9cbaae7e9c2 100644 --- a/lib/browser/api/session.js +++ b/lib/browser/api/session.js @@ -20,6 +20,6 @@ Object.setPrototypeOf(Session.prototype, EventEmitter.prototype) Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype) Session.prototype._init = function () { - this.protocol.isProtocolHandled = deprecate.promisify(this.protocol.isProtocolHandled, 1) + this.protocol.isProtocolHandled = deprecate.promisify(this.protocol.isProtocolHandled, 2) app.emit('session-created', this) } diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 683be389b8c7e..26c4d49577d2f 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -334,7 +334,7 @@ WebContents.prototype._init = function () { // render-view-deleted event, so ignore the listeners warning. this.setMaxListeners(0) - this.capturePage = deprecate.promisify(this.capturePage, 1) + this.capturePage = deprecate.promisify(this.capturePage, 2) // Dispatch IPC messages to the ipc module. this.on('ipc-message', function (event, [channel, ...args]) { diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 9084c7ec4c235..02bed5aea8bf0 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -836,7 +836,7 @@ describe('app module', () => { }) }) - describe('getFileIcon() API', () => { + describe('getFileIcon() API', (done) => { const iconPath = path.join(__dirname, 'fixtures/assets/icon.ico') const sizes = { small: 16, @@ -858,6 +858,14 @@ describe('app module', () => { expect(icon.isEmpty()).to.be.false() }) + // TODO(codebytere): remove when promisification is complete + it('fetches a non-empty icon (callback)', () => { + app.getFileIcon(iconPath, (icon) => { + expect(icon.isEmpty()).to.be.false() + done() + }) + }) + it('fetches normal icon size by default', async () => { const icon = await app.getFileIcon(iconPath) const size = icon.getSize() @@ -866,6 +874,17 @@ describe('app module', () => { expect(size.width).to.equal(sizes.normal) }) + // TODO(codebytere): remove when promisification is complete + it('fetches normal icon size by default (callback)', () => { + app.getFileIcon(iconPath, (icon) => { + const size = icon.getSize() + + expect(size.height).to.equal(sizes.normal) + expect(size.width).to.equal(sizes.normal) + done() + }) + }) + describe('size option', () => { it('fetches a small icon', async () => { const icon = await app.getFileIcon(iconPath, { size: 'small' }) @@ -883,6 +902,17 @@ describe('app module', () => { expect(size.width).to.equal(sizes.normal) }) + // TODO(codebytere): remove when promisification is complete + it('fetches a normal icon (callback)', () => { + app.getFileIcon(iconPath, { size: 'normal' }, (icon) => { + const size = icon.getSize() + + expect(size.height).to.equal(sizes.normal) + expect(size.width).to.equal(sizes.normal) + done() + }) + }) + it('fetches a large icon', async () => { // macOS does not support large icons if (process.platform === 'darwin') return diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 5b41755cdc687..23c1f760dceec 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -483,7 +483,7 @@ describe('BrowserWindow module', () => { }) }) - describe('BrowserWindow.getFocusedWindow()', (done) => { + describe('BrowserWindow.getFocusedWindow()', () => { it('returns the opener window when dev tools window is focused', (done) => { w.show() w.webContents.once('devtools-focused', () => { @@ -506,6 +506,19 @@ describe('BrowserWindow module', () => { expect(image.isEmpty()).to.be.true() }) + // TODO(codebytere): remove when promisification is complete + it('returns a Promise with a Buffer (callback)', (done) => { + w.capturePage({ + x: 0, + y: 0, + width: 100, + height: 100 + }, (image) => { + expect(image.isEmpty()).to.be.true() + done() + }) + }) + it('preserves transparency', async () => { const w = await openTheWindow({ show: false, @@ -525,6 +538,27 @@ describe('BrowserWindow module', () => { // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha expect(imgBuffer[25]).to.equal(6) }) + + it('preserves transparency (callback)', async (done) => { + const w = await openTheWindow({ + show: false, + width: 400, + height: 400, + transparent: true + }) + const p = emittedOnce(w, 'ready-to-show') + w.loadURL('data:text/html,') + await p + w.show() + + w.capturePage((image) => { + const imgBuffer = image.toPNG() + // Check the 25th byte in the PNG. + // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha + expect(imgBuffer[25]).to.equal(6) + done() + }) + }) }) describe('BrowserWindow.setBounds(bounds[, animate])', () => { diff --git a/spec/api-protocol-spec.js b/spec/api-protocol-spec.js index 1fe687bc5f1ae..543778adf4cd4 100644 --- a/spec/api-protocol-spec.js +++ b/spec/api-protocol-spec.js @@ -656,17 +656,31 @@ describe('protocol module', () => { }) }) - describe('protocol.isProtocolHandled', () => { + describe('protocol.isProtocolHandled', (done) => { it('returns true for about:', async () => { const result = await protocol.isProtocolHandled('about') assert.strictEqual(result, true) }) + it('returns true for about: (callback)', () => { + protocol.isProtocolHandled('about', (result) => { + assert.strictEqual(result, true) + done() + }) + }) + it('returns true for file:', async () => { const result = await protocol.isProtocolHandled('file') assert.strictEqual(result, true) }) + it('returns true for file: (callback)', () => { + protocol.isProtocolHandled('file', (result) => { + assert.strictEqual(result, true) + done() + }) + }) + it('returns true for http:', async () => { const result = await protocol.isProtocolHandled('http') assert.strictEqual(result, true) @@ -682,7 +696,7 @@ describe('protocol module', () => { assert.strictEqual(result, false) }) - it('returns true for custom protocol', (done) => { + it('returns true for custom protocol', () => { const emptyHandler = (request, callback) => callback() protocol.registerStringProtocol(protocolName, emptyHandler, async (error) => { assert.strictEqual(error, null) @@ -692,7 +706,18 @@ describe('protocol module', () => { }) }) - it('returns true for intercepted protocol', (done) => { + it('returns true for custom protocol (callback)', () => { + const emptyHandler = (request, callback) => callback() + protocol.registerStringProtocol(protocolName, emptyHandler, (error) => { + assert.strictEqual(error, null) + protocol.isProtocolHandled(protocolName, (result) => { + assert.strictEqual(result, true) + done() + }) + }) + }) + + it('returns true for intercepted protocol', () => { const emptyHandler = (request, callback) => callback() protocol.interceptStringProtocol('http', emptyHandler, async (error) => { assert.strictEqual(error, null) @@ -701,6 +726,17 @@ describe('protocol module', () => { done() }) }) + + it('returns true for intercepted protocol (callback)', () => { + const emptyHandler = (request, callback) => callback() + protocol.interceptStringProtocol('http', emptyHandler, (error) => { + assert.strictEqual(error, null) + protocol.isProtocolHandled('http', (result) => { + assert.strictEqual(result, true) + done() + }) + }) + }) }) describe('protocol.intercept(Any)Protocol', () => { From 35b01e95bc6d95d13df986f6735468e8241190f4 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 17 Jan 2019 10:29:55 -0800 Subject: [PATCH 2/3] add TODO removal comments --- spec/api-browser-window-spec.js | 1 + spec/api-protocol-spec.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 23c1f760dceec..8d012adc35fc8 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -539,6 +539,7 @@ describe('BrowserWindow module', () => { expect(imgBuffer[25]).to.equal(6) }) + // TODO(codebytere): remove when promisification is complete it('preserves transparency (callback)', async (done) => { const w = await openTheWindow({ show: false, diff --git a/spec/api-protocol-spec.js b/spec/api-protocol-spec.js index 543778adf4cd4..cedc87380c827 100644 --- a/spec/api-protocol-spec.js +++ b/spec/api-protocol-spec.js @@ -662,6 +662,7 @@ describe('protocol module', () => { assert.strictEqual(result, true) }) + // TODO(codebytere): remove when promisification is complete it('returns true for about: (callback)', () => { protocol.isProtocolHandled('about', (result) => { assert.strictEqual(result, true) @@ -674,6 +675,7 @@ describe('protocol module', () => { assert.strictEqual(result, true) }) + // TODO(codebytere): remove when promisification is complete it('returns true for file: (callback)', () => { protocol.isProtocolHandled('file', (result) => { assert.strictEqual(result, true) @@ -706,6 +708,7 @@ describe('protocol module', () => { }) }) + // TODO(codebytere): remove when promisification is complete it('returns true for custom protocol (callback)', () => { const emptyHandler = (request, callback) => callback() protocol.registerStringProtocol(protocolName, emptyHandler, (error) => { @@ -727,6 +730,7 @@ describe('protocol module', () => { }) }) + // TODO(codebytere): remove when promisification is complete it('returns true for intercepted protocol (callback)', () => { const emptyHandler = (request, callback) => callback() protocol.interceptStringProtocol('http', emptyHandler, (error) => { From a2b91ed8d8d651ff4472808327211fe1963c1361 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 17 Jan 2019 12:14:56 -0800 Subject: [PATCH 3/3] fix callback spec --- spec/api-browser-window-spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 8d012adc35fc8..a9d9b6a3fe711 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -494,7 +494,7 @@ describe('BrowserWindow module', () => { }) }) - describe('BrowserWindow.capturePage(rect)', () => { + describe('BrowserWindow.capturePage(rect)', (done) => { it('returns a Promise with a Buffer', async () => { const image = await w.capturePage({ x: 0, @@ -507,7 +507,7 @@ describe('BrowserWindow module', () => { }) // TODO(codebytere): remove when promisification is complete - it('returns a Promise with a Buffer (callback)', (done) => { + it('returns a Promise with a Buffer (callback)', () => { w.capturePage({ x: 0, y: 0, @@ -540,7 +540,7 @@ describe('BrowserWindow module', () => { }) // TODO(codebytere): remove when promisification is complete - it('preserves transparency (callback)', async (done) => { + it('preserves transparency (callback)', async () => { const w = await openTheWindow({ show: false, width: 400,