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 "ambient-light-sensor": |
| 29 return {name: "ambient-light-sensor"}; |
| 30 case "accelerometer": |
| 31 return {name: "accelerometer"}; |
| 32 case "gyroscope": |
| 33 return {name: "gyroscope"}; |
| 34 case "magnetometer": |
| 35 return {name: "magnetometer"}; |
| 36 case "orientation-sensor": |
| 37 return {name: "orientation-sensor"}; |
28 default: | 38 default: |
29 throw "Invalid permission name provided"; | 39 throw "Invalid permission name provided"; |
30 } | 40 } |
31 } | 41 } |
32 | 42 |
33 return { | 43 return { |
34 setPermission: function(name, state) { | 44 setPermission: function(name, state) { |
35 return new Promise(function(resolver, reject) { | 45 return new Promise(function(resolver, reject) { |
36 navigator.permissions.query(nameToObject(name)).then(function(result) { | 46 navigator.permissions.query(nameToObject(name)).then(function(result) { |
37 if (result.state == state) { | 47 if (result.state == state) { |
38 resolver() | 48 resolver() |
39 return; | 49 return; |
40 } | 50 } |
41 | 51 |
42 result.onchange = function() { | 52 result.onchange = function() { |
43 result.onchange = null; | 53 result.onchange = null; |
44 resolver(); | 54 resolver(); |
45 }; | 55 }; |
46 | 56 |
47 testRunner.setPermission(name, state, location.origin, location.orig
in); | 57 testRunner.setPermission(name, state, location.origin, location.orig
in); |
48 }); | 58 }); |
49 }); | 59 }); |
50 } | 60 } |
51 } | 61 } |
52 })(); | 62 })(); |
OLD | NEW |