Update To 11.40.268.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();
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       test_util.fileSystem.root.getFile(
103           TESTING_FILE.name, {create: false},
104           chrome.test.callbackPass(function(sourceEntry) {
105             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
106             chrome.test.assertFalse(sourceEntry.isDirectory);
107             sourceEntry.moveTo(
108                 test_util.fileSystem.root,
109                 TESTING_NEW_FILE_NAME,
110                 chrome.test.callbackPass(function(targetEntry) {
111                   chrome.test.assertEq(TESTING_NEW_FILE_NAME, targetEntry.name);
112                   chrome.test.assertFalse(targetEntry.isDirectory);
113                   // The source file should be deleted.
114                   test_util.fileSystem.root.getFile(
115                       TESTING_FILE.name, {create: false},
116                       function(newSourceEntry) {
117                         chrome.test.fail('Source file not deleted.');
118                       },
119                       chrome.test.callbackPass(function(error) {
120                         chrome.test.assertEq('NotFoundError', error.name);
121                       }))
122                 }), function(error) {
123                   chrome.test.fail(error.name);
124                 });
125           }), function(error) {
126             chrome.test.fail(error.name);
127           });
128     },
129
130     // Move an existing file to a location which already holds a file.
131     // Should fail.
132     function moveEntryExistsError() {
133       test_util.fileSystem.root.getFile(
134           TESTING_ANOTHER_FILE.name, {create: false},
135           chrome.test.callbackPass(function(sourceEntry) {
136             chrome.test.assertEq(TESTING_ANOTHER_FILE.name, sourceEntry.name);
137             chrome.test.assertFalse(sourceEntry.isDirectory);
138             sourceEntry.moveTo(
139                 test_util.fileSystem.root,
140                 TESTING_NEW_FILE_NAME,
141                 function(targetEntry) {
142                   chrome.test.fail('Succeeded, but should fail.');
143                 }, chrome.test.callbackPass(function(error) {
144                   chrome.test.assertEq('InvalidModificationError', error.name);
145                 }));
146           }), function(error) {
147             chrome.test.fail(error.name);
148           });
149     }
150   ]);
151 }
152
153 // Setup and run all of the test cases.
154 setUp(runTests);