From 4e0da56296999c457df6925264b539a61282cdcc Mon Sep 17 00:00:00 2001 From: yuanwhy <710136012@qq.com> Date: Mon, 29 Aug 2016 22:52:26 +0800 Subject: [PATCH 01/10] send event to save state after 'Add Project Folder' or 'Remove Project Folder --- src/application-delegate.coffee | 2 ++ src/main-process/atom-application.coffee | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/application-delegate.coffee b/src/application-delegate.coffee index 853ac54f5..20e4a54cb 100644 --- a/src/application-delegate.coffee +++ b/src/application-delegate.coffee @@ -126,6 +126,8 @@ class ApplicationDelegate loadSettings = getWindowLoadSettings() loadSettings['initialPaths'] = paths setWindowLoadSettings(loadSettings) + ipcRenderer.send("did-save-state") + setAutoHideWindowMenuBar: (autoHide) -> ipcRenderer.send("call-window-method", "setAutoHideMenuBar", autoHide) diff --git a/src/main-process/atom-application.coffee b/src/main-process/atom-application.coffee index 0fd7f5630..d4be3ea27 100644 --- a/src/main-process/atom-application.coffee +++ b/src/main-process/atom-application.coffee @@ -351,6 +351,9 @@ class AtomApplication @fileRecoveryService.didSavePath(@windowForEvent(event), path) event.returnValue = true + @disposable.add ipcHelpers.on ipcMain, 'did-save-state', => + @saveState(false) + setupDockMenu: -> if process.platform is 'darwin' dockMenu = Menu.buildFromTemplate [ From 1c1640c1d8539f46eade7bf645b84b901b92c356 Mon Sep 17 00:00:00 2001 From: yuanwhy <710136012@qq.com> Date: Tue, 30 Aug 2016 20:29:05 +0800 Subject: [PATCH 02/10] rename to did-change-paths --- src/application-delegate.coffee | 2 +- src/main-process/atom-application.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/application-delegate.coffee b/src/application-delegate.coffee index 20e4a54cb..4e04df7cd 100644 --- a/src/application-delegate.coffee +++ b/src/application-delegate.coffee @@ -126,7 +126,7 @@ class ApplicationDelegate loadSettings = getWindowLoadSettings() loadSettings['initialPaths'] = paths setWindowLoadSettings(loadSettings) - ipcRenderer.send("did-save-state") + ipcRenderer.send("did-change-paths") setAutoHideWindowMenuBar: (autoHide) -> diff --git a/src/main-process/atom-application.coffee b/src/main-process/atom-application.coffee index d4be3ea27..51ea70af9 100644 --- a/src/main-process/atom-application.coffee +++ b/src/main-process/atom-application.coffee @@ -351,7 +351,7 @@ class AtomApplication @fileRecoveryService.didSavePath(@windowForEvent(event), path) event.returnValue = true - @disposable.add ipcHelpers.on ipcMain, 'did-save-state', => + @disposable.add ipcHelpers.on ipcMain, 'did-change-paths', => @saveState(false) setupDockMenu: -> From 1320ff28c39afd563820b30b43389ea38552e74c Mon Sep 17 00:00:00 2001 From: yuanwhy <710136012@qq.com> Date: Tue, 30 Aug 2016 23:00:20 +0800 Subject: [PATCH 03/10] delete empty line --- src/application-delegate.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/application-delegate.coffee b/src/application-delegate.coffee index 4e04df7cd..b55ab6964 100644 --- a/src/application-delegate.coffee +++ b/src/application-delegate.coffee @@ -128,7 +128,6 @@ class ApplicationDelegate setWindowLoadSettings(loadSettings) ipcRenderer.send("did-change-paths") - setAutoHideWindowMenuBar: (autoHide) -> ipcRenderer.send("call-window-method", "setAutoHideMenuBar", autoHide) From bc77a79e86d70048277476fdb0fbaca161614b99 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 7 Jan 2017 10:59:22 +0300 Subject: [PATCH 04/10] Add test to ensure the state saved when project folders changed. --- spec/main-process/atom-application.test.js | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index 8af139f21..b9dd62042 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -41,6 +41,39 @@ describe('AtomApplication', function () { electron.app.quit = originalAppQuit }) + describe('project paths', function () { + it('sync application state on changes', async function () { + const dirA = makeTempDir() + const dirB = makeTempDir() + const atomApplication = buildAtomApplication() + + const window = atomApplication.launch(parseCommandLine([])) + await focusWindow(window) + + const addProjectPathFn = function (dir) { + return 'function (sendBackToMainProcess) { atom.project.addPath("' + dir + '"); sendBackToMainProcess(null); }' + } + const removeProjectPathFn = function (dir) { + return 'function (sendBackToMainProcess) { atom.project.removePath("' + dir + '"); sendBackToMainProcess(null); }' + } + + await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) + + const appState1 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState1[0].initialPaths, [dirA]) + + await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) + + const appState2 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState2[0].initialPaths, [dirA, dirB]) + + await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) + + const appState3 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState3[0].initialPaths, [dirB]) + }) + }) + describe('launch', function () { it('can open to a specific line number of a file', async function () { const filePath = path.join(makeTempDir(), 'new-file') From 4ba6919d4d68f1bab5d77a2fba1eca0eb4b7ecc6 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 7 Jan 2017 13:04:05 +0300 Subject: [PATCH 05/10] Try fix tests on Windows. Only count AtomApplication#saveState calls, not saved content. --- spec/main-process/atom-application.test.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index b9dd62042..5bebcb1d3 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -47,6 +47,13 @@ describe('AtomApplication', function () { const dirB = makeTempDir() const atomApplication = buildAtomApplication() + let cnt = 0 + const origSaveState = atomApplication.saveState + const mockSaveState = function () { + cnt++ + origSaveState.apply(atomApplication, arguments) + } + const window = atomApplication.launch(parseCommandLine([])) await focusWindow(window) @@ -57,20 +64,13 @@ describe('AtomApplication', function () { return 'function (sendBackToMainProcess) { atom.project.removePath("' + dir + '"); sendBackToMainProcess(null); }' } + atomApplication.saveState = mockSaveState await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) - - const appState1 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) - assert.deepEqual(appState1[0].initialPaths, [dirA]) - + assert.equal(cnt, 1) await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) - - const appState2 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) - assert.deepEqual(appState2[0].initialPaths, [dirA, dirB]) - + assert.equal(cnt, 2) await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) - - const appState3 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) - assert.deepEqual(appState3[0].initialPaths, [dirB]) + assert.equal(cnt, 3) }) }) From 94f28a3877a9d742b6a87a374d86ecf468534d2a Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 7 Jan 2017 14:10:24 +0300 Subject: [PATCH 06/10] Another try to fix tests on Windows. Wait for ipc. --- spec/main-process/atom-application.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index 5bebcb1d3..a5e28b238 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -66,10 +66,13 @@ describe('AtomApplication', function () { atomApplication.saveState = mockSaveState await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) + await conditionPromise(() => cnt === 1) assert.equal(cnt, 1) await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) + await conditionPromise(() => cnt === 2) assert.equal(cnt, 2) await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) + await conditionPromise(() => cnt === 3) assert.equal(cnt, 3) }) }) From 1fb066ad682aa506cdccf232056df13be2ebb425 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 7 Jan 2017 23:11:31 +0300 Subject: [PATCH 07/10] Really fix tests on windows Path names with backslashes was not quoted. --- spec/main-process/atom-application.test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index a5e28b238..c504d2503 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -58,21 +58,18 @@ describe('AtomApplication', function () { await focusWindow(window) const addProjectPathFn = function (dir) { - return 'function (sendBackToMainProcess) { atom.project.addPath("' + dir + '"); sendBackToMainProcess(null); }' + return 'function (sendBackToMainProcess) { atom.project.addPath(' + JSON.stringify(dir) + '); sendBackToMainProcess(null); }' } const removeProjectPathFn = function (dir) { - return 'function (sendBackToMainProcess) { atom.project.removePath("' + dir + '"); sendBackToMainProcess(null); }' + return 'function (sendBackToMainProcess) { atom.project.removePath(' + JSON.stringify(dir) + '); sendBackToMainProcess(null); }' } atomApplication.saveState = mockSaveState await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) - await conditionPromise(() => cnt === 1) assert.equal(cnt, 1) await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) - await conditionPromise(() => cnt === 2) assert.equal(cnt, 2) await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) - await conditionPromise(() => cnt === 3) assert.equal(cnt, 3) }) }) From 4d5312fec5cfd6ab6c32d81178f5a5f2bf74cade Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 7 Jan 2017 23:52:29 +0300 Subject: [PATCH 08/10] Restore test behaviour to check content of storage/application.json --- spec/main-process/atom-application.test.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index c504d2503..4683375dc 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -47,13 +47,6 @@ describe('AtomApplication', function () { const dirB = makeTempDir() const atomApplication = buildAtomApplication() - let cnt = 0 - const origSaveState = atomApplication.saveState - const mockSaveState = function () { - cnt++ - origSaveState.apply(atomApplication, arguments) - } - const window = atomApplication.launch(parseCommandLine([])) await focusWindow(window) @@ -64,13 +57,20 @@ describe('AtomApplication', function () { return 'function (sendBackToMainProcess) { atom.project.removePath(' + JSON.stringify(dir) + '); sendBackToMainProcess(null); }' } - atomApplication.saveState = mockSaveState await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) - assert.equal(cnt, 1) + + const appState1 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState1[0].initialPaths, [dirA]) + await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) - assert.equal(cnt, 2) + + const appState2 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState2[0].initialPaths, [dirA, dirB]) + await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) - assert.equal(cnt, 3) + + const appState3 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + assert.deepEqual(appState3[0].initialPaths, [dirB]) }) }) From be57e4cd48b8c27e7e808bfd958f4bb10f9804a0 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 7 Jan 2017 23:58:28 +0300 Subject: [PATCH 09/10] Additional check that storage/application.json exists --- spec/main-process/atom-application.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index 4683375dc..147ff0fc2 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -59,6 +59,8 @@ describe('AtomApplication', function () { await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) + assert( fs.existsSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json')), 'ATOM_HOME/storage/application.json not exists' ) + const appState1 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) assert.deepEqual(appState1[0].initialPaths, [dirA]) From a7dda0e6f61f5ee1ca9754d060f79a20577afd86 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sun, 8 Jan 2017 00:03:36 +0300 Subject: [PATCH 10/10] Refactor test code --- spec/main-process/atom-application.test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/main-process/atom-application.test.js b/spec/main-process/atom-application.test.js index 147ff0fc2..ff02418ea 100644 --- a/spec/main-process/atom-application.test.js +++ b/spec/main-process/atom-application.test.js @@ -45,6 +45,7 @@ describe('AtomApplication', function () { it('sync application state on changes', async function () { const dirA = makeTempDir() const dirB = makeTempDir() + const storageFilePath = path.join(process.env.ATOM_HOME, 'storage', 'application.json') const atomApplication = buildAtomApplication() const window = atomApplication.launch(parseCommandLine([])) @@ -59,19 +60,19 @@ describe('AtomApplication', function () { await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA)) - assert( fs.existsSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json')), 'ATOM_HOME/storage/application.json not exists' ) + assert( fs.existsSync(storageFilePath), 'ATOM_HOME/storage/application.json not exists' ) - const appState1 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + const appState1 = JSON.parse(fs.readFileSync(storageFilePath, 'utf8')) assert.deepEqual(appState1[0].initialPaths, [dirA]) await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB)) - const appState2 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + const appState2 = JSON.parse(fs.readFileSync(storageFilePath, 'utf8')) assert.deepEqual(appState2[0].initialPaths, [dirA, dirB]) await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA)) - const appState3 = JSON.parse(fs.readFileSync(path.join(process.env.ATOM_HOME, 'storage', 'application.json'), 'utf8')) + const appState3 = JSON.parse(fs.readFileSync(storageFilePath, 'utf8')) assert.deepEqual(appState3[0].initialPaths, [dirB]) }) })