Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / get_metadata / 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_ROOT = Object.freeze({
12   isDirectory: true,
13   name: '',
14   size: 0,
15   modificationTime: new Date(2013, 3, 27, 9, 38, 14)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_FILE = Object.freeze({
23   isDirectory: false,
24   name: 'tiramisu.txt',
25   size: 4096,
26   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
27 });
28
29 /**
30  * @type {Object}
31  * @const
32  */
33 var TESTING_WRONG_TIME_FILE = Object.freeze({
34   isDirectory: false,
35   name: 'invalid-time.txt',
36   size: 4096,
37   modificationTime: new Date('Invalid date.')
38 });
39
40 /**
41  * Returns metadata for a requested entry.
42  *
43  * @param {GetMetadataRequestedOptions} options Options.
44  * @param {function(Object)} onSuccess Success callback with metadata passed
45  *     an argument.
46  * @param {function(string)} onError Error callback with an error code.
47  */
48 function onGetMetadataRequested(options, onSuccess, onError) {
49   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
50     onError('SECURITY');  // enum ProviderError.
51     return;
52   }
53
54   if (options.entryPath == '/') {
55     onSuccess(TESTING_ROOT);
56     return;
57   }
58
59   if (options.entryPath == '/' + TESTING_FILE.name) {
60     onSuccess(TESTING_FILE);
61     return;
62   }
63
64   if (options.entryPath == '/' + TESTING_WRONG_TIME_FILE.name) {
65     onSuccess(TESTING_WRONG_TIME_FILE);
66     return;
67   }
68
69   onError('NOT_FOUND');  // enum ProviderError.
70 }
71
72 /**
73  * Sets up the tests. Called once per all test cases. In case of a failure,
74  * the callback is not called.
75  *
76  * @param {function()} callback Success callback.
77  */
78 function setUp(callback) {
79   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
80       onGetMetadataRequested);
81   test_util.mountFileSystem(callback);
82 }
83
84 /**
85  * Runs all of the test cases, one by one.
86  */
87 function runTests() {
88   chrome.test.runTests([
89     // Read metadata of the root.
90     function getFileMetadataSuccess() {
91       var onSuccess = chrome.test.callbackPass();
92       test_util.fileSystem.root.getMetadata(
93         function(metadata) {
94           chrome.test.assertEq(TESTING_ROOT.size, metadata.size);
95           chrome.test.assertEq(
96               TESTING_ROOT.modificationTime.toString(),
97               metadata.modificationTime.toString());
98           onSuccess();
99         }, function(error) {
100           chrome.test.fail(error.name);
101         });
102     },
103     // Read metadata of an existing testing file.
104     function getFileMetadataSuccess() {
105       var onSuccess = chrome.test.callbackPass();
106       test_util.fileSystem.root.getFile(
107           TESTING_FILE.name,
108           {create: false},
109           function(fileEntry) {
110             chrome.test.assertEq(TESTING_FILE.name, fileEntry.name);
111             chrome.test.assertEq(
112                 TESTING_FILE.isDirectory, fileEntry.isDirectory);
113             fileEntry.getMetadata(function(metadata) {
114               chrome.test.assertEq(TESTING_FILE.size, metadata.size);
115               chrome.test.assertEq(
116                   TESTING_FILE.modificationTime.toString(),
117                   metadata.modificationTime.toString());
118               onSuccess();
119             }, function(error) {
120               chrome.test.fail(error.name);
121             });
122           },
123           function(error) {
124             chrome.test.fail(error.name);
125           });
126     },
127     // Read metadata of an existing testing file, which however has an invalid
128     // modification time. It should not cause an error, but an invalid date
129     // should be passed to fileapi instead. The reason is, that there is no
130     // easy way to verify an incorrect modification time at early stage.
131     function getFileMetadataWrongTimeSuccess() {
132       var onSuccess = chrome.test.callbackPass();
133       test_util.fileSystem.root.getFile(
134           TESTING_WRONG_TIME_FILE.name,
135           {create: false},
136           function(fileEntry) {
137             chrome.test.assertEq(TESTING_WRONG_TIME_FILE.name, fileEntry.name);
138             fileEntry.getMetadata(function(metadata) {
139               chrome.test.assertTrue(
140                   Number.isNaN(metadata.modificationTime.getTime()));
141               onSuccess();
142             }, function(error) {
143               chrome.test.fail(error.name);
144             });
145           }, function(error) {
146             chrome.test.fail(error.name);
147           });
148     },
149     // Read metadata of a directory which does not exist, what should return an
150     // error. DirectoryEntry.getDirectory() causes fetching metadata.
151     function getFileMetadataNotFound() {
152       var onSuccess = chrome.test.callbackPass();
153       test_util.fileSystem.root.getDirectory(
154           'cranberries',
155           {create: false},
156           function(dirEntry) {
157             chrome.test.fail();
158           },
159           function(error) {
160             chrome.test.assertEq('NotFoundError', error.name);
161             onSuccess();
162           });
163     },
164     // Read metadata of a file using getDirectory(). An error should be returned
165     // because of type mismatching. DirectoryEntry.getDirectory() causes
166     // fetching metadata.
167     function getFileMetadataWrongType() {
168       var onSuccess = chrome.test.callbackPass();
169       test_util.fileSystem.root.getDirectory(
170           TESTING_FILE.name,
171           {create: false},
172           function(fileEntry) {
173             chrome.test.fail();
174           },
175           function(error) {
176             chrome.test.assertEq('TypeMismatchError', error.name);
177             onSuccess();
178           });
179     }
180   ]);
181 }
182
183 // Setup and run all of the test cases.
184 setUp(runTests);