Skip to content

Commit

Permalink
re-enable, refactor and move visibilitychange specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Jul 9, 2019
1 parent 74e28df commit 90f9cfb
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 194 deletions.
144 changes: 144 additions & 0 deletions spec-main/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2282,4 +2282,148 @@ describe('BrowserWindow module', () => {
w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
})
})

describe.only('document.visibilityState/hidden', () => {
afterEach(closeAllWindows)

it('visibilityState is initially visible despite window being hidden', async () => {
const w = new BrowserWindow({
show: false,
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

let readyToShow = false
w.once('ready-to-show', () => {
readyToShow = true
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))

const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')

expect(readyToShow).to.be.false('ready to show')
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false('hidden')
})

it('visibilityState changes when window is hidden', async () => {
const w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))

{
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false('hidden')
}

w.hide()

{
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('hidden')
expect(hidden).to.be.true('hidden')
}
})

it('visibilityState changes when window is shown', async () => {
const w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
await emittedOnce(w, 'show')
w.hide()
w.show()
const [, visibilityState] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('visible')
})

ifit(!(isCI && process.platform === 'win32'))('visibilityState changes when window is shown inactive', async () => {
const w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
await emittedOnce(w, 'show')
w.hide()
w.showInactive()
const [, visibilityState] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('visible')
})

ifit(!(isCI && process.platform === 'linux'))('visibilityState changes when window is minimized', async () => {
const w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))

{
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false('hidden')
}

w.minimize()

{
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('hidden')
expect(hidden).to.be.true('hidden')
}
})

it('visibilityState remains visible if backgroundThrottling is disabled', async () => {
const w = new BrowserWindow({
show: false,
width: 100,
height: 100,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
{
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong')
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false('hidden')
}

ipcMain.once('pong', (event, visibilityState, hidden) => {
throw new Error(`Unexpected visibility change event. visibilityState: ${visibilityState} hidden: ${hidden}`)
})
try {
w.show()
await emittedOnce(w, 'show')
w.hide()
await emittedOnce(w, 'hide')
w.show()
await emittedOnce(w, 'show')
} finally {
ipcMain.removeAllListeners('pong')
}
})
})
})
194 changes: 0 additions & 194 deletions spec/api-browser-window-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,200 +112,6 @@ describe('BrowserWindow module', () => {

afterEach(closeTheWindow)

// visibilitychange event is broken upstream, see crbug.com/920839
xdescribe('document.visibilityState/hidden', () => {
beforeEach(() => { w.destroy() })

function onVisibilityChange (callback) {
ipcMain.on('pong', (event, visibilityState, hidden) => {
if (event.sender.id === w.webContents.id) {
callback(visibilityState, hidden)
}
})
}

function onNextVisibilityChange (callback) {
ipcMain.once('pong', (event, visibilityState, hidden) => {
if (event.sender.id === w.webContents.id) {
callback(visibilityState, hidden)
}
})
}

afterEach(() => { ipcMain.removeAllListeners('pong') })

it('visibilityState is initially visible despite window being hidden', (done) => {
w = new BrowserWindow({
show: false,
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

let readyToShow = false
w.once('ready-to-show', () => {
readyToShow = true
})

onNextVisibilityChange((visibilityState, hidden) => {
expect(readyToShow).to.be.false()
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false()

done()
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
it('visibilityState changes when window is hidden', (done) => {
w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

onNextVisibilityChange((visibilityState, hidden) => {
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false()

onNextVisibilityChange((visibilityState, hidden) => {
expect(visibilityState).to.equal('hidden')
expect(hidden).to.be.true()
done()
})

w.hide()
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
it('visibilityState changes when window is shown', (done) => {
w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

onNextVisibilityChange((visibilityState, hidden) => {
onVisibilityChange((visibilityState, hidden) => {
if (!hidden) {
expect(visibilityState).to.equal('visible')
done()
}
})

w.hide()
w.show()
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
it('visibilityState changes when window is shown inactive', function (done) {
if (isCI && process.platform === 'win32') {
// FIXME(alexeykuzmin): Skip the test instead of marking it as passed.
// afterEach hook won't be run if a test is skipped dynamically.
// If afterEach isn't run current window won't be destroyed
// and the next test will fail on assertion in `closeWindow()`.
// this.skip()
return done()
}

w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

onNextVisibilityChange((visibilityState, hidden) => {
onVisibilityChange((visibilityState, hidden) => {
if (!hidden) {
expect(visibilityState).to.equal('visible')
done()
}
})

w.hide()
w.showInactive()
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
it('visibilityState changes when window is minimized', function (done) {
if (isCI && process.platform === 'linux') {
// FIXME(alexeykuzmin): Skip the test instead of marking it as passed.
// afterEach hook won't be run if a test is skipped dynamically.
// If afterEach isn't run current window won't be destroyed
// and the next test will fail on assertion in `closeWindow()`.
// this.skip()
return done()
}

w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})

onNextVisibilityChange((visibilityState, hidden) => {
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false()

onNextVisibilityChange((visibilityState, hidden) => {
expect(visibilityState).to.equal('hidden')
expect(hidden).to.be.true()
done()
})

w.minimize()
})

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
it('visibilityState remains visible if backgroundThrottling is disabled', (done) => {
w = new BrowserWindow({
show: false,
width: 100,
height: 100,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true
}
})

onNextVisibilityChange((visibilityState, hidden) => {
expect(visibilityState).to.equal('visible')
expect(hidden).to.be.false()

onNextVisibilityChange((visibilityState, hidden) => {
done(new Error(`Unexpected visibility change event. visibilityState: ${visibilityState} hidden: ${hidden}`))
})
})

w.once('show', () => {
w.once('hide', () => {
w.once('show', () => {
done()
})
w.show()
})
w.hide()
})
w.show()

w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
})
})

describe('new-window event', () => {
before(function () {
if (isCI && process.platform === 'darwin') {
Expand Down

0 comments on commit 90f9cfb

Please sign in to comment.