From 515f6898148474f499a8d5b1452c27bede0004e8 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 26 Jan 2017 09:42:45 -0800 Subject: [PATCH 1/4] Use helper function to show last dev tools panel --- spec/api-browser-window-spec.js | 58 ++++++++++++++------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 3942f8183634..5255cd55e425 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1663,9 +1663,29 @@ describe('BrowserWindow module', function () { }) describe('dev tool extensions', function () { - describe('BrowserWindow.addDevToolsExtension', function () { - let showPanelIntervalId + let showPanelIntervalId + const showLastDevToolsPanel = () => { + w.webContents.once('devtools-opened', function () { + showPanelIntervalId = setInterval(function () { + if (w && w.devToolsWebContents) { + var showLastPanel = function () { + var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id + WebInspector.inspectorView.showPanel(lastPanelId) + } + w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`) + } else { + clearInterval(showPanelIntervalId) + } + }, 100) + }) + } + + afterEach(function () { + clearInterval(showPanelIntervalId) + }) + + describe('BrowserWindow.addDevToolsExtension', function () { beforeEach(function () { BrowserWindow.removeDevToolsExtension('foo') assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false) @@ -1674,27 +1694,11 @@ describe('BrowserWindow module', function () { BrowserWindow.addDevToolsExtension(extensionPath) assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true) - w.webContents.on('devtools-opened', function () { - showPanelIntervalId = setInterval(function () { - if (w && w.devToolsWebContents) { - var showLastPanel = function () { - var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id - WebInspector.inspectorView.showPanel(lastPanelId) - } - w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`) - } else { - clearInterval(showPanelIntervalId) - } - }, 100) - }) + showLastDevToolsPanel() w.loadURL('about:blank') }) - afterEach(function () { - clearInterval(showPanelIntervalId) - }) - it('throws errors for missing manifest.json files', function () { assert.throws(function () { BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist')) @@ -1760,21 +1764,7 @@ describe('BrowserWindow module', function () { BrowserWindow.removeDevToolsExtension('foo') BrowserWindow.addDevToolsExtension(extensionPath) - let showPanelIntervalId = null - - w.webContents.once('devtools-opened', function () { - showPanelIntervalId = setInterval(function () { - if (w && w.devToolsWebContents) { - var showLastPanel = function () { - var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id - WebInspector.inspectorView.showPanel(lastPanelId) - } - w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`) - } else { - clearInterval(showPanelIntervalId) - } - }, 100) - }) + showLastDevToolsPanel() w.loadURL('about:blank') w.webContents.openDevTools({mode: 'bottom'}) From e2e33a8cf093e9d24c84b5a41a7f769a9fc805ea Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 26 Jan 2017 09:56:40 -0800 Subject: [PATCH 2/4] Guard against destroyed window or web contents --- spec/api-browser-window-spec.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 5255cd55e425..00a21c21890c 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1668,15 +1668,22 @@ describe('BrowserWindow module', function () { const showLastDevToolsPanel = () => { w.webContents.once('devtools-opened', function () { showPanelIntervalId = setInterval(function () { - if (w && w.devToolsWebContents) { - var showLastPanel = function () { - var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id - WebInspector.inspectorView.showPanel(lastPanelId) - } - w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`) - } else { - clearInterval(showPanelIntervalId) + if (w == null || w.isDestroyed()) { + clearInterval(showLastDevToolsPanel) + return } + + const {devToolsWebContents} = w + if (devToolsWebContents == null || devToolsWebContents.isDestroyed()) { + clearInterval(showLastDevToolsPanel) + return + } + + var showLastPanel = function () { + var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id + WebInspector.inspectorView.showPanel(lastPanelId) + } + devToolsWebContents.executeJavaScript(`(${showLastPanel})()`) }, 100) }) } @@ -1770,7 +1777,6 @@ describe('BrowserWindow module', function () { w.webContents.openDevTools({mode: 'bottom'}) ipcMain.once('answer', function (event, message) { - clearInterval(showPanelIntervalId) assert.equal(message.runtimeId, 'foo') done() }) From a851695bb90172c3f04cb8155ebf6a91c793d0b6 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 26 Jan 2017 09:57:53 -0800 Subject: [PATCH 3/4] Clear interval before setting new one --- spec/api-browser-window-spec.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 00a21c21890c..f54a3e0c39b3 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1667,6 +1667,8 @@ describe('BrowserWindow module', function () { const showLastDevToolsPanel = () => { w.webContents.once('devtools-opened', function () { + clearInterval(showPanelIntervalId) + showPanelIntervalId = setInterval(function () { if (w == null || w.isDestroyed()) { clearInterval(showLastDevToolsPanel) From 2cfa67bdffcfb5b61d503126b51a1eaeb47fef5f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 26 Jan 2017 09:59:07 -0800 Subject: [PATCH 4/4] Remove linter warning --- spec/api-browser-window-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index f54a3e0c39b3..9ca429136ed7 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1670,7 +1670,7 @@ describe('BrowserWindow module', function () { clearInterval(showPanelIntervalId) showPanelIntervalId = setInterval(function () { - if (w == null || w.isDestroyed()) { + if (w == null || w.isDestroyed()) { clearInterval(showLastDevToolsPanel) return }