fix: nativeImage.createThumbnailFromPath and shell.openExternal in renderer (#41875)

* fix: nativeImage.createThumbnailFromPath in renderer

* also fix shell.openExternal
This commit is contained in:
Jeremy Rose 2024-04-19 06:43:01 -07:00 committed by GitHub
parent c4aeb17245
commit ed9fec7da4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 34 deletions

View file

@ -1,6 +1,6 @@
import { expect } from 'chai';
import { nativeImage } from 'electron/common';
import { ifdescribe, ifit } from './lib/spec-helpers';
import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
import * as path from 'node:path';
describe('nativeImage module', () => {
@ -426,6 +426,8 @@ describe('nativeImage module', () => {
});
ifdescribe(process.platform !== 'linux')('createThumbnailFromPath(path, size)', () => {
useRemoteContext({ webPreferences: { contextIsolation: false, nodeIntegration: true } });
it('throws when invalid size is passed', async () => {
const badSize = { width: -1, height: -1 };
@ -473,6 +475,13 @@ describe('nativeImage module', () => {
const result = await nativeImage.createThumbnailFromPath(imgPath, maxSize);
expect(result.getSize()).to.deep.equal(maxSize);
});
itremote('works in the renderer', async (path: string) => {
const { nativeImage } = require('electron');
const goodSize = { width: 100, height: 100 };
const result = await nativeImage.createThumbnailFromPath(path, goodSize);
expect(result.isEmpty()).to.equal(false);
}, [path.join(fixturesPath, 'assets', 'logo.png')]);
});
describe('addRepresentation()', () => {

View file

@ -31,7 +31,7 @@ describe('shell module', () => {
});
afterEach(closeAllWindows);
it('opens an external link', async () => {
async function urlOpened () {
let url = 'http://127.0.0.1';
let requestReceived: Promise<any>;
if (process.platform === 'linux') {
@ -53,12 +53,26 @@ describe('shell module', () => {
url = (await listen(server)).url;
requestReceived = new Promise<void>(resolve => server.on('connection', () => resolve()));
}
return { url, requestReceived };
}
it('opens an external link', async () => {
const { url, requestReceived } = await urlOpened();
await Promise.all<void>([
shell.openExternal(url),
requestReceived
]);
});
it('opens an external link in the renderer', async () => {
const { url, requestReceived } = await urlOpened();
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: false, contextIsolation: false, nodeIntegration: true } });
await w.loadURL('about:blank');
await Promise.all<void>([
w.webContents.executeJavaScript(`require("electron").shell.openExternal(${JSON.stringify(url)})`),
requestReceived
]);
});
});
describe('shell.trashItem()', () => {