Upstream version 9.38.198.0
[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       var onSuccess = chrome.test.callbackPass();
87       test_util.fileSystem.root.getFile(
88           TESTING_FILE.name, {create: false},
89           function(sourceEntry) {
90             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
91             chrome.test.assertFalse(sourceEntry.isDirectory);
92             sourceEntry.copyTo(
93                 test_util.fileSystem.root,
94                 TESTING_NEW_FILE_NAME,
95                 function(targetEntry) {
96                   chrome.test.assertEq(TESTING_NEW_FILE_NAME, targetEntry.name);
97                   chrome.test.assertFalse(targetEntry.isDirectory);
98                   onSuccess();
99                 }, function(error) {
100                   chrome.test.fail(error.name);
101                 });
102           }, function(error) {
103             chrome.test.fail(error.name);
104           });
105     },
106
107     // Copy an existing file to a location which already holds a file.
108     // Should fail.
109     function copyEntryExistsError() {
110       var onSuccess = chrome.test.callbackPass();
111       test_util.fileSystem.root.getFile(
112           TESTING_FILE.name, {create: false},
113           function(sourceEntry) {
114             chrome.test.assertEq(TESTING_FILE.name, sourceEntry.name);
115             chrome.test.assertFalse(sourceEntry.isDirectory);
116             sourceEntry.copyTo(
117                 test_util.fileSystem.root,
118                 TESTING_NEW_FILE_NAME,
119                 function(targetEntry) {
120                   chrome.test.fail('Succeeded, but should fail.');
121                 }, function(error) {
122                   chrome.test.assertEq('InvalidModificationError', error.name);
123                   onSuccess();
124                 });
125           }, function(error) {
126             chrome.test.fail(error.name);
127           });
128     }
129   ]);
130 }
131
132 // Setup and run all of the test cases.
133 setUp(runTests);