OLD | NEW |
| (Empty) |
1 // This file provides a PermissionsHelper object which can be used by | |
2 // LayoutTests using testRunner to handle permissions. The methods in the object | |
3 // return promises so can be used to write idiomatic, race-free code. | |
4 // | |
5 // The current available methods are: | |
6 // - setPermission: given a permission name (known by testRunner) and a state, | |
7 // it will set the permission to the specified state and resolve the promise | |
8 // when done. | |
9 // Example: | |
10 // PermissionsHelper.setPermission('geolocation', 'prompt').then(runTest); | |
11 "use strict"; | |
12 | |
13 var PermissionsHelper = (function() { | |
14 function nameToObject(permissionName) { | |
15 switch (permissionName) { | |
16 case "midi": | |
17 return {name: "midi"}; | |
18 case "midi-sysex": | |
19 return {name: "midi", sysex: true}; | |
20 case "push-messaging": | |
21 return {name: "push", userVisibleOnly: true}; | |
22 case "notifications": | |
23 return {name: "notifications"}; | |
24 case "geolocation": | |
25 return {name: "geolocation"}; | |
26 default: | |
27 throw "Invalid permission name provided"; | |
28 } | |
29 } | |
30 | |
31 return { | |
32 setPermission: function(name, state) { | |
33 return new Promise(function(resolver, reject) { | |
34 navigator.permissions.query(nameToObject(name)).then(function(result) { | |
35 if (result.state == state) { | |
36 resolver() | |
37 return; | |
38 } | |
39 | |
40 result.onchange = function() { | |
41 result.onchange = null; | |
42 resolver(); | |
43 }; | |
44 | |
45 testRunner.setPermission(name, state, location.origin, location.orig
in); | |
46 }); | |
47 }); | |
48 } | |
49 } | |
50 })(); | |
OLD | NEW |