test: move desktopCapturer spec to main runner (#20057)

This commit is contained in:
Jeremy Apthorp 2019-09-02 23:59:54 -07:00 committed by Cheng Zhao
parent 614079654c
commit 6e88b6b445

View file

@ -1,53 +1,48 @@
const chai = require('chai') import { expect } from 'chai'
const dirtyChai = require('dirty-chai') import { desktopCapturer, ipcRenderer, screen, BrowserWindow, SourcesOptions } from 'electron'
const chaiAsPromised = require('chai-as-promised') import { emittedOnce } from './events-helpers'
const { desktopCapturer, ipcRenderer, remote } = require('electron') import { ifdescribe } from './spec-helpers';
const { screen } = remote import { closeAllWindows } from './window-helpers';
const features = process.electronBinding('features') const features = process.electronBinding('features')
const { emittedOnce } = require('./events-helpers')
const { expect } = chai ifdescribe(features.isDesktopCapturerEnabled() && !process.arch.includes('arm') && process.platform !== 'win32')('desktopCapturer', () => {
chai.use(dirtyChai) let w: BrowserWindow
chai.use(chaiAsPromised) before(async () => {
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
const isCI = remote.getGlobal('isCi') await w.loadURL('about:blank')
describe('desktopCapturer', () => {
before(function () {
if (!features.isDesktopCapturerEnabled() || process.arch.indexOf('arm') === 0) {
// It's been disabled during build time.
this.skip()
return
}
if (isCI && process.platform === 'win32') {
this.skip()
}
}) })
after(closeAllWindows)
const getSources: typeof desktopCapturer.getSources = (options: SourcesOptions) => {
return w.webContents.executeJavaScript(`
require('electron').desktopCapturer.getSources(${JSON.stringify(options)})
`)
}
it('should return a non-empty array of sources', async () => { it('should return a non-empty array of sources', async () => {
const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] }) const sources = await getSources({ types: ['window', 'screen'] })
expect(sources).to.be.an('array').that.is.not.empty() expect(sources).to.be.an('array').that.is.not.empty()
}) })
it('throws an error for invalid options', async () => { it('throws an error for invalid options', async () => {
const promise = desktopCapturer.getSources(['window', 'screen']) const promise = getSources(['window', 'screen'] as any)
expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options') expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options')
}) })
it('does not throw an error when called more than once (regression)', async () => { it('does not throw an error when called more than once (regression)', async () => {
const sources1 = await desktopCapturer.getSources({ types: ['window', 'screen'] }) const sources1 = await getSources({ types: ['window', 'screen'] })
expect(sources1).to.be.an('array').that.is.not.empty() expect(sources1).to.be.an('array').that.is.not.empty()
const sources2 = await desktopCapturer.getSources({ types: ['window', 'screen'] }) const sources2 = await getSources({ types: ['window', 'screen'] })
expect(sources2).to.be.an('array').that.is.not.empty() expect(sources2).to.be.an('array').that.is.not.empty()
}) })
it('responds to subsequent calls of different options', async () => { it('responds to subsequent calls of different options', async () => {
const promise1 = desktopCapturer.getSources({ types: ['window'] }) const promise1 = getSources({ types: ['window'] })
expect(promise1).to.not.eventually.be.rejected() expect(promise1).to.not.eventually.be.rejected()
const promise2 = desktopCapturer.getSources({ types: ['screen'] }) const promise2 = getSources({ types: ['screen'] })
expect(promise2).to.not.eventually.be.rejected() expect(promise2).to.not.eventually.be.rejected()
}) })
@ -55,10 +50,9 @@ describe('desktopCapturer', () => {
// Linux doesn't return any window sources. // Linux doesn't return any window sources.
if (process.platform !== 'win32' && process.platform !== 'darwin') return if (process.platform !== 'win32' && process.platform !== 'darwin') return
const { BrowserWindow } = remote
const w = new BrowserWindow({ width: 200, height: 200 }) const w = new BrowserWindow({ width: 200, height: 200 })
const sources = await desktopCapturer.getSources({ types: ['window'] }) const sources = await getSources({ types: ['window'] })
w.destroy() w.destroy()
expect(sources).to.be.an('array').that.is.not.empty() expect(sources).to.be.an('array').that.is.not.empty()
for (const { display_id: displayId } of sources) { for (const { display_id: displayId } of sources) {
@ -70,7 +64,7 @@ describe('desktopCapturer', () => {
if (process.platform !== 'win32' && process.platform !== 'darwin') return if (process.platform !== 'win32' && process.platform !== 'darwin') return
const displays = screen.getAllDisplays() const displays = screen.getAllDisplays()
const sources = await desktopCapturer.getSources({ types: ['screen'] }) const sources = await getSources({ types: ['screen'] })
expect(sources).to.be.an('array').of.length(displays.length) expect(sources).to.be.an('array').of.length(displays.length)
for (let i = 0; i < sources.length; i++) { for (let i = 0; i < sources.length; i++) {
@ -79,32 +73,32 @@ describe('desktopCapturer', () => {
it('returns empty sources when blocked', async () => { it('returns empty sources when blocked', async () => {
ipcRenderer.send('handle-next-desktop-capturer-get-sources') ipcRenderer.send('handle-next-desktop-capturer-get-sources')
const sources = await desktopCapturer.getSources({ types: ['screen'] }) const sources = await getSources({ types: ['screen'] })
expect(sources).to.be.empty() expect(sources).to.be.empty()
}) })
}) })
it('disabling thumbnail should return empty images', async () => { it('disabling thumbnail should return empty images', async () => {
const { BrowserWindow } = remote const w2 = new BrowserWindow({ show: false, width: 200, height: 200 })
const w = new BrowserWindow({ show: false, width: 200, height: 200 }) const wShown = emittedOnce(w2, 'show')
const wShown = emittedOnce(w, 'show') w2.show()
w.show()
await wShown await wShown
const sources = await desktopCapturer.getSources({ const isEmpties: boolean[] = await w.webContents.executeJavaScript(`
types: ['window', 'screen'], require('electron').desktopCapturer.getSources({
thumbnailSize: { width: 0, height: 0 } types: ['window', 'screen'],
}) thumbnailSize: { width: 0, height: 0 }
w.destroy() }).then((sources) => {
expect(sources).to.be.an('array').that.is.not.empty() return sources.map(s => s.thumbnail.constructor.name === 'NativeImage' && s.thumbnail.isEmpty())
for (const { thumbnail: thumbnailImage } of sources) { })
expect(thumbnailImage).to.be.a('NativeImage') `)
expect(thumbnailImage.isEmpty()).to.be.true()
} 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 () => { it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
const { BrowserWindow } = remote
const w = new BrowserWindow({ show: false, width: 100, height: 100 }) const w = new BrowserWindow({ show: false, width: 100, height: 100 })
const wShown = emittedOnce(w, 'show') const wShown = emittedOnce(w, 'show')
const wFocused = emittedOnce(w, 'focus') const wFocused = emittedOnce(w, 'focus')
@ -114,7 +108,7 @@ describe('desktopCapturer', () => {
await wFocused await wFocused
const mediaSourceId = w.getMediaSourceId() const mediaSourceId = w.getMediaSourceId()
const sources = await desktopCapturer.getSources({ const sources = await getSources({
types: ['window'], types: ['window'],
thumbnailSize: { width: 0, height: 0 } thumbnailSize: { width: 0, height: 0 }
}) })
@ -132,18 +126,16 @@ describe('desktopCapturer', () => {
const foundSource = sources.find((source) => { const foundSource = sources.find((source) => {
return source.id === mediaSourceId return source.id === mediaSourceId
}) })
expect(mediaSourceId).to.equal(foundSource.id) expect(mediaSourceId).to.equal(foundSource!.id)
}) })
it('moveAbove should move the window at the requested place', async () => { it('moveAbove should move the window at the requested place', async () => {
// DesktopCapturer.getSources() is guaranteed to return in the correct // DesktopCapturer.getSources() is guaranteed to return in the correct
// z-order from foreground to background. // z-order from foreground to background.
const MAX_WIN = 4 const MAX_WIN = 4
const { BrowserWindow } = remote const mainWindow = w
const mainWindow = remote.getCurrentWindow()
const wList = [mainWindow] const wList = [mainWindow]
try { try {
// Add MAX_WIN-1 more window so we have MAX_WIN in total.
for (let i = 0; i < MAX_WIN - 1; i++) { for (let i = 0; i < MAX_WIN - 1; i++) {
const w = new BrowserWindow({ show: true, width: 100, height: 100 }) const w = new BrowserWindow({ show: true, width: 100, height: 100 })
wList.push(w) wList.push(w)
@ -160,7 +152,7 @@ describe('desktopCapturer', () => {
// DesktopCapturer.getSources() returns sources sorted from foreground to // DesktopCapturer.getSources() returns sources sorted from foreground to
// background, i.e. top to bottom. // background, i.e. top to bottom.
let sources = await desktopCapturer.getSources({ let sources = await getSources({
types: ['window'], types: ['window'],
thumbnailSize: { width: 0, height: 0 } thumbnailSize: { width: 0, height: 0 }
}) })
@ -206,7 +198,7 @@ describe('desktopCapturer', () => {
} }
}) })
sources = await desktopCapturer.getSources({ sources = await getSources({
types: ['window'], types: ['window'],
thumbnailSize: { width: 0, height: 0 } thumbnailSize: { width: 0, height: 0 }
}) })