Skip to content

Commit

Permalink
test: ensure legacy callback functions work (#16436)
Browse files Browse the repository at this point in the history
* test: test legacy callback functions

* add TODO removal comments

* fix callback spec
  • Loading branch information
codebytere committed Jan 17, 2019
1 parent 720197f commit f105c84
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/browser/api/app.js
Expand Up @@ -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 = () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/session.js
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion lib/browser/api/web-contents.js
Expand Up @@ -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]) {
Expand Down
32 changes: 31 additions & 1 deletion spec/api-app-spec.js
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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' })
Expand All @@ -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
Expand Down
39 changes: 37 additions & 2 deletions spec/api-browser-window-spec.js
Expand Up @@ -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', () => {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,<html><body background-color: rgba(255,255,255,0)></body></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])', () => {
Expand Down
46 changes: 43 additions & 3 deletions spec/api-protocol-spec.js
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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', () => {
Expand Down

0 comments on commit f105c84

Please sign in to comment.