fix: context-menu event with BaseWindows (#44940)

fix: context-menu event with BaseWindows
This commit is contained in:
Shelley Vohr 2024-12-04 22:35:28 +01:00 committed by GitHub
parent 687a59b43b
commit 208dc568d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 26 deletions

View file

@ -1,4 +1,4 @@
import { BrowserWindow, ipcMain, webContents, session, app, BrowserView, WebContents } from 'electron/main';
import { BrowserWindow, ipcMain, webContents, session, app, BrowserView, WebContents, BaseWindow, WebContentsView } from 'electron/main';
import { expect } from 'chai';
@ -2749,6 +2749,57 @@ describe('webContents module', () => {
expect(params.x).to.be.a('number');
expect(params.y).to.be.a('number');
});
it('emits when right clicked in a WebContentsView', async () => {
const w = new BaseWindow({ show: false });
const mainView = new WebContentsView({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
const draggablePage = path.join(fixturesPath, 'pages', 'draggable-page.html');
await mainView.webContents.loadFile(draggablePage);
w.contentView.addChildView(mainView);
const { width, height } = w.getContentBounds();
mainView.setBounds({ x: 0, y: 0, width, height });
const promise = once(mainView.webContents, 'context-menu') as Promise<[any, Electron.ContextMenuParams]>;
// Simulate right-click to create context-menu event.
const opts = { x: 0, y: 0, button: 'right' as const };
mainView.webContents.sendInputEvent({ ...opts, type: 'mouseDown' });
mainView.webContents.sendInputEvent({ ...opts, type: 'mouseUp' });
const [, params] = await promise;
expect(params.pageURL).to.equal(mainView.webContents.getURL());
expect(params.frame).to.be.an('object');
expect(params.x).to.be.a('number');
expect(params.y).to.be.a('number');
});
it('emits when right clicked in a BrowserWindow with vibrancy', async () => {
const w = new BrowserWindow({ show: false, vibrancy: 'titlebar' });
await w.loadFile(path.join(fixturesPath, 'pages', 'draggable-page.html'));
const promise = once(w.webContents, 'context-menu') as Promise<[any, Electron.ContextMenuParams]>;
// Simulate right-click to create context-menu event.
const opts = { x: 0, y: 0, button: 'right' as const };
w.webContents.sendInputEvent({ ...opts, type: 'mouseDown' });
w.webContents.sendInputEvent({ ...opts, type: 'mouseUp' });
const [, params] = await promise;
expect(params.pageURL).to.equal(w.webContents.getURL());
expect(params.frame).to.be.an('object');
expect(params.x).to.be.a('number');
expect(params.y).to.be.a('number');
});
});
describe('close() method', () => {