Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / move_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_FILE = Object.freeze({
12   isDirectory: false,
13   name: 'kitty',
14   size: 1024,
15   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_ANOTHER_FILE = Object.freeze({
23   isDirectory: false,
24   name: 'bunny',
25   size: 2048,
26   modificationTime: new Date(2014, 4, 28, 9, 38, 14)
27 });
28
29 /**
30  * @type {string}
31  * @const
32  */
33 var TESTING_NEW_FILE_NAME = 'puppy.txt';
34
35 /**
36  * Moves an entry within the same file system.
37  *
38  * @param {MoveEntryRequestedOptions} options Options.
39  * @param {function(Object)} onSuccess Success callback
40  * @param {function(string)} onError Error callback with an error code.
41  */
42 function onMoveEntryRequested(options, onSuccess, onError) {
43   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
44     onError('SECURITY');  // enum ProviderError.
45     return;
46   }
47
48   if (options.sourcePath == '/') {
49     onError('INVALID_OPERATION');
50     return;
51   }
52
53   if (!(options.sourcePath in test_util.defaultMetadata)) {
54     onError('NOT_FOUND');
55     return;
56   }
57
58   if (options.targetPath in test_util.defaultMetadata) {
59     onError('EXISTS');
60     return;
61   }
62
63   // Move the metadata with changing the 'name' field.
64   var newMetadata =
65       JSON.parse(JSON.stringify(test_util.defaultMetadata[options.sourcePath]));
66   newMetadata.name = options.targetPath.split('/').pop();
67   test_util.defaultMetadata[options.targetPath] = newMetadata;
68
69   // Remove the source file.
70   delete test_util.defaultMetadata[options.sourcePath];
71
72   onSuccess();  // enum ProviderError.
73 }
74
75 /**
76  * Sets up the tests. Called once per all test cases. In case of a failure,
77  * the callback is not called.
78  *
79  * @param {function()} callback Success callback.
80  */
81 function setUp(callback) {
82   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
83       test_util.onGetMetadataRequestedDefault);
84
85   test_util.defaultMetadata['/' + TESTING_FILE.name] = TESTING_FILE;
86   test_util.defaultMetadata['/' + TESTING_ANOTHER_FILE.name] =
87       TESTING_ANOTHER_FILE;
88
89   chrome.fileSystemProvider.onMoveEntryRequested.addListener(
90       onMoveEntryRequested);
91
92   test_util.mountFileSystem(callback);
93 }
94
95 /**
96  * Runs all of the test cases, one by one.
97  */
98 function runTests() {
99   chrome.test.runTests([
100     // Move an existing file to a non-existing destination. Should succeed.
101     function moveEntrySuccess() {
102       var onSuccess = chrome.test.callbackPass();
103       test_util.fileSystem.root.getFile(
104           TESTING_FILE.name, {create: false},
105           function(sourceEntry) {
106             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
107             chrome.test.assertFalse(sourceEntry.isDirectory);
108             sourceEntry.moveTo(
109                 test_util.fileSystem.root,
110                 TESTING_NEW_FILE_NAME,
111                 function(targetEntry) {
112                   chrome.test.assertEq(TESTING_NEW_FILE_NAME, targetEntry.name);
113                   chrome.test.assertFalse(targetEntry.isDirectory);
114                   // The source file should be deleted.
115                   test_util.fileSystem.root.getFile(
116                       TESTING_FILE.name, {create: false},
117                       function(newSourceEntry) {
118                         chrome.test.fail('Source file not deleted.');
119                       },
120                       function(error) {
121                         chrome.test.assertEq('NotFoundError', error.name);
122                         onSuccess();
123                       });
124                 }, function(error) {
125                   chrome.test.fail(error.name);
126                 });
127           }, function(error) {
128             chrome.test.fail(error.name);
129           });
130     },
131
132     // Move an existing file to a location which already holds a file.
133     // Should fail.
134     function moveEntryExistsError() {
135       var onSuccess = chrome.test.callbackPass();
136       test_util.fileSystem.root.getFile(
137           TESTING_ANOTHER_FILE.name, {create: false},
138           function(sourceEntry) {
139             chrome.test.assertEq(TESTING_ANOTHER_FILE.name, sourceEntry.name);
140             chrome.test.assertFalse(sourceEntry.isDirectory);
141             sourceEntry.moveTo(
142                 test_util.fileSystem.root,
143                 TESTING_NEW_FILE_NAME,
144                 function(targetEntry) {
145                   chrome.test.fail('Succeeded, but should fail.');
146                 }, function(error) {
147                   chrome.test.assertEq('InvalidModificationError', error.name);
148                   onSuccess();
149                 });
150           }, function(error) {
151             chrome.test.fail(error.name);
152           });
153     }
154   ]);
155 }
156
157 // Setup and run all of the test cases.
158 setUp(runTests);