Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / file_manager / unit_tests / mocks / mock_entry.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 /**
6  * Joins paths so that the two paths are connected by only 1 '/'.
7  * @param {string} a Path.
8  * @param {string} b Path.
9  * @return {string} Joined path.
10  */
11 function joinPath(a, b) {
12   return a.replace(/\/+$/, '') + '/' + b.replace(/^\/+/, '');
13 };
14
15 /**
16  * Test file system.
17  *
18  * @param {string} fileSystemId File system ID.
19  * @constructor
20  */
21 function TestFileSystem(fileSystemId) {
22   this.fileSystemId = fileSystemId;
23   this.entries = {};
24 };
25
26 TestFileSystem.prototype = {
27   get root() { return this.entries['/']; }
28 };
29
30 /**
31  * Base class of mock entries.
32  *
33  * @param {TestFileSystem} filesystem File system where the entry is localed.
34  * @param {string} fullpath Full path of the entry.
35  * @constructor
36  */
37 function MockEntry(filesystem, fullPath) {
38   this.filesystem = filesystem;
39   this.fullPath = fullPath;
40 }
41
42 MockEntry.prototype = {
43   /**
44    * @return {string} Name of the entry.
45    */
46   get name() {
47     return this.fullPath.replace(/^.*\//, '');
48   }
49 };
50
51 /**
52  * Returns fake URL.
53  *
54  * @return {string} Fake URL.
55  */
56 MockEntry.prototype.toURL = function() {
57   return 'filesystem:' + this.filesystem.fileSystemId + this.fullPath;
58 };
59
60 /**
61  * Obtains parent directory.
62  *
63  * @param {function(MockDirectoryEntry)} onSuccess Callback invoked with
64  *     the parent directory.
65  * @param {function(Object)} onError Callback invoked with an error
66  *     object.
67  */
68 MockEntry.prototype.getParent = function(
69     onSuccess, onError) {
70   var path = this.fullPath.replace(/\/[^\/]+$/, '') || '/';
71   if (this.filesystem.entries[path])
72     onSuccess(this.filesystem.entries[path]);
73   else
74     onError({name: util.FileError.NOT_FOUND_ERR});
75 };
76
77 /**
78  * Moves the entry to the directory.
79  *
80  * @param {MockDirectoryEntry} parent Destination directory.
81  * @param {string=} opt_newName New name.
82  * @param {function(MockDirectoryEntry)} onSuccess Callback invoked with the
83  *     moved entry.
84  * @param {function(Object)} onError Callback invoked with an error object.
85  */
86 MockEntry.prototype.moveTo = function(parent, opt_newName, onSuccess, onError) {
87   Promise.resolve().then(function() {
88     this.filesystem.entries[this.fullPath] = null;
89     return this.clone(joinPath(parent.fullPath, opt_newName || this.name));
90   }.bind(this)).then(onSuccess, onError);
91 };
92
93 /**
94  * Removes the entry.
95  *
96  * @param {function()} onSuccess Success callback.
97  * @param {function(Object)} onError Callback invoked with an error object.
98  */
99 MockEntry.prototype.remove = function(onSuccess, onError) {
100   Promise.resolve().then(function() {
101     this.filesystem.entries[this.fullPath] = null;
102   }.bind(this)).then(onSuccess, onError);
103 };
104
105 /**
106  * Clones the entry with the new fullpath.
107  *
108  * @param {string} fullpath New fullpath.
109  * @return {MockEntry} Cloned entry.
110  */
111 MockEntry.prototype.clone = function(fullpath) {
112   throw new Error('Not implemented.');
113 };
114
115 /**
116  * Mock class for FileEntry.
117  *
118  * @param {FileSystem} filesystem File system where the entry is localed.
119  * @param {string} fullPath Full path for the entry.
120  * @param {Object} metadata Metadata.
121  * @extends {MockEntry}
122  * @constructor
123  */
124 function MockFileEntry(filesystem, fullPath, metadata) {
125   MockEntry.call(this, filesystem, fullPath);
126   this.metadata_ = metadata;
127   this.isFile = true;
128   this.isDirectory = false;
129 }
130
131 MockFileEntry.prototype = {
132   __proto__: MockEntry.prototype
133 };
134
135 /**
136  * Obtains metadata of the entry.
137  * @param {function(Object)} callback Function to take the metadata.
138  */
139 MockFileEntry.prototype.getMetadata = function(callback) {
140   Promise.resolve(this.metadata_).then(callback).catch(function(error) {
141     console.error(error.stack || error);
142     window.onerror();
143   });
144 };
145
146 /**
147  * @override
148  */
149 MockFileEntry.prototype.clone = function(path) {
150   return new MockFileEntry(this.filesystem, path, this.metadata);
151 };
152
153 /**
154  * Mock class for DirectoryEntry.
155  *
156  * @param {FileSystem} filesystem File system where the entry is localed.
157  * @param {string} fullPath Full path for the entry.
158  * @extends {MockEntry}
159  * @constructor
160  */
161 function MockDirectoryEntry(filesystem, fullPath) {
162   MockEntry.call(this, filesystem, fullPath);
163   this.isFile = false;
164   this.isDirectory = true;
165 }
166
167 MockDirectoryEntry.prototype = {
168   __proto__: MockEntry.prototype
169 };
170
171 /**
172  * @override
173  */
174 MockDirectoryEntry.prototype.clone = function(path) {
175   return new MockDirectoryEntry(this.filesystem, path);
176 };
177
178 /**
179  * Returns a file under the directory.
180  *
181  * @param {string} path Path.
182  * @param {Object} option Option.
183  * @param {callback(MockFileEntry)} onSuccess Success callback.
184  * @param {callback(Object)} onError Failure callback;
185  */
186 MockDirectoryEntry.prototype.getFile = function(
187     path, option, onSuccess, onError) {
188   var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path);
189   if (!this.filesystem.entries[fullPath])
190     onError({name: util.FileError.NOT_FOUND_ERR});
191   else if (!(this.filesystem.entries[fullPath] instanceof MockFileEntry))
192     onError({name: util.FileError.TYPE_MISMATCH_ERR});
193   else
194     onSuccess(this.filesystem.entries[fullPath]);
195 };
196
197 /**
198  * Returns a directory under the directory.
199  *
200  * @param {string} path Path.
201  * @param {Object} option Option.
202  * @param {callback(MockDirectoryEntry)} onSuccess Success callback.
203  * @param {callback(Object)} onError Failure callback;
204  */
205 MockDirectoryEntry.prototype.getDirectory =
206     function(path, option, onSuccess, onError) {
207   var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path);
208   if (!this.filesystem.entries[fullPath])
209     onError({name: util.FileError.NOT_FOUND_ERR});
210   else if (!(this.filesystem.entries[fullPath] instanceof MockDirectoryEntry))
211     onError({name: util.FileError.TYPE_MISMATCH_ERR});
212   else
213     onSuccess(this.filesystem.entries[fullPath]);
214 };