Skip to content

Instantly share code, notes, and snippets.

@ckerr
Last active March 8, 2021 20:26
Show Gist options
  • Save ckerr/af3e1a018f5dcce4a2ff40004ef5bab5 to your computer and use it in GitHub Desktop.
Save ckerr/af3e1a018f5dcce4a2ff40004ef5bab5 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
</head>
<body>
Display is <span id="size"></span>
<script src="renderer.js"></script>
</body>
</html>
const { app, ipcMain, BrowserWindow } = require("electron");
app.whenReady().then(() => {
const mainWindow = new BrowserWindow({
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
},
});
mainWindow.loadFile("index.html");
mainWindow.webContents.openDevTools();
});
app.on("window-all-closed", () => app.quit());
// test harness
function finishTest(success, ...rest) {
if (!Object.prototype.hasOwnProperty.call(global, 'testResult')) {
global.testResult = [ success, ...rest ];
console.log(JSON.stringify(global.testResult));
}
}
ipcMain.on('test-passed', (event, ...args) => finishTest(true, ...args));
ipcMain.on('test-failed', (event, ...args) => finishTest(false, ...args));
ipcMain.on('test-finished', (event, success, ...args) => finishTest(success, ...args));
process.on('uncaughtException', (error) => finishTest(false, error));
const { ipcRenderer, remote } = require("electron");
try {
const primaryDisplay = remote.screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.workAreaSize;
document.querySelector("#size").textContent = `${width}×${height}`;
ipcRenderer.send('test-passed');
} catch (e) {
ipcRenderer.send('test-failed', e.toString());
}
// poll the main process for test completion
const getTestResultFromMain = async () => {
const IntervalMsec = 20;
return new Promise (resolve => {
const timeout = setInterval(() => {
if (global.hasOwnProperty('testResult')) {
clearInterval(timeout);
resolve(global.testResult);
}
}, IntervalMsec);
});
};
describe('26610', function () {
it('sees remote.screen.getPrimaryDisplay', async () => {
const result = await global.app.evaluate(getTestResultFromMain);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThanOrEqual(1);
if (!result[0]) {
console.warn(...result.slice(1));
}
expect(result[0]).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment