Skip to content

Commit

Permalink
spec: add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 10, 2022
1 parent 1667e42 commit eccb487
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -1937,6 +1937,26 @@ describe('BrowserWindow module', () => {
});
});

describe('Opening a BrowserWindow from a link', () => {
let appProcess: childProcess.ChildProcessWithoutNullStreams | undefined;

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

it('can properly open and load a new window from a link', async () => {
const appPath = path.join(__dirname, 'fixtures', 'apps', 'open-new-window-from-link');

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

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

describe('BrowserWindow.fromWebContents(webContents)', () => {
afterEach(closeAllWindows);

Expand Down
11 changes: 11 additions & 0 deletions spec-main/fixtures/apps/open-new-window-from-link/index.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<a href="./new-window-page.html">Open New Window</a>
</body>
</html>
64 changes: 64 additions & 0 deletions spec-main/fixtures/apps/open-new-window-from-link/main.js
@@ -0,0 +1,64 @@
const { app, BrowserWindow } = require('electron');
const path = require('path');

async function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
x: 100,
y: 100,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
nodeIntegration: true
}
});

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

const rect = await mainWindow.webContents.executeJavaScript('JSON.parse(JSON.stringify(document.querySelector("a").getBoundingClientRect()))');
const x = rect.x + rect.width / 2;
const y = rect.y + rect.height / 2;

function click (x, y, options) {
x = Math.floor(x);
y = Math.floor(y);
mainWindow.webContents.sendInputEvent({
type: 'mouseDown',
button: 'left',
x,
y,
clickCount: 1,
...options
});

mainWindow.webContents.sendInputEvent({
type: 'mouseUp',
button: 'left',
x,
y,
clickCount: 1,
...options
});
}

click(x, y, { modifiers: ['shift'] });
}

app.whenReady().then(() => {
app.on('web-contents-created', (e, wc) => {
wc.on('render-process-gone', (e, details) => {
console.error(details);
process.exit(1);
});

wc.on('did-finish-load', () => {
const title = wc.getTitle();
if (title === 'Window From Link') {
process.exit(0);
}
});
});

createWindow();
});
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<title>Window From Link</title>
</head>
<body>
I'm a window opened from a link!
</body>
</html>
@@ -0,0 +1,4 @@
{
"name": "electron-test-open-new-window-from-link",
"main": "main.js"
}
3 changes: 3 additions & 0 deletions spec-main/fixtures/apps/open-new-window-from-link/preload.js
@@ -0,0 +1,3 @@
window.addEventListener('click', e => {
console.log('click', e);
});

0 comments on commit eccb487

Please sign in to comment.