Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / delete_entry / 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 {Object}
9  * @const
10  */
11 var TESTING_A_DIRECTORY = Object.freeze({
12   isDirectory: true,
13   name: 'a',
14   size: 0,
15   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_B_DIRECTORY = Object.freeze({
23   isDirectory: true,
24   name: 'b',
25   size: 0,
26   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
27 });
28
29 /**
30  * @type {Object}
31  * @const
32  */
33 var TESTING_C_FILE = Object.freeze({
34   isDirectory: false,
35   name: 'c',
36   size: 0,
37   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
38 });
39
40 /**
41  * Deletes an entry.
42  *
43  * @param {DeleteEntryRequestedOptions} options Options.
44  * @param {function(Object)} onSuccess Success callback
45  * @param {function(string)} onError Error callback with an error code.
46  */
47 function onDeleteEntryRequested(options, onSuccess, onError) {
48   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
49     onError('SECURITY');  // enum ProviderError.
50     return;
51   }
52
53   if (options.entryPath == '/') {
54     onError('INVALID_OPERATION');
55     return;
56   }
57
58   if (options.entryPath == '/' + TESTING_A_DIRECTORY.name) {
59     if (options.recursive)
60       onSuccess();
61     else
62       onError('INVALID_OPERATION');
63     return;
64   }
65
66   if (options.entryPath == '/' + TESTING_C_FILE.name ||
67       options.entryPath == '/' + TESTING_A_DIRECTORY.name + '/' +
68       TESTING_B_DIRECTORY.name) {
69     onSuccess();
70     return;
71   }
72
73   onError('NOT_FOUND');  // enum ProviderError.
74 }
75
76 /**
77  * Sets up the tests. Called once per all test cases. In case of a failure,
78  * the callback is not called.
79  *
80  * @param {function()} callback Success callback.
81  */
82 function setUp(callback) {
83   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
84       test_util.onGetMetadataRequestedDefault);
85
86   test_util.defaultMetadata['/' + TESTING_A_DIRECTORY.name] =
87       TESTING_A_DIRECTORY;
88   test_util.defaultMetadata['/' + TESTING_A_DIRECTORY.name + '/' +
89       TESTING_B_DIRECTORY.name] = TESTING_B_DIRECTORY;
90   test_util.defaultMetadata['/' + TESTING_C_FILE.name] =
91       TESTING_C_FILE;
92
93   chrome.fileSystemProvider.onDeleteEntryRequested.addListener(
94       onDeleteEntryRequested);
95
96   test_util.mountFileSystem(callback);
97 }
98
99 /**
100  * Runs all of the test cases, one by one.
101  */
102 function runTests() {
103   chrome.test.runTests([
104     // Delete a file. Should succeed.
105     function deleteDirectorySuccessSimple() {
106       test_util.fileSystem.root.getFile(
107           TESTING_C_FILE.name, {create: false},
108           chrome.test.callbackPass(function(entry) {
109             chrome.test.assertEq(TESTING_C_FILE.name, entry.name);
110             chrome.test.assertFalse(entry.isDirectory);
111             entry.remove(chrome.test.callbackPass(), function(error) {
112               chrome.test.fail(error.name);
113             });
114           }), function(error) {
115             chrome.test.fail(error.name);
116           });
117     },
118     // Delete a directory which has contents, non-recursively. Should fail.
119     function deleteDirectoryErrorNotEmpty() {
120       test_util.fileSystem.root.getDirectory(
121           TESTING_A_DIRECTORY.name, {create: false},
122           chrome.test.callbackPass(function(entry) {
123             chrome.test.assertEq(TESTING_A_DIRECTORY.name, entry.name);
124             chrome.test.assertTrue(entry.isDirectory);
125             entry.remove(function() {
126               chrome.test.fail('Unexpectedly succeded to remove a directory.');
127             }, chrome.test.callbackPass);
128           }), function(error) {
129             chrome.test.fail(error.name);
130           });
131     },
132     // Delete a directory which has contents, recursively. Should succeed.
133     function deleteDirectoryRecursively() {
134       test_util.fileSystem.root.getDirectory(
135           TESTING_A_DIRECTORY.name, {create: false},
136           chrome.test.callbackPass(function(entry) {
137             chrome.test.assertEq(TESTING_A_DIRECTORY.name, entry.name);
138             chrome.test.assertTrue(entry.isDirectory);
139             entry.removeRecursively(
140                 chrome.test.callbackPass(),
141                 function(error) {
142                   chrome.test.fail(error);
143                 });
144           }), function(error) {
145             chrome.test.fail(error.name);
146           });
147     }
148   ]);
149 }
150
151 // Setup and run all of the test cases.
152 setUp(runTests);