Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_browser / filesystem_file_origin_url / 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 /**
6  * Reports the promise state to the test system.
7  *
8  * @param {Promise} promise Promise to be resolved with the test result.
9  */
10 function reportPromise(promise) {
11   promise.then(
12       chrome.test.callbackPass(),
13       function(error) {
14         chrome.test.fail(error.stack || error);
15       });
16 }
17
18 /**
19  * Checks if the promise is rejected or not.
20  * If the promise is fulfilled, it causes test failure.
21  *
22  * @param {Promise} promise Promise expected to reject.
23  * @return {Promise} Promise that is fulfilled when the given promise is
24  *     rejected.
25  */
26 function expectRejection(promise) {
27   var errorMessage = new Error('Promise is fulfilled unintentionally.').stack;
28   return promise.then(function() {
29     chrome.test.fail(errorMessage);
30     return Promise.reject(errorMessage);
31   }, function(arg) {
32     /* Recover rejection. */
33     return arg;
34   });
35 }
36
37 /**
38  * Calls webkitResolveLocalFileSystemURL, and returns the result promise.
39  *
40  * @param {string} url URL.
41  * @return {Promise} Promise to be fulfilled/rejected with the result.
42  */
43 function resolveLocalFileSystemURL(url) {
44   return new Promise(webkitResolveLocalFileSystemURL.bind(null, url));
45 }
46
47 /**
48  * Sends XHR, and returns the result promise.
49  *
50  * @param {string} url URL.
51  * @return {Promise} Promise to be fulfilled/rejected with the result.
52  */
53 function sendXHR(url) {
54   return new Promise(function(fulfill, reject) {
55     var xhr = new XMLHttpRequest();
56     xhr.open('GET', url);
57     xhr.onload = fulfill;
58     try {
59       // XHR.send sends DOMException for security error.
60       xhr.send();
61     } catch (exception) {
62       reject('SECURITY_ERROR');
63     }
64     xhr.onerror = function() {
65       reject('ERROR_STATUS: ' + xhr.status);
66     };
67   });
68 }
69
70 /**
71  * Requests the drive file system.
72  *
73  * @return {Promise} Promise to be fulfilled with the drive file system.
74  */
75 function requestDriveFileSystem() {
76   return new Promise(function(fulfill) {
77     chrome.fileManagerPrivate.requestFileSystem(
78         'drive:drive-user',
79         function(fileSystem) {
80           chrome.test.assertTrue(!!fileSystem);
81           fulfill(fileSystem);
82         });
83   });
84 }
85
86 /**
87  * Ensures resolveLocalFileSystemURL does not resolve filesystem:file/// URL.
88  *
89  * @return {Promise} Promise to be resolved with the test result.
90  */
91 function testResolveFileSystemURL() {
92   reportPromise(requestDriveFileSystem().then(function(fileSystem) {
93     return Promise.all([
94         resolveLocalFileSystemURL(
95             'filesystem:chrome-extension://pkplfbidichfdicaijlchgnapepdginl/' +
96             'external/drive/root/test_dir/test_file.xul'),
97         expectRejection(resolveLocalFileSystemURL(
98             'filesystem:file:///external/drive/root/test_dir/test_file.xul'))
99     ]);
100   }));
101 }
102
103 /**
104  * Ensures filesystem:file/// URL does not respond to XHR.
105  *
106  * @return {Promise} Promise to be resolved with the test result.
107  */
108 function testSendXHRToFileSystemURL() {
109   // Thus we grant the permission to the extension in
110   // chrome.fileManagerPrivate.requestFileSystem, we need to call the method
111   // before.
112   reportPromise(requestDriveFileSystem().then(function(fileSystem) {
113     return Promise.all([
114         sendXHR(
115             'filesystem:chrome-extension://pkplfbidichfdicaijlchgnapepdginl/' +
116             'external/drive/root/test_dir/test_file.xul'),
117         expectRejection(sendXHR(
118             'filesystem:file:///external/drive/root/test_dir/test_file.xul')).
119             then(function(error) {
120               chrome.test.assertEq('ERROR_STATUS: 0', error);
121             })
122     ]);
123   }));
124 }
125
126 // Run tests.
127 chrome.test.runTests([
128   testResolveFileSystemURL,
129   testSendXHRToFileSystemURL
130 ]);