f684f843fc98467aee1b65e0c1f83f841eee6623
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / copy_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 {string}
20  * @const
21  */
22 var TESTING_NEW_FILE_NAME = 'puppy.txt';
23
24 /**
25  * Copies an entry within the same file system.
26  *
27  * @param {CopyEntryRequestedOptions} options Options.
28  * @param {function(Object)} onSuccess Success callback
29  * @param {function(string)} onError Error callback with an error code.
30  */
31 function onCopyEntryRequested(options, onSuccess, onError) {
32   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
33     onError('SECURITY');  // enum ProviderError.
34     return;
35   }
36
37   if (options.sourcePath == '/') {
38     onError('INVALID_OPERATION');
39     return;
40   }
41
42   if (!(options.sourcePath in test_util.defaultMetadata)) {
43     onError('NOT_FOUND');
44     return;
45   }
46
47   if (options.targetPath in test_util.defaultMetadata) {
48     onError('EXISTS');
49     return;
50   }
51
52   // Copy the metadata, but change the 'name' field.
53   var newMetadata =
54       JSON.parse(JSON.stringify(test_util.defaultMetadata[options.sourcePath]));
55   newMetadata.name = options.targetPath.split('/').pop();
56   test_util.defaultMetadata[options.targetPath] = newMetadata;
57
58   onSuccess();  // enum ProviderError.
59 }
60
61 /**
62  * Sets up the tests. Called once per all test cases. In case of a failure,
63  * the callback is not called.
64  *
65  * @param {function()} callback Success callback.
66  */
67 function setUp(callback) {
68   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
69       test_util.onGetMetadataRequestedDefault);
70
71   test_util.defaultMetadata['/' + TESTING_FILE.name] = TESTING_FILE;
72
73   chrome.fileSystemProvider.onCopyEntryRequested.addListener(
74       onCopyEntryRequested);
75
76   test_util.mountFileSystem(callback);
77 }
78
79 /**
80  * Runs all of the test cases, one by one.
81  */
82 function runTests() {
83   chrome.test.runTests([
84     // Copy an existing file to a non-existing destination. Should succeed.
85     function copyEntrySuccess() {
86       test_util.fileSystem.root.getFile(
87           TESTING_FILE.name, {create: false},
88           chrome.test.callbackPass(function(sourceEntry) {
89             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
90             chrome.test.assertFalse(sourceEntry.isDirectory);
91             sourceEntry.copyTo(
92                 test_util.fileSystem.root,
93                 TESTING_NEW_FILE_NAME,
94                 chrome.test.callbackPass(function(targetEntry) {
95                   chrome.test.assertEq(TESTING_NEW_FILE_NAME, targetEntry.name);
96                   chrome.test.assertFalse(targetEntry.isDirectory);
97                 }), function(error) {
98                   chrome.test.fail(error.name);
99                 });
100           }), function(error) {
101             chrome.test.fail(error.name);
102           });
103     },
104
105     // Copy an existing file to a location which already holds a file.
106     // Should fail.
107     function copyEntryExistsError() {
108       test_util.fileSystem.root.getFile(
109           TESTING_FILE.name, {create: false},
110           chrome.test.callbackPass(function(sourceEntry) {
111             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
112             chrome.test.assertFalse(sourceEntry.isDirectory);
113             sourceEntry.copyTo(
114                 test_util.fileSystem.root,
115                 TESTING_NEW_FILE_NAME,
116                 function(targetEntry) {
117                   chrome.test.fail('Succeeded, but should fail.');
118                 }, chrome.test.callbackPass(function(error) {
119                   chrome.test.assertEq('InvalidModificationError', error.name);
120                 }));
121           }), function(error) {
122             chrome.test.fail(error.name);
123           });
124     }
125   ]);
126 }
127
128 // Setup and run all of the test cases.
129 setUp(runTests);