From f105c84349f8ece35a619ebdc74180f45aeb75b9 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 17 Jan 2019 14:17:16 -0800 Subject: [PATCH] test: ensure legacy callback functions work (#16436) * test: test legacy callback functions * add TODO removal comments * fix callback spec --- 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 | 39 ++++++++++++++++++++++++++-- spec/api-protocol-spec.js | 46 ++++++++++++++++++++++++++++++--- 6 files changed, 114 insertions(+), 9 deletions(-) diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 06ff5b597236..694ab2e59bb6 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 89d380209882..fa9cbaae7e9c 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 683be389b8c7..26c4d49577d2 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 9084c7ec4c23..02bed5aea8bf 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 5b41755cdc68..a9d9b6a3fe71 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', () => { @@ -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, @@ -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)', () => { + 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,28 @@ 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) }) + + // TODO(codebytere): remove when promisification is complete + it('preserves transparency (callback)', async () => { + 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 1fe687bc5f1a..cedc87380c82 100644 --- a/spec/api-protocol-spec.js +++ b/spec/api-protocol-spec.js @@ -656,17 +656,33 @@ 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) }) + // TODO(codebytere): remove when promisification is complete + 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) }) + // TODO(codebytere): remove when promisification is complete + 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 +698,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 +708,19 @@ describe('protocol module', () => { }) }) - it('returns true for intercepted protocol', (done) => { + // TODO(codebytere): remove when promisification is complete + 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 +729,18 @@ describe('protocol module', () => { done() }) }) + + // TODO(codebytere): remove when promisification is complete + 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', () => {