Add initial isolated world spec
authorKevin Sawicki <kevinsawicki@gmail.com>
Tue, 13 Dec 2016 19:47:54 +0000 (11:47 -0800)
committerKevin Sawicki <kevinsawicki@gmail.com>
Mon, 16 Jan 2017 20:38:16 +0000 (12:38 -0800)
spec/api-browser-window-spec.js
spec/fixtures/api/isolated-preload.js [new file with mode: 0644]
spec/fixtures/api/isolated.html [new file with mode: 0644]

index ab5d6d2..2dd7fc8 100644 (file)
@@ -1835,6 +1835,39 @@ describe('BrowserWindow module', function () {
     })
   })
 
+  describe('isolated option', () => {
+    it('separates the page context from the Electron/preload context', (done) => {
+      if (w != null) w.destroy()
+
+      ipcMain.once('isolated-world', (event, data) => {
+        assert.deepEqual(data, {
+          preloadContext: {
+            preloadProperty: 'number',
+            pageProperty: 'undefined',
+            typeofRequire: 'function',
+            typeofProcess: 'object'
+          },
+          pageContext: {
+            preloadProperty: 'undefined',
+            pageProperty: 'string',
+            typeofRequire: 'undefined',
+            typeofProcess: 'undefined'
+          }
+        })
+        done()
+      })
+
+      w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          isolated: true,
+          preload: path.join(fixtures, 'api', 'isolated-preload.js')
+        }
+      })
+      w.loadURL('file://' + fixtures + '/api/isolated.html')
+    })
+  })
+
   describe('offscreen rendering', function () {
     beforeEach(function () {
       if (w != null) w.destroy()
diff --git a/spec/fixtures/api/isolated-preload.js b/spec/fixtures/api/isolated-preload.js
new file mode 100644 (file)
index 0000000..8cb80d3
--- /dev/null
@@ -0,0 +1,15 @@
+const {ipcRenderer} = require('electron')
+
+window.foo = 3
+
+window.addEventListener('message', (event) => {
+  ipcRenderer.send('isolated-world', {
+    preloadContext: {
+      preloadProperty: typeof window.foo,
+      pageProperty: typeof window.hello,
+      typeofRequire: typeof require,
+      typeofProcess: typeof process
+    },
+    pageContext: event.data
+  })
+})
diff --git a/spec/fixtures/api/isolated.html b/spec/fixtures/api/isolated.html
new file mode 100644 (file)
index 0000000..bb0aca9
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Isolated World</title>
+    <script>
+      window.hello = 'world'
+      window.postMessage({
+        preloadProperty: typeof window.foo,
+        pageProperty: typeof window.hello,
+        typeofRequire: typeof require,
+        typeofProcess: typeof process
+      }, '*')
+    </script>
+  </head>
+  <body>
+
+  </body>
+</html>