Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / permissions / optional_retain_gesture / background.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var fail = chrome.test.callbackFail;
6
7 var GESTURE_ERROR = "This function must be called during a user gesture";
8
9 function busyLoop(milliseconds) {
10   var startTime = new Date().getTime();
11   while (new Date().getTime() - startTime < milliseconds) {}
12 }
13
14 chrome.test.getConfig(function(config) {
15   chrome.test.runTests([
16     function testPermissionsRetainGesture() {
17       chrome.test.runWithUserGesture(function() {
18         chrome.permissions.request(
19             {permissions: ['bookmarks']},
20             function(granted) {
21               chrome.test.assertTrue(granted);
22
23               // The user gesture is retained, so we can request again.
24               chrome.permissions.request(
25                   {permissions: ['bookmarks']},
26                   function(granted) {
27                     chrome.test.assertTrue(granted);
28
29                     // The user gesture is retained but is consumed outside,
30                     // so the following request will fail.
31                     chrome.permissions.request(
32                         {permissions: ['bookmarks']},
33                         fail(GESTURE_ERROR));
34                   }
35               );
36
37               // Consume the user gesture
38               window.open("", "", "");
39             }
40         );
41       });
42     },
43
44     function testPermissionsRetainGestureExpire() {
45       chrome.test.runWithUserGesture(function() {
46         chrome.permissions.request(
47             {permissions: ['bookmarks']},
48             function(granted) {
49               chrome.test.assertTrue(granted);
50
51               var request = function() {
52                 // The following request will fail if the user gesture is
53                 // expired.
54                 chrome.permissions.request(
55                     {permissions: ['bookmarks']},
56                     function(granted1) {
57                       if (chrome.runtime.lastError) {
58                         chrome.test.assertFalse(granted1);
59                         chrome.test.assertEq(chrome.runtime.lastError.message,
60                                              GESTURE_ERROR);
61                         chrome.test.succeed();
62                       } else {
63                         console.log("Retrying permissions.request in 3 " +
64                             "seconds");
65                         busyLoop(3000);
66                         request();
67                       }
68                     });
69               };
70
71               // Wait 2s since the user gesture timeout is 1s.
72               busyLoop(2000);
73               request();
74             }
75         );
76       });
77     }
78   ]);
79 });