Update To 11.40.268.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       test_util.fileSystem.root.getMetadata(
92         chrome.test.callbackPass(function(metadata) {
93           chrome.test.assertEq(TESTING_ROOT.size, metadata.size);
94           chrome.test.assertEq(
95               TESTING_ROOT.modificationTime.toString(),
96               metadata.modificationTime.toString());
97         }), function(error) {
98           chrome.test.fail(error.name);
99         });
100     },
101
102     // Read metadata of an existing testing file.
103     function getFileMetadataSuccess() {
104       test_util.fileSystem.root.getFile(
105           TESTING_FILE.name,
106           {create: false},
107           chrome.test.callbackPass(function(fileEntry) {
108             chrome.test.assertEq(TESTING_FILE.name, fileEntry.name);
109             chrome.test.assertEq(
110                 TESTING_FILE.isDirectory, fileEntry.isDirectory);
111             fileEntry.getMetadata(chrome.test.callbackPass(function(metadata) {
112               chrome.test.assertEq(TESTING_FILE.size, metadata.size);
113               chrome.test.assertEq(
114                   TESTING_FILE.modificationTime.toString(),
115                   metadata.modificationTime.toString());
116             }), function(error) {
117               chrome.test.fail(error.name);
118             });
119           }),
120           function(error) {
121             chrome.test.fail(error.name);
122           });
123     },
124
125     // Read metadata of an existing testing file, which however has an invalid
126     // modification time. It should not cause an error, but an invalid date
127     // should be passed to fileapi instead. The reason is, that there is no
128     // easy way to verify an incorrect modification time at early stage.
129     function getFileMetadataWrongTimeSuccess() {
130       test_util.fileSystem.root.getFile(
131           TESTING_WRONG_TIME_FILE.name,
132           {create: false},
133           chrome.test.callbackPass(function(fileEntry) {
134             chrome.test.assertEq(TESTING_WRONG_TIME_FILE.name, fileEntry.name);
135             fileEntry.getMetadata(chrome.test.callbackPass(function(metadata) {
136               chrome.test.assertTrue(
137                   Number.isNaN(metadata.modificationTime.getTime()));
138             }), function(error) {
139               chrome.test.fail(error.name);
140             });
141           }), function(error) {
142             chrome.test.fail(error.name);
143           });
144     },
145
146     // Read metadata of a directory which does not exist, what should return an
147     // error. DirectoryEntry.getDirectory() causes fetching metadata.
148     function getFileMetadataNotFound() {
149       test_util.fileSystem.root.getDirectory(
150           'cranberries',
151           {create: false},
152           function(dirEntry) {
153             chrome.test.fail();
154           },
155           chrome.test.callbackPass(function(error) {
156             chrome.test.assertEq('NotFoundError', error.name);
157           }));
158     },
159
160     // Read metadata of a file using getDirectory(). An error should be returned
161     // because of type mismatching. DirectoryEntry.getDirectory() causes
162     // fetching metadata.
163     function getFileMetadataWrongType() {
164       test_util.fileSystem.root.getDirectory(
165           TESTING_FILE.name,
166           {create: false},
167           function(fileEntry) {
168             chrome.test.fail();
169           },
170           chrome.test.callbackPass(function(error) {
171             chrome.test.assertEq('TypeMismatchError', error.name);
172           }));
173     }
174   ]);
175 }
176
177 // Setup and run all of the test cases.
178 setUp(runTests);