Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / permissions / chromium / resources / test-request.js
blob95f452065a5a7927c0959e07d799e29a0a722b0f
1 if (self.importScripts) {
2     importScripts('../../resources/helpers.js');
3     importScripts('testrunner-helpers.js');
5     if (get_current_scope() == 'ServiceWorker')
6         importScripts('../../../serviceworker/resources/worker-testharness.js');
7     else
8         importScripts('../../../resources/testharness.js');
11 var tests = [
13     test: async_test('Test PermissionDescription WebIDL rules in ' + get_current_scope() + ' scope.'),
14     fn: function(callback) {
15         // Requesting a random permission name should fail.
16         navigator.permissions.request({name:'foobar'}).then(function(result) {
17             throw 'requesting a random permission should fail';
18         }, function(error) {
19             assert_equals(error.name, 'TypeError');
21             // Querying a permission without a name should fail.
22             return navigator.permissions.request({});
23         }).then(function(result) {
24             throw 'requesting a permission without a name should fail';
25         }, function(error) {
26             assert_equals(error.name, 'TypeError');
27             callback();
28         }).catch(function(error) {
29             assert_unreached(error);
30             callback();
31         });
32     }
35     // request() is expected to show a UI then return the new permission status.
36     // In layout tests no UI is shown so it boils down to returning the permission
37     // status. The tests only check that behaviour given that trying to simulate
38     // user decision would simply test the test infrastructure.
39     test: async_test('Test basic request behaviour in ' + get_current_scope() + ' scope.'),
40     fn: function(callback) {
41       navigator.permissions.request({name:'geolocation'}).then(function(result) {
42           assert_true(result instanceof PermissionStatus);
43           assert_equals(result.state, 'denied');
45           result.onchange = function() {
46               assert_equals(result.state, 'granted');
48               navigator.permissions.request({name:'geolocation'}).then(function() {
49                   assert_true(result instanceof PermissionStatus);
50                   assert_equals(result.state, 'granted');
51                   callback();
52               }).catch(function() {
53                   assert_unreached('requesting geolocation permission should not fail.')
54                   callback();
55               });
56           };
58           setPermission('geolocation', 'granted', location.origin, location.origin)
59       }).catch(function() {
60           assert_unreached('requesting geolocation permission should not fail.')
61           callback();
62       });
63     }
64 }];
66 function runTest(i) {
67   tests[i].test.step(function() {
68       tests[i].fn(function() {
69           tests[i].test.done();
70           if (i + 1 < tests.length) {
71               runTest(i + 1);
72           } else {
73               done();
74           }
75       });
76   });
78 runTest(0);