electron/spec-main/api-desktop-capturer-spec.ts

264 lines
10 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import { screen, BrowserWindow, SourcesOptions } from 'electron/main';
import { desktopCapturer } from 'electron/common';
2020-03-20 20:28:31 +00:00
import { emittedOnce } from './events-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import { closeAllWindows } from './window-helpers';
const features = process._linkedBinding('electron_common_features');
ifdescribe(!process.arch.includes('arm') && process.platform !== 'win32')('desktopCapturer', () => {
if (!features.isDesktopCapturerEnabled()) {
// This condition can't go the `ifdescribe` call because its inner code
// it still executed, and if the feature is disabled some function calls here fail.
return;
}
2020-03-20 20:28:31 +00:00
let w: BrowserWindow;
before(async () => {
2020-03-20 20:28:31 +00:00
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
await w.loadURL('about:blank');
});
2020-03-20 20:28:31 +00:00
after(closeAllWindows);
const getSources: typeof desktopCapturer.getSources = (options: SourcesOptions) => {
return w.webContents.executeJavaScript(`
refactor: use v8 serialization for ipc (#20214) * refactor: use v8 serialization for ipc * cloning process.env doesn't work * serialize host objects by enumerating key/values * new serialization can handle NaN, Infinity, and undefined correctly * can't allocate v8 objects during GC * backport microtasks fix * fix compile * fix node_stream_loader reentrancy * update subframe spec to expect undefined instead of null * write undefined instead of crashing when serializing host objects * fix webview spec * fix download spec * buffers are transformed into uint8arrays * can't serialize promises * fix chrome.i18n.getMessage * fix devtools tests * fix zoom test * fix debug build * fix lint * update ipcRenderer tests * fix printToPDF test * update patch * remove accidentally re-added remote-side spec * wip * don't attempt to serialize host objects * jump through different hoops to set options.webContents sometimes * whoops * fix lint * clean up error-handling logic * fix memory leak * fix lint * convert host objects using old base::Value serialization * fix lint more * fall back to base::Value-based serialization * remove commented-out code * add docs to breaking-changes.md * Update breaking-changes.md * update ipcRenderer and WebContents docs * lint * use named values for format tag * save a memcpy for ~30% speedup * get rid of calls to ShallowClone * extra debugging for paranoia * d'oh, use the correct named tags * apparently msstl doesn't like this DCHECK * funny story about that DCHECK * disable remote-related functions when enable_remote_module = false * nits * use EnableIf to disable remote methods in mojom * fix include * review comments
2019-10-09 17:59:08 +00:00
require('electron').desktopCapturer.getSources(${JSON.stringify(options)}).then(m => JSON.parse(JSON.stringify(m)))
2020-03-20 20:28:31 +00:00
`);
};
const generateSpecs = (description: string, getSources: typeof desktopCapturer.getSources) => {
describe(description, () => {
// TODO(nornagon): figure out why this test is failing on Linux and re-enable it.
ifit(process.platform !== 'linux')('should return a non-empty array of sources', async () => {
const sources = await getSources({ types: ['window', 'screen'] });
expect(sources).to.be.an('array').that.is.not.empty();
});
it('throws an error for invalid options', async () => {
const promise = getSources(['window', 'screen'] as any);
await expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options');
});
2016-08-09 22:31:24 +00:00
// TODO(nornagon): figure out why this test is failing on Linux and re-enable it.
ifit(process.platform !== 'linux')('does not throw an error when called more than once (regression)', async () => {
const sources1 = await getSources({ types: ['window', 'screen'] });
expect(sources1).to.be.an('array').that.is.not.empty();
const sources2 = await getSources({ types: ['window', 'screen'] });
expect(sources2).to.be.an('array').that.is.not.empty();
});
ifit(process.platform !== 'linux')('responds to subsequent calls of different options', async () => {
const promise1 = getSources({ types: ['window'] });
await expect(promise1).to.eventually.be.fulfilled();
2016-01-19 18:57:18 +00:00
const promise2 = getSources({ types: ['screen'] });
await expect(promise2).to.eventually.be.fulfilled();
});
// Linux doesn't return any window sources.
ifit(process.platform !== 'linux')('returns an empty display_id for window sources on Windows and Mac', async () => {
const w = new BrowserWindow({ width: 200, height: 200 });
await w.loadURL('about:blank');
const sources = await getSources({ types: ['window'] });
w.destroy();
expect(sources).to.be.an('array').that.is.not.empty();
for (const { display_id: displayId } of sources) {
expect(displayId).to.be.a('string').and.be.empty();
}
});
ifit(process.platform !== 'linux')('returns display_ids matching the Screen API on Windows and Mac', async () => {
const displays = screen.getAllDisplays();
const sources = await getSources({ types: ['screen'] });
expect(sources).to.be.an('array').of.length(displays.length);
for (let i = 0; i < sources.length; i++) {
expect(sources[i].display_id).to.equal(displays[i].id.toString());
}
});
});
};
generateSpecs('in renderer process', getSources);
generateSpecs('in main process', desktopCapturer.getSources);
ifit(process.platform !== 'linux')('returns an empty source list if blocked by the main process', async () => {
w.webContents.once('desktop-capturer-get-sources', (event) => {
2020-03-20 20:28:31 +00:00
event.preventDefault();
});
const sources = await getSources({ types: ['screen'] });
expect(sources).to.be.empty();
});
it('disabling thumbnail should return empty images', async () => {
2020-03-20 20:28:31 +00:00
const w2 = new BrowserWindow({ show: false, width: 200, height: 200 });
const wShown = emittedOnce(w2, 'show');
w2.show();
await wShown;
const isEmpties: boolean[] = await w.webContents.executeJavaScript(`
require('electron').desktopCapturer.getSources({
types: ['window', 'screen'],
thumbnailSize: { width: 0, height: 0 }
}).then((sources) => {
return sources.map(s => s.thumbnail.constructor.name === 'NativeImage' && s.thumbnail.isEmpty())
})
2020-03-20 20:28:31 +00:00
`);
2020-03-20 20:28:31 +00:00
w2.destroy();
expect(isEmpties).to.be.an('array').that.is.not.empty();
expect(isEmpties.every(e => e === true)).to.be.true();
});
it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
2020-03-20 20:28:31 +00:00
const w = new BrowserWindow({ show: false, width: 100, height: 100 });
const wShown = emittedOnce(w, 'show');
const wFocused = emittedOnce(w, 'focus');
w.show();
w.focus();
await wShown;
await wFocused;
const mediaSourceId = w.getMediaSourceId();
const sources = await getSources({
types: ['window'],
thumbnailSize: { width: 0, height: 0 }
2020-03-20 20:28:31 +00:00
});
w.destroy();
// TODO(julien.isorce): investigate why |sources| is empty on the linux
// bots while it is not on my workstation, as expected, with and without
// the --ci parameter.
if (process.platform === 'linux' && sources.length === 0) {
2020-03-20 20:28:31 +00:00
it.skip('desktopCapturer.getSources returned an empty source list');
return;
}
2020-03-20 20:28:31 +00:00
expect(sources).to.be.an('array').that.is.not.empty();
const foundSource = sources.find((source) => {
2020-03-20 20:28:31 +00:00
return source.id === mediaSourceId;
});
expect(mediaSourceId).to.equal(foundSource!.id);
});
describe('getMediaSourceIdForWebContents', () => {
const getMediaSourceIdForWebContents: typeof desktopCapturer.getMediaSourceIdForWebContents = (webContentsId: number) => {
return w.webContents.executeJavaScript(`
require('electron').desktopCapturer.getMediaSourceIdForWebContents(${JSON.stringify(webContentsId)}).then(r => JSON.parse(JSON.stringify(r)))
`);
};
it('should return a stream id for web contents', async () => {
const result = await getMediaSourceIdForWebContents(w.webContents.id);
expect(result).to.be.a('string').that.is.not.empty();
});
it('throws an error for invalid options', async () => {
const promise = getMediaSourceIdForWebContents('not-an-id' as unknown as number);
await expect(promise).to.be.eventually.rejectedWith(Error, 'TypeError: Error processing argument');
});
it('throws an error for invalid web contents id', async () => {
const promise = getMediaSourceIdForWebContents(-200);
await expect(promise).to.be.eventually.rejectedWith(Error, 'Failed to find WebContents');
});
});
// TODO(deepak1556): currently fails on all ci, enable it after upgrade.
it.skip('moveAbove should move the window at the requested place', async () => {
// DesktopCapturer.getSources() is guaranteed to return in the correct
// z-order from foreground to background.
2020-03-20 20:28:31 +00:00
const MAX_WIN = 4;
const mainWindow = w;
const wList = [mainWindow];
try {
for (let i = 0; i < MAX_WIN - 1; i++) {
2020-03-20 20:28:31 +00:00
const w = new BrowserWindow({ show: true, width: 100, height: 100 });
wList.push(w);
}
2020-03-20 20:28:31 +00:00
expect(wList.length).to.equal(MAX_WIN);
// Show and focus all the windows.
wList.forEach(async (w) => {
2020-03-20 20:28:31 +00:00
const wFocused = emittedOnce(w, 'focus');
w.focus();
await wFocused;
});
// At this point our windows should be showing from bottom to top.
// DesktopCapturer.getSources() returns sources sorted from foreground to
// background, i.e. top to bottom.
let sources = await getSources({
types: ['window'],
thumbnailSize: { width: 0, height: 0 }
2020-03-20 20:28:31 +00:00
});
// TODO(julien.isorce): investigate why |sources| is empty on the linux
// bots while it is not on my workstation, as expected, with and without
// the --ci parameter.
if (process.platform === 'linux' && sources.length === 0) {
wList.forEach((w) => {
if (w !== mainWindow) {
2020-03-20 20:28:31 +00:00
w.destroy();
}
2020-03-20 20:28:31 +00:00
});
it.skip('desktopCapturer.getSources returned an empty source list');
return;
}
2020-03-20 20:28:31 +00:00
expect(sources).to.be.an('array').that.is.not.empty();
expect(sources.length).to.gte(MAX_WIN);
// Only keep our windows, they must be in the MAX_WIN first windows.
2020-03-20 20:28:31 +00:00
sources.splice(MAX_WIN, sources.length - MAX_WIN);
expect(sources.length).to.equal(MAX_WIN);
expect(sources.length).to.equal(wList.length);
// Check that the sources and wList are sorted in the reverse order.
2020-03-20 20:28:31 +00:00
const wListReversed = wList.slice(0).reverse();
const canGoFurther = sources.every(
2020-03-20 20:28:31 +00:00
(source, index) => source.id === wListReversed[index].getMediaSourceId());
if (!canGoFurther) {
// Skip remaining checks because either focus or window placement are
// not reliable in the running test environment. So there is no point
// to go further to test moveAbove as requirements are not met.
2020-03-20 20:28:31 +00:00
return;
}
// Do the real work, i.e. move each window above the next one so that
// wList is sorted from foreground to background.
wList.forEach(async (w, index) => {
if (index < (wList.length - 1)) {
2020-03-20 20:28:31 +00:00
const wNext = wList[index + 1];
w.moveAbove(wNext.getMediaSourceId());
}
2020-03-20 20:28:31 +00:00
});
sources = await getSources({
types: ['window'],
thumbnailSize: { width: 0, height: 0 }
2020-03-20 20:28:31 +00:00
});
// Only keep our windows again.
2020-03-20 20:28:31 +00:00
sources.splice(MAX_WIN, sources.length - MAX_WIN);
expect(sources.length).to.equal(MAX_WIN);
expect(sources.length).to.equal(wList.length);
// Check that the sources and wList are sorted in the same order.
sources.forEach((source, index) => {
2020-03-20 20:28:31 +00:00
expect(source.id).to.equal(wList[index].getMediaSourceId());
});
} finally {
wList.forEach((w) => {
if (w !== mainWindow) {
2020-03-20 20:28:31 +00:00
w.destroy();
}
2020-03-20 20:28:31 +00:00
});
}
2020-03-20 20:28:31 +00:00
});
});