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.
6 * Reports the promise state to the test system.
8 * @param {Promise} promise Promise to be resolved with the test result.
10 function reportPromise(promise) {
12 chrome.test.callbackPass(),
14 chrome.test.fail(error.stack || error);
19 * Checks if the promise is rejected or not.
20 * If the promise is fulfilled, it causes test failure.
22 * @param {Promise} promise Promise expected to reject.
23 * @return {Promise} Promise that is fulfilled when the given promise is
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);
32 /* Recover rejection. */
38 * Calls webkitResolveLocalFileSystemURL, and returns the result promise.
40 * @param {string} url URL.
41 * @return {Promise} Promise to be fulfilled/rejected with the result.
43 function resolveLocalFileSystemURL(url) {
44 return new Promise(webkitResolveLocalFileSystemURL.bind(null, url));
48 * Sends XHR, and returns the result promise.
50 * @param {string} url URL.
51 * @return {Promise} Promise to be fulfilled/rejected with the result.
53 function sendXHR(url) {
54 return new Promise(function(fulfill, reject) {
55 var xhr = new XMLHttpRequest();
59 // XHR.send sends DOMException for security error.
62 reject('SECURITY_ERROR');
64 xhr.onerror = function() {
65 reject('ERROR_STATUS: ' + xhr.status);
71 * Requests the drive file system.
73 * @return {Promise} Promise to be fulfilled with the drive file system.
75 function requestDriveFileSystem() {
76 return new Promise(function(fulfill) {
77 chrome.fileManagerPrivate.requestFileSystem(
79 function(fileSystem) {
80 chrome.test.assertTrue(!!fileSystem);
87 * Ensures resolveLocalFileSystemURL does not resolve filesystem:file/// URL.
89 * @return {Promise} Promise to be resolved with the test result.
91 function testResolveFileSystemURL() {
92 reportPromise(requestDriveFileSystem().then(function(fileSystem) {
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'))
104 * Ensures filesystem:file/// URL does not respond to XHR.
106 * @return {Promise} Promise to be resolved with the test result.
108 function testSendXHRToFileSystemURL() {
109 // Thus we grant the permission to the extension in
110 // chrome.fileManagerPrivate.requestFileSystem, we need to call the method
112 reportPromise(requestDriveFileSystem().then(function(fileSystem) {
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);
127 chrome.test.runTests([
128 testResolveFileSystemURL,
129 testSendXHRToFileSystemURL