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.
11 var TESTING_ROOT = Object.freeze({
15 modificationTime: new Date(2013, 3, 27, 9, 38, 14)
22 var TESTING_FILE = Object.freeze({
26 modificationTime: new Date(2014, 4, 28, 10, 39, 15)
33 var TESTING_WRONG_TIME_FILE = Object.freeze({
35 name: 'invalid-time.txt',
37 modificationTime: new Date('Invalid date.')
41 * Returns metadata for a requested entry.
43 * @param {GetMetadataRequestedOptions} options Options.
44 * @param {function(Object)} onSuccess Success callback with metadata passed
46 * @param {function(string)} onError Error callback with an error code.
48 function onGetMetadataRequested(options, onSuccess, onError) {
49 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
50 onError('SECURITY'); // enum ProviderError.
54 if (options.entryPath == '/') {
55 onSuccess(TESTING_ROOT);
59 if (options.entryPath == '/' + TESTING_FILE.name) {
60 onSuccess(TESTING_FILE);
64 if (options.entryPath == '/' + TESTING_WRONG_TIME_FILE.name) {
65 onSuccess(TESTING_WRONG_TIME_FILE);
69 onError('NOT_FOUND'); // enum ProviderError.
73 * Sets up the tests. Called once per all test cases. In case of a failure,
74 * the callback is not called.
76 * @param {function()} callback Success callback.
78 function setUp(callback) {
79 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
80 onGetMetadataRequested);
81 test_util.mountFileSystem(callback);
85 * Runs all of the test cases, one by one.
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);
95 TESTING_ROOT.modificationTime.toString(),
96 metadata.modificationTime.toString());
98 chrome.test.fail(error.name);
102 // Read metadata of an existing testing file.
103 function getFileMetadataSuccess() {
104 test_util.fileSystem.root.getFile(
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);
121 chrome.test.fail(error.name);
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,
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);
141 }), function(error) {
142 chrome.test.fail(error.name);
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(
155 chrome.test.callbackPass(function(error) {
156 chrome.test.assertEq('NotFoundError', error.name);
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(
167 function(fileEntry) {
170 chrome.test.callbackPass(function(error) {
171 chrome.test.assertEq('TypeMismatchError', error.name);
177 // Setup and run all of the test cases.