Browse Source

spec: Add test case for app.makeSingleInstance

Cheng Zhao 7 years ago
parent
commit
b4075bed87

+ 22 - 0
spec/api-app-spec.js

@@ -157,6 +157,28 @@ describe('app module', function () {
     })
   })
 
+  describe('app.makeSingleInstance', function () {
+    it('prevents the second launch of app', function (done) {
+      this.timeout(120000)
+      const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
+      // First launch should exit with 0.
+      let secondLaunched = false
+      const first = ChildProcess.spawn(remote.process.execPath, [appPath])
+      first.once('exit', (code) => {
+        assert.ok(secondLaunched)
+        assert.equal(code, 0)
+        done()
+      })
+      // Second launch should exit with 1.
+      const second = ChildProcess.spawn(remote.process.execPath, [appPath])
+      second.once('exit', (code) => {
+        assert.ok(!secondLaunched)
+        assert.equal(code, 1)
+        secondLaunched = true
+      })
+    })
+  })
+
   describe('app.relaunch', function () {
     let server = null
     const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-relaunch' : '/tmp/electron-app-relaunch'

+ 13 - 0
spec/fixtures/api/singleton/main.js

@@ -0,0 +1,13 @@
+const {app} = require('electron')
+
+process.on('uncaughtException', () => {
+  app.exit(2)
+})
+
+const shouldExit = app.makeSingleInstance(() => {
+  process.nextTick(() => app.exit(0))
+})
+
+if (shouldExit) {
+  app.exit(1)
+}

+ 5 - 0
spec/fixtures/api/singleton/package.json

@@ -0,0 +1,5 @@
+{
+  "name": "electron-app-singleton",
+  "main": "main.js"
+}
+