api-web-contents-view-spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { expect } from 'chai';
  2. import * as ChildProcess from 'child_process';
  3. import * as path from 'path';
  4. import { emittedOnce } from './events-helpers';
  5. import { closeWindow } from './window-helpers';
  6. import { TopLevelWindow, WebContentsView } from 'electron/main';
  7. describe('WebContentsView', () => {
  8. let w: TopLevelWindow;
  9. afterEach(() => closeWindow(w as any).then(() => { w = null as unknown as TopLevelWindow; }));
  10. it('can be used as content view', () => {
  11. w = new TopLevelWindow({ show: false });
  12. w.setContentView(new WebContentsView({}));
  13. });
  14. describe('new WebContentsView()', () => {
  15. it('does not crash on exit', async () => {
  16. const appPath = path.join(__dirname, 'fixtures', 'api', 'leak-exit-webcontentsview.js');
  17. const electronPath = process.execPath;
  18. const appProcess = ChildProcess.spawn(electronPath, ['--enable-logging', appPath]);
  19. let output = '';
  20. appProcess.stdout.on('data', data => { output += data; });
  21. appProcess.stderr.on('data', data => { output += data; });
  22. const [code] = await emittedOnce(appProcess, 'exit');
  23. if (code !== 0) {
  24. console.log(code, output);
  25. }
  26. expect(code).to.equal(0);
  27. });
  28. });
  29. function triggerGCByAllocation () {
  30. const arr = [];
  31. for (let i = 0; i < 1000000; i++) {
  32. arr.push([]);
  33. }
  34. return arr;
  35. }
  36. it('doesn\'t crash when GCed during allocation', (done) => {
  37. // eslint-disable-next-line no-new
  38. new WebContentsView({});
  39. setTimeout(() => {
  40. // NB. the crash we're testing for is the lack of a current `v8::Context`
  41. // when emitting an event in WebContents's destructor. V8 is inconsistent
  42. // about whether or not there's a current context during garbage
  43. // collection, and it seems that `v8Util.requestGarbageCollectionForTesting`
  44. // causes a GC in which there _is_ a current context, so the crash isn't
  45. // triggered. Thus, we force a GC by other means: namely, by allocating a
  46. // bunch of stuff.
  47. triggerGCByAllocation();
  48. done();
  49. });
  50. });
  51. });