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