Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / unmount / test.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 'use strict';
6
7 /**
8  * @type {string}
9  * @const
10  */
11 var FIRST_FILE_SYSTEM_ID = 'vanilla';
12
13 /**
14  * @type {string}
15  * @const
16  */
17 var SECOND_FILE_SYSTEM_ID = 'ice-cream';
18
19 /**
20  * Sets up the tests. Called once per all test cases. In case of a failure,
21  * the callback is not called.
22  *
23  * @param {function()} callback Success callback.
24  */
25 function setUp(callback) {
26   Promise.race([
27     new Promise(function(fulfill, reject) {
28       chrome.fileSystemProvider.mount(
29           {fileSystemId: FIRST_FILE_SYSTEM_ID, displayName: 'vanilla.zip'},
30           chrome.test.callbackPass(fulfill));
31     }),
32     new Promise(function(fulfill, reject) {
33       chrome.fileSystemProvider.mount(
34           {fileSystemId: SECOND_FILE_SYSTEM_ID, displayName: 'ice-cream.zip'},
35           chrome.test.callbackPass(fulfill));
36     })
37   ]).then(callback).catch(function(error) {
38     chrome.test.fail(error.stack || error);
39   });
40 }
41
42 /**
43  * Runs all of the test cases, one by one.
44  */
45 function runTests() {
46   chrome.test.runTests([
47     // Tests the fileSystemProvider.unmount(). Verifies if the unmount event
48     // is emitted by VolumeManager.
49     function unmount() {
50       var onMountCompleted = function(event) {
51         chrome.test.assertEq('unmount', event.eventType);
52         chrome.test.assertEq('success', event.status);
53         chrome.test.assertEq(
54             chrome.runtime.id, event.volumeMetadata.extensionId);
55         chrome.test.assertEq(
56             FIRST_FILE_SYSTEM_ID, event.volumeMetadata.fileSystemId);
57         chrome.fileManagerPrivate.onMountCompleted.removeListener(
58             onMountCompleted);
59       };
60
61       chrome.fileManagerPrivate.onMountCompleted.addListener(
62           onMountCompleted);
63       chrome.fileSystemProvider.unmount(
64           {fileSystemId: FIRST_FILE_SYSTEM_ID},
65           chrome.test.callbackPass());
66     },
67
68     // Tests the fileSystemProvider.unmount() with a wrong id. Verifies that
69     // it fails with a correct error code.
70     function unmountWrongId() {
71       chrome.fileSystemProvider.unmount(
72           {fileSystemId: 'wrong-fs-id'},
73           chrome.test.callbackFail('SECURITY'));
74     },
75
76     // Tests if fileManagerPrivate.removeMount() for provided file systems emits
77     // the onMountRequested() event with correct arguments.
78     function requestUnmountSuccess() {
79       var onUnmountRequested = chrome.test.callbackPass(
80           function(options, onSuccess, onError) {
81         chrome.test.assertEq(SECOND_FILE_SYSTEM_ID, options.fileSystemId);
82         // Not calling fileSystemProvider.unmount(), so the onMountCompleted
83         // event will not be raised.
84         chrome.fileSystemProvider.onUnmountRequested.removeListener(
85             onUnmountRequested);
86         onSuccess();
87       });
88
89       chrome.fileSystemProvider.onUnmountRequested.addListener(
90           onUnmountRequested);
91
92       test_util.getVolumeInfo(SECOND_FILE_SYSTEM_ID, function(volumeInfo) {
93         chrome.test.assertTrue(!!volumeInfo);
94         chrome.fileManagerPrivate.removeMount(volumeInfo.volumeId);
95       });
96     },
97
98     // End to end test with a failure. Invokes fileSystemProvider.removeMount()
99     // on a provided file system, and verifies (1) if the onMountRequested()
100     // event is called with correct aguments, and (2) if calling onError(),
101     // results in an unmount event fired from the VolumeManager instance.
102     function requestUnmountError() {
103       var unmountRequested = false;
104
105       var onUnmountRequested = function(options, onSuccess, onError) {
106         chrome.test.assertEq(false, unmountRequested);
107         chrome.test.assertEq(SECOND_FILE_SYSTEM_ID, options.fileSystemId);
108         onError('IN_USE');  // enum ProviderError.
109         unmountRequested = true;
110         chrome.fileSystemProvider.onUnmountRequested.removeListener(
111             onUnmountRequested);
112       };
113
114       var onMountCompleted = chrome.test.callbackPass(function(event) {
115         chrome.test.assertEq('unmount', event.eventType);
116         chrome.test.assertEq('error_unknown', event.status);
117         chrome.test.assertEq(
118             chrome.runtime.id, event.volumeMetadata.extensionId);
119         chrome.test.assertEq(
120             SECOND_FILE_SYSTEM_ID, event.volumeMetadata.fileSystemId);
121         chrome.test.assertTrue(unmountRequested);
122
123         // Remove the handlers and mark the test as succeeded.
124         chrome.fileManagerPrivate.removeMount(SECOND_FILE_SYSTEM_ID);
125         chrome.fileManagerPrivate.onMountCompleted.removeListener(
126             onMountCompleted);
127       });
128
129       chrome.fileSystemProvider.onUnmountRequested.addListener(
130           onUnmountRequested);
131       chrome.fileManagerPrivate.onMountCompleted.addListener(onMountCompleted);
132
133       test_util.getVolumeInfo(SECOND_FILE_SYSTEM_ID, function(volumeInfo) {
134         chrome.test.assertTrue(!!volumeInfo);
135         chrome.fileManagerPrivate.removeMount(volumeInfo.volumeId);
136       });
137     }
138   ]);
139 }
140
141 // Setup and run all of the test cases.
142 setUp(runTests);