const UNSUPPORTED_PERMISSIONS = [
{ name: 'foobarbaz' }, // Not in spec, for testing only.
];
// Create a closure, so that tests are run on the correct window object.
function createPermissionTester(iframe) {
const iframeWindow = iframe.contentWindow;
return {
async setPermissions(allow, context = iframeWindow.document) {
const permissions = PERMISSIONS.map(({ type }) => {
return {
type,
allow,
context,
};
});
await SpecialPowers.popPermissions();
return SpecialPowers.pushPermissions(permissions);
},
checkPermissions(permissions, expectedState, mediaExpectedState = expectedState) {
const promisesToQuery = permissions.map(({ name: expectedName }) => {
return iframeWindow.navigator.permissions
.query({ name: expectedName })
.then(
({ state, name }) => {
is(name, expectedName, `correct name for '${expectedName}'`);
if (['camera', 'microphone'].includes(expectedName)) {
is(state, mediaExpectedState, `correct state for '${expectedName}'`);
} else {
is(state, expectedState, `correct state for '${expectedName}'`);
}
},
() => ok(false, `query should not have rejected for '${name}'`)
);
});
return Promise.all(promisesToQuery);
},
checkUnsupportedPermissions(permissions) {
const promisesToQuery = permissions.map(({ name }) => {
return iframeWindow.navigator.permissions
.query({ name })
.then(
() => ok(false, `query should not have resolved for '${name}'`),
error => {
is(error.name, 'TypeError',
`query should have thrown TypeError for '${name}'`);
}
);
});
return Promise.all(promisesToQuery);
},
promiseStateChanged(name, state) {
return iframeWindow.navigator.permissions
.query({ name })
.then(status => {
return new Promise( resolve => {
status.onchange = () => {
status.onchange = null;
is(status.state, state, `state changed for '${name}'`);
resolve();
};
});
},
() => ok(false, `query should not have rejected for '${name}'`));
},
testStatusOnChange() {
return new Promise((resolve) => {
SpecialPowers.popPermissions(() => {
const permission = 'geolocation';
const promiseGranted = this.promiseStateChanged(permission, 'granted');
this.setPermissions(ALLOW_ACTION);
promiseGranted.then(async () => {
const promisePrompt = this.promiseStateChanged(permission, 'prompt');
await SpecialPowers.popPermissions();
return promisePrompt;
}).then(resolve);
});
});
},
testInvalidQuery() {
return iframeWindow.navigator.permissions
.query({ name: 'invalid' })
.then(
() => ok(false, 'invalid query should not have resolved'),
() => ok(true, 'invalid query should have rejected')
);
},
async testNotFullyActiveDoc() {
const iframe1 = await createIframe();
const expectedErrorClass = iframe1.contentWindow.DOMException;
const permAPI = iframe1.contentWindow.navigator.permissions;
// Document no longer fully active
iframe1.remove();
await new Promise((res) => {
permAPI.query({ name: "geolocation" }).catch((error) => {
ok(
error instanceof expectedErrorClass, "DOMException from other realm"
);
is(
error.name, "InvalidStateError", "Must reject with a InvalidStateError"
);
iframe1.remove();
res();
});
});
},
async testNotFullyActiveChange() {
await SpecialPowers.popPermissions();
const iframe2 = await createIframe();
const initialStatus = await iframe2.contentWindow.navigator.permissions.query(
{ name: "geolocation" }
);
await SpecialPowers.pushPermissions([
{
type: "geo",
allow: PROMPT_ACTION,
context: iframe2.contentWindow.document,
},
]);
is(
initialStatus.state, "prompt", "Initially the iframe's permission is prompt"
);
// Document no longer fully active
const stolenDoc = iframe2.contentWindow.document;
iframe2.remove();
initialStatus.onchange = () => {
ok(false, "onchange must not fire when document is not fully active.");
};
// We set it to grant for this origin, but the PermissionStatus doesn't change.
await SpecialPowers.pushPermissions([
{
type: "geo",
allow: ALLOW_ACTION,
context: stolenDoc,
},
]);
is(
initialStatus.state, "prompt", "Inactive document's permission must not change"
);
// Re-attach the iframe
document.body.appendChild(iframe2);
await new Promise((res) => (iframe2.onload = res));
// Fully active again
const newStatus = await iframe2.contentWindow.navigator.permissions.query({
name: "geolocation",
});
is(newStatus.state, "granted", "Reflect that we are granted");
const newEventPromise = new Promise((res) => (newStatus.onchange = res));
await SpecialPowers.pushPermissions([
{
type: "geo",
allow: DENY_ACTION,
context: iframe2.contentWindow.document,
},
]);
// Event fires...
await newEventPromise;
is(initialStatus.state, "prompt", "Remains prompt, as it's actually dead.");
is(newStatus.state, "denied", "New status must be 'denied'.");
iframe2.remove();
},
};
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.