Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add missing tests to api-web-frame-spec.ts #39720

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 98 additions & 33 deletions spec/api-web-frame-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,57 +76,122 @@ describe('webFrame module', () => {
let w: WebContents;
before(async () => {
const win = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false, nodeIntegration: true } });
await win.loadURL('about:blank');
await win.loadURL('data:text/html,<iframe name="test"></iframe>');
w = win.webContents;
await w.executeJavaScript('webFrame = require(\'electron\').webFrame; null');
await w.executeJavaScript(`
var { webFrame } = require('electron');
var isSameWebFrame = (a, b) => a.context === b.context;
childFrame = webFrame.firstChild;
null
`);
});
it('top is self for top frame', async () => {
const equal = await w.executeJavaScript('webFrame.top.context === webFrame.context');
expect(equal).to.be.true();

describe('top', () => {
it('is self for top frame', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(webFrame.top, webFrame)');
expect(equal).to.be.true();
});

it('is self for child frame', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(childFrame.top, webFrame)');
expect(equal).to.be.true();
});
});

it('opener is null for top frame', async () => {
const equal = await w.executeJavaScript('webFrame.opener === null');
expect(equal).to.be.true();
describe('opener', () => {
it('is null for top frame', async () => {
const equal = await w.executeJavaScript('webFrame.opener === null');
expect(equal).to.be.true();
});
});

it('firstChild is null for top frame', async () => {
const equal = await w.executeJavaScript('webFrame.firstChild === null');
expect(equal).to.be.true();
describe('parent', () => {
it('is null for top frame', async () => {
const equal = await w.executeJavaScript('webFrame.parent === null');
expect(equal).to.be.true();
});

it('is top frame for child frame', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(childFrame.parent, webFrame)');
expect(equal).to.be.true();
});
});

it('getFrameForSelector() does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.getFrameForSelector(\'unexist-selector\') === null');
expect(equal).to.be.true();
describe('firstChild', () => {
it('is child frame for top frame', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(webFrame.firstChild, childFrame)');
expect(equal).to.be.true();
});

it('is null for child frame', async () => {
const equal = await w.executeJavaScript('childFrame.firstChild === null');
expect(equal).to.be.true();
});
});

it('findFrameByName() does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.findFrameByName(\'unexist-name\') === null');
expect(equal).to.be.true();
describe('getFrameForSelector()', () => {
it('does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.getFrameForSelector("unexist-selector") === null');
expect(equal).to.be.true();
});

it('returns the webFrame when found', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(webFrame.getFrameForSelector("iframe"), childFrame)');
expect(equal).to.be.true();
});
});

it('findFrameByRoutingId() does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.findFrameByRoutingId(-1) === null');
expect(equal).to.be.true();
describe('findFrameByName()', () => {
it('does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.findFrameByName("unexist-name") === null');
expect(equal).to.be.true();
});

it('returns the webFrame when found', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByName("test"), childFrame)');
expect(equal).to.be.true();
});
});

describe('executeJavaScript', () => {
before(() => {
w.executeJavaScript(`
childFrameElement = document.createElement('iframe');
document.body.appendChild(childFrameElement);
childFrame = webFrame.firstChild;
null
`);
describe('findFrameByRoutingId()', () => {
it('does not crash when not found', async () => {
const equal = await w.executeJavaScript('webFrame.findFrameByRoutingId(-1) === null');
expect(equal).to.be.true();
});

it('returns the webFrame when found', async () => {
const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByRoutingId(childFrame.routingId), childFrame)');
expect(equal).to.be.true();
});
});

describe('setZoomFactor()', () => {
it('works', async () => {
const equal = await w.executeJavaScript('childFrame.setZoomFactor(2.0); childFrame.getZoomFactor() === 2.0');
expect(equal).to.be.true();
});
});

after(() => {
w.executeJavaScript(`
childFrameElement.remove();
null
`);
describe('setZoomLevel()', () => {
it('works', async () => {
const equal = await w.executeJavaScript('childFrame.setZoomLevel(5); childFrame.getZoomLevel() === 5');
expect(equal).to.be.true();
});
});

describe('getResourceUsage()', () => {
it('works', async () => {
const result = await w.executeJavaScript('childFrame.getResourceUsage()');
expect(result).to.have.property('images').that.is.an('object');
expect(result).to.have.property('scripts').that.is.an('object');
expect(result).to.have.property('cssStyleSheets').that.is.an('object');
expect(result).to.have.property('xslStyleSheets').that.is.an('object');
expect(result).to.have.property('fonts').that.is.an('object');
expect(result).to.have.property('other').that.is.an('object');
});
});

describe('executeJavaScript', () => {
it('executeJavaScript() yields results via a promise and a sync callback', async () => {
const { callbackResult, callbackError, result } = await w.executeJavaScript(`new Promise(resolve => {
let callbackResult, callbackError;
Expand Down