Close windows instead of destroying them
This commit is contained in:
parent
cce6be1900
commit
0ebe142b2f
10 changed files with 45 additions and 62 deletions
|
@ -5,6 +5,7 @@ const net = require('net')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const {remote} = require('electron')
|
const {remote} = require('electron')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const {app, BrowserWindow, ipcMain} = remote
|
const {app, BrowserWindow, ipcMain} = remote
|
||||||
|
|
||||||
|
@ -19,9 +20,6 @@ describe('electron module', function () {
|
||||||
let window = null
|
let window = null
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
if (window != null) {
|
|
||||||
window.destroy()
|
|
||||||
}
|
|
||||||
window = new BrowserWindow({
|
window = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
|
@ -30,10 +28,7 @@ describe('electron module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (window != null) {
|
return closeWindow(window).then(function () { window = null })
|
||||||
window.destroy()
|
|
||||||
}
|
|
||||||
window = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('always returns the internal electron module', function (done) {
|
it('always returns the internal electron module', function (done) {
|
||||||
|
@ -191,10 +186,7 @@ describe('app module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can import certificate into platform cert store', function (done) {
|
it('can import certificate into platform cert store', function (done) {
|
||||||
|
@ -232,10 +224,7 @@ describe('app module', function () {
|
||||||
var w = null
|
var w = null
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should emit browser-window-focus event when window is focused', function (done) {
|
it('should emit browser-window-focus event when window is focused', function (done) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
const screen = require('electron').screen
|
const screen = require('electron').screen
|
||||||
|
@ -38,9 +39,6 @@ describe('browser-window module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
if (w != null) {
|
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
|
@ -52,10 +50,7 @@ describe('browser-window module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('BrowserWindow.close()', function () {
|
describe('BrowserWindow.close()', function () {
|
||||||
|
|
|
@ -3,6 +3,7 @@ const http = require('http')
|
||||||
const multiparty = require('multiparty')
|
const multiparty = require('multiparty')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
const app = remote.require('electron').app
|
const app = remote.require('electron').app
|
||||||
|
@ -20,7 +21,7 @@ describe('crash-reporter module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
w.destroy()
|
return closeWindow(w).then(function () { w = null })
|
||||||
})
|
})
|
||||||
|
|
||||||
if (process.mas) {
|
if (process.mas) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
const BrowserWindow = require('electron').remote.BrowserWindow
|
const BrowserWindow = require('electron').remote.BrowserWindow
|
||||||
|
|
||||||
describe('debugger module', function () {
|
describe('debugger module', function () {
|
||||||
|
@ -7,9 +8,6 @@ describe('debugger module', function () {
|
||||||
var w = null
|
var w = null
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
if (w != null) {
|
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
|
@ -18,10 +16,7 @@ describe('debugger module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('debugger.attach', function () {
|
describe('debugger.attach', function () {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const {ipcRenderer, remote} = require('electron')
|
const {ipcRenderer, remote} = require('electron')
|
||||||
const {ipcMain, webContents, BrowserWindow} = remote
|
const {ipcMain, webContents, BrowserWindow} = remote
|
||||||
|
@ -17,6 +18,12 @@ const comparePaths = function (path1, path2) {
|
||||||
describe('ipc module', function () {
|
describe('ipc module', function () {
|
||||||
var fixtures = path.join(__dirname, 'fixtures')
|
var fixtures = path.join(__dirname, 'fixtures')
|
||||||
|
|
||||||
|
var w = null
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
return closeWindow(w).then(function () { w = null })
|
||||||
|
})
|
||||||
|
|
||||||
describe('remote.require', function () {
|
describe('remote.require', function () {
|
||||||
it('should returns same object for the same module', function () {
|
it('should returns same object for the same module', function () {
|
||||||
var dialog1 = remote.require('electron')
|
var dialog1 = remote.require('electron')
|
||||||
|
@ -302,19 +309,18 @@ describe('ipc module', function () {
|
||||||
it('does not crash when reply is not sent and browser is destroyed', function (done) {
|
it('does not crash when reply is not sent and browser is destroyed', function (done) {
|
||||||
this.timeout(10000)
|
this.timeout(10000)
|
||||||
|
|
||||||
var w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false
|
show: false
|
||||||
})
|
})
|
||||||
ipcMain.once('send-sync-message', function (event) {
|
ipcMain.once('send-sync-message', function (event) {
|
||||||
event.returnValue = null
|
event.returnValue = null
|
||||||
w.destroy()
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
|
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('does not crash when reply is sent by multiple listeners', function (done) {
|
it('does not crash when reply is sent by multiple listeners', function (done) {
|
||||||
var w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false
|
show: false
|
||||||
})
|
})
|
||||||
ipcMain.on('send-sync-message', function (event) {
|
ipcMain.on('send-sync-message', function (event) {
|
||||||
|
@ -322,7 +328,6 @@ describe('ipc module', function () {
|
||||||
})
|
})
|
||||||
ipcMain.on('send-sync-message', function (event) {
|
ipcMain.on('send-sync-message', function (event) {
|
||||||
event.returnValue = null
|
event.returnValue = null
|
||||||
w.destroy()
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
|
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
|
||||||
|
@ -354,12 +359,6 @@ describe('ipc module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('remote listeners', function () {
|
describe('remote listeners', function () {
|
||||||
var w = null
|
|
||||||
|
|
||||||
afterEach(function () {
|
|
||||||
w.destroy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('can be added and removed correctly', function () {
|
it('can be added and removed correctly', function () {
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false
|
show: false
|
||||||
|
|
|
@ -2,6 +2,7 @@ const assert = require('assert')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const qs = require('querystring')
|
const qs = require('querystring')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
const {BrowserWindow, protocol, webContents} = remote
|
const {BrowserWindow, protocol, webContents} = remote
|
||||||
|
|
||||||
|
@ -896,13 +897,12 @@ describe('protocol module', function () {
|
||||||
|
|
||||||
afterEach(function (done) {
|
afterEach(function (done) {
|
||||||
protocol.unregisterProtocol(standardScheme, function () {
|
protocol.unregisterProtocol(standardScheme, function () {
|
||||||
if (w != null) {
|
closeWindow(w).then(function () {
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
w = null
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('resolves relative resources', function (done) {
|
it('resolves relative resources', function (done) {
|
||||||
var handler = function (request, callback) {
|
var handler = function (request, callback) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ const assert = require('assert')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const {ipcRenderer, remote} = require('electron')
|
const {ipcRenderer, remote} = require('electron')
|
||||||
const {ipcMain, session, BrowserWindow} = remote
|
const {ipcMain, session, BrowserWindow} = remote
|
||||||
|
@ -14,9 +15,6 @@ describe('session module', function () {
|
||||||
var url = 'http://127.0.0.1'
|
var url = 'http://127.0.0.1'
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
if (w != null) {
|
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
|
@ -25,10 +23,7 @@ describe('session module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('session.defaultSession', function () {
|
describe('session.defaultSession', function () {
|
||||||
|
@ -194,7 +189,7 @@ describe('session module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
w.destroy()
|
return closeWindow(w).then(function () { w = null })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can cancel default download behavior', function (done) {
|
it('can cancel default download behavior', function (done) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const {remote} = require('electron')
|
const {remote} = require('electron')
|
||||||
const {BrowserWindow, webContents} = remote
|
const {BrowserWindow, webContents} = remote
|
||||||
|
@ -13,9 +14,6 @@ describe('webContents module', function () {
|
||||||
let w
|
let w
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
if (w != null) {
|
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
|
@ -27,10 +25,7 @@ describe('webContents module', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
if (w != null) {
|
return closeWindow(w).then(function () { w = null })
|
||||||
w.destroy()
|
|
||||||
}
|
|
||||||
w = null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getAllWebContents() API', function () {
|
describe('getAllWebContents() API', function () {
|
||||||
|
|
|
@ -2,6 +2,7 @@ const assert = require('assert')
|
||||||
const ChildProcess = require('child_process')
|
const ChildProcess = require('child_process')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
const nativeImage = require('electron').nativeImage
|
const nativeImage = require('electron').nativeImage
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
|
@ -794,8 +795,8 @@ describe('asar package', function () {
|
||||||
|
|
||||||
it('sets __dirname correctly', function (done) {
|
it('sets __dirname correctly', function (done) {
|
||||||
after(function () {
|
after(function () {
|
||||||
w.destroy()
|
|
||||||
ipcMain.removeAllListeners('dirname')
|
ipcMain.removeAllListeners('dirname')
|
||||||
|
return closeWindow(w).then(function () { w = null })
|
||||||
})
|
})
|
||||||
|
|
||||||
var w = new BrowserWindow({
|
var w = new BrowserWindow({
|
||||||
|
@ -818,8 +819,8 @@ describe('asar package', function () {
|
||||||
|
|
||||||
it('loads script tag in html', function (done) {
|
it('loads script tag in html', function (done) {
|
||||||
after(function () {
|
after(function () {
|
||||||
w.destroy()
|
|
||||||
ipcMain.removeAllListeners('ping')
|
ipcMain.removeAllListeners('ping')
|
||||||
|
return closeWindow(w).then(function () { w = null })
|
||||||
})
|
})
|
||||||
|
|
||||||
var w = new BrowserWindow({
|
var w = new BrowserWindow({
|
||||||
|
|
13
spec/window-helpers.js
Normal file
13
spec/window-helpers.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
exports.closeWindow = (window) => {
|
||||||
|
if (window == null || window.isDestroyed()) {
|
||||||
|
return Promise.resolve()
|
||||||
|
} else {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
window.once('closed', () => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
window.setClosable(true)
|
||||||
|
window.close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue