Skip to content

Commit

Permalink
spec: add a test for correct behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 26, 2022
1 parent 2ffdfac commit 8925074
Show file tree
Hide file tree
Showing 7 changed files with 985 additions and 212 deletions.
19 changes: 18 additions & 1 deletion spec-main/api-browser-window-spec.ts
Expand Up @@ -1641,7 +1641,15 @@ describe('BrowserWindow module', () => {
});

ifdescribe(process.platform === 'darwin')('BrowserWindow.setVibrancy(type)', () => {
afterEach(closeAllWindows);
let appProcess: childProcess.ChildProcessWithoutNullStreams | undefined;

afterEach(() => {
if (appProcess && !appProcess.killed) {
appProcess.kill();
appProcess = undefined;
}
closeAllWindows();
});

it('allows setting, changing, and removing the vibrancy', () => {
const w = new BrowserWindow({ show: false });
Expand All @@ -1660,6 +1668,15 @@ describe('BrowserWindow module', () => {
w.setVibrancy('i-am-not-a-valid-vibrancy-type' as any);
}).to.not.throw();
});

it('Allows setting a transparent window via CSS', async () => {
const appPath = path.join(__dirname, 'fixtures', 'apps', 'background-color-transparent');

appProcess = childProcess.spawn(process.execPath, [appPath]);

const [code] = await emittedOnce(appProcess, 'exit');
expect(code).to.equal(0);
});
});

ifdescribe(process.platform === 'darwin')('trafficLightPosition', () => {
Expand Down
15 changes: 15 additions & 0 deletions spec-main/fixtures/apps/background-color-transparent/index.html
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test-color-window</title>
<style>
body {
background: green;
}
</style>
</head>
<body id="body">
<script src="./renderer.js"></script>
</body>
</html>
59 changes: 59 additions & 0 deletions spec-main/fixtures/apps/background-color-transparent/main.js
@@ -0,0 +1,59 @@
const { app, BrowserWindow, desktopCapturer, ipcMain } = require('electron');
const getColors = require('get-image-colors');

const colors = {};

// Fetch the test window.
const getWindow = async () => {
const sources = await desktopCapturer.getSources({ types: ['window'] });
const filtered = sources.filter(s => s.name === 'test-color-window');

if (filtered.length === 0) {
throw new Error('Could not find test window');
}

return filtered[0];
};

async function createWindow () {
const mainWindow = new BrowserWindow({
frame: false,
transparent: true,
vibrancy: 'under-window',
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
});

await mainWindow.loadFile('index.html');

// Get initial green background color.
const window = await getWindow();
const buf = window.thumbnail.toJPEG(100);
const result = await getColors(buf, { count: 1, type: 'image/jpg' });
colors.green = result[0].hex();
}

ipcMain.on('set-transparent', async () => {
// Get updated background color.
const window = await getWindow();
const buf = window.thumbnail.toJPEG(100);
const result = await getColors(buf, { count: 1, type: 'image/jpg' });
colors.transparent = result[0].hex();

const { green, transparent } = colors;
process.exit(green === transparent ? 1 : 0);
});

app.whenReady().then(() => {
createWindow();

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
@@ -0,0 +1,4 @@
{
"name": "background-color-transparent",
"main": "main.js"
}
@@ -0,0 +1,9 @@
const { ipcRenderer } = require('electron');

window.setTimeout(async (_) => {
document.body.style.background = 'transparent';
}, 3000);

window.setTimeout(async (_) => {
ipcRenderer.send('set-transparent');
}, 5000);
1 change: 1 addition & 0 deletions spec-main/package.json
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"get-image-colors": "^4.0.0",
"pdfjs-dist": "^2.2.228"
}
}

0 comments on commit 8925074

Please sign in to comment.