chore: enable notification permission tests in WebKit

The Notifications API has been supported in WebKit since 2022, enable related permission and tests.
This commit is contained in:
Yury Semikhatsky 2024-07-15 14:50:31 -07:00
parent 37ffbd757e
commit 652dd1f388
2 changed files with 112 additions and 115 deletions

View file

@ -1176,6 +1176,7 @@ export class WKPage implements PageDelegate {
async _grantPermissions(origin: string, permissions: string[]) { async _grantPermissions(origin: string, permissions: string[]) {
const webPermissionToProtocol = new Map<string, string>([ const webPermissionToProtocol = new Map<string, string>([
['geolocation', 'geolocation'], ['geolocation', 'geolocation'],
['notifications', 'notifications'],
['clipboard-read', 'clipboard-read'], ['clipboard-read', 'clipboard-read'],
]); ]);
const filtered = permissions.map(permission => { const filtered = permissions.map(permission => {

View file

@ -21,141 +21,137 @@ function getPermission(page, name) {
return page.evaluate(name => navigator.permissions.query({ name }).then(result => result.state), name); return page.evaluate(name => navigator.permissions.query({ name }).then(result => result.state), name);
} }
it.describe('permissions', () => { it('should be prompt by default', async ({ page, server }) => {
it.skip(({ browserName }) => browserName === 'webkit', 'Permissions API is not implemented in WebKit (see https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API)'); await page.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');
});
it('should be prompt by default', async ({ page, server }) => { it('should deny permission when not listed', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt'); await context.grantPermissions([], { origin: server.EMPTY_PAGE });
}); expect(await getPermission(page, 'geolocation')).toBe('denied');
});
it('should deny permission when not listed', async ({ page, context, server }) => { it('should fail when bad permission is given', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.grantPermissions([], { origin: server.EMPTY_PAGE }); let error: Error;
expect(await getPermission(page, 'geolocation')).toBe('denied'); await context.grantPermissions(['foo'], { origin: server.EMPTY_PAGE }).catch(e => error = e);
}); expect(error.message).toContain('Unknown permission: foo');
});
it('should fail when bad permission is given', async ({ page, context, server }) => { it('should grant geolocation permission when origin is listed', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
let error: Error; await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
await context.grantPermissions(['foo'], { origin: server.EMPTY_PAGE }).catch(e => error = e); expect(await getPermission(page, 'geolocation')).toBe('granted');
expect(error.message).toContain('Unknown permission: foo'); });
});
it('should grant geolocation permission when origin is listed', async ({ page, context, server }) => { it('should grant window-management permission when origin is listed', async ({ page, context, server, browserName }) => {
await page.goto(server.EMPTY_PAGE); it.skip(browserName !== 'chromium', 'Only Chromium supports window management API.');
await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('granted');
});
it('should grant window-management permission when origin is listed', async ({ page, context, server, browserName }) => { await page.goto(server.EMPTY_PAGE);
it.fail(browserName === 'firefox'); await context.grantPermissions(['window-management'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'window-management')).toBe('granted');
});
await page.goto(server.EMPTY_PAGE); it('should prompt for geolocation permission when origin is not listed', async ({ page, context, server }) => {
await context.grantPermissions(['window-management'], { origin: server.EMPTY_PAGE }); await page.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'window-management')).toBe('granted'); await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
}); await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
expect(await getPermission(page, 'geolocation')).toBe('prompt');
});
it('should prompt for geolocation permission when origin is not listed', async ({ page, context, server }) => { it('should grant notifications permission when listed', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); await context.grantPermissions(['notifications'], { origin: server.EMPTY_PAGE });
await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); expect(await getPermission(page, 'notifications')).toBe('granted');
expect(await getPermission(page, 'geolocation')).toBe('prompt'); });
});
it('should grant notifications permission when listed', async ({ page, context, server }) => { it('should accumulate when adding', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['notifications'], { origin: server.EMPTY_PAGE }); await context.grantPermissions(['geolocation']);
expect(await getPermission(page, 'notifications')).toBe('granted'); await context.grantPermissions(['notifications']);
}); expect(await getPermission(page, 'geolocation')).toBe('granted');
expect(await getPermission(page, 'notifications')).toBe('granted');
});
it('should accumulate when adding', async ({ page, context, server }) => { it('should clear permissions', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation']); await context.grantPermissions(['geolocation']);
await context.grantPermissions(['notifications']); await context.clearPermissions();
expect(await getPermission(page, 'geolocation')).toBe('granted'); await context.grantPermissions(['notifications']);
expect(await getPermission(page, 'notifications')).toBe('granted'); expect(await getPermission(page, 'geolocation')).not.toBe('granted');
}); expect(await getPermission(page, 'notifications')).toBe('granted');
});
it('should clear permissions', async ({ page, context, server }) => { it('should grant permission when listed for all domains', async ({ page, context, server }) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation']); await context.grantPermissions(['geolocation']);
await context.clearPermissions(); expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.grantPermissions(['notifications']); });
expect(await getPermission(page, 'geolocation')).not.toBe('granted');
expect(await getPermission(page, 'notifications')).toBe('granted');
});
it('should grant permission when listed for all domains', async ({ page, context, server }) => { it('should grant permission when creating context', async ({ server, browser }) => {
await page.goto(server.EMPTY_PAGE); const context = await browser.newContext({ permissions: ['geolocation'] });
await context.grantPermissions(['geolocation']); const page = await context.newPage();
expect(await getPermission(page, 'geolocation')).toBe('granted'); await page.goto(server.EMPTY_PAGE);
}); expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.close();
});
it('should grant permission when creating context', async ({ server, browser }) => { it('should reset permissions', async ({ page, context, server }) => {
const context = await browser.newContext({ permissions: ['geolocation'] }); await page.goto(server.EMPTY_PAGE);
const page = await context.newPage(); await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
await page.goto(server.EMPTY_PAGE); expect(await getPermission(page, 'geolocation')).toBe('granted');
expect(await getPermission(page, 'geolocation')).toBe('granted'); await context.clearPermissions();
await context.close(); expect(await getPermission(page, 'geolocation')).toBe('prompt');
}); });
it('should reset permissions', async ({ page, context, server }) => { it('should trigger permission onchange', async ({ page, context, server, browserName, browserMajorVersion }) => {
await page.goto(server.EMPTY_PAGE); it.fail(browserName === 'webkit');
await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.clearPermissions();
expect(await getPermission(page, 'geolocation')).toBe('prompt');
});
it('should trigger permission onchange', async ({ page, context, server, browserName, browserMajorVersion }) => { await page.goto(server.EMPTY_PAGE);
it.fail(browserName === 'webkit'); await page.evaluate(() => {
window['events'] = [];
await page.goto(server.EMPTY_PAGE); return navigator.permissions.query({ name: 'geolocation' }).then(function(result) {
await page.evaluate(() => { window['events'].push(result.state);
window['events'] = []; result.onchange = function() {
return navigator.permissions.query({ name: 'geolocation' }).then(function(result) {
window['events'].push(result.state); window['events'].push(result.state);
result.onchange = function() { };
window['events'].push(result.state);
};
});
}); });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt']);
await context.grantPermissions([], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied']);
await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted']);
await context.clearPermissions();
// Note: Chromium 110 stopped triggering "onchange" when clearing permissions.
expect(await page.evaluate(() => window['events'])).toEqual(
(browserName === 'chromium' && browserMajorVersion === 110) ?
['prompt', 'denied', 'granted'] :
['prompt', 'denied', 'granted', 'prompt']);
}); });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt']);
await context.grantPermissions([], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied']);
await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted']);
await context.clearPermissions();
it('should isolate permissions between browser contexts', async ({ server, browser }) => { // Note: Chromium 110 stopped triggering "onchange" when clearing permissions.
const context = await browser.newContext(); expect(await page.evaluate(() => window['events'])).toEqual(
const page = await context.newPage(); (browserName === 'chromium' && browserMajorVersion === 110) ?
await page.goto(server.EMPTY_PAGE); ['prompt', 'denied', 'granted'] :
const otherContext = await browser.newContext(); ['prompt', 'denied', 'granted', 'prompt']);
const otherPage = await otherContext.newPage(); });
await otherPage.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('prompt');
await context.grantPermissions([], { origin: server.EMPTY_PAGE }); it('should isolate permissions between browser contexts', async ({ server, browser }) => {
await otherContext.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); const context = await browser.newContext();
expect(await getPermission(page, 'geolocation')).toBe('denied'); const page = await context.newPage();
expect(await getPermission(otherPage, 'geolocation')).toBe('granted'); await page.goto(server.EMPTY_PAGE);
const otherContext = await browser.newContext();
const otherPage = await otherContext.newPage();
await otherPage.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('prompt');
await context.clearPermissions(); await context.grantPermissions([], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('prompt'); await otherContext.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(otherPage, 'geolocation')).toBe('granted'); expect(await getPermission(page, 'geolocation')).toBe('denied');
await otherContext.close(); expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
await context.close();
}); await context.clearPermissions();
expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
await otherContext.close();
await context.close();
}); });
it('should support clipboard read', async ({ page, context, server, browserName, isWindows, isLinux, headless }) => { it('should support clipboard read', async ({ page, context, server, browserName, isWindows, isLinux, headless }) => {