Upstream version 10.39.225.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           function() { fulfill(); },
31           function(error) { reject(error); });
32     }),
33     new Promise(function(fulfill, reject) {
34       chrome.fileSystemProvider.mount(
35           {fileSystemId: SECOND_FILE_SYSTEM_ID, displayName: 'ice-cream.zip'},
36           function() { fulfill(); },
37           function(error) { reject(error); });
38     })
39   ]).then(callback).catch(function(error) {
40     chrome.test.fail(error.name);
41   });
42 }
43
44 /**
45  * Runs all of the test cases, one by one.
46  */
47 function runTests() {
48   chrome.test.runTests([
49     // Tests the fileSystemProvider.unmount(). Verifies if the unmount event
50     // is emitted by VolumeManager.
51     function unmount() {
52       var onTestSuccess = chrome.test.callbackPass();
53
54       var onMountCompleted = function(event) {
55         chrome.test.assertEq('unmount', event.eventType);
56         chrome.test.assertEq('success', event.status);
57         chrome.test.assertEq(
58             chrome.runtime.id, event.volumeMetadata.extensionId);
59         chrome.test.assertEq(
60             FIRST_FILE_SYSTEM_ID, event.volumeMetadata.fileSystemId);
61         chrome.fileManagerPrivate.onMountCompleted.removeListener(
62             onMountCompleted);
63         onTestSuccess();
64       };
65
66       chrome.fileManagerPrivate.onMountCompleted.addListener(
67           onMountCompleted);
68       chrome.fileSystemProvider.unmount(
69           {fileSystemId: FIRST_FILE_SYSTEM_ID},
70           function() {
71             // Wait for the unmount event.
72           },
73           function(error) {
74             chrome.test.fail(error.name);
75           });
76     },
77
78     // Tests the fileSystemProvider.unmount() with a wrong id. Verifies that
79     // it fails with a correct error code.
80     function unmountWrongId() {
81       var onTestSuccess = chrome.test.callbackPass();
82       chrome.fileSystemProvider.unmount(
83           {fileSystemId: 'wrong-fs-id'},
84           function() {
85             chrome.test.fail();
86           },
87           function(error) {
88             chrome.test.assertEq('SecurityError', error.name);
89             onTestSuccess();
90           });
91     },
92
93     // Tests if fileManagerPrivate.removeMount() for provided file systems emits
94     // the onMountRequested() event with correct arguments.
95     function requestUnmountSuccess() {
96       var onTestSuccess = chrome.test.callbackPass();
97
98       var onUnmountRequested = function(options, onSuccess, onError) {
99         chrome.test.assertEq(SECOND_FILE_SYSTEM_ID, options.fileSystemId);
100         onSuccess();
101         // Not calling fileSystemProvider.unmount(), so the onMountCompleted
102         // event will not be raised.
103         chrome.fileSystemProvider.onUnmountRequested.removeListener(
104             onUnmountRequested);
105         onTestSuccess();
106       };
107
108       chrome.fileSystemProvider.onUnmountRequested.addListener(
109           onUnmountRequested);
110
111       test_util.getVolumeInfo(SECOND_FILE_SYSTEM_ID, function(volumeInfo) {
112         chrome.test.assertTrue(!!volumeInfo);
113         chrome.fileManagerPrivate.removeMount(volumeInfo.volumeId);
114       });
115     },
116
117     // End to end test with a failure. Invokes fileSystemProvider.removeMount()
118     // on a provided file system, and verifies (1) if the onMountRequested()
119     // event is called with correct aguments, and (2) if calling onError(),
120     // results in an unmount event fired from the VolumeManager instance.
121     function requestUnmountError() {
122       var onTestSuccess = chrome.test.callbackPass();
123       var unmountRequested = false;
124
125       var onUnmountRequested = function(options, onSuccess, onError) {
126         chrome.test.assertEq(false, unmountRequested);
127         chrome.test.assertEq(SECOND_FILE_SYSTEM_ID, options.fileSystemId);
128         onError('IN_USE');  // enum ProviderError.
129         unmountRequested = true;
130         chrome.fileSystemProvider.onUnmountRequested.removeListener(
131             onUnmountRequested);
132       };
133
134       var onMountCompleted = function(event) {
135         chrome.test.assertEq('unmount', event.eventType);
136         chrome.test.assertEq('error_unknown', event.status);
137         chrome.test.assertEq(
138             chrome.runtime.id, event.volumeMetadata.extensionId);
139         chrome.test.assertEq(
140             SECOND_FILE_SYSTEM_ID, event.volumeMetadata.fileSystemId);
141         chrome.test.assertTrue(unmountRequested);
142
143         // Remove the handlers and mark the test as succeeded.
144         chrome.fileManagerPrivate.removeMount(SECOND_FILE_SYSTEM_ID);
145         chrome.fileManagerPrivate.onMountCompleted.removeListener(
146             onMountCompleted);
147         onTestSuccess();
148       };
149
150       chrome.fileSystemProvider.onUnmountRequested.addListener(
151           onUnmountRequested);
152       chrome.fileManagerPrivate.onMountCompleted.addListener(onMountCompleted);
153
154       test_util.getVolumeInfo(SECOND_FILE_SYSTEM_ID, function(volumeInfo) {
155         chrome.test.assertTrue(!!volumeInfo);
156         chrome.fileManagerPrivate.removeMount(volumeInfo.volumeId);
157       });
158     }
159   ]);
160 }
161
162 // Setup and run all of the test cases.
163 setUp(runTests);