2019-09-03 06:59:54 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { desktopCapturer, ipcRenderer, screen, BrowserWindow, SourcesOptions } from 'electron'
|
|
|
|
import { emittedOnce } from './events-helpers'
|
|
|
|
import { ifdescribe } from './spec-helpers';
|
|
|
|
import { closeAllWindows } from './window-helpers';
|
2016-04-30 11:51:09 +00:00
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
const features = process.electronBinding('features')
|
2018-06-13 16:15:34 +00:00
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
ifdescribe(features.isDesktopCapturerEnabled() && !process.arch.includes('arm') && process.platform !== 'win32')('desktopCapturer', () => {
|
|
|
|
let w: BrowserWindow
|
|
|
|
before(async () => {
|
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
|
|
|
|
await w.loadURL('about:blank')
|
2017-11-15 21:05:46 +00:00
|
|
|
})
|
2019-09-03 06:59:54 +00:00
|
|
|
after(closeAllWindows)
|
|
|
|
|
|
|
|
const getSources: typeof desktopCapturer.getSources = (options: SourcesOptions) => {
|
|
|
|
return w.webContents.executeJavaScript(`
|
|
|
|
require('electron').desktopCapturer.getSources(${JSON.stringify(options)})
|
|
|
|
`)
|
|
|
|
}
|
2016-04-30 11:51:09 +00:00
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
it('should return a non-empty array of sources', async () => {
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources = await getSources({ types: ['window', 'screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(sources).to.be.an('array').that.is.not.empty()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws an error for invalid options', async () => {
|
2019-09-03 06:59:54 +00:00
|
|
|
const promise = getSources(['window', 'screen'] as any)
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options')
|
2016-08-09 22:31:24 +00:00
|
|
|
})
|
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
it('does not throw an error when called more than once (regression)', async () => {
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources1 = await getSources({ types: ['window', 'screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(sources1).to.be.an('array').that.is.not.empty()
|
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources2 = await getSources({ types: ['window', 'screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(sources2).to.be.an('array').that.is.not.empty()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('responds to subsequent calls of different options', async () => {
|
2019-09-03 06:59:54 +00:00
|
|
|
const promise1 = getSources({ types: ['window'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(promise1).to.not.eventually.be.rejected()
|
2016-01-19 18:57:18 +00:00
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
const promise2 = getSources({ types: ['screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(promise2).to.not.eventually.be.rejected()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-27 20:32:14 +00:00
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
it('returns an empty display_id for window sources on Windows and Mac', async () => {
|
2018-04-09 05:43:35 +00:00
|
|
|
// Linux doesn't return any window sources.
|
2019-01-18 23:29:32 +00:00
|
|
|
if (process.platform !== 'win32' && process.platform !== 'darwin') return
|
2018-06-17 23:00:28 +00:00
|
|
|
|
2018-07-17 04:20:50 +00:00
|
|
|
const w = new BrowserWindow({ width: 200, height: 200 })
|
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources = await getSources({ types: ['window'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
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()
|
|
|
|
}
|
2018-04-09 05:43:35 +00:00
|
|
|
})
|
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
it('returns display_ids matching the Screen API on Windows and Mac', async () => {
|
|
|
|
if (process.platform !== 'win32' && process.platform !== 'darwin') return
|
2018-06-17 23:00:28 +00:00
|
|
|
|
2018-04-09 05:43:35 +00:00
|
|
|
const displays = screen.getAllDisplays()
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources = await getSources({ types: ['screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(sources).to.be.an('array').of.length(displays.length)
|
2018-06-17 23:00:28 +00:00
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
for (let i = 0; i < sources.length; i++) {
|
|
|
|
expect(sources[i].display_id).to.equal(displays[i].id.toString())
|
|
|
|
}
|
2018-12-20 02:44:30 +00:00
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
it('returns empty sources when blocked', async () => {
|
2018-12-20 02:44:30 +00:00
|
|
|
ipcRenderer.send('handle-next-desktop-capturer-get-sources')
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources = await getSources({ types: ['screen'] })
|
2019-01-18 23:29:32 +00:00
|
|
|
expect(sources).to.be.empty()
|
2018-12-20 02:44:30 +00:00
|
|
|
})
|
2018-04-09 05:43:35 +00:00
|
|
|
})
|
2019-02-13 18:27:42 +00:00
|
|
|
|
|
|
|
it('disabling thumbnail should return empty images', async () => {
|
2019-09-03 06:59:54 +00:00
|
|
|
const w2 = new BrowserWindow({ show: false, width: 200, height: 200 })
|
|
|
|
const wShown = emittedOnce(w2, 'show')
|
|
|
|
w2.show()
|
2019-02-13 18:27:42 +00:00
|
|
|
await wShown
|
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
|
|
|
|
w2.destroy()
|
|
|
|
expect(isEmpties).to.be.an('array').that.is.not.empty()
|
|
|
|
expect(isEmpties.every(e => e === true)).to.be.true()
|
2019-02-13 18:27:42 +00:00
|
|
|
})
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
|
|
|
|
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()
|
2019-09-03 06:59:54 +00:00
|
|
|
const sources = await getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
it.skip('desktopCapturer.getSources returned an empty source list')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(sources).to.be.an('array').that.is.not.empty()
|
|
|
|
const foundSource = sources.find((source) => {
|
|
|
|
return source.id === mediaSourceId
|
|
|
|
})
|
2019-09-03 06:59:54 +00:00
|
|
|
expect(mediaSourceId).to.equal(foundSource!.id)
|
2019-08-15 06:51:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('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.
|
|
|
|
const MAX_WIN = 4
|
2019-09-03 06:59:54 +00:00
|
|
|
const mainWindow = w
|
2019-08-15 06:51:15 +00:00
|
|
|
const wList = [mainWindow]
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < MAX_WIN - 1; i++) {
|
|
|
|
const w = new BrowserWindow({ show: true, width: 100, height: 100 })
|
|
|
|
wList.push(w)
|
|
|
|
}
|
|
|
|
expect(wList.length).to.equal(MAX_WIN)
|
|
|
|
|
|
|
|
// Show and focus all the windows.
|
|
|
|
wList.forEach(async (w) => {
|
|
|
|
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.
|
2019-09-03 06:59:54 +00:00
|
|
|
let sources = await getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
w.destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
it.skip('desktopCapturer.getSources returned an empty source list')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
const wListReversed = wList.slice(0).reverse()
|
|
|
|
const canGoFurther = sources.every(
|
|
|
|
(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.
|
|
|
|
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)) {
|
|
|
|
const wNext = wList[index + 1]
|
|
|
|
w.moveAbove(wNext.getMediaSourceId())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
sources = await getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
|
|
|
})
|
|
|
|
// Only keep our windows again.
|
|
|
|
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) => {
|
|
|
|
expect(source.id).to.equal(wList[index].getMediaSourceId())
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
wList.forEach((w) => {
|
|
|
|
if (w !== mainWindow) {
|
|
|
|
w.destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|