test: move more web contents specs (#20099)
This commit is contained in:
		
					parent
					
						
							
								b7b0992768
							
						
					
				
			
			
				commit
				
					
						a9e695d05f
					
				
			
		
					 5 changed files with 504 additions and 572 deletions
				
			
		| 
						 | 
				
			
			@ -933,10 +933,14 @@ void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
 | 
			
		|||
  bool is_main_frame = !render_frame_host->GetParent();
 | 
			
		||||
  int frame_process_id = render_frame_host->GetProcess()->GetID();
 | 
			
		||||
  int frame_routing_id = render_frame_host->GetRoutingID();
 | 
			
		||||
  auto weak_this = GetWeakPtr();
 | 
			
		||||
  Emit("did-frame-finish-load", is_main_frame, frame_process_id,
 | 
			
		||||
       frame_routing_id);
 | 
			
		||||
 | 
			
		||||
  if (is_main_frame)
 | 
			
		||||
  // ⚠️WARNING!⚠️
 | 
			
		||||
  // Emit() triggers JS which can call destroy() on |this|. It's not safe to
 | 
			
		||||
  // assume that |this| points to valid memory at this point.
 | 
			
		||||
  if (is_main_frame && weak_this)
 | 
			
		||||
    Emit("did-finish-load");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1092,6 +1096,9 @@ void WebContents::DidFinishNavigation(
 | 
			
		|||
    frame_routing_id = frame_host->GetRoutingID();
 | 
			
		||||
  }
 | 
			
		||||
  if (!navigation_handle->IsErrorPage()) {
 | 
			
		||||
    // FIXME: All the Emit() calls below could potentially result in |this|
 | 
			
		||||
    // being destroyed (by JS listening for the event and calling
 | 
			
		||||
    // webContents.destroy()).
 | 
			
		||||
    auto url = navigation_handle->GetURL();
 | 
			
		||||
    bool is_same_document = navigation_handle->IsSameDocument();
 | 
			
		||||
    if (is_same_document) {
 | 
			
		||||
| 
						 | 
				
			
			@ -1345,6 +1352,9 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
 | 
			
		|||
    params.load_type = content::NavigationController::LOAD_TYPE_DATA;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Calling LoadURLWithParams() can trigger JS which destroys |this|.
 | 
			
		||||
  auto weak_this = GetWeakPtr();
 | 
			
		||||
 | 
			
		||||
  params.transition_type = ui::PAGE_TRANSITION_TYPED;
 | 
			
		||||
  params.should_clear_history_list = true;
 | 
			
		||||
  params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
 | 
			
		||||
| 
						 | 
				
			
			@ -1353,10 +1363,16 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
 | 
			
		|||
  web_contents()->GetController().DiscardNonCommittedEntries();
 | 
			
		||||
  web_contents()->GetController().LoadURLWithParams(params);
 | 
			
		||||
 | 
			
		||||
  // ⚠️WARNING!⚠️
 | 
			
		||||
  // LoadURLWithParams() triggers JS events which can call destroy() on |this|.
 | 
			
		||||
  // It's not safe to assume that |this| points to valid memory at this point.
 | 
			
		||||
  if (!weak_this)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  // Set the background color of RenderWidgetHostView.
 | 
			
		||||
  // We have to call it right after LoadURL because the RenderViewHost is only
 | 
			
		||||
  // created after loading a page.
 | 
			
		||||
  auto* const view = web_contents()->GetRenderWidgetHostView();
 | 
			
		||||
  auto* const view = weak_this->web_contents()->GetRenderWidgetHostView();
 | 
			
		||||
  if (view) {
 | 
			
		||||
    auto* web_preferences = WebContentsPreferences::From(web_contents());
 | 
			
		||||
    std::string color_name;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -107,15 +107,17 @@ class EventEmitter : public Wrappable<T> {
 | 
			
		|||
                     v8::Local<v8::Object> event,
 | 
			
		||||
                     Args&&... args) {
 | 
			
		||||
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
 | 
			
		||||
    v8::Locker locker(isolate());
 | 
			
		||||
    v8::HandleScope handle_scope(isolate());
 | 
			
		||||
    EmitEvent(isolate(), GetWrapper(), name, event,
 | 
			
		||||
              std::forward<Args>(args)...);
 | 
			
		||||
    auto context = isolate()->GetCurrentContext();
 | 
			
		||||
    // It's possible that |this| will be deleted by EmitEvent, so save anything
 | 
			
		||||
    // we need from |this| before calling EmitEvent.
 | 
			
		||||
    auto* isolate = this->isolate();
 | 
			
		||||
    v8::Locker locker(isolate);
 | 
			
		||||
    v8::HandleScope handle_scope(isolate);
 | 
			
		||||
    auto context = isolate->GetCurrentContext();
 | 
			
		||||
    EmitEvent(isolate, GetWrapper(), name, event, std::forward<Args>(args)...);
 | 
			
		||||
    v8::Local<v8::Value> defaultPrevented;
 | 
			
		||||
    if (event->Get(context, StringToV8(isolate(), "defaultPrevented"))
 | 
			
		||||
    if (event->Get(context, StringToV8(isolate, "defaultPrevented"))
 | 
			
		||||
            .ToLocal(&defaultPrevented)) {
 | 
			
		||||
      return defaultPrevented->BooleanValue(isolate());
 | 
			
		||||
      return defaultPrevented->BooleanValue(isolate);
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,8 +2,10 @@ import * as chai from 'chai'
 | 
			
		|||
import { AddressInfo } from 'net'
 | 
			
		||||
import * as chaiAsPromised from 'chai-as-promised'
 | 
			
		||||
import * as path from 'path'
 | 
			
		||||
import * as fs from 'fs'
 | 
			
		||||
import * as http from 'http'
 | 
			
		||||
import { BrowserWindow, ipcMain, webContents, session, clipboard } from 'electron'
 | 
			
		||||
import * as ChildProcess from 'child_process'
 | 
			
		||||
import { BrowserWindow, ipcMain, webContents, session, WebContents, app, clipboard } from 'electron'
 | 
			
		||||
import { emittedOnce } from './events-helpers'
 | 
			
		||||
import { closeAllWindows } from './window-helpers'
 | 
			
		||||
import { ifdescribe, ifit } from './spec-helpers'
 | 
			
		||||
| 
						 | 
				
			
			@ -13,6 +15,7 @@ const { expect } = chai
 | 
			
		|||
chai.use(chaiAsPromised)
 | 
			
		||||
 | 
			
		||||
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures')
 | 
			
		||||
const features = process.electronBinding('features')
 | 
			
		||||
 | 
			
		||||
describe('webContents module', () => {
 | 
			
		||||
  describe('getAllWebContents() API', () => {
 | 
			
		||||
| 
						 | 
				
			
			@ -950,6 +953,479 @@ describe('webContents module', () => {
 | 
			
		|||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('webrtc ip policy api', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('can set and get webrtc ip policies', () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      const policies = [
 | 
			
		||||
        'default',
 | 
			
		||||
        'default_public_interface_only',
 | 
			
		||||
        'default_public_and_private_interfaces',
 | 
			
		||||
        'disable_non_proxied_udp'
 | 
			
		||||
      ]
 | 
			
		||||
      policies.forEach((policy) => {
 | 
			
		||||
        w.webContents.setWebRTCIPHandlingPolicy(policy as any)
 | 
			
		||||
        expect(w.webContents.getWebRTCIPHandlingPolicy()).to.equal(policy)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('render view deleted events', () => {
 | 
			
		||||
    let server: http.Server
 | 
			
		||||
    let serverUrl: string
 | 
			
		||||
    let crossSiteUrl: string
 | 
			
		||||
 | 
			
		||||
    before((done) => {
 | 
			
		||||
      server = http.createServer((req, res) => {
 | 
			
		||||
        const respond = () => {
 | 
			
		||||
          if (req.url === '/redirect-cross-site') {
 | 
			
		||||
            res.setHeader('Location', `${crossSiteUrl}/redirected`)
 | 
			
		||||
            res.statusCode = 302
 | 
			
		||||
            res.end()
 | 
			
		||||
          } else if (req.url === '/redirected') {
 | 
			
		||||
            res.end('<html><script>window.localStorage</script></html>')
 | 
			
		||||
          } else {
 | 
			
		||||
            res.end()
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        setTimeout(respond, 0)
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
 | 
			
		||||
        crossSiteUrl = `http://localhost:${(server.address() as AddressInfo).port}`
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    after(() => {
 | 
			
		||||
      server.close()
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
 | 
			
		||||
    it('does not emit current-render-view-deleted when speculative RVHs are deleted', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      let currentRenderViewDeletedEmitted = false
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        expect(currentRenderViewDeletedEmitted).to.be.false('current-render-view-deleted was emitted')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      const renderViewDeletedHandler = () => {
 | 
			
		||||
        currentRenderViewDeletedEmitted = true
 | 
			
		||||
      }
 | 
			
		||||
      w.webContents.on('current-render-view-deleted' as any, renderViewDeletedHandler)
 | 
			
		||||
      w.webContents.on('did-finish-load', () => {
 | 
			
		||||
        w.webContents.removeListener('current-render-view-deleted' as any, renderViewDeletedHandler)
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${serverUrl}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('emits current-render-view-deleted if the current RVHs are deleted', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      let currentRenderViewDeletedEmitted = false
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        expect(currentRenderViewDeletedEmitted).to.be.true('current-render-view-deleted wasn\'t emitted')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('current-render-view-deleted' as any, () => {
 | 
			
		||||
        currentRenderViewDeletedEmitted = true
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('did-finish-load', () => {
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${serverUrl}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('emits render-view-deleted if any RVHs are deleted', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      let rvhDeletedCount = 0
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        const expectedRenderViewDeletedEventCount = 3 // 1 speculative upon redirection + 2 upon window close.
 | 
			
		||||
        expect(rvhDeletedCount).to.equal(expectedRenderViewDeletedEventCount, 'render-view-deleted wasn\'t emitted the expected nr. of times')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('render-view-deleted' as any, () => {
 | 
			
		||||
        rvhDeletedCount++
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('did-finish-load', () => {
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${serverUrl}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('setIgnoreMenuShortcuts(ignore)', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('does not throw', () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      expect(() => {
 | 
			
		||||
        w.webContents.setIgnoreMenuShortcuts(true)
 | 
			
		||||
        w.webContents.setIgnoreMenuShortcuts(false)
 | 
			
		||||
      }).to.not.throw()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('create()', () => {
 | 
			
		||||
    it('does not crash on exit', async () => {
 | 
			
		||||
      const appPath = path.join(fixturesPath, 'api', 'leak-exit-webcontents.js')
 | 
			
		||||
      const electronPath = process.execPath
 | 
			
		||||
      const appProcess = ChildProcess.spawn(electronPath, [appPath])
 | 
			
		||||
      const [code] = await emittedOnce(appProcess, 'close')
 | 
			
		||||
      expect(code).to.equal(0)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  // Destroying webContents in its event listener is going to crash when
 | 
			
		||||
  // Electron is built in Debug mode.
 | 
			
		||||
  describe('destroy()', () => {
 | 
			
		||||
    let server: http.Server
 | 
			
		||||
    let serverUrl: string
 | 
			
		||||
 | 
			
		||||
    before((done) => {
 | 
			
		||||
      server = http.createServer((request, response) => {
 | 
			
		||||
        switch (request.url) {
 | 
			
		||||
          case '/net-error':
 | 
			
		||||
            response.destroy()
 | 
			
		||||
            break
 | 
			
		||||
          case '/200':
 | 
			
		||||
            response.end()
 | 
			
		||||
            break
 | 
			
		||||
          default:
 | 
			
		||||
            done('unsupported endpoint')
 | 
			
		||||
        }
 | 
			
		||||
      }).listen(0, '127.0.0.1', () => {
 | 
			
		||||
        serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    after(() => {
 | 
			
		||||
      server.close()
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    const events = [
 | 
			
		||||
      { name: 'did-start-loading', url: '/200' },
 | 
			
		||||
      { name: 'dom-ready', url: '/200' },
 | 
			
		||||
      { name: 'did-stop-loading', url: '/200' },
 | 
			
		||||
      { name: 'did-finish-load', url: '/200' },
 | 
			
		||||
      // FIXME: Multiple Emit calls inside an observer assume that object
 | 
			
		||||
      // will be alive till end of the observer. Synchronous `destroy` api
 | 
			
		||||
      // violates this contract and crashes.
 | 
			
		||||
      { name: 'did-frame-finish-load', url: '/200' },
 | 
			
		||||
      { name: 'did-fail-load', url: '/net-error' }
 | 
			
		||||
    ]
 | 
			
		||||
    for (const e of events) {
 | 
			
		||||
      it(`should not crash when invoked synchronously inside ${e.name} handler`, async () => {
 | 
			
		||||
        const contents = (webContents as any).create() as WebContents
 | 
			
		||||
        const originalEmit = contents.emit.bind(contents)
 | 
			
		||||
        contents.emit = (...args) => { console.log(args); return originalEmit(...args) }
 | 
			
		||||
        contents.once(e.name as any, () => (contents as any).destroy())
 | 
			
		||||
        const destroyed = emittedOnce(contents, 'destroyed')
 | 
			
		||||
        contents.loadURL(serverUrl + e.url)
 | 
			
		||||
        await destroyed
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('did-change-theme-color event', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('is triggered with correct theme color', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: true })
 | 
			
		||||
      let count = 0
 | 
			
		||||
      w.webContents.on('did-change-theme-color', (e, color) => {
 | 
			
		||||
        if (count === 0) {
 | 
			
		||||
          count += 1
 | 
			
		||||
          expect(color).to.equal('#FFEEDD')
 | 
			
		||||
          w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'))
 | 
			
		||||
        } else if (count === 1) {
 | 
			
		||||
          expect(color).to.be.null()
 | 
			
		||||
          done()
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixturesPath, 'pages', 'theme-color.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('console-message event', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('is triggered with correct log message', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: true })
 | 
			
		||||
      w.webContents.on('console-message', (e, level, message) => {
 | 
			
		||||
        // Don't just assert as Chromium might emit other logs that we should ignore.
 | 
			
		||||
        if (message === 'a') {
 | 
			
		||||
          done()
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixturesPath, 'pages', 'a.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('ipc-message event', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('emits when the renderer process sends an asynchronous message', async () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true } })
 | 
			
		||||
      await w.webContents.loadURL('about:blank')
 | 
			
		||||
      w.webContents.executeJavaScript(`
 | 
			
		||||
        require('electron').ipcRenderer.send('message', 'Hello World!')
 | 
			
		||||
      `)
 | 
			
		||||
 | 
			
		||||
      const [, channel, message] = await emittedOnce(w.webContents, 'ipc-message')
 | 
			
		||||
      expect(channel).to.equal('message')
 | 
			
		||||
      expect(message).to.equal('Hello World!')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('ipc-message-sync event', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('emits when the renderer process sends a synchronous message', async () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true } })
 | 
			
		||||
      await w.webContents.loadURL('about:blank')
 | 
			
		||||
      const promise: Promise<[string, string]> = new Promise(resolve => {
 | 
			
		||||
        w.webContents.once('ipc-message-sync', (event, channel, arg) => {
 | 
			
		||||
          event.returnValue = 'foobar' as any
 | 
			
		||||
          resolve([channel, arg])
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
      const result = await w.webContents.executeJavaScript(`
 | 
			
		||||
        require('electron').ipcRenderer.sendSync('message', 'Hello World!')
 | 
			
		||||
      `)
 | 
			
		||||
 | 
			
		||||
      const [channel, message] = await promise
 | 
			
		||||
      expect(channel).to.equal('message')
 | 
			
		||||
      expect(message).to.equal('Hello World!')
 | 
			
		||||
      expect(result).to.equal('foobar')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('referrer', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('propagates referrer information to new target=_blank windows', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      const server = http.createServer((req, res) => {
 | 
			
		||||
        if (req.url === '/should_have_referrer') {
 | 
			
		||||
          expect(req.headers.referer).to.equal(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`)
 | 
			
		||||
          server.close()
 | 
			
		||||
          return done()
 | 
			
		||||
        }
 | 
			
		||||
        res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>')
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/'
 | 
			
		||||
        w.webContents.once('did-finish-load', () => {
 | 
			
		||||
          w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
 | 
			
		||||
            expect(referrer.url).to.equal(url)
 | 
			
		||||
            expect(referrer.policy).to.equal('no-referrer-when-downgrade')
 | 
			
		||||
          })
 | 
			
		||||
          w.webContents.executeJavaScript('a.click()')
 | 
			
		||||
        })
 | 
			
		||||
        w.loadURL(url)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // TODO(jeremy): window.open() in a real browser passes the referrer, but
 | 
			
		||||
    // our hacked-up window.open() shim doesn't. It should.
 | 
			
		||||
    xit('propagates referrer information to windows opened with window.open', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      const server = http.createServer((req, res) => {
 | 
			
		||||
        if (req.url === '/should_have_referrer') {
 | 
			
		||||
          expect(req.headers.referer).to.equal(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`)
 | 
			
		||||
          return done()
 | 
			
		||||
        }
 | 
			
		||||
        res.end('')
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/'
 | 
			
		||||
        w.webContents.once('did-finish-load', () => {
 | 
			
		||||
          w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
 | 
			
		||||
            expect(referrer.url).to.equal(url)
 | 
			
		||||
            expect(referrer.policy).to.equal('no-referrer-when-downgrade')
 | 
			
		||||
          })
 | 
			
		||||
          w.webContents.executeJavaScript('window.open(location.href + "should_have_referrer")')
 | 
			
		||||
        })
 | 
			
		||||
        w.loadURL(url)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('webframe messages in sandboxed contents', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('responds to executeJavaScript', async () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } })
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
      const result = await w.webContents.executeJavaScript('37 + 5')
 | 
			
		||||
      expect(result).to.equal(42)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('preload-error event', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    const generateSpecs = (description: string, sandbox: boolean) => {
 | 
			
		||||
      describe(description, () => {
 | 
			
		||||
        it('is triggered when unhandled exception is thrown', async () => {
 | 
			
		||||
          const preload = path.join(fixturesPath, 'module', 'preload-error-exception.js')
 | 
			
		||||
 | 
			
		||||
          const w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.equal('Hello World!')
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        it('is triggered on syntax errors', async () => {
 | 
			
		||||
          const preload = path.join(fixturesPath, 'module', 'preload-error-syntax.js')
 | 
			
		||||
 | 
			
		||||
          const w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.equal('foobar is not defined')
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        it('is triggered when preload script loading fails', async () => {
 | 
			
		||||
          const preload = path.join(fixturesPath, 'module', 'preload-invalid.js')
 | 
			
		||||
 | 
			
		||||
          const w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.contain('preload-invalid.js')
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    generateSpecs('without sandbox', false)
 | 
			
		||||
    generateSpecs('with sandbox', true)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('takeHeapSnapshot()', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
 | 
			
		||||
    it('works with sandboxed renderers', async () => {
 | 
			
		||||
      const w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
      const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot')
 | 
			
		||||
 | 
			
		||||
      const cleanup = () => {
 | 
			
		||||
        try {
 | 
			
		||||
          fs.unlinkSync(filePath)
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
          // ignore error
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      try {
 | 
			
		||||
        await w.webContents.takeHeapSnapshot(filePath)
 | 
			
		||||
        const stats = fs.statSync(filePath)
 | 
			
		||||
        expect(stats.size).not.to.be.equal(0)
 | 
			
		||||
      } finally {
 | 
			
		||||
        cleanup()
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('fails with invalid file path', async () => {
 | 
			
		||||
      const w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
      const promise = w.webContents.takeHeapSnapshot('')
 | 
			
		||||
      return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('setBackgroundThrottling()', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('does not crash when allowing', () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false })
 | 
			
		||||
      w.webContents.setBackgroundThrottling(true)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('does not crash when called via BrowserWindow', () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false });
 | 
			
		||||
 | 
			
		||||
      (w as any).setBackgroundThrottling(true)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('does not crash when disallowing', () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false, webPreferences: { backgroundThrottling: true } })
 | 
			
		||||
 | 
			
		||||
      w.webContents.setBackgroundThrottling(false)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  ifdescribe(features.isPrintingEnabled())('getPrinters()', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('can get printer list', async () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } })
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
      const printers = w.webContents.getPrinters()
 | 
			
		||||
      expect(printers).to.be.an('array')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  ifdescribe(features.isPrintingEnabled())('printToPDF()', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('can print to PDF', async () => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } })
 | 
			
		||||
      await w.loadURL('data:text/html,<h1>Hello, World!</h1>')
 | 
			
		||||
      const data = await w.webContents.printToPDF({})
 | 
			
		||||
      expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('PictureInPicture video', () => {
 | 
			
		||||
    afterEach(closeAllWindows)
 | 
			
		||||
    it('works as expected', (done) => {
 | 
			
		||||
      const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } })
 | 
			
		||||
      w.webContents.once('did-finish-load', async () => {
 | 
			
		||||
        const result = await w.webContents.executeJavaScript(
 | 
			
		||||
          `runTest(${features.isPictureInPictureEnabled()})`, true)
 | 
			
		||||
        expect(result).to.be.true()
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixturesPath, 'api', 'picture-in-picture.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('devtools window', () => {
 | 
			
		||||
    let hasRobotJS = false
 | 
			
		||||
    try {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,549 +0,0 @@
 | 
			
		|||
'use strict'
 | 
			
		||||
 | 
			
		||||
const ChildProcess = require('child_process')
 | 
			
		||||
const fs = require('fs')
 | 
			
		||||
const http = require('http')
 | 
			
		||||
const path = require('path')
 | 
			
		||||
const { closeWindow } = require('./window-helpers')
 | 
			
		||||
const { emittedOnce } = require('./events-helpers')
 | 
			
		||||
const chai = require('chai')
 | 
			
		||||
const dirtyChai = require('dirty-chai')
 | 
			
		||||
 | 
			
		||||
const features = process.electronBinding('features')
 | 
			
		||||
const { ipcRenderer, remote, clipboard } = require('electron')
 | 
			
		||||
const { BrowserWindow, webContents, ipcMain, session } = remote
 | 
			
		||||
const { expect } = chai
 | 
			
		||||
 | 
			
		||||
const isCi = remote.getGlobal('isCi')
 | 
			
		||||
 | 
			
		||||
chai.use(dirtyChai)
 | 
			
		||||
 | 
			
		||||
/* The whole webContents API doesn't use standard callbacks */
 | 
			
		||||
/* eslint-disable standard/no-callback-literal */
 | 
			
		||||
 | 
			
		||||
describe('webContents module', () => {
 | 
			
		||||
  const fixtures = path.resolve(__dirname, 'fixtures')
 | 
			
		||||
  let w
 | 
			
		||||
 | 
			
		||||
  beforeEach(() => {
 | 
			
		||||
    w = new BrowserWindow({
 | 
			
		||||
      show: false,
 | 
			
		||||
      width: 400,
 | 
			
		||||
      height: 400,
 | 
			
		||||
      webPreferences: {
 | 
			
		||||
        backgroundThrottling: false,
 | 
			
		||||
        nodeIntegration: true,
 | 
			
		||||
        webviewTag: true
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  afterEach(() => closeWindow(w).then(() => { w = null }))
 | 
			
		||||
 | 
			
		||||
  describe('webrtc ip policy api', () => {
 | 
			
		||||
    it('can set and get webrtc ip policies', () => {
 | 
			
		||||
      const policies = [
 | 
			
		||||
        'default',
 | 
			
		||||
        'default_public_interface_only',
 | 
			
		||||
        'default_public_and_private_interfaces',
 | 
			
		||||
        'disable_non_proxied_udp'
 | 
			
		||||
      ]
 | 
			
		||||
      policies.forEach((policy) => {
 | 
			
		||||
        w.webContents.setWebRTCIPHandlingPolicy(policy)
 | 
			
		||||
        expect(w.webContents.getWebRTCIPHandlingPolicy()).to.equal(policy)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('render view deleted events', () => {
 | 
			
		||||
    let server = null
 | 
			
		||||
 | 
			
		||||
    before((done) => {
 | 
			
		||||
      server = http.createServer((req, res) => {
 | 
			
		||||
        const respond = () => {
 | 
			
		||||
          if (req.url === '/redirect-cross-site') {
 | 
			
		||||
            res.setHeader('Location', `${server.cross_site_url}/redirected`)
 | 
			
		||||
            res.statusCode = 302
 | 
			
		||||
            res.end()
 | 
			
		||||
          } else if (req.url === '/redirected') {
 | 
			
		||||
            res.end('<html><script>window.localStorage</script></html>')
 | 
			
		||||
          } else {
 | 
			
		||||
            res.end()
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        setTimeout(respond, 0)
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        server.url = `http://127.0.0.1:${server.address().port}`
 | 
			
		||||
        server.cross_site_url = `http://localhost:${server.address().port}`
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    after(() => {
 | 
			
		||||
      server.close()
 | 
			
		||||
      server = null
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('does not emit current-render-view-deleted when speculative RVHs are deleted', (done) => {
 | 
			
		||||
      let currentRenderViewDeletedEmitted = false
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        expect(currentRenderViewDeletedEmitted).to.be.false('current-render-view-deleted was emitted')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      const renderViewDeletedHandler = () => {
 | 
			
		||||
        currentRenderViewDeletedEmitted = true
 | 
			
		||||
      }
 | 
			
		||||
      w.webContents.on('current-render-view-deleted', renderViewDeletedHandler)
 | 
			
		||||
      w.webContents.on('did-finish-load', (e) => {
 | 
			
		||||
        w.webContents.removeListener('current-render-view-deleted', renderViewDeletedHandler)
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${server.url}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('emits current-render-view-deleted if the current RVHs are deleted', (done) => {
 | 
			
		||||
      let currentRenderViewDeletedEmitted = false
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        expect(currentRenderViewDeletedEmitted).to.be.true('current-render-view-deleted wasn\'t emitted')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('current-render-view-deleted', () => {
 | 
			
		||||
        currentRenderViewDeletedEmitted = true
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('did-finish-load', (e) => {
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${server.url}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('emits render-view-deleted if any RVHs are deleted', (done) => {
 | 
			
		||||
      let rvhDeletedCount = 0
 | 
			
		||||
      w.webContents.once('destroyed', () => {
 | 
			
		||||
        const expectedRenderViewDeletedEventCount = 3 // 1 speculative upon redirection + 2 upon window close.
 | 
			
		||||
        expect(rvhDeletedCount).to.equal(expectedRenderViewDeletedEventCount, 'render-view-deleted wasn\'t emitted the expected nr. of times')
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('render-view-deleted', () => {
 | 
			
		||||
        rvhDeletedCount++
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.on('did-finish-load', (e) => {
 | 
			
		||||
        w.close()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadURL(`${server.url}/redirect-cross-site`)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('setIgnoreMenuShortcuts(ignore)', () => {
 | 
			
		||||
    it('does not throw', () => {
 | 
			
		||||
      expect(() => {
 | 
			
		||||
        w.webContents.setIgnoreMenuShortcuts(true)
 | 
			
		||||
        w.webContents.setIgnoreMenuShortcuts(false)
 | 
			
		||||
      }).to.not.throw()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('create()', () => {
 | 
			
		||||
    it('does not crash on exit', async () => {
 | 
			
		||||
      const appPath = path.join(__dirname, 'fixtures', 'api', 'leak-exit-webcontents.js')
 | 
			
		||||
      const electronPath = remote.getGlobal('process').execPath
 | 
			
		||||
      const appProcess = ChildProcess.spawn(electronPath, [appPath])
 | 
			
		||||
      const [code] = await emittedOnce(appProcess, 'close')
 | 
			
		||||
      expect(code).to.equal(0)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  // Destroying webContents in its event listener is going to crash when
 | 
			
		||||
  // Electron is built in Debug mode.
 | 
			
		||||
  xdescribe('destroy()', () => {
 | 
			
		||||
    let server
 | 
			
		||||
 | 
			
		||||
    before((done) => {
 | 
			
		||||
      server = http.createServer((request, response) => {
 | 
			
		||||
        switch (request.url) {
 | 
			
		||||
          case '/404':
 | 
			
		||||
            response.statusCode = '404'
 | 
			
		||||
            response.end()
 | 
			
		||||
            break
 | 
			
		||||
          case '/301':
 | 
			
		||||
            response.statusCode = '301'
 | 
			
		||||
            response.setHeader('Location', '/200')
 | 
			
		||||
            response.end()
 | 
			
		||||
            break
 | 
			
		||||
          case '/200':
 | 
			
		||||
            response.statusCode = '200'
 | 
			
		||||
            response.end('hello')
 | 
			
		||||
            break
 | 
			
		||||
          default:
 | 
			
		||||
            done('unsupported endpoint')
 | 
			
		||||
        }
 | 
			
		||||
      }).listen(0, '127.0.0.1', () => {
 | 
			
		||||
        server.url = 'http://127.0.0.1:' + server.address().port
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    after(() => {
 | 
			
		||||
      server.close()
 | 
			
		||||
      server = null
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('should not crash when invoked synchronously inside navigation observer', (done) => {
 | 
			
		||||
      const events = [
 | 
			
		||||
        { name: 'did-start-loading', url: `${server.url}/200` },
 | 
			
		||||
        { name: 'dom-ready', url: `${server.url}/200` },
 | 
			
		||||
        { name: 'did-stop-loading', url: `${server.url}/200` },
 | 
			
		||||
        { name: 'did-finish-load', url: `${server.url}/200` },
 | 
			
		||||
        // FIXME: Multiple Emit calls inside an observer assume that object
 | 
			
		||||
        // will be alive till end of the observer. Synchronous `destroy` api
 | 
			
		||||
        // violates this contract and crashes.
 | 
			
		||||
        // { name: 'did-frame-finish-load', url: `${server.url}/200` },
 | 
			
		||||
        { name: 'did-fail-load', url: `${server.url}/404` }
 | 
			
		||||
      ]
 | 
			
		||||
      const responseEvent = 'webcontents-destroyed'
 | 
			
		||||
 | 
			
		||||
      function * genNavigationEvent () {
 | 
			
		||||
        let eventOptions = null
 | 
			
		||||
        while ((eventOptions = events.shift()) && events.length) {
 | 
			
		||||
          eventOptions.responseEvent = responseEvent
 | 
			
		||||
          ipcRenderer.send('test-webcontents-navigation-observer', eventOptions)
 | 
			
		||||
          yield 1
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const gen = genNavigationEvent()
 | 
			
		||||
      ipcRenderer.on(responseEvent, () => {
 | 
			
		||||
        if (!gen.next().value) done()
 | 
			
		||||
      })
 | 
			
		||||
      gen.next()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('did-change-theme-color event', () => {
 | 
			
		||||
    it('is triggered with correct theme color', (done) => {
 | 
			
		||||
      let count = 0
 | 
			
		||||
      w.webContents.on('did-change-theme-color', (e, color) => {
 | 
			
		||||
        if (count === 0) {
 | 
			
		||||
          count += 1
 | 
			
		||||
          expect(color).to.equal('#FFEEDD')
 | 
			
		||||
          w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
 | 
			
		||||
        } else if (count === 1) {
 | 
			
		||||
          expect(color).to.be.null()
 | 
			
		||||
          done()
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixtures, 'pages', 'theme-color.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('console-message event', () => {
 | 
			
		||||
    it('is triggered with correct log message', (done) => {
 | 
			
		||||
      w.webContents.on('console-message', (e, level, message) => {
 | 
			
		||||
        // Don't just assert as Chromium might emit other logs that we should ignore.
 | 
			
		||||
        if (message === 'a') {
 | 
			
		||||
          done()
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixtures, 'pages', 'a.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('ipc-message event', () => {
 | 
			
		||||
    it('emits when the renderer process sends an asynchronous message', async () => {
 | 
			
		||||
      const webContents = remote.getCurrentWebContents()
 | 
			
		||||
      const promise = emittedOnce(webContents, 'ipc-message')
 | 
			
		||||
 | 
			
		||||
      ipcRenderer.send('message', 'Hello World!')
 | 
			
		||||
 | 
			
		||||
      const [, channel, message] = await promise
 | 
			
		||||
      expect(channel).to.equal('message')
 | 
			
		||||
      expect(message).to.equal('Hello World!')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('ipc-message-sync event', () => {
 | 
			
		||||
    it('emits when the renderer process sends a synchronous message', async () => {
 | 
			
		||||
      const webContents = remote.getCurrentWebContents()
 | 
			
		||||
      const promise = emittedOnce(webContents, 'ipc-message-sync')
 | 
			
		||||
 | 
			
		||||
      ipcRenderer.send('handle-next-ipc-message-sync', 'foobar')
 | 
			
		||||
      const result = ipcRenderer.sendSync('message', 'Hello World!')
 | 
			
		||||
 | 
			
		||||
      const [, channel, message] = await promise
 | 
			
		||||
      expect(channel).to.equal('message')
 | 
			
		||||
      expect(message).to.equal('Hello World!')
 | 
			
		||||
      expect(result).to.equal('foobar')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('referrer', () => {
 | 
			
		||||
    it('propagates referrer information to new target=_blank windows', (done) => {
 | 
			
		||||
      const server = http.createServer((req, res) => {
 | 
			
		||||
        if (req.url === '/should_have_referrer') {
 | 
			
		||||
          expect(req.headers.referer).to.equal(`http://127.0.0.1:${server.address().port}/`)
 | 
			
		||||
          return done()
 | 
			
		||||
        }
 | 
			
		||||
        res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>')
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        const url = 'http://127.0.0.1:' + server.address().port + '/'
 | 
			
		||||
        w.webContents.once('did-finish-load', () => {
 | 
			
		||||
          w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
 | 
			
		||||
            expect(referrer.url).to.equal(url)
 | 
			
		||||
            expect(referrer.policy).to.equal('no-referrer-when-downgrade')
 | 
			
		||||
          })
 | 
			
		||||
          w.webContents.executeJavaScript('a.click()')
 | 
			
		||||
        })
 | 
			
		||||
        w.loadURL(url)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // TODO(jeremy): window.open() in a real browser passes the referrer, but
 | 
			
		||||
    // our hacked-up window.open() shim doesn't. It should.
 | 
			
		||||
    xit('propagates referrer information to windows opened with window.open', (done) => {
 | 
			
		||||
      const server = http.createServer((req, res) => {
 | 
			
		||||
        if (req.url === '/should_have_referrer') {
 | 
			
		||||
          expect(req.headers.referer).to.equal(`http://127.0.0.1:${server.address().port}/`)
 | 
			
		||||
          return done()
 | 
			
		||||
        }
 | 
			
		||||
        res.end('')
 | 
			
		||||
      })
 | 
			
		||||
      server.listen(0, '127.0.0.1', () => {
 | 
			
		||||
        const url = 'http://127.0.0.1:' + server.address().port + '/'
 | 
			
		||||
        w.webContents.once('did-finish-load', () => {
 | 
			
		||||
          w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
 | 
			
		||||
            expect(referrer.url).to.equal(url)
 | 
			
		||||
            expect(referrer.policy).to.equal('no-referrer-when-downgrade')
 | 
			
		||||
          })
 | 
			
		||||
          w.webContents.executeJavaScript('window.open(location.href + "should_have_referrer")')
 | 
			
		||||
        })
 | 
			
		||||
        w.loadURL(url)
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('webframe messages in sandboxed contents', () => {
 | 
			
		||||
    it('responds to executeJavaScript', async () => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
      const result = await w.webContents.executeJavaScript('37 + 5')
 | 
			
		||||
      expect(result).to.equal(42)
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('preload-error event', () => {
 | 
			
		||||
    const generateSpecs = (description, sandbox) => {
 | 
			
		||||
      describe(description, () => {
 | 
			
		||||
        it('is triggered when unhandled exception is thrown', async () => {
 | 
			
		||||
          const preload = path.join(fixtures, 'module', 'preload-error-exception.js')
 | 
			
		||||
 | 
			
		||||
          w.destroy()
 | 
			
		||||
          w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.equal('Hello World!')
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        it('is triggered on syntax errors', async () => {
 | 
			
		||||
          const preload = path.join(fixtures, 'module', 'preload-error-syntax.js')
 | 
			
		||||
 | 
			
		||||
          w.destroy()
 | 
			
		||||
          w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.equal('foobar is not defined')
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        it('is triggered when preload script loading fails', async () => {
 | 
			
		||||
          const preload = path.join(fixtures, 'module', 'preload-invalid.js')
 | 
			
		||||
 | 
			
		||||
          w.destroy()
 | 
			
		||||
          w = new BrowserWindow({
 | 
			
		||||
            show: false,
 | 
			
		||||
            webPreferences: {
 | 
			
		||||
              sandbox,
 | 
			
		||||
              preload
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
 | 
			
		||||
          const promise = emittedOnce(w.webContents, 'preload-error')
 | 
			
		||||
          w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
          const [, preloadPath, error] = await promise
 | 
			
		||||
          expect(preloadPath).to.equal(preload)
 | 
			
		||||
          expect(error.message).to.contain('preload-invalid.js')
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    generateSpecs('without sandbox', false)
 | 
			
		||||
    generateSpecs('with sandbox', true)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('takeHeapSnapshot()', () => {
 | 
			
		||||
    it('works with sandboxed renderers', async () => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
      const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot')
 | 
			
		||||
 | 
			
		||||
      const cleanup = () => {
 | 
			
		||||
        try {
 | 
			
		||||
          fs.unlinkSync(filePath)
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
          // ignore error
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      try {
 | 
			
		||||
        await w.webContents.takeHeapSnapshot(filePath)
 | 
			
		||||
        const stats = fs.statSync(filePath)
 | 
			
		||||
        expect(stats.size).not.to.be.equal(0)
 | 
			
		||||
      } finally {
 | 
			
		||||
        cleanup()
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('fails with invalid file path', async () => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      await w.loadURL('about:blank')
 | 
			
		||||
 | 
			
		||||
      const promise = w.webContents.takeHeapSnapshot('')
 | 
			
		||||
      return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('setBackgroundThrottling()', () => {
 | 
			
		||||
    it('does not crash when allowing', (done) => {
 | 
			
		||||
      w.webContents.setBackgroundThrottling(true)
 | 
			
		||||
      done()
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('does not crash when disallowing', (done) => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        width: 400,
 | 
			
		||||
        height: 400,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          backgroundThrottling: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      w.webContents.setBackgroundThrottling(false)
 | 
			
		||||
      done()
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('does not crash when called via BrowserWindow', (done) => {
 | 
			
		||||
      w.setBackgroundThrottling(true)
 | 
			
		||||
      done()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('getPrinterList()', () => {
 | 
			
		||||
    before(function () {
 | 
			
		||||
      if (!features.isPrintingEnabled()) {
 | 
			
		||||
        return closeWindow(w).then(() => {
 | 
			
		||||
          w = null
 | 
			
		||||
          this.skip()
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('can get printer list', async () => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
 | 
			
		||||
      const printers = w.webContents.getPrinters()
 | 
			
		||||
      expect(printers).to.be.an('array')
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('printToPDF()', () => {
 | 
			
		||||
    before(function () {
 | 
			
		||||
      if (!features.isPrintingEnabled()) {
 | 
			
		||||
        return closeWindow(w).then(() => {
 | 
			
		||||
          w = null
 | 
			
		||||
          this.skip()
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it('can print to PDF', async () => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
 | 
			
		||||
      const data = await w.webContents.printToPDF({})
 | 
			
		||||
      expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('PictureInPicture video', () => {
 | 
			
		||||
    it('works as expected', (done) => {
 | 
			
		||||
      w.destroy()
 | 
			
		||||
      w = new BrowserWindow({
 | 
			
		||||
        show: false,
 | 
			
		||||
        webPreferences: {
 | 
			
		||||
          sandbox: true
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      w.webContents.once('did-finish-load', async () => {
 | 
			
		||||
        const result = await w.webContents.executeJavaScript(
 | 
			
		||||
          `runTest(${features.isPictureInPictureEnabled()})`, true)
 | 
			
		||||
        expect(result).to.be.true()
 | 
			
		||||
        done()
 | 
			
		||||
      })
 | 
			
		||||
      w.loadFile(path.join(fixtures, 'api', 'picture-in-picture.html'))
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			@ -152,19 +152,6 @@ app.on('ready', function () {
 | 
			
		|||
    console.error('Renderer process crashed')
 | 
			
		||||
    process.exit(1)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  ipcMain.on('prevent-next-input-event', (event, key, id) => {
 | 
			
		||||
    webContents.fromId(id).once('before-input-event', (event, input) => {
 | 
			
		||||
      if (key === input.key) event.preventDefault()
 | 
			
		||||
    })
 | 
			
		||||
    event.returnValue = null
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
ipcMain.on('handle-next-ipc-message-sync', function (event, returnValue) {
 | 
			
		||||
  event.sender.once('ipc-message-sync', (event, channel, args) => {
 | 
			
		||||
    event.returnValue = returnValue
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
for (const eventName of [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue