webview-spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. import * as path from 'path';
  2. import * as url from 'url';
  3. import { BrowserWindow, session, ipcMain, app, WebContents } from 'electron/main';
  4. import { closeAllWindows } from './window-helpers';
  5. import { emittedOnce, emittedUntil } from './events-helpers';
  6. import { expect } from 'chai';
  7. async function loadWebView (w: WebContents, attributes: Record<string, string>, openDevTools: boolean = false): Promise<void> {
  8. await w.executeJavaScript(`
  9. new Promise((resolve, reject) => {
  10. const webview = new WebView()
  11. for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
  12. webview.setAttribute(k, v)
  13. }
  14. document.body.appendChild(webview)
  15. webview.addEventListener('dom-ready', () => {
  16. if (${openDevTools}) {
  17. webview.openDevTools()
  18. }
  19. })
  20. webview.addEventListener('did-finish-load', () => {
  21. resolve()
  22. })
  23. })
  24. `);
  25. }
  26. describe('<webview> tag', function () {
  27. const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
  28. afterEach(closeAllWindows);
  29. function hideChildWindows (e: any, wc: WebContents) {
  30. wc.on('new-window', (event, url, frameName, disposition, options) => {
  31. options.show = false;
  32. });
  33. }
  34. before(() => {
  35. app.on('web-contents-created', hideChildWindows);
  36. });
  37. after(() => {
  38. app.off('web-contents-created', hideChildWindows);
  39. });
  40. it('works without script tag in page', async () => {
  41. const w = new BrowserWindow({
  42. show: false,
  43. webPreferences: {
  44. webviewTag: true,
  45. nodeIntegration: true,
  46. contextIsolation: false
  47. }
  48. });
  49. w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
  50. await emittedOnce(ipcMain, 'pong');
  51. });
  52. it('works with sandbox', async () => {
  53. const w = new BrowserWindow({
  54. show: false,
  55. webPreferences: {
  56. webviewTag: true,
  57. sandbox: true
  58. }
  59. });
  60. w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
  61. await emittedOnce(ipcMain, 'pong');
  62. });
  63. it('works with contextIsolation', async () => {
  64. const w = new BrowserWindow({
  65. show: false,
  66. webPreferences: {
  67. webviewTag: true,
  68. contextIsolation: true
  69. }
  70. });
  71. w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
  72. await emittedOnce(ipcMain, 'pong');
  73. });
  74. it('works with contextIsolation + sandbox', async () => {
  75. const w = new BrowserWindow({
  76. show: false,
  77. webPreferences: {
  78. webviewTag: true,
  79. contextIsolation: true,
  80. sandbox: true
  81. }
  82. });
  83. w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
  84. await emittedOnce(ipcMain, 'pong');
  85. });
  86. it('works with Trusted Types', async () => {
  87. const w = new BrowserWindow({
  88. show: false,
  89. webPreferences: {
  90. webviewTag: true
  91. }
  92. });
  93. w.loadFile(path.join(fixtures, 'pages', 'webview-trusted-types.html'));
  94. await emittedOnce(ipcMain, 'pong');
  95. });
  96. it('is disabled by default', async () => {
  97. const w = new BrowserWindow({
  98. show: false,
  99. webPreferences: {
  100. preload: path.join(fixtures, 'module', 'preload-webview.js'),
  101. nodeIntegration: true
  102. }
  103. });
  104. const webview = emittedOnce(ipcMain, 'webview');
  105. w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
  106. const [, type] = await webview;
  107. expect(type).to.equal('undefined', 'WebView still exists');
  108. });
  109. // FIXME(deepak1556): Ch69 follow up.
  110. xdescribe('document.visibilityState/hidden', () => {
  111. afterEach(() => {
  112. ipcMain.removeAllListeners('pong');
  113. });
  114. it('updates when the window is shown after the ready-to-show event', async () => {
  115. const w = new BrowserWindow({ show: false });
  116. const readyToShowSignal = emittedOnce(w, 'ready-to-show');
  117. const pongSignal1 = emittedOnce(ipcMain, 'pong');
  118. w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
  119. await pongSignal1;
  120. const pongSignal2 = emittedOnce(ipcMain, 'pong');
  121. await readyToShowSignal;
  122. w.show();
  123. const [, visibilityState, hidden] = await pongSignal2;
  124. expect(visibilityState).to.equal('visible');
  125. expect(hidden).to.be.false();
  126. });
  127. it('inherits the parent window visibility state and receives visibilitychange events', async () => {
  128. const w = new BrowserWindow({ show: false });
  129. w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
  130. const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong');
  131. expect(visibilityState).to.equal('hidden');
  132. expect(hidden).to.be.true();
  133. // We have to start waiting for the event
  134. // before we ask the webContents to resize.
  135. const getResponse = emittedOnce(ipcMain, 'pong');
  136. w.webContents.emit('-window-visibility-change', 'visible');
  137. return getResponse.then(([, visibilityState, hidden]) => {
  138. expect(visibilityState).to.equal('visible');
  139. expect(hidden).to.be.false();
  140. });
  141. });
  142. });
  143. describe('did-attach-webview event', () => {
  144. it('is emitted when a webview has been attached', async () => {
  145. const w = new BrowserWindow({
  146. show: false,
  147. webPreferences: {
  148. webviewTag: true,
  149. nodeIntegration: true,
  150. contextIsolation: false
  151. }
  152. });
  153. const didAttachWebview = emittedOnce(w.webContents, 'did-attach-webview');
  154. const webviewDomReady = emittedOnce(ipcMain, 'webview-dom-ready');
  155. w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'));
  156. const [, webContents] = await didAttachWebview;
  157. const [, id] = await webviewDomReady;
  158. expect(webContents.id).to.equal(id);
  159. });
  160. });
  161. describe('did-change-theme-color event', () => {
  162. it('emits when theme color changes', async () => {
  163. const w = new BrowserWindow({
  164. webPreferences: {
  165. webviewTag: true
  166. }
  167. });
  168. await w.loadURL('about:blank');
  169. const src = url.format({
  170. pathname: `${fixtures.replace(/\\/g, '/')}/pages/theme-color.html`,
  171. protocol: 'file',
  172. slashes: true
  173. });
  174. const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
  175. const webview = new WebView()
  176. webview.setAttribute('src', '${src}')
  177. webview.addEventListener('did-change-theme-color', (e) => {
  178. resolve('ok')
  179. })
  180. document.body.appendChild(webview)
  181. })`);
  182. expect(message).to.equal('ok');
  183. });
  184. });
  185. it('loads devtools extensions registered on the parent window', async () => {
  186. const w = new BrowserWindow({
  187. show: false,
  188. webPreferences: {
  189. webviewTag: true,
  190. nodeIntegration: true,
  191. contextIsolation: false
  192. }
  193. });
  194. w.webContents.session.removeExtension('foo');
  195. const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo');
  196. await w.webContents.session.loadExtension(extensionPath);
  197. w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html'));
  198. loadWebView(w.webContents, {
  199. nodeintegration: 'on',
  200. webpreferences: 'contextIsolation=no',
  201. src: `file://${path.join(__dirname, 'fixtures', 'blank.html')}`
  202. }, true);
  203. let childWebContentsId = 0;
  204. app.once('web-contents-created', (e, webContents) => {
  205. childWebContentsId = webContents.id;
  206. webContents.on('devtools-opened', function () {
  207. const showPanelIntervalId = setInterval(function () {
  208. if (!webContents.isDestroyed() && webContents.devToolsWebContents) {
  209. webContents.devToolsWebContents.executeJavaScript('(' + function () {
  210. const { UI } = (window as any);
  211. const tabs = UI.inspectorView._tabbedPane._tabs;
  212. const lastPanelId: any = tabs[tabs.length - 1].id;
  213. UI.inspectorView.showPanel(lastPanelId);
  214. }.toString() + ')()');
  215. } else {
  216. clearInterval(showPanelIntervalId);
  217. }
  218. }, 100);
  219. });
  220. });
  221. const [, { runtimeId, tabId }] = await emittedOnce(ipcMain, 'answer');
  222. expect(runtimeId).to.match(/^[a-z]{32}$/);
  223. expect(tabId).to.equal(childWebContentsId);
  224. });
  225. describe('zoom behavior', () => {
  226. const zoomScheme = standardScheme;
  227. const webviewSession = session.fromPartition('webview-temp');
  228. before(() => {
  229. const protocol = webviewSession.protocol;
  230. protocol.registerStringProtocol(zoomScheme, (request, callback) => {
  231. callback('hello');
  232. });
  233. });
  234. after(() => {
  235. const protocol = webviewSession.protocol;
  236. protocol.unregisterProtocol(zoomScheme);
  237. });
  238. it('inherits the zoomFactor of the parent window', async () => {
  239. const w = new BrowserWindow({
  240. show: false,
  241. webPreferences: {
  242. webviewTag: true,
  243. nodeIntegration: true,
  244. zoomFactor: 1.2,
  245. contextIsolation: false
  246. }
  247. });
  248. const zoomEventPromise = emittedOnce(ipcMain, 'webview-parent-zoom-level');
  249. w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-factor.html'));
  250. const [, zoomFactor, zoomLevel] = await zoomEventPromise;
  251. expect(zoomFactor).to.equal(1.2);
  252. expect(zoomLevel).to.equal(1);
  253. });
  254. it('maintains zoom level on navigation', async () => {
  255. const w = new BrowserWindow({
  256. show: false,
  257. webPreferences: {
  258. webviewTag: true,
  259. nodeIntegration: true,
  260. zoomFactor: 1.2,
  261. contextIsolation: false
  262. }
  263. });
  264. const promise = new Promise<void>((resolve) => {
  265. ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, newHost, final) => {
  266. if (!newHost) {
  267. expect(zoomFactor).to.equal(1.44);
  268. expect(zoomLevel).to.equal(2.0);
  269. } else {
  270. expect(zoomFactor).to.equal(1.2);
  271. expect(zoomLevel).to.equal(1);
  272. }
  273. if (final) {
  274. resolve();
  275. }
  276. });
  277. });
  278. w.loadFile(path.join(fixtures, 'pages', 'webview-custom-zoom-level.html'));
  279. await promise;
  280. });
  281. it('maintains zoom level when navigating within same page', async () => {
  282. const w = new BrowserWindow({
  283. show: false,
  284. webPreferences: {
  285. webviewTag: true,
  286. nodeIntegration: true,
  287. zoomFactor: 1.2,
  288. contextIsolation: false
  289. }
  290. });
  291. const promise = new Promise<void>((resolve) => {
  292. ipcMain.on('webview-zoom-in-page', (event, zoomLevel, zoomFactor, final) => {
  293. expect(zoomFactor).to.equal(1.44);
  294. expect(zoomLevel).to.equal(2.0);
  295. if (final) {
  296. resolve();
  297. }
  298. });
  299. });
  300. w.loadFile(path.join(fixtures, 'pages', 'webview-in-page-navigate.html'));
  301. await promise;
  302. });
  303. it('inherits zoom level for the origin when available', async () => {
  304. const w = new BrowserWindow({
  305. show: false,
  306. webPreferences: {
  307. webviewTag: true,
  308. nodeIntegration: true,
  309. zoomFactor: 1.2,
  310. contextIsolation: false
  311. }
  312. });
  313. w.loadFile(path.join(fixtures, 'pages', 'webview-origin-zoom-level.html'));
  314. const [, zoomLevel] = await emittedOnce(ipcMain, 'webview-origin-zoom-level');
  315. expect(zoomLevel).to.equal(2.0);
  316. });
  317. it('does not crash when navigating with zoom level inherited from parent', async () => {
  318. const w = new BrowserWindow({
  319. show: false,
  320. webPreferences: {
  321. webviewTag: true,
  322. nodeIntegration: true,
  323. zoomFactor: 1.2,
  324. session: webviewSession,
  325. contextIsolation: false
  326. }
  327. });
  328. const attachPromise = emittedOnce(w.webContents, 'did-attach-webview');
  329. const readyPromise = emittedOnce(ipcMain, 'dom-ready');
  330. w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
  331. const [, webview] = await attachPromise;
  332. await readyPromise;
  333. expect(webview.getZoomFactor()).to.equal(1.2);
  334. await w.loadURL(`${zoomScheme}://host1`);
  335. });
  336. });
  337. describe('nativeWindowOpen option', () => {
  338. let w: BrowserWindow;
  339. beforeEach(async () => {
  340. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true, contextIsolation: false } });
  341. await w.loadURL('about:blank');
  342. });
  343. afterEach(closeAllWindows);
  344. it('opens window of about:blank with cross-scripting enabled', async () => {
  345. // Don't wait for loading to finish.
  346. loadWebView(w.webContents, {
  347. allowpopups: 'on',
  348. nodeintegration: 'on',
  349. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  350. src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`
  351. });
  352. const [, content] = await emittedOnce(ipcMain, 'answer');
  353. expect(content).to.equal('Hello');
  354. });
  355. it('opens window of same domain with cross-scripting enabled', async () => {
  356. // Don't wait for loading to finish.
  357. loadWebView(w.webContents, {
  358. allowpopups: 'on',
  359. nodeintegration: 'on',
  360. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  361. src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`
  362. });
  363. const [, content] = await emittedOnce(ipcMain, 'answer');
  364. expect(content).to.equal('Hello');
  365. });
  366. it('returns null from window.open when allowpopups is not set', async () => {
  367. // Don't wait for loading to finish.
  368. loadWebView(w.webContents, {
  369. nodeintegration: 'on',
  370. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  371. src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}`
  372. });
  373. const [, { windowOpenReturnedNull }] = await emittedOnce(ipcMain, 'answer');
  374. expect(windowOpenReturnedNull).to.be.true();
  375. });
  376. it('blocks accessing cross-origin frames', async () => {
  377. // Don't wait for loading to finish.
  378. loadWebView(w.webContents, {
  379. allowpopups: 'on',
  380. nodeintegration: 'on',
  381. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  382. src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`
  383. });
  384. const [, content] = await emittedOnce(ipcMain, 'answer');
  385. const expectedContent =
  386. 'Blocked a frame with origin "file://" from accessing a cross-origin frame.';
  387. expect(content).to.equal(expectedContent);
  388. });
  389. it('emits a new-window event', async () => {
  390. // Don't wait for loading to finish.
  391. const attributes = {
  392. allowpopups: 'on',
  393. nodeintegration: 'on',
  394. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  395. src: `file://${fixtures}/pages/window-open.html`
  396. };
  397. const { url, frameName } = await w.webContents.executeJavaScript(`
  398. new Promise((resolve, reject) => {
  399. const webview = document.createElement('webview')
  400. for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
  401. webview.setAttribute(k, v)
  402. }
  403. document.body.appendChild(webview)
  404. webview.addEventListener('new-window', (e) => {
  405. resolve({url: e.url, frameName: e.frameName})
  406. })
  407. })
  408. `);
  409. expect(url).to.equal('http://host/');
  410. expect(frameName).to.equal('host');
  411. });
  412. it('emits a browser-window-created event', async () => {
  413. // Don't wait for loading to finish.
  414. loadWebView(w.webContents, {
  415. allowpopups: 'on',
  416. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  417. src: `file://${fixtures}/pages/window-open.html`
  418. });
  419. await emittedOnce(app, 'browser-window-created');
  420. });
  421. it('emits a web-contents-created event', async () => {
  422. const webContentsCreated = emittedUntil(app, 'web-contents-created',
  423. (event: Electron.Event, contents: Electron.WebContents) => contents.getType() === 'window');
  424. loadWebView(w.webContents, {
  425. allowpopups: 'on',
  426. webpreferences: 'nativeWindowOpen=1,contextIsolation=no',
  427. src: `file://${fixtures}/pages/window-open.html`
  428. });
  429. await webContentsCreated;
  430. });
  431. });
  432. describe('webpreferences attribute', () => {
  433. let w: BrowserWindow;
  434. beforeEach(async () => {
  435. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
  436. await w.loadURL('about:blank');
  437. });
  438. afterEach(closeAllWindows);
  439. it('can enable context isolation', async () => {
  440. loadWebView(w.webContents, {
  441. allowpopups: 'yes',
  442. preload: `file://${fixtures}/api/isolated-preload.js`,
  443. src: `file://${fixtures}/api/isolated.html`,
  444. webpreferences: 'contextIsolation=yes'
  445. });
  446. const [, data] = await emittedOnce(ipcMain, 'isolated-world');
  447. expect(data).to.deep.equal({
  448. preloadContext: {
  449. preloadProperty: 'number',
  450. pageProperty: 'undefined',
  451. typeofRequire: 'function',
  452. typeofProcess: 'object',
  453. typeofArrayPush: 'function',
  454. typeofFunctionApply: 'function',
  455. typeofPreloadExecuteJavaScriptProperty: 'undefined'
  456. },
  457. pageContext: {
  458. preloadProperty: 'undefined',
  459. pageProperty: 'string',
  460. typeofRequire: 'undefined',
  461. typeofProcess: 'undefined',
  462. typeofArrayPush: 'number',
  463. typeofFunctionApply: 'boolean',
  464. typeofPreloadExecuteJavaScriptProperty: 'number',
  465. typeofOpenedWindow: 'object'
  466. }
  467. });
  468. });
  469. });
  470. describe('permission request handlers', () => {
  471. let w: BrowserWindow;
  472. beforeEach(async () => {
  473. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true, contextIsolation: false } });
  474. await w.loadURL('about:blank');
  475. });
  476. afterEach(closeAllWindows);
  477. const partition = 'permissionTest';
  478. function setUpRequestHandler (webContentsId: number, requestedPermission: string) {
  479. return new Promise<void>((resolve, reject) => {
  480. session.fromPartition(partition).setPermissionRequestHandler(function (webContents, permission, callback) {
  481. if (webContents.id === webContentsId) {
  482. // requestMIDIAccess with sysex requests both midi and midiSysex so
  483. // grant the first midi one and then reject the midiSysex one
  484. if (requestedPermission === 'midiSysex' && permission === 'midi') {
  485. return callback(true);
  486. }
  487. try {
  488. expect(permission).to.equal(requestedPermission);
  489. } catch (e) {
  490. return reject(e);
  491. }
  492. callback(false);
  493. resolve();
  494. }
  495. });
  496. });
  497. }
  498. afterEach(() => {
  499. session.fromPartition(partition).setPermissionRequestHandler(null);
  500. });
  501. // This is disabled because CI machines don't have cameras or microphones,
  502. // so Chrome responds with "NotFoundError" instead of
  503. // "PermissionDeniedError". It should be re-enabled if we find a way to mock
  504. // the presence of a microphone & camera.
  505. xit('emits when using navigator.getUserMedia api', async () => {
  506. const errorFromRenderer = emittedOnce(ipcMain, 'message');
  507. loadWebView(w.webContents, {
  508. src: `file://${fixtures}/pages/permissions/media.html`,
  509. partition,
  510. nodeintegration: 'on'
  511. });
  512. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  513. setUpRequestHandler(webViewContents.id, 'media');
  514. const [, errorName] = await errorFromRenderer;
  515. expect(errorName).to.equal('PermissionDeniedError');
  516. });
  517. it('emits when using navigator.geolocation api', async () => {
  518. const errorFromRenderer = emittedOnce(ipcMain, 'message');
  519. loadWebView(w.webContents, {
  520. src: `file://${fixtures}/pages/permissions/geolocation.html`,
  521. partition,
  522. nodeintegration: 'on',
  523. webpreferences: 'contextIsolation=no'
  524. });
  525. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  526. setUpRequestHandler(webViewContents.id, 'geolocation');
  527. const [, error] = await errorFromRenderer;
  528. expect(error).to.equal('User denied Geolocation');
  529. });
  530. it('emits when using navigator.requestMIDIAccess without sysex api', async () => {
  531. const errorFromRenderer = emittedOnce(ipcMain, 'message');
  532. loadWebView(w.webContents, {
  533. src: `file://${fixtures}/pages/permissions/midi.html`,
  534. partition,
  535. nodeintegration: 'on',
  536. webpreferences: 'contextIsolation=no'
  537. });
  538. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  539. setUpRequestHandler(webViewContents.id, 'midi');
  540. const [, error] = await errorFromRenderer;
  541. expect(error).to.equal('SecurityError');
  542. });
  543. it('emits when using navigator.requestMIDIAccess with sysex api', async () => {
  544. const errorFromRenderer = emittedOnce(ipcMain, 'message');
  545. loadWebView(w.webContents, {
  546. src: `file://${fixtures}/pages/permissions/midi-sysex.html`,
  547. partition,
  548. nodeintegration: 'on',
  549. webpreferences: 'contextIsolation=no'
  550. });
  551. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  552. setUpRequestHandler(webViewContents.id, 'midiSysex');
  553. const [, error] = await errorFromRenderer;
  554. expect(error).to.equal('SecurityError');
  555. });
  556. it('emits when accessing external protocol', async () => {
  557. loadWebView(w.webContents, {
  558. src: 'magnet:test',
  559. partition
  560. });
  561. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  562. await setUpRequestHandler(webViewContents.id, 'openExternal');
  563. });
  564. it('emits when using Notification.requestPermission', async () => {
  565. const errorFromRenderer = emittedOnce(ipcMain, 'message');
  566. loadWebView(w.webContents, {
  567. src: `file://${fixtures}/pages/permissions/notification.html`,
  568. partition,
  569. nodeintegration: 'on',
  570. webpreferences: 'contextIsolation=no'
  571. });
  572. const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
  573. await setUpRequestHandler(webViewContents.id, 'notifications');
  574. const [, error] = await errorFromRenderer;
  575. expect(error).to.equal('denied');
  576. });
  577. });
  578. describe('DOM events', () => {
  579. afterEach(closeAllWindows);
  580. it('receives extra properties on DOM events when contextIsolation is enabled', async () => {
  581. const w = new BrowserWindow({
  582. show: false,
  583. webPreferences: {
  584. webviewTag: true,
  585. contextIsolation: true
  586. }
  587. });
  588. await w.loadURL('about:blank');
  589. const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
  590. const webview = new WebView()
  591. webview.setAttribute('src', 'data:text/html,<script>console.log("hi")</script>')
  592. webview.addEventListener('console-message', (e) => {
  593. resolve(e.message)
  594. })
  595. document.body.appendChild(webview)
  596. })`);
  597. expect(message).to.equal('hi');
  598. });
  599. it('emits focus event when contextIsolation is enabled', async () => {
  600. const w = new BrowserWindow({
  601. show: false,
  602. webPreferences: {
  603. webviewTag: true,
  604. contextIsolation: true
  605. }
  606. });
  607. await w.loadURL('about:blank');
  608. await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
  609. const webview = new WebView()
  610. webview.setAttribute('src', 'about:blank')
  611. webview.addEventListener('dom-ready', () => {
  612. webview.focus()
  613. })
  614. webview.addEventListener('focus', () => {
  615. resolve();
  616. })
  617. document.body.appendChild(webview)
  618. })`);
  619. });
  620. });
  621. });