diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 70d57977af6a..8c9c45e60282 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -12,6 +12,7 @@ import { app, BrowserWindow, BrowserView, dialog, ipcMain, OnBeforeSendHeadersLi import { emittedOnce, emittedUntil, emittedNTimes } from './events-helpers'; import { ifit, ifdescribe, defer, delay } from './spec-helpers'; import { closeWindow, closeAllWindows } from './window-helpers'; +import { areColorsSimilar, captureScreen, CHROMA_COLOR_HEX, getPixelColor } from './screen-helpers'; const features = process._linkedBinding('electron_common_features'); const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures'); @@ -4872,5 +4873,69 @@ describe('BrowserWindow module', () => { w.setBounds(newBounds); expect(w.getBounds()).to.deep.equal(newBounds); }); + + // Linux doesn't return any capture sources. + ifit(process.platform !== 'linux')('should not display a visible background', async () => { + const display = screen.getPrimaryDisplay(); + + const backgroundWindow = new BrowserWindow({ + ...display.bounds, + frame: false, + backgroundColor: CHROMA_COLOR_HEX, + hasShadow: false + }); + + await backgroundWindow.loadURL('about:blank'); + + const foregroundWindow = new BrowserWindow({ + ...display.bounds, + show: true, + transparent: true, + frame: false, + hasShadow: false + }); + + foregroundWindow.loadFile(path.join(__dirname, 'fixtures', 'pages', 'half-background-color.html')); + await emittedOnce(foregroundWindow, 'ready-to-show'); + + const screenCapture = await captureScreen(); + const leftHalfColor = getPixelColor(screenCapture, { + x: display.size.width / 4, + y: display.size.height / 2 + }); + const rightHalfColor = getPixelColor(screenCapture, { + x: display.size.width - (display.size.width / 4), + y: display.size.height / 2 + }); + + expect(areColorsSimilar(leftHalfColor, CHROMA_COLOR_HEX)).to.be.true(); + expect(areColorsSimilar(rightHalfColor, '#ff0000')).to.be.true(); + }); + }); + + describe('"backgroundColor" option', () => { + afterEach(closeAllWindows); + + // Linux doesn't return any capture sources. + ifit(process.platform !== 'linux')('should display the set color', async () => { + const display = screen.getPrimaryDisplay(); + + const w = new BrowserWindow({ + ...display.bounds, + show: true, + backgroundColor: CHROMA_COLOR_HEX + }); + + w.loadURL('about:blank'); + await emittedOnce(w, 'ready-to-show'); + + const screenCapture = await captureScreen(); + const centerColor = getPixelColor(screenCapture, { + x: display.size.width / 2, + y: display.size.height / 2 + }); + + expect(areColorsSimilar(centerColor, CHROMA_COLOR_HEX)).to.be.true(); + }); }); }); diff --git a/spec-main/fixtures/pages/half-background-color.html b/spec-main/fixtures/pages/half-background-color.html new file mode 100644 index 000000000000..07e5ccdcadf9 --- /dev/null +++ b/spec-main/fixtures/pages/half-background-color.html @@ -0,0 +1,20 @@ + + +
+ + + + + + + + diff --git a/spec-main/screen-helpers.ts b/spec-main/screen-helpers.ts new file mode 100644 index 000000000000..26fbcb8ea2b0 --- /dev/null +++ b/spec-main/screen-helpers.ts @@ -0,0 +1,87 @@ +import * as path from 'path'; +import * as fs from 'fs'; +import { screen, desktopCapturer, NativeImage } from 'electron'; + +const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures'); + +/** Chroma key green. */ +export const CHROMA_COLOR_HEX = '#00b140'; + +/** + * Capture the screen at the given point. + * + * NOTE: Not yet supported on Linux in CI due to empty sources list. + */ +export const captureScreen = async (point: Electron.Point = { x: 0, y: 0 }): Promise