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 var onSuccess = chrome.test.callbackPass();
92 test_util.fileSystem.root.getMetadata(
94 chrome.test.assertEq(TESTING_ROOT.size, metadata.size);
96 TESTING_ROOT.modificationTime.toString(),
97 metadata.modificationTime.toString());
100 chrome.test.fail(error.name);
103 // Read metadata of an existing testing file.
104 function getFileMetadataSuccess() {
105 var onSuccess = chrome.test.callbackPass();
106 test_util.fileSystem.root.getFile(
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());
120 chrome.test.fail(error.name);
124 chrome.test.fail(error.name);
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,
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()));
143 chrome.test.fail(error.name);
146 chrome.test.fail(error.name);
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(
160 chrome.test.assertEq('NotFoundError', error.name);
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(
172 function(fileEntry) {
176 chrome.test.assertEq('TypeMismatchError', error.name);
183 // Setup and run all of the test cases.