Commit 10584b73 authored by lalitm@google.com's avatar lalitm@google.com

Add permissions helper class to carry out promise based testing

There are numerous flaky tests which are likely caused by race conditions
between testRunner.setPermission and subsequent operations.

Introduce a helper class which wraps testRunner.setPermission
with a promise based system which can be used to prevent races

BUG=(multiple)

Review URL: https://codereview.chromium.org/1305043004

git-svn-id: svn://svn.chromium.org/blink/trunk@201057 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 0ee6adf9
// This file provides a PermissionsHelper object which can be used by
// LayoutTests using testRunner to handle permissions. The methods in the object
// return promises so can be used to write idiomatic, race-free code.
//
// The current available methods are:
// - setPermission: given a permission name (known by testRunner) and a state,
// it will set the permission to the specified state and resolve the promise
// when done.
// Example:
// PermissionsHelper.setPermission('geolocation', 'prompt').then(runTest);
"use strict";
var PermissionsHelper = (function() {
function nameToObject(permissionName) {
switch (permissionName) {
case "midi":
return {name: "midi"};
case "midi-sysex":
return {name: "midi", sysex: true};
case "push-messaging":
return {name: "push", userVisibleOnly: true};
case "notifications":
return {name: "notifications"};
case "geolocation":
return {name: "geolocation"};
default:
throw "Invalid permission name provided";
}
}
return {
setPermission: function(name, state) {
return new Promise(function(resolver, reject) {
navigator.permissions.query(nameToObject(name)).then(function(result) {
if (result.state == state) {
resolver()
return;
}
result.onchange = function() {
result.onchange = null;
resolver();
};
testRunner.setPermission(name, state, location.origin, location.origin);
});
});
}
}
})();
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment