fix: ensure correct WebContents
when checking PDFReadyToPrint
(33-x-y) (#43998)
* fix: ensure correct `WebContents` when checking `PDFReadyToPrint` (#43943) * fix: ensure correct WebContents when checking PDFReadyToPrint * test: fix paths on Windows * Update spec/api-web-contents-spec.ts Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> --------- Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
parent
73c1413374
commit
a44059aa05
2 changed files with 58 additions and 9 deletions
|
@ -3,6 +3,7 @@ import { AddressInfo } from 'node:net';
|
|||
import * as path from 'node:path';
|
||||
import * as fs from 'node:fs';
|
||||
import * as http from 'node:http';
|
||||
import * as url from 'node:url';
|
||||
import { BrowserWindow, ipcMain, webContents, session, app, BrowserView, WebContents } from 'electron/main';
|
||||
import { closeAllWindows } from './lib/window-helpers';
|
||||
import { ifdescribe, defer, waitUntil, listen, ifit } from './lib/spec-helpers';
|
||||
|
@ -11,7 +12,6 @@ import { setTimeout } from 'node:timers/promises';
|
|||
|
||||
const pdfjs = require('pdfjs-dist');
|
||||
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
||||
const mainFixturesPath = path.resolve(__dirname, 'fixtures');
|
||||
const features = process._linkedBinding('electron_common_features');
|
||||
|
||||
describe('webContents module', () => {
|
||||
|
@ -1184,7 +1184,7 @@ describe('webContents module', () => {
|
|||
}).to.throw('\'icon\' parameter is required');
|
||||
|
||||
expect(() => {
|
||||
w.webContents.startDrag({ file: __filename, icon: path.join(mainFixturesPath, 'blank.png') });
|
||||
w.webContents.startDrag({ file: __filename, icon: path.join(fixturesPath, 'blank.png') });
|
||||
}).to.throw(/Failed to load image from path (.+)/);
|
||||
});
|
||||
});
|
||||
|
@ -2478,6 +2478,45 @@ describe('webContents module', () => {
|
|||
const { items } = await page.getTextContent();
|
||||
expect(containsText(items, /Cat: The Ideal Pet/)).to.be.true();
|
||||
});
|
||||
|
||||
it('from an existing pdf document in a WebView', async () => {
|
||||
const win = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
webviewTag: true
|
||||
}
|
||||
});
|
||||
|
||||
await win.loadURL('about:blank');
|
||||
const webContentsCreated = once(app, 'web-contents-created') as Promise<[any, WebContents]>;
|
||||
|
||||
const src = url.format({
|
||||
pathname: `${fixturesPath.replaceAll('\\', '/')}/cat.pdf`,
|
||||
protocol: 'file',
|
||||
slashes: true
|
||||
});
|
||||
await win.webContents.executeJavaScript(`
|
||||
new Promise((resolve, reject) => {
|
||||
const webview = new WebView()
|
||||
webview.setAttribute('src', '${src}')
|
||||
document.body.appendChild(webview)
|
||||
webview.addEventListener('did-finish-load', () => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
`);
|
||||
|
||||
const [, webContents] = await webContentsCreated;
|
||||
|
||||
await once(webContents, '-pdf-ready-to-print');
|
||||
|
||||
const data = await webContents.printToPDF({});
|
||||
const doc = await pdfjs.getDocument(data).promise;
|
||||
expect(doc.numPages).to.equal(2);
|
||||
const page = await doc.getPage(1);
|
||||
const { items } = await page.getTextContent();
|
||||
expect(containsText(items, /Cat: The Ideal Pet/)).to.be.true();
|
||||
});
|
||||
});
|
||||
|
||||
describe('PictureInPicture video', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue