chore: [tests] update some BrowserWindow tests

- manually close an existing windows before creating a new one.
  We have to do it because the `afterEach` hook doesn't get called
  if a test fails.
- add some async/await goodies
This commit is contained in:
Aleksei Kuzmin 2018-07-24 12:32:23 +02:00 committed by Jeremy Apthorp
parent 3094f62f0b
commit ea16445be7

View file

@ -1,7 +1,8 @@
'use strict' 'use strict'
const assert = require('assert') const assert = require('assert')
const {expect} = require('chai') const chai = require('chai')
const dirtyChai = require('dirty-chai')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const os = require('os') const os = require('os')
@ -13,10 +14,12 @@ const {ipcRenderer, remote, screen} = require('electron')
const {app, ipcMain, BrowserWindow, BrowserView, protocol, session, webContents} = remote const {app, ipcMain, BrowserWindow, BrowserView, protocol, session, webContents} = remote
const features = process.atomBinding('features') const features = process.atomBinding('features')
const {expect} = chai
const isCI = remote.getGlobal('isCi') const isCI = remote.getGlobal('isCi')
const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled') const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
chai.use(dirtyChai)
describe('BrowserWindow module', () => { describe('BrowserWindow module', () => {
const fixtures = path.resolve(__dirname, 'fixtures') const fixtures = path.resolve(__dirname, 'fixtures')
let w = null let w = null
@ -24,6 +27,24 @@ describe('BrowserWindow module', () => {
let server let server
let postData let postData
const defaultOptions = {
show: false,
width: 400,
height: 400,
webPreferences: {
backgroundThrottling: false
}
}
const openTheWindow = async (options = defaultOptions) => {
// The `afterEach` hook isn't called if a test fails,
// we should make sure that the window is closed ourselves.
await closeTheWindow()
w = new BrowserWindow(options)
return w
}
const closeTheWindow = function () { const closeTheWindow = function () {
return closeWindow(w).then(() => { w = null }) return closeWindow(w).then(() => { w = null })
} }
@ -78,24 +99,13 @@ describe('BrowserWindow module', () => {
server = null server = null
}) })
beforeEach(() => { beforeEach(openTheWindow)
w = new BrowserWindow({
show: false,
width: 400,
height: 400,
webPreferences: {
backgroundThrottling: false
}
})
})
afterEach(closeTheWindow) afterEach(closeTheWindow)
describe('BrowserWindow constructor', () => { describe('BrowserWindow constructor', () => {
it('allows passing void 0 as the webContents', () => { it('allows passing void 0 as the webContents', () => {
w.close() openTheWindow({
w = null
w = new BrowserWindow({
webContents: void 0 webContents: void 0
}) })
}) })
@ -412,50 +422,48 @@ describe('BrowserWindow module', () => {
}) })
describe('BrowserWindow.capturePage(rect, callback)', () => { describe('BrowserWindow.capturePage(rect, callback)', () => {
it('calls the callback with a Buffer', (done) => { it('calls the callback with a Buffer', async () => {
const image = await new Promise((resolve) => {
w.capturePage({ w.capturePage({
x: 0, x: 0,
y: 0, y: 0,
width: 100, width: 100,
height: 100 height: 100
}, (image) => { }, resolve)
assert.equal(image.isEmpty(), true)
done()
})
}) })
it('preserves transparency', (done) => { expect(image.isEmpty()).to.be.true()
w.close() })
const width = 400
const height = 400 it('preserves transparency', async () => {
w = new BrowserWindow({ const w = await openTheWindow({
show: false, show: false,
width: width, width: 400,
height: height, height: 400,
transparent: true transparent: true
}) })
w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>') w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>')
w.once('ready-to-show', () => { await emittedOnce(w, 'ready-to-show')
w.show() w.show()
w.capturePage((image) => {
let imgBuffer = image.toPNG() const image = await new Promise((resolve) => w.capturePage(resolve))
// Check 25th byte in the PNG 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 // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha
assert.equal(imgBuffer[25], 6) expect(imgBuffer[25]).to.equal(6)
done()
})
})
}) })
}) })
describe('BrowserWindow.setSize(width, height)', () => { describe('BrowserWindow.setSize(width, height)', () => {
it('sets the window size', (done) => { it('sets the window size', async () => {
const size = [300, 400] const size = [300, 400]
w.once('resize', () => {
assertBoundsEqual(w.getSize(), size) const resized = emittedOnce(w, 'resize')
done()
})
w.setSize(size[0], size[1]) w.setSize(size[0], size[1])
await resized
assertBoundsEqual(w.getSize(), size)
}) })
}) })